Checkpoints.sol 14 KB

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