GovernorTimelockControl.test.js 21 KB

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