GovernorTimelockAccess.test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 { selector } = require('../../helpers/methods');
  8. const AccessManager = artifacts.require('$AccessManager');
  9. const Governor = artifacts.require('$GovernorTimelockAccessMock');
  10. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  11. const TOKENS = [
  12. // { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  13. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  14. ];
  15. const hashOperation = (caller, target, data) =>
  16. web3.utils.keccak256(web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [caller, target, data]));
  17. contract('GovernorTimelockAccess', function (accounts) {
  18. const [admin, voter1, voter2, voter3, voter4] = accounts;
  19. const name = 'OZ-Governor';
  20. const version = '1';
  21. const tokenName = 'MockToken';
  22. const tokenSymbol = 'MTKN';
  23. const tokenSupply = web3.utils.toWei('100');
  24. const votingDelay = web3.utils.toBN(4);
  25. const votingPeriod = web3.utils.toBN(16);
  26. const value = web3.utils.toWei('1');
  27. for (const { mode, Token } of TOKENS) {
  28. describe(`using ${Token._json.contractName}`, function () {
  29. beforeEach(async function () {
  30. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  31. this.manager = await AccessManager.new(admin);
  32. this.mock = await Governor.new(
  33. name,
  34. votingDelay,
  35. votingPeriod,
  36. 0, // proposal threshold
  37. this.manager.address,
  38. 0, // base delay
  39. this.token.address,
  40. 0, // quorum
  41. );
  42. this.receiver = await AccessManagedTarget.new(this.manager.address);
  43. this.helper = new GovernorHelper(this.mock, mode);
  44. await web3.eth.sendTransaction({ from: admin, to: this.mock.address, value });
  45. await this.token.$_mint(admin, tokenSupply);
  46. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: admin });
  47. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: admin });
  48. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: admin });
  49. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: admin });
  50. // default proposals
  51. this.restricted = {};
  52. this.restricted.selector = this.receiver.contract.methods.fnRestricted().encodeABI();
  53. this.restricted.operation = {
  54. target: this.receiver.address,
  55. value: '0',
  56. data: this.restricted.selector,
  57. };
  58. this.restricted.operationId = hashOperation(
  59. this.mock.address,
  60. this.restricted.operation.target,
  61. this.restricted.operation.data,
  62. );
  63. this.unrestricted = {};
  64. this.unrestricted.selector = this.receiver.contract.methods.fnUnrestricted().encodeABI();
  65. this.unrestricted.operation = {
  66. target: this.receiver.address,
  67. value: '0',
  68. data: this.unrestricted.selector,
  69. };
  70. this.unrestricted.operationId = hashOperation(
  71. this.mock.address,
  72. this.unrestricted.operation.target,
  73. this.unrestricted.operation.data,
  74. );
  75. });
  76. it('accepts ether transfers', async function () {
  77. await web3.eth.sendTransaction({ from: admin, to: this.mock.address, value: 1 });
  78. });
  79. it('post deployment check', async function () {
  80. expect(await this.mock.name()).to.be.equal(name);
  81. expect(await this.mock.token()).to.be.equal(this.token.address);
  82. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  83. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  84. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  85. expect(await this.mock.accessManager()).to.be.equal(this.manager.address);
  86. });
  87. describe('base delay only', function () {
  88. for (const [delay, queue] of [
  89. [0, true],
  90. [0, false],
  91. [1000, true],
  92. ]) {
  93. it(`delay ${delay}, ${queue ? 'with' : 'without'} queuing`, async function () {
  94. await this.mock.$_setBaseDelaySeconds(delay);
  95. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  96. await this.helper.propose();
  97. await this.helper.waitForSnapshot();
  98. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  99. await this.helper.waitForDeadline();
  100. if (queue) {
  101. const txQueue = await this.helper.queue();
  102. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  103. }
  104. if (delay > 0) {
  105. await this.helper.waitForEta();
  106. }
  107. const txExecute = await this.helper.execute();
  108. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  109. expectEvent.inTransaction(txExecute, this.receiver, 'CalledUnrestricted');
  110. });
  111. }
  112. });
  113. it('single operation with access manager delay', async function () {
  114. const delay = 1000;
  115. const roleId = '1';
  116. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  117. from: admin,
  118. });
  119. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  120. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  121. await this.helper.propose();
  122. await this.helper.waitForSnapshot();
  123. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  124. await this.helper.waitForDeadline();
  125. const txQueue = await this.helper.queue();
  126. await this.helper.waitForEta();
  127. const txExecute = await this.helper.execute();
  128. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  129. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  130. operationId: this.restricted.operationId,
  131. nonce: '1',
  132. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay),
  133. caller: this.mock.address,
  134. target: this.restricted.operation.target,
  135. data: this.restricted.operation.data,
  136. });
  137. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  138. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  139. operationId: this.restricted.operationId,
  140. nonce: '1',
  141. });
  142. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  143. });
  144. it('bundle of varied operations', async function () {
  145. const managerDelay = 1000;
  146. const roleId = '1';
  147. const baseDelay = managerDelay * 2;
  148. await this.mock.$_setBaseDelaySeconds(baseDelay);
  149. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  150. from: admin,
  151. });
  152. await this.manager.grantRole(roleId, this.mock.address, managerDelay, { from: admin });
  153. this.proposal = await this.helper.setProposal(
  154. [this.restricted.operation, this.unrestricted.operation],
  155. 'descr',
  156. );
  157. await this.helper.propose();
  158. await this.helper.waitForSnapshot();
  159. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  160. await this.helper.waitForDeadline();
  161. const txQueue = await this.helper.queue();
  162. await this.helper.waitForEta();
  163. const txExecute = await this.helper.execute();
  164. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  165. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  166. operationId: this.restricted.operationId,
  167. nonce: '1',
  168. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(baseDelay),
  169. caller: this.mock.address,
  170. target: this.restricted.operation.target,
  171. data: this.restricted.operation.data,
  172. });
  173. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  174. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  175. operationId: this.restricted.operationId,
  176. nonce: '1',
  177. });
  178. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  179. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledUnrestricted');
  180. });
  181. describe('cancel', function () {
  182. const delay = 1000;
  183. const roleId = '1';
  184. beforeEach(async function () {
  185. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  186. from: admin,
  187. });
  188. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  189. });
  190. it('cancellation after queue (internal)', async function () {
  191. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  192. await this.helper.propose();
  193. await this.helper.waitForSnapshot();
  194. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  195. await this.helper.waitForDeadline();
  196. await this.helper.queue();
  197. const txCancel = await this.helper.cancel('internal');
  198. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  199. await expectEvent.inTransaction(txCancel.tx, this.manager, 'OperationCanceled', {
  200. operationId: this.restricted.operationId,
  201. nonce: '1',
  202. });
  203. await this.helper.waitForEta();
  204. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  205. this.proposal.id,
  206. Enums.ProposalState.Canceled,
  207. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  208. ]);
  209. });
  210. it('cancel calls already canceled by guardian', async function () {
  211. const operationA = { target: this.receiver.address, data: this.restricted.selector + '00' };
  212. const operationB = { target: this.receiver.address, data: this.restricted.selector + '01' };
  213. const operationC = { target: this.receiver.address, data: this.restricted.selector + '02' };
  214. const operationAId = hashOperation(this.mock.address, operationA.target, operationA.data);
  215. const operationBId = hashOperation(this.mock.address, operationB.target, operationB.data);
  216. const proposal1 = new GovernorHelper(this.mock, mode);
  217. const proposal2 = new GovernorHelper(this.mock, mode);
  218. proposal1.setProposal([operationA, operationB], 'proposal A+B');
  219. proposal2.setProposal([operationA, operationC], 'proposal A+C');
  220. for (const p of [proposal1, proposal2]) {
  221. await p.propose();
  222. await p.waitForSnapshot();
  223. await p.vote({ support: Enums.VoteType.For }, { from: voter1 });
  224. await p.waitForDeadline();
  225. }
  226. // Can queue the first proposal
  227. await proposal1.queue();
  228. // Cannot queue the second proposal: operation A already scheduled with delay
  229. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  230. // Admin cancels operation B on the manager
  231. await this.manager.cancel(this.mock.address, operationB.target, operationB.data, { from: admin });
  232. // Still cannot queue the second proposal: operation A already scheduled with delay
  233. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  234. await proposal1.waitForEta();
  235. // Cannot execute first proposal: operation B has been canceled
  236. await expectRevertCustomError(proposal1.execute(), 'AccessManagerNotScheduled', [operationBId]);
  237. // Cancel the first proposal to release operation A
  238. await proposal1.cancel('internal');
  239. // can finally queue the second proposal
  240. await proposal2.queue();
  241. await proposal2.waitForEta();
  242. // Can execute second proposal
  243. await proposal2.execute();
  244. });
  245. });
  246. describe('ignore AccessManager', function () {
  247. it('defaults', async function () {
  248. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  249. false,
  250. );
  251. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(true);
  252. });
  253. it('internal setter', async function () {
  254. const p1 = { target: this.receiver.address, selector: this.restricted.selector, ignored: true };
  255. const tx1 = await this.mock.$_setAccessManagerIgnored(p1.target, p1.selector, p1.ignored);
  256. expect(await this.mock.isAccessManagerIgnored(p1.target, p1.selector)).to.equal(p1.ignored);
  257. expectEvent(tx1, 'AccessManagerIgnoredSet', p1);
  258. const p2 = { target: this.mock.address, selector: '0x12341234', ignored: false };
  259. const tx2 = await this.mock.$_setAccessManagerIgnored(p2.target, p2.selector, p2.ignored);
  260. expect(await this.mock.isAccessManagerIgnored(p2.target, p2.selector)).to.equal(p2.ignored);
  261. expectEvent(tx2, 'AccessManagerIgnoredSet', p2);
  262. });
  263. it('external setter', async function () {
  264. const setAccessManagerIgnored = (...args) =>
  265. this.mock.contract.methods.setAccessManagerIgnored(...args).encodeABI();
  266. await this.helper.setProposal(
  267. [
  268. {
  269. target: this.mock.address,
  270. data: setAccessManagerIgnored(
  271. this.receiver.address,
  272. [this.restricted.selector, this.unrestricted.selector],
  273. true,
  274. ),
  275. value: '0',
  276. },
  277. {
  278. target: this.mock.address,
  279. data: setAccessManagerIgnored(this.mock.address, ['0x12341234', '0x67896789'], false),
  280. value: '0',
  281. },
  282. ],
  283. 'descr',
  284. );
  285. await this.helper.propose();
  286. await this.helper.waitForSnapshot();
  287. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  288. await this.helper.waitForDeadline();
  289. const tx = await this.helper.execute();
  290. expectEvent(tx, 'AccessManagerIgnoredSet');
  291. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  292. true,
  293. );
  294. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.unrestricted.selector)).to.equal(
  295. true,
  296. );
  297. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(false);
  298. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x67896789')).to.equal(false);
  299. });
  300. it('locked function', async function () {
  301. const setAccessManagerIgnored = selector('setAccessManagerIgnored(address,bytes4[],bool)');
  302. await expectRevertCustomError(
  303. this.mock.$_setAccessManagerIgnored(this.mock.address, setAccessManagerIgnored, true),
  304. 'GovernorLockedIgnore',
  305. [],
  306. );
  307. await this.mock.$_setAccessManagerIgnored(this.receiver.address, setAccessManagerIgnored, true);
  308. });
  309. it('ignores access manager', async function () {
  310. const amount = 100;
  311. const target = this.token.address;
  312. const data = this.token.contract.methods.transfer(voter4, amount).encodeABI();
  313. const selector = data.slice(0, 10);
  314. await this.token.$_mint(this.mock.address, amount);
  315. const roleId = '1';
  316. await this.manager.setTargetFunctionRole(target, [selector], roleId, { from: admin });
  317. await this.manager.grantRole(roleId, this.mock.address, 0, { from: admin });
  318. await this.helper.setProposal([{ target, data, value: '0' }], '1');
  319. await this.helper.propose();
  320. await this.helper.waitForSnapshot();
  321. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  322. await this.helper.waitForDeadline();
  323. await expectRevertCustomError(this.helper.execute(), 'ERC20InsufficientBalance', [
  324. this.manager.address,
  325. 0,
  326. amount,
  327. ]);
  328. await this.mock.$_setAccessManagerIgnored(target, selector, true);
  329. await this.helper.setProposal([{ target, data, value: '0' }], '2');
  330. await this.helper.propose();
  331. await this.helper.waitForSnapshot();
  332. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  333. await this.helper.waitForDeadline();
  334. const tx = await this.helper.execute();
  335. expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.mock.address });
  336. });
  337. });
  338. });
  339. }
  340. });