Checkpoints.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.19;
  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. /**
  18. * @dev A value was attempted to be inserted on a past checkpoint.
  19. */
  20. error CheckpointUnorderedInsertion();
  21. struct Trace224 {
  22. Checkpoint224[] _checkpoints;
  23. }
  24. struct Checkpoint224 {
  25. uint32 _key;
  26. uint224 _value;
  27. }
  28. /**
  29. * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
  30. *
  31. * Returns previous value and new value.
  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 there is none.
  38. */
  39. function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  40. uint256 len = self._checkpoints.length;
  41. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  42. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  43. }
  44. /**
  45. * @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.
  46. */
  47. function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
  48. uint256 len = self._checkpoints.length;
  49. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  50. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  51. }
  52. /**
  53. * @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.
  54. *
  55. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys).
  56. */
  57. function upperLookupRecent(Trace224 storage self, uint32 key) internal view returns (uint224) {
  58. uint256 len = self._checkpoints.length;
  59. uint256 low = 0;
  60. uint256 high = len;
  61. if (len > 5) {
  62. uint256 mid = len - Math.sqrt(len);
  63. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  64. high = mid;
  65. } else {
  66. low = mid + 1;
  67. }
  68. }
  69. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  70. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  71. }
  72. /**
  73. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  74. */
  75. function latest(Trace224 storage self) internal view returns (uint224) {
  76. uint256 pos = self._checkpoints.length;
  77. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  78. }
  79. /**
  80. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  81. * in the most recent checkpoint.
  82. */
  83. function latestCheckpoint(Trace224 storage self) internal view returns (bool exists, uint32 _key, uint224 _value) {
  84. uint256 pos = self._checkpoints.length;
  85. if (pos == 0) {
  86. return (false, 0, 0);
  87. } else {
  88. Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  89. return (true, ckpt._key, ckpt._value);
  90. }
  91. }
  92. /**
  93. * @dev Returns the number of checkpoint.
  94. */
  95. function length(Trace224 storage self) internal view returns (uint256) {
  96. return self._checkpoints.length;
  97. }
  98. /**
  99. * @dev Returns checkpoint at given position.
  100. */
  101. function at(Trace224 storage self, uint32 pos) internal view returns (Checkpoint224 memory) {
  102. return self._checkpoints[pos];
  103. }
  104. /**
  105. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  106. * or by updating the last one.
  107. */
  108. function _insert(Checkpoint224[] storage self, uint32 key, uint224 value) private returns (uint224, uint224) {
  109. uint256 pos = self.length;
  110. if (pos > 0) {
  111. // Copying to memory is important here.
  112. Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
  113. // Checkpoint keys must be non-decreasing.
  114. if (last._key > key) {
  115. revert CheckpointUnorderedInsertion();
  116. }
  117. // Update or push new checkpoint
  118. if (last._key == key) {
  119. _unsafeAccess(self, pos - 1)._value = value;
  120. } else {
  121. self.push(Checkpoint224({_key: key, _value: value}));
  122. }
  123. return (last._value, value);
  124. } else {
  125. self.push(Checkpoint224({_key: key, _value: value}));
  126. return (0, value);
  127. }
  128. }
  129. /**
  130. * @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.
  131. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  132. *
  133. * WARNING: `high` should not be greater than the array's length.
  134. */
  135. function _upperBinaryLookup(
  136. Checkpoint224[] storage self,
  137. uint32 key,
  138. uint256 low,
  139. uint256 high
  140. ) private view returns (uint256) {
  141. while (low < high) {
  142. uint256 mid = Math.average(low, high);
  143. if (_unsafeAccess(self, mid)._key > key) {
  144. high = mid;
  145. } else {
  146. low = mid + 1;
  147. }
  148. }
  149. return high;
  150. }
  151. /**
  152. * @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.
  153. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  154. *
  155. * WARNING: `high` should not be greater than the array's length.
  156. */
  157. function _lowerBinaryLookup(
  158. Checkpoint224[] storage self,
  159. uint32 key,
  160. uint256 low,
  161. uint256 high
  162. ) private view returns (uint256) {
  163. while (low < high) {
  164. uint256 mid = Math.average(low, high);
  165. if (_unsafeAccess(self, mid)._key < key) {
  166. low = mid + 1;
  167. } else {
  168. high = mid;
  169. }
  170. }
  171. return high;
  172. }
  173. /**
  174. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  175. */
  176. function _unsafeAccess(
  177. Checkpoint224[] storage self,
  178. uint256 pos
  179. ) private pure returns (Checkpoint224 storage result) {
  180. assembly {
  181. mstore(0, self.slot)
  182. result.slot := add(keccak256(0, 0x20), pos)
  183. }
  184. }
  185. struct Trace160 {
  186. Checkpoint160[] _checkpoints;
  187. }
  188. struct Checkpoint160 {
  189. uint96 _key;
  190. uint160 _value;
  191. }
  192. /**
  193. * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
  194. *
  195. * Returns previous value and new value.
  196. */
  197. function push(Trace160 storage self, uint96 key, uint160 value) internal returns (uint160, uint160) {
  198. return _insert(self._checkpoints, key, value);
  199. }
  200. /**
  201. * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.
  202. */
  203. function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  204. uint256 len = self._checkpoints.length;
  205. uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
  206. return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
  207. }
  208. /**
  209. * @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.
  210. */
  211. function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
  212. uint256 len = self._checkpoints.length;
  213. uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
  214. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  215. }
  216. /**
  217. * @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.
  218. *
  219. * NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys).
  220. */
  221. function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) {
  222. uint256 len = self._checkpoints.length;
  223. uint256 low = 0;
  224. uint256 high = len;
  225. if (len > 5) {
  226. uint256 mid = len - Math.sqrt(len);
  227. if (key < _unsafeAccess(self._checkpoints, mid)._key) {
  228. high = mid;
  229. } else {
  230. low = mid + 1;
  231. }
  232. }
  233. uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
  234. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  235. }
  236. /**
  237. * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
  238. */
  239. function latest(Trace160 storage self) internal view returns (uint160) {
  240. uint256 pos = self._checkpoints.length;
  241. return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
  242. }
  243. /**
  244. * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
  245. * in the most recent checkpoint.
  246. */
  247. function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
  248. uint256 pos = self._checkpoints.length;
  249. if (pos == 0) {
  250. return (false, 0, 0);
  251. } else {
  252. Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
  253. return (true, ckpt._key, ckpt._value);
  254. }
  255. }
  256. /**
  257. * @dev Returns the number of checkpoint.
  258. */
  259. function length(Trace160 storage self) internal view returns (uint256) {
  260. return self._checkpoints.length;
  261. }
  262. /**
  263. * @dev Returns checkpoint at given position.
  264. */
  265. function at(Trace160 storage self, uint32 pos) internal view returns (Checkpoint160 memory) {
  266. return self._checkpoints[pos];
  267. }
  268. /**
  269. * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
  270. * or by updating the last one.
  271. */
  272. function _insert(Checkpoint160[] storage self, uint96 key, uint160 value) private returns (uint160, uint160) {
  273. uint256 pos = self.length;
  274. if (pos > 0) {
  275. // Copying to memory is important here.
  276. Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
  277. // Checkpoint keys must be non-decreasing.
  278. if (last._key > key) {
  279. revert CheckpointUnorderedInsertion();
  280. }
  281. // Update or push new checkpoint
  282. if (last._key == key) {
  283. _unsafeAccess(self, pos - 1)._value = value;
  284. } else {
  285. self.push(Checkpoint160({_key: key, _value: value}));
  286. }
  287. return (last._value, value);
  288. } else {
  289. self.push(Checkpoint160({_key: key, _value: value}));
  290. return (0, value);
  291. }
  292. }
  293. /**
  294. * @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.
  295. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  296. *
  297. * WARNING: `high` should not be greater than the array's length.
  298. */
  299. function _upperBinaryLookup(
  300. Checkpoint160[] storage self,
  301. uint96 key,
  302. uint256 low,
  303. uint256 high
  304. ) private view returns (uint256) {
  305. while (low < high) {
  306. uint256 mid = Math.average(low, high);
  307. if (_unsafeAccess(self, mid)._key > key) {
  308. high = mid;
  309. } else {
  310. low = mid + 1;
  311. }
  312. }
  313. return high;
  314. }
  315. /**
  316. * @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.
  317. * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
  318. *
  319. * WARNING: `high` should not be greater than the array's length.
  320. */
  321. function _lowerBinaryLookup(
  322. Checkpoint160[] storage self,
  323. uint96 key,
  324. uint256 low,
  325. uint256 high
  326. ) private view returns (uint256) {
  327. while (low < high) {
  328. uint256 mid = Math.average(low, high);
  329. if (_unsafeAccess(self, mid)._key < key) {
  330. low = mid + 1;
  331. } else {
  332. high = mid;
  333. }
  334. }
  335. return high;
  336. }
  337. /**
  338. * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
  339. */
  340. function _unsafeAccess(
  341. Checkpoint160[] storage self,
  342. uint256 pos
  343. ) private pure returns (Checkpoint160 storage result) {
  344. assembly {
  345. mstore(0, self.slot)
  346. result.slot := add(keccak256(0, 0x20), pos)
  347. }
  348. }
  349. }