GovernorTimelockAccess.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper, proposalStatesToBitMap } = require('../../helpers/governance');
  5. const { expectRevertCustomError } = require('../../helpers/customError');
  6. const { clockFromReceipt } = require('../../helpers/time');
  7. const AccessManager = artifacts.require('$AccessManager');
  8. const Governor = artifacts.require('$GovernorTimelockAccessMock');
  9. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  10. const TOKENS = [
  11. // { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  12. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  13. ];
  14. const hashOperation = (caller, target, data) =>
  15. web3.utils.keccak256(web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [caller, target, data]));
  16. contract('GovernorTimelockAccess', function (accounts) {
  17. const [admin, voter1, voter2, voter3, voter4] = accounts;
  18. const name = 'OZ-Governor';
  19. const version = '1';
  20. const tokenName = 'MockToken';
  21. const tokenSymbol = 'MTKN';
  22. const tokenSupply = web3.utils.toWei('100');
  23. const votingDelay = web3.utils.toBN(4);
  24. const votingPeriod = web3.utils.toBN(16);
  25. const value = web3.utils.toWei('1');
  26. for (const { mode, Token } of TOKENS) {
  27. describe(`using ${Token._json.contractName}`, function () {
  28. beforeEach(async function () {
  29. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  30. this.manager = await AccessManager.new(admin);
  31. this.mock = await Governor.new(
  32. name,
  33. votingDelay,
  34. votingPeriod,
  35. 0, // proposal threshold
  36. this.manager.address,
  37. 0, // base delay
  38. this.token.address,
  39. 0, // quorum
  40. );
  41. this.receiver = await AccessManagedTarget.new(this.manager.address);
  42. this.helper = new GovernorHelper(this.mock, mode);
  43. await web3.eth.sendTransaction({ from: admin, to: this.mock.address, value });
  44. await this.token.$_mint(admin, tokenSupply);
  45. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: admin });
  46. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: admin });
  47. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: admin });
  48. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: admin });
  49. // default proposals
  50. this.restricted = {};
  51. this.restricted.selector = this.receiver.contract.methods.fnRestricted().encodeABI();
  52. this.restricted.operation = {
  53. target: this.receiver.address,
  54. value: '0',
  55. data: this.restricted.selector,
  56. };
  57. this.restricted.operationId = hashOperation(
  58. this.mock.address,
  59. this.restricted.operation.target,
  60. this.restricted.operation.data,
  61. );
  62. this.unrestricted = {};
  63. this.unrestricted.selector = this.receiver.contract.methods.fnUnrestricted().encodeABI();
  64. this.unrestricted.operation = {
  65. target: this.receiver.address,
  66. value: '0',
  67. data: this.unrestricted.selector,
  68. };
  69. this.unrestricted.operationId = hashOperation(
  70. this.mock.address,
  71. this.unrestricted.operation.target,
  72. this.unrestricted.operation.data,
  73. );
  74. });
  75. it('accepts ether transfers', async function () {
  76. await web3.eth.sendTransaction({ from: admin, to: this.mock.address, value: 1 });
  77. });
  78. it('post deployment check', async function () {
  79. expect(await this.mock.name()).to.be.equal(name);
  80. expect(await this.mock.token()).to.be.equal(this.token.address);
  81. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  82. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  83. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  84. expect(await this.mock.accessManager()).to.be.equal(this.manager.address);
  85. });
  86. describe('base delay only', function () {
  87. for (const [delay, queue] of [
  88. [0, true],
  89. [0, false],
  90. [1000, true],
  91. ]) {
  92. it(`delay ${delay}, ${queue ? 'with' : 'without'} queuing`, async function () {
  93. await this.mock.$_setBaseDelaySeconds(delay);
  94. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  95. await this.helper.propose();
  96. await this.helper.waitForSnapshot();
  97. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  98. await this.helper.waitForDeadline();
  99. if (queue) {
  100. const txQueue = await this.helper.queue();
  101. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  102. }
  103. if (delay > 0) {
  104. await this.helper.waitForEta();
  105. }
  106. const txExecute = await this.helper.execute();
  107. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  108. expectEvent.inTransaction(txExecute, this.receiver, 'CalledUnrestricted');
  109. });
  110. }
  111. });
  112. it('single operation with access manager delay', async function () {
  113. const delay = 1000;
  114. const roleId = '1';
  115. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  116. from: admin,
  117. });
  118. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  119. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  120. await this.helper.propose();
  121. await this.helper.waitForSnapshot();
  122. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  123. await this.helper.waitForDeadline();
  124. const txQueue = await this.helper.queue();
  125. await this.helper.waitForEta();
  126. const txExecute = await this.helper.execute();
  127. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  128. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  129. operationId: this.restricted.operationId,
  130. nonce: '1',
  131. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay),
  132. caller: this.mock.address,
  133. target: this.restricted.operation.target,
  134. data: this.restricted.operation.data,
  135. });
  136. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  137. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  138. operationId: this.restricted.operationId,
  139. nonce: '1',
  140. });
  141. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  142. });
  143. it('bundle of varied operations', async function () {
  144. const managerDelay = 1000;
  145. const roleId = '1';
  146. const baseDelay = managerDelay * 2;
  147. await this.mock.$_setBaseDelaySeconds(baseDelay);
  148. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  149. from: admin,
  150. });
  151. await this.manager.grantRole(roleId, this.mock.address, managerDelay, { from: admin });
  152. this.proposal = await this.helper.setProposal(
  153. [this.restricted.operation, this.unrestricted.operation],
  154. 'descr',
  155. );
  156. await this.helper.propose();
  157. await this.helper.waitForSnapshot();
  158. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  159. await this.helper.waitForDeadline();
  160. const txQueue = await this.helper.queue();
  161. await this.helper.waitForEta();
  162. const txExecute = await this.helper.execute();
  163. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  164. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  165. operationId: this.restricted.operationId,
  166. nonce: '1',
  167. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(baseDelay),
  168. caller: this.mock.address,
  169. target: this.restricted.operation.target,
  170. data: this.restricted.operation.data,
  171. });
  172. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  173. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  174. operationId: this.restricted.operationId,
  175. nonce: '1',
  176. });
  177. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  178. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledUnrestricted');
  179. });
  180. it('cancellation after queue (internal)', async function () {
  181. const delay = 1000;
  182. const roleId = '1';
  183. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  184. from: admin,
  185. });
  186. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  187. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  188. await this.helper.propose();
  189. await this.helper.waitForSnapshot();
  190. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  191. await this.helper.waitForDeadline();
  192. await this.helper.queue();
  193. const txCancel = await this.helper.cancel('internal');
  194. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  195. await expectEvent.inTransaction(txCancel.tx, this.manager, 'OperationCanceled', {
  196. operationId: this.restricted.operationId,
  197. nonce: '1',
  198. });
  199. await this.helper.waitForEta();
  200. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  201. this.proposal.id,
  202. Enums.ProposalState.Canceled,
  203. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  204. ]);
  205. });
  206. });
  207. }
  208. });