Skip to content
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

Delegate override vote #5192

Merged
merged 31 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9ad82b2
Initial commit
arr00 Aug 12, 2024
e5dc350
use only one extension. add counting
arr00 Aug 14, 2024
ed3da11
Update docs
arr00 Aug 18, 2024
4bc3b1d
Use existing castVoteWithReasonAndParams for override
arr00 Aug 18, 2024
c6a048b
fix: override per proposalId instead of timepoint
arr00 Aug 19, 2024
1e6e530
update comments
arr00 Aug 20, 2024
d7550a4
Use hardhat testing
arr00 Aug 21, 2024
5cdcb44
Inherit `GovernorVotes` to avoid code duplication
arr00 Aug 21, 2024
45cb330
suggestions
arr00 Aug 22, 2024
ff25d10
Iterate following discussion with Tally
Amxx Sep 9, 2024
dfc7a17
up
Amxx Sep 9, 2024
e5f4f53
update + more testing
Amxx Sep 9, 2024
2510af2
fix tests
Amxx Sep 9, 2024
9ecbb13
rename VoteCast event to OverrideVoteCast on override vote
Oct 10, 2024
74aff19
use `_tallyUpdated` when vote tally is updated
arr00 Oct 10, 2024
24bf708
rename contracts
arr00 Oct 14, 2024
428723c
fix lint
arr00 Oct 14, 2024
d2e815a
remove `.only`
arr00 Oct 14, 2024
ae5e042
suggestion
arr00 Oct 14, 2024
fe3ea67
fix tests
arr00 Oct 14, 2024
90f4177
test override vote by sig
arr00 Oct 14, 2024
2acf074
add docs
arr00 Oct 14, 2024
3d30f11
Merge branch 'master' into feat/delegate-override-vote
arr00 Oct 14, 2024
e6c1f01
additional tests
arr00 Oct 15, 2024
f0b4aa1
add changesets
arr00 Oct 15, 2024
dfcb745
Update README.adoc
Amxx Oct 16, 2024
14eb39b
refactor to minimize diff
Amxx Oct 16, 2024
614576e
Rename `GovernorAlreadyCastVoteOverride` to `GovernorAlreadyOverriden…
arr00 Oct 16, 2024
93c668d
Update .changeset/pink-wasps-hammer.md
arr00 Oct 16, 2024
99bfb73
Update contracts/governance/Governor.sol
arr00 Oct 16, 2024
ca6feb3
Rename `VotesAdditionalCheckpoints` to `VotesExtended`
arr00 Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
suggestions
  • Loading branch information
arr00 committed Aug 22, 2024
commit 45cb3302af886f7e99db823c4f2ad9ba651946ce
101 changes: 44 additions & 57 deletions contracts/governance/extensions/GovernorOverrideDelegateVote.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,24 @@ abstract contract GovernorOverrideDelegateVote is GovernorVotes {
}

struct VoteReceipt {
bool hasVoted;
uint8 support;
bool hasOverriden;
uint208 overrideWeight;
}

struct ProposalVote {
uint256 againstVotes;
uint256 forVotes;
uint256 abstainVotes;
mapping(address voter => VoteReceipt) voteReceipt;
mapping(address voter => VoteReceipt) overrideVoteReceipt;
}

error GovernorAlreadyCastVoteOverride(address account);

mapping(uint256 proposalId => ProposalVote) private _proposalVotes;
mapping(address account => mapping(uint256 proposalId => uint256 votes)) private _overrideVoteWeight;

constructor(VotesOverridable tokenAddress) GovernorVotes(tokenAddress) {}

/**
* @dev Fetch the past delegate for an `account` at a given `timepoint` from the token.
*/
function _getPastDelegate(address account, uint256 timepoint) internal view virtual returns (address) {
return VotesOverridable(address(token())).getPastDelegate(account, timepoint);
}

/**
* @dev Fetch the past `balanceOf` for an `account` at a given `timepoint` from the token.
*/
function _getPastBalanceOf(address account, uint256 timepoint) internal view virtual returns (uint256) {
return VotesOverridable(address(token())).getPastBalanceOf(account, timepoint);
}

/**
* @dev See {IGovernor-COUNTING_MODE}.
*/
Expand All @@ -69,14 +54,14 @@ abstract contract GovernorOverrideDelegateVote is GovernorVotes {
* @dev See {IGovernor-hasVoted}.
*/
function hasVoted(uint256 proposalId, address account) public view virtual override returns (bool) {
return _proposalVotes[proposalId].voteReceipt[account].hasVoted;
return _proposalVotes[proposalId].voteReceipt[account].support != 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].overrideVoteReceipt[account].hasVoted;
return _proposalVotes[proposalId].voteReceipt[account].hasOverriden;
}

/**
Expand Down Expand Up @@ -118,24 +103,17 @@ abstract contract GovernorOverrideDelegateVote is GovernorVotes {
return _countVotesOverride(proposalId, account, support, params);
}

totalWeight -= _overrideVoteWeight[account][proposalId];

ProposalVote storage proposalVote = _proposalVotes[proposalId];

if (proposalVote.voteReceipt[account].hasVoted) {
totalWeight -= proposalVote.voteReceipt[account].overrideWeight;

if (proposalVote.voteReceipt[account].support != 0) {
revert GovernorAlreadyCastVote(account);
}
proposalVote.voteReceipt[account] = VoteReceipt({hasVoted: true, support: support});
// Support tracks support and if a user voted. Store support + 1.
proposalVote.voteReceipt[account].support = support + 1;

if (support == uint8(VoteType.Against)) {
proposalVote.againstVotes += totalWeight;
} else if (support == uint8(VoteType.For)) {
proposalVote.forVotes += totalWeight;
} else if (support == uint8(VoteType.Abstain)) {
proposalVote.abstainVotes += totalWeight;
} else {
revert GovernorInvalidVoteType();
}
_tallyVote(proposalVote, support, _add, totalWeight);

return totalWeight;
}
Expand All @@ -148,47 +126,56 @@ abstract contract GovernorOverrideDelegateVote is GovernorVotes {
) private returns (uint256) {
ProposalVote storage proposalVote = _proposalVotes[proposalId];
uint256 proposalSnapshot = proposalSnapshot(proposalId);
address delegate = _getPastDelegate(account, proposalSnapshot);
address delegate = VotesOverridable(address(token())).getPastDelegate(account, proposalSnapshot);

if (proposalVote.overrideVoteReceipt[account].hasVoted) {
if (proposalVote.voteReceipt[account].hasOverriden) {
revert GovernorAlreadyCastVoteOverride(account);
}
proposalVote.voteReceipt[account].hasOverriden = true;

uint256 overrideWeight = _getPastBalanceOf(account, proposalSnapshot);

proposalVote.overrideVoteReceipt[account] = VoteReceipt({hasVoted: true, support: support});
if (support == uint8(VoteType.Against)) {
proposalVote.againstVotes += overrideWeight;
} else if (support == uint8(VoteType.For)) {
proposalVote.forVotes += overrideWeight;
} else if (support == uint8(VoteType.Abstain)) {
proposalVote.abstainVotes += overrideWeight;
} else {
revert GovernorInvalidVoteType();
}
uint256 overrideWeight = VotesOverridable(address(token())).getPastBalanceOf(account, proposalSnapshot);
_tallyVote(proposalVote, support, _add, overrideWeight);

// Account for the delegate's vote
VoteReceipt memory delegateVoteReceipt = proposalVote.voteReceipt[delegate];
if (delegateVoteReceipt.hasVoted) {
if (delegateVoteReceipt.support != 0) {
uint8 correctedSupport = delegateVoteReceipt.support - 1;
// If delegate has voted, remove the delegatee's vote weight from their support
if (delegateVoteReceipt.support == uint8(VoteType.Against)) {
proposalVote.againstVotes -= overrideWeight;
} else if (delegateVoteReceipt.support == uint8(VoteType.For)) {
proposalVote.forVotes -= overrideWeight;
} else if (delegateVoteReceipt.support == uint8(VoteType.Abstain)) {
proposalVote.abstainVotes -= overrideWeight;
} else {
revert GovernorInvalidVoteType();
}
_tallyVote(proposalVote, correctedSupport, _subtract, overrideWeight);

// Write delegate into the params for event
assembly {
mstore(add(params, 0x20), or(mload(add(params, 0x20)), shl(64, delegate)))
}
} else {
// Only write override weight if they have not voted yet
_overrideVoteWeight[delegate][proposalId] += overrideWeight;
proposalVote.voteReceipt[delegate].overrideWeight += uint208(overrideWeight);
}
return overrideWeight;
}

function _tallyVote(
ProposalVote storage proposalVote,
uint8 support,
function(uint256, uint256) view returns (uint256) op,
uint256 delta
) private {
if (support == uint8(VoteType.Against)) {
proposalVote.againstVotes = op(proposalVote.againstVotes, delta);
} else if (support == uint8(VoteType.For)) {
proposalVote.forVotes = op(proposalVote.forVotes, delta);
} else if (support == uint8(VoteType.Abstain)) {
proposalVote.abstainVotes = op(proposalVote.abstainVotes, delta);
} else {
revert GovernorInvalidVoteType();
}
}

function _add(uint256 a, uint256 b) private pure returns (uint256) {
return a + b;
}

function _subtract(uint256 a, uint256 b) private pure returns (uint256) {
return a - b;
}
}
16 changes: 3 additions & 13 deletions contracts/governance/utils/VotesOverridable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ abstract contract VotesOverridable is Votes {
mapping(address account => Checkpoints.Trace208) private _balanceOfCheckpoints;

function _delegate(address account, address delegatee) internal virtual override {
address oldDelegate = delegates(account);
super._delegate(account, delegatee);

_delegateCheckpoints[account].push(clock(), uint160(delegatee));

emit DelegateChanged(account, oldDelegate, delegatee);
_moveDelegateVotes(oldDelegate, delegatee, _getVotingUnits(account));
}

/**
* @inheritdoc Votes
*/
function delegates(address delegatee) public view virtual override returns (address) {
return address(uint160(_delegateCheckpoints[delegatee].latest()));
}

/**
Expand All @@ -57,11 +47,11 @@ abstract contract VotesOverridable is Votes {
if (from != to) {
if (from != address(0)) {
Checkpoints.Trace208 storage store = _balanceOfCheckpoints[from];
store.push(clock(), uint208(store.latest() - amount));
store.push(clock(), uint208(_getVotingUnits(from)));
}
if (to != address(0)) {
Checkpoints.Trace208 storage store = _balanceOfCheckpoints[to];
store.push(clock(), uint208(store.latest() + amount));
store.push(clock(), uint208(_getVotingUnits(to)));
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions contracts/mocks/token/ERC20VotesOverridableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ abstract contract ERC20VotesOverridableMock is ERC20Votes, VotesOverridable {
) internal virtual override(Votes, VotesOverridable) {
return super._transferVotingUnits(from, to, amount);
}

function delegates(address delegatee) public view virtual override(Votes, VotesOverridable) returns (address) {
return super.delegates(delegatee);
}
}

abstract contract ERC20VotesOverridableTimestampMock is ERC20VotesOverridableMock {
Expand Down
Loading