AuthorityUtils.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { MAX_UINT32, MAX_UINT64 } = require('../../helpers/constants');
  5. async function fixture() {
  6. const [user, other] = await ethers.getSigners();
  7. const mock = await ethers.deployContract('$AuthorityUtils');
  8. const notAuthorityMock = await ethers.deployContract('NotAuthorityMock');
  9. const authorityNoDelayMock = await ethers.deployContract('AuthorityNoDelayMock');
  10. const authorityDelayMock = await ethers.deployContract('AuthorityDelayMock');
  11. const authorityNoResponse = await ethers.deployContract('AuthorityNoResponse');
  12. return {
  13. user,
  14. other,
  15. mock,
  16. notAuthorityMock,
  17. authorityNoDelayMock,
  18. authorityDelayMock,
  19. authorityNoResponse,
  20. };
  21. }
  22. describe('AuthorityUtils', function () {
  23. beforeEach(async function () {
  24. Object.assign(this, await loadFixture(fixture));
  25. });
  26. describe('canCallWithDelay', function () {
  27. describe('when authority does not have a canCall function', function () {
  28. beforeEach(async function () {
  29. this.authority = this.notAuthorityMock;
  30. });
  31. it('returns (immediate = 0, delay = 0)', async function () {
  32. const { immediate, delay } = await this.mock.$canCallWithDelay(
  33. this.authority,
  34. this.user,
  35. this.other,
  36. '0x12345678',
  37. );
  38. expect(immediate).to.be.false;
  39. expect(delay).to.equal(0n);
  40. });
  41. });
  42. describe('when authority has no delay', function () {
  43. beforeEach(async function () {
  44. this.authority = this.authorityNoDelayMock;
  45. this.immediate = true;
  46. await this.authority._setImmediate(this.immediate);
  47. });
  48. it('returns (immediate, delay = 0)', async function () {
  49. const { immediate, delay } = await this.mock.$canCallWithDelay(
  50. this.authority,
  51. this.user,
  52. this.other,
  53. '0x12345678',
  54. );
  55. expect(immediate).to.equal(this.immediate);
  56. expect(delay).to.equal(0n);
  57. });
  58. });
  59. describe('when authority replies with a delay', function () {
  60. beforeEach(async function () {
  61. this.authority = this.authorityDelayMock;
  62. });
  63. for (const immediate of [true, false]) {
  64. for (const delay of [0n, 42n, MAX_UINT32]) {
  65. it(`returns (immediate=${immediate}, delay=${delay})`, async function () {
  66. await this.authority._setImmediate(immediate);
  67. await this.authority._setDelay(delay);
  68. const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
  69. expect(result.immediate).to.equal(immediate);
  70. expect(result.delay).to.equal(delay);
  71. });
  72. }
  73. }
  74. it('out of bound delay', async function () {
  75. await this.authority._setImmediate(false);
  76. await this.authority._setDelay(MAX_UINT64); // bigger than the expected uint32
  77. const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
  78. expect(result.immediate).to.equal(false);
  79. expect(result.delay).to.equal(0n);
  80. });
  81. });
  82. describe('when authority replies with empty data', function () {
  83. beforeEach(async function () {
  84. this.authority = this.authorityNoResponse;
  85. });
  86. it('returns (immediate = 0, delay = 0)', async function () {
  87. const { immediate, delay } = await this.mock.$canCallWithDelay(
  88. this.authority,
  89. this.user,
  90. this.other,
  91. '0x12345678',
  92. );
  93. expect(immediate).to.be.false;
  94. expect(delay).to.equal(0n);
  95. });
  96. });
  97. });
  98. });