Refactor governor testing (#3194)
* starting a governor test refactor * improve governor tests * refactor compatibility tests using the governor helper * improve governor helper * improve governor helper * refactor governor tests * refactor testing * fix testing (still TODO) * fix tests * fix tests * fix spelling * use different instances of GovernorHelper * add vote with params support * coverage * simplify ERC165 helper * remove unused proposal argument * refactor setProposal * lint * refactor setProposal return values * add a data default value * improve proposal reconstruction and storage in helper * proposal object refactoring * lint Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
@ -1,9 +1,7 @@
|
||||
const { BN, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
|
||||
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const Enums = require('../../helpers/enums');
|
||||
|
||||
const {
|
||||
runGovernorWorkflow,
|
||||
} = require('../GovernorWorkflow.behavior');
|
||||
const { GovernorHelper } = require('../../helpers/governance');
|
||||
|
||||
const Token = artifacts.require('ERC20VotesCompMock');
|
||||
const Governor = artifacts.require('GovernorPreventLateQuorumMock');
|
||||
@ -21,6 +19,7 @@ contract('GovernorPreventLateQuorum', function (accounts) {
|
||||
const votingPeriod = new BN(16);
|
||||
const lateQuorumVoteExtension = new BN(8);
|
||||
const quorum = web3.utils.toWei('1');
|
||||
const value = web3.utils.toWei('1');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.owner = owner;
|
||||
@ -34,11 +33,25 @@ contract('GovernorPreventLateQuorum', function (accounts) {
|
||||
lateQuorumVoteExtension,
|
||||
);
|
||||
this.receiver = await CallReceiver.new();
|
||||
|
||||
this.helper = new GovernorHelper(this.mock);
|
||||
|
||||
await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
|
||||
|
||||
await this.token.mint(owner, tokenSupply);
|
||||
await this.token.delegate(voter1, { from: voter1 });
|
||||
await this.token.delegate(voter2, { from: voter2 });
|
||||
await this.token.delegate(voter3, { from: voter3 });
|
||||
await this.token.delegate(voter4, { from: voter4 });
|
||||
await this.helper.delegate({ token: this.token, to: voter1, value: web3.utils.toWei('10') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter2, value: web3.utils.toWei('7') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter3, value: web3.utils.toWei('5') }, { from: owner });
|
||||
await this.helper.delegate({ token: this.token, to: voter4, value: web3.utils.toWei('2') }, { from: owner });
|
||||
|
||||
// default proposal
|
||||
this.proposal = this.helper.setProposal([
|
||||
{
|
||||
target: this.receiver.address,
|
||||
value,
|
||||
data: this.receiver.contract.methods.mockFunction().encodeABI(),
|
||||
},
|
||||
], '<proposal description>');
|
||||
});
|
||||
|
||||
it('deployment check', async function () {
|
||||
@ -50,198 +63,115 @@ contract('GovernorPreventLateQuorum', function (accounts) {
|
||||
expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal(lateQuorumVoteExtension);
|
||||
});
|
||||
|
||||
describe('nominal is unaffected', function () {
|
||||
beforeEach(async function () {
|
||||
this.settings = {
|
||||
proposal: [
|
||||
[ this.receiver.address ],
|
||||
[ 0 ],
|
||||
[ this.receiver.contract.methods.mockFunction().encodeABI() ],
|
||||
'<proposal description>',
|
||||
],
|
||||
it('nominal workflow unaffected', async function () {
|
||||
const txPropose = await this.helper.propose({ from: proposer });
|
||||
await this.helper.waitForSnapshot();
|
||||
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
|
||||
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
|
||||
await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 });
|
||||
await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 });
|
||||
await this.helper.waitForDeadline();
|
||||
await this.helper.execute();
|
||||
|
||||
expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter3)).to.be.equal(true);
|
||||
expect(await this.mock.hasVoted(this.proposal.id, voter4)).to.be.equal(true);
|
||||
|
||||
await this.mock.proposalVotes(this.proposal.id).then(results => {
|
||||
expect(results.forVotes).to.be.bignumber.equal(web3.utils.toWei('17'));
|
||||
expect(results.againstVotes).to.be.bignumber.equal(web3.utils.toWei('5'));
|
||||
expect(results.abstainVotes).to.be.bignumber.equal(web3.utils.toWei('2'));
|
||||
});
|
||||
|
||||
const startBlock = new BN(txPropose.receipt.blockNumber).add(votingDelay);
|
||||
const endBlock = new BN(txPropose.receipt.blockNumber).add(votingDelay).add(votingPeriod);
|
||||
expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(startBlock);
|
||||
expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(endBlock);
|
||||
|
||||
expectEvent(
|
||||
txPropose,
|
||||
'ProposalCreated',
|
||||
{
|
||||
proposalId: this.proposal.id,
|
||||
proposer,
|
||||
tokenHolder: owner,
|
||||
voters: [
|
||||
{ voter: voter1, weight: web3.utils.toWei('1'), support: Enums.VoteType.For, reason: 'This is nice' },
|
||||
{ voter: voter2, weight: web3.utils.toWei('7'), support: Enums.VoteType.For },
|
||||
{ voter: voter3, weight: web3.utils.toWei('5'), support: Enums.VoteType.Against },
|
||||
{ voter: voter4, weight: web3.utils.toWei('2'), support: Enums.VoteType.Abstain },
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
expect(await this.mock.hasVoted(this.id, owner)).to.be.equal(false);
|
||||
expect(await this.mock.hasVoted(this.id, voter1)).to.be.equal(true);
|
||||
expect(await this.mock.hasVoted(this.id, voter2)).to.be.equal(true);
|
||||
|
||||
await this.mock.proposalVotes(this.id).then(result => {
|
||||
for (const [key, value] of Object.entries(Enums.VoteType)) {
|
||||
expect(result[`${key.toLowerCase()}Votes`]).to.be.bignumber.equal(
|
||||
Object.values(this.settings.voters).filter(({ support }) => support === value).reduce(
|
||||
(acc, { weight }) => acc.add(new BN(weight)),
|
||||
new BN('0'),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const startBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay);
|
||||
const endBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay).add(votingPeriod);
|
||||
expect(await this.mock.proposalSnapshot(this.id)).to.be.bignumber.equal(startBlock);
|
||||
expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(endBlock);
|
||||
|
||||
expectEvent(
|
||||
this.receipts.propose,
|
||||
'ProposalCreated',
|
||||
{
|
||||
proposalId: this.id,
|
||||
proposer,
|
||||
targets: this.settings.proposal[0],
|
||||
// values: this.settings.proposal[1].map(value => new BN(value)),
|
||||
signatures: this.settings.proposal[2].map(() => ''),
|
||||
calldatas: this.settings.proposal[2],
|
||||
startBlock,
|
||||
endBlock,
|
||||
description: this.settings.proposal[3],
|
||||
},
|
||||
);
|
||||
|
||||
this.receipts.castVote.filter(Boolean).forEach(vote => {
|
||||
const { voter } = vote.logs.find(Boolean).args;
|
||||
expectEvent(
|
||||
vote,
|
||||
'VoteCast',
|
||||
this.settings.voters.find(({ address }) => address === voter),
|
||||
);
|
||||
expectEvent.notEmitted(
|
||||
vote,
|
||||
'ProposalExtended',
|
||||
);
|
||||
});
|
||||
expectEvent(
|
||||
this.receipts.execute,
|
||||
'ProposalExecuted',
|
||||
{ proposalId: this.id },
|
||||
);
|
||||
await expectEvent.inTransaction(
|
||||
this.receipts.execute.transactionHash,
|
||||
this.receiver,
|
||||
'MockFunctionCalled',
|
||||
);
|
||||
});
|
||||
runGovernorWorkflow();
|
||||
targets: this.proposal.targets,
|
||||
// values: this.proposal.values.map(value => new BN(value)),
|
||||
signatures: this.proposal.signatures,
|
||||
calldatas: this.proposal.data,
|
||||
startBlock,
|
||||
endBlock,
|
||||
description: this.proposal.description,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('Delay is extended to prevent last minute take-over', function () {
|
||||
beforeEach(async function () {
|
||||
this.settings = {
|
||||
proposal: [
|
||||
[ this.receiver.address ],
|
||||
[ 0 ],
|
||||
[ this.receiver.contract.methods.mockFunction().encodeABI() ],
|
||||
'<proposal description>',
|
||||
],
|
||||
proposer,
|
||||
tokenHolder: owner,
|
||||
voters: [
|
||||
{ voter: voter1, weight: web3.utils.toWei('0.2'), support: Enums.VoteType.Against },
|
||||
{ voter: voter2, weight: web3.utils.toWei('1.0') }, // do not actually vote, only getting tokens
|
||||
{ voter: voter3, weight: web3.utils.toWei('0.9') }, // do not actually vote, only getting tokens
|
||||
],
|
||||
steps: {
|
||||
wait: { enable: false },
|
||||
execute: { enable: false },
|
||||
},
|
||||
};
|
||||
});
|
||||
it('Delay is extended to prevent last minute take-over', async function () {
|
||||
const txPropose = await this.helper.propose({ from: proposer });
|
||||
|
||||
afterEach(async function () {
|
||||
expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
|
||||
// compute original schedule
|
||||
const startBlock = new BN(txPropose.receipt.blockNumber).add(votingDelay);
|
||||
const endBlock = new BN(txPropose.receipt.blockNumber).add(votingDelay).add(votingPeriod);
|
||||
expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(startBlock);
|
||||
expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(endBlock);
|
||||
|
||||
const startBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay);
|
||||
const endBlock = new BN(this.receipts.propose.blockNumber).add(votingDelay).add(votingPeriod);
|
||||
expect(await this.mock.proposalSnapshot(this.id)).to.be.bignumber.equal(startBlock);
|
||||
expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(endBlock);
|
||||
// wait for the last minute to vote
|
||||
await this.helper.waitForDeadline(-1);
|
||||
const txVote = await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 });
|
||||
|
||||
// wait until the vote is almost over
|
||||
await time.advanceBlockTo(endBlock.subn(1));
|
||||
expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
|
||||
// cannot execute yet
|
||||
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
|
||||
|
||||
// try to overtake the vote at the last minute
|
||||
const tx = await this.mock.castVote(this.id, Enums.VoteType.For, { from: voter2 });
|
||||
// compute new extended schedule
|
||||
const extendedDeadline = new BN(txVote.receipt.blockNumber).add(lateQuorumVoteExtension);
|
||||
expect(await this.mock.proposalSnapshot(this.proposal.id)).to.be.bignumber.equal(startBlock);
|
||||
expect(await this.mock.proposalDeadline(this.proposal.id)).to.be.bignumber.equal(extendedDeadline);
|
||||
|
||||
// vote duration is extended
|
||||
const extendedBlock = new BN(tx.receipt.blockNumber).add(lateQuorumVoteExtension);
|
||||
expect(await this.mock.proposalDeadline(this.id)).to.be.bignumber.equal(extendedBlock);
|
||||
// still possible to vote
|
||||
await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter1 });
|
||||
|
||||
expectEvent(
|
||||
tx,
|
||||
'ProposalExtended',
|
||||
{ proposalId: this.id, extendedDeadline: extendedBlock },
|
||||
);
|
||||
await this.helper.waitForDeadline();
|
||||
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
|
||||
await this.helper.waitForDeadline(+1);
|
||||
expect(await this.mock.state(this.proposal.id)).to.be.bignumber.equal(Enums.ProposalState.Defeated);
|
||||
|
||||
// vote is still active after expected end
|
||||
await time.advanceBlockTo(endBlock.addn(1));
|
||||
expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Active);
|
||||
|
||||
// Still possible to vote
|
||||
await this.mock.castVote(this.id, Enums.VoteType.Against, { from: voter3 });
|
||||
|
||||
// proposal fails
|
||||
await time.advanceBlockTo(extendedBlock.addn(1));
|
||||
expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Defeated);
|
||||
});
|
||||
runGovernorWorkflow();
|
||||
// check extension event
|
||||
expectEvent(
|
||||
txVote,
|
||||
'ProposalExtended',
|
||||
{ proposalId: this.proposal.id, extendedDeadline },
|
||||
);
|
||||
});
|
||||
|
||||
describe('setLateQuorumVoteExtension', function () {
|
||||
beforeEach(async function () {
|
||||
this.newVoteExtension = new BN(0); // disable voting delay extension
|
||||
});
|
||||
|
||||
it('protected', async function () {
|
||||
describe('onlyGovernance updates', function () {
|
||||
it('setLateQuorumVoteExtension is protected', async function () {
|
||||
await expectRevert(
|
||||
this.mock.setLateQuorumVoteExtension(this.newVoteExtension),
|
||||
this.mock.setLateQuorumVoteExtension(0),
|
||||
'Governor: onlyGovernance',
|
||||
);
|
||||
});
|
||||
|
||||
describe('using workflow', function () {
|
||||
beforeEach(async function () {
|
||||
this.settings = {
|
||||
proposal: [
|
||||
[ this.mock.address ],
|
||||
[ web3.utils.toWei('0') ],
|
||||
[ this.mock.contract.methods.setLateQuorumVoteExtension(this.newVoteExtension).encodeABI() ],
|
||||
'<proposal description>',
|
||||
],
|
||||
proposer,
|
||||
tokenHolder: owner,
|
||||
voters: [
|
||||
{ voter: voter1, weight: web3.utils.toWei('1.0'), support: Enums.VoteType.For },
|
||||
],
|
||||
};
|
||||
});
|
||||
afterEach(async function () {
|
||||
expectEvent(
|
||||
this.receipts.propose,
|
||||
'ProposalCreated',
|
||||
{ proposalId: this.id },
|
||||
);
|
||||
expectEvent(
|
||||
this.receipts.execute,
|
||||
'ProposalExecuted',
|
||||
{ proposalId: this.id },
|
||||
);
|
||||
expectEvent(
|
||||
this.receipts.execute,
|
||||
'LateQuorumVoteExtensionSet',
|
||||
{ oldVoteExtension: lateQuorumVoteExtension, newVoteExtension: this.newVoteExtension },
|
||||
);
|
||||
expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal(this.newVoteExtension);
|
||||
});
|
||||
runGovernorWorkflow();
|
||||
it('can setLateQuorumVoteExtension through governance', async function () {
|
||||
this.helper.setProposal([
|
||||
{
|
||||
target: this.mock.address,
|
||||
data: this.mock.contract.methods.setLateQuorumVoteExtension('0').encodeABI(),
|
||||
},
|
||||
], '<proposal description>');
|
||||
|
||||
await this.helper.propose();
|
||||
await this.helper.waitForSnapshot();
|
||||
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
|
||||
await this.helper.waitForDeadline();
|
||||
|
||||
expectEvent(
|
||||
await this.helper.execute(),
|
||||
'LateQuorumVoteExtensionSet',
|
||||
{ oldVoteExtension: lateQuorumVoteExtension, newVoteExtension: '0' },
|
||||
);
|
||||
|
||||
expect(await this.mock.lateQuorumVoteExtension()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user