GovernorPreventLateQuorum.test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const { BN, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const Enums = require('../../helpers/enums');
  3. const {
  4. runGovernorWorkflow,
  5. } = require('../GovernorWorkflow.behavior');
  6. const Token = artifacts.require('ERC20VotesCompMock');
  7. const Governor = artifacts.require('GovernorPreventLateQuorumMock');
  8. const CallReceiver = artifacts.require('CallReceiverMock');
  9. contract('GovernorPreventLateQuorum', function (accounts) {
  10. const [ owner, proposer, voter1, voter2, voter3, voter4 ] = accounts;
  11. const name = 'OZ-Governor';
  12. // const version = '1';
  13. const tokenName = 'MockToken';
  14. const tokenSymbol = 'MTKN';
  15. const tokenSupply = web3.utils.toWei('100');
  16. const votingDelay = new BN(4);
  17. const votingPeriod = new BN(16);
  18. const lateQuorumVoteExtension = new BN(8);
  19. const quorum = web3.utils.toWei('1');
  20. beforeEach(async function () {
  21. this.owner = owner;
  22. this.token = await Token.new(tokenName, tokenSymbol);
  23. this.mock = await Governor.new(
  24. name,
  25. this.token.address,
  26. votingDelay,
  27. votingPeriod,
  28. quorum,
  29. lateQuorumVoteExtension,
  30. );
  31. this.receiver = await CallReceiver.new();
  32. await this.token.mint(owner, tokenSupply);
  33. await this.token.delegate(voter1, { from: voter1 });
  34. await this.token.delegate(voter2, { from: voter2 });
  35. await this.token.delegate(voter3, { from: voter3 });
  36. await this.token.delegate(voter4, { from: voter4 });
  37. });
  38. it('deployment check', async function () {
  39. expect(await this.mock.name()).to.be.equal(name);
  40. expect(await this.mock.token()).to.be.equal(this.token.address);
  41. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  42. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  43. expect(await this.mock.quorum(0)).to.be.bignumber.equal(quorum);
  44. expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal(lateQuorumVoteExtension);
  45. });
  46. describe('nominal is unaffected', function () {
  47. beforeEach(async function () {
  48. this.settings = {
  49. proposal: [
  50. [ this.receiver.address ],
  51. [ 0 ],
  52. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  53. '<proposal description>',
  54. ],
  55. proposer,
  56. tokenHolder: owner,
  57. voters: [
  58. { voter: voter1, weight: web3.utils.toWei('1'), support: Enums.VoteType.For, reason: 'This is nice' },
  59. { voter: voter2, weight: web3.utils.toWei('7'), support: Enums.VoteType.For },
  60. { voter: voter3, weight: web3.utils.toWei('5'), support: Enums.VoteType.Against },
  61. { voter: voter4, weight: web3.utils.toWei('2'), support: Enums.VoteType.Abstain },
  62. ],
  63. };
  64. });
  65. afterEach(async function () {
  66. expect(await this.mock.hasVoted(this.id, owner)).to.be.equal(false);
  67. expect(await this.mock.hasVoted(this.id, voter1)).to.be.equal(true);
  68. expect(await this.mock.hasVoted(this.id, voter2)).to.be.equal(true);
  69. await this.mock.proposalVotes(this.id).then(result => {
  70. for (const [key, value] of Object.entries(Enums.VoteType)) {
  71. expect(result[`${key.toLowerCase()}Votes`]).to.be.bignumber.equal(
  72. Object.values(this.settings.voters).filter(({ support }) => support === value).reduce(
  73. (acc, { weight }) => acc.add(new BN(weight)),
  74. new BN('0'),
  75. ),
  76. );
  77. }
  78. });
  79. const startBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay);
  80. const endBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay).add(votingPeriod);
  81. expect(await this.mock.proposalSnapshot(this.id)).to.be.bignumber.equal(startBlock);
  82. expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(endBlock);
  83. expectEvent(
  84. this.receipts.propose,
  85. 'ProposalCreated',
  86. {
  87. proposalId: this.id,
  88. proposer,
  89. targets: this.settings.proposal[0],
  90. // values: this.settings.proposal[1].map(value => new BN(value)),
  91. signatures: this.settings.proposal[2].map(() => ''),
  92. calldatas: this.settings.proposal[2],
  93. startBlock,
  94. endBlock,
  95. description: this.settings.proposal[3],
  96. },
  97. );
  98. this.receipts.castVote.filter(Boolean).forEach(vote => {
  99. const { voter } = vote.logs.find(Boolean).args;
  100. expectEvent(
  101. vote,
  102. 'VoteCast',
  103. this.settings.voters.find(({ address }) => address === voter),
  104. );
  105. expectEvent.notEmitted(
  106. vote,
  107. 'ProposalExtended',
  108. );
  109. });
  110. expectEvent(
  111. this.receipts.execute,
  112. 'ProposalExecuted',
  113. { proposalId: this.id },
  114. );
  115. await expectEvent.inTransaction(
  116. this.receipts.execute.transactionHash,
  117. this.receiver,
  118. 'MockFunctionCalled',
  119. );
  120. });
  121. runGovernorWorkflow();
  122. });
  123. describe('Delay is extended to prevent last minute take-over', function () {
  124. beforeEach(async function () {
  125. this.settings = {
  126. proposal: [
  127. [ this.receiver.address ],
  128. [ 0 ],
  129. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  130. '<proposal description>',
  131. ],
  132. proposer,
  133. tokenHolder: owner,
  134. voters: [
  135. { voter: voter1, weight: web3.utils.toWei('0.2'), support: Enums.VoteType.Against },
  136. { voter: voter2, weight: web3.utils.toWei('1.0') }, // do not actually vote, only getting tokens
  137. { voter: voter3, weight: web3.utils.toWei('0.9') }, // do not actually vote, only getting tokens
  138. ],
  139. steps: {
  140. wait: { enable: false },
  141. execute: { enable: false },
  142. },
  143. };
  144. });
  145. afterEach(async function () {
  146. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  147. const startBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay);
  148. const endBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay).add(votingPeriod);
  149. expect(await this.mock.proposalSnapshot(this.id)).to.be.bignumber.equal(startBlock);
  150. expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(endBlock);
  151. // wait until the vote is almost over
  152. await time.advanceBlockTo(endBlock.subn(1));
  153. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  154. // try to overtake the vote at the last minute
  155. const tx = await this.mock.castVote(this.id, Enums.VoteType.For, { from: voter2 });
  156. // vote duration is extended
  157. const extendedBlock = new BN(tx.receipt.blockNumber).add(lateQuorumVoteExtension);
  158. expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(extendedBlock);
  159. expectEvent(
  160. tx,
  161. 'ProposalExtended',
  162. { proposalId: this.id, extendedDeadline: extendedBlock },
  163. );
  164. // vote is still active after expected end
  165. await time.advanceBlockTo(endBlock.addn(1));
  166. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
  167. // Still possible to vote
  168. await this.mock.castVote(this.id, Enums.VoteType.Against, { from: voter3 });
  169. // proposal fails
  170. await time.advanceBlockTo(extendedBlock.addn(1));
  171. expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Defeated);
  172. });
  173. runGovernorWorkflow();
  174. });
  175. describe('setLateQuorumVoteExtension', function () {
  176. beforeEach(async function () {
  177. this.newVoteExtension = new BN(0); // disable voting delay extension
  178. });
  179. it('protected', async function () {
  180. await expectRevert(
  181. this.mock.setLateQuorumVoteExtension(this.newVoteExtension),
  182. 'Governor: onlyGovernance',
  183. );
  184. });
  185. describe('using workflow', function () {
  186. beforeEach(async function () {
  187. this.settings = {
  188. proposal: [
  189. [ this.mock.address ],
  190. [ web3.utils.toWei('0') ],
  191. [ this.mock.contract.methods.setLateQuorumVoteExtension(this.newVoteExtension).encodeABI() ],
  192. '<proposal description>',
  193. ],
  194. proposer,
  195. tokenHolder: owner,
  196. voters: [
  197. { voter: voter1, weight: web3.utils.toWei('1.0'), support: Enums.VoteType.For },
  198. ],
  199. };
  200. });
  201. afterEach(async function () {
  202. expectEvent(
  203. this.receipts.propose,
  204. 'ProposalCreated',
  205. { proposalId: this.id },
  206. );
  207. expectEvent(
  208. this.receipts.execute,
  209. 'ProposalExecuted',
  210. { proposalId: this.id },
  211. );
  212. expectEvent(
  213. this.receipts.execute,
  214. 'LateQuorumVoteExtensionSet',
  215. { oldVoteExtension: lateQuorumVoteExtension, newVoteExtension: this.newVoteExtension },
  216. );
  217. expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal(this.newVoteExtension);
  218. });
  219. runGovernorWorkflow();
  220. });
  221. });
  222. });