Checkpoints.sol 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./math/Math.sol";
  4. import "./math/SafeCast.sol";
  5. /**
  6. * @dev This library defines the `History` struct, for checkpointing values as they change at different points in
  7. * time, and later looking up past values by block number. See {Votes} as an example.
  8. *
  9. * To create a history of checkpoints define a variable type `Checkpoints.History` in your contract, and store a new
  10. * checkpoint for the current transaction block using the {push} function.
  11. *
  12. * _Available since v4.5._
  13. */
  14. library Checkpoints {
  15. struct Checkpoint {
  16. uint32 _blockNumber;
  17. uint224 _value;
  18. }
  19. struct History {
  20. Checkpoint[] _checkpoints;
  21. }
  22. /**
  23. * @dev Returns the value in the latest checkpoint, or zero if there are no checkpoints.
  24. */
  25. function latest(History storage self) internal view returns (uint256) {
  26. uint256 pos = self._checkpoints.length;
  27. return pos == 0 ? 0 : self._checkpoints[pos - 1]._value;
  28. }
  29. /**
  30. * @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
  31. * before it is returned, or zero otherwise.
  32. */
  33. function getAtBlock(History storage self, uint256 blockNumber) internal view returns (uint256) {
  34. require(blockNumber < block.number, "Checkpoints: block not yet mined");
  35. uint256 high = self._checkpoints.length;
  36. uint256 low = 0;
  37. while (low < high) {
  38. uint256 mid = Math.average(low, high);
  39. if (self._checkpoints[mid]._blockNumber > blockNumber) {
  40. high = mid;
  41. } else {
  42. low = mid + 1;
  43. }
  44. }
  45. return high == 0 ? 0 : self._checkpoints[high - 1]._value;
  46. }
  47. /**
  48. * @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
  49. *
  50. * Returns previous value and new value.
  51. */
  52. function push(History storage self, uint256 value) internal returns (uint256, uint256) {
  53. uint256 pos = self._checkpoints.length;
  54. uint256 old = latest(self);
  55. if (pos > 0 && self._checkpoints[pos - 1]._blockNumber == block.number) {
  56. self._checkpoints[pos - 1]._value = SafeCast.toUint224(value);
  57. } else {
  58. self._checkpoints.push(
  59. Checkpoint({_blockNumber: SafeCast.toUint32(block.number), _value: SafeCast.toUint224(value)})
  60. );
  61. }
  62. return (old, value);
  63. }
  64. /**
  65. * @dev Pushes a value onto a History, by updating the latest value using binary operation `op`. The new value will
  66. * be set to `op(latest, delta)`.
  67. *
  68. * Returns previous value and new value.
  69. */
  70. function push(
  71. History storage self,
  72. function(uint256, uint256) view returns (uint256) op,
  73. uint256 delta
  74. ) internal returns (uint256, uint256) {
  75. return push(self, op(latest(self), delta));
  76. }
  77. }