Checkpoints.sol 14 KB

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