GovernorTimelockControl.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const {
  5. runGovernorWorkflow,
  6. } = require('../GovernorWorkflow.behavior');
  7. const {
  8. shouldSupportInterfaces,
  9. } = require('../../utils/introspection/SupportsInterface.behavior');
  10. const Token = artifacts.require('ERC20VotesMock');
  11. const Timelock = artifacts.require('TimelockController');
  12. const Governor = artifacts.require('GovernorTimelockControlMock');
  13. const CallReceiver = artifacts.require('CallReceiverMock');
  14. contract('GovernorTimelockControl', function (accounts) {
  15. const [ admin, voter, other ] = accounts;
  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. beforeEach(async function () {
  22. const [ deployer ] = await web3.eth.getAccounts();
  23. this.token = await Token.new(tokenName, tokenSymbol);
  24. this.timelock = await Timelock.new(3600, [], []);
  25. this.mock = await Governor.new(name, this.token.address, 4, 16, this.timelock.address, 0);
  26. this.receiver = await CallReceiver.new();
  27. // normal setup: governor and admin are proposers, everyone is executor, timelock is its own admin
  28. await this.timelock.grantRole(await this.timelock.PROPOSER_ROLE(), this.mock.address);
  29. await this.timelock.grantRole(await this.timelock.PROPOSER_ROLE(), admin);
  30. await this.timelock.grantRole(await this.timelock.EXECUTOR_ROLE(), constants.ZERO_ADDRESS);
  31. await this.timelock.revokeRole(await this.timelock.TIMELOCK_ADMIN_ROLE(), deployer);
  32. await this.token.mint(voter, tokenSupply);
  33. await this.token.delegate(voter, { from: voter });
  34. });
  35. shouldSupportInterfaces([
  36. 'ERC165',
  37. 'Governor',
  38. 'GovernorWithParams',
  39. 'GovernorTimelock',
  40. ]);
  41. it('doesn\'t accept ether transfers', async function () {
  42. await expectRevert.unspecified(web3.eth.sendTransaction({ from: voter, to: this.mock.address, value: 1 }));
  43. });
  44. it('post deployment check', async function () {
  45. expect(await this.mock.name()).to.be.equal(name);
  46. expect(await this.mock.token()).to.be.equal(this.token.address);
  47. expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
  48. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
  49. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  50. expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
  51. });
  52. describe('nominal', function () {
  53. beforeEach(async function () {
  54. this.settings = {
  55. proposal: [
  56. [ this.receiver.address ],
  57. [ web3.utils.toWei('0') ],
  58. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  59. '<proposal description>',
  60. ],
  61. voters: [
  62. { voter: voter, support: Enums.VoteType.For },
  63. ],
  64. steps: {
  65. queue: { delay: 3600 },
  66. },
  67. };
  68. });
  69. afterEach(async function () {
  70. const timelockid = await this.timelock.hashOperationBatch(
  71. ...this.settings.proposal.slice(0, 3),
  72. '0x0',
  73. this.descriptionHash,
  74. );
  75. expectEvent(
  76. this.receipts.propose,
  77. 'ProposalCreated',
  78. { proposalId: this.id },
  79. );
  80. expectEvent(
  81. this.receipts.queue,
  82. 'ProposalQueued',
  83. { proposalId: this.id },
  84. );
  85. await expectEvent.inTransaction(
  86. this.receipts.queue.transactionHash,
  87. this.timelock,
  88. 'CallScheduled',
  89. { id: timelockid },
  90. );
  91. expectEvent(
  92. this.receipts.execute,
  93. 'ProposalExecuted',
  94. { proposalId: this.id },
  95. );
  96. await expectEvent.inTransaction(
  97. this.receipts.execute.transactionHash,
  98. this.timelock,
  99. 'CallExecuted',
  100. { id: timelockid },
  101. );
  102. await expectEvent.inTransaction(
  103. this.receipts.execute.transactionHash,
  104. this.receiver,
  105. 'MockFunctionCalled',
  106. );
  107. });
  108. runGovernorWorkflow();
  109. });
  110. describe('executed by other proposer', function () {
  111. beforeEach(async function () {
  112. this.settings = {
  113. proposal: [
  114. [ this.receiver.address ],
  115. [ web3.utils.toWei('0') ],
  116. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  117. '<proposal description>',
  118. ],
  119. voters: [
  120. { voter: voter, support: Enums.VoteType.For },
  121. ],
  122. steps: {
  123. queue: { delay: 3600 },
  124. execute: { enable: false },
  125. },
  126. };
  127. });
  128. afterEach(async function () {
  129. await this.timelock.executeBatch(
  130. ...this.settings.proposal.slice(0, 3),
  131. '0x0',
  132. this.descriptionHash,
  133. );
  134. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
  135. await expectRevert(
  136. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  137. 'Governor: proposal not successful',
  138. );
  139. });
  140. runGovernorWorkflow();
  141. });
  142. describe('not queued', function () {
  143. beforeEach(async function () {
  144. this.settings = {
  145. proposal: [
  146. [ this.receiver.address ],
  147. [ web3.utils.toWei('0') ],
  148. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  149. '<proposal description>',
  150. ],
  151. voters: [
  152. { voter: voter, support: Enums.VoteType.For },
  153. ],
  154. steps: {
  155. queue: { enable: false },
  156. execute: { error: 'TimelockController: operation is not ready' },
  157. },
  158. };
  159. });
  160. afterEach(async function () {
  161. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  162. });
  163. runGovernorWorkflow();
  164. });
  165. describe('to early', function () {
  166. beforeEach(async function () {
  167. this.settings = {
  168. proposal: [
  169. [ this.receiver.address ],
  170. [ web3.utils.toWei('0') ],
  171. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  172. '<proposal description>',
  173. ],
  174. voters: [
  175. { voter: voter, support: Enums.VoteType.For },
  176. ],
  177. steps: {
  178. execute: { error: 'TimelockController: operation is not ready' },
  179. },
  180. };
  181. });
  182. afterEach(async function () {
  183. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  184. });
  185. runGovernorWorkflow();
  186. });
  187. describe('re-queue / re-execute', function () {
  188. beforeEach(async function () {
  189. this.settings = {
  190. proposal: [
  191. [ this.receiver.address ],
  192. [ web3.utils.toWei('0') ],
  193. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  194. '<proposal description>',
  195. ],
  196. voters: [
  197. { voter: voter, support: Enums.VoteType.For },
  198. ],
  199. steps: {
  200. queue: { delay: 3600 },
  201. },
  202. };
  203. });
  204. afterEach(async function () {
  205. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
  206. await expectRevert(
  207. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  208. 'Governor: proposal not successful',
  209. );
  210. await expectRevert(
  211. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  212. 'Governor: proposal not successful',
  213. );
  214. });
  215. runGovernorWorkflow();
  216. });
  217. describe('cancel before queue prevents scheduling', function () {
  218. beforeEach(async function () {
  219. this.settings = {
  220. proposal: [
  221. [ this.receiver.address ],
  222. [ web3.utils.toWei('0') ],
  223. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  224. '<proposal description>',
  225. ],
  226. voters: [
  227. { voter: voter, support: Enums.VoteType.For },
  228. ],
  229. steps: {
  230. queue: { enable: false },
  231. execute: { enable: false },
  232. },
  233. };
  234. });
  235. afterEach(async function () {
  236. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
  237. expectEvent(
  238. await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  239. 'ProposalCanceled',
  240. { proposalId: this.id },
  241. );
  242. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  243. await expectRevert(
  244. this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  245. 'Governor: proposal not successful',
  246. );
  247. });
  248. runGovernorWorkflow();
  249. });
  250. describe('cancel after queue prevents execution', function () {
  251. beforeEach(async function () {
  252. this.settings = {
  253. proposal: [
  254. [ this.receiver.address ],
  255. [ web3.utils.toWei('0') ],
  256. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  257. '<proposal description>',
  258. ],
  259. voters: [
  260. { voter: voter, support: Enums.VoteType.For },
  261. ],
  262. steps: {
  263. queue: { delay: 3600 },
  264. execute: { enable: false },
  265. },
  266. };
  267. });
  268. afterEach(async function () {
  269. const timelockid = await this.timelock.hashOperationBatch(
  270. ...this.settings.proposal.slice(0, 3),
  271. '0x0',
  272. this.descriptionHash,
  273. );
  274. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  275. const receipt = await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash);
  276. expectEvent(
  277. receipt,
  278. 'ProposalCanceled',
  279. { proposalId: this.id },
  280. );
  281. await expectEvent.inTransaction(
  282. receipt.receipt.transactionHash,
  283. this.timelock,
  284. 'Cancelled',
  285. { id: timelockid },
  286. );
  287. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  288. await expectRevert(
  289. this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
  290. 'Governor: proposal not successful',
  291. );
  292. });
  293. runGovernorWorkflow();
  294. });
  295. describe('relay', function () {
  296. beforeEach(async function () {
  297. await this.token.mint(this.mock.address, 1);
  298. this.call = [
  299. this.token.address,
  300. 0,
  301. this.token.contract.methods.transfer(other, 1).encodeABI(),
  302. ];
  303. });
  304. it('protected', async function () {
  305. await expectRevert(
  306. this.mock.relay(...this.call),
  307. 'Governor: onlyGovernance',
  308. );
  309. });
  310. it('protected against other proposers', async function () {
  311. await this.timelock.schedule(
  312. this.mock.address,
  313. web3.utils.toWei('0'),
  314. this.mock.contract.methods.relay(...this.call).encodeABI(),
  315. constants.ZERO_BYTES32,
  316. constants.ZERO_BYTES32,
  317. 3600,
  318. { from: admin },
  319. );
  320. await time.increase(3600);
  321. await expectRevert(
  322. this.timelock.execute(
  323. this.mock.address,
  324. web3.utils.toWei('0'),
  325. this.mock.contract.methods.relay(...this.call).encodeABI(),
  326. constants.ZERO_BYTES32,
  327. constants.ZERO_BYTES32,
  328. { from: admin },
  329. ),
  330. 'TimelockController: underlying transaction reverted',
  331. );
  332. });
  333. describe('using workflow', function () {
  334. beforeEach(async function () {
  335. this.settings = {
  336. proposal: [
  337. [
  338. this.mock.address,
  339. ],
  340. [
  341. web3.utils.toWei('0'),
  342. ],
  343. [
  344. this.mock.contract.methods.relay(...this.call).encodeABI(),
  345. ],
  346. '<proposal description>',
  347. ],
  348. voters: [
  349. { voter: voter, support: Enums.VoteType.For },
  350. ],
  351. steps: {
  352. queue: { delay: 7 * 86400 },
  353. },
  354. };
  355. expect(await this.token.balanceOf(this.mock.address), 1);
  356. expect(await this.token.balanceOf(other), 0);
  357. });
  358. afterEach(async function () {
  359. expect(await this.token.balanceOf(this.mock.address), 0);
  360. expect(await this.token.balanceOf(other), 1);
  361. });
  362. runGovernorWorkflow();
  363. });
  364. });
  365. describe('cancel on timelock is forwarded in state', function () {
  366. beforeEach(async function () {
  367. this.settings = {
  368. proposal: [
  369. [ this.receiver.address ],
  370. [ web3.utils.toWei('0') ],
  371. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  372. '<proposal description>',
  373. ],
  374. voters: [
  375. { voter: voter, support: Enums.VoteType.For },
  376. ],
  377. steps: {
  378. queue: { delay: 3600 },
  379. execute: { enable: false },
  380. },
  381. };
  382. });
  383. afterEach(async function () {
  384. const timelockid = await this.timelock.hashOperationBatch(
  385. ...this.settings.proposal.slice(0, 3),
  386. '0x0',
  387. this.descriptionHash,
  388. );
  389. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  390. const receipt = await this.timelock.cancel(timelockid, { from: admin });
  391. expectEvent(
  392. receipt,
  393. 'Cancelled',
  394. { id: timelockid },
  395. );
  396. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  397. });
  398. runGovernorWorkflow();
  399. });
  400. describe('updateTimelock', function () {
  401. beforeEach(async function () {
  402. this.newTimelock = await Timelock.new(3600, [], []);
  403. });
  404. it('protected', async function () {
  405. await expectRevert(
  406. this.mock.updateTimelock(this.newTimelock.address),
  407. 'Governor: onlyGovernance',
  408. );
  409. });
  410. describe('using workflow', function () {
  411. beforeEach(async function () {
  412. this.settings = {
  413. proposal: [
  414. [ this.mock.address ],
  415. [ web3.utils.toWei('0') ],
  416. [ this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI() ],
  417. '<proposal description>',
  418. ],
  419. voters: [
  420. { voter: voter, support: Enums.VoteType.For },
  421. ],
  422. steps: {
  423. queue: { delay: 3600 },
  424. },
  425. };
  426. });
  427. afterEach(async function () {
  428. expectEvent(
  429. this.receipts.propose,
  430. 'ProposalCreated',
  431. { proposalId: this.id },
  432. );
  433. expectEvent(
  434. this.receipts.execute,
  435. 'ProposalExecuted',
  436. { proposalId: this.id },
  437. );
  438. expectEvent(
  439. this.receipts.execute,
  440. 'TimelockChange',
  441. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  442. );
  443. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  444. });
  445. runGovernorWorkflow();
  446. });
  447. });
  448. describe('clear queue of pending governor calls', function () {
  449. beforeEach(async function () {
  450. this.settings = {
  451. proposal: [
  452. [ this.mock.address ],
  453. [ web3.utils.toWei('0') ],
  454. [ this.mock.contract.methods.nonGovernanceFunction().encodeABI() ],
  455. '<proposal description>',
  456. ],
  457. voters: [
  458. { voter: voter, support: Enums.VoteType.For },
  459. ],
  460. steps: {
  461. queue: { delay: 3600 },
  462. },
  463. };
  464. });
  465. afterEach(async function () {
  466. expectEvent(
  467. this.receipts.execute,
  468. 'ProposalExecuted',
  469. { proposalId: this.id },
  470. );
  471. });
  472. runGovernorWorkflow();
  473. });
  474. });