Checkpoints.sol 15 KB

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