GovernorTimelockControl.test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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(), 'GovernorIncorrectState', [
  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(), 'TimelockIncorrectState', [
  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(), 'TimelockIncorrectState', [
  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(), 'GovernorIncorrectState', [
  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(), 'GovernorIncorrectState', [
  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(), 'GovernorIncorrectState', [
  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(), 'GovernorIncorrectState', [
  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. 'GovernorOnlyGovernance',
  256. [],
  257. );
  258. });
  259. it('can be executed through governance', async function () {
  260. this.helper.setProposal(
  261. [
  262. {
  263. target: this.mock.address,
  264. data: this.mock.contract.methods
  265. .relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
  266. .encodeABI(),
  267. },
  268. ],
  269. '<proposal description>',
  270. );
  271. expect(await this.token.balanceOf(this.mock.address), 1);
  272. expect(await this.token.balanceOf(other), 0);
  273. await this.helper.propose();
  274. await this.helper.waitForSnapshot();
  275. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  276. await this.helper.waitForDeadline();
  277. await this.helper.queue();
  278. await this.helper.waitForEta();
  279. const txExecute = await this.helper.execute();
  280. expect(await this.token.balanceOf(this.mock.address), 0);
  281. expect(await this.token.balanceOf(other), 1);
  282. await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
  283. from: this.mock.address,
  284. to: other,
  285. value: '1',
  286. });
  287. });
  288. it('is payable and can transfer eth to EOA', async function () {
  289. const t2g = web3.utils.toBN(128); // timelock to governor
  290. const g2o = web3.utils.toBN(100); // governor to eoa (other)
  291. this.helper.setProposal(
  292. [
  293. {
  294. target: this.mock.address,
  295. value: t2g,
  296. data: this.mock.contract.methods.relay(other, g2o, '0x').encodeABI(),
  297. },
  298. ],
  299. '<proposal description>',
  300. );
  301. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  302. const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
  303. const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
  304. await this.helper.propose();
  305. await this.helper.waitForSnapshot();
  306. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  307. await this.helper.waitForDeadline();
  308. await this.helper.queue();
  309. await this.helper.waitForEta();
  310. await this.helper.execute();
  311. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(
  312. timelockBalance.sub(t2g),
  313. );
  314. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
  315. expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
  316. });
  317. it('protected against other proposers', async function () {
  318. await this.timelock.schedule(
  319. this.mock.address,
  320. web3.utils.toWei('0'),
  321. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  322. constants.ZERO_BYTES32,
  323. constants.ZERO_BYTES32,
  324. 3600,
  325. { from: owner },
  326. );
  327. await time.increase(3600);
  328. await expectRevertCustomError(
  329. this.timelock.execute(
  330. this.mock.address,
  331. web3.utils.toWei('0'),
  332. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  333. constants.ZERO_BYTES32,
  334. constants.ZERO_BYTES32,
  335. { from: owner },
  336. ),
  337. 'TimelockFailedCall',
  338. [],
  339. );
  340. });
  341. });
  342. describe('updateTimelock', function () {
  343. beforeEach(async function () {
  344. this.newTimelock = await Timelock.new(
  345. 3600,
  346. [this.mock.address],
  347. [this.mock.address],
  348. constants.ZERO_ADDRESS,
  349. );
  350. });
  351. it('is protected', async function () {
  352. await expectRevertCustomError(
  353. this.mock.updateTimelock(this.newTimelock.address),
  354. 'GovernorOnlyGovernance',
  355. [],
  356. );
  357. });
  358. it('can be executed through governance to', async function () {
  359. this.helper.setProposal(
  360. [
  361. {
  362. target: this.mock.address,
  363. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  364. },
  365. ],
  366. '<proposal description>',
  367. );
  368. await this.helper.propose();
  369. await this.helper.waitForSnapshot();
  370. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  371. await this.helper.waitForDeadline();
  372. await this.helper.queue();
  373. await this.helper.waitForEta();
  374. const txExecute = await this.helper.execute();
  375. expectEvent(txExecute, 'TimelockChange', {
  376. oldTimelock: this.timelock.address,
  377. newTimelock: this.newTimelock.address,
  378. });
  379. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  380. });
  381. });
  382. });
  383. it('clear queue of pending governor calls', async function () {
  384. this.helper.setProposal(
  385. [
  386. {
  387. target: this.mock.address,
  388. data: this.mock.contract.methods.nonGovernanceFunction().encodeABI(),
  389. },
  390. ],
  391. '<proposal description>',
  392. );
  393. await this.helper.propose();
  394. await this.helper.waitForSnapshot();
  395. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  396. await this.helper.waitForDeadline();
  397. await this.helper.queue();
  398. await this.helper.waitForEta();
  399. await this.helper.execute();
  400. // This path clears _governanceCall as part of the afterExecute call,
  401. // but we have not way to check that the cleanup actually happened other
  402. // then coverage reports.
  403. });
  404. });
  405. });
  406. });
  407. }
  408. });