Checkpoints.sol 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/Checkpoints.sol)
  3. // This file was procedurally generated from scripts/generate/templates/Checkpoints.js.
  4. pragma solidity ^0.8.20;
  5. import {Math} from "../math/Math.sol";
  6. /**
  7. * @dev This library defines the `Trace*` struct, for checkpointing values as they change at different points in
  8. * time, and later looking up past values by block number. See {Votes} as an example.
  9. *
  10. * To create a history of checkpoints define a variable type `Checkpoints.Trace*` in your contract, and store a new
  11. * checkpoint for the current transaction block using the {push} function.
  12. */
  13. library Checkpoints {
  14. /**
  15. * @dev A value was attempted to be inserted on a past checkpoint.
  16. */
  17. error CheckpointUnorderedInsertion();
  18. struct Trace224 {
  19. Checkpoint224[] _checkpoints;
  20. }
  21. struct Checkpoint224 {
  22. uint32 _key;
  23. uint224 _value;
  24. }
  25. /**
  26. * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
  27. *
  28. * Returns previous value and new value.
  29. *
  30. * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint32).max` key set will disable the
  31. * library.
  32. */
  33. function push(Trace224 storage self, uint32 key, uint224 value) internal returns (uint224, uint224) {
  34. return _insert(self._checkpoints, key, value);
  35. }
  36. /**
  37. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
  38. * there is none.
  39. */
  40. function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  41. uint256 len = self._checkpoints.length;
  42. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  43. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  44. }
  45. /**
  46. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  47. * if there is none.
  48. */
  49. function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  50. uint256 len = self._checkpoints.length;
  51. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  52. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  53. }
  54. /**
  55. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  56. * if there is none.
  57. *
  58. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high
  59. * keys).
  60. */
  61. function upperLookupRecent(Trace224 storage self, uint32 key) internal view returns (uint224) {
  62. uint256 len = self._checkpoints.length;
  63. uint256 low = 0;
  64. uint256 high = len;
  65. if (len > 5) {
  66. uint256 mid = len - Math.sqrt(len);
  67. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  68. high = mid;
  69. } else {
  70. low = mid + 1;
  71. }
  72. }
  73. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  74. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  75. }
  76. /**
  77. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  78. */
  79. function latest(Trace224 storage self) internal view returns (uint224) {
  80. uint256 pos = self._checkpoints.length;
  81. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  82. }
  83. /**
  84. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  85. * in the most recent checkpoint.
  86. */
  87. function latestCheckpoint(Trace224 storage self) internal view returns (bool exists, uint32 _key, uint224 _value) {
  88. uint256 pos = self._checkpoints.length;
  89. if (pos == 0) {
  90. return (false, 0, 0);
  91. } else {
  92. Checkpoint224 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  93. return (true, ckpt._key, ckpt._value);
  94. }
  95. }
  96. /**
  97. * @dev Returns the number of checkpoint.
  98. */
  99. function length(Trace224 storage self) internal view returns (uint256) {
  100. return self._checkpoints.length;
  101. }
  102. /**
  103. * @dev Returns checkpoint at given position.
  104. */
  105. function at(Trace224 storage self, uint32 pos) internal view returns (Checkpoint224 memory) {
  106. return self._checkpoints[pos];
  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(Checkpoint224[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) {
  113. uint256 pos = self.length;
  114. if (pos > 0) {
  115. Checkpoint224 storage last = _unsafeAccess(self, pos - 1);
  116. uint32 lastKey = last._key;
  117. uint224 lastValue = last._value;
  118. // Checkpoint keys must be non-decreasing.
  119. if (lastKey > key) {
  120. revert CheckpointUnorderedInsertion();
  121. }
  122. // Update or push new checkpoint
  123. if (lastKey == key) {
  124. _unsafeAccess(self, pos - 1)._value = value;
  125. } else {
  126. self.push(Checkpoint224({_key: key, _value: value}));
  127. }
  128. return (lastValue, value);
  129. } else {
  130. self.push(Checkpoint224({_key: key, _value: value}));
  131. return (0, value);
  132. }
  133. }
  134. /**
  135. * @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
  136. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  137. * `high`.
  138. *
  139. * WARNING: `high` should not be greater than the array's length.
  140. */
  141. function _upperBinaryLookup(
  142. Checkpoint224[] storage self,
  143. uint32 key,
  144. uint256 low,
  145. uint256 high
  146. ) private view returns (uint256) {
  147. while (low < high) {
  148. uint256 mid = Math.average(low, high);
  149. if (_unsafeAccess(self, mid)._key > key) {
  150. high = mid;
  151. } else {
  152. low = mid + 1;
  153. }
  154. }
  155. return high;
  156. }
  157. /**
  158. * @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
  159. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  160. * `high`.
  161. *
  162. * WARNING: `high` should not be greater than the array's length.
  163. */
  164. function _lowerBinaryLookup(
  165. Checkpoint224[] storage self,
  166. uint32 key,
  167. uint256 low,
  168. uint256 high
  169. ) private view returns (uint256) {
  170. while (low < high) {
  171. uint256 mid = Math.average(low, high);
  172. if (_unsafeAccess(self, mid)._key < key) {
  173. low = mid + 1;
  174. } else {
  175. high = mid;
  176. }
  177. }
  178. return high;
  179. }
  180. /**
  181. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  182. */
  183. function _unsafeAccess(
  184. Checkpoint224[] storage self,
  185. uint256 pos
  186. ) private pure returns (Checkpoint224 storage result) {
  187. assembly {
  188. mstore(0, self.slot)
  189. result.slot := add(keccak256(0, 0x20), pos)
  190. }
  191. }
  192. struct Trace208 {
  193. Checkpoint208[] _checkpoints;
  194. }
  195. struct Checkpoint208 {
  196. uint48 _key;
  197. uint208 _value;
  198. }
  199. /**
  200. * @dev Pushes a (`key`, `value`) pair into a Trace208 so that it is stored as the checkpoint.
  201. *
  202. * Returns previous value and new value.
  203. *
  204. * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint48).max` key set will disable the
  205. * library.
  206. */
  207. function push(Trace208 storage self, uint48 key, uint208 value) internal returns (uint208, uint208) {
  208. return _insert(self._checkpoints, key, value);
  209. }
  210. /**
  211. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
  212. * there is none.
  213. */
  214. function lowerLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
  215. uint256 len = self._checkpoints.length;
  216. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  217. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  218. }
  219. /**
  220. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  221. * if there is none.
  222. */
  223. function upperLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
  224. uint256 len = self._checkpoints.length;
  225. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  226. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  227. }
  228. /**
  229. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  230. * if there is none.
  231. *
  232. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high
  233. * keys).
  234. */
  235. function upperLookupRecent(Trace208 storage self, uint48 key) internal view returns (uint208) {
  236. uint256 len = self._checkpoints.length;
  237. uint256 low = 0;
  238. uint256 high = len;
  239. if (len > 5) {
  240. uint256 mid = len - Math.sqrt(len);
  241. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  242. high = mid;
  243. } else {
  244. low = mid + 1;
  245. }
  246. }
  247. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  248. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  249. }
  250. /**
  251. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  252. */
  253. function latest(Trace208 storage self) internal view returns (uint208) {
  254. uint256 pos = self._checkpoints.length;
  255. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  256. }
  257. /**
  258. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  259. * in the most recent checkpoint.
  260. */
  261. function latestCheckpoint(Trace208 storage self) internal view returns (bool exists, uint48 _key, uint208 _value) {
  262. uint256 pos = self._checkpoints.length;
  263. if (pos == 0) {
  264. return (false, 0, 0);
  265. } else {
  266. Checkpoint208 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  267. return (true, ckpt._key, ckpt._value);
  268. }
  269. }
  270. /**
  271. * @dev Returns the number of checkpoint.
  272. */
  273. function length(Trace208 storage self) internal view returns (uint256) {
  274. return self._checkpoints.length;
  275. }
  276. /**
  277. * @dev Returns checkpoint at given position.
  278. */
  279. function at(Trace208 storage self, uint32 pos) internal view returns (Checkpoint208 memory) {
  280. return self._checkpoints[pos];
  281. }
  282. /**
  283. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  284. * or by updating the last one.
  285. */
  286. function _insert(Checkpoint208[] storage self, uint48 key, uint208 value) private returns (uint208, uint208) {
  287. uint256 pos = self.length;
  288. if (pos > 0) {
  289. Checkpoint208 storage last = _unsafeAccess(self, pos - 1);
  290. uint48 lastKey = last._key;
  291. uint208 lastValue = last._value;
  292. // Checkpoint keys must be non-decreasing.
  293. if (lastKey > key) {
  294. revert CheckpointUnorderedInsertion();
  295. }
  296. // Update or push new checkpoint
  297. if (lastKey == key) {
  298. _unsafeAccess(self, pos - 1)._value = value;
  299. } else {
  300. self.push(Checkpoint208({_key: key, _value: value}));
  301. }
  302. return (lastValue, value);
  303. } else {
  304. self.push(Checkpoint208({_key: key, _value: value}));
  305. return (0, value);
  306. }
  307. }
  308. /**
  309. * @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
  310. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  311. * `high`.
  312. *
  313. * WARNING: `high` should not be greater than the array's length.
  314. */
  315. function _upperBinaryLookup(
  316. Checkpoint208[] storage self,
  317. uint48 key,
  318. uint256 low,
  319. uint256 high
  320. ) private view returns (uint256) {
  321. while (low < high) {
  322. uint256 mid = Math.average(low, high);
  323. if (_unsafeAccess(self, mid)._key > key) {
  324. high = mid;
  325. } else {
  326. low = mid + 1;
  327. }
  328. }
  329. return high;
  330. }
  331. /**
  332. * @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
  333. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  334. * `high`.
  335. *
  336. * WARNING: `high` should not be greater than the array's length.
  337. */
  338. function _lowerBinaryLookup(
  339. Checkpoint208[] storage self,
  340. uint48 key,
  341. uint256 low,
  342. uint256 high
  343. ) private view returns (uint256) {
  344. while (low < high) {
  345. uint256 mid = Math.average(low, high);
  346. if (_unsafeAccess(self, mid)._key < key) {
  347. low = mid + 1;
  348. } else {
  349. high = mid;
  350. }
  351. }
  352. return high;
  353. }
  354. /**
  355. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  356. */
  357. function _unsafeAccess(
  358. Checkpoint208[] storage self,
  359. uint256 pos
  360. ) private pure returns (Checkpoint208 storage result) {
  361. assembly {
  362. mstore(0, self.slot)
  363. result.slot := add(keccak256(0, 0x20), pos)
  364. }
  365. }
  366. struct Trace160 {
  367. Checkpoint160[] _checkpoints;
  368. }
  369. struct Checkpoint160 {
  370. uint96 _key;
  371. uint160 _value;
  372. }
  373. /**
  374. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  375. *
  376. * Returns previous value and new value.
  377. *
  378. * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint96).max` key set will disable the
  379. * library.
  380. */
  381. function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) {
  382. return _insert(self._checkpoints, key, value);
  383. }
  384. /**
  385. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
  386. * there is none.
  387. */
  388. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  389. uint256 len = self._checkpoints.length;
  390. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  391. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  392. }
  393. /**
  394. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  395. * if there is none.
  396. */
  397. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  398. uint256 len = self._checkpoints.length;
  399. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  400. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  401. }
  402. /**
  403. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  404. * if there is none.
  405. *
  406. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high
  407. * keys).
  408. */
  409. function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) {
  410. uint256 len = self._checkpoints.length;
  411. uint256 low = 0;
  412. uint256 high = len;
  413. if (len > 5) {
  414. uint256 mid = len - Math.sqrt(len);
  415. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  416. high = mid;
  417. } else {
  418. low = mid + 1;
  419. }
  420. }
  421. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  422. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  423. }
  424. /**
  425. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  426. */
  427. function latest(Trace160 storage self) internal view returns (uint160) {
  428. uint256 pos = self._checkpoints.length;
  429. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  430. }
  431. /**
  432. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  433. * in the most recent checkpoint.
  434. */
  435. function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
  436. uint256 pos = self._checkpoints.length;
  437. if (pos == 0) {
  438. return (false, 0, 0);
  439. } else {
  440. Checkpoint160 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  441. return (true, ckpt._key, ckpt._value);
  442. }
  443. }
  444. /**
  445. * @dev Returns the number of checkpoint.
  446. */
  447. function length(Trace160 storage self) internal view returns (uint256) {
  448. return self._checkpoints.length;
  449. }
  450. /**
  451. * @dev Returns checkpoint at given position.
  452. */
  453. function at(Trace160 storage self, uint32 pos) internal view returns (Checkpoint160 memory) {
  454. return self._checkpoints[pos];
  455. }
  456. /**
  457. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  458. * or by updating the last one.
  459. */
  460. function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) {
  461. uint256 pos = self.length;
  462. if (pos > 0) {
  463. Checkpoint160 storage last = _unsafeAccess(self, pos - 1);
  464. uint96 lastKey = last._key;
  465. uint160 lastValue = last._value;
  466. // Checkpoint keys must be non-decreasing.
  467. if (lastKey > key) {
  468. revert CheckpointUnorderedInsertion();
  469. }
  470. // Update or push new checkpoint
  471. if (lastKey == key) {
  472. _unsafeAccess(self, pos - 1)._value = value;
  473. } else {
  474. self.push(Checkpoint160({_key: key, _value: value}));
  475. }
  476. return (lastValue, value);
  477. } else {
  478. self.push(Checkpoint160({_key: key, _value: value}));
  479. return (0, value);
  480. }
  481. }
  482. /**
  483. * @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
  484. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  485. * `high`.
  486. *
  487. * WARNING: `high` should not be greater than the array's length.
  488. */
  489. function _upperBinaryLookup(
  490. Checkpoint160[] storage self,
  491. uint96 key,
  492. uint256 low,
  493. uint256 high
  494. ) private view returns (uint256) {
  495. while (low < high) {
  496. uint256 mid = Math.average(low, high);
  497. if (_unsafeAccess(self, mid)._key > key) {
  498. high = mid;
  499. } else {
  500. low = mid + 1;
  501. }
  502. }
  503. return high;
  504. }
  505. /**
  506. * @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
  507. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  508. * `high`.
  509. *
  510. * WARNING: `high` should not be greater than the array's length.
  511. */
  512. function _lowerBinaryLookup(
  513. Checkpoint160[] storage self,
  514. uint96 key,
  515. uint256 low,
  516. uint256 high
  517. ) private view returns (uint256) {
  518. while (low < high) {
  519. uint256 mid = Math.average(low, high);
  520. if (_unsafeAccess(self, mid)._key < key) {
  521. low = mid + 1;
  522. } else {
  523. high = mid;
  524. }
  525. }
  526. return high;
  527. }
  528. /**
  529. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  530. */
  531. function _unsafeAccess(
  532. Checkpoint160[] storage self,
  533. uint256 pos
  534. ) private pure returns (Checkpoint160 storage result) {
  535. assembly {
  536. mstore(0, self.slot)
  537. result.slot := add(keccak256(0, 0x20), pos)
  538. }
  539. }
  540. }