123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.8.0-rc.2) (utils/Checkpoints.sol)
- // This file was procedurally generated from scripts/generate/templates/Checkpoints.js.
- pragma solidity ^0.8.0;
- import "./math/Math.sol";
- import "./math/SafeCast.sol";
- /**
- * @dev This library defines the `History` struct, for checkpointing values as they change at different points in
- * time, and later looking up past values by block number. See {Votes} as an example.
- *
- * To create a history of checkpoints define a variable type `Checkpoints.History` in your contract, and store a new
- * checkpoint for the current transaction block using the {push} function.
- *
- * _Available since v4.5._
- */
- library Checkpoints {
- struct History {
- Checkpoint[] _checkpoints;
- }
- struct Checkpoint {
- uint32 _blockNumber;
- uint224 _value;
- }
- /**
- * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
- * before it is returned, or zero otherwise.
- */
- function getAtBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
- require(blockNumber < block.number, "Checkpoints: block not yet mined");
- uint32 key = SafeCast.toUint32(blockNumber);
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
- * before it is returned, or zero otherwise. Similar to {upperLookup} but optimized for the case when the searched
- * checkpoint is probably "recent", defined as being among the last sqrt(N) checkpoints where N is the number of
- * checkpoints.
- */
- function getAtProbablyRecentBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
- require(blockNumber < block.number, "Checkpoints: block not yet mined");
- uint32 key = SafeCast.toUint32(blockNumber);
- uint256 len = self._checkpoints.length;
- uint256 low = 0;
- uint256 high = len;
- if (len > 5) {
- uint256 mid = len - Math.sqrt(len);
- if (key < _unsafeAccess(self._checkpoints, mid)._blockNumber) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
- *
- * Returns previous value and new value.
- */
- function push(History storage self, uint256 value) internal returns (uint256, uint256) {
- return _insert(self._checkpoints, SafeCast.toUint32(block.number), SafeCast.toUint224(value));
- }
- /**
- * @dev Pushes a value onto a History, by updating the latest value using binary operation `op`. The new value will
- * be set to `op(latest, delta)`.
- *
- * Returns previous value and new value.
- */
- function push(
- History storage self,
- function(uint256, uint256) view returns (uint256) op,
- uint256 delta
- ) internal returns (uint256, uint256) {
- return push(self, op(latest(self), delta));
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(History storage self) internal view returns (uint224) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(History storage self)
- internal
- view
- returns (
- bool exists,
- uint32 _blockNumber,
- uint224 _value
- )
- {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._blockNumber, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoint.
- */
- function length(History storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint[] storage self,
- uint32 key,
- uint224 value
- ) private returns (uint224, uint224) {
- uint256 pos = self.length;
- if (pos > 0) {
- // Copying to memory is important here.
- Checkpoint memory last = _unsafeAccess(self, pos - 1);
- // Checkpoints keys must be increasing.
- require(last._blockNumber <= key, "Checkpoint: invalid key");
- // Update or push new checkpoint
- if (last._blockNumber == key) {
- _unsafeAccess(self, pos - 1)._value = value;
- } else {
- self.push(Checkpoint({_blockNumber: key, _value: value}));
- }
- return (last._value, value);
- } else {
- self.push(Checkpoint({_blockNumber: key, _value: value}));
- return (0, value);
- }
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _upperBinaryLookup(
- Checkpoint[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._blockNumber > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _lowerBinaryLookup(
- Checkpoint[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._blockNumber < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- function _unsafeAccess(Checkpoint[] storage self, uint256 pos) private pure returns (Checkpoint storage result) {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- struct Trace224 {
- Checkpoint224[] _checkpoints;
- }
- struct Checkpoint224 {
- uint32 _key;
- uint224 _value;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
- *
- * Returns previous value and new value.
- */
- function push(
- Trace224 storage self,
- uint32 key,
- uint224 value
- ) internal returns (uint224, uint224) {
- return _insert(self._checkpoints, key, value);
- }
- /**
- * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
- */
- function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
- return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
- */
- function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(Trace224 storage self) internal view returns (uint224) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(Trace224 storage self)
- internal
- view
- returns (
- bool exists,
- uint32 _key,
- uint224 _value
- )
- {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint224 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._key, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoint.
- */
- function length(Trace224 storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint224[] storage self,
- uint32 key,
- uint224 value
- ) private returns (uint224, uint224) {
- uint256 pos = self.length;
- if (pos > 0) {
- // Copying to memory is important here.
- Checkpoint224 memory last = _unsafeAccess(self, pos - 1);
- // Checkpoints keys must be increasing.
- require(last._key <= key, "Checkpoint: invalid key");
- // Update or push new checkpoint
- if (last._key == key) {
- _unsafeAccess(self, pos - 1)._value = value;
- } else {
- self.push(Checkpoint224({_key: key, _value: value}));
- }
- return (last._value, value);
- } else {
- self.push(Checkpoint224({_key: key, _value: value}));
- return (0, value);
- }
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _upperBinaryLookup(
- Checkpoint224[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _lowerBinaryLookup(
- Checkpoint224[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- function _unsafeAccess(Checkpoint224[] storage self, uint256 pos)
- private
- pure
- returns (Checkpoint224 storage result)
- {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- struct Trace160 {
- Checkpoint160[] _checkpoints;
- }
- struct Checkpoint160 {
- uint96 _key;
- uint160 _value;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
- *
- * Returns previous value and new value.
- */
- function push(
- Trace160 storage self,
- uint96 key,
- uint160 value
- ) internal returns (uint160, uint160) {
- return _insert(self._checkpoints, key, value);
- }
- /**
- * @dev Returns the value in the oldest checkpoint with key greater or equal than the search key, or zero if there is none.
- */
- function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
- return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint with key lower or equal than the search key.
- */
- function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(Trace160 storage self) internal view returns (uint160) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(Trace160 storage self)
- internal
- view
- returns (
- bool exists,
- uint96 _key,
- uint160 _value
- )
- {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint160 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._key, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoint.
- */
- function length(Trace160 storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint160[] storage self,
- uint96 key,
- uint160 value
- ) private returns (uint160, uint160) {
- uint256 pos = self.length;
- if (pos > 0) {
- // Copying to memory is important here.
- Checkpoint160 memory last = _unsafeAccess(self, pos - 1);
- // Checkpoints keys must be increasing.
- require(last._key <= key, "Checkpoint: invalid key");
- // Update or push new checkpoint
- if (last._key == key) {
- _unsafeAccess(self, pos - 1)._value = value;
- } else {
- self.push(Checkpoint160({_key: key, _value: value}));
- }
- return (last._value, value);
- } else {
- self.push(Checkpoint160({_key: key, _value: value}));
- return (0, value);
- }
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _upperBinaryLookup(
- Checkpoint160[] storage self,
- uint96 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @dev Return the index of the oldest checkpoint whose key is greater or equal than the search key, or `high` if there is none.
- * `low` and `high` define a section where to do the search, with inclusive `low` and exclusive `high`.
- *
- * WARNING: `high` should not be greater than the array's length.
- */
- function _lowerBinaryLookup(
- Checkpoint160[] storage self,
- uint96 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- function _unsafeAccess(Checkpoint160[] storage self, uint256 pos)
- private
- pure
- returns (Checkpoint160 storage result)
- {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- }
|