generated from ScopeLift/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
L2CountingFractional.sol
205 lines (182 loc) · 7.55 KB
/
L2CountingFractional.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {Governor} from "@openzeppelin/contracts/governance/Governor.sol";
import {GovernorCompatibilityBravo} from
"@openzeppelin/contracts/governance/compatibility/GovernorCompatibilityBravo.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
abstract contract L2CountingFractional {
struct ProposalVote {
uint128 againstVotes;
uint128 forVotes;
uint128 abstainVotes;
}
/**
* @dev Mapping from proposal ID to vote tallies for that proposal.
*/
mapping(uint256 => ProposalVote) internal _proposalVotes;
/**
* @dev Mapping from proposal ID and address to the weight the address
* has cast on that proposal, e.g. _proposalVotersWeightCast[42][0xBEEF]
* would tell you the number of votes that 0xBEEF has cast on proposal 42.
*/
// Made both of these internal
mapping(uint256 => mapping(address => uint128)) internal _proposalVotersWeightCast;
/**
* @dev See {IGovernor-COUNTING_MODE}.
*/
// solhint-disable-next-line func-name-mixedcase
function COUNTING_MODE() public pure virtual returns (string memory) {
return "support=bravo&quorum=for,abstain¶ms=fractional";
}
/**
* @dev See {IGovernor-hasVoted}.
*/
function hasVoted(uint256 proposalId, address account) public view virtual returns (bool) {
return _proposalVotersWeightCast[proposalId][account] > 0;
}
/**
* @dev Get the number of votes cast thus far on proposal `proposalId` by
* account `account`. Useful for integrations that allow delegates to cast
* rolling, partial votes.
*/
function voteWeightCast(uint256 proposalId, address account) public view returns (uint128) {
return _proposalVotersWeightCast[proposalId][account];
}
/**
* @dev Accessor to the internal vote counts.
*/
function proposalVotes(uint256 proposalId)
public
view
virtual
returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes)
{
ProposalVote storage proposalVote = _proposalVotes[proposalId];
return (proposalVote.againstVotes, proposalVote.forVotes, proposalVote.abstainVotes);
}
/**
* @notice See {Governor-_countVote}.
*
* @dev Function that records the delegate's votes.
*
* If the `voteData` bytes parameter is empty, then this module behaves
* identically to GovernorBravo. That is, it assigns the full weight of the
* delegate to the `support` parameter, which follows the `VoteType` enum
* from Governor Bravo.
*
* If the `voteData` bytes parameter is not zero, then it _must_ be three
* packed uint128s, totaling 48 bytes, representing the weight the delegate
* assigns to Against, For, and Abstain respectively, i.e.
* `abi.encodePacked(againstVotes, forVotes, abstainVotes)`. The sum total of
* the three decoded vote weights _must_ be less than or equal to the
* delegate's remaining weight on the proposal, i.e. their checkpointed
* total weight minus votes already cast on the proposal.
*
* See `_countVoteNominal` and `_countVoteFractional` for more details.
*/
function _countVote(
uint256 proposalId,
address account,
uint8 support,
uint256 totalWeight,
bytes memory voteData
) internal virtual {
require(totalWeight > 0, "L2CountingFractional: no weight");
if (_proposalVotersWeightCast[proposalId][account] >= totalWeight) {
revert("L2CountingFractional: all weight cast");
}
uint128 safeTotalWeight = SafeCast.toUint128(totalWeight);
if (voteData.length == 0) _countVoteNominal(proposalId, account, safeTotalWeight, support);
else _countVoteFractional(proposalId, account, safeTotalWeight, voteData);
}
/**
* @dev Record votes with full weight cast for `support`.
*
* Because this function votes with the delegate's full weight, it can only
* be called once per proposal. It will revert if combined with a fractional
* vote before or after.
*/
function _countVoteNominal(
uint256 proposalId,
address account,
uint128 totalWeight,
uint8 support
) internal {
require(
_proposalVotersWeightCast[proposalId][account] == 0,
"L2CountingFractional: vote would exceed weight"
);
_proposalVotersWeightCast[proposalId][account] = totalWeight;
if (support == uint8(GovernorCompatibilityBravo.VoteType.Against)) {
_proposalVotes[proposalId].againstVotes += totalWeight;
} else if (support == uint8(GovernorCompatibilityBravo.VoteType.For)) {
_proposalVotes[proposalId].forVotes += totalWeight;
} else if (support == uint8(GovernorCompatibilityBravo.VoteType.Abstain)) {
_proposalVotes[proposalId].abstainVotes += totalWeight;
} else {
revert("L2CountingFractional: invalid support value, must be included in VoteType enum");
}
}
/**
* @dev Count votes with fractional weight.
*
* `voteData` is expected to be three packed uint128s, i.e.
* `abi.encodePacked(againstVotes, forVotes, abstainVotes)`.
*
* This function can be called multiple times for the same account and
* proposal, i.e. partial/rolling votes are allowed. For example, an account
* with total weight of 10 could call this function three times with the
* following vote data:
* - against: 1, for: 0, abstain: 2
* - against: 3, for: 1, abstain: 0
* - against: 1, for: 1, abstain: 1
* The result of these three calls would be that the account casts 5 votes
* AGAINST, 2 votes FOR, and 3 votes ABSTAIN on the proposal. Though
* partial, votes are still final once cast and cannot be changed or
* overridden. Subsequent partial votes simply increment existing totals.
*
* Note that if partial votes are cast, all remaining weight must be cast
* with _countVoteFractional: _countVoteNominal will revert.
*/
function _countVoteFractional(
uint256 proposalId,
address account,
uint128 totalWeight,
bytes memory voteData
) internal {
require(voteData.length == 48, "L2CountingFractional: invalid voteData");
(uint128 _againstVotes, uint128 _forVotes, uint128 _abstainVotes) = _decodePackedVotes(voteData);
uint128 _existingWeight = _proposalVotersWeightCast[proposalId][account];
uint256 _newWeight = uint256(_againstVotes) + _forVotes + _abstainVotes + _existingWeight;
require(_newWeight <= totalWeight, "L2CountingFractional: vote would exceed weight");
// It's safe to downcast here because we've just confirmed that
// _newWeight <= totalWeight, and totalWeight is a uint128.
_proposalVotersWeightCast[proposalId][account] = uint128(_newWeight);
ProposalVote memory _proposalVote = _proposalVotes[proposalId];
_proposalVote = ProposalVote(
_proposalVote.againstVotes + _againstVotes,
_proposalVote.forVotes + _forVotes,
_proposalVote.abstainVotes + _abstainVotes
);
_proposalVotes[proposalId] = _proposalVote;
}
uint256 internal constant _MASK_HALF_WORD_RIGHT = 0xffffffffffffffffffffffffffffffff; // 128 bits
// of 0's, 128 bits of 1's
/**
* @dev Decodes three packed uint128's. Uses assembly because of a Solidity
* language limitation which prevents slicing bytes stored in memory, rather
* than calldata.
*/
function _decodePackedVotes(bytes memory voteData)
internal
pure
returns (uint128 againstVotes, uint128 forVotes, uint128 abstainVotes)
{
assembly {
againstVotes := shr(128, mload(add(voteData, 0x20)))
forVotes := and(_MASK_HALF_WORD_RIGHT, mload(add(voteData, 0x20)))
abstainVotes := shr(128, mload(add(voteData, 0x40)))
}
}
}