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 DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  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, version);
  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.PROPOSER_ROLE = await this.timelock.PROPOSER_ROLE();
  45. this.EXECUTOR_ROLE = await this.timelock.EXECUTOR_ROLE();
  46. this.CANCELLER_ROLE = await this.timelock.CANCELLER_ROLE();
  47. await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
  48. // normal setup: governor is proposer, everyone is executor, timelock is its own admin
  49. await this.timelock.grantRole(PROPOSER_ROLE, this.mock.address);
  50. await this.timelock.grantRole(PROPOSER_ROLE, owner);
  51. await this.timelock.grantRole(CANCELLER_ROLE, this.mock.address);
  52. await this.timelock.grantRole(CANCELLER_ROLE, owner);
  53. await this.timelock.grantRole(EXECUTOR_ROLE, constants.ZERO_ADDRESS);
  54. await this.timelock.revokeRole(DEFAULT_ADMIN_ROLE, deployer);
  55. await this.token.$_mint(owner, tokenSupply);
  56. await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
  57. await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
  58. await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
  59. await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
  60. // default proposal
  61. this.proposal = this.helper.setProposal(
  62. [
  63. {
  64. target: this.receiver.address,
  65. value,
  66. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  67. },
  68. ],
  69. '<proposal description>',
  70. );
  71. this.proposal.timelockid = await this.timelock.hashOperationBatch(
  72. ...this.proposal.shortProposal.slice(0, 3),
  73. '0x0',
  74. this.proposal.shortProposal[3],
  75. );
  76. });
  77. shouldSupportInterfaces(['ERC165', 'Governor', 'GovernorWithParams', 'GovernorTimelock']);
  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. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallSalt', {
  103. id: this.proposal.timelockid,
  104. });
  105. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  106. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', { id: this.proposal.timelockid });
  107. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  108. });
  109. describe('should revert', function () {
  110. describe('on queue', function () {
  111. it('if already queued', async function () {
  112. await this.helper.propose();
  113. await this.helper.waitForSnapshot();
  114. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  115. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
  116. await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
  117. await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
  118. await this.helper.waitForDeadline();
  119. const txQueue = await this.helper.queue();
  120. await this.helper.waitForEta();
  121. const txExecute = await this.helper.execute();
  122. expectEvent(txQueue, 'ProposalQueued', { proposalId: this.proposal.id });
  123. await expectEvent.inTransaction(txQueue.tx, this.timelock, 'CallScheduled', {
  124. id: this.proposal.timelockid,
  125. });
  126. expectEvent(txExecute, 'ProposalExecuted', { proposalId: this.proposal.id });
  127. await expectEvent.inTransaction(txExecute.tx, this.timelock, 'CallExecuted', {
  128. id: this.proposal.timelockid,
  129. });
  130. await expectEvent.inTransaction(txExecute.tx, this.receiver, 'MockFunctionCalled');
  131. });
  132. describe('should revert', function () {
  133. describe('on queue', function () {
  134. it('if already queued', async function () {
  135. await this.helper.propose();
  136. await this.helper.waitForSnapshot();
  137. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  138. await this.helper.waitForDeadline();
  139. await this.helper.queue();
  140. await expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  141. });
  142. });
  143. describe('on execute', function () {
  144. it('if not queued', async function () {
  145. await this.helper.propose();
  146. await this.helper.waitForSnapshot();
  147. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  148. await this.helper.waitForDeadline(+1);
  149. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  150. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  151. });
  152. it('if too early', async function () {
  153. await this.helper.propose();
  154. await this.helper.waitForSnapshot();
  155. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  156. await this.helper.waitForDeadline();
  157. await this.helper.queue();
  158. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  159. await expectRevert(this.helper.execute(), 'TimelockController: operation is not ready');
  160. });
  161. it('if already executed', async function () {
  162. await this.helper.propose();
  163. await this.helper.waitForSnapshot();
  164. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  165. await this.helper.waitForDeadline();
  166. await this.helper.queue();
  167. await this.helper.waitForEta();
  168. await this.helper.execute();
  169. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  170. });
  171. it('if already executed by another proposer', async function () {
  172. await this.helper.propose();
  173. await this.helper.waitForSnapshot();
  174. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  175. await this.helper.waitForDeadline();
  176. await this.helper.queue();
  177. await this.helper.waitForEta();
  178. await this.timelock.executeBatch(
  179. ...this.proposal.shortProposal.slice(0, 3),
  180. '0x0',
  181. this.proposal.shortProposal[3],
  182. );
  183. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  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 expectRevert(this.helper.queue(), 'Governor: proposal not successful');
  196. });
  197. it('cancel after queue prevents executing', async function () {
  198. await this.helper.propose();
  199. await this.helper.waitForSnapshot();
  200. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  201. await this.helper.waitForDeadline();
  202. await this.helper.queue();
  203. expectEvent(await this.helper.cancel('internal'), 'ProposalCanceled', { proposalId: this.proposal.id });
  204. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  205. await expectRevert(this.helper.execute(), 'Governor: proposal not successful');
  206. });
  207. it('cancel on timelock is reflected on governor', 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. await this.helper.queue();
  213. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  214. expectEvent(await this.timelock.cancel(this.proposal.timelockid, { from: owner }), 'Cancelled', {
  215. id: this.proposal.timelockid,
  216. });
  217. expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  218. });
  219. });
  220. describe('onlyGovernance', function () {
  221. describe('relay', function () {
  222. beforeEach(async function () {
  223. await this.token.$_mint(this.mock.address, 1);
  224. });
  225. it('is protected', async function () {
  226. await expectRevert(
  227. this.mock.relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI()),
  228. 'Governor: onlyGovernance',
  229. );
  230. });
  231. it('can be executed through governance', async function () {
  232. this.helper.setProposal(
  233. [
  234. {
  235. target: this.mock.address,
  236. data: this.mock.contract.methods
  237. .relay(this.token.address, 0, this.token.contract.methods.transfer(other, 1).encodeABI())
  238. .encodeABI(),
  239. },
  240. ],
  241. '<proposal description>',
  242. );
  243. expect(await this.token.balanceOf(this.mock.address), 1);
  244. expect(await this.token.balanceOf(other), 0);
  245. await this.helper.propose();
  246. await this.helper.waitForSnapshot();
  247. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  248. await this.helper.waitForDeadline();
  249. await this.helper.queue();
  250. await this.helper.waitForEta();
  251. const txExecute = await this.helper.execute();
  252. expect(await this.token.balanceOf(this.mock.address), 0);
  253. expect(await this.token.balanceOf(other), 1);
  254. await expectEvent.inTransaction(txExecute.tx, this.token, 'Transfer', {
  255. from: this.mock.address,
  256. to: other,
  257. value: '1',
  258. });
  259. });
  260. it('is payable and can transfer eth to EOA', async function () {
  261. const t2g = web3.utils.toBN(128); // timelock to governor
  262. const g2o = web3.utils.toBN(100); // governor to eoa (other)
  263. this.helper.setProposal(
  264. [
  265. {
  266. target: this.mock.address,
  267. value: t2g,
  268. data: this.mock.contract.methods.relay(other, g2o, '0x').encodeABI(),
  269. },
  270. ],
  271. '<proposal description>',
  272. );
  273. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(web3.utils.toBN(0));
  274. const timelockBalance = await web3.eth.getBalance(this.timelock.address).then(web3.utils.toBN);
  275. const otherBalance = await web3.eth.getBalance(other).then(web3.utils.toBN);
  276. await this.helper.propose();
  277. await this.helper.waitForSnapshot();
  278. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  279. await this.helper.waitForDeadline();
  280. await this.helper.queue();
  281. await this.helper.waitForEta();
  282. await this.helper.execute();
  283. expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(
  284. timelockBalance.sub(t2g),
  285. );
  286. expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal(t2g.sub(g2o));
  287. expect(await web3.eth.getBalance(other)).to.be.bignumber.equal(otherBalance.add(g2o));
  288. });
  289. it('protected against other proposers', async function () {
  290. await this.timelock.schedule(
  291. this.mock.address,
  292. web3.utils.toWei('0'),
  293. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  294. constants.ZERO_BYTES32,
  295. constants.ZERO_BYTES32,
  296. 3600,
  297. { from: owner },
  298. );
  299. await time.increase(3600);
  300. await expectRevert(
  301. this.timelock.execute(
  302. this.mock.address,
  303. web3.utils.toWei('0'),
  304. this.mock.contract.methods.relay(constants.ZERO_ADDRESS, 0, '0x').encodeABI(),
  305. constants.ZERO_BYTES32,
  306. constants.ZERO_BYTES32,
  307. { from: owner },
  308. ),
  309. 'TimelockController: underlying transaction reverted',
  310. );
  311. });
  312. });
  313. describe('updateTimelock', function () {
  314. beforeEach(async function () {
  315. this.newTimelock = await Timelock.new(
  316. 3600,
  317. [this.mock.address],
  318. [this.mock.address],
  319. constants.ZERO_ADDRESS,
  320. );
  321. });
  322. it('is protected', async function () {
  323. await expectRevert(this.mock.updateTimelock(this.newTimelock.address), 'Governor: onlyGovernance');
  324. });
  325. it('can be executed through governance to', async function () {
  326. this.helper.setProposal(
  327. [
  328. {
  329. target: this.mock.address,
  330. data: this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI(),
  331. },
  332. ],
  333. '<proposal description>',
  334. );
  335. await this.helper.propose();
  336. await this.helper.waitForSnapshot();
  337. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  338. await this.helper.waitForDeadline();
  339. await this.helper.queue();
  340. await this.helper.waitForEta();
  341. const txExecute = await this.helper.execute();
  342. expectEvent(txExecute, 'TimelockChange', {
  343. oldTimelock: this.timelock.address,
  344. newTimelock: this.newTimelock.address,
  345. });
  346. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  347. });
  348. });
  349. });
  350. it('clear queue of pending governor calls', async function () {
  351. this.helper.setProposal(
  352. [
  353. {
  354. target: this.mock.address,
  355. data: this.mock.contract.methods.nonGovernanceFunction().encodeABI(),
  356. },
  357. ],
  358. '<proposal description>',
  359. );
  360. await this.helper.propose();
  361. await this.helper.waitForSnapshot();
  362. await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
  363. await this.helper.waitForDeadline();
  364. await this.helper.queue();
  365. await this.helper.waitForEta();
  366. await this.helper.execute();
  367. // This path clears _governanceCall as part of the afterExecute call,
  368. // but we have not way to check that the cleanup actually happened other
  369. // then coverage reports.
  370. });
  371. });
  372. });
  373. });
  374. }
  375. });