GovernorTimelockControl.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. const { constants, expectEvent, expectRevert } = 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 ] = 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 is proposer, 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('cancel on timelock is forwarded in state', function () {
  295. beforeEach(async function () {
  296. this.settings = {
  297. proposal: [
  298. [ this.receiver.address ],
  299. [ web3.utils.toWei('0') ],
  300. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  301. '<proposal description>',
  302. ],
  303. voters: [
  304. { voter: voter, support: Enums.VoteType.For },
  305. ],
  306. steps: {
  307. queue: { delay: 3600 },
  308. execute: { enable: false },
  309. },
  310. };
  311. });
  312. afterEach(async function () {
  313. const timelockid = await this.timelock.hashOperationBatch(
  314. ...this.settings.proposal.slice(0, 3),
  315. '0x0',
  316. this.descriptionHash,
  317. );
  318. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
  319. const receipt = await this.timelock.cancel(timelockid, { from: admin });
  320. expectEvent(
  321. receipt,
  322. 'Cancelled',
  323. { id: timelockid },
  324. );
  325. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
  326. });
  327. runGovernorWorkflow();
  328. });
  329. describe('updateTimelock', function () {
  330. beforeEach(async function () {
  331. this.newTimelock = await Timelock.new(3600, [], []);
  332. });
  333. it('protected', async function () {
  334. await expectRevert(
  335. this.mock.updateTimelock(this.newTimelock.address),
  336. 'Governor: onlyGovernance',
  337. );
  338. });
  339. describe('using workflow', function () {
  340. beforeEach(async function () {
  341. this.settings = {
  342. proposal: [
  343. [ this.mock.address ],
  344. [ web3.utils.toWei('0') ],
  345. [ this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI() ],
  346. '<proposal description>',
  347. ],
  348. voters: [
  349. { voter: voter, support: Enums.VoteType.For },
  350. ],
  351. steps: {
  352. queue: { delay: 3600 },
  353. },
  354. };
  355. });
  356. afterEach(async function () {
  357. expectEvent(
  358. this.receipts.propose,
  359. 'ProposalCreated',
  360. { proposalId: this.id },
  361. );
  362. expectEvent(
  363. this.receipts.execute,
  364. 'ProposalExecuted',
  365. { proposalId: this.id },
  366. );
  367. expectEvent(
  368. this.receipts.execute,
  369. 'TimelockChange',
  370. { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
  371. );
  372. expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
  373. });
  374. runGovernorWorkflow();
  375. });
  376. });
  377. });