-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Delegate override vote #5192
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
Merged
Merged
Delegate override vote #5192
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9ad82b2
Initial commit
arr00 e5dc350
use only one extension. add counting
arr00 ed3da11
Update docs
arr00 4bc3b1d
Use existing castVoteWithReasonAndParams for override
arr00 c6a048b
fix: override per proposalId instead of timepoint
arr00 1e6e530
update comments
arr00 d7550a4
Use hardhat testing
arr00 5cdcb44
Inherit `GovernorVotes` to avoid code duplication
arr00 45cb330
suggestions
arr00 ff25d10
Iterate following discussion with Tally
Amxx dfc7a17
up
Amxx e5f4f53
update + more testing
Amxx 2510af2
fix tests
Amxx 9ecbb13
rename VoteCast event to OverrideVoteCast on override vote
74aff19
use `_tallyUpdated` when vote tally is updated
arr00 24bf708
rename contracts
arr00 428723c
fix lint
arr00 d2e815a
remove `.only`
arr00 ae5e042
suggestion
arr00 fe3ea67
fix tests
arr00 90f4177
test override vote by sig
arr00 2acf074
add docs
arr00 3d30f11
Merge branch 'master' into feat/delegate-override-vote
arr00 e6c1f01
additional tests
arr00 f0b4aa1
add changesets
arr00 dfcb745
Update README.adoc
Amxx 14eb39b
refactor to minimize diff
Amxx 614576e
Rename `GovernorAlreadyCastVoteOverride` to `GovernorAlreadyOverriden…
arr00 93c668d
Update .changeset/pink-wasps-hammer.md
arr00 99bfb73
Update contracts/governance/Governor.sol
arr00 ca6feb3
Rename `VotesAdditionalCheckpoints` to `VotesExtended`
arr00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
209 changes: 209 additions & 0 deletions
209
contracts/governance/extensions/GovernorOverrideDelegateVote.sol
This file contains hidden or 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,209 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {SignatureChecker} from "../../utils/cryptography/SignatureChecker.sol"; | ||
import {SafeCast} from "../../utils/math/SafeCast.sol"; | ||
import {VotesOverridable} from "../utils/VotesOverridable.sol"; | ||
import {GovernorVotes} from "./GovernorVotes.sol"; | ||
|
||
/** | ||
* @dev Extension of {Governor} which enables delegatees to override the vote of their delegates. This module requires a | ||
* token token that inherits `VotesOverridable`. | ||
*/ | ||
abstract contract GovernorOverrideDelegateVote is GovernorVotes { | ||
bytes32 public constant OVERRIDE_BALLOT_TYPEHASH = | ||
keccak256("OverrideBallot(uint256 proposalId,uint8 support,address voter,uint256 nonce,string reason)"); | ||
|
||
/** | ||
* @dev Supported vote types. Matches Governor Bravo ordering. | ||
*/ | ||
enum VoteType { | ||
Against, | ||
For, | ||
Abstain | ||
} | ||
|
||
struct VoteReceipt { | ||
uint8 casted; // 0 if vote was not casted. Otherwise: support + 1 | ||
bool hasOverriden; | ||
uint208 overridenWeight; | ||
} | ||
|
||
struct ProposalVote { | ||
uint256[3] votes; | ||
mapping(address voter => VoteReceipt) voteReceipt; | ||
} | ||
|
||
event VoteReduced(address indexed voter, uint256 proposalId, uint8 support, uint256 weight); | ||
|
||
error GovernorAlreadyCastVoteOverride(address account); | ||
|
||
mapping(uint256 proposalId => ProposalVote) private _proposalVotes; | ||
|
||
/** | ||
* @dev See {IGovernor-COUNTING_MODE}. | ||
*/ | ||
// solhint-disable-next-line func-name-mixedcase | ||
function COUNTING_MODE() public pure virtual override returns (string memory) { | ||
return "support=bravo,override&quorum=for,abstain&overridable=true"; | ||
} | ||
|
||
/** | ||
* @dev See {IGovernor-hasVoted}. | ||
*/ | ||
function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) { | ||
return _proposalVotes[proposalId].voteReceipt[account].casted != 0; | ||
} | ||
|
||
/** | ||
* @dev Check if an `account` has overridden their delegate for a proposal. | ||
*/ | ||
function hasVotedOverride(uint256 proposalId, address account) public view virtual returns (bool) { | ||
return _proposalVotes[proposalId].voteReceipt[account].hasOverriden; | ||
} | ||
|
||
/** | ||
* @dev Accessor to the internal vote counts. | ||
*/ | ||
function proposalVotes( | ||
uint256 proposalId | ||
) public view virtual returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes) { | ||
uint256[3] storage votes = _proposalVotes[proposalId].votes; | ||
return (votes[uint8(VoteType.Against)], votes[uint8(VoteType.For)], votes[uint8(VoteType.Abstain)]); | ||
} | ||
|
||
/** | ||
* @dev See {Governor-_quorumReached}. | ||
*/ | ||
function _quorumReached(uint256 proposalId) internal view virtual override returns (bool) { | ||
uint256[3] storage votes = _proposalVotes[proposalId].votes; | ||
return quorum(proposalSnapshot(proposalId)) <= votes[uint8(VoteType.For)] + votes[uint8(VoteType.Abstain)]; | ||
} | ||
|
||
/** | ||
* @dev See {Governor-_voteSucceeded}. In this module, the forVotes must be strictly over the againstVotes. | ||
*/ | ||
function _voteSucceeded(uint256 proposalId) internal view virtual override returns (bool) { | ||
uint256[3] storage votes = _proposalVotes[proposalId].votes; | ||
return votes[uint8(VoteType.For)] > votes[uint8(VoteType.Against)]; | ||
} | ||
|
||
/** | ||
* @dev See {Governor-_countVote}. In this module, the support follows the `VoteType` enum (from Governor Bravo). | ||
* | ||
* NOTE: called by {Governor-_castVote} which emits the {IGovernor-VoteCast} (or {IGovernor-VoteCastWithParams}) | ||
* event. | ||
*/ | ||
function _countVote( | ||
uint256 proposalId, | ||
address account, | ||
uint8 support, | ||
uint256 totalWeight, | ||
bytes memory /*params*/ | ||
) internal virtual override returns (uint256) { | ||
ProposalVote storage proposalVote = _proposalVotes[proposalId]; | ||
|
||
if (support > uint8(VoteType.Abstain)) { | ||
revert GovernorInvalidVoteType(); | ||
} | ||
|
||
if (proposalVote.voteReceipt[account].casted != 0) { | ||
revert GovernorAlreadyCastVote(account); | ||
} | ||
|
||
totalWeight -= proposalVote.voteReceipt[account].overridenWeight; | ||
proposalVote.votes[support] += totalWeight; | ||
proposalVote.voteReceipt[account].casted = support + 1; | ||
|
||
return totalWeight; | ||
} | ||
|
||
/// @dev Variant of {Governor-_countVote} that deals with vote overrides. | ||
function _countOverride(uint256 proposalId, address account, uint8 support) internal virtual returns (uint256) { | ||
ProposalVote storage proposalVote = _proposalVotes[proposalId]; | ||
|
||
if (support > uint8(VoteType.Abstain)) { | ||
revert GovernorInvalidVoteType(); | ||
} | ||
|
||
if (proposalVote.voteReceipt[account].hasOverriden) { | ||
revert GovernorAlreadyCastVoteOverride(account); | ||
} | ||
|
||
uint256 proposalSnapshot = proposalSnapshot(proposalId); | ||
uint256 overridenWeight = VotesOverridable(address(token())).getPastBalanceOf(account, proposalSnapshot); | ||
address delegate = VotesOverridable(address(token())).getPastDelegate(account, proposalSnapshot); | ||
uint8 delegateCasted = proposalVote.voteReceipt[delegate].casted; | ||
|
||
proposalVote.voteReceipt[account].hasOverriden = true; | ||
proposalVote.votes[support] += overridenWeight; | ||
if (delegateCasted == 0) { | ||
proposalVote.voteReceipt[delegate].overridenWeight += SafeCast.toUint208(overridenWeight); | ||
} else { | ||
uint8 delegateSupport = delegateCasted - 1; | ||
proposalVote.votes[delegateSupport] -= overridenWeight; | ||
emit VoteReduced(delegate, proposalId, delegateSupport, overridenWeight); | ||
} | ||
|
||
return overridenWeight; | ||
} | ||
|
||
/// @dev variant of {Governor-_castVote} that deals with vote overrides. | ||
function _castOverride( | ||
uint256 proposalId, | ||
address account, | ||
uint8 support, | ||
string calldata reason | ||
) internal virtual returns (uint256) { | ||
_validateStateBitmap(proposalId, _encodeStateBitmap(ProposalState.Active)); | ||
|
||
uint256 overridenWeight = _countOverride(proposalId, account, support); | ||
|
||
emit VoteCast(account, proposalId, support, overridenWeight, reason); | ||
|
||
return overridenWeight; | ||
} | ||
|
||
/// @dev Public function for casting an override vote | ||
function castOverrideVote( | ||
uint256 proposalId, | ||
uint8 support, | ||
string calldata reason | ||
) public virtual returns (uint256) { | ||
address voter = _msgSender(); | ||
return _castOverride(proposalId, voter, support, reason); | ||
} | ||
|
||
/// @dev Public function for casting an override vote using a voter's signature | ||
function castOverrideVoteBySig( | ||
uint256 proposalId, | ||
uint8 support, | ||
address voter, | ||
string calldata reason, | ||
bytes calldata signature | ||
) public virtual returns (uint256) { | ||
bool valid = SignatureChecker.isValidSignatureNow( | ||
voter, | ||
_hashTypedDataV4( | ||
keccak256( | ||
abi.encode( | ||
OVERRIDE_BALLOT_TYPEHASH, | ||
proposalId, | ||
support, | ||
voter, | ||
_useNonce(voter), | ||
keccak256(bytes(reason)) | ||
) | ||
) | ||
), | ||
signature | ||
); | ||
|
||
if (!valid) { | ||
revert GovernorInvalidSignature(voter); | ||
} | ||
|
||
return _castOverride(proposalId, voter, support, reason); | ||
} | ||
} |
This file contains hidden or 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,71 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {Checkpoints} from "../../utils/structs/Checkpoints.sol"; | ||
import {Votes} from "./Votes.sol"; | ||
import {SafeCast} from "../../utils/math/SafeCast.sol"; | ||
|
||
/** | ||
* @dev Extension of {Votes} that adds support for checkpointed delegations and balances. This is required | ||
* to use the `GovernorOverrideDelegateVote` extension. | ||
*/ | ||
abstract contract VotesOverridable is Votes { | ||
using SafeCast for uint256; | ||
using Checkpoints for Checkpoints.Trace160; | ||
using Checkpoints for Checkpoints.Trace208; | ||
|
||
mapping(address delegatee => Checkpoints.Trace160) private _delegateCheckpoints; | ||
mapping(address account => Checkpoints.Trace208) private _balanceOfCheckpoints; | ||
|
||
/** | ||
* @dev Returns the delegate of an `account` at a specific moment in the past. If the `clock()` is | ||
* configured to use block numbers, this will return the value at the end of the corresponding block. | ||
* | ||
* Requirements: | ||
* | ||
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined. | ||
*/ | ||
function getPastDelegate(address account, uint256 timepoint) public view virtual returns (address) { | ||
uint48 currentTimepoint = clock(); | ||
if (timepoint >= currentTimepoint) { | ||
revert ERC5805FutureLookup(timepoint, currentTimepoint); | ||
} | ||
return address(_delegateCheckpoints[account].upperLookupRecent(timepoint.toUint48())); | ||
} | ||
|
||
/** | ||
* @dev Returns the `balanceOf` of an `account` at a specific moment in the past. If the `clock()` is | ||
* configured to use block numbers, this will return the value at the end of the corresponding block. | ||
* | ||
* Requirements: | ||
* | ||
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined. | ||
*/ | ||
function getPastBalanceOf(address account, uint256 timepoint) public view virtual returns (uint256) { | ||
uint48 currentTimepoint = clock(); | ||
if (timepoint >= currentTimepoint) { | ||
revert ERC5805FutureLookup(timepoint, currentTimepoint); | ||
} | ||
return _balanceOfCheckpoints[account].upperLookupRecent(timepoint.toUint48()); | ||
} | ||
|
||
/// @inheritdoc Votes | ||
function _delegate(address account, address delegatee) internal virtual override { | ||
super._delegate(account, delegatee); | ||
|
||
_delegateCheckpoints[account].push(clock(), uint160(delegatee)); | ||
} | ||
|
||
/// @inheritdoc Votes | ||
function _transferVotingUnits(address from, address to, uint256 amount) internal virtual override { | ||
super._transferVotingUnits(from, to, amount); | ||
if (from != to) { | ||
if (from != address(0)) { | ||
_balanceOfCheckpoints[from].push(clock(), _getVotingUnits(from).toUint208()); | ||
} | ||
if (to != address(0)) { | ||
_balanceOfCheckpoints[to].push(clock(), _getVotingUnits(to).toUint208()); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {VotesOverridable} from "../governance/utils/VotesOverridable.sol"; | ||
|
||
abstract contract VotesOverridableMock is VotesOverridable { | ||
mapping(address voter => uint256) private _votingUnits; | ||
|
||
function getTotalSupply() public view returns (uint256) { | ||
return _getTotalSupply(); | ||
} | ||
|
||
function delegate(address account, address newDelegation) public { | ||
return _delegate(account, newDelegation); | ||
} | ||
|
||
function _getVotingUnits(address account) internal view override returns (uint256) { | ||
return _votingUnits[account]; | ||
} | ||
|
||
function _mint(address account, uint256 votes) internal { | ||
_votingUnits[account] += votes; | ||
_transferVotingUnits(address(0), account, votes); | ||
} | ||
|
||
function _burn(address account, uint256 votes) internal { | ||
_votingUnits[account] += votes; | ||
_transferVotingUnits(account, address(0), votes); | ||
} | ||
} | ||
|
||
abstract contract VotesOverridableTimestampMock is VotesOverridableMock { | ||
function clock() public view override returns (uint48) { | ||
return uint48(block.timestamp); | ||
} | ||
|
||
// solhint-disable-next-line func-name-mixedcase | ||
function CLOCK_MODE() public view virtual override returns (string memory) { | ||
return "mode=timestamp"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
contracts/mocks/governance/GovernorOverrideDelegateVoteMock.sol
This file contains hidden or 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,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Governor} from "../../governance/Governor.sol"; | ||
import {GovernorSettings} from "../../governance/extensions/GovernorSettings.sol"; | ||
import {GovernorVotesQuorumFraction} from "../../governance/extensions/GovernorVotesQuorumFraction.sol"; | ||
import {GovernorOverrideDelegateVote, VotesOverridable} from "../../governance/extensions/GovernorOverrideDelegateVote.sol"; | ||
import {GovernorVotesQuorumFraction} from "../../governance/extensions/GovernorVotesQuorumFraction.sol"; | ||
|
||
abstract contract GovernorOverrideDelegateVoteMock is | ||
GovernorSettings, | ||
GovernorVotesQuorumFraction, | ||
GovernorOverrideDelegateVote | ||
{ | ||
function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) { | ||
return super.proposalThreshold(); | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {ERC20Votes} from "../../token/ERC20/extensions/ERC20Votes.sol"; | ||
import {VotesOverridable, Votes} from "../../governance/utils/VotesOverridable.sol"; | ||
import {SafeCast} from "../../utils/math/SafeCast.sol"; | ||
|
||
abstract contract ERC20VotesOverridableMock is ERC20Votes, VotesOverridable { | ||
function _delegate(address account, address delegatee) internal virtual override(Votes, VotesOverridable) { | ||
return super._delegate(account, delegatee); | ||
} | ||
|
||
function _transferVotingUnits( | ||
address from, | ||
address to, | ||
uint256 amount | ||
) internal virtual override(Votes, VotesOverridable) { | ||
return super._transferVotingUnits(from, to, amount); | ||
} | ||
} | ||
|
||
abstract contract ERC20VotesOverridableTimestampMock is ERC20VotesOverridableMock { | ||
function clock() public view virtual override returns (uint48) { | ||
return SafeCast.toUint48(block.timestamp); | ||
} | ||
|
||
// solhint-disable-next-line func-name-mixedcase | ||
function CLOCK_MODE() public view virtual override returns (string memory) { | ||
return "mode=timestamp"; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.