Checkpoints.sol 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (utils/Checkpoints.sol)
  3. // This file was procedurally generated from scripts/generate/templates/Checkpoints.js.
  4. pragma solidity ^0.8.0;
  5. import "./math/Math.sol";
  6. import "./math/SafeCast.sol";
  7. /**
  8. * @dev This library defines the `History` struct, for checkpointing values as they change at different points in
  9. * time, and later looking up past values by block number. See {Votes} as an example.
  10. *
  11. * To create a history of checkpoints define a variable type `Checkpoints.History` in your contract, and store a new
  12. * checkpoint for the current transaction block using the {push} function.
  13. *
  14. * _Available since v4.5._
  15. */
  16. library Checkpoints {
  17. struct History {
  18. Checkpoint[] _checkpoints;
  19. }
  20. struct Checkpoint {
  21. uint32 _blockNumber;
  22. uint224 _value;
  23. }
  24. /**
  25. * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
  26. * before it is returned, or zero otherwise. Because the number returned corresponds to that at the end of the
  27. * block, the requested block number must be in the past, excluding the current block.
  28. */
  29. function getAtBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
  30. require(blockNumber < block.number, "Checkpoints: block not yet mined");
  31. uint32 key = SafeCast.toUint32(blockNumber);
  32. uint256 len = self._checkpoints.length;
  33. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  34. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  35. }
  36. /**
  37. * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
  38. * before it is returned, or zero otherwise. Similar to {upperLookup} but optimized for the case when the searched
  39. * checkpoint is probably "recent", defined as being among the last sqrt(N) checkpoints where N is the number of
  40. * checkpoints.
  41. */
  42. function getAtProbablyRecentBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
  43. require(blockNumber < block.number, "Checkpoints: block not yet mined");
  44. uint32 key = SafeCast.toUint32(blockNumber);
  45. uint256 len = self._checkpoints.length;
  46. uint256 low = 0;
  47. uint256 high = len;
  48. if (len > 5) {
  49. uint256 mid = len - Math.sqrt(len);
  50. if (key < _unsafeAccess(self._checkpoints, mid)._blockNumber) {
  51. high = mid;
  52. } else {
  53. low = mid + 1;
  54. }
  55. }
  56. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  57. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  58. }
  59. /**
  60. * @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
  61. *
  62. * Returns previous value and new value.
  63. */
  64. function push(History storage self, uint256 value) internal returns (uint256, uint256) {
  65. return _insert(self._checkpoints, SafeCast.toUint32(block.number), SafeCast.toUint224(value));
  66. }
  67. /**
  68. * @dev Pushes a value onto a History, by updating the latest value using binary operation `op`. The new value will
  69. * be set to `op(latest, delta)`.
  70. *
  71. * Returns previous value and new value.
  72. */
  73. function push(
  74. History storage self,
  75. function(uint256, uint256) view returns (uint256) op,
  76. uint256 delta
  77. ) internal returns (uint256, uint256) {
  78. return push(self, op(latest(self), delta));
  79. }
  80. /**
  81. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  82. */
  83. function latest(History storage self) internal view returns (uint224) {
  84. uint256 pos = self._checkpoints.length;
  85. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  86. }
  87. /**
  88. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  89. * in the most recent checkpoint.
  90. */
  91. function latestCheckpoint(
  92. History storage self
  93. ) internal view returns (bool exists, uint32 _blockNumber, uint224 _value) {
  94. uint256 pos = self._checkpoints.length;
  95. if (pos == 0) {
  96. return (false, 0, 0);
  97. } else {
  98. Checkpoint memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  99. return (true, ckpt._blockNumber, ckpt._value);
  100. }
  101. }
  102. /**
  103. * @dev Returns the number of checkpoint.
  104. */
  105. function length(History storage self) internal view returns (uint256) {
  106. return self._checkpoints.length;
  107. }
  108. /**
  109. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  110. * or by updating the last one.
  111. */
  112. function _insert(Checkpoint[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) {
  113. uint256 pos = self.length;
  114. if (pos > 0) {
  115. // Copying to memory is important here.
  116. Checkpoint memory last = _unsafeAccess(self, pos - 1);
  117. // Checkpoint keys must be non-decreasing.
  118. require(last._blockNumber <= key, "Checkpoint: decreasing keys");
  119. // Update or push new checkpoint
  120. if (last._blockNumber == key) {
  121. _unsafeAccess(self, pos - 1)._value = value;
  122. } else {
  123. self.push(Checkpoint({_blockNumber: key, _value: value}));
  124. }
  125. return (last._value, value);
  126. } else {
  127. self.push(Checkpoint({_blockNumber: key, _value: value}));
  128. return (0, value);
  129. }
  130. }
  131. /**
  132. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  133. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  134. *
  135. * WARNING: `high` should not be greater than the array's length.
  136. */
  137. function _upperBinaryLookup(
  138. Checkpoint[] storage self,
  139. uint32 key,
  140. uint256 low,
  141. uint256 high
  142. ) private view returns (uint256) {
  143. while (low < high) {
  144. uint256 mid = Math.average(low, high);
  145. if (_unsafeAccess(self, mid)._blockNumber > key) {
  146. high = mid;
  147. } else {
  148. low = mid + 1;
  149. }
  150. }
  151. return high;
  152. }
  153. /**
  154. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  155. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  156. *
  157. * WARNING: `high` should not be greater than the array's length.
  158. */
  159. function _lowerBinaryLookup(
  160. Checkpoint[] storage self,
  161. uint32 key,
  162. uint256 low,
  163. uint256 high
  164. ) private view returns (uint256) {
  165. while (low < high) {
  166. uint256 mid = Math.average(low, high);
  167. if (_unsafeAccess(self, mid)._blockNumber < key) {
  168. low = mid + 1;
  169. } else {
  170. high = mid;
  171. }
  172. }
  173. return high;
  174. }
  175. /**
  176. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  177. */
  178. function _unsafeAccess(Checkpoint[] storage self, uint256 pos) private pure returns (Checkpoint storage result) {
  179. assembly {
  180. mstore(0, self.slot)
  181. result.slot := add(keccak256(0, 0x20), pos)
  182. }
  183. }
  184. struct Trace224 {
  185. Checkpoint224[] _checkpoints;
  186. }
  187. struct Checkpoint224 {
  188. uint32 _key;
  189. uint224 _value;
  190. }
  191. /**
  192. * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
  193. *
  194. * Returns previous value and new value.
  195. */
  196. function push(Trace224 storage self, uint32 key, uint224 value) internal returns (uint224, uint224) {
  197. return _insert(self._checkpoints, key, value);
  198. }
  199. /**
  200. * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
  201. */
  202. function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  203. uint256 len = self._checkpoints.length;
  204. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  205. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  206. }
  207. /**
  208. * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
  209. */
  210. function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  211. uint256 len = self._checkpoints.length;
  212. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  213. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  214. }
  215. /**
  216. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  217. */
  218. function latest(Trace224 storage self) internal view returns (uint224) {
  219. uint256 pos = self._checkpoints.length;
  220. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  221. }
  222. /**
  223. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  224. * in the most recent checkpoint.
  225. */
  226. function latestCheckpoint(Trace224 storage self) internal view returns (bool exists, uint32 _key, uint224 _value) {
  227. uint256 pos = self._checkpoints.length;
  228. if (pos == 0) {
  229. return (false, 0, 0);
  230. } else {
  231. Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  232. return (true, ckpt._key, ckpt._value);
  233. }
  234. }
  235. /**
  236. * @dev Returns the number of checkpoint.
  237. */
  238. function length(Trace224 storage self) internal view returns (uint256) {
  239. return self._checkpoints.length;
  240. }
  241. /**
  242. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  243. * or by updating the last one.
  244. */
  245. function _insert(Checkpoint224[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) {
  246. uint256 pos = self.length;
  247. if (pos > 0) {
  248. // Copying to memory is important here.
  249. Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
  250. // Checkpoint keys must be non-decreasing.
  251. require(last._key <= key, "Checkpoint: decreasing keys");
  252. // Update or push new checkpoint
  253. if (last._key == key) {
  254. _unsafeAccess(self, pos - 1)._value = value;
  255. } else {
  256. self.push(Checkpoint224({_key: key, _value: value}));
  257. }
  258. return (last._value, value);
  259. } else {
  260. self.push(Checkpoint224({_key: key, _value: value}));
  261. return (0, value);
  262. }
  263. }
  264. /**
  265. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  266. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  267. *
  268. * WARNING: `high` should not be greater than the array's length.
  269. */
  270. function _upperBinaryLookup(
  271. Checkpoint224[] storage self,
  272. uint32 key,
  273. uint256 low,
  274. uint256 high
  275. ) private view returns (uint256) {
  276. while (low < high) {
  277. uint256 mid = Math.average(low, high);
  278. if (_unsafeAccess(self, mid)._key > key) {
  279. high = mid;
  280. } else {
  281. low = mid + 1;
  282. }
  283. }
  284. return high;
  285. }
  286. /**
  287. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  288. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  289. *
  290. * WARNING: `high` should not be greater than the array's length.
  291. */
  292. function _lowerBinaryLookup(
  293. Checkpoint224[] storage self,
  294. uint32 key,
  295. uint256 low,
  296. uint256 high
  297. ) private view returns (uint256) {
  298. while (low < high) {
  299. uint256 mid = Math.average(low, high);
  300. if (_unsafeAccess(self, mid)._key < key) {
  301. low = mid + 1;
  302. } else {
  303. high = mid;
  304. }
  305. }
  306. return high;
  307. }
  308. /**
  309. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  310. */
  311. function _unsafeAccess(
  312. Checkpoint224[] storage self,
  313. uint256 pos
  314. ) private pure returns (Checkpoint224 storage result) {
  315. assembly {
  316. mstore(0, self.slot)
  317. result.slot := add(keccak256(0, 0x20), pos)
  318. }
  319. }
  320. struct Trace160 {
  321. Checkpoint160[] _checkpoints;
  322. }
  323. struct Checkpoint160 {
  324. uint96 _key;
  325. uint160 _value;
  326. }
  327. /**
  328. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  329. *
  330. * Returns previous value and new value.
  331. */
  332. function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) {
  333. return _insert(self._checkpoints, key, value);
  334. }
  335. /**
  336. * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
  337. */
  338. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  339. uint256 len = self._checkpoints.length;
  340. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  341. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  342. }
  343. /**
  344. * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
  345. */
  346. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  347. uint256 len = self._checkpoints.length;
  348. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  349. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  350. }
  351. /**
  352. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  353. */
  354. function latest(Trace160 storage self) internal view returns (uint160) {
  355. uint256 pos = self._checkpoints.length;
  356. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  357. }
  358. /**
  359. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  360. * in the most recent checkpoint.
  361. */
  362. function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
  363. uint256 pos = self._checkpoints.length;
  364. if (pos == 0) {
  365. return (false, 0, 0);
  366. } else {
  367. Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  368. return (true, ckpt._key, ckpt._value);
  369. }
  370. }
  371. /**
  372. * @dev Returns the number of checkpoint.
  373. */
  374. function length(Trace160 storage self) internal view returns (uint256) {
  375. return self._checkpoints.length;
  376. }
  377. /**
  378. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  379. * or by updating the last one.
  380. */
  381. function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) {
  382. uint256 pos = self.length;
  383. if (pos > 0) {
  384. // Copying to memory is important here.
  385. Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
  386. // Checkpoint keys must be non-decreasing.
  387. require(last._key <= key, "Checkpoint: decreasing keys");
  388. // Update or push new checkpoint
  389. if (last._key == key) {
  390. _unsafeAccess(self, pos - 1)._value = value;
  391. } else {
  392. self.push(Checkpoint160({_key: key, _value: value}));
  393. }
  394. return (last._value, value);
  395. } else {
  396. self.push(Checkpoint160({_key: key, _value: value}));
  397. return (0, value);
  398. }
  399. }
  400. /**
  401. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  402. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  403. *
  404. * WARNING: `high` should not be greater than the array's length.
  405. */
  406. function _upperBinaryLookup(
  407. Checkpoint160[] storage self,
  408. uint96 key,
  409. uint256 low,
  410. uint256 high
  411. ) private view returns (uint256) {
  412. while (low < high) {
  413. uint256 mid = Math.average(low, high);
  414. if (_unsafeAccess(self, mid)._key > key) {
  415. high = mid;
  416. } else {
  417. low = mid + 1;
  418. }
  419. }
  420. return high;
  421. }
  422. /**
  423. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  424. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  425. *
  426. * WARNING: `high` should not be greater than the array's length.
  427. */
  428. function _lowerBinaryLookup(
  429. Checkpoint160[] storage self,
  430. uint96 key,
  431. uint256 low,
  432. uint256 high
  433. ) private view returns (uint256) {
  434. while (low < high) {
  435. uint256 mid = Math.average(low, high);
  436. if (_unsafeAccess(self, mid)._key < key) {
  437. low = mid + 1;
  438. } else {
  439. high = mid;
  440. }
  441. }
  442. return high;
  443. }
  444. /**
  445. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  446. */
  447. function _unsafeAccess(
  448. Checkpoint160[] storage self,
  449. uint256 pos
  450. ) private pure returns (Checkpoint160 storage result) {
  451. assembly {
  452. mstore(0, self.slot)
  453. result.slot := add(keccak256(0, 0x20), pos)
  454. }
  455. }
  456. }