Checkpoints.sol 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 Returns checkpoint at given position.
  61. */
  62. function getAtPosition(History storage self, uint32 pos) internal view returns (Checkpoint memory) {
  63. return self._checkpoints[pos];
  64. }
  65. /**
  66. * @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
  67. *
  68. * Returns previous value and new value.
  69. */
  70. function push(History storage self, uint256 value) internal returns (uint256, uint256) {
  71. return _insert(self._checkpoints, SafeCast.toUint32(block.number), SafeCast.toUint224(value));
  72. }
  73. /**
  74. * @dev Pushes a value onto a History, by updating the latest value using binary operation `op`. The new value will
  75. * be set to `op(latest, delta)`.
  76. *
  77. * Returns previous value and new value.
  78. */
  79. function push(
  80. History storage self,
  81. function(uint256, uint256) view returns (uint256) op,
  82. uint256 delta
  83. ) internal returns (uint256, uint256) {
  84. return push(self, op(latest(self), delta));
  85. }
  86. /**
  87. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  88. */
  89. function latest(History storage self) internal view returns (uint224) {
  90. uint256 pos = self._checkpoints.length;
  91. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  92. }
  93. /**
  94. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  95. * in the most recent checkpoint.
  96. */
  97. function latestCheckpoint(History storage self)
  98. internal
  99. view
  100. returns (
  101. bool exists,
  102. uint32 _blockNumber,
  103. uint224 _value
  104. )
  105. {
  106. uint256 pos = self._checkpoints.length;
  107. if (pos == 0) {
  108. return (false, 0, 0);
  109. } else {
  110. Checkpoint memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  111. return (true, ckpt._blockNumber, ckpt._value);
  112. }
  113. }
  114. /**
  115. * @dev Returns the number of checkpoint.
  116. */
  117. function length(History storage self) internal view returns (uint256) {
  118. return self._checkpoints.length;
  119. }
  120. /**
  121. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  122. * or by updating the last one.
  123. */
  124. function _insert(
  125. Checkpoint[] storage self,
  126. uint32 key,
  127. uint224 value
  128. ) private returns (uint224, uint224) {
  129. uint256 pos = self.length;
  130. if (pos > 0) {
  131. // Copying to memory is important here.
  132. Checkpoint memory last = _unsafeAccess(self, pos - 1);
  133. // Checkpoint keys must be non-decreasing.
  134. require(last._blockNumber <= key, "Checkpoint: decreasing keys");
  135. // Update or push new checkpoint
  136. if (last._blockNumber == key) {
  137. _unsafeAccess(self, pos - 1)._value = value;
  138. } else {
  139. self.push(Checkpoint({_blockNumber: key, _value: value}));
  140. }
  141. return (last._value, value);
  142. } else {
  143. self.push(Checkpoint({_blockNumber: key, _value: value}));
  144. return (0, value);
  145. }
  146. }
  147. /**
  148. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  149. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  150. *
  151. * WARNING: `high` should not be greater than the array's length.
  152. */
  153. function _upperBinaryLookup(
  154. Checkpoint[] storage self,
  155. uint32 key,
  156. uint256 low,
  157. uint256 high
  158. ) private view returns (uint256) {
  159. while (low < high) {
  160. uint256 mid = Math.average(low, high);
  161. if (_unsafeAccess(self, mid)._blockNumber > key) {
  162. high = mid;
  163. } else {
  164. low = mid + 1;
  165. }
  166. }
  167. return high;
  168. }
  169. /**
  170. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  171. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  172. *
  173. * WARNING: `high` should not be greater than the array's length.
  174. */
  175. function _lowerBinaryLookup(
  176. Checkpoint[] storage self,
  177. uint32 key,
  178. uint256 low,
  179. uint256 high
  180. ) private view returns (uint256) {
  181. while (low < high) {
  182. uint256 mid = Math.average(low, high);
  183. if (_unsafeAccess(self, mid)._blockNumber < key) {
  184. low = mid + 1;
  185. } else {
  186. high = mid;
  187. }
  188. }
  189. return high;
  190. }
  191. /**
  192. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  193. */
  194. function _unsafeAccess(Checkpoint[] storage self, uint256 pos) private pure returns (Checkpoint storage result) {
  195. assembly {
  196. mstore(0, self.slot)
  197. result.slot := add(keccak256(0, 0x20), pos)
  198. }
  199. }
  200. struct Trace224 {
  201. Checkpoint224[] _checkpoints;
  202. }
  203. struct Checkpoint224 {
  204. uint32 _key;
  205. uint224 _value;
  206. }
  207. /**
  208. * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
  209. *
  210. * Returns previous value and new value.
  211. */
  212. function push(
  213. Trace224 storage self,
  214. uint32 key,
  215. uint224 value
  216. ) internal returns (uint224, uint224) {
  217. return _insert(self._checkpoints, key, value);
  218. }
  219. /**
  220. * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
  221. */
  222. function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  223. uint256 len = self._checkpoints.length;
  224. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  225. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  226. }
  227. /**
  228. * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
  229. */
  230. function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  231. uint256 len = self._checkpoints.length;
  232. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  233. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  234. }
  235. /**
  236. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  237. */
  238. function latest(Trace224 storage self) internal view returns (uint224) {
  239. uint256 pos = self._checkpoints.length;
  240. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  241. }
  242. /**
  243. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  244. * in the most recent checkpoint.
  245. */
  246. function latestCheckpoint(Trace224 storage self)
  247. internal
  248. view
  249. returns (
  250. bool exists,
  251. uint32 _key,
  252. uint224 _value
  253. )
  254. {
  255. uint256 pos = self._checkpoints.length;
  256. if (pos == 0) {
  257. return (false, 0, 0);
  258. } else {
  259. Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  260. return (true, ckpt._key, ckpt._value);
  261. }
  262. }
  263. /**
  264. * @dev Returns the number of checkpoint.
  265. */
  266. function length(Trace224 storage self) internal view returns (uint256) {
  267. return self._checkpoints.length;
  268. }
  269. /**
  270. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  271. * or by updating the last one.
  272. */
  273. function _insert(
  274. Checkpoint224[] storage self,
  275. uint32 key,
  276. uint224 value
  277. ) private returns (uint224, uint224) {
  278. uint256 pos = self.length;
  279. if (pos > 0) {
  280. // Copying to memory is important here.
  281. Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
  282. // Checkpoint keys must be non-decreasing.
  283. require(last._key <= key, "Checkpoint: decreasing keys");
  284. // Update or push new checkpoint
  285. if (last._key == key) {
  286. _unsafeAccess(self, pos - 1)._value = value;
  287. } else {
  288. self.push(Checkpoint224({_key: key, _value: value}));
  289. }
  290. return (last._value, value);
  291. } else {
  292. self.push(Checkpoint224({_key: key, _value: value}));
  293. return (0, value);
  294. }
  295. }
  296. /**
  297. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  298. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  299. *
  300. * WARNING: `high` should not be greater than the array's length.
  301. */
  302. function _upperBinaryLookup(
  303. Checkpoint224[] storage self,
  304. uint32 key,
  305. uint256 low,
  306. uint256 high
  307. ) private view returns (uint256) {
  308. while (low < high) {
  309. uint256 mid = Math.average(low, high);
  310. if (_unsafeAccess(self, mid)._key > key) {
  311. high = mid;
  312. } else {
  313. low = mid + 1;
  314. }
  315. }
  316. return high;
  317. }
  318. /**
  319. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  320. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  321. *
  322. * WARNING: `high` should not be greater than the array's length.
  323. */
  324. function _lowerBinaryLookup(
  325. Checkpoint224[] storage self,
  326. uint32 key,
  327. uint256 low,
  328. uint256 high
  329. ) private view returns (uint256) {
  330. while (low < high) {
  331. uint256 mid = Math.average(low, high);
  332. if (_unsafeAccess(self, mid)._key < key) {
  333. low = mid + 1;
  334. } else {
  335. high = mid;
  336. }
  337. }
  338. return high;
  339. }
  340. /**
  341. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  342. */
  343. function _unsafeAccess(Checkpoint224[] storage self, uint256 pos)
  344. private
  345. pure
  346. returns (Checkpoint224 storage result)
  347. {
  348. assembly {
  349. mstore(0, self.slot)
  350. result.slot := add(keccak256(0, 0x20), pos)
  351. }
  352. }
  353. struct Trace160 {
  354. Checkpoint160[] _checkpoints;
  355. }
  356. struct Checkpoint160 {
  357. uint96 _key;
  358. uint160 _value;
  359. }
  360. /**
  361. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  362. *
  363. * Returns previous value and new value.
  364. */
  365. function push(
  366. Trace160 storage self,
  367. uint96 key,
  368. uint160 value
  369. ) internal returns (uint160, uint160) {
  370. return _insert(self._checkpoints, key, value);
  371. }
  372. /**
  373. * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
  374. */
  375. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  376. uint256 len = self._checkpoints.length;
  377. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  378. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  379. }
  380. /**
  381. * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
  382. */
  383. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  384. uint256 len = self._checkpoints.length;
  385. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  386. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  387. }
  388. /**
  389. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  390. */
  391. function latest(Trace160 storage self) internal view returns (uint160) {
  392. uint256 pos = self._checkpoints.length;
  393. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  394. }
  395. /**
  396. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  397. * in the most recent checkpoint.
  398. */
  399. function latestCheckpoint(Trace160 storage self)
  400. internal
  401. view
  402. returns (
  403. bool exists,
  404. uint96 _key,
  405. uint160 _value
  406. )
  407. {
  408. uint256 pos = self._checkpoints.length;
  409. if (pos == 0) {
  410. return (false, 0, 0);
  411. } else {
  412. Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  413. return (true, ckpt._key, ckpt._value);
  414. }
  415. }
  416. /**
  417. * @dev Returns the number of checkpoint.
  418. */
  419. function length(Trace160 storage self) internal view returns (uint256) {
  420. return self._checkpoints.length;
  421. }
  422. /**
  423. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  424. * or by updating the last one.
  425. */
  426. function _insert(
  427. Checkpoint160[] storage self,
  428. uint96 key,
  429. uint160 value
  430. ) private returns (uint160, uint160) {
  431. uint256 pos = self.length;
  432. if (pos > 0) {
  433. // Copying to memory is important here.
  434. Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
  435. // Checkpoint keys must be non-decreasing.
  436. require(last._key <= key, "Checkpoint: decreasing keys");
  437. // Update or push new checkpoint
  438. if (last._key == key) {
  439. _unsafeAccess(self, pos - 1)._value = value;
  440. } else {
  441. self.push(Checkpoint160({_key: key, _value: value}));
  442. }
  443. return (last._value, value);
  444. } else {
  445. self.push(Checkpoint160({_key: key, _value: value}));
  446. return (0, value);
  447. }
  448. }
  449. /**
  450. * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
  451. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  452. *
  453. * WARNING: `high` should not be greater than the array's length.
  454. */
  455. function _upperBinaryLookup(
  456. Checkpoint160[] storage self,
  457. uint96 key,
  458. uint256 low,
  459. uint256 high
  460. ) private view returns (uint256) {
  461. while (low < high) {
  462. uint256 mid = Math.average(low, high);
  463. if (_unsafeAccess(self, mid)._key > key) {
  464. high = mid;
  465. } else {
  466. low = mid + 1;
  467. }
  468. }
  469. return high;
  470. }
  471. /**
  472. * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
  473. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  474. *
  475. * WARNING: `high` should not be greater than the array's length.
  476. */
  477. function _lowerBinaryLookup(
  478. Checkpoint160[] storage self,
  479. uint96 key,
  480. uint256 low,
  481. uint256 high
  482. ) private view returns (uint256) {
  483. while (low < high) {
  484. uint256 mid = Math.average(low, high);
  485. if (_unsafeAccess(self, mid)._key < key) {
  486. low = mid + 1;
  487. } else {
  488. high = mid;
  489. }
  490. }
  491. return high;
  492. }
  493. /**
  494. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  495. */
  496. function _unsafeAccess(Checkpoint160[] storage self, uint256 pos)
  497. private
  498. pure
  499. returns (Checkpoint160 storage result)
  500. {
  501. assembly {
  502. mstore(0, self.slot)
  503. result.slot := add(keccak256(0, 0x20), pos)
  504. }
  505. }
  506. }