GovernorTimelockAccess.test.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 { hashOperation } = require('../../helpers/access-manager');
  9. const AccessManager = artifacts.require('$AccessManager');
  10. const Governor = artifacts.require('$GovernorTimelockAccessMock');
  11. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  12. const Ownable = artifacts.require('$Ownable');
  13. const TOKENS = [
  14. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  15. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  16. ];
  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. it('does not need to queue proposals with no delay', async function () {
  214. const roleId = '1';
  215. const executionDelay = web3.utils.toBN(0);
  216. const baseDelay = web3.utils.toBN(0);
  217. // Set execution delay
  218. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  219. from: admin,
  220. });
  221. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  222. // Set base delay
  223. await this.mock.$_setBaseDelaySeconds(baseDelay);
  224. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  225. await this.helper.propose();
  226. expect(await this.mock.proposalNeedsQueuing(this.helper.currentProposal.id)).to.be.false;
  227. });
  228. it('needs to queue proposals with any delay', async function () {
  229. const roleId = '1';
  230. const delays = [
  231. [time.duration.hours(1), time.duration.hours(2)],
  232. [time.duration.hours(2), time.duration.hours(1)],
  233. ];
  234. for (const [executionDelay, baseDelay] of delays) {
  235. // Set execution delay
  236. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  237. from: admin,
  238. });
  239. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  240. // Set base delay
  241. await this.mock.$_setBaseDelaySeconds(baseDelay);
  242. const helper = new GovernorHelper(this.mock, mode);
  243. this.proposal = await helper.setProposal(
  244. [this.restricted.operation],
  245. `executionDelay=${executionDelay.toString()}}baseDelay=${baseDelay.toString()}}`,
  246. );
  247. await helper.propose();
  248. expect(await this.mock.proposalNeedsQueuing(helper.currentProposal.id)).to.be.true;
  249. }
  250. });
  251. describe('execution plan', function () {
  252. it('returns plan for delayed operations', async function () {
  253. const roleId = '1';
  254. const delays = [
  255. [time.duration.hours(1), time.duration.hours(2)],
  256. [time.duration.hours(2), time.duration.hours(1)],
  257. ];
  258. for (const [executionDelay, baseDelay] of delays) {
  259. // Set execution delay
  260. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  261. from: admin,
  262. });
  263. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  264. // Set base delay
  265. await this.mock.$_setBaseDelaySeconds(baseDelay);
  266. const helper = new GovernorHelper(this.mock, mode);
  267. this.proposal = await helper.setProposal(
  268. [this.restricted.operation],
  269. `executionDelay=${executionDelay.toString()}}baseDelay=${baseDelay.toString()}}`,
  270. );
  271. await helper.propose();
  272. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  273. const maxDelay = web3.utils.toBN(Math.max(baseDelay.toNumber(), executionDelay.toNumber()));
  274. expect(planDelay).to.be.bignumber.eq(maxDelay);
  275. expect(indirect).to.deep.eq([true]);
  276. expect(withDelay).to.deep.eq([true]);
  277. }
  278. });
  279. it('returns plan for not delayed operations', async function () {
  280. const roleId = '1';
  281. const executionDelay = web3.utils.toBN(0);
  282. const baseDelay = web3.utils.toBN(0);
  283. // Set execution delay
  284. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  285. from: admin,
  286. });
  287. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  288. // Set base delay
  289. await this.mock.$_setBaseDelaySeconds(baseDelay);
  290. this.proposal = await this.helper.setProposal([this.restricted.operation], `descr`);
  291. await this.helper.propose();
  292. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  293. expect(planDelay).to.be.bignumber.eq(web3.utils.toBN(0));
  294. expect(indirect).to.deep.eq([true]);
  295. expect(withDelay).to.deep.eq([false]);
  296. });
  297. it('returns plan for an operation ignoring the manager', async function () {
  298. await this.mock.$_setAccessManagerIgnored(this.receiver.address, this.restricted.selector, true);
  299. const roleId = '1';
  300. const delays = [
  301. [time.duration.hours(1), time.duration.hours(2)],
  302. [time.duration.hours(2), time.duration.hours(1)],
  303. ];
  304. for (const [executionDelay, baseDelay] of delays) {
  305. // Set execution delay
  306. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  307. from: admin,
  308. });
  309. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  310. // Set base delay
  311. await this.mock.$_setBaseDelaySeconds(baseDelay);
  312. const helper = new GovernorHelper(this.mock, mode);
  313. this.proposal = await helper.setProposal(
  314. [this.restricted.operation],
  315. `executionDelay=${executionDelay.toString()}}baseDelay=${baseDelay.toString()}}`,
  316. );
  317. await helper.propose();
  318. const { delay: planDelay, indirect, withDelay } = await this.mock.proposalExecutionPlan(this.proposal.id);
  319. expect(planDelay).to.be.bignumber.eq(baseDelay);
  320. expect(indirect).to.deep.eq([false]);
  321. expect(withDelay).to.deep.eq([false]);
  322. }
  323. });
  324. });
  325. describe('base delay only', function () {
  326. for (const [delay, queue] of [
  327. [0, true],
  328. [0, false],
  329. [1000, true],
  330. ]) {
  331. it(`delay ${delay}, ${queue ? 'with' : 'without'} queuing`, async function () {
  332. await this.mock.$_setBaseDelaySeconds(delay);
  333. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  334. await this.helper.propose();
  335. await this.helper.waitForSnapshot();
  336. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  337. await this.helper.waitForDeadline();
  338. if (await this.mock.proposalNeedsQueuing(this.proposal.id)) {
  339. const txQueue = await this.helper.queue();
  340. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  341. }
  342. if (delay > 0) {
  343. await this.helper.waitForEta();
  344. }
  345. const txExecute = await this.helper.execute();
  346. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  347. expectEvent.inTransaction(txExecute, this.receiver, 'CalledUnrestricted');
  348. });
  349. }
  350. });
  351. it('reverts when an operation is executed before eta', async function () {
  352. const delay = time.duration.hours(2);
  353. await this.mock.$_setBaseDelaySeconds(delay);
  354. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  355. await this.helper.propose();
  356. await this.helper.waitForSnapshot();
  357. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  358. await this.helper.waitForDeadline();
  359. await this.helper.queue();
  360. await expectRevertCustomError(this.helper.execute(), 'GovernorUnmetDelay', [
  361. this.proposal.id,
  362. await this.mock.proposalEta(this.proposal.id),
  363. ]);
  364. });
  365. it('reverts with a proposal including multiple operations but one of those was cancelled in the manager', async function () {
  366. const delay = time.duration.hours(2);
  367. const roleId = '1';
  368. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  369. from: admin,
  370. });
  371. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  372. // Set proposals
  373. const original = new GovernorHelper(this.mock, mode);
  374. await original.setProposal([this.restricted.operation, this.unrestricted.operation], 'descr');
  375. // Go through all the governance process
  376. await original.propose();
  377. await original.waitForSnapshot();
  378. await original.vote({ support: Enums.VoteType.For }, { from: voter1 });
  379. await original.waitForDeadline();
  380. await original.queue();
  381. await original.waitForEta();
  382. // Suddenly cancel one of the proposed operations in the manager
  383. await this.manager.cancel(this.mock.address, this.restricted.operation.target, this.restricted.operation.data, {
  384. from: admin,
  385. });
  386. // Reschedule the same operation in a different proposal to avoid "AccessManagerNotScheduled" error
  387. const rescheduled = new GovernorHelper(this.mock, mode);
  388. await rescheduled.setProposal([this.restricted.operation], 'descr');
  389. await rescheduled.propose();
  390. await rescheduled.waitForSnapshot();
  391. await rescheduled.vote({ support: Enums.VoteType.For }, { from: voter1 });
  392. await rescheduled.waitForDeadline();
  393. await rescheduled.queue(); // This will schedule it again in the manager
  394. await rescheduled.waitForEta();
  395. // Attempt to execute
  396. await expectRevertCustomError(original.execute(), 'GovernorMismatchedNonce', [
  397. original.currentProposal.id,
  398. 1,
  399. 2,
  400. ]);
  401. });
  402. it('single operation with access manager delay', async function () {
  403. const delay = 1000;
  404. const roleId = '1';
  405. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  406. from: admin,
  407. });
  408. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  409. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  410. await this.helper.propose();
  411. await this.helper.waitForSnapshot();
  412. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  413. await this.helper.waitForDeadline();
  414. const txQueue = await this.helper.queue();
  415. await this.helper.waitForEta();
  416. const txExecute = await this.helper.execute();
  417. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  418. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  419. operationId: this.restricted.operationId,
  420. nonce: '1',
  421. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay),
  422. caller: this.mock.address,
  423. target: this.restricted.operation.target,
  424. data: this.restricted.operation.data,
  425. });
  426. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  427. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  428. operationId: this.restricted.operationId,
  429. nonce: '1',
  430. });
  431. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  432. });
  433. it('bundle of varied operations', async function () {
  434. const managerDelay = 1000;
  435. const roleId = '1';
  436. const baseDelay = managerDelay * 2;
  437. await this.mock.$_setBaseDelaySeconds(baseDelay);
  438. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  439. from: admin,
  440. });
  441. await this.manager.grantRole(roleId, this.mock.address, managerDelay, { from: admin });
  442. this.proposal = await this.helper.setProposal(
  443. [this.restricted.operation, this.unrestricted.operation, this.fallback.operation],
  444. 'descr',
  445. );
  446. await this.helper.propose();
  447. await this.helper.waitForSnapshot();
  448. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  449. await this.helper.waitForDeadline();
  450. const txQueue = await this.helper.queue();
  451. await this.helper.waitForEta();
  452. const txExecute = await this.helper.execute();
  453. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  454. await expectEvent.inTransaction(txQueue.tx, this.manager, 'OperationScheduled', {
  455. operationId: this.restricted.operationId,
  456. nonce: '1',
  457. schedule: web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(baseDelay),
  458. caller: this.mock.address,
  459. target: this.restricted.operation.target,
  460. data: this.restricted.operation.data,
  461. });
  462. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  463. await expectEvent.inTransaction(txExecute.tx, this.manager, 'OperationExecuted', {
  464. operationId: this.restricted.operationId,
  465. nonce: '1',
  466. });
  467. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledRestricted');
  468. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledUnrestricted');
  469. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'CalledFallback');
  470. });
  471. describe('cancel', function () {
  472. const delay = 1000;
  473. const roleId = '1';
  474. beforeEach(async function () {
  475. await this.manager.setTargetFunctionRole(this.receiver.address, [this.restricted.selector], roleId, {
  476. from: admin,
  477. });
  478. await this.manager.grantRole(roleId, this.mock.address, delay, { from: admin });
  479. });
  480. it('cancels restricted with delay after queue (internal)', async function () {
  481. this.proposal = await this.helper.setProposal([this.restricted.operation], 'descr');
  482. await this.helper.propose();
  483. await this.helper.waitForSnapshot();
  484. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  485. await this.helper.waitForDeadline();
  486. await this.helper.queue();
  487. const txCancel = await this.helper.cancel('internal');
  488. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  489. await expectEvent.inTransaction(txCancel.tx, this.manager, 'OperationCanceled', {
  490. operationId: this.restricted.operationId,
  491. nonce: '1',
  492. });
  493. await this.helper.waitForEta();
  494. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  495. this.proposal.id,
  496. Enums.ProposalState.Canceled,
  497. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  498. ]);
  499. });
  500. it('cancels restricted with queueing if the same operation is part of a more recent proposal (internal)', async function () {
  501. // Set proposals
  502. const original = new GovernorHelper(this.mock, mode);
  503. await original.setProposal([this.restricted.operation], 'descr');
  504. // Go through all the governance process
  505. await original.propose();
  506. await original.waitForSnapshot();
  507. await original.vote({ support: Enums.VoteType.For }, { from: voter1 });
  508. await original.waitForDeadline();
  509. await original.queue();
  510. // Cancel the operation in the manager
  511. await this.manager.cancel(
  512. this.mock.address,
  513. this.restricted.operation.target,
  514. this.restricted.operation.data,
  515. { from: admin },
  516. );
  517. // Another proposal is added with the same operation
  518. const rescheduled = new GovernorHelper(this.mock, mode);
  519. await rescheduled.setProposal([this.restricted.operation], 'another descr');
  520. // Queue the new proposal
  521. await rescheduled.propose();
  522. await rescheduled.waitForSnapshot();
  523. await rescheduled.vote({ support: Enums.VoteType.For }, { from: voter1 });
  524. await rescheduled.waitForDeadline();
  525. await rescheduled.queue(); // This will schedule it again in the manager
  526. // Cancel
  527. const eta = await this.mock.proposalEta(rescheduled.currentProposal.id);
  528. const txCancel = await original.cancel('internal');
  529. expectEvent(txCancel, 'ProposalCanceled', { proposalId: original.currentProposal.id });
  530. await time.increase(eta); // waitForEta()
  531. await expectRevertCustomError(original.execute(), 'GovernorUnexpectedProposalState', [
  532. original.currentProposal.id,
  533. Enums.ProposalState.Canceled,
  534. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  535. ]);
  536. });
  537. it('cancels unrestricted with queueing (internal)', async function () {
  538. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  539. await this.helper.propose();
  540. await this.helper.waitForSnapshot();
  541. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  542. await this.helper.waitForDeadline();
  543. await this.helper.queue();
  544. const eta = await this.mock.proposalEta(this.proposal.id);
  545. const txCancel = await this.helper.cancel('internal');
  546. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  547. await time.increase(eta); // waitForEta()
  548. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  549. this.proposal.id,
  550. Enums.ProposalState.Canceled,
  551. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  552. ]);
  553. });
  554. it('cancels unrestricted without queueing (internal)', async function () {
  555. this.proposal = await this.helper.setProposal([this.unrestricted.operation], 'descr');
  556. await this.helper.propose();
  557. await this.helper.waitForSnapshot();
  558. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  559. await this.helper.waitForDeadline();
  560. // await this.helper.queue();
  561. // const eta = await this.mock.proposalEta(this.proposal.id);
  562. const txCancel = await this.helper.cancel('internal');
  563. expectEvent(txCancel, 'ProposalCanceled', { proposalId: this.proposal.id });
  564. // await time.increase(eta); // waitForEta()
  565. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  566. this.proposal.id,
  567. Enums.ProposalState.Canceled,
  568. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  569. ]);
  570. });
  571. it('cancels calls already canceled by guardian', async function () {
  572. const operationA = { target: this.receiver.address, data: this.restricted.selector + '00' };
  573. const operationB = { target: this.receiver.address, data: this.restricted.selector + '01' };
  574. const operationC = { target: this.receiver.address, data: this.restricted.selector + '02' };
  575. const operationAId = hashOperation(this.mock.address, operationA.target, operationA.data);
  576. const operationBId = hashOperation(this.mock.address, operationB.target, operationB.data);
  577. const proposal1 = new GovernorHelper(this.mock, mode);
  578. const proposal2 = new GovernorHelper(this.mock, mode);
  579. proposal1.setProposal([operationA, operationB], 'proposal A+B');
  580. proposal2.setProposal([operationA, operationC], 'proposal A+C');
  581. for (const p of [proposal1, proposal2]) {
  582. await p.propose();
  583. await p.waitForSnapshot();
  584. await p.vote({ support: Enums.VoteType.For }, { from: voter1 });
  585. await p.waitForDeadline();
  586. }
  587. // Can queue the first proposal
  588. await proposal1.queue();
  589. // Cannot queue the second proposal: operation A already scheduled with delay
  590. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  591. // Admin cancels operation B on the manager
  592. await this.manager.cancel(this.mock.address, operationB.target, operationB.data, { from: admin });
  593. // Still cannot queue the second proposal: operation A already scheduled with delay
  594. await expectRevertCustomError(proposal2.queue(), 'AccessManagerAlreadyScheduled', [operationAId]);
  595. await proposal1.waitForEta();
  596. // Cannot execute first proposal: operation B has been canceled
  597. await expectRevertCustomError(proposal1.execute(), 'AccessManagerNotScheduled', [operationBId]);
  598. // Cancel the first proposal to release operation A
  599. await proposal1.cancel('internal');
  600. // can finally queue the second proposal
  601. await proposal2.queue();
  602. await proposal2.waitForEta();
  603. // Can execute second proposal
  604. await proposal2.execute();
  605. });
  606. });
  607. describe('ignore AccessManager', function () {
  608. it('defaults', async function () {
  609. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  610. false,
  611. );
  612. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(true);
  613. });
  614. it('internal setter', async function () {
  615. const p1 = { target: this.receiver.address, selector: this.restricted.selector, ignored: true };
  616. const tx1 = await this.mock.$_setAccessManagerIgnored(p1.target, p1.selector, p1.ignored);
  617. expect(await this.mock.isAccessManagerIgnored(p1.target, p1.selector)).to.equal(p1.ignored);
  618. expectEvent(tx1, 'AccessManagerIgnoredSet', p1);
  619. const p2 = { target: this.mock.address, selector: '0x12341234', ignored: false };
  620. const tx2 = await this.mock.$_setAccessManagerIgnored(p2.target, p2.selector, p2.ignored);
  621. expect(await this.mock.isAccessManagerIgnored(p2.target, p2.selector)).to.equal(p2.ignored);
  622. expectEvent(tx2, 'AccessManagerIgnoredSet', p2);
  623. });
  624. it('external setter', async function () {
  625. const setAccessManagerIgnored = (...args) =>
  626. this.mock.contract.methods.setAccessManagerIgnored(...args).encodeABI();
  627. await this.helper.setProposal(
  628. [
  629. {
  630. target: this.mock.address,
  631. data: setAccessManagerIgnored(
  632. this.receiver.address,
  633. [this.restricted.selector, this.unrestricted.selector],
  634. true,
  635. ),
  636. value: '0',
  637. },
  638. {
  639. target: this.mock.address,
  640. data: setAccessManagerIgnored(this.mock.address, ['0x12341234', '0x67896789'], false),
  641. value: '0',
  642. },
  643. ],
  644. 'descr',
  645. );
  646. await this.helper.propose();
  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(tx, 'AccessManagerIgnoredSet');
  652. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.restricted.selector)).to.equal(
  653. true,
  654. );
  655. expect(await this.mock.isAccessManagerIgnored(this.receiver.address, this.unrestricted.selector)).to.equal(
  656. true,
  657. );
  658. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x12341234')).to.equal(false);
  659. expect(await this.mock.isAccessManagerIgnored(this.mock.address, '0x67896789')).to.equal(false);
  660. });
  661. it('locked function', async function () {
  662. const setAccessManagerIgnored = selector('setAccessManagerIgnored(address,bytes4[],bool)');
  663. await expectRevertCustomError(
  664. this.mock.$_setAccessManagerIgnored(this.mock.address, setAccessManagerIgnored, true),
  665. 'GovernorLockedIgnore',
  666. [],
  667. );
  668. await this.mock.$_setAccessManagerIgnored(this.receiver.address, setAccessManagerIgnored, true);
  669. });
  670. it('ignores access manager', async function () {
  671. const amount = 100;
  672. const target = this.token.address;
  673. const data = this.token.contract.methods.transfer(voter4, amount).encodeABI();
  674. const selector = data.slice(0, 10);
  675. await this.token.$_mint(this.mock.address, amount);
  676. const roleId = '1';
  677. await this.manager.setTargetFunctionRole(target, [selector], roleId, { from: admin });
  678. await this.manager.grantRole(roleId, this.mock.address, 0, { from: admin });
  679. this.proposal = await this.helper.setProposal([{ target, data, value: '0' }], '1');
  680. await this.helper.propose();
  681. await this.helper.waitForSnapshot();
  682. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  683. await this.helper.waitForDeadline();
  684. await expectRevertCustomError(this.helper.execute(), 'ERC20InsufficientBalance', [
  685. this.manager.address,
  686. 0,
  687. amount,
  688. ]);
  689. await this.mock.$_setAccessManagerIgnored(target, selector, true);
  690. await this.helper.setProposal([{ target, data, value: '0' }], '2');
  691. await this.helper.propose();
  692. await this.helper.waitForSnapshot();
  693. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  694. await this.helper.waitForDeadline();
  695. const tx = await this.helper.execute();
  696. expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.mock.address });
  697. });
  698. });
  699. describe('operating on an Ownable contract', function () {
  700. const method = selector('$_checkOwner()');
  701. beforeEach(async function () {
  702. this.ownable = await Ownable.new(this.manager.address);
  703. this.operation = {
  704. target: this.ownable.address,
  705. value: '0',
  706. data: this.ownable.contract.methods.$_checkOwner().encodeABI(),
  707. };
  708. });
  709. it('succeeds with delay', async function () {
  710. const roleId = '1';
  711. const executionDelay = time.duration.hours(2);
  712. const baseDelay = time.duration.hours(1);
  713. // Set execution delay
  714. await this.manager.setTargetFunctionRole(this.ownable.address, [method], roleId, {
  715. from: admin,
  716. });
  717. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  718. // Set base delay
  719. await this.mock.$_setBaseDelaySeconds(baseDelay);
  720. this.proposal = await this.helper.setProposal([this.operation], `descr`);
  721. await this.helper.propose();
  722. await this.helper.waitForSnapshot();
  723. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  724. await this.helper.waitForDeadline();
  725. await this.helper.queue();
  726. await this.helper.waitForEta();
  727. await this.helper.execute(); // Don't revert
  728. });
  729. it('succeeds without delay', async function () {
  730. const roleId = '1';
  731. const executionDelay = web3.utils.toBN(0);
  732. const baseDelay = web3.utils.toBN(0);
  733. // Set execution delay
  734. await this.manager.setTargetFunctionRole(this.ownable.address, [method], roleId, {
  735. from: admin,
  736. });
  737. await this.manager.grantRole(roleId, this.mock.address, executionDelay, { from: admin });
  738. // Set base delay
  739. await this.mock.$_setBaseDelaySeconds(baseDelay);
  740. this.proposal = await this.helper.setProposal([this.operation], `descr`);
  741. await this.helper.propose();
  742. await this.helper.waitForSnapshot();
  743. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  744. await this.helper.waitForDeadline();
  745. await this.helper.execute(); // Don't revert
  746. });
  747. });
  748. });
  749. }
  750. });