|
@@ -1,12 +1,15 @@
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
-// OpenZeppelin Contracts (last updated v4.5.0) (governance/Governor.sol)
|
|
|
+// OpenZeppelin Contracts (last updated v4.6.0) (governance/Governor.sol)
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
+import "../token/ERC721/IERC721Receiver.sol";
|
|
|
+import "../token/ERC1155/IERC1155Receiver.sol";
|
|
|
import "../utils/cryptography/ECDSA.sol";
|
|
|
import "../utils/cryptography/draft-EIP712.sol";
|
|
|
import "../utils/introspection/ERC165.sol";
|
|
|
import "../utils/math/SafeCast.sol";
|
|
|
+import "../utils/structs/DoubleEndedQueue.sol";
|
|
|
import "../utils/Address.sol";
|
|
|
import "../utils/Context.sol";
|
|
|
import "../utils/Timers.sol";
|
|
@@ -18,16 +21,19 @@ import "./IGovernor.sol";
|
|
|
* This contract is abstract and requires several function to be implemented in various modules:
|
|
|
*
|
|
|
* - A counting module must implement {quorum}, {_quorumReached}, {_voteSucceeded} and {_countVote}
|
|
|
- * - A voting module must implement {getVotes}
|
|
|
+ * - A voting module must implement {_getVotes}
|
|
|
* - Additionanly, the {votingPeriod} must also be implemented
|
|
|
*
|
|
|
* _Available since v4.3._
|
|
|
*/
|
|
|
-abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
+abstract contract Governor is Context, ERC165, EIP712, IGovernor, IERC721Receiver, IERC1155Receiver {
|
|
|
+ using DoubleEndedQueue for DoubleEndedQueue.Bytes32Deque;
|
|
|
using SafeCast for uint256;
|
|
|
using Timers for Timers.BlockNumber;
|
|
|
|
|
|
bytes32 public constant BALLOT_TYPEHASH = keccak256("Ballot(uint256 proposalId,uint8 support)");
|
|
|
+ bytes32 public constant EXTENDED_BALLOT_TYPEHASH =
|
|
|
+ keccak256("ExtendedBallot(uint256 proposalId,uint8 support,string reason,bytes params)");
|
|
|
|
|
|
struct ProposalCore {
|
|
|
Timers.BlockNumber voteStart;
|
|
@@ -40,13 +46,29 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
|
|
|
mapping(uint256 => 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 {_beforeExecute},
|
|
|
+ // consumed by the {onlyGovernance} modifier and eventually reset in {_afterExecute}. This ensures that the
|
|
|
+ // execution of {onlyGovernance} protected calls can only be achieved through successful proposals.
|
|
|
+ DoubleEndedQueue.Bytes32Deque private _governanceCall;
|
|
|
+
|
|
|
/**
|
|
|
- * @dev Restrict access of functions to the governance executor, which may be the Governor itself or a timelock
|
|
|
- * contract, as specified by {_executor}. This generally means that function with this modifier must be voted on and
|
|
|
- * executed through the governance protocol.
|
|
|
+ * @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() {
|
|
|
require(_msgSender() == _executor(), "Governor: onlyGovernance");
|
|
|
+ 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) {}
|
|
|
+ }
|
|
|
_;
|
|
|
}
|
|
|
|
|
@@ -68,7 +90,17 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
* @dev See {IERC165-supportsInterface}.
|
|
|
*/
|
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
|
|
|
- return interfaceId == type(IGovernor).interfaceId || super.supportsInterface(interfaceId);
|
|
|
+ // In addition to the current interfaceId, also support previous version of the interfaceId that did not
|
|
|
+ // include the castVoteWithReasonAndParams() function as standard
|
|
|
+ return
|
|
|
+ interfaceId ==
|
|
|
+ (type(IGovernor).interfaceId ^
|
|
|
+ this.castVoteWithReasonAndParams.selector ^
|
|
|
+ this.castVoteWithReasonAndParamsBySig.selector ^
|
|
|
+ this.getVotesWithParams.selector) ||
|
|
|
+ interfaceId == type(IGovernor).interfaceId ||
|
|
|
+ interfaceId == type(IERC1155Receiver).interfaceId ||
|
|
|
+ super.supportsInterface(interfaceId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -95,7 +127,7 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
*
|
|
|
* 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
|
|
|
- * accross multiple networks. This also means that in order to execute the same operation twice (on the same
|
|
|
+ * 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(
|
|
@@ -175,6 +207,15 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
*/
|
|
|
function _voteSucceeded(uint256 proposalId) internal view virtual returns (bool);
|
|
|
|
|
|
+ /**
|
|
|
+ * @dev Get the voting weight of `account` at a specific `blockNumber`, for a vote as described by `params`.
|
|
|
+ */
|
|
|
+ function _getVotes(
|
|
|
+ address account,
|
|
|
+ uint256 blockNumber,
|
|
|
+ bytes memory params
|
|
|
+ ) internal view virtual returns (uint256);
|
|
|
+
|
|
|
/**
|
|
|
* @dev Register a vote with a given support and voting weight.
|
|
|
*
|
|
@@ -184,9 +225,20 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
uint256 proposalId,
|
|
|
address account,
|
|
|
uint8 support,
|
|
|
- uint256 weight
|
|
|
+ uint256 weight,
|
|
|
+ bytes memory params
|
|
|
) 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}.
|
|
|
*/
|
|
@@ -197,8 +249,8 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
string memory description
|
|
|
) public virtual override returns (uint256) {
|
|
|
require(
|
|
|
- getVotes(msg.sender, block.number - 1) >= proposalThreshold(),
|
|
|
- "GovernorCompatibilityBravo: proposer votes below proposal threshold"
|
|
|
+ getVotes(_msgSender(), block.number - 1) >= proposalThreshold(),
|
|
|
+ "Governor: proposer votes below proposal threshold"
|
|
|
);
|
|
|
|
|
|
uint256 proposalId = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
|
|
@@ -251,13 +303,15 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
|
|
|
emit ProposalExecuted(proposalId);
|
|
|
|
|
|
+ _beforeExecute(proposalId, targets, values, calldatas, descriptionHash);
|
|
|
_execute(proposalId, targets, values, calldatas, descriptionHash);
|
|
|
+ _afterExecute(proposalId, targets, values, calldatas, descriptionHash);
|
|
|
|
|
|
return proposalId;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @dev Internal execution mechanism. Can be overriden to implement different execution mechanism
|
|
|
+ * @dev Internal execution mechanism. Can be overridden to implement different execution mechanism
|
|
|
*/
|
|
|
function _execute(
|
|
|
uint256, /* proposalId */
|
|
@@ -273,6 +327,42 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @dev Hook before execution is triggered.
|
|
|
+ */
|
|
|
+ function _beforeExecute(
|
|
|
+ uint256, /* proposalId */
|
|
|
+ address[] memory targets,
|
|
|
+ uint256[] memory, /* values */
|
|
|
+ bytes[] memory calldatas,
|
|
|
+ bytes32 /*descriptionHash*/
|
|
|
+ ) internal virtual {
|
|
|
+ if (_executor() != address(this)) {
|
|
|
+ for (uint256 i = 0; i < targets.length; ++i) {
|
|
|
+ if (targets[i] == address(this)) {
|
|
|
+ _governanceCall.pushBack(keccak256(calldatas[i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev Hook after execution is triggered.
|
|
|
+ */
|
|
|
+ function _afterExecute(
|
|
|
+ uint256, /* proposalId */
|
|
|
+ address[] memory, /* targets */
|
|
|
+ uint256[] memory, /* values */
|
|
|
+ bytes[] memory, /* calldatas */
|
|
|
+ bytes32 /*descriptionHash*/
|
|
|
+ ) internal virtual {
|
|
|
+ if (_executor() != address(this)) {
|
|
|
+ if (!_governanceCall.empty()) {
|
|
|
+ _governanceCall.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @dev Internal cancel mechanism: locks up the proposal timer, preventing it from being re-submitted. Marks it as
|
|
|
* canceled to allow distinguishing it from executed proposals.
|
|
@@ -299,6 +389,24 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
return proposalId;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @dev See {IGovernor-getVotes}.
|
|
|
+ */
|
|
|
+ function getVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
|
|
|
+ return _getVotes(account, blockNumber, _defaultParams());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev See {IGovernor-getVotesWithParams}.
|
|
|
+ */
|
|
|
+ function getVotesWithParams(
|
|
|
+ address account,
|
|
|
+ uint256 blockNumber,
|
|
|
+ bytes memory params
|
|
|
+ ) public view virtual override returns (uint256) {
|
|
|
+ return _getVotes(account, blockNumber, params);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @dev See {IGovernor-castVote}.
|
|
|
*/
|
|
@@ -319,6 +427,19 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
return _castVote(proposalId, voter, support, reason);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @dev See {IGovernor-castVoteWithReasonAndParams}.
|
|
|
+ */
|
|
|
+ function castVoteWithReasonAndParams(
|
|
|
+ uint256 proposalId,
|
|
|
+ uint8 support,
|
|
|
+ string calldata reason,
|
|
|
+ bytes memory params
|
|
|
+ ) public virtual override returns (uint256) {
|
|
|
+ address voter = _msgSender();
|
|
|
+ return _castVote(proposalId, voter, support, reason, params);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @dev See {IGovernor-castVoteBySig}.
|
|
|
*/
|
|
@@ -338,9 +459,41 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
return _castVote(proposalId, voter, support, "");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @dev See {IGovernor-castVoteWithReasonAndParamsBySig}.
|
|
|
+ */
|
|
|
+ function castVoteWithReasonAndParamsBySig(
|
|
|
+ uint256 proposalId,
|
|
|
+ uint8 support,
|
|
|
+ string calldata reason,
|
|
|
+ bytes memory params,
|
|
|
+ uint8 v,
|
|
|
+ bytes32 r,
|
|
|
+ bytes32 s
|
|
|
+ ) public virtual override returns (uint256) {
|
|
|
+ address voter = ECDSA.recover(
|
|
|
+ _hashTypedDataV4(
|
|
|
+ keccak256(
|
|
|
+ abi.encode(
|
|
|
+ EXTENDED_BALLOT_TYPEHASH,
|
|
|
+ proposalId,
|
|
|
+ support,
|
|
|
+ keccak256(bytes(reason)),
|
|
|
+ keccak256(params)
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ),
|
|
|
+ v,
|
|
|
+ r,
|
|
|
+ s
|
|
|
+ );
|
|
|
+
|
|
|
+ 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.
|
|
|
+ * voting weight using {IGovernor-getVotes} and call the {_countVote} internal function. Uses the _defaultParams().
|
|
|
*
|
|
|
* Emits a {IGovernor-VoteCast} event.
|
|
|
*/
|
|
@@ -349,14 +502,34 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
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) {
|
|
|
ProposalCore storage proposal = _proposals[proposalId];
|
|
|
require(state(proposalId) == ProposalState.Active, "Governor: vote not currently active");
|
|
|
|
|
|
- uint256 weight = getVotes(account, proposal.voteStart.getDeadline());
|
|
|
- _countVote(proposalId, account, support, weight);
|
|
|
+ uint256 weight = _getVotes(account, proposal.voteStart.getDeadline(), params);
|
|
|
+ _countVote(proposalId, account, support, weight, params);
|
|
|
|
|
|
- emit VoteCast(account, proposalId, support, weight, reason);
|
|
|
+ if (params.length == 0) {
|
|
|
+ emit VoteCast(account, proposalId, support, weight, reason);
|
|
|
+ } else {
|
|
|
+ emit VoteCastWithParams(account, proposalId, support, weight, reason, params);
|
|
|
+ }
|
|
|
|
|
|
return weight;
|
|
|
}
|
|
@@ -382,4 +555,42 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
|
|
|
function _executor() internal view virtual returns (address) {
|
|
|
return address(this);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev See {IERC721Receiver-onERC721Received}.
|
|
|
+ */
|
|
|
+ function onERC721Received(
|
|
|
+ address,
|
|
|
+ address,
|
|
|
+ uint256,
|
|
|
+ bytes memory
|
|
|
+ ) public virtual override returns (bytes4) {
|
|
|
+ return this.onERC721Received.selector;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev See {IERC1155Receiver-onERC1155Received}.
|
|
|
+ */
|
|
|
+ function onERC1155Received(
|
|
|
+ address,
|
|
|
+ address,
|
|
|
+ uint256,
|
|
|
+ uint256,
|
|
|
+ bytes memory
|
|
|
+ ) public virtual override returns (bytes4) {
|
|
|
+ return this.onERC1155Received.selector;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev See {IERC1155Receiver-onERC1155BatchReceived}.
|
|
|
+ */
|
|
|
+ function onERC1155BatchReceived(
|
|
|
+ address,
|
|
|
+ address,
|
|
|
+ uint256[] memory,
|
|
|
+ uint256[] memory,
|
|
|
+ bytes memory
|
|
|
+ ) public virtual override returns (bytes4) {
|
|
|
+ return this.onERC1155BatchReceived.selector;
|
|
|
+ }
|
|
|
}
|