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:
Hadrien Croubois
2022-03-11 09:30:30 +01:00
committed by GitHub
parent 8372b4f923
commit 6a5bbfc4cb
12 changed files with 1870 additions and 2816 deletions

View File

@ -1,10 +1,8 @@
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const Enums = require('../../helpers/enums');
const { expect } = require('chai');
const RLP = require('rlp');
const {
runGovernorWorkflow,
} = require('../GovernorWorkflow.behavior');
const Enums = require('../../helpers/enums');
const { GovernorHelper } = require('../../helpers/governance');
const Token = artifacts.require('ERC20VotesCompMock');
const Timelock = artifacts.require('CompTimelock');
@ -23,7 +21,10 @@ contract('GovernorCompatibilityBravo', function (accounts) {
const tokenName = 'MockToken';
const tokenSymbol = 'MTKN';
const tokenSupply = web3.utils.toWei('100');
const votingDelay = new BN(4);
const votingPeriod = new BN(16);
const proposalThreshold = web3.utils.toWei('10');
const value = web3.utils.toWei('1');
beforeEach(async function () {
const [ deployer ] = await web3.eth.getAccounts();
@ -35,392 +36,220 @@ contract('GovernorCompatibilityBravo', function (accounts) {
const predictGovernor = makeContractAddress(deployer, nonce + 1);
this.timelock = await Timelock.new(predictGovernor, 2 * 86400);
this.mock = await Governor.new(name, this.token.address, 4, 16, proposalThreshold, this.timelock.address);
this.mock = await Governor.new(
name,
this.token.address,
votingDelay,
votingPeriod,
proposalThreshold,
this.timelock.address,
);
this.receiver = await CallReceiver.new();
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.token.transfer(proposer, proposalThreshold, { from: owner });
await this.token.delegate(proposer, { from: proposer });
this.helper = new GovernorHelper(this.mock);
await web3.eth.sendTransaction({ from: owner, to: this.timelock.address, value });
await this.token.mint(owner, tokenSupply);
await this.helper.delegate({ token: this.token, to: proposer, value: proposalThreshold }, { from: owner });
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,
signature: 'mockFunction()',
},
], '<proposal description>');
});
it('deployment check', async function () {
expect(await this.mock.name()).to.be.equal(name);
expect(await this.mock.token()).to.be.equal(this.token.address);
expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
expect(await this.mock.quorumVotes()).to.be.bignumber.equal('0');
expect(await this.mock.COUNTING_MODE()).to.be.equal('support=bravo&quorum=bravo');
});
describe('nominal', function () {
beforeEach(async function () {
this.settings = {
proposal: [
[ this.receiver.address ], // targets
[ web3.utils.toWei('0') ], // values
[ this.receiver.contract.methods.mockFunction().encodeABI() ], // calldatas
'<proposal description>', // description
],
it('nominal workflow', async function () {
// Before
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(false);
expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(false);
expect(await web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(value);
expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal('0');
// Run proposal
const txPropose = await this.helper.propose({ from: proposer });
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { 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.queue();
await this.helper.waitForEta();
const txExecute = await this.helper.execute();
// After
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 web3.eth.getBalance(this.mock.address)).to.be.bignumber.equal('0');
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal('0');
expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
const proposal = await this.mock.proposals(this.proposal.id);
expect(proposal.id).to.be.bignumber.equal(this.proposal.id);
expect(proposal.proposer).to.be.equal(proposer);
expect(proposal.eta).to.be.bignumber.equal(await this.mock.proposalEta(this.proposal.id));
expect(proposal.startBlock).to.be.bignumber.equal(await this.mock.proposalSnapshot(this.proposal.id));
expect(proposal.endBlock).to.be.bignumber.equal(await this.mock.proposalDeadline(this.proposal.id));
expect(proposal.canceled).to.be.equal(false);
expect(proposal.executed).to.be.equal(true);
const action = await this.mock.getActions(this.proposal.id);
expect(action.targets).to.be.deep.equal(this.proposal.targets);
// expect(action.values).to.be.deep.equal(this.proposal.values);
expect(action.signatures).to.be.deep.equal(this.proposal.signatures);
expect(action.calldatas).to.be.deep.equal(this.proposal.data);
const voteReceipt1 = await this.mock.getReceipt(this.proposal.id, voter1);
expect(voteReceipt1.hasVoted).to.be.equal(true);
expect(voteReceipt1.support).to.be.bignumber.equal(Enums.VoteType.For);
expect(voteReceipt1.votes).to.be.bignumber.equal(web3.utils.toWei('10'));
const voteReceipt2 = await this.mock.getReceipt(this.proposal.id, voter2);
expect(voteReceipt2.hasVoted).to.be.equal(true);
expect(voteReceipt2.support).to.be.bignumber.equal(Enums.VoteType.For);
expect(voteReceipt2.votes).to.be.bignumber.equal(web3.utils.toWei('7'));
const voteReceipt3 = await this.mock.getReceipt(this.proposal.id, voter3);
expect(voteReceipt3.hasVoted).to.be.equal(true);
expect(voteReceipt3.support).to.be.bignumber.equal(Enums.VoteType.Against);
expect(voteReceipt3.votes).to.be.bignumber.equal(web3.utils.toWei('5'));
const voteReceipt4 = await this.mock.getReceipt(this.proposal.id, voter4);
expect(voteReceipt4.hasVoted).to.be.equal(true);
expect(voteReceipt4.support).to.be.bignumber.equal(Enums.VoteType.Abstain);
expect(voteReceipt4.votes).to.be.bignumber.equal(web3.utils.toWei('2'));
expectEvent(
txPropose,
'ProposalCreated',
{
proposalId: this.proposal.id,
proposer,
tokenHolder: owner,
voters: [
{
voter: voter1,
weight: web3.utils.toWei('1'),
support: Enums.VoteType.Abstain,
},
{
voter: voter2,
weight: web3.utils.toWei('10'),
support: Enums.VoteType.For,
},
{
voter: voter3,
weight: web3.utils.toWei('5'),
support: Enums.VoteType.Against,
},
{
voter: voter4,
support: '100',
error: 'GovernorCompatibilityBravo: invalid vote type',
},
{
voter: voter1,
support: Enums.VoteType.For,
error: 'GovernorCompatibilityBravo: vote already cast',
skip: true,
},
],
steps: {
queue: { delay: 7 * 86400 },
},
};
this.votingDelay = await this.mock.votingDelay();
this.votingPeriod = await this.mock.votingPeriod();
this.receipts = {};
});
afterEach(async function () {
const proposal = await this.mock.proposals(this.id);
expect(proposal.id).to.be.bignumber.equal(this.id);
expect(proposal.proposer).to.be.equal(proposer);
expect(proposal.eta).to.be.bignumber.equal(this.eta);
expect(proposal.startBlock).to.be.bignumber.equal(this.snapshot);
expect(proposal.endBlock).to.be.bignumber.equal(this.deadline);
expect(proposal.canceled).to.be.equal(false);
expect(proposal.executed).to.be.equal(true);
targets: this.proposal.targets,
// values: this.proposal.values,
signatures: this.proposal.signatures.map(() => ''), // this event doesn't contain the proposal detail
calldatas: this.proposal.fulldata,
startBlock: new BN(txPropose.receipt.blockNumber).add(votingDelay),
endBlock: new BN(txPropose.receipt.blockNumber).add(votingDelay).add(votingPeriod),
description: this.proposal.description,
},
);
expectEvent(
txExecute,
'ProposalExecuted',
{ proposalId: this.proposal.id },
);
await expectEvent.inTransaction(
txExecute.tx,
this.receiver,
'MockFunctionCalled',
);
});
for (const [key, value] of Object.entries(Enums.VoteType)) {
expect(proposal[`${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'),
),
);
}
it('with function selector and arguments', async function () {
const target = this.receiver.address;
this.helper.setProposal([
{ target, data: this.receiver.contract.methods.mockFunction().encodeABI() },
{ target, data: this.receiver.contract.methods.mockFunctionWithArgs(17, 42).encodeABI() },
{ target, signature: 'mockFunctionNonPayable()' },
{
target,
signature: 'mockFunctionWithArgs(uint256,uint256)',
data: web3.eth.abi.encodeParameters(['uint256', 'uint256'], [18, 43]),
},
], '<proposal description>');
const action = await this.mock.getActions(this.id);
expect(action.targets).to.be.deep.equal(this.settings.proposal[0]);
// expect(action.values).to.be.deep.equal(this.settings.proposal[1]);
expect(action.signatures).to.be.deep.equal(Array(this.settings.proposal[2].length).fill(''));
expect(action.calldatas).to.be.deep.equal(this.settings.proposal[2]);
await this.helper.propose({ from: proposer });
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For, reason: 'This is nice' }, { from: voter1 });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
const txExecute = await this.helper.execute();
for (const voter of this.settings.voters.filter(({ skip }) => !skip)) {
expect(await this.mock.hasVoted(this.id, voter.voter)).to.be.equal(voter.error === undefined);
await expectEvent.inTransaction(
txExecute.tx,
this.receiver,
'MockFunctionCalled',
);
await expectEvent.inTransaction(
txExecute.tx,
this.receiver,
'MockFunctionCalled',
);
await expectEvent.inTransaction(
txExecute.tx,
this.receiver,
'MockFunctionCalledWithArgs',
{ a: '17', b: '42' },
);
await expectEvent.inTransaction(
txExecute.tx,
this.receiver,
'MockFunctionCalledWithArgs',
{ a: '18', b: '43' },
);
});
const receipt = await this.mock.getReceipt(this.id, voter.voter);
expect(receipt.hasVoted).to.be.equal(voter.error === undefined);
expect(receipt.support).to.be.bignumber.equal(voter.error === undefined ? voter.support : '0');
expect(receipt.votes).to.be.bignumber.equal(voter.error === undefined ? voter.weight : '0');
}
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: new BN(this.receipts.propose.blockNumber).add(this.votingDelay),
endBlock: new BN(this.receipts.propose.blockNumber).add(this.votingDelay).add(this.votingPeriod),
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),
describe('should revert', function () {
describe('on propose', function () {
it('if proposal doesnt meet proposalThreshold', async function () {
await expectRevert(
this.helper.propose({ from: other }),
'GovernorCompatibilityBravo: proposer votes below proposal threshold',
);
});
expectEvent(
this.receipts.execute,
'ProposalExecuted',
{ proposalId: this.id },
);
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalled',
);
});
runGovernorWorkflow();
});
describe('with function selector and arguments', function () {
beforeEach(async function () {
this.settings = {
proposal: [
Array(4).fill(this.receiver.address),
Array(4).fill(web3.utils.toWei('0')),
[
'',
'',
'mockFunctionNonPayable()',
'mockFunctionWithArgs(uint256,uint256)',
],
[
this.receiver.contract.methods.mockFunction().encodeABI(),
this.receiver.contract.methods.mockFunctionWithArgs(17, 42).encodeABI(),
'0x',
web3.eth.abi.encodeParameters(['uint256', 'uint256'], [18, 43]),
],
'<proposal description>', // description
],
proposer,
tokenHolder: owner,
voters: [
{
voter: voter1,
weight: web3.utils.toWei('10'),
support: Enums.VoteType.For,
},
],
steps: {
queue: { delay: 7 * 86400 },
},
};
describe('on vote', function () {
it('if vote type is invalide', async function () {
await this.helper.propose({ from: proposer });
await this.helper.waitForSnapshot();
await expectRevert(
this.helper.vote({ support: 5 }, { from: voter1 }),
'GovernorCompatibilityBravo: invalid vote type',
);
});
});
runGovernorWorkflow();
afterEach(async function () {
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalled',
);
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalled',
);
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalledWithArgs',
{ a: '17', b: '42' },
);
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalledWithArgs',
{ a: '18', b: '43' },
);
});
});
describe('proposalThreshold not reached', function () {
beforeEach(async function () {
this.settings = {
proposal: [
[ this.receiver.address ], // targets
[ web3.utils.toWei('0') ], // values
[ this.receiver.contract.methods.mockFunction().encodeABI() ], // calldatas
'<proposal description>', // description
],
proposer: other,
steps: {
propose: { error: 'GovernorCompatibilityBravo: proposer votes below proposal threshold' },
wait: { enable: false },
queue: { enable: false },
execute: { enable: false },
},
};
});
runGovernorWorkflow();
});
describe('cancel', function () {
beforeEach(async function () {
this.settings = {
proposal: [
[ this.receiver.address ], // targets
[ web3.utils.toWei('0') ], // values
[ this.receiver.contract.methods.mockFunction().encodeABI() ], // calldatas
'<proposal description>', // description
],
proposer,
tokenHolder: owner,
steps: {
wait: { enable: false },
queue: { enable: false },
execute: { enable: false },
},
};
it('proposer can cancel', async function () {
await this.helper.propose({ from: proposer });
await this.helper.cancel({ from: proposer });
});
describe('by proposer', function () {
afterEach(async function () {
await this.mock.cancel(this.id, { from: proposer });
});
runGovernorWorkflow();
it('anyone can cancel if proposer drop below threshold', async function () {
await this.helper.propose({ from: proposer });
await this.token.transfer(voter1, web3.utils.toWei('1'), { from: proposer });
await this.helper.cancel();
});
describe('if proposer below threshold', function () {
afterEach(async function () {
await this.token.transfer(voter1, web3.utils.toWei('1'), { from: proposer });
await this.mock.cancel(this.id);
});
runGovernorWorkflow();
it('cannot cancel is proposer is still above threshold', async function () {
await this.helper.propose({ from: proposer });
await expectRevert(this.helper.cancel(), 'GovernorBravo: proposer above threshold');
});
describe('not if proposer above threshold', function () {
afterEach(async function () {
await expectRevert(
this.mock.cancel(this.id),
'GovernorBravo: proposer above threshold',
);
});
runGovernorWorkflow();
});
});
describe('with compatibility interface', function () {
beforeEach(async function () {
this.settings = {
proposal: [
[ this.receiver.address ], // targets
[ web3.utils.toWei('0') ], // values
[ 'mockFunction()' ], // signatures
[ '0x' ], // calldatas
'<proposal description>', // description
],
proposer,
tokenHolder: owner,
voters: [
{
voter: voter1,
weight: web3.utils.toWei('1'),
support: Enums.VoteType.Abstain,
},
{
voter: voter2,
weight: web3.utils.toWei('10'),
support: Enums.VoteType.For,
},
{
voter: voter3,
weight: web3.utils.toWei('5'),
support: Enums.VoteType.Against,
},
{
voter: voter4,
support: '100',
error: 'GovernorCompatibilityBravo: invalid vote type',
},
{
voter: voter1,
support: Enums.VoteType.For,
error: 'GovernorCompatibilityBravo: vote already cast',
skip: true,
},
],
steps: {
queue: { delay: 7 * 86400 },
},
};
this.votingDelay = await this.mock.votingDelay();
this.votingPeriod = await this.mock.votingPeriod();
this.receipts = {};
});
afterEach(async function () {
const proposal = await this.mock.proposals(this.id);
expect(proposal.id).to.be.bignumber.equal(this.id);
expect(proposal.proposer).to.be.equal(proposer);
expect(proposal.eta).to.be.bignumber.equal(this.eta);
expect(proposal.startBlock).to.be.bignumber.equal(this.snapshot);
expect(proposal.endBlock).to.be.bignumber.equal(this.deadline);
expect(proposal.canceled).to.be.equal(false);
expect(proposal.executed).to.be.equal(true);
for (const [key, value] of Object.entries(Enums.VoteType)) {
expect(proposal[`${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 action = await this.mock.getActions(this.id);
expect(action.targets).to.be.deep.equal(this.settings.proposal[0]);
// expect(action.values).to.be.deep.equal(this.settings.proposal[1]);
expect(action.signatures).to.be.deep.equal(this.settings.proposal[2]);
expect(action.calldatas).to.be.deep.equal(this.settings.proposal[3]);
for (const voter of this.settings.voters.filter(({ skip }) => !skip)) {
expect(await this.mock.hasVoted(this.id, voter.voter)).to.be.equal(voter.error === undefined);
const receipt = await this.mock.getReceipt(this.id, voter.voter);
expect(receipt.hasVoted).to.be.equal(voter.error === undefined);
expect(receipt.support).to.be.bignumber.equal(voter.error === undefined ? voter.support : '0');
expect(receipt.votes).to.be.bignumber.equal(voter.error === undefined ? voter.weight : '0');
}
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.shortProposal[2],
startBlock: new BN(this.receipts.propose.blockNumber).add(this.votingDelay),
endBlock: new BN(this.receipts.propose.blockNumber).add(this.votingDelay).add(this.votingPeriod),
description: this.settings.proposal[4],
},
);
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(
this.receipts.execute,
'ProposalExecuted',
{ proposalId: this.id },
);
await expectEvent.inTransaction(
this.receipts.execute.transactionHash,
this.receiver,
'MockFunctionCalled',
);
});
runGovernorWorkflow();
});
});