GovernorTimelockControl.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper } = require('../../helpers/governance');
  5. const {
  6. shouldSupportInterfaces,
  7. } = require('../../utils/introspection/SupportsInterface.behavior');
  8. const Token = artifacts.require('$ERC20Votes');
  9. const Timelock = artifacts.require('TimelockController');
  10. const Governor = artifacts.require('$GovernorTimelockControlMock');
  11. const CallReceiver = artifacts.require('CallReceiverMock');
  12. contract('GovernorTimelockControl', function (accounts) {
  13. const [ owner, voter1, voter2, voter3, voter4, other ] = accounts;
  14. const TIMELOCK_ADMIN_ROLE = web3.utils.soliditySha3('TIMELOCK_ADMIN_ROLE');
  15. const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
  16. const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
  17. const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
  18. const name = 'OZ-Governor';
  19. // const version = '1';
  20. const tokenName = 'MockToken';
  21. const tokenSymbol = 'MTKN';
  22. const tokenSupply = web3.utils.toWei('100');
  23. const votingDelay = new BN(4);
  24. const votingPeriod = new BN(16);
  25. const value = web3.utils.toWei('1');
  26. beforeEach(async function () {
  27. const [ deployer ] = await web3.eth.getAccounts();
  28. this.token = await Token.new(tokenName, tokenSymbol, tokenName);
  29. this.timelock = await Timelock.new(3600, [], [], deployer);
  30. this.mock = await Governor.new(
  31. name,
  32. votingDelay,
  33. votingPeriod,
  34. 0,
  35. this.timelock.address,
  36. this.token.address,
  37. 0,
  38. );
  39. this.receiver = await CallReceiver.new();
  40. this.helper = new GovernorHelper(this.mock);
  41. this.TIMELOCK_ADMIN_ROLE = await this.timelock.TIMELOCK_ADMIN_ROLE();
  42. this.PROPOSER_ROLE = await this.timelock.PROPOSER_ROLE();
  43. this.EXECUTOR_ROLE = await this.timelock.EXECUTOR_ROLE();
  44. this.CANCELLER_ROLE = await this.timelock.CANCELLER_ROLE();
  45. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  46. // normal setup: governor is proposer, everyone is executor, timelock is its own admin
  47. await this.timelock.grantRole(PROPOSER_ROLE, this.mock.address);
  48. await this.timelock.grantRole(PROPOSER_ROLE, owner);
  49. await this.timelock.grantRole(CANCELLER_ROLE, this.mock.address);
  50. await this.timelock.grantRole(CANCELLER_ROLE, owner);
  51. await this.timelock.grantRole(EXECUTOR_ROLE, constants.ZERO_ADDRESS);
  52. await this.timelock.revokeRole(TIMELOCK_ADMIN_ROLE, deployer);
  53. await this.token.$_mint(owner, tokenSupply);
  54. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  55. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  56. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  57. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  58. // default proposal
  59. this.proposal = this.helper.setProposal([
  60. {
  61. target: this.receiver.address,
  62. value,
  63. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  64. },
  65. ], '<proposal description>');
  66. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  67. ...this.proposal.shortProposal.slice(0, 3),
  68. '0x0',
  69. this.proposal.shortProposal[3],
  70. );
  71. });
  72. shouldSupportInterfaces([
  73. 'ERC165',
  74. 'Governor',
  75. 'GovernorWithParams',
  76. 'GovernorTimelock',
  77. ]);
  78. it('doesn\'t accept ether transfers', async function () {
  79. await expectRevert.unspecified(web3.eth.sendTransaction({ from: owner, to: this.mock.address, value: 1 }));
  80. });
  81. it('post deployment check', async function () {
  82. expect(await this.mock.name()).to.be.equal(name);
  83. expect(await this.mock.token()).to.be.equal(this.token.address);
  84. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  85. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  86. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  87. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  88. });
  89. it('nominal', async function () {
  90. await this.helper.propose();
  91. await this.helper.waitForSnapshot();
  92. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  93. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  94. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  95. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  96. await this.helper.waitForDeadline();
  97. const txQueue = await this.helper.queue();
  98. await this.helper.waitForEta();
  99. const txExecute = await this.helper.execute();
  100. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  101. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallScheduled', { id: this.proposal.timelockid });
  102. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  103. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', { id: this.proposal.timelockid });
  104. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  105. });
  106. describe('should revert', function () {
  107. describe('on queue', function () {
  108. it('if already queued', async function () {
  109. await this.helper.propose();
  110. await this.helper.waitForSnapshot();
  111. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  112. await this.helper.waitForDeadline();
  113. await this.helper.queue();
  114. await expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  115. });
  116. });
  117. describe('on execute', function () {
  118. it('if not queued', async function () {
  119. await this.helper.propose();
  120. await this.helper.waitForSnapshot();
  121. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  122. await this.helper.waitForDeadline(+1);
  123. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  124. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  125. });
  126. it('if too early', 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();
  131. await this.helper.queue();
  132. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  133. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  134. });
  135. it('if already executed', 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 this.helper.waitForEta();
  142. await this.helper.execute();
  143. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  144. });
  145. it('if already executed by another proposer', async function () {
  146. await this.helper.propose();
  147. await this.helper.waitForSnapshot();
  148. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  149. await this.helper.waitForDeadline();
  150. await this.helper.queue();
  151. await this.helper.waitForEta();
  152. await this.timelock.executeBatch(
  153. ...this.proposal.shortProposal.slice(0, 3),
  154. '0x0',
  155. this.proposal.shortProposal[3],
  156. );
  157. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  158. });
  159. });
  160. });
  161. describe('cancel', function () {
  162. it('cancel before queue prevents scheduling', async function () {
  163. await this.helper.propose();
  164. await this.helper.waitForSnapshot();
  165. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  166. await this.helper.waitForDeadline();
  167. expectEvent(
  168. await this.helper.cancel(),
  169. 'ProposalCanceled',
  170. { proposalId: this.proposal.id },
  171. );
  172. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  173. await expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  174. });
  175. it('cancel after queue prevents executing', async function () {
  176. await this.helper.propose();
  177. await this.helper.waitForSnapshot();
  178. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  179. await this.helper.waitForDeadline();
  180. await this.helper.queue();
  181. expectEvent(
  182. await this.helper.cancel(),
  183. 'ProposalCanceled',
  184. { proposalId: this.proposal.id },
  185. );
  186. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  187. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  188. });
  189. it('cancel on timelock is reflected on governor', async function () {
  190. await this.helper.propose();
  191. await this.helper.waitForSnapshot();
  192. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  193. await this.helper.waitForDeadline();
  194. await this.helper.queue();
  195. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  196. expectEvent(
  197. await this.timelock.cancel(this.proposal.timelockid, { from: owner }),
  198. 'Cancelled',
  199. { id: this.proposal.timelockid },
  200. );
  201. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  202. });
  203. });
  204. describe('onlyGovernance', function () {
  205. describe('relay', function () {
  206. beforeEach(async function () {
  207. await this.token.$_mint(this.mock.address, 1);
  208. });
  209. it('is protected', async function () {
  210. await expectRevert(
  211. this.mock.relay(
  212. this.token.address,
  213. 0,
  214. this.token.contract.methods.transfer(other, 1).encodeABI(),
  215. ),
  216. 'Governor: onlyGovernance',
  217. );
  218. });
  219. it('can be executed through governance', async function () {
  220. this.helper.setProposal([
  221. {
  222. target: this.mock.address,
  223. data: this.mock.contract.methods.relay(
  224. this.token.address,
  225. 0,
  226. this.token.contract.methods.transfer(other, 1).encodeABI(),
  227. ).encodeABI(),
  228. },
  229. ], '<proposal description>');
  230. expect(await this.token.balanceOf(this.mock.address), 1);
  231. expect(await this.token.balanceOf(other), 0);
  232. await this.helper.propose();
  233. await this.helper.waitForSnapshot();
  234. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  235. await this.helper.waitForDeadline();
  236. await this.helper.queue();
  237. await this.helper.waitForEta();
  238. const txExecute = await this.helper.execute();
  239. expect(await this.token.balanceOf(this.mock.address), 0);
  240. expect(await this.token.balanceOf(other), 1);
  241. expectEvent.inTransaction(
  242. txExecute.tx,
  243. this.token,
  244. 'Transfer',
  245. { from: this.mock.address, to: other, value: '1' },
  246. );
  247. });
  248. it('is payable and can transfer eth to EOA', async function () {
  249. const t2g = web3.utils.toBN(128); // timelock to governor
  250. const g2o = web3.utils.toBN(100); // governor to eoa (other)
  251. this.helper.setProposal([
  252. {
  253. target: this.mock.address,
  254. value: t2g,
  255. data: this.mock.contract.methods.relay(
  256. other,
  257. g2o,
  258. '0x',
  259. ).encodeABI(),
  260. },
  261. ], '<proposal description>');
  262. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  263. const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
  264. const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
  265. await this.helper.propose();
  266. await this.helper.waitForSnapshot();
  267. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  268. await this.helper.waitForDeadline();
  269. await this.helper.queue();
  270. await this.helper.waitForEta();
  271. await this.helper.execute();
  272. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(timelockBalance.sub(t2g));
  273. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
  274. expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
  275. });
  276. it('protected against other proposers', async function () {
  277. await this.timelock.schedule(
  278. this.mock.address,
  279. web3.utils.toWei('0'),
  280. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  281. constants.ZERO_BYTES32,
  282. constants.ZERO_BYTES32,
  283. 3600,
  284. { from: owner },
  285. );
  286. await time.increase(3600);
  287. await expectRevert(
  288. this.timelock.execute(
  289. this.mock.address,
  290. web3.utils.toWei('0'),
  291. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  292. constants.ZERO_BYTES32,
  293. constants.ZERO_BYTES32,
  294. { from: owner },
  295. ),
  296. 'TimelockController: underlying transaction reverted',
  297. );
  298. });
  299. });
  300. describe('updateTimelock', function () {
  301. beforeEach(async function () {
  302. this.newTimelock = await Timelock.new(
  303. 3600,
  304. [ this.mock.address ],
  305. [ this.mock.address ],
  306. constants.ZERO_ADDRESS,
  307. );
  308. });
  309. it('is protected', async function () {
  310. await expectRevert(
  311. this.mock.updateTimelock(this.newTimelock.address),
  312. 'Governor: onlyGovernance',
  313. );
  314. });
  315. it('can be executed through governance to', async function () {
  316. this.helper.setProposal([
  317. {
  318. target: this.mock.address,
  319. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  320. },
  321. ], '<proposal description>');
  322. await this.helper.propose();
  323. await this.helper.waitForSnapshot();
  324. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  325. await this.helper.waitForDeadline();
  326. await this.helper.queue();
  327. await this.helper.waitForEta();
  328. const txExecute = await this.helper.execute();
  329. expectEvent(
  330. txExecute,
  331. 'TimelockChange',
  332. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  333. );
  334. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  335. });
  336. });
  337. });
  338. it('clear queue of pending governor calls', async function () {
  339. this.helper.setProposal([
  340. {
  341. target: this.mock.address,
  342. data: this.mock.contract.methods.nonGovernanceFunction().encodeABI(),
  343. },
  344. ], '<proposal description>');
  345. await this.helper.propose();
  346. await this.helper.waitForSnapshot();
  347. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  348. await this.helper.waitForDeadline();
  349. await this.helper.queue();
  350. await this.helper.waitForEta();
  351. await this.helper.execute();
  352. // This path clears _governanceCall as part of the afterExecute call,
  353. // but we have not way to check that the cleanup actually happened other
  354. // then coverage reports.
  355. });
  356. });