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

Governor: Sequential Proposal Ids #5290

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 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
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.
23 changes: 18 additions & 5 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 @@ -469,7 +482,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
// 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);
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
12 changes: 11 additions & 1 deletion contracts/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ 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.
*/
function hashProposal(
address[] memory targets,
Expand All @@ -212,6 +212,16 @@ interface IGovernor is IERC165, IERC6372 {
bytes32 descriptionHash
) external pure returns (uint256);

/**
* @dev Function used to get the proposal id from the proposal details.
*/
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
69 changes: 69 additions & 0 deletions contracts/governance/extensions/GovernorSequentialProposalId.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

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

abstract contract GovernorSequentialProposalId is Governor {
Copy link
Member

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

uint256 private _proposalCount;
mapping(uint256 proposalHash => uint256 proposalId) private _proposalIds;

/**
* @dev The proposal count may only be set if the current proposal count is 0 in {_setProposalCount}.
*/
error GovernorCanNotSetProposalCount();
Comment on lines +11 to +14
Copy link
Member

Choose a reason for hiding this comment

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

I'd rename it "uninitialized" since it better indicates what's wrong. Also, it goes well with the change I'm proposing to rename {_setProposalCount} to {_initializeProposalCount}. I think that instead of explaining the error literally, we should give a hint to solve it.

Suggested change
/**
* @dev The proposal count may only be set if the current proposal count is 0 in {_setProposalCount}.
*/
error GovernorCanNotSetProposalCount();
/**
* @dev The proposal count may only be set if the current proposal count is uninitialized.
*/
error GovernorUninitializedProposalCount();


/**
* @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 current proposal count.
*/
function proposalCount() public view virtual returns (uint256) {
return _proposalCount;
}

/**
* @dev 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] = ++_proposalCount;
}
return super._propose(targets, values, calldatas, description, proposer);
}

/**
* @dev Internal function to set the current proposal count--the next proposal id will be `newProposalCount` + 1.
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this sentence.

Suggested change
* @dev Internal function to set the current proposal count--the next proposal id will be `newProposalCount` + 1.
* @dev Internal function to set the current proposal count--the next proposal id will be `newProposalCount` + 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you suggest a change?

Copy link
Member

Choose a reason for hiding this comment

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

I forgot to come back and finish the suggestion. Now that I read it, it makes more sense.

I'd only suggest pointing to the definition of proposalCount. My confusion came because I thought the proposal count was an internal implementation detail. This should improve with some docs IMO.

Suggested change
* @dev Internal function to set the current proposal count--the next proposal id will be `newProposalCount` + 1.
* @dev Internal function to set the value of {proposalCount}--the next proposal id will be `newProposalCount` + 1.

*
* May only call this function if the current proposal count is 0.
*/
function _setProposalCount(uint256 newProposalCount) internal virtual {
Copy link
Member

Choose a reason for hiding this comment

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

I assume the purpose of including this function is to allow DAOs that are upgrading to set an initial proposal count in their initializers. If this is the case, I think this should be documented somewhere

Copy link
Member

Choose a reason for hiding this comment

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

I would be in favor of changing this function's name to indicate better what to do with it. In this case, it suggests developers that it should be used within an initializer.

Suggested change
function _setProposalCount(uint256 newProposalCount) internal virtual {
function _initializeProposalCount(uint256 newProposalCount) internal virtual {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As of now the function is only usable when the proposal count is 0 (one time use), but if there are legitimate use-cases for using it beyond that we would open it up to allow any increase in proposal count. Keeping this phrasing enables that to be a non-breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Given it's a one-time use, I think "initialize" is a good name.

The use case I was thinking of is the following:

  • Consider an upgradeable Governor (let's call it DAO) that currently uses hashed proposal Ids
  • OpenZeppelin Contracts releases the sequential id extension of Governor
  • DAO proposes an upgrade to itself. However, they've been enumerating their proposals in the description. Current is 12
  • The version 2 of DAO should make use of _initializeProposalCount(12) within their reinitializer function if their voters want to start voting ids starting from 12

I don't see any other legitimate use case to open up the function either. In this case, I think it makes sense to make it more explicit.

if (_proposalCount != 0) {
revert GovernorCanNotSetProposalCount();
}
_proposalCount = newProposalCount;
}
}
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