GovernorTimelockControl.test.js 21 KB

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