GovernorTimelockControl.test.js 16 KB

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