-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GovernorSequentialProposalId extension for sequential numbers on …
…proposals (#5290) Co-authored-by: Hadrien Croubois <[email protected]> Co-authored-by: Ernesto García <[email protected]>
- Loading branch information
1 parent
3b240d7
commit 03e06bf
Showing
10 changed files
with
405 additions
and
48 deletions.
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
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. |
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,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`IGovernor`: Add the `getProposalId` function to the governor interface. |
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
76 changes: 76 additions & 0 deletions
76
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,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 { | ||
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( | ||
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
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 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); | ||
} | ||
} |
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
Oops, something went wrong.