Checkpoints.sol 18 KB

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