This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PolicyholderCheckpoints.sol
281 lines (249 loc) · 11.5 KB
/
PolicyholderCheckpoints.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
275
276
277
278
279
280
281
// SPDX-License-Identifier: MIT
// forgefmt: disable-start
pragma solidity ^0.8.0;
import {LlamaUtils} from "src/lib/LlamaUtils.sol";
/**
* @dev This library defines the `History` struct, for checkpointing values as they change at different points in
* time, and later looking up past values by block timestamp.
*
* To create a history of checkpoints define a variable type `PolicyholderCheckpoints.History` in your contract, and store a new
* checkpoint for the current transaction timestamp using the {push} function.
*
* @dev This was created by modifying then running the OpenZeppelin `Checkpoints.js` script, which generated a version
* of this library that uses a 64 bit `timestamp` and 96 bit `quantity` field in the `Checkpoint` struct. The struct
* was then modified to add a 64 bit `expiration` field. For simplicity, safe cast and math methods were inlined from
* the OpenZeppelin versions at the same commit. We disable forge-fmt for this file to simplify diffing against the
* original OpenZeppelin version: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/d00acef4059807535af0bd0dd0ddf619747a044b/contracts/utils/Checkpoints.sol
*/
library PolicyholderCheckpoints {
struct History {
Checkpoint[] _checkpoints;
}
struct Checkpoint {
uint64 timestamp;
uint64 expiration;
uint96 quantity;
}
/**
* @dev Returns the quantity at a given block timestamp. If a checkpoint is not available at that time, the closest
* one before it is returned, or zero otherwise. Similar to {upperLookup} but optimized for the case when the
* searched checkpoint is probably "recent", defined as being among the last sqrt(N) checkpoints where N is the
* timestamp of checkpoints.
*/
function getAtProbablyRecentTimestamp(History storage self, uint256 timestamp) internal view returns (uint96) {
require(timestamp < block.timestamp, "PolicyholderCheckpoints: timestamp is not in the past");
uint64 _timestamp = LlamaUtils.toUint64(timestamp);
uint256 len = self._checkpoints.length;
uint256 low = 0;
uint256 high = len;
if (len > 5) {
uint256 mid = len - sqrt(len);
if (_timestamp < _unsafeAccess(self._checkpoints, mid).timestamp) {
high = mid;
} else {
low = mid + 1;
}
}
uint256 pos = _upperBinaryLookup(self._checkpoints, _timestamp, low, high);
return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1).quantity;
}
/**
* @dev Pushes a `quantity` and `expiration` onto a History so that it is stored as the checkpoint for the current
* `timestamp`.
*
* Returns previous quantity and new quantity.
*
* @dev Note that the order of the `expiration` and `quantity` parameters is reversed from the ordering used
* everywhere else in this file. The struct and other methods have the order as `(expiration, quantity)` but this
* method has it as `(quantity, expiration)`. As a result, use caution when editing this method to avoid
* accidentally introducing a bug or breaking change.
*/
function push(History storage self, uint256 quantity, uint256 expiration) internal returns (uint96, uint96) {
return _insert(self._checkpoints, LlamaUtils.toUint64(block.timestamp), LlamaUtils.toUint64(expiration), LlamaUtils.toUint96(quantity));
}
/**
* @dev Returns the quantity in the most recent checkpoint, or zero if there are no checkpoints.
*/
function latest(History storage self) internal view returns (uint96) {
uint256 pos = self._checkpoints.length;
return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1).quantity;
}
/**
* @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the timestamp and
* quantity in the most recent checkpoint.
*/
function latestCheckpoint(History storage self)
internal
view
returns (
bool exists,
uint64 timestamp,
uint64 expiration,
uint96 quantity
)
{
uint256 pos = self._checkpoints.length;
if (pos == 0) {
return (false, 0, 0, 0);
} else {
Checkpoint memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
return (true, ckpt.timestamp, ckpt.expiration, ckpt.quantity);
}
}
/**
* @dev Returns the number of checkpoints.
*/
function length(History storage self) internal view returns (uint256) {
return self._checkpoints.length;
}
/**
* @dev Pushes a (`timestamp`, `expiration`, `quantity`) pair into an ordered list of checkpoints, either by inserting a new
* checkpoint, or by updating the last one.
*/
function _insert(
Checkpoint[] storage self,
uint64 timestamp,
uint64 expiration,
uint96 quantity
) private returns (uint96, uint96) {
uint256 pos = self.length;
if (pos > 0) {
// Copying to memory is important here.
Checkpoint memory last = _unsafeAccess(self, pos - 1);
// Checkpoints timestamps must be increasing.
require(last.timestamp <= timestamp, "Role Checkpoint: invalid timestamp");
// Update or push new checkpoint
if (last.timestamp == timestamp) {
Checkpoint storage ckpt = _unsafeAccess(self, pos - 1);
ckpt.quantity = quantity;
ckpt.expiration = expiration;
} else {
self.push(Checkpoint({timestamp: timestamp, expiration: expiration, quantity: quantity}));
}
return (last.quantity, quantity);
} else {
self.push(Checkpoint({timestamp: timestamp, expiration: expiration, quantity: quantity}));
return (0, quantity);
}
}
/**
* @dev Return the index of the oldest checkpoint whose timestamp is greater than the search timestamp, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function _upperBinaryLookup(
Checkpoint[] storage self,
uint64 timestamp,
uint256 low,
uint256 high
) private view returns (uint256) {
while (low < high) {
uint256 mid = average(low, high);
if (_unsafeAccess(self, mid).timestamp > timestamp) {
high = mid;
} else {
low = mid + 1;
}
}
return high;
}
/**
* @dev Return the index of the oldest checkpoint whose timestamp is greater or equal than the search timestamp, or
* `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and
* exclusive `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function _lowerBinaryLookup(
Checkpoint[] storage self,
uint64 timestamp,
uint256 low,
uint256 high
) private view returns (uint256) {
while (low < high) {
uint256 mid = average(low, high);
if (_unsafeAccess(self, mid).timestamp < timestamp) {
low = mid + 1;
} else {
high = mid;
}
}
return high;
}
function _unsafeAccess(Checkpoint[] storage self, uint256 pos)
private
pure
returns (Checkpoint storage result)
{
assembly {
mstore(0, self.slot)
result.slot := add(keccak256(0, 0x20), pos)
}
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) private pure returns (uint256) {
return (a & b) + (a ^ b) / 2; // (a + b) / 2 can overflow.
}
/**
* @dev This was copied from Solmate v7 https://github.com/transmissions11/solmate/blob/e8f96f25d48fe702117ce76c79228ca4f20206cb/src/utils/FixedPointMathLib.sol
* @notice The math utils in solmate v7 were reviewed/audited by spearbit as part of the art gobblers audit, and are more efficient than the v6 versions.
*/
function sqrt(uint256 x) internal pure returns (uint256 z) {
assembly {
let y := x // We start y at x, which will help us make our initial estimate.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// We check y >= 2^(k + 8) but shift right by k bits
// each branch to ensure that if x >= 256, then y >= 256.
if iszero(lt(y, 0x10000000000000000000000000000000000)) {
y := shr(128, y)
z := shl(64, z)
}
if iszero(lt(y, 0x1000000000000000000)) {
y := shr(64, y)
z := shl(32, z)
}
if iszero(lt(y, 0x10000000000)) {
y := shr(32, y)
z := shl(16, z)
}
if iszero(lt(y, 0x1000000)) {
y := shr(16, y)
z := shl(8, z)
}
// Goal was to get z*z*y within a small factor of x. More iterations could
// get y in a tighter range. Currently, we will have y in [256, 256*2^16).
// We ensured y >= 256 so that the relative difference between y and y+1 is small.
// That's not possible if x < 256 but we can just verify those cases exhaustively.
// Now, z*z*y <= x < z*z*(y+1), and y <= 2^(16+8), and either y >= 256, or x < 256.
// Correctness can be checked exhaustively for x < 256, so we assume y >= 256.
// Then z*sqrt(y) is within sqrt(257)/sqrt(256) of sqrt(x), or about 20bps.
// For s in the range [1/256, 256], the estimate f(s) = (181/1024) * (s+1) is in the range
// (1/2.84 * sqrt(s), 2.84 * sqrt(s)), with largest error when s = 1 and when s = 256 or 1/256.
// Since y is in [256, 256*2^16), let a = y/65536, so that a is in [1/256, 256). Then we can estimate
// sqrt(y) using sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2^18.
// There is no overflow risk here since y < 2^136 after the first branch above.
z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If x+1 is a perfect square, the Babylonian method cycles between
// floor(sqrt(x)) and ceil(sqrt(x)). This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
// Since the ceil is rare, we save gas on the assignment and repeat division in the rare case.
// If you don't care whether the floor or ceil square root is returned, you can remove this statement.
z := sub(z, lt(div(x, z), z))
}
}
}