Checkpoints.sol 14 KB

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