Skip to content
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

Add GovernorSequentialProposalId extension for sequential numbers on proposals #5290

Merged
merged 46 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a25d139
feat: governor sequential proposal id
arr00 Oct 25, 2024
a4ee283
use super hash
arr00 Oct 25, 2024
e34a8b9
Make `numberOfProposals` private
arr00 Oct 30, 2024
3cf1b80
Update contracts/governance/extensions/GovernorSequentialProposalId.sol
arr00 Oct 31, 2024
9063080
use internal `getProposalId` function
arr00 Nov 1, 2024
8755aa6
hash proposal is view
arr00 Nov 1, 2024
c52f001
Add `_setNextProposalId` helper func
arr00 Nov 4, 2024
d9f3477
Revert if next proposal id set and current proposal not 0
arr00 Nov 4, 2024
b063a61
Add `getNextProposalId` func
arr00 Nov 4, 2024
19dfdf8
Add view function `getProposalId`
arr00 Nov 18, 2024
185b6a3
Set proposal count instead of next
arr00 Nov 18, 2024
cb91e76
tests
Amxx Nov 18, 2024
5ff56da
revert when calling getProposalId before propose
Amxx Nov 18, 2024
f0e9fb5
Rename `getProposalCount` to `proposalCount`
arr00 Nov 19, 2024
becc77a
Update test/helpers/governance.js
Amxx Nov 19, 2024
8ffad72
fix test
arr00 Nov 19, 2024
3e49fb9
add patch
arr00 Nov 22, 2024
41d49c4
fix repropose bug
arr00 Nov 22, 2024
f8cfe02
interface Id
Amxx Nov 25, 2024
76d57a2
code style uniformity
Amxx Nov 25, 2024
74bdb35
require proposal count to be strictly increasing
arr00 Nov 26, 2024
3206080
Merge branch 'master' into feat/sequential-proposal-id-alt-2
arr00 Nov 26, 2024
c174090
update comment
arr00 Nov 26, 2024
9786e76
only allow setting the proposal count from 0
arr00 Nov 27, 2024
df711fe
update docs
arr00 Nov 27, 2024
a04b47f
fix test
arr00 Nov 27, 2024
2de3660
Update contracts/governance/IGovernor.sol
arr00 Dec 2, 2024
da959cd
move `proposalCount`
arr00 Dec 2, 2024
e037708
reuse governor interface
arr00 Dec 2, 2024
fdea2cc
add coverage
arr00 Dec 2, 2024
9b42d64
Update test/governance/extensions/GovernorStorage.test.js
Amxx Dec 3, 2024
1dcb637
Update contracts/governance/IGovernor.sol
Amxx Dec 3, 2024
9ec9b40
Update contracts/governance/extensions/GovernorSequentialProposalId.sol
arr00 Dec 3, 2024
99a4126
Update contracts/governance/extensions/GovernorSequentialProposalId.sol
arr00 Dec 3, 2024
98ee374
Add natspec
arr00 Dec 3, 2024
1526974
update docs
arr00 Dec 3, 2024
512aa40
change set proposal id to initialize proposal id
arr00 Dec 3, 2024
4791572
reword comment
arr00 Dec 3, 2024
0502607
change helper detection
Amxx Dec 4, 2024
b931eea
rename `proposalCount` to `nextProposalId`
arr00 Dec 10, 2024
0808e8c
add comment
arr00 Dec 10, 2024
a1194af
fix lint
arr00 Dec 10, 2024
8d532ec
Add changeset
arr00 Dec 10, 2024
7a78f2f
Replace `nextProposalId` with `latestProposalId`
arr00 Dec 11, 2024
df16ef1
remove 0 check on `_initializeLatestProposalId`
arr00 Dec 12, 2024
b10f9be
remove only
arr00 Dec 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/brave-islands-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`GovernorSequentialProposalId`: Adds a `Governor` extension that sequentially numbers proposal ids instead of using the hash.
5 changes: 5 additions & 0 deletions .changeset/ten-fishes-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`IGovernor`: Add the `getProposalId` function to the governor interface.
25 changes: 19 additions & 6 deletions contracts/governance/Governor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
return
interfaceId == type(IGovernor).interfaceId ||
interfaceId == type(IGovernor).interfaceId ^ IGovernor.getProposalId.selector ||
Amxx marked this conversation as resolved.
Show resolved Hide resolved
interfaceId == type(IERC1155Receiver).interfaceId ||
super.supportsInterface(interfaceId);
}
Expand Down Expand Up @@ -132,6 +133,18 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
return uint256(keccak256(abi.encode(targets, values, calldatas, descriptionHash)));
}

/**
* @dev See {IGovernor-getProposalId}.
*/
function getProposalId(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 descriptionHash
) public view virtual returns (uint256) {
return hashProposal(targets, values, calldatas, descriptionHash);
}

