GovernorTimelockControl.test.js 14 KB

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