-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCheckpoints.sol
274 lines (246 loc) · 10.1 KB
/
Checkpoints.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
/// @title Checkpoints
/// @dev Abstract contract to support checkpoints for Compound-like voting and
/// delegation. This implementation supports token supply up to 2^96 - 1.
/// This contract keeps a history (checkpoints) of each account's vote
/// power. Vote power can be delegated either by calling the {delegate}
/// function directly, or by providing a signature to be used with
/// {delegateBySig}. Voting power can be publicly queried through
/// {getVotes} and {getPastVotes}.
/// NOTE: Extracted from OpenZeppelin ERCVotes.sol.
abstract contract Checkpoints {
struct Checkpoint {
uint32 fromBlock;
uint96 votes;
}
mapping(address => address) internal _delegates;
mapping(address => uint128[]) internal _checkpoints;
uint128[] internal _totalSupplyCheckpoints;
/// @notice Emitted when an account changes their delegate.
event DelegateChanged(
address indexed delegator,
address indexed fromDelegate,
address indexed toDelegate
);
/// @notice Emitted when a balance or delegate change results in changes
/// to an account's voting power.
event DelegateVotesChanged(
address indexed delegate,
uint256 previousBalance,
uint256 newBalance
);
function checkpoints(address account, uint32 pos)
public
view
virtual
returns (Checkpoint memory checkpoint)
{
(uint32 fromBlock, uint96 votes) = decodeCheckpoint(
_checkpoints[account][pos]
);
checkpoint = Checkpoint(fromBlock, votes);
}
/// @notice Get number of checkpoints for `account`.
function numCheckpoints(address account)
public
view
virtual
returns (uint32)
{
return SafeCast.toUint32(_checkpoints[account].length);
}
/// @notice Get the address `account` is currently delegating to.
function delegates(address account) public view virtual returns (address) {
return _delegates[account];
}
/// @notice Gets the current votes balance for `account`.
/// @param account The address to get votes balance
/// @return The number of current votes for `account`
function getVotes(address account) public view returns (uint96) {
uint256 pos = _checkpoints[account].length;
return pos == 0 ? 0 : decodeValue(_checkpoints[account][pos - 1]);
}
/// @notice Determine the prior number of votes for an account as of
/// a block number.
/// @dev Block number must be a finalized block or else this function will
/// revert to prevent misinformation.
/// @param account The address of the account to check
/// @param blockNumber The block number to get the vote balance at
/// @return The number of votes the account had as of the given block
function getPastVotes(address account, uint256 blockNumber)
public
view
returns (uint96)
{
return lookupCheckpoint(_checkpoints[account], blockNumber);
}
/// @notice Retrieve the `totalSupply` at the end of `blockNumber`.
/// Note, this value is the sum of all balances, but it is NOT the
/// sum of all the delegated votes!
/// @param blockNumber The block number to get the total supply at
/// @dev `blockNumber` must have been already mined
function getPastTotalSupply(uint256 blockNumber)
public
view
returns (uint96)
{
return lookupCheckpoint(_totalSupplyCheckpoints, blockNumber);
}
/// @notice Change delegation for `delegator` to `delegatee`.
// slither-disable-next-line dead-code
function delegate(address delegator, address delegatee) internal virtual;
/// @notice Moves voting power from one delegate to another
/// @param src Address of old delegate
/// @param dst Address of new delegate
/// @param amount Voting power amount to transfer between delegates
function moveVotingPower(
address src,
address dst,
uint256 amount
) internal {
if (src != dst && amount > 0) {
if (src != address(0)) {
// https://github.com/crytic/slither/issues/960
// slither-disable-next-line variable-scope
(uint256 oldWeight, uint256 newWeight) = writeCheckpoint(
_checkpoints[src],
subtract,
amount
);
emit DelegateVotesChanged(src, oldWeight, newWeight);
}
if (dst != address(0)) {
// https://github.com/crytic/slither/issues/959
// slither-disable-next-line uninitialized-local
(uint256 oldWeight, uint256 newWeight) = writeCheckpoint(
_checkpoints[dst],
add,
amount
);
emit DelegateVotesChanged(dst, oldWeight, newWeight);
}
}
}
/// @notice Writes a new checkpoint based on operating last stored value
/// with a `delta`. Usually, said operation is the `add` or
/// `subtract` functions from this contract, but more complex
/// functions can be passed as parameters.
/// @param ckpts The checkpoints array to use
/// @param op The function to apply over the last value and the `delta`
/// @param delta Variation with respect to last stored value to be used
/// for new checkpoint
function writeCheckpoint(
uint128[] storage ckpts,
function(uint256, uint256) view returns (uint256) op,
uint256 delta
) internal returns (uint256 oldWeight, uint256 newWeight) {
uint256 pos = ckpts.length;
oldWeight = pos == 0 ? 0 : decodeValue(ckpts[pos - 1]);
newWeight = op(oldWeight, delta);
if (pos > 0) {
uint32 fromBlock = decodeBlockNumber(ckpts[pos - 1]);
// slither-disable-next-line incorrect-equality
if (fromBlock == block.number) {
ckpts[pos - 1] = encodeCheckpoint(
fromBlock,
SafeCast.toUint96(newWeight)
);
return (oldWeight, newWeight);
}
}
ckpts.push(
encodeCheckpoint(
SafeCast.toUint32(block.number),
SafeCast.toUint96(newWeight)
)
);
}
/// @notice Lookup a value in a list of (sorted) checkpoints.
/// @param ckpts The checkpoints array to use
/// @param blockNumber Block number when we want to get the checkpoint at
function lookupCheckpoint(uint128[] storage ckpts, uint256 blockNumber)
internal
view
returns (uint96)
{
// We run a binary search to look for the earliest checkpoint taken
// after `blockNumber`. During the loop, the index of the wanted
// checkpoint remains in the range [low-1, high). With each iteration,
// either `low` or `high` is moved towards the middle of the range to
// maintain the invariant.
// - If the middle checkpoint is after `blockNumber`,
// we look in [low, mid)
// - If the middle checkpoint is before or equal to `blockNumber`,
// we look in [mid+1, high)
// Once we reach a single value (when low == high), we've found the
// right checkpoint at the index high-1, if not out of bounds (in that
// case we're looking too far in the past and the result is 0).
// Note that if the latest checkpoint available is exactly for
// `blockNumber`, we end up with an index that is past the end of the
// array, so we technically don't find a checkpoint after
// `blockNumber`, but it works out the same.
require(blockNumber < block.number, "Block not yet determined");
uint256 high = ckpts.length;
uint256 low = 0;
while (low < high) {
uint256 mid = Math.average(low, high);
uint32 midBlock = decodeBlockNumber(ckpts[mid]);
if (midBlock > blockNumber) {
high = mid;
} else {
low = mid + 1;
}
}
return high == 0 ? 0 : decodeValue(ckpts[high - 1]);
}
/// @notice Maximum token supply. Defaults to `type(uint96).max` (2^96 - 1)
// slither-disable-next-line dead-code
function maxSupply() internal view virtual returns (uint96) {
return type(uint96).max;
}
/// @notice Encodes a `blockNumber` and `value` into a single `uint128`
/// checkpoint.
/// @dev `blockNumber` is stored in the first 32 bits, while `value` in the
/// remaining 96 bits.
function encodeCheckpoint(uint32 blockNumber, uint96 value)
internal
pure
returns (uint128)
{
return (uint128(blockNumber) << 96) | uint128(value);
}
/// @notice Decodes a block number from a `uint128` `checkpoint`.
function decodeBlockNumber(uint128 checkpoint)
internal
pure
returns (uint32)
{
return uint32(bytes4(bytes16(checkpoint)));
}
/// @notice Decodes a voting value from a `uint128` `checkpoint`.
function decodeValue(uint128 checkpoint) internal pure returns (uint96) {
return uint96(checkpoint);
}
/// @notice Decodes a block number and voting value from a `uint128`
/// `checkpoint`.
function decodeCheckpoint(uint128 checkpoint)
internal
pure
returns (uint32 blockNumber, uint96 value)
{
blockNumber = decodeBlockNumber(checkpoint);
value = decodeValue(checkpoint);
}
// slither-disable-next-line dead-code
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
// slither-disable-next-line dead-code
function subtract(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
}