GovernorTimelockControl.test.js 15 KB

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