Checkpoints.sol 17 KB

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