Checkpoints.sol 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (utils/Checkpoints.sol)
  3. pragma solidity ^0.8.0;
  4. import "./math/Math.sol";
  5. import "./math/SafeCast.sol";
  6. /**
  7. * @dev This library defines the `History` 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.History` 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. struct Checkpoint {
  17. uint32 _blockNumber;
  18. uint224 _value;
  19. }
  20. struct History {
  21. Checkpoint[] _checkpoints;
  22. }
  23. /**
  24. * @dev Returns the value in the latest checkpoint, or zero if there are no checkpoints.
  25. */
  26. function latest(History storage self) internal view returns (uint256) {
  27. uint256 pos = self._checkpoints.length;
  28. return pos == 0 ? 0 : self._checkpoints[pos - 1]._value;
  29. }
  30. /**
  31. * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
  32. * before it is returned, or zero otherwise.
  33. */
  34. function getAtBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
  35. require(blockNumber < block.number, "Checkpoints: block not yet mined");
  36. uint256 high = self._checkpoints.length;
  37. uint256 low = 0;
  38. while (low < high) {
  39. uint256 mid = Math.average(low, high);
  40. if (self._checkpoints[mid]._blockNumber > blockNumber) {
  41. high = mid;
  42. } else {
  43. low = mid + 1;
  44. }
  45. }
  46. return high == 0 ? 0 : self._checkpoints[high - 1]._value;
  47. }
  48. /**
  49. * @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
  50. *
  51. * Returns previous value and new value.
  52. */
  53. function push(History storage self, uint256 value) internal returns (uint256, uint256) {
  54. uint256 pos = self._checkpoints.length;
  55. uint256 old = latest(self);
  56. if (pos > 0 && self._checkpoints[pos - 1]._blockNumber == block.number) {
  57. self._checkpoints[pos - 1]._value = SafeCast.toUint224(value);
  58. } else {
  59. self._checkpoints.push(
  60. Checkpoint({_blockNumber: SafeCast.toUint32(block.number), _value: SafeCast.toUint224(value)})
  61. );
  62. }
  63. return (old, value);
  64. }
  65. /**
  66. * @dev Pushes a value onto a History, by updating the latest value using binary operation `op`. The new value will
  67. * be set to `op(latest, delta)`.
  68. *
  69. * Returns previous value and new value.
  70. */
  71. function push(
  72. History storage self,
  73. function(uint256, uint256) view returns (uint256) op,
  74. uint256 delta
  75. ) internal returns (uint256, uint256) {
  76. return push(self, op(latest(self), delta));
  77. }
  78. }