GovernorTimelockControl.test.js 21 KB

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