-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Governor: Sequential Proposal Ids #5290
Open
arr00
wants to merge
30
commits into
OpenZeppelin:master
Choose a base branch
from
arr00:feat/sequential-proposal-id-alt-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
a25d139
feat: governor sequential proposal id
arr00 a4ee283
use super hash
arr00 e34a8b9
Make `numberOfProposals` private
arr00 3cf1b80
Update contracts/governance/extensions/GovernorSequentialProposalId.sol
arr00 9063080
use internal `getProposalId` function
arr00 8755aa6
hash proposal is view
arr00 c52f001
Add `_setNextProposalId` helper func
arr00 d9f3477
Revert if next proposal id set and current proposal not 0
arr00 b063a61
Add `getNextProposalId` func
arr00 19dfdf8
Add view function `getProposalId`
arr00 185b6a3
Set proposal count instead of next
arr00 cb91e76
tests
Amxx 5ff56da
revert when calling getProposalId before propose
Amxx f0e9fb5
Rename `getProposalCount` to `proposalCount`
arr00 becc77a
Update test/helpers/governance.js
Amxx 8ffad72
fix test
arr00 3e49fb9
add patch
arr00 41d49c4
fix repropose bug
arr00 f8cfe02
interface Id
Amxx 76d57a2
code style uniformity
Amxx 74bdb35
require proposal count to be strictly increasing
arr00 3206080
Merge branch 'master' into feat/sequential-proposal-id-alt-2
arr00 c174090
update comment
arr00 9786e76
only allow setting the proposal count from 0
arr00 df711fe
update docs
arr00 a04b47f
fix test
arr00 2de3660
Update contracts/governance/IGovernor.sol
arr00 da959cd
move `proposalCount`
arr00 e037708
reuse governor interface
arr00 fdea2cc
add coverage
arr00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
contracts/governance/extensions/GovernorSequentialProposalId.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Governor} from "../Governor.sol"; | ||
|
||
abstract contract GovernorSequentialProposalId is Governor { | ||
uint256 private _numberOfProposals; | ||
mapping(uint256 proposalHash => uint256 proposalId) private _proposalIds; | ||
|
||
function hashProposal( | ||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
bytes32 descriptionHash | ||
) public view virtual override returns (uint256) { | ||
uint256 proposalHash = super.hashProposal(targets, values, calldatas, descriptionHash); | ||
|
||
uint256 storedProposalId = _proposalIds[proposalHash]; | ||
return storedProposalId == 0 ? (_numberOfProposals + 1) : storedProposalId; | ||
} | ||
|
||
function _propose( | ||
arr00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
string memory description, | ||
address proposer | ||
) internal virtual override returns (uint256) { | ||
uint256 proposalHash = super.hashProposal(targets, values, calldatas, keccak256(bytes(description))); | ||
_proposalIds[proposalHash] = ++_numberOfProposals; | ||
|
||
return super._propose(targets, values, calldatas, description, proposer); | ||
arr00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
contracts/mocks/governance/GovernorSequentialProposalIdMock.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Governor} from "../../governance/Governor.sol"; | ||
import {GovernorSettings} from "../../governance/extensions/GovernorSettings.sol"; | ||
import {GovernorCountingSimple} from "../../governance/extensions/GovernorCountingSimple.sol"; | ||
import {GovernorVotesQuorumFraction} from "../../governance/extensions/GovernorVotesQuorumFraction.sol"; | ||
import {GovernorSequentialProposalId} from "../../governance/extensions/GovernorSequentialProposalId.sol"; | ||
|
||
abstract contract GovernorSequentialProposalIdMock is | ||
GovernorSettings, | ||
GovernorVotesQuorumFraction, | ||
GovernorCountingSimple, | ||
GovernorSequentialProposalId | ||
{ | ||
function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) { | ||
return super.proposalThreshold(); | ||
} | ||
|
||
function hashProposal( | ||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
bytes32 descriptionHash | ||
) public view virtual override(Governor, GovernorSequentialProposalId) returns (uint256) { | ||
return super.hashProposal(targets, values, calldatas, descriptionHash); | ||
} | ||
|
||
function _propose( | ||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
string memory description, | ||
address proposer | ||
) internal virtual override(Governor, GovernorSequentialProposalId) returns (uint256 proposalId) { | ||
return super._propose(targets, values, calldatas, description, proposer); | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
test/governance/extensions/GovernorSequentialProposalId.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
||
const { GovernorHelper } = require('../../helpers/governance'); | ||
const { VoteType } = require('../../helpers/enums'); | ||
const time = require('../../helpers/time'); | ||
|
||
const TOKENS = [ | ||
{ Token: '$ERC20Votes', mode: 'blocknumber' }, | ||
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' }, | ||
]; | ||
|
||
const name = 'OZ-Governor'; | ||
const version = '1'; | ||
const tokenName = 'MockToken'; | ||
const tokenSymbol = 'MTKN'; | ||
const tokenSupply = ethers.parseEther('100'); | ||
const votingDelay = 4n; | ||
const votingPeriod = 16n; | ||
const value = ethers.parseEther('1'); | ||
|
||
async function deployToken(contractName) { | ||
try { | ||
return await ethers.deployContract(contractName, [tokenName, tokenSymbol, tokenName, version]); | ||
} catch (error) { | ||
if (error.message == 'incorrect number of arguments to constructor') { | ||
// ERC20VotesLegacyMock has a different construction that uses version='1' by default. | ||
return ethers.deployContract(contractName, [tokenName, tokenSymbol, tokenName]); | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
describe('GovernorSequentialProposalId', function () { | ||
for (const { Token, mode } of TOKENS) { | ||
const fixture = async () => { | ||
const [owner, proposer, voter1, voter2, voter3, voter4, userEOA] = await ethers.getSigners(); | ||
const receiver = await ethers.deployContract('CallReceiverMock'); | ||
|
||
const token = await deployToken(Token, [tokenName, tokenSymbol, version]); | ||
const mock = await ethers.deployContract('$GovernorSequentialProposalIdMock', [ | ||
name, // name | ||
votingDelay, // initialVotingDelay | ||
votingPeriod, // initialVotingPeriod | ||
0n, // initialProposalThreshold | ||
token, // tokenAddress | ||
10n, // quorumNumeratorValue | ||
]); | ||
|
||
await owner.sendTransaction({ to: mock, value }); | ||
await token.$_mint(owner, tokenSupply); | ||
|
||
const helper = new GovernorHelper(mock, mode); | ||
await helper.connect(owner).delegate({ token: token, to: voter1, value: ethers.parseEther('10') }); | ||
await helper.connect(owner).delegate({ token: token, to: voter2, value: ethers.parseEther('7') }); | ||
await helper.connect(owner).delegate({ token: token, to: voter3, value: ethers.parseEther('5') }); | ||
await helper.connect(owner).delegate({ token: token, to: voter4, value: ethers.parseEther('2') }); | ||
|
||
return { | ||
owner, | ||
proposer, | ||
voter1, | ||
voter2, | ||
voter3, | ||
voter4, | ||
userEOA, | ||
receiver, | ||
token, | ||
mock, | ||
helper, | ||
}; | ||
}; | ||
|
||
describe(`using ${Token}`, function () { | ||
beforeEach(async function () { | ||
Object.assign(this, await loadFixture(fixture)); | ||
|
||
this.proposal = this.helper.setProposal( | ||
[ | ||
{ | ||
target: this.receiver.target, | ||
data: this.receiver.interface.encodeFunctionData('mockFunction'), | ||
value, | ||
}, | ||
], | ||
'<proposal description>', | ||
); | ||
}); | ||
|
||
it('sequential proposal ids', async function () { | ||
const txPropose = await this.helper.connect(this.proposer).propose(); | ||
const timepoint = await time.clockFromReceipt[mode](txPropose); | ||
|
||
await expect(txPropose) | ||
.to.emit(this.mock, 'ProposalCreated') | ||
.withArgs( | ||
1, | ||
this.proposer, | ||
this.proposal.targets, | ||
this.proposal.values, | ||
this.proposal.signatures, | ||
this.proposal.data, | ||
timepoint + votingDelay, | ||
timepoint + votingDelay + votingPeriod, | ||
this.proposal.description, | ||
); | ||
|
||
this.proposal = this.helper.setProposal( | ||
[ | ||
{ | ||
target: this.receiver.target, | ||
data: this.receiver.interface.encodeFunctionData('mockFunction'), | ||
value, | ||
}, | ||
], | ||
'<proposal description 2>', | ||
); | ||
const txPropose2 = await this.helper.connect(this.proposer).propose(); | ||
const timepoint2 = await time.clockFromReceipt[mode](txPropose2); | ||
|
||
await expect(txPropose2) | ||
.to.emit(this.mock, 'ProposalCreated') | ||
.withArgs( | ||
2, | ||
this.proposer, | ||
this.proposal.targets, | ||
this.proposal.values, | ||
this.proposal.signatures, | ||
this.proposal.data, | ||
timepoint2 + votingDelay, | ||
timepoint2 + votingDelay + votingPeriod, | ||
this.proposal.description, | ||
); | ||
}); | ||
|
||
it('nominal workflow', async function () { | ||
await this.helper.connect(this.proposer).propose(); | ||
let timepoint = await this.mock.proposalSnapshot(1); | ||
await time.increaseTo[mode](timepoint); | ||
|
||
await expect(this.mock.connect(this.voter1).castVote(1, VoteType.For)) | ||
.to.emit(this.mock, 'VoteCast') | ||
.withArgs(this.voter1, 1, VoteType.For, ethers.parseEther('10'), ''); | ||
|
||
await expect(this.mock.connect(this.voter2).castVote(1, VoteType.For)) | ||
.to.emit(this.mock, 'VoteCast') | ||
.withArgs(this.voter2, 1, VoteType.For, ethers.parseEther('7'), ''); | ||
|
||
await expect(this.mock.connect(this.voter3).castVote(1, VoteType.For)) | ||
.to.emit(this.mock, 'VoteCast') | ||
.withArgs(this.voter3, 1, VoteType.For, ethers.parseEther('5'), ''); | ||
|
||
await expect(this.mock.connect(this.voter4).castVote(1, VoteType.Abstain)) | ||
.to.emit(this.mock, 'VoteCast') | ||
.withArgs(this.voter4, 1, VoteType.Abstain, ethers.parseEther('2'), ''); | ||
|
||
timepoint = await this.mock.proposalDeadline(1); | ||
await time.increaseTo[mode](timepoint); | ||
|
||
expect(this.helper.execute()) | ||
.to.eventually.emit(this.mock, 'ProposalExecuted') | ||
.withArgs(1) | ||
.emit(this.receiver, 'MockFunctionCalled'); | ||
}); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're missing NatSpec here