GovernorTimelockControl.test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper, proposalStatesToBitMap, timelockSalt } = require('../../helpers/governance');
  5. const { expectRevertCustomError } = require('../../helpers/customError');
  6. const { clockFromReceipt } = require('../../helpers/time');
  7. const Timelock = artifacts.require('TimelockController');
  8. const Governor = artifacts.require('$GovernorTimelockControlMock');
  9. const CallReceiver = artifacts.require('CallReceiverMock');
  10. const ERC721 = artifacts.require('$ERC721');
  11. const ERC1155 = artifacts.require('$ERC1155');
  12. const TOKENS = [
  13. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  14. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  15. ];
  16. contract('GovernorTimelockControl', function (accounts) {
  17. const [owner, voter1, voter2, voter3, voter4, other] = accounts;
  18. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  19. const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
  20. const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
  21. const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
  22. const name = 'OZ-Governor';
  23. const version = '1';
  24. const tokenName = 'MockToken';
  25. const tokenSymbol = 'MTKN';
  26. const tokenSupply = web3.utils.toWei('100');
  27. const votingDelay = web3.utils.toBN(4);
  28. const votingPeriod = web3.utils.toBN(16);
  29. const value = web3.utils.toWei('1');
  30. const delay = 3600;
  31. for (const { mode, Token } of TOKENS) {
  32. describe(`using ${Token._json.contractName}`, function () {
  33. beforeEach(async function () {
  34. const [deployer] = await web3.eth.getAccounts();
  35. this.token = await Token.new(tokenName, tokenSymbol, tokenName, version);
  36. this.timelock = await Timelock.new(delay, [], [], deployer);
  37. this.mock = await Governor.new(
  38. name,
  39. votingDelay,
  40. votingPeriod,
  41. 0,
  42. this.timelock.address,
  43. this.token.address,
  44. 0,
  45. );
  46. this.receiver = await CallReceiver.new();
  47. this.helper = new GovernorHelper(this.mock, mode);
  48. this.PROPOSER_ROLE = await this.timelock.PROPOSER_ROLE();
  49. this.EXECUTOR_ROLE = await this.timelock.EXECUTOR_ROLE();
  50. this.CANCELLER_ROLE = await this.timelock.CANCELLER_ROLE();
  51. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  52. // normal setup: governor is proposer, everyone is executor, timelock is its own admin
  53. await this.timelock.grantRole(PROPOSER_ROLE, this.mock.address);
  54. await this.timelock.grantRole(PROPOSER_ROLE, owner);
  55. await this.timelock.grantRole(CANCELLER_ROLE, this.mock.address);
  56. await this.timelock.grantRole(CANCELLER_ROLE, owner);
  57. await this.timelock.grantRole(EXECUTOR_ROLE, constants.ZERO_ADDRESS);
  58. await this.timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  59. await this.token.$_mint(owner, tokenSupply);
  60. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  61. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  62. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  63. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  64. // default proposal
  65. this.proposal = this.helper.setProposal(
  66. [
  67. {
  68. target: this.receiver.address,
  69. value,
  70. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  71. },
  72. ],
  73. '<proposal description>',
  74. );
  75. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  76. ...this.proposal.shortProposal.slice(0, 3),
  77. '0x0',
  78. timelockSalt(this.mock.address, this.proposal.shortProposal[3]),
  79. );
  80. });
  81. it("doesn't accept ether transfers", async function () {
  82. await expectRevert.unspecified(web3.eth.sendTransaction({ from: owner, to: this.mock.address, value: 1 }));
  83. });
  84. it('post deployment check', async function () {
  85. expect(await this.mock.name()).to.be.equal(name);
  86. expect(await this.mock.token()).to.be.equal(this.token.address);
  87. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  88. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  89. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  90. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  91. });
  92. it('nominal', async function () {
  93. expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal('0');
  94. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
  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.vote({ support: Enums.VoteType.For }, { from: voter2 });
  99. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  100. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  101. await this.helper.waitForDeadline();
  102. const txQueue = await this.helper.queue();
  103. await this.helper.waitForEta();
  104. const eta = web3.utils.toBN(await clockFromReceipt.timestamp(txQueue.receipt)).addn(delay);
  105. expect(await this.mock.proposalEta(this.proposal.id)).to.be.bignumber.equal(eta);
  106. expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.equal(true);
  107. const txExecute = await this.helper.execute();
  108. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  109. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallScheduled', { id: this.proposal.timelockid });
  110. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallSalt', {
  111. id: this.proposal.timelockid,
  112. });
  113. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  114. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', { id: this.proposal.timelockid });
  115. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  116. });
  117. describe('should revert', function () {
  118. describe('on queue', function () {
  119. it('if already queued', async function () {
  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. await this.helper.queue();
  125. await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
  126. this.proposal.id,
  127. Enums.ProposalState.Queued,
  128. proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  129. ]);
  130. });
  131. });
  132. describe('on execute', function () {
  133. it('if not queued', async function () {
  134. await this.helper.propose();
  135. await this.helper.waitForSnapshot();
  136. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  137. await this.helper.waitForDeadline(+1);
  138. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  139. await expectRevertCustomError(this.helper.execute(), 'TimelockUnexpectedOperationState', [
  140. this.proposal.timelockid,
  141. proposalStatesToBitMap(Enums.OperationState.Ready),
  142. ]);
  143. });
  144. it('if too early', async function () {
  145. await this.helper.propose();
  146. await this.helper.waitForSnapshot();
  147. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  148. await this.helper.waitForDeadline();
  149. await this.helper.queue();
  150. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  151. await expectRevertCustomError(this.helper.execute(), 'TimelockUnexpectedOperationState', [
  152. this.proposal.timelockid,
  153. proposalStatesToBitMap(Enums.OperationState.Ready),
  154. ]);
  155. });
  156. it('if already executed', async function () {
  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. await this.helper.queue();
  162. await this.helper.waitForEta();
  163. await this.helper.execute();
  164. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  165. this.proposal.id,
  166. Enums.ProposalState.Executed,
  167. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  168. ]);
  169. });
  170. it('if already executed by another proposer', async function () {
  171. await this.helper.propose();
  172. await this.helper.waitForSnapshot();
  173. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  174. await this.helper.waitForDeadline();
  175. await this.helper.queue();
  176. await this.helper.waitForEta();
  177. await this.timelock.executeBatch(
  178. ...this.proposal.shortProposal.slice(0, 3),
  179. '0x0',
  180. timelockSalt(this.mock.address, this.proposal.shortProposal[3]),
  181. );
  182. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  183. this.proposal.id,
  184. Enums.ProposalState.Executed,
  185. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  186. ]);
  187. });
  188. });
  189. });
  190. describe('cancel', function () {
  191. it('cancel before queue prevents scheduling', async function () {
  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. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  197. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  198. await expectRevertCustomError(this.helper.queue(), 'GovernorUnexpectedProposalState', [
  199. this.proposal.id,
  200. Enums.ProposalState.Canceled,
  201. proposalStatesToBitMap([Enums.ProposalState.Succeeded]),
  202. ]);
  203. });
  204. it('cancel after queue prevents executing', async function () {
  205. await this.helper.propose();
  206. await this.helper.waitForSnapshot();
  207. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  208. await this.helper.waitForDeadline();
  209. await this.helper.queue();
  210. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  211. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  212. await expectRevertCustomError(this.helper.execute(), 'GovernorUnexpectedProposalState', [
  213. this.proposal.id,
  214. Enums.ProposalState.Canceled,
  215. proposalStatesToBitMap([Enums.ProposalState.Succeeded, Enums.ProposalState.Queued]),
  216. ]);
  217. });
  218. it('cancel on timelock is reflected on governor', async function () {
  219. await this.helper.propose();
  220. await this.helper.waitForSnapshot();
  221. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  222. await this.helper.waitForDeadline();
  223. await this.helper.queue();
  224. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  225. expectEvent(await this.timelock.cancel(this.proposal.timelockid, { from: owner }), 'Cancelled', {
  226. id: this.proposal.timelockid,
  227. });
  228. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  229. });
  230. });
  231. describe('onlyGovernance', function () {
  232. describe('relay', function () {
  233. beforeEach(async function () {
  234. await this.token.$_mint(this.mock.address, 1);
  235. });
  236. it('is protected', async function () {
  237. await expectRevertCustomError(
  238. this.mock.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI(), {
  239. from: owner,
  240. }),
  241. 'GovernorOnlyExecutor',
  242. [owner],
  243. );
  244. });
  245. it('can be executed through governance', async function () {
  246. this.helper.setProposal(
  247. [
  248. {
  249. target: this.mock.address,
  250. data: this.mock.contract.methods
  251. .relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
  252. .encodeABI(),
  253. },
  254. ],
  255. '<proposal description>',
  256. );
  257. expect(await this.token.balanceOf(this.mock.address), 1);
  258. expect(await this.token.balanceOf(other), 0);
  259. await this.helper.propose();
  260. await this.helper.waitForSnapshot();
  261. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  262. await this.helper.waitForDeadline();
  263. await this.helper.queue();
  264. await this.helper.waitForEta();
  265. const txExecute = await this.helper.execute();
  266. expect(await this.token.balanceOf(this.mock.address), 0);
  267. expect(await this.token.balanceOf(other), 1);
  268. await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
  269. from: this.mock.address,
  270. to: other,
  271. value: '1',
  272. });
  273. });
  274. it('is payable and can transfer eth to EOA', async function () {
  275. const t2g = web3.utils.toBN(128); // timelock to governor
  276. const g2o = web3.utils.toBN(100); // governor to eoa (other)
  277. this.helper.setProposal(
  278. [
  279. {
  280. target: this.mock.address,
  281. value: t2g,
  282. data: this.mock.contract.methods.relay(other, g2o, '0x').encodeABI(),
  283. },
  284. ],
  285. '<proposal description>',
  286. );
  287. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  288. const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
  289. const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
  290. await this.helper.propose();
  291. await this.helper.waitForSnapshot();
  292. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  293. await this.helper.waitForDeadline();
  294. await this.helper.queue();
  295. await this.helper.waitForEta();
  296. await this.helper.execute();
  297. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(timelockBalance.sub(t2g));
  298. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
  299. expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
  300. });
  301. it('protected against other proposers', async function () {
  302. const target = this.mock.address;
  303. const value = web3.utils.toWei('0');
  304. const data = this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI();
  305. const predecessor = constants.ZERO_BYTES32;
  306. const salt = constants.ZERO_BYTES32;
  307. await this.timelock.schedule(target, value, data, predecessor, salt, delay, { from: owner });
  308. await time.increase(delay);
  309. await expectRevertCustomError(
  310. this.timelock.execute(target, value, data, predecessor, salt, { from: owner }),
  311. 'QueueEmpty', // Bubbled up from Governor
  312. [],
  313. );
  314. });
  315. });
  316. describe('updateTimelock', function () {
  317. beforeEach(async function () {
  318. this.newTimelock = await Timelock.new(
  319. delay,
  320. [this.mock.address],
  321. [this.mock.address],
  322. constants.ZERO_ADDRESS,
  323. );
  324. });
  325. it('is protected', async function () {
  326. await expectRevertCustomError(
  327. this.mock.updateTimelock(this.newTimelock.address, { from: owner }),
  328. 'GovernorOnlyExecutor',
  329. [owner],
  330. );
  331. });
  332. it('can be executed through governance to', async function () {
  333. this.helper.setProposal(
  334. [
  335. {
  336. target: this.mock.address,
  337. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  338. },
  339. ],
  340. '<proposal description>',
  341. );
  342. await this.helper.propose();
  343. await this.helper.waitForSnapshot();
  344. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  345. await this.helper.waitForDeadline();
  346. await this.helper.queue();
  347. await this.helper.waitForEta();
  348. const txExecute = await this.helper.execute();
  349. expectEvent(txExecute, 'TimelockChange', {
  350. oldTimelock: this.timelock.address,
  351. newTimelock: this.newTimelock.address,
  352. });
  353. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  354. });
  355. });
  356. describe('on safe receive', function () {
  357. describe('ERC721', function () {
  358. const name = 'Non Fungible Token';
  359. const symbol = 'NFT';
  360. const tokenId = web3.utils.toBN(1);
  361. beforeEach(async function () {
  362. this.token = await ERC721.new(name, symbol);
  363. await this.token.$_mint(owner, tokenId);
  364. });
  365. it("can't receive an ERC721 safeTransfer", async function () {
  366. await expectRevertCustomError(
  367. this.token.safeTransferFrom(owner, this.mock.address, tokenId, { from: owner }),
  368. 'GovernorDisabledDeposit',
  369. [],
  370. );
  371. });
  372. });
  373. describe('ERC1155', function () {
  374. const uri = 'https://token-cdn-domain/{id}.json';
  375. const tokenIds = {
  376. 1: web3.utils.toBN(1000),
  377. 2: web3.utils.toBN(2000),
  378. 3: web3.utils.toBN(3000),
  379. };
  380. beforeEach(async function () {
  381. this.token = await ERC1155.new(uri);
  382. await this.token.$_mintBatch(owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
  383. });
  384. it("can't receive ERC1155 safeTransfer", async function () {
  385. await expectRevertCustomError(
  386. this.token.safeTransferFrom(
  387. owner,
  388. this.mock.address,
  389. ...Object.entries(tokenIds)[0], // id + amount
  390. '0x',
  391. { from: owner },
  392. ),
  393. 'GovernorDisabledDeposit',
  394. [],
  395. );
  396. });
  397. it("can't receive ERC1155 safeBatchTransfer", async function () {
  398. await expectRevertCustomError(
  399. this.token.safeBatchTransferFrom(
  400. owner,
  401. this.mock.address,
  402. Object.keys(tokenIds),
  403. Object.values(tokenIds),
  404. '0x',
  405. { from: owner },
  406. ),
  407. 'GovernorDisabledDeposit',
  408. [],
  409. );
  410. });
  411. });
  412. });
  413. });
  414. it('clear queue of pending governor calls', async function () {
  415. this.helper.setProposal(
  416. [
  417. {
  418. target: this.mock.address,
  419. data: this.mock.contract.methods.nonGovernanceFunction().encodeABI(),
  420. },
  421. ],
  422. '<proposal description>',
  423. );
  424. await this.helper.propose();
  425. await this.helper.waitForSnapshot();
  426. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  427. await this.helper.waitForDeadline();
  428. await this.helper.queue();
  429. await this.helper.waitForEta();
  430. await this.helper.execute();
  431. // This path clears _governanceCall as part of the afterExecute call,
  432. // but we have not way to check that the cleanup actually happened other
  433. // then coverage reports.
  434. });
  435. });
  436. }
  437. });