GovernorTimelockAccess.test.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. const { expectEvent, time } = 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, other] = 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. this.fallback = {};
  76. this.fallback.operation = {
  77. target: this.receiver.address,
  78. value: '0',
  79. data: '0x1234',
  80. };
  81. this.fallback.operationId = hashOperation(
  82. this.mock.address,
  83. this.fallback.operation.target,
  84. this.fallback.operation.data,
  85. );
  86. });
  87. it('accepts ether transfers', async function () {
  88. await web3.eth.sendTransaction({ from: admin, to: this.mock.address, value: 1 });
  89. });
  90. it('post deployment check', async function () {
  91. expect(await this.mock.name()).to.be.equal(name);
  92. expect(await this.mock.token()).to.be.equal(this.token.address);
  93. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  94. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  95. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  96. expect(await this.mock.accessManager()).to.be.equal(this.manager.address);
  97. });
  98. it('sets base delay (seconds)', async function () {
  99. const baseDelay = time.duration.hours(10);
  100. // Only through governance
  101. await expectRevertCustomError(
  102. this.mock.setBaseDelaySeconds(baseDelay, { from: voter1 }),
  103. 'GovernorOnlyExecutor',
  104. [voter1],
  105. );
  106. this.proposal = await this.helper.setProposal(
  107. [
  108. {
  109. target: this.mock.address,
  110. value: '0',
  111. data: this.mock.contract.methods.setBaseDelaySeconds(baseDelay).encodeABI(),
  112. },
  113. ],
  114. 'descr',
  115. );
  116. await this.helper.propose();
  117. await this.helper.waitForSnapshot();
  118. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  119. await this.helper.waitForDeadline();
  120. const receipt = await this.helper.execute();
  121. expectEvent(receipt, 'BaseDelaySet', {
  122. oldBaseDelaySeconds: '0',
  123. newBaseDelaySeconds: baseDelay,
  124. });
  125. expect(await this.mock.baseDelaySeconds()).to.be.bignumber.eq(baseDelay);
  126. });
  127. it('sets access manager ignored', async function () {
  128. const selectors = ['0x12345678', '0x87654321', '0xabcdef01'];
  129. // Only through governance
  130. await expectRevertCustomError(
  131. this.mock.setAccessManagerIgnored(other, selectors, true, { from: voter1 }),
  132. 'GovernorOnlyExecutor',
  133. [voter1],
  134. );
  135. // Ignore
  136. const helperIgnore = new GovernorHelper(this.mock, mode);
  137. await helperIgnore.setProposal(
  138. [
  139. {
  140. target: this.mock.address,
  141. value: '0',
  142. data: this.mock.contract.methods.setAccessManagerIgnored(other, selectors, true).encodeABI(),
  143. },
  144. ],
  145. 'descr',
  146. );
  147. await helperIgnore.propose();
  148. await helperIgnore.waitForSnapshot();
  149. await helperIgnore.vote({ support: Enums.VoteType.For }, { from: voter1 });
  150. await helperIgnore.waitForDeadline();
  151. const ignoreReceipt = await helperIgnore.execute();
  152. for (const selector of selectors) {
  153. expectEvent(ignoreReceipt, 'AccessManagerIgnoredSet', {
  154. target: other,
  155. selector,
  156. ignored: true,
  157. });
  158. expect(await this.mock.isAccessManagerIgnored(other, selector)).to.be.true;
  159. }
  160. // Unignore
  161. const helperUnignore = new GovernorHelper(this.mock, mode);
  162. await helperUnignore.setProposal(
  163. [
  164. {
  165. target: this.mock.address,
  166. value: '0',
  167. data: this.mock.contract.methods.setAccessManagerIgnored(other, selectors, false).encodeABI(),
  168. },
  169. ],
  170. 'descr',
  171. );
  172. await helperUnignore.propose();
  173. await helperUnignore.waitForSnapshot();
  174. await helperUnignore.vote({ support: Enums.VoteType.For }, { from: voter1 });
  175. await helperUnignore.waitForDeadline();
  176. const unignoreReceipt = await helperUnignore.execute();
  177. for (const selector of selectors) {
  178. expectEvent(unignoreReceipt, 'AccessManagerIgnoredSet', {
  179. target: other,
  180. selector,
  181. ignored: false,
  182. });
  183. expect(await this.mock.isAccessManagerIgnored(other, selector)).to.be.false;
  184. }
  185. });
  186. it('sets access manager ignored when target is the governor', async function () {
  187. const other = this.mock.address;
  188. const selectors = ['0x12345678', '0x87654321', '0xabcdef01'];
  189. await this.helper.setProposal(
  190. [
  191. {
  192. target: this.mock.address,
  193. value: '0',
  194. data: this.mock.contract.methods.setAccessManagerIgnored(other, selectors, true).encodeABI(),
  195. },
  196. ],
  197. 'descr',
  198. );
  199. await this.helper.propose();
  200. await this.helper.waitForSnapshot();
  201. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  202. await this.helper.waitForDeadline();
  203. const receipt = await this.helper.execute();
  204. for (const selector of selectors) {
  205. expectEvent(receipt, 'AccessManagerIgnoredSet', {
  206. target: other,
  207. selector,
  208. ignored: true,
  209. });
  210. expect(await this.mock.isAccessManagerIgnored(other, selector)).to.be.true;
  211. }
  212. });
  213. describe('base delay only', function () {
  214. for (const [delay, queue] of [
  215. [0, true],
  216. [0, false],
  217. [1000, true],
  218. ]) {
  219. it(`delay ${delay}, ${queue ? 'with' : 'without'} queuing`, async function () {
  220. await this.mock.$_setBaseDelaySeconds(delay);
  221. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  222. await this.helper.propose();
  223. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  224. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  225. expect(indirect).to.deep.eq([false]);
  226. expect(withDelay).to.deep.eq([false]);
  227. await this.helper.waitForSnapshot();
  228. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  229. await this.helper.waitForDeadline();
  230. if (await this.mock.proposalNeedsQueuing(this.proposal.id)) {
  231. const txQueue = await this.helper.queue();
  232. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  233. }
  234. if (delay > 0) {
  235. await this.helper.waitForEta();
  236. }
  237. const txExecute = await this.helper.execute();
  238. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  239. expectEvent.inTransaction(txExecute, this.receiver, 'CalledUnrestricted');
  240. });
  241. }
  242. });
  243. it('reverts when an operation is executed before eta', async function () {
  244. const delay = time.duration.hours(2);
  245. await this.mock.$_setBaseDelaySeconds(delay);
  246. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  247. await this.helper.propose();
  248. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(true);
  249. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  250. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  251. expect(indirect).to.deep.eq([false]);
  252. expect(withDelay).to.deep.eq([false]);
  253. await this.helper.waitForSnapshot();
  254. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  255. await this.helper.waitForDeadline();
  256. await this.helper.queue();
  257. await expectRevertCustomError(this.helper.execute(), 'GovernorUnmetDelay', [
  258. this.proposal.id,
  259. await this.mock.proposalEta(this.proposal.id),
  260. ]);
  261. });
  262. it('reverts with a proposal including multiple operations but one of those was cancelled in the manager', async function () {
  263. const delay = time.duration.hours(2);
  264. const roleId = '1';
  265. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  266. from: admin,
  267. });
  268. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  269. // Set proposals
  270. const original = new GovernorHelper(this.mock, mode);
  271. await original.setProposal([this.restricted.operation, this.unrestricted.operation], 'descr');
  272. // Go through all the governance process
  273. await original.propose();
  274. expect(await this.mock.proposalNeedsQueuing(original.currentProposal.id)).to.be.eq(true);
  275. const {
  276. delay: planDelay,
  277. indirect,
  278. withDelay,
  279. } = await this.mock.proposalExecutionPlan(original.currentProposal.id);
  280. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  281. expect(indirect).to.deep.eq([true, false]);
  282. expect(withDelay).to.deep.eq([true, false]);
  283. await original.waitForSnapshot();
  284. await original.vote({ support: Enums.VoteType.For }, { from: voter1 });
  285. await original.waitForDeadline();
  286. await original.queue();
  287. await original.waitForEta();
  288. // Suddenly cancel one of the proposed operations in the manager
  289. await this.manager.cancel(this.mock.address, this.restricted.operation.target, this.restricted.operation.data, {
  290. from: admin,
  291. });
  292. // Reschedule the same operation in a different proposal to avoid "AccessManagerNotScheduled" error
  293. const rescheduled = new GovernorHelper(this.mock, mode);
  294. await rescheduled.setProposal([this.restricted.operation], 'descr');
  295. await rescheduled.propose();
  296. await rescheduled.waitForSnapshot();
  297. await rescheduled.vote({ support: Enums.VoteType.For }, { from: voter1 });
  298. await rescheduled.waitForDeadline();
  299. await rescheduled.queue(); // This will schedule it again in the manager
  300. await rescheduled.waitForEta();
  301. // Attempt to execute
  302. await expectRevertCustomError(original.execute(), 'GovernorMismatchedNonce', [
  303. original.currentProposal.id,
  304. 1,
  305. 2,
  306. ]);
  307. });
  308. it('single operation with access manager delay', async function () {
  309. const delay = 1000;
  310. const roleId = '1';
  311. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  312. from: admin,
  313. });
  314. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  315. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  316. await this.helper.propose();
  317. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(true);
  318. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  319. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  320. expect(indirect).to.deep.eq([true]);
  321. expect(withDelay).to.deep.eq([true]);
  322. await this.helper.waitForSnapshot();
  323. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  324. await this.helper.waitForDeadline();
  325. const txQueue = await this.helper.queue();
  326. await this.helper.waitForEta();
  327. const txExecute = await this.helper.execute();
  328. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  329. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  330. operationId: this.restricted.operationId,
  331. nonce: '1',
  332. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay),
  333. caller: this.mock.address,
  334. target: this.restricted.operation.target,
  335. data: this.restricted.operation.data,
  336. });
  337. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  338. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  339. operationId: this.restricted.operationId,
  340. nonce: '1',
  341. });
  342. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  343. });
  344. it('bundle of varied operations', async function () {
  345. const managerDelay = 1000;
  346. const roleId = '1';
  347. const baseDelay = managerDelay * 2;
  348. await this.mock.$_setBaseDelaySeconds(baseDelay);
  349. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  350. from: admin,
  351. });
  352. await this.manager.grantRole(roleId, this.mock.address, managerDelay, { from: admin });
  353. this.proposal = await this.helper.setProposal(
  354. [this.restricted.operation, this.unrestricted.operation, this.fallback.operation],
  355. 'descr',
  356. );
  357. await this.helper.propose();
  358. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(true);
  359. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  360. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(baseDelay));
  361. expect(indirect).to.deep.eq([true, false, false]);
  362. expect(withDelay).to.deep.eq([true, false, false]);
  363. await this.helper.waitForSnapshot();
  364. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  365. await this.helper.waitForDeadline();
  366. const txQueue = await this.helper.queue();
  367. await this.helper.waitForEta();
  368. const txExecute = await this.helper.execute();
  369. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  370. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  371. operationId: this.restricted.operationId,
  372. nonce: '1',
  373. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(baseDelay),
  374. caller: this.mock.address,
  375. target: this.restricted.operation.target,
  376. data: this.restricted.operation.data,
  377. });
  378. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  379. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  380. operationId: this.restricted.operationId,
  381. nonce: '1',
  382. });
  383. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  384. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledUnrestricted');
  385. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledFallback');
  386. });
  387. describe('cancel', function () {
  388. const delay = 1000;
  389. const roleId = '1';
  390. beforeEach(async function () {
  391. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  392. from: admin,
  393. });
  394. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  395. });
  396. it('cancels restricted with delay after queue (internal)', async function () {
  397. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  398. await this.helper.propose();
  399. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(true);
  400. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  401. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  402. expect(indirect).to.deep.eq([true]);
  403. expect(withDelay).to.deep.eq([true]);
  404. await this.helper.waitForSnapshot();
  405. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  406. await this.helper.waitForDeadline();
  407. await this.helper.queue();
  408. const txCancel = await this.helper.cancel('internal');
  409. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  410. await expectEvent.inTransaction(txCancel.tx, this.manager, 'OperationCanceled', {
  411. operationId: this.restricted.operationId,
  412. nonce: '1',
  413. });
  414. await this.helper.waitForEta();
  415. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  416. this.proposal.id,
  417. Enums.ProposalState.Canceled,
  418. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  419. ]);
  420. });
  421. it('cancels restricted with queueing if the same operation is part of a more recent proposal (internal)', async function () {
  422. // Set proposals
  423. const original = new GovernorHelper(this.mock, mode);
  424. await original.setProposal([this.restricted.operation], 'descr');
  425. // Go through all the governance process
  426. await original.propose();
  427. expect(await this.mock.proposalNeedsQueuing(original.currentProposal.id)).to.be.eq(true);
  428. const {
  429. delay: planDelay,
  430. indirect,
  431. withDelay,
  432. } = await this.mock.proposalExecutionPlan(original.currentProposal.id);
  433. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(delay));
  434. expect(indirect).to.deep.eq([true]);
  435. expect(withDelay).to.deep.eq([true]);
  436. await original.waitForSnapshot();
  437. await original.vote({ support: Enums.VoteType.For }, { from: voter1 });
  438. await original.waitForDeadline();
  439. await original.queue();
  440. // Cancel the operation in the manager
  441. await this.manager.cancel(
  442. this.mock.address,
  443. this.restricted.operation.target,
  444. this.restricted.operation.data,
  445. { from: admin },
  446. );
  447. // Another proposal is added with the same operation
  448. const rescheduled = new GovernorHelper(this.mock, mode);
  449. await rescheduled.setProposal([this.restricted.operation], 'another descr');
  450. // Queue the new proposal
  451. await rescheduled.propose();
  452. await rescheduled.waitForSnapshot();
  453. await rescheduled.vote({ support: Enums.VoteType.For }, { from: voter1 });
  454. await rescheduled.waitForDeadline();
  455. await rescheduled.queue(); // This will schedule it again in the manager
  456. // Cancel
  457. const eta = await this.mock.proposalEta(rescheduled.currentProposal.id);
  458. const txCancel = await original.cancel('internal');
  459. expectEvent(txCancel, 'ProposalCanceled', { proposalId: original.currentProposal.id });
  460. await time.increase(eta); // waitForEta()
  461. await expectRevertCustomError(original.execute(), 'GovernorUnexpectedProposalState', [
  462. original.currentProposal.id,
  463. Enums.ProposalState.Canceled,
  464. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  465. ]);
  466. });
  467. it('cancels unrestricted with queueing (internal)', async function () {
  468. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  469. await this.helper.propose();
  470. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(false);
  471. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  472. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN('0'));
  473. expect(indirect).to.deep.eq([false]);
  474. expect(withDelay).to.deep.eq([false]);
  475. await this.helper.waitForSnapshot();
  476. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  477. await this.helper.waitForDeadline();
  478. await this.helper.queue();
  479. const eta = await this.mock.proposalEta(this.proposal.id);
  480. const txCancel = await this.helper.cancel('internal');
  481. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  482. await time.increase(eta); // waitForEta()
  483. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  484. this.proposal.id,
  485. Enums.ProposalState.Canceled,
  486. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  487. ]);
  488. });
  489. it('cancels unrestricted without queueing (internal)', async function () {
  490. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  491. await this.helper.propose();
  492. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(false);
  493. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  494. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN('0'));
  495. expect(indirect).to.deep.eq([false]);
  496. expect(withDelay).to.deep.eq([false]);
  497. await this.helper.waitForSnapshot();
  498. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  499. await this.helper.waitForDeadline();
  500. // await this.helper.queue();
  501. // const eta = await this.mock.proposalEta(this.proposal.id);
  502. const txCancel = await this.helper.cancel('internal');
  503. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  504. // await time.increase(eta); // waitForEta()
  505. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  506. this.proposal.id,
  507. Enums.ProposalState.Canceled,
  508. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  509. ]);
  510. });
  511. it('cancels calls already canceled by guardian', async function () {
  512. const operationA = { target: this.receiver.address, data: this.restricted.selector + '00' };
  513. const operationB = { target: this.receiver.address, data: this.restricted.selector + '01' };
  514. const operationC = { target: this.receiver.address, data: this.restricted.selector + '02' };
  515. const operationAId = hashOperation(this.mock.address, operationA.target, operationA.data);
  516. const operationBId = hashOperation(this.mock.address, operationB.target, operationB.data);
  517. const proposal1 = new GovernorHelper(this.mock, mode);
  518. const proposal2 = new GovernorHelper(this.mock, mode);
  519. proposal1.setProposal([operationA, operationB], 'proposal A+B');
  520. proposal2.setProposal([operationA, operationC], 'proposal A+C');
  521. for (const p of [proposal1, proposal2]) {
  522. await p.propose();
  523. await p.waitForSnapshot();
  524. await p.vote({ support: Enums.VoteType.For }, { from: voter1 });
  525. await p.waitForDeadline();
  526. }
  527. // Can queue the first proposal
  528. await proposal1.queue();
  529. // Cannot queue the second proposal: operation A already scheduled with delay
  530. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  531. // Admin cancels operation B on the manager
  532. await this.manager.cancel(this.mock.address, operationB.target, operationB.data, { from: admin });
  533. // Still cannot queue the second proposal: operation A already scheduled with delay
  534. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  535. await proposal1.waitForEta();
  536. // Cannot execute first proposal: operation B has been canceled
  537. await expectRevertCustomError(proposal1.execute(), 'AccessManagerNotScheduled', [operationBId]);
  538. // Cancel the first proposal to release operation A
  539. await proposal1.cancel('internal');
  540. // can finally queue the second proposal
  541. await proposal2.queue();
  542. await proposal2.waitForEta();
  543. // Can execute second proposal
  544. await proposal2.execute();
  545. });
  546. });
  547. describe('ignore AccessManager', function () {
  548. it('defaults', async function () {
  549. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  550. false,
  551. );
  552. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(true);
  553. });
  554. it('internal setter', async function () {
  555. const p1 = { target: this.receiver.address, selector: this.restricted.selector, ignored: true };
  556. const tx1 = await this.mock.$_setAccessManagerIgnored(p1.target, p1.selector, p1.ignored);
  557. expect(await this.mock.isAccessManagerIgnored(p1.target, p1.selector)).to.equal(p1.ignored);
  558. expectEvent(tx1, 'AccessManagerIgnoredSet', p1);
  559. const p2 = { target: this.mock.address, selector: '0x12341234', ignored: false };
  560. const tx2 = await this.mock.$_setAccessManagerIgnored(p2.target, p2.selector, p2.ignored);
  561. expect(await this.mock.isAccessManagerIgnored(p2.target, p2.selector)).to.equal(p2.ignored);
  562. expectEvent(tx2, 'AccessManagerIgnoredSet', p2);
  563. });
  564. it('external setter', async function () {
  565. const setAccessManagerIgnored = (...args) =>
  566. this.mock.contract.methods.setAccessManagerIgnored(...args).encodeABI();
  567. await this.helper.setProposal(
  568. [
  569. {
  570. target: this.mock.address,
  571. data: setAccessManagerIgnored(
  572. this.receiver.address,
  573. [this.restricted.selector, this.unrestricted.selector],
  574. true,
  575. ),
  576. value: '0',
  577. },
  578. {
  579. target: this.mock.address,
  580. data: setAccessManagerIgnored(this.mock.address, ['0x12341234', '0x67896789'], false),
  581. value: '0',
  582. },
  583. ],
  584. 'descr',
  585. );
  586. await this.helper.propose();
  587. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(false);
  588. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  589. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN('0'));
  590. expect(indirect).to.deep.eq([]); // Governor operations ignore access manager
  591. expect(withDelay).to.deep.eq([]); // Governor operations ignore access manager
  592. await this.helper.waitForSnapshot();
  593. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  594. await this.helper.waitForDeadline();
  595. const tx = await this.helper.execute();
  596. expectEvent(tx, 'AccessManagerIgnoredSet');
  597. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  598. true,
  599. );
  600. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.unrestricted.selector)).to.equal(
  601. true,
  602. );
  603. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(false);
  604. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x67896789')).to.equal(false);
  605. });
  606. it('locked function', async function () {
  607. const setAccessManagerIgnored = selector('setAccessManagerIgnored(address,bytes4[],bool)');
  608. await expectRevertCustomError(
  609. this.mock.$_setAccessManagerIgnored(this.mock.address, setAccessManagerIgnored, true),
  610. 'GovernorLockedIgnore',
  611. [],
  612. );
  613. await this.mock.$_setAccessManagerIgnored(this.receiver.address, setAccessManagerIgnored, true);
  614. });
  615. it('ignores access manager', async function () {
  616. const amount = 100;
  617. const target = this.token.address;
  618. const data = this.token.contract.methods.transfer(voter4, amount).encodeABI();
  619. const selector = data.slice(0, 10);
  620. await this.token.$_mint(this.mock.address, amount);
  621. const roleId = '1';
  622. await this.manager.setTargetFunctionRole(target, [selector], roleId, { from: admin });
  623. await this.manager.grantRole(roleId, this.mock.address, 0, { from: admin });
  624. const proposal = await this.helper.setProposal([{ target, data, value: '0' }], '1');
  625. await this.helper.propose();
  626. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(false);
  627. const plan = await this.mock.proposalExecutionPlan(proposal.id);
  628. expect(plan.delay).to.be.bignumber.eq(web3.utils.toBN('0'));
  629. expect(plan.indirect).to.deep.eq([true]);
  630. expect(plan.withDelay).to.deep.eq([false]);
  631. await this.helper.waitForSnapshot();
  632. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  633. await this.helper.waitForDeadline();
  634. await expectRevertCustomError(this.helper.execute(), 'ERC20InsufficientBalance', [
  635. this.manager.address,
  636. 0,
  637. amount,
  638. ]);
  639. await this.mock.$_setAccessManagerIgnored(target, selector, true);
  640. const proposalIgnored = await this.helper.setProposal([{ target, data, value: '0' }], '2');
  641. await this.helper.propose();
  642. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.eq(false);
  643. const planIgnored = await this.mock.proposalExecutionPlan(proposalIgnored.id);
  644. expect(planIgnored.delay).to.be.bignumber.eq(web3.utils.toBN('0'));
  645. expect(planIgnored.indirect).to.deep.eq([false]);
  646. expect(planIgnored.withDelay).to.deep.eq([false]);
  647. await this.helper.waitForSnapshot();
  648. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  649. await this.helper.waitForDeadline();
  650. const tx = await this.helper.execute();
  651. expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.mock.address });
  652. });
  653. });
  654. });
  655. }
  656. });