Skip to content

Commit

Permalink
feat: remove methods with support for additional params
Browse files Browse the repository at this point in the history
0.8% of the limit imporve in deployment cost
#11
  • Loading branch information
DuBento committed Jul 30, 2023
1 parent 2114a48 commit 74cbef7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 139 deletions.
100 changes: 6 additions & 94 deletions blockchain/contracts/DAO/governance/Governor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,10 @@ abstract contract Governor is
bytes4 governorCancelId = this.cancel.selector ^
this.proposalProposer.selector;

bytes4 governorParamsId = this.castVoteWithReasonAndParams.selector ^
this.castVoteWithReasonAndParamsBySig.selector;

// The original interface id in v4.3.
bytes4 governor43Id = type(IGovernor).interfaceId ^
type(IERC6372).interfaceId ^
governorCancelId ^
governorParamsId;
governorCancelId;

// An updated interface id in v4.6, with params added.
bytes4 governor46Id = type(IGovernor).interfaceId ^
Expand Down Expand Up @@ -252,28 +248,17 @@ abstract contract Governor is
function _getVotes(address account) internal view virtual returns (uint8);

/**
* @dev Register a vote for `proposalId` by `account` with a given `support`, voting `weight` and voting `params`.
* @dev Register a vote for `proposalId` by `account` with a given `support` and voting `weight``.
*
* Note: Support is generic and can represent various things depending on the voting system used.
*/
function _countVote(
uint256 proposalId,
address account,
uint8 support,
uint256 weight,
bytes memory params
uint256 weight
) 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}.
*/
Expand Down Expand Up @@ -466,19 +451,6 @@ abstract contract Governor is
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 = msg.sender;
return _castVote(proposalId, voter, support, reason, params);
}

/**
* @dev See {IGovernor-castVoteBySig}.
*/
Expand All @@ -500,54 +472,6 @@ abstract contract Governor is
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. 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.
Expand All @@ -558,8 +482,7 @@ abstract contract Governor is
uint256 proposalId,
address account,
uint8 support,
string memory reason,
bytes memory params
string memory reason
) internal virtual returns (uint256) {
ProposalState currentState = state(proposalId);
if (currentState != ProposalState.Active) {
Expand All @@ -571,20 +494,9 @@ abstract contract Governor is
}

uint256 weight = _getVotes(account);
_countVote(proposalId, account, support, weight, params);
_countVote(proposalId, account, support, weight);

if (params.length == 0) {
emit VoteCast(account, proposalId, support, weight, reason);
} else {
emit VoteCastWithParams(
account,
proposalId,
support,
weight,
reason,
params
);
}
emit VoteCast(account, proposalId, support, weight, reason);

return weight;
}
Expand Down
44 changes: 1 addition & 43 deletions blockchain/contracts/DAO/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ abstract contract IGovernor is IERC165, IERC6372 {
event ProposalExecuted(uint256 proposalId);

/**
* @dev Emitted when a vote is cast without params.
* @dev Emitted when a vote.
*
* Note: `support` values should be seen as buckets. Their interpretation depends on the voting module used.
*/
Expand All @@ -124,21 +124,6 @@ abstract contract IGovernor is IERC165, IERC6372 {
string reason
);

/**
* @dev Emitted when a vote is cast with params.
*
* Note: `support` values should be seen as buckets. Their interpretation depends on the voting module used.
* `params` are additional encoded parameters. Their interpepretation also depends on the voting module used.
*/
event VoteCastWithParams(
address indexed voter,
uint256 proposalId,
uint8 support,
uint256 weight,
string reason,
bytes params
);

/**
* @notice module:core
* @dev Name of the governor instance (used in building the ERC712 domain separator).
Expand Down Expand Up @@ -338,18 +323,6 @@ abstract contract IGovernor is IERC165, IERC6372 {
string calldata reason
) public virtual returns (uint256 balance);

/**
* @dev Cast a vote with a reason and additional encoded parameters
*
* Emits a {VoteCast} or {VoteCastWithParams} event depending on the length of params.
*/
function castVoteWithReasonAndParams(
uint256 proposalId,
uint8 support,
string calldata reason,
bytes memory params
) public virtual returns (uint256 balance);

/**
* @dev Cast a vote using the user's cryptographic signature.
*
Expand All @@ -362,19 +335,4 @@ abstract contract IGovernor is IERC165, IERC6372 {
bytes32 r,
bytes32 s
) public virtual returns (uint256 balance);

/**
* @dev Cast a vote with a reason and additional encoded parameters using the user's cryptographic signature.
*
* Emits a {VoteCast} or {VoteCastWithParams} event depending on the length of params.
*/
function castVoteWithReasonAndParamsBySig(
uint256 proposalId,
uint8 support,
string calldata reason,
bytes memory params,
uint8 v,
bytes32 r,
bytes32 s
) public virtual returns (uint256 balance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ abstract contract GovernorCountingSimple is Governor {
uint256 proposalId,
address account,
uint8 support,
uint256 weight,
bytes memory // params
uint256 weight
) internal virtual override {
ProposalVote storage proposalVote = _proposalVotes[proposalId];

Expand Down

0 comments on commit 74cbef7

Please sign in to comment.