Checkpoints.sol 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.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 memory 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. // Copying to memory is important here.
  116. Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
  117. // Checkpoint keys must be non-decreasing.
  118. if (last._key > key) {
  119. revert CheckpointUnorderedInsertion();
  120. }
  121. // Update or push new checkpoint
  122. if (last._key == key) {
  123. _unsafeAccess(self, pos - 1)._value = value;
  124. } else {
  125. self.push(Checkpoint224({_key: key, _value: value}));
  126. }
  127. return (last._value, value);
  128. } else {
  129. self.push(Checkpoint224({_key: key, _value: value}));
  130. return (0, value);
  131. }
  132. }
  133. /**
  134. * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high`
  135. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  136. * `high`.
  137. *
  138. * WARNING: `high` should not be greater than the array's length.
  139. */
  140. function _upperBinaryLookup(
  141. Checkpoint224[] storage self,
  142. uint32 key,
  143. uint256 low,
  144. uint256 high
  145. ) private view returns (uint256) {
  146. while (low < high) {
  147. uint256 mid = Math.average(low, high);
  148. if (_unsafeAccess(self, mid)._key > key) {
  149. high = mid;
  150. } else {
  151. low = mid + 1;
  152. }
  153. }
  154. return high;
  155. }
  156. /**
  157. * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or
  158. * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and
  159. * exclusive `high`.
  160. *
  161. * WARNING: `high` should not be greater than the array's length.
  162. */
  163. function _lowerBinaryLookup(
  164. Checkpoint224[] storage self,
  165. uint32 key,
  166. uint256 low,
  167. uint256 high
  168. ) private view returns (uint256) {
  169. while (low < high) {
  170. uint256 mid = Math.average(low, high);
  171. if (_unsafeAccess(self, mid)._key < key) {
  172. low = mid + 1;
  173. } else {
  174. high = mid;
  175. }
  176. }
  177. return high;
  178. }
  179. /**
  180. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  181. */
  182. function _unsafeAccess(
  183. Checkpoint224[] storage self,
  184. uint256 pos
  185. ) private pure returns (Checkpoint224 storage result) {
  186. assembly {
  187. mstore(0, self.slot)
  188. result.slot := add(keccak256(0, 0x20), pos)
  189. }
  190. }
  191. struct Trace208 {
  192. Checkpoint208[] _checkpoints;
  193. }
  194. struct Checkpoint208 {
  195. uint48 _key;
  196. uint208 _value;
  197. }
  198. /**
  199. * @dev Pushes a (`key`, `value`) pair into a Trace208 so that it is stored as the checkpoint.
  200. *
  201. * Returns previous value and new value.
  202. *
  203. * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint48).max` key set will disable the
  204. * library.
  205. */
  206. function push(Trace208 storage self, uint48 key, uint208 value) internal returns (uint208, uint208) {
  207. return _insert(self._checkpoints, key, value);
  208. }
  209. /**
  210. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
  211. * there is none.
  212. */
  213. function lowerLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
  214. uint256 len = self._checkpoints.length;
  215. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  216. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  217. }
  218. /**
  219. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  220. * if there is none.
  221. */
  222. function upperLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
  223. uint256 len = self._checkpoints.length;
  224. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  225. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  226. }
  227. /**
  228. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  229. * if there is none.
  230. *
  231. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high
  232. * keys).
  233. */
  234. function upperLookupRecent(Trace208 storage self, uint48 key) internal view returns (uint208) {
  235. uint256 len = self._checkpoints.length;
  236. uint256 low = 0;
  237. uint256 high = len;
  238. if (len > 5) {
  239. uint256 mid = len - Math.sqrt(len);
  240. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  241. high = mid;
  242. } else {
  243. low = mid + 1;
  244. }
  245. }
  246. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  247. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  248. }
  249. /**
  250. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  251. */
  252. function latest(Trace208 storage self) internal view returns (uint208) {
  253. uint256 pos = self._checkpoints.length;
  254. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  255. }
  256. /**
  257. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  258. * in the most recent checkpoint.
  259. */
  260. function latestCheckpoint(Trace208 storage self) internal view returns (bool exists, uint48 _key, uint208 _value) {
  261. uint256 pos = self._checkpoints.length;
  262. if (pos == 0) {
  263. return (false, 0, 0);
  264. } else {
  265. Checkpoint208 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  266. return (true, ckpt._key, ckpt._value);
  267. }
  268. }
  269. /**
  270. * @dev Returns the number of checkpoint.
  271. */
  272. function length(Trace208 storage self) internal view returns (uint256) {
  273. return self._checkpoints.length;
  274. }
  275. /**
  276. * @dev Returns checkpoint at given position.
  277. */
  278. function at(Trace208 storage self, uint32 pos) internal view returns (Checkpoint208 memory) {
  279. return self._checkpoints[pos];
  280. }
  281. /**
  282. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  283. * or by updating the last one.
  284. */
  285. function _insert(Checkpoint208[] storage self, uint48 key, uint208 value) private returns (uint208, uint208) {
  286. uint256 pos = self.length;
  287. if (pos > 0) {
  288. // Copying to memory is important here.
  289. Checkpoint208 memory last = _unsafeAccess(self, pos - 1);
  290. // Checkpoint keys must be non-decreasing.
  291. if (last._key > key) {
  292. revert CheckpointUnorderedInsertion();
  293. }
  294. // Update or push new checkpoint
  295. if (last._key == key) {
  296. _unsafeAccess(self, pos - 1)._value = value;
  297. } else {
  298. self.push(Checkpoint208({_key: key, _value: value}));
  299. }
  300. return (last._value, value);
  301. } else {
  302. self.push(Checkpoint208({_key: key, _value: value}));
  303. return (0, value);
  304. }
  305. }
  306. /**
  307. * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high`
  308. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  309. * `high`.
  310. *
  311. * WARNING: `high` should not be greater than the array's length.
  312. */
  313. function _upperBinaryLookup(
  314. Checkpoint208[] storage self,
  315. uint48 key,
  316. uint256 low,
  317. uint256 high
  318. ) private view returns (uint256) {
  319. while (low < high) {
  320. uint256 mid = Math.average(low, high);
  321. if (_unsafeAccess(self, mid)._key > key) {
  322. high = mid;
  323. } else {
  324. low = mid + 1;
  325. }
  326. }
  327. return high;
  328. }
  329. /**
  330. * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or
  331. * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and
  332. * exclusive `high`.
  333. *
  334. * WARNING: `high` should not be greater than the array's length.
  335. */
  336. function _lowerBinaryLookup(
  337. Checkpoint208[] storage self,
  338. uint48 key,
  339. uint256 low,
  340. uint256 high
  341. ) private view returns (uint256) {
  342. while (low < high) {
  343. uint256 mid = Math.average(low, high);
  344. if (_unsafeAccess(self, mid)._key < key) {
  345. low = mid + 1;
  346. } else {
  347. high = mid;
  348. }
  349. }
  350. return high;
  351. }
  352. /**
  353. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  354. */
  355. function _unsafeAccess(
  356. Checkpoint208[] storage self,
  357. uint256 pos
  358. ) private pure returns (Checkpoint208 storage result) {
  359. assembly {
  360. mstore(0, self.slot)
  361. result.slot := add(keccak256(0, 0x20), pos)
  362. }
  363. }
  364. struct Trace160 {
  365. Checkpoint160[] _checkpoints;
  366. }
  367. struct Checkpoint160 {
  368. uint96 _key;
  369. uint160 _value;
  370. }
  371. /**
  372. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  373. *
  374. * Returns previous value and new value.
  375. *
  376. * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint96).max` key set will disable the
  377. * library.
  378. */
  379. function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) {
  380. return _insert(self._checkpoints, key, value);
  381. }
  382. /**
  383. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
  384. * there is none.
  385. */
  386. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  387. uint256 len = self._checkpoints.length;
  388. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  389. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  390. }
  391. /**
  392. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  393. * if there is none.
  394. */
  395. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  396. uint256 len = self._checkpoints.length;
  397. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  398. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  399. }
  400. /**
  401. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
  402. * if there is none.
  403. *
  404. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high
  405. * keys).
  406. */
  407. function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) {
  408. uint256 len = self._checkpoints.length;
  409. uint256 low = 0;
  410. uint256 high = len;
  411. if (len > 5) {
  412. uint256 mid = len - Math.sqrt(len);
  413. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  414. high = mid;
  415. } else {
  416. low = mid + 1;
  417. }
  418. }
  419. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  420. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  421. }
  422. /**
  423. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  424. */
  425. function latest(Trace160 storage self) internal view returns (uint160) {
  426. uint256 pos = self._checkpoints.length;
  427. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  428. }
  429. /**
  430. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  431. * in the most recent checkpoint.
  432. */
  433. function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
  434. uint256 pos = self._checkpoints.length;
  435. if (pos == 0) {
  436. return (false, 0, 0);
  437. } else {
  438. Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  439. return (true, ckpt._key, ckpt._value);
  440. }
  441. }
  442. /**
  443. * @dev Returns the number of checkpoint.
  444. */
  445. function length(Trace160 storage self) internal view returns (uint256) {
  446. return self._checkpoints.length;
  447. }
  448. /**
  449. * @dev Returns checkpoint at given position.
  450. */
  451. function at(Trace160 storage self, uint32 pos) internal view returns (Checkpoint160 memory) {
  452. return self._checkpoints[pos];
  453. }
  454. /**
  455. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  456. * or by updating the last one.
  457. */
  458. function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) {
  459. uint256 pos = self.length;
  460. if (pos > 0) {
  461. // Copying to memory is important here.
  462. Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
  463. // Checkpoint keys must be non-decreasing.
  464. if (last._key > key) {
  465. revert CheckpointUnorderedInsertion();
  466. }
  467. // Update or push new checkpoint
  468. if (last._key == key) {
  469. _unsafeAccess(self, pos - 1)._value = value;
  470. } else {
  471. self.push(Checkpoint160({_key: key, _value: value}));
  472. }
  473. return (last._value, value);
  474. } else {
  475. self.push(Checkpoint160({_key: key, _value: value}));
  476. return (0, value);
  477. }
  478. }
  479. /**
  480. * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high`
  481. * if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
  482. * `high`.
  483. *
  484. * WARNING: `high` should not be greater than the array's length.
  485. */
  486. function _upperBinaryLookup(
  487. Checkpoint160[] storage self,
  488. uint96 key,
  489. uint256 low,
  490. uint256 high
  491. ) private view returns (uint256) {
  492. while (low < high) {
  493. uint256 mid = Math.average(low, high);
  494. if (_unsafeAccess(self, mid)._key > key) {
  495. high = mid;
  496. } else {
  497. low = mid + 1;
  498. }
  499. }
  500. return high;
  501. }
  502. /**
  503. * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or
  504. * `high` if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and
  505. * exclusive `high`.
  506. *
  507. * WARNING: `high` should not be greater than the array's length.
  508. */
  509. function _lowerBinaryLookup(
  510. Checkpoint160[] storage self,
  511. uint96 key,
  512. uint256 low,
  513. uint256 high
  514. ) private view returns (uint256) {
  515. while (low < high) {
  516. uint256 mid = Math.average(low, high);
  517. if (_unsafeAccess(self, mid)._key < key) {
  518. low = mid + 1;
  519. } else {
  520. high = mid;
  521. }
  522. }
  523. return high;
  524. }
  525. /**
  526. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  527. */
  528. function _unsafeAccess(
  529. Checkpoint160[] storage self,
  530. uint256 pos
  531. ) private pure returns (Checkpoint160 storage result) {
  532. assembly {
  533. mstore(0, self.slot)
  534. result.slot := add(keccak256(0, 0x20), pos)
  535. }
  536. }
  537. }