GovernorTimelockControl.test.js 21 KB

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