Checkpoints.sol 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.1) (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 last (most recent) checkpoint with key lower or equal 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 first (oldest) checkpoint with 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 first (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 last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
  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 last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
  217. *
  218. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys).
  219. */
  220. function upperLookupRecent(Trace224 storage self, uint32 key) internal view returns (uint224) {
  221. uint256 len = self._checkpoints.length;
  222. uint256 low = 0;
  223. uint256 high = len;
  224. if (len > 5) {
  225. uint256 mid = len - Math.sqrt(len);
  226. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  227. high = mid;
  228. } else {
  229. low = mid + 1;
  230. }
  231. }
  232. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  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) internal view returns (bool exists, uint32 _key, uint224 _value) {
  247. uint256 pos = self._checkpoints.length;
  248. if (pos == 0) {
  249. return (false, 0, 0);
  250. } else {
  251. Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  252. return (true, ckpt._key, ckpt._value);
  253. }
  254. }
  255. /**
  256. * @dev Returns the number of checkpoint.
  257. */
  258. function length(Trace224 storage self) internal view returns (uint256) {
  259. return self._checkpoints.length;
  260. }
  261. /**
  262. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  263. * or by updating the last one.
  264. */
  265. function _insert(Checkpoint224[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) {
  266. uint256 pos = self.length;
  267. if (pos > 0) {
  268. // Copying to memory is important here.
  269. Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
  270. // Checkpoint keys must be non-decreasing.
  271. require(last._key <= key, "Checkpoint: decreasing keys");
  272. // Update or push new checkpoint
  273. if (last._key == key) {
  274. _unsafeAccess(self, pos - 1)._value = value;
  275. } else {
  276. self.push(Checkpoint224({_key: key, _value: value}));
  277. }
  278. return (last._value, value);
  279. } else {
  280. self.push(Checkpoint224({_key: key, _value: value}));
  281. return (0, value);
  282. }
  283. }
  284. /**
  285. * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high` if there is none.
  286. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  287. *
  288. * WARNING: `high` should not be greater than the array's length.
  289. */
  290. function _upperBinaryLookup(
  291. Checkpoint224[] storage self,
  292. uint32 key,
  293. uint256 low,
  294. uint256 high
  295. ) private view returns (uint256) {
  296. while (low < high) {
  297. uint256 mid = Math.average(low, high);
  298. if (_unsafeAccess(self, mid)._key > key) {
  299. high = mid;
  300. } else {
  301. low = mid + 1;
  302. }
  303. }
  304. return high;
  305. }
  306. /**
  307. * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or `high` if there is none.
  308. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  309. *
  310. * WARNING: `high` should not be greater than the array's length.
  311. */
  312. function _lowerBinaryLookup(
  313. Checkpoint224[] storage self,
  314. uint32 key,
  315. uint256 low,
  316. uint256 high
  317. ) private view returns (uint256) {
  318. while (low < high) {
  319. uint256 mid = Math.average(low, high);
  320. if (_unsafeAccess(self, mid)._key < key) {
  321. low = mid + 1;
  322. } else {
  323. high = mid;
  324. }
  325. }
  326. return high;
  327. }
  328. /**
  329. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  330. */
  331. function _unsafeAccess(
  332. Checkpoint224[] storage self,
  333. uint256 pos
  334. ) private pure returns (Checkpoint224 storage result) {
  335. assembly {
  336. mstore(0, self.slot)
  337. result.slot := add(keccak256(0, 0x20), pos)
  338. }
  339. }
  340. struct Trace160 {
  341. Checkpoint160[] _checkpoints;
  342. }
  343. struct Checkpoint160 {
  344. uint96 _key;
  345. uint160 _value;
  346. }
  347. /**
  348. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  349. *
  350. * Returns previous value and new value.
  351. */
  352. function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) {
  353. return _insert(self._checkpoints, key, value);
  354. }
  355. /**
  356. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.
  357. */
  358. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  359. uint256 len = self._checkpoints.length;
  360. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  361. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  362. }
  363. /**
  364. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
  365. */
  366. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  367. uint256 len = self._checkpoints.length;
  368. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  369. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  370. }
  371. /**
  372. * @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.
  373. *
  374. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys).
  375. */
  376. function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) {
  377. uint256 len = self._checkpoints.length;
  378. uint256 low = 0;
  379. uint256 high = len;
  380. if (len > 5) {
  381. uint256 mid = len - Math.sqrt(len);
  382. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  383. high = mid;
  384. } else {
  385. low = mid + 1;
  386. }
  387. }
  388. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  389. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  390. }
  391. /**
  392. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  393. */
  394. function latest(Trace160 storage self) internal view returns (uint160) {
  395. uint256 pos = self._checkpoints.length;
  396. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  397. }
  398. /**
  399. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  400. * in the most recent checkpoint.
  401. */
  402. function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
  403. uint256 pos = self._checkpoints.length;
  404. if (pos == 0) {
  405. return (false, 0, 0);
  406. } else {
  407. Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  408. return (true, ckpt._key, ckpt._value);
  409. }
  410. }
  411. /**
  412. * @dev Returns the number of checkpoint.
  413. */
  414. function length(Trace160 storage self) internal view returns (uint256) {
  415. return self._checkpoints.length;
  416. }
  417. /**
  418. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  419. * or by updating the last one.
  420. */
  421. function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) {
  422. uint256 pos = self.length;
  423. if (pos > 0) {
  424. // Copying to memory is important here.
  425. Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
  426. // Checkpoint keys must be non-decreasing.
  427. require(last._key <= key, "Checkpoint: decreasing keys");
  428. // Update or push new checkpoint
  429. if (last._key == key) {
  430. _unsafeAccess(self, pos - 1)._value = value;
  431. } else {
  432. self.push(Checkpoint160({_key: key, _value: value}));
  433. }
  434. return (last._value, value);
  435. } else {
  436. self.push(Checkpoint160({_key: key, _value: value}));
  437. return (0, value);
  438. }
  439. }
  440. /**
  441. * @dev Return the index of the last (most recent) checkpoint with key lower or equal than the search key, or `high` if there is none.
  442. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  443. *
  444. * WARNING: `high` should not be greater than the array's length.
  445. */
  446. function _upperBinaryLookup(
  447. Checkpoint160[] storage self,
  448. uint96 key,
  449. uint256 low,
  450. uint256 high
  451. ) private view returns (uint256) {
  452. while (low < high) {
  453. uint256 mid = Math.average(low, high);
  454. if (_unsafeAccess(self, mid)._key > key) {
  455. high = mid;
  456. } else {
  457. low = mid + 1;
  458. }
  459. }
  460. return high;
  461. }
  462. /**
  463. * @dev Return the index of the first (oldest) checkpoint with key is greater or equal than the search key, or `high` if there is none.
  464. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  465. *
  466. * WARNING: `high` should not be greater than the array's length.
  467. */
  468. function _lowerBinaryLookup(
  469. Checkpoint160[] storage self,
  470. uint96 key,
  471. uint256 low,
  472. uint256 high
  473. ) private view returns (uint256) {
  474. while (low < high) {
  475. uint256 mid = Math.average(low, high);
  476. if (_unsafeAccess(self, mid)._key < key) {
  477. low = mid + 1;
  478. } else {
  479. high = mid;
  480. }
  481. }
  482. return high;
  483. }
  484. /**
  485. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  486. */
  487. function _unsafeAccess(
  488. Checkpoint160[] storage self,
  489. uint256 pos
  490. ) private pure returns (Checkpoint160 storage result) {
  491. assembly {
  492. mstore(0, self.slot)
  493. result.slot := add(keccak256(0, 0x20), pos)
  494. }
  495. }
  496. }