/**
* @dev See {IGovernor-state}.
*/
Expand Down Expand Up @@ -317,7 +330,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
string memory description,
address proposer
) internal virtual returns (uint256 proposalId) {
proposalId = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
proposalId = getProposalId(targets, values, calldatas, keccak256(bytes(description)));

if (targets.length != values.length || targets.length != calldatas.length || targets.length == 0) {
revert GovernorInvalidProposalLength(targets.length, calldatas.length, values.length);
Expand Down Expand Up @@ -358,7 +371,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
bytes[] memory calldatas,
bytes32 descriptionHash
) public virtual returns (uint256) {
uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);

_validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Succeeded));

Expand Down Expand Up @@ -406,7 +419,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
bytes[] memory calldatas,
bytes32 descriptionHash
) public payable virtual returns (uint256) {
uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);

_validateStateBitmap(
proposalId,
Expand Down Expand Up @@ -468,8 +481,8 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
) public virtual returns (uint256) {
// The proposalId will be recomputed in the `_cancel` call further down. However we need the value before we
// do the internal call, because we need to check the proposal state BEFORE the internal `_cancel` call
// changes it. The `hashProposal` duplication has a cost that is limited, and that we accept.
uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
// changes it. The `getProposalId` duplication has a cost that is limited, and that we accept.
uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);

// public cancel restrictions (on top of existing _cancel restrictions).
_validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Pending));
Expand All @@ -492,7 +505,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
bytes[] memory calldatas,
bytes32 descriptionHash
) internal virtual returns (uint256) {
uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);

_validateStateBitmap(
proposalId,
Expand Down
15 changes: 14 additions & 1 deletion contracts/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ interface IGovernor is IERC165, IERC6372 {

/**
* @notice module:core
* @dev Hashing function used to (re)build the proposal id from the proposal details..
* @dev Hashing function used to (re)build the proposal id from the proposal details.
*
* NOTE: For all off-chain and external calls, use {getProposalId}.
*/
function hashProposal(
address[] memory targets,
Expand All @@ -212,6 +214,17 @@ interface IGovernor is IERC165, IERC6372 {
bytes32 descriptionHash
) external pure returns (uint256);

/**
* @notice module:core
* @dev Function used to get the proposal id from the proposal details.
Amxx marked this conversation as resolved.
Show resolved Hide resolved
*/
function getProposalId(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 descriptionHash
) external view returns (uint256);

/**
* @notice module:core
* @dev Current state of a proposal, following Compound's convention
Expand Down
76 changes: 76 additions & 0 deletions contracts/governance/extensions/GovernorSequentialProposalId.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Governor} from "../Governor.sol";

/**
* @dev Extension of {Governor} that changes the numbering of proposal ids from the default hash-based approach to
* sequential ids.
*/
abstract contract GovernorSequentialProposalId is Governor {
arr00 marked this conversation as resolved.
Show resolved Hide resolved
uint256 private _latestProposalId;
mapping(uint256 proposalHash => uint256 proposalId) private _proposalIds;

/**
* @dev The {latestProposalId} may only be initialized if it hasn't been set yet
* (through initialization or the creation of a proposal).
*/
error GovernorAlreadyInitializedLatestProposalId();

/**
* @dev See {IGovernor-getProposalId}.
*/
function getProposalId(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 descriptionHash
) public view virtual override returns (uint256) {
uint256 proposalHash = hashProposal(targets, values, calldatas, descriptionHash);
uint256 storedProposalId = _proposalIds[proposalHash];
if (storedProposalId == 0) {
revert GovernorNonexistentProposal(0);
}
return storedProposalId;
}

/**
* @dev Returns the latest proposal id. A return value of 0 means no proposals have been created yet.
*/
function latestProposalId() public view virtual returns (uint256) {
return _latestProposalId;
}

/**
* @dev See {IGovernor-_propose}.
* Hook into the proposing mechanism to increment proposal count.
*/
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 = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
uint256 storedProposalId = _proposalIds[proposalHash];
if (storedProposalId == 0) {
_proposalIds[proposalHash] = ++_latestProposalId;
}
return super._propose(targets, values, calldatas, description, proposer);
}

/**
* @dev Internal function to set the {latestProposalId}. This function is helpful when transitioning
* from another governance system. The next proposal id will be `newLatestProposalId` + 1.
*
* May only call this function if the current value of {latestProposalId} is 0.
*/
function _initializeLatestProposalId(uint256 newLatestProposalId) internal virtual {
if (_latestProposalId != 0) {
revert GovernorAlreadyInitializedLatestProposalId();
}
_latestProposalId = newLatestProposalId;
}
}
39 changes: 39 additions & 0 deletions contracts/mocks/governance/GovernorSequentialProposalIdMock.sol
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 getProposalId(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 descriptionHash
) public view virtual override(Governor, GovernorSequentialProposalId) returns (uint256) {
return super.getProposalId(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);
}
}
2 changes: 1 addition & 1 deletion test/governance/Governor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Governor', function () {
);
});

shouldSupportInterfaces(['ERC1155Receiver', 'Governor']);
shouldSupportInterfaces(['ERC1155Receiver', 'Governor', 'Governor_5_3']);
shouldBehaveLikeERC6372(mode);

it('deployment check', async function () {
Expand Down
Loading
Loading