GovernorTimelockControl.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. const { 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 { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
  6. const Timelock = artifacts.require('TimelockController');
  7. const Governor = artifacts.require('$GovernorTimelockControlMock');
  8. const CallReceiver = artifacts.require('CallReceiverMock');
  9. const TOKENS = [
  10. { Token: artifacts.require('$ERC20Votes'), mode: 'blocknumber' },
  11. { Token: artifacts.require('$ERC20VotesTimestampMock'), mode: 'timestamp' },
  12. ];
  13. contract('GovernorTimelockControl', function (accounts) {
  14. const [owner, voter1, voter2, voter3, voter4, other] = accounts;
  15. const TIMELOCK_ADMIN_ROLE = web3.utils.soliditySha3('TIMELOCK_ADMIN_ROLE');
  16. const PROPOSER_ROLE = web3.utils.soliditySha3('PROPOSER_ROLE');
  17. const EXECUTOR_ROLE = web3.utils.soliditySha3('EXECUTOR_ROLE');
  18. const CANCELLER_ROLE = web3.utils.soliditySha3('CANCELLER_ROLE');
  19. const name = 'OZ-Governor';
  20. // const version = '1';
  21. const tokenName = 'MockToken';
  22. const tokenSymbol = 'MTKN';
  23. const tokenSupply = web3.utils.toWei('100');
  24. const votingDelay = web3.utils.toBN(4);
  25. const votingPeriod = web3.utils.toBN(16);
  26. const value = web3.utils.toWei('1');
  27. for (const { mode, Token } of TOKENS) {
  28. describe(`using ${Token._json.contractName}`, function () {
  29. beforeEach(async function () {
  30. const [deployer] = await web3.eth.getAccounts();
  31. this.token = await Token.new(tokenName, tokenSymbol, tokenName);
  32. this.timelock = await Timelock.new(3600, [], [], deployer);
  33. this.mock = await Governor.new(
  34. name,
  35. votingDelay,
  36. votingPeriod,
  37. 0,
  38. this.timelock.address,
  39. this.token.address,
  40. 0,
  41. );
  42. this.receiver = await CallReceiver.new();
  43. this.helper = new GovernorHelper(this.mock, mode);
  44. this.TIMELOCK_ADMIN_ROLE = await this.timelock.TIMELOCK_ADMIN_ROLE();
  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(TIMELOCK_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 expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  142. });
  143. });
  144. describe('on execute', function () {
  145. it('if not queued', 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(+1);
  150. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  151. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  152. });
  153. it('if too early', 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. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  160. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  161. });
  162. it('if already executed', 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. await this.helper.queue();
  168. await this.helper.waitForEta();
  169. await this.helper.execute();
  170. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  171. });
  172. it('if already executed by another proposer', 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.timelock.executeBatch(
  180. ...this.proposal.shortProposal.slice(0, 3),
  181. '0x0',
  182. this.proposal.shortProposal[3],
  183. );
  184. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  185. });
  186. });
  187. });
  188. describe('cancel', function () {
  189. it('cancel before queue prevents scheduling', 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. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  195. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  196. await expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  197. });
  198. it('cancel after queue prevents executing', async function () {
  199. await this.helper.propose();
  200. await this.helper.waitForSnapshot();
  201. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  202. await this.helper.waitForDeadline();
  203. await this.helper.queue();
  204. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  205. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  206. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  207. });
  208. it('cancel on timelock is reflected on governor', async function () {
  209. await this.helper.propose();
  210. await this.helper.waitForSnapshot();
  211. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  212. await this.helper.waitForDeadline();
  213. await this.helper.queue();
  214. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  215. expectEvent(await this.timelock.cancel(this.proposal.timelockid, { from: owner }), 'Cancelled', {
  216. id: this.proposal.timelockid,
  217. });
  218. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  219. });
  220. });
  221. describe('onlyGovernance', function () {
  222. describe('relay', function () {
  223. beforeEach(async function () {
  224. await this.token.$_mint(this.mock.address, 1);
  225. });
  226. it('is protected', async function () {
  227. await expectRevert(
  228. this.mock.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI()),
  229. 'Governor: onlyGovernance',
  230. );
  231. });
  232. it('can be executed through governance', async function () {
  233. this.helper.setProposal(
  234. [
  235. {
  236. target: this.mock.address,
  237. data: this.mock.contract.methods
  238. .relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
  239. .encodeABI(),
  240. },
  241. ],
  242. '<proposal description>',
  243. );
  244. expect(await this.token.balanceOf(this.mock.address), 1);
  245. expect(await this.token.balanceOf(other), 0);
  246. await this.helper.propose();
  247. await this.helper.waitForSnapshot();
  248. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  249. await this.helper.waitForDeadline();
  250. await this.helper.queue();
  251. await this.helper.waitForEta();
  252. const txExecute = await this.helper.execute();
  253. expect(await this.token.balanceOf(this.mock.address), 0);
  254. expect(await this.token.balanceOf(other), 1);
  255. await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
  256. from: this.mock.address,
  257. to: other,
  258. value: '1',
  259. });
  260. });
  261. it('is payable and can transfer eth to EOA', async function () {
  262. const t2g = web3.utils.toBN(128); // timelock to governor
  263. const g2o = web3.utils.toBN(100); // governor to eoa (other)
  264. this.helper.setProposal(
  265. [
  266. {
  267. target: this.mock.address,
  268. value: t2g,
  269. data: this.mock.contract.methods.relay(other, g2o, '0x').encodeABI(),
  270. },
  271. ],
  272. '<proposal description>',
  273. );
  274. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  275. const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
  276. const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
  277. await this.helper.propose();
  278. await this.helper.waitForSnapshot();
  279. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  280. await this.helper.waitForDeadline();
  281. await this.helper.queue();
  282. await this.helper.waitForEta();
  283. await this.helper.execute();
  284. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(
  285. timelockBalance.sub(t2g),
  286. );
  287. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
  288. expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
  289. });
  290. it('protected against other proposers', async function () {
  291. await this.timelock.schedule(
  292. this.mock.address,
  293. web3.utils.toWei('0'),
  294. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  295. constants.ZERO_BYTES32,
  296. constants.ZERO_BYTES32,
  297. 3600,
  298. { from: owner },
  299. );
  300. await time.increase(3600);
  301. await expectRevert(
  302. this.timelock.execute(
  303. this.mock.address,
  304. web3.utils.toWei('0'),
  305. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  306. constants.ZERO_BYTES32,
  307. constants.ZERO_BYTES32,
  308. { from: owner },
  309. ),
  310. 'TimelockController: underlying transaction reverted',
  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 expectRevert(this.mock.updateTimelock(this.newTimelock.address), 'Governor: onlyGovernance');
  325. });
  326. it('can be executed through governance to', async function () {
  327. this.helper.setProposal(
  328. [
  329. {
  330. target: this.mock.address,
  331. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  332. },
  333. ],
  334. '<proposal description>',
  335. );
  336. await this.helper.propose();
  337. await this.helper.waitForSnapshot();
  338. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  339. await this.helper.waitForDeadline();
  340. await this.helper.queue();
  341. await this.helper.waitForEta();
  342. const txExecute = await this.helper.execute();
  343. expectEvent(txExecute, 'TimelockChange', {
  344. oldTimelock: this.timelock.address,
  345. newTimelock: this.newTimelock.address,
  346. });
  347. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  348. });
  349. });
  350. });
  351. it('clear queue of pending governor calls', async function () {
  352. this.helper.setProposal(
  353. [
  354. {
  355. target: this.mock.address,
  356. data: this.mock.contract.methods.nonGovernanceFunction().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. await this.helper.execute();
  368. // This path clears _governanceCall as part of the afterExecute call,
  369. // but we have not way to check that the cleanup actually happened other
  370. // then coverage reports.
  371. });
  372. });
  373. });
  374. });
  375. }
  376. });