Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Governor signature nonces
  • Loading branch information
ernestognw committed Jun 22, 2023
commit 0619b5cb0c3ce4f3df282767af70fc22713b149f
37 changes: 29 additions & 8 deletions contracts/governance/Governor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "../utils/math/SafeCast.sol";
import "../utils/structs/DoubleEndedQueue.sol";
import "../utils/Address.sol";
import "../utils/Context.sol";
import "../utils/Nonces.sol";
import "./IGovernor.sol";

/**
Expand All @@ -25,12 +26,15 @@ import "./IGovernor.sol";
*
* _Available since v4.3._
*/
abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receiver, IERC1155Receiver {
abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC721Receiver, IERC1155Receiver {
using DoubleEndedQueue for DoubleEndedQueue.Bytes32Deque;

bytes32 public constant BALLOT_TYPEHASH = keccak256("Ballot(uint256 proposalId,uint8 support)");
bytes32 public constant BALLOT_TYPEHASH =
keccak256("Ballot(uint256 proposalId,uint8 support,address voter,uint256 nonce)");
bytes32 public constant EXTENDED_BALLOT_TYPEHASH =
keccak256("ExtendedBallot(uint256 proposalId,uint8 support,string reason,bytes params)");
keccak256(
"ExtendedBallot(uint256 proposalId,uint8 support,address voter,uint256 nonce,string reason,bytes params)"
);

// solhint-disable var-name-mixedcase
struct ProposalCore {
Expand Down Expand Up @@ -519,17 +523,25 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
function castVoteBySig(
uint256 proposalId,
uint8 support,
address voter,
Copy link
Contributor

@frangio frangio Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changing the ERC-165 ids, i.e. removing the previously supported interfaces (correctly) and adding new ones. However, we've discussed before that ERC-165 is not a good fit for this contract so we should really think whether we want to continue using it and particularly adding new interfaces.

No need to change anything on this PR, but it is an unsolved question we need to address.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is relevant to #4360

uint8 v,
bytes32 r,
bytes32 s
) public virtual override returns (uint256) {
address voter = ECDSA.recover(
_hashTypedDataV4(keccak256(abi.encode(BALLOT_TYPEHASH, proposalId, support))),
uint256 currentNonce = _useNonce(voter);

address signer = ECDSA.recover(
_hashTypedDataV4(keccak256(abi.encode(BALLOT_TYPEHASH, proposalId, support, voter, currentNonce))),
v,
r,
s
);
return _castVote(proposalId, voter, support, "");

if (voter != signer) {
revert GovernorInvalidSigner(signer, voter);
}

return _castVote(proposalId, signer, support, "");
}

/**
Expand All @@ -538,19 +550,24 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
function castVoteWithReasonAndParamsBySig(
uint256 proposalId,
uint8 support,
address voter,
string calldata reason,
bytes memory params,
uint8 v,
bytes32 r,
bytes32 s
Comment on lines 549 to 551
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we are changing the signature of the castVoteBySig functions, what do you think about changing these arguments to bytes signature and using the opportunity to add EIP-1271 compatibility?

I don't think we should do this here so it's not a blocker for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also aligns really well with the addition of the voter argument, which is needed for EIP-1271.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to what we discussed, I'll add the signature changes and 1271 compatibility in a follow up PR

) public virtual override returns (uint256) {
address voter = ECDSA.recover(
uint256 currentNonce = _useNonce(voter);

address signer = ECDSA.recover(
_hashTypedDataV4(
keccak256(
abi.encode(
EXTENDED_BALLOT_TYPEHASH,
proposalId,
support,
voter,
currentNonce,
keccak256(bytes(reason)),
keccak256(params)
)
Expand All @@ -561,7 +578,11 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receive
s
);

return _castVote(proposalId, voter, support, reason, params);
if (voter != signer) {
revert GovernorInvalidSigner(signer, voter);
}

return _castVote(proposalId, signer, support, reason, params);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions contracts/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ abstract contract IGovernor is IERC165, IERC6372 {
*/
error GovernorInvalidVoteType();

/**
* @dev The `voter` doesn't match with the recovered `signer`.
*/
error GovernorInvalidSigner(address signer, address voter);

/**
* @dev Emitted when a proposal is created.
*/
Expand Down Expand Up @@ -348,6 +353,7 @@ abstract contract IGovernor is IERC165, IERC6372 {
function castVoteBySig(
uint256 proposalId,
uint8 support,
address voter,
uint8 v,
bytes32 r,
bytes32 s
Expand All @@ -361,6 +367,7 @@ abstract contract IGovernor is IERC165, IERC6372 {
function castVoteWithReasonAndParamsBySig(
uint256 proposalId,
uint8 support,
address voter,
string calldata reason,
bytes memory params,
uint8 v,
Expand Down
86 changes: 81 additions & 5 deletions test/governance/Governor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ contract('Governor', function (accounts) {
expect(await web3.eth.getBalance(this.receiver.address)).to.be.bignumber.equal(value);
});

it('vote with signature', async function () {
it('votes with signature', async function () {
const voterBySig = Wallet.generate();
const voterBySigAddress = web3.utils.toChecksumAddress(voterBySig.getAddressString());

Expand All @@ -179,6 +179,8 @@ contract('Governor', function (accounts) {
Ballot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'voter', type: 'address' },
{ name: 'nonce', type: 'uint256' },
],
},
domain,
Expand All @@ -189,13 +191,19 @@ contract('Governor', function (accounts) {

await this.token.delegate(voterBySigAddress, { from: voter1 });

const nonce = await this.mock.nonces(voterBySigAddress);

// Run proposal
await this.helper.propose();
await this.helper.waitForSnapshot();
expectEvent(await this.helper.vote({ support: Enums.VoteType.For, signature }), 'VoteCast', {
voter: voterBySigAddress,
support: Enums.VoteType.For,
});
expectEvent(
await this.helper.vote({ support: Enums.VoteType.For, voter: voterBySigAddress, nonce: nonce, signature }),
'VoteCast',
{
voter: voterBySigAddress,
support: Enums.VoteType.For,
},
);
await this.helper.waitForDeadline();
await this.helper.execute();

Expand All @@ -204,6 +212,7 @@ contract('Governor', function (accounts) {
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 this.mock.hasVoted(this.proposal.id, voterBySigAddress)).to.be.equal(true);
expect(await this.mock.nonces(voterBySigAddress)).to.be.bignumber.equal(nonce.addn(1));
});

it('send ethers', async function () {
Expand Down Expand Up @@ -297,6 +306,73 @@ contract('Governor', function (accounts) {
});
});

describe('on vote by signature', function () {
beforeEach(async function () {
this.voterBySig = Wallet.generate();
this.voterBySig.address = web3.utils.toChecksumAddress(this.voterBySig.getAddressString());

this.signature = (contract, message) =>
getDomain(contract)
.then(domain => ({
primaryType: 'Ballot',
types: {
EIP712Domain: domainType(domain),
Ballot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'voter', type: 'address' },
{ name: 'nonce', type: 'uint256' },
],
},
domain,
message,
}))
.then(data => ethSigUtil.signTypedMessage(this.voterBySig.getPrivateKey(), { data }))
.then(fromRpcSig);

await this.token.delegate(this.voterBySig.address, { from: voter1 });

// Run proposal
await this.helper.propose();
await this.helper.waitForSnapshot();
});

it('if signature does not match signer', async function () {
const nonce = await this.mock.nonces(this.voterBySig.address);

expectRevertCustomError(
this.helper.vote({
support: Enums.VoteType.For,
voter: this.voterBySig.address,
nonce,
signature: (...params) => {
const sig = this.signature(...params);
sig[69] ^= 0xff;
return sig;
},
}),
'GovernorInvalidSigner',
[],
);
});

it('if vote nonce is incorrect', async function () {
const nonce = await this.mock.nonces(this.voterBySig.address);

expectRevertCustomError(
this.helper.vote({
support: Enums.VoteType.For,
voter: this.voterBySig.address,
nonce: nonce.addn(1),
signature: this.signature,
}),
// The signature check implies the nonce can't be tampered without changing the signer
'GovernorInvalidSigner',
[],
);
});
});

describe('on execute', function () {
it('if proposal does not exist', async function () {
await expectRevertCustomError(this.helper.execute(), 'GovernorNonexistentProposal', [this.proposal.id]);
Expand Down
131 changes: 89 additions & 42 deletions test/governance/extensions/GovernorWithParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { fromRpcSig } = require('ethereumjs-util');
const Enums = require('../../helpers/enums');
const { getDomain, domainType } = require('../../helpers/eip712');
const { GovernorHelper } = require('../../helpers/governance');
const { expectRevertCustomError } = require('../../helpers/customError');

const Governor = artifacts.require('$GovernorWithParamsMock');
const CallReceiver = artifacts.require('CallReceiverMock');
Expand Down Expand Up @@ -119,56 +120,102 @@ contract('GovernorWithParams', function (accounts) {
expect(votes.forVotes).to.be.bignumber.equal(weight);
});

it('Voting with params by signature is properly supported', async function () {
const voterBySig = Wallet.generate();
const voterBySigAddress = web3.utils.toChecksumAddress(voterBySig.getAddressString());

const signature = (contract, message) =>
getDomain(contract)
.then(domain => ({
primaryType: 'ExtendedBallot',
types: {
EIP712Domain: domainType(domain),
ExtendedBallot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'reason', type: 'string' },
{ name: 'params', type: 'bytes' },
],
},
domain,
message,
}))
.then(data => ethSigUtil.signTypedMessage(voterBySig.getPrivateKey(), { data }))
.then(fromRpcSig);
describe('voting by signature', function () {
beforeEach(async function () {
this.voterBySig = Wallet.generate();
this.voterBySig.address = web3.utils.toChecksumAddress(this.voterBySig.getAddressString());

this.signature = (contract, message) =>
getDomain(contract)
.then(domain => ({
primaryType: 'ExtendedBallot',
types: {
EIP712Domain: domainType(domain),
ExtendedBallot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' },
{ name: 'voter', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'reason', type: 'string' },
{ name: 'params', type: 'bytes' },
],
},
domain,
message,
}))
.then(data => ethSigUtil.signTypedMessage(this.voterBySig.getPrivateKey(), { data }))
.then(fromRpcSig);

await this.token.delegate(this.voterBySig.address, { from: voter2 });

// Run proposal
await this.helper.propose();
await this.helper.waitForSnapshot();
});

await this.token.delegate(voterBySigAddress, { from: voter2 });
it('is properly supported', async function () {
const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);

// Run proposal
await this.helper.propose();
await this.helper.waitForSnapshot();
const nonce = await this.mock.nonces(this.voterBySig.address);

const weight = web3.utils.toBN(web3.utils.toWei('7')).sub(rawParams.uintParam);
const tx = await this.helper.vote({
support: Enums.VoteType.For,
voter: this.voterBySig.address,
nonce: nonce,
reason: 'no particular reason',
params: encodedParams,
signature: this.signature,
});

const tx = await this.helper.vote({
support: Enums.VoteType.For,
reason: 'no particular reason',
params: encodedParams,
signature,
expectEvent(tx, 'CountParams', { ...rawParams });
expectEvent(tx, 'VoteCastWithParams', {
voter: this.voterBySig.address,
proposalId: this.proposal.id,
support: Enums.VoteType.For,
weight,
reason: 'no particular reason',
params: encodedParams,
});

const votes = await this.mock.proposalVotes(this.proposal.id);
expect(votes.forVotes).to.be.bignumber.equal(weight);
expect(await this.mock.nonces(this.voterBySig.address)).to.be.bignumber.equal(nonce.addn(1));
});

expectEvent(tx, 'CountParams', { ...rawParams });
expectEvent(tx, 'VoteCastWithParams', {
voter: voterBySigAddress,
proposalId: this.proposal.id,
support: Enums.VoteType.For,
weight,
reason: 'no particular reason',
params: encodedParams,
it('reverts if signature does not match signer', async function () {
const nonce = await this.mock.nonces(this.voterBySig.address);

expectRevertCustomError(
this.helper.vote({
support: Enums.VoteType.For,
voter: this.voterBySig.address,
nonce,
signature: (...params) => {
const sig = this.signature(...params);
sig[69] ^= 0xff;
return sig;
},
}),
'GovernorInvalidSigner',
[],
);
});

const votes = await this.mock.proposalVotes(this.proposal.id);
expect(votes.forVotes).to.be.bignumber.equal(weight);
it('reverts if vote nonce is incorrect', async function () {
const nonce = await this.mock.nonces(this.voterBySig.address);

expectRevertCustomError(
this.helper.vote({
support: Enums.VoteType.For,
voter: this.voterBySig.address,
nonce: nonce.addn(1),
signature: this.signature,
}),
// The signature check implies the nonce can't be tampered without changing the signer
'GovernorInvalidSigner',
[],
);
});
});
});
}
Expand Down
Loading