123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.2.0) (governance/Governor.sol)
- pragma solidity ^0.8.20;
- import {IERC721Receiver} from "../token/ERC721/IERC721Receiver.sol";
- import {IERC1155Receiver} from "../token/ERC1155/IERC1155Receiver.sol";
- import {EIP712} from "../utils/cryptography/EIP712.sol";
- import {SignatureChecker} from "../utils/cryptography/SignatureChecker.sol";
- import {IERC165, ERC165} from "../utils/introspection/ERC165.sol";
- import {SafeCast} from "../utils/math/SafeCast.sol";
- import {DoubleEndedQueue} from "../utils/structs/DoubleEndedQueue.sol";
- import {Address} from "../utils/Address.sol";
- import {Context} from "../utils/Context.sol";
- import {Nonces} from "../utils/Nonces.sol";
- import {Strings} from "../utils/Strings.sol";
- import {IGovernor, IERC6372} from "./IGovernor.sol";
- /**
- * @dev Core of the governance system, designed to be extended through various modules.
- *
- * This contract is abstract and requires several functions to be implemented in various modules:
- *
- * - A counting module must implement {quorum}, {_quorumReached}, {_voteSucceeded} and {_countVote}
- * - A voting module must implement {_getVotes}
- * - Additionally, {votingPeriod} must also be implemented
- */
- 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,address voter,uint256 nonce)");
- bytes32 public constant EXTENDED_BALLOT_TYPEHASH =
- keccak256(
- "ExtendedBallot(uint256 proposalId,uint8 support,address voter,uint256 nonce,string reason,bytes params)"
- );
- struct ProposalCore {
- address proposer;
- uint48 voteStart;
- uint32 voteDuration;
- bool executed;
- bool canceled;
- uint48 etaSeconds;
- }
- bytes32 private constant ALL_PROPOSAL_STATES_BITMAP = bytes32((2 ** (uint8(type(ProposalState).max) + 1)) - 1);
- string private _name;
- mapping(uint256 proposalId => ProposalCore) private _proposals;
- // This queue keeps track of the governor operating on itself. Calls to functions protected by the {onlyGovernance}
- // modifier needs to be whitelisted in this queue. Whitelisting is set in {execute}, consumed by the
- // {onlyGovernance} modifier and eventually reset after {_executeOperations} completes. This ensures that the
- // execution of {onlyGovernance} protected calls can only be achieved through successful proposals.
- DoubleEndedQueue.Bytes32Deque private _governanceCall;
- /**
- * @dev Restricts a function so it can only be executed through governance proposals. For example, governance
- * parameter setters in {GovernorSettings} are protected using this modifier.
- *
- * The governance executing address may be different from the Governor's own address, for example it could be a
- * timelock. This can be customized by modules by overriding {_executor}. The executor is only able to invoke these
- * functions during the execution of the governor's {execute} function, and not under any other circumstances. Thus,
- * for example, additional timelock proposers are not able to change governance parameters without going through the
- * governance protocol (since v4.6).
- */
- modifier onlyGovernance() {
- _checkGovernance();
- _;
- }
- /**
- * @dev Sets the value for {name} and {version}
- */
- constructor(string memory name_) EIP712(name_, version()) {
- _name = name_;
- }
- /**
- * @dev Function to receive ETH that will be handled by the governor (disabled if executor is a third party contract)
- */
- receive() external payable virtual {
- if (_executor() != address(this)) {
- revert GovernorDisabledDeposit();
- }
- }
- /**
- * @dev See {IERC165-supportsInterface}.
- */
- function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
- return
- interfaceId == type(IGovernor).interfaceId ||
- interfaceId == type(IGovernor).interfaceId ^ IGovernor.getProposalId.selector ||
- interfaceId == type(IERC1155Receiver).interfaceId ||
- super.supportsInterface(interfaceId);
- }
- /**
- * @dev See {IGovernor-name}.
- */
- function name() public view virtual returns (string memory) {
- return _name;
- }
- /**
- * @dev See {IGovernor-version}.
- */
- function version() public view virtual returns (string memory) {
- return "1";
- }
- /**
- * @dev See {IGovernor-hashProposal}.
- *
- * The proposal id is produced by hashing the ABI encoded `targets` array, the `values` array, the `calldatas` array
- * and the descriptionHash (bytes32 which itself is the keccak256 hash of the description string). This proposal id
- * can be produced from the proposal data which is part of the {ProposalCreated} event. It can even be computed in
- * advance, before the proposal is submitted.
- *
- * Note that the chainId and the governor address are not part of the proposal id computation. Consequently, the
- * same proposal (with same operation and same description) will have the same id if submitted on multiple governors
- * across multiple networks. This also means that in order to execute the same operation twice (on the same
- * governor) the proposer will have to change the description in order to avoid proposal id conflicts.
- */
- function hashProposal(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 descriptionHash
- ) public pure virtual returns (uint256) {
- 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}.
- */
- function state(uint256 proposalId) public view virtual returns (ProposalState) {
- // We read the struct fields into the stack at once so Solidity emits a single SLOAD
- ProposalCore storage proposal = _proposals[proposalId];
- bool proposalExecuted = proposal.executed;
- bool proposalCanceled = proposal.canceled;
- if (proposalExecuted) {
- return ProposalState.Executed;
- }
- if (proposalCanceled) {
- return ProposalState.Canceled;
- }
- uint256 snapshot = proposalSnapshot(proposalId);
- if (snapshot == 0) {
- revert GovernorNonexistentProposal(proposalId);
- }
- uint256 currentTimepoint = clock();
- if (snapshot >= currentTimepoint) {
- return ProposalState.Pending;
- }
- uint256 deadline = proposalDeadline(proposalId);
- if (deadline >= currentTimepoint) {
- return ProposalState.Active;
- } else if (!_quorumReached(proposalId) || !_voteSucceeded(proposalId)) {
- return ProposalState.Defeated;
- } else if (proposalEta(proposalId) == 0) {
- return ProposalState.Succeeded;
- } else {
- return ProposalState.Queued;
- }
- }
- /**
- * @dev See {IGovernor-proposalThreshold}.
- */
- function proposalThreshold() public view virtual returns (uint256) {
- return 0;
- }
- /**
- * @dev See {IGovernor-proposalSnapshot}.
- */
- function proposalSnapshot(uint256 proposalId) public view virtual returns (uint256) {
- return _proposals[proposalId].voteStart;
- }
- /**
- * @dev See {IGovernor-proposalDeadline}.
- */
- function proposalDeadline(uint256 proposalId) public view virtual returns (uint256) {
- return _proposals[proposalId].voteStart + _proposals[proposalId].voteDuration;
- }
- /**
- * @dev See {IGovernor-proposalProposer}.
- */
- function proposalProposer(uint256 proposalId) public view virtual returns (address) {
- return _proposals[proposalId].proposer;
- }
- /**
- * @dev See {IGovernor-proposalEta}.
- */
- function proposalEta(uint256 proposalId) public view virtual returns (uint256) {
- return _proposals[proposalId].etaSeconds;
- }
- /**
- * @dev See {IGovernor-proposalNeedsQueuing}.
- */
- function proposalNeedsQueuing(uint256) public view virtual returns (bool) {
- return false;
- }
- /**
- * @dev Reverts if the `msg.sender` is not the executor. In case the executor is not this contract
- * itself, the function reverts if `msg.data` is not whitelisted as a result of an {execute}
- * operation. See {onlyGovernance}.
- */
- function _checkGovernance() internal virtual {
- if (_executor() != _msgSender()) {
- revert GovernorOnlyExecutor(_msgSender());
- }
- if (_executor() != address(this)) {
- bytes32 msgDataHash = keccak256(_msgData());
- // loop until popping the expected operation - throw if deque is empty (operation not authorized)
- while (_governanceCall.popFront() != msgDataHash) {}
- }
- }
- /**
- * @dev Amount of votes already cast passes the threshold limit.
- */
- function _quorumReached(uint256 proposalId) internal view virtual returns (bool);
- /**
- * @dev Is the proposal successful or not.
- */
- function _voteSucceeded(uint256 proposalId) internal view virtual returns (bool);
- /**
- * @dev Get the voting weight of `account` at a specific `timepoint`, for a vote as described by `params`.
- */
- function _getVotes(address account, uint256 timepoint, bytes memory params) internal view virtual returns (uint256);
- /**
- * @dev Register a vote for `proposalId` by `account` with a given `support`, voting `weight` and voting `params`.
- *
- * Note: Support is generic and can represent various things depending on the voting system used.
- */
- function _countVote(
- uint256 proposalId,
- address account,
- uint8 support,
- uint256 totalWeight,
- bytes memory params
- ) internal virtual returns (uint256);
- /**
- * @dev Hook that should be called every time the tally for a proposal is updated.
- *
- * Note: This function must run successfully. Reverts will result in the bricking of governance
- */
- function _tallyUpdated(uint256 proposalId) internal virtual {}
- /**
- * @dev Default additional encoded parameters used by castVote methods that don't include them
- *
- * Note: Should be overridden by specific implementations to use an appropriate value, the
- * meaning of the additional params, in the context of that implementation
- */
- function _defaultParams() internal view virtual returns (bytes memory) {
- return "";
- }
- /**
- * @dev See {IGovernor-propose}. This function has opt-in frontrunning protection, described in {_isValidDescriptionForProposer}.
- */
- function propose(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- string memory description
- ) public virtual returns (uint256) {
- address proposer = _msgSender();
- // check description restriction
- if (!_isValidDescriptionForProposer(proposer, description)) {
- revert GovernorRestrictedProposer(proposer);
- }
- // check proposal threshold
- uint256 votesThreshold = proposalThreshold();
- if (votesThreshold > 0) {
- uint256 proposerVotes = getVotes(proposer, clock() - 1);
- if (proposerVotes < votesThreshold) {
- revert GovernorInsufficientProposerVotes(proposer, proposerVotes, votesThreshold);
- }
- }
- return _propose(targets, values, calldatas, description, proposer);
- }
- /**
- * @dev Internal propose mechanism. Can be overridden to add more logic on proposal creation.
- *
- * Emits a {IGovernor-ProposalCreated} event.
- */
- function _propose(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- string memory description,
- address proposer
- ) internal virtual returns (uint256 proposalId) {
- 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);
- }
- if (_proposals[proposalId].voteStart != 0) {
- revert GovernorUnexpectedProposalState(proposalId, state(proposalId), bytes32(0));
- }
- uint256 snapshot = clock() + votingDelay();
- uint256 duration = votingPeriod();
- ProposalCore storage proposal = _proposals[proposalId];
- proposal.proposer = proposer;
- proposal.voteStart = SafeCast.toUint48(snapshot);
- proposal.voteDuration = SafeCast.toUint32(duration);
- emit ProposalCreated(
- proposalId,
- proposer,
- targets,
- values,
- new string[](targets.length),
- calldatas,
- snapshot,
- snapshot + duration,
- description
- );
- // Using a named return variable to avoid stack too deep errors
- }
- /**
- * @dev See {IGovernor-queue}.
- */
- function queue(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 descriptionHash
- ) public virtual returns (uint256) {
- uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);
- _validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Succeeded));
- uint48 etaSeconds = _queueOperations(proposalId, targets, values, calldatas, descriptionHash);
- if (etaSeconds != 0) {
- _proposals[proposalId].etaSeconds = etaSeconds;
- emit ProposalQueued(proposalId, etaSeconds);
- } else {
- revert GovernorQueueNotImplemented();
- }
- return proposalId;
- }
- /**
- * @dev Internal queuing mechanism. Can be overridden (without a super call) to modify the way queuing is
- * performed (for example adding a vault/timelock).
- *
- * This is empty by default, and must be overridden to implement queuing.
- *
- * This function returns a timestamp that describes the expected ETA for execution. If the returned value is 0
- * (which is the default value), the core will consider queueing did not succeed, and the public {queue} function
- * will revert.
- *
- * NOTE: Calling this function directly will NOT check the current state of the proposal, or emit the
- * `ProposalQueued` event. Queuing a proposal should be done using {queue}.
- */
- function _queueOperations(
- uint256 /*proposalId*/,
- address[] memory /*targets*/,
- uint256[] memory /*values*/,
- bytes[] memory /*calldatas*/,
- bytes32 /*descriptionHash*/
- ) internal virtual returns (uint48) {
- return 0;
- }
- /**
- * @dev See {IGovernor-execute}.
- */
- function execute(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 descriptionHash
- ) public payable virtual returns (uint256) {
- uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);
- _validateStateBitmap(
- proposalId,
- _encodeStateBitmap(ProposalState.Succeeded) | _encodeStateBitmap(ProposalState.Queued)
- );
- // mark as executed before calls to avoid reentrancy
- _proposals[proposalId].executed = true;
- // before execute: register governance call in queue.
- if (_executor() != address(this)) {
- for (uint256 i = 0; i < targets.length; ++i) {
- if (targets[i] == address(this)) {
- _governanceCall.pushBack(keccak256(calldatas[i]));
- }
- }
- }
- _executeOperations(proposalId, targets, values, calldatas, descriptionHash);
- // after execute: cleanup governance call queue.
- if (_executor() != address(this) && !_governanceCall.empty()) {
- _governanceCall.clear();
- }
- emit ProposalExecuted(proposalId);
- return proposalId;
- }
- /**
- * @dev Internal execution mechanism. Can be overridden (without a super call) to modify the way execution is
- * performed (for example adding a vault/timelock).
- *
- * NOTE: Calling this function directly will NOT check the current state of the proposal, set the executed flag to
- * true or emit the `ProposalExecuted` event. Executing a proposal should be done using {execute}.
- */
- function _executeOperations(
- uint256 /* proposalId */,
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 /*descriptionHash*/
- ) internal virtual {
- for (uint256 i = 0; i < targets.length; ++i) {
- (bool success, bytes memory returndata) = targets[i].call{value: values[i]}(calldatas[i]);
- Address.verifyCallResult(success, returndata);
- }
- }
- /**
- * @dev See {IGovernor-cancel}.
- */
- function cancel(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 descriptionHash
- ) 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 `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));
- if (_msgSender() != proposalProposer(proposalId)) {
- revert GovernorOnlyProposer(_msgSender());
- }
- return _cancel(targets, values, calldatas, descriptionHash);
- }
- /**
- * @dev Internal cancel mechanism with minimal restrictions. A proposal can be cancelled in any state other than
- * Canceled, Expired, or Executed. Once cancelled a proposal can't be re-submitted.
- *
- * Emits a {IGovernor-ProposalCanceled} event.
- */
- function _cancel(
- address[] memory targets,
- uint256[] memory values,
- bytes[] memory calldatas,
- bytes32 descriptionHash
- ) internal virtual returns (uint256) {
- uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash);
- _validateStateBitmap(
- proposalId,
- ALL_PROPOSAL_STATES_BITMAP ^
- _encodeStateBitmap(ProposalState.Canceled) ^
- _encodeStateBitmap(ProposalState.Expired) ^
- _encodeStateBitmap(ProposalState.Executed)
- );
- _proposals[proposalId].canceled = true;
- emit ProposalCanceled(proposalId);
- return proposalId;
- }
- /**
- * @dev See {IGovernor-getVotes}.
- */
- function getVotes(address account, uint256 timepoint) public view virtual returns (uint256) {
- return _getVotes(account, timepoint, _defaultParams());
- }
- /**
- * @dev See {IGovernor-getVotesWithParams}.
- */
- function getVotesWithParams(
- address account,
- uint256 timepoint,
- bytes memory params
- ) public view virtual returns (uint256) {
- return _getVotes(account, timepoint, params);
- }
- /**
- * @dev See {IGovernor-castVote}.
- */
- function castVote(uint256 proposalId, uint8 support) public virtual returns (uint256) {
- address voter = _msgSender();
- return _castVote(proposalId, voter, support, "");
- }
- /**
- * @dev See {IGovernor-castVoteWithReason}.
- */
- function castVoteWithReason(
- uint256 proposalId,
- uint8 support,
- string calldata reason
- ) public virtual returns (uint256) {
- address voter = _msgSender();
- return _castVote(proposalId, voter, support, reason);
- }
- /**
- * @dev See {IGovernor-castVoteWithReasonAndParams}.
- */
- function castVoteWithReasonAndParams(
- uint256 proposalId,
- uint8 support,
- string calldata reason,
- bytes memory params
- ) public virtual returns (uint256) {
- address voter = _msgSender();
- return _castVote(proposalId, voter, support, reason, params);
- }
- /**
- * @dev See {IGovernor-castVoteBySig}.
- */
- function castVoteBySig(
- uint256 proposalId,
- uint8 support,
- address voter,
- bytes memory signature
- ) public virtual returns (uint256) {
- bool valid = SignatureChecker.isValidSignatureNow(
- voter,
- _hashTypedDataV4(keccak256(abi.encode(BALLOT_TYPEHASH, proposalId, support, voter, _useNonce(voter)))),
- signature
- );
- if (!valid) {
- revert GovernorInvalidSignature(voter);
- }
- return _castVote(proposalId, voter, support, "");
- }
- /**
- * @dev See {IGovernor-castVoteWithReasonAndParamsBySig}.
- */
- function castVoteWithReasonAndParamsBySig(
- uint256 proposalId,
- uint8 support,
- address voter,
- string calldata reason,
- bytes memory params,
- bytes memory signature
- ) public virtual returns (uint256) {
- bool valid = SignatureChecker.isValidSignatureNow(
- voter,
- _hashTypedDataV4(
- keccak256(
- abi.encode(
- EXTENDED_BALLOT_TYPEHASH,
- proposalId,
- support,
- voter,
- _useNonce(voter),
- keccak256(bytes(reason)),
- keccak256(params)
- )
- )
- ),
- signature
- );
- if (!valid) {
- revert GovernorInvalidSignature(voter);
- }
- return _castVote(proposalId, voter, support, reason, params);
- }
- /**
- * @dev Internal vote casting mechanism: Check that the vote is pending, that it has not been cast yet, retrieve
- * voting weight using {IGovernor-getVotes} and call the {_countVote} internal function. Uses the _defaultParams().
- *
- * Emits a {IGovernor-VoteCast} event.
- */
- function _castVote(
- uint256 proposalId,
- address account,
- uint8 support,
- string memory reason
- ) internal virtual returns (uint256) {
- return _castVote(proposalId, account, support, reason, _defaultParams());
- }
- /**
- * @dev Internal vote casting mechanism: Check that the vote is pending, that it has not been cast yet, retrieve
- * voting weight using {IGovernor-getVotes} and call the {_countVote} internal function.
- *
- * Emits a {IGovernor-VoteCast} event.
- */
- function _castVote(
- uint256 proposalId,
- address account,
- uint8 support,
- string memory reason,
- bytes memory params
- ) internal virtual returns (uint256) {
- _validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Active));
- uint256 totalWeight = _getVotes(account, proposalSnapshot(proposalId), params);
- uint256 votedWeight = _countVote(proposalId, account, support, totalWeight, params);
- if (params.length == 0) {
- emit VoteCast(account, proposalId, support, votedWeight, reason);
- } else {
- emit VoteCastWithParams(account, proposalId, support, votedWeight, reason, params);
- }
- _tallyUpdated(proposalId);
- return votedWeight;
- }
- /**
- * @dev Relays a transaction or function call to an arbitrary target. In cases where the governance executor
- * is some contract other than the governor itself, like when using a timelock, this function can be invoked
- * in a governance proposal to recover tokens or Ether that was sent to the governor contract by mistake.
- * Note that if the executor is simply the governor itself, use of `relay` is redundant.
- */
- function relay(address target, uint256 value, bytes calldata data) external payable virtual onlyGovernance {
- (bool success, bytes memory returndata) = target.call{value: value}(data);
- Address.verifyCallResult(success, returndata);
- }
- /**
- * @dev Address through which the governor executes action. Will be overloaded by module that execute actions
- * through another contract such as a timelock.
- */
- function _executor() internal view virtual returns (address) {
- return address(this);
- }
- /**
- * @dev See {IERC721Receiver-onERC721Received}.
- * Receiving tokens is disabled if the governance executor is other than the governor itself (eg. when using with a timelock).
- */
- function onERC721Received(address, address, uint256, bytes memory) public virtual returns (bytes4) {
- if (_executor() != address(this)) {
- revert GovernorDisabledDeposit();
- }
- return this.onERC721Received.selector;
- }
- /**
- * @dev See {IERC1155Receiver-onERC1155Received}.
- * Receiving tokens is disabled if the governance executor is other than the governor itself (eg. when using with a timelock).
- */
- function onERC1155Received(address, address, uint256, uint256, bytes memory) public virtual returns (bytes4) {
- if (_executor() != address(this)) {
- revert GovernorDisabledDeposit();
- }
- return this.onERC1155Received.selector;
- }
- /**
- * @dev See {IERC1155Receiver-onERC1155BatchReceived}.
- * Receiving tokens is disabled if the governance executor is other than the governor itself (eg. when using with a timelock).
- */
- function onERC1155BatchReceived(
- address,
- address,
- uint256[] memory,
- uint256[] memory,
- bytes memory
- ) public virtual returns (bytes4) {
- if (_executor() != address(this)) {
- revert GovernorDisabledDeposit();
- }
- return this.onERC1155BatchReceived.selector;
- }
- /**
- * @dev Encodes a `ProposalState` into a `bytes32` representation where each bit enabled corresponds to
- * the underlying position in the `ProposalState` enum. For example:
- *
- * 0x000...10000
- * ^^^^^^------ ...
- * ^----- Succeeded
- * ^---- Defeated
- * ^--- Canceled
- * ^-- Active
- * ^- Pending
- */
- function _encodeStateBitmap(ProposalState proposalState) internal pure returns (bytes32) {
- return bytes32(1 << uint8(proposalState));
- }
- /**
- * @dev Check that the current state of a proposal matches the requirements described by the `allowedStates` bitmap.
- * This bitmap should be built using `_encodeStateBitmap`.
- *
- * If requirements are not met, reverts with a {GovernorUnexpectedProposalState} error.
- */
- function _validateStateBitmap(uint256 proposalId, bytes32 allowedStates) internal view returns (ProposalState) {
- ProposalState currentState = state(proposalId);
- if (_encodeStateBitmap(currentState) & allowedStates == bytes32(0)) {
- revert GovernorUnexpectedProposalState(proposalId, currentState, allowedStates);
- }
- return currentState;
- }
- /*
- * @dev Check if the proposer is authorized to submit a proposal with the given description.
- *
- * If the proposal description ends with `#proposer=0x???`, where `0x???` is an address written as a hex string
- * (case insensitive), then the submission of this proposal will only be authorized to said address.
- *
- * This is used for frontrunning protection. By adding this pattern at the end of their proposal, one can ensure
- * that no other address can submit the same proposal. An attacker would have to either remove or change that part,
- * which would result in a different proposal id.
- *
- * If the description does not match this pattern, it is unrestricted and anyone can submit it. This includes:
- * - If the `0x???` part is not a valid hex string.
- * - If the `0x???` part is a valid hex string, but does not contain exactly 40 hex digits.
- * - If it ends with the expected suffix followed by newlines or other whitespace.
- * - If it ends with some other similar suffix, e.g. `#other=abc`.
- * - If it does not end with any such suffix.
- */
- function _isValidDescriptionForProposer(
- address proposer,
- string memory description
- ) internal view virtual returns (bool) {
- unchecked {
- uint256 length = bytes(description).length;
- // Length is too short to contain a valid proposer suffix
- if (length < 52) {
- return true;
- }
- // Extract what would be the `#proposer=` marker beginning the suffix
- bytes10 marker = bytes10(_unsafeReadBytesOffset(bytes(description), length - 52));
- // If the marker is not found, there is no proposer suffix to check
- if (marker != bytes10("#proposer=")) {
- return true;
- }
- // Check that the last 42 characters (after the marker) are a properly formatted address.
- (bool success, address recovered) = Strings.tryParseAddress(description, length - 42, length);
- return !success || recovered == proposer;
- }
- }
- /**
- * @inheritdoc IERC6372
- */
- function clock() public view virtual returns (uint48);
- /**
- * @inheritdoc IERC6372
- */
- // solhint-disable-next-line func-name-mixedcase
- function CLOCK_MODE() public view virtual returns (string memory);
- /**
- * @inheritdoc IGovernor
- */
- function votingDelay() public view virtual returns (uint256);
- /**
- * @inheritdoc IGovernor
- */
- function votingPeriod() public view virtual returns (uint256);
- /**
- * @inheritdoc IGovernor
- */
- function quorum(uint256 timepoint) public view virtual returns (uint256);
- /**
- * @dev Reads a bytes32 from a bytes array without bounds checking.
- *
- * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
- * assembly block as such would prevent some optimizations.
- */
- function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
- // This is not memory safe in the general case, but all calls to this private function are within bounds.
- assembly ("memory-safe") {
- value := mload(add(buffer, add(0x20, offset)))
- }
- }
- }
|