range.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package snap
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/holiman/uint256"
  21. )
  22. // hashRange is a utility to handle ranges of hashes, Split up the
  23. // hash-space into sections, and 'walk' over the sections
  24. type hashRange struct {
  25. current *uint256.Int
  26. step *uint256.Int
  27. }
  28. // newHashRange creates a new hashRange, initiated at the start position,
  29. // and with the step set to fill the desired 'num' chunks
  30. func newHashRange(start common.Hash, num uint64) *hashRange {
  31. left := new(big.Int).Sub(hashSpace, start.Big())
  32. step := new(big.Int).Div(
  33. new(big.Int).Add(left, new(big.Int).SetUint64(num-1)),
  34. new(big.Int).SetUint64(num),
  35. )
  36. step256 := new(uint256.Int)
  37. step256.SetFromBig(step)
  38. return &hashRange{
  39. current: new(uint256.Int).SetBytes32(start[:]),
  40. step: step256,
  41. }
  42. }
  43. // Next pushes the hash range to the next interval.
  44. func (r *hashRange) Next() bool {
  45. next, overflow := new(uint256.Int).AddOverflow(r.current, r.step)
  46. if overflow {
  47. return false
  48. }
  49. r.current = next
  50. return true
  51. }
  52. // Start returns the first hash in the current interval.
  53. func (r *hashRange) Start() common.Hash {
  54. return r.current.Bytes32()
  55. }
  56. // End returns the last hash in the current interval.
  57. func (r *hashRange) End() common.Hash {
  58. // If the end overflows (non divisible range), return a shorter interval
  59. next, overflow := new(uint256.Int).AddOverflow(r.current, r.step)
  60. if overflow {
  61. return common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
  62. }
  63. return next.SubUint64(next, 1).Bytes32()
  64. }
  65. // incHash returns the next hash, in lexicographical order (a.k.a plus one)
  66. func incHash(h common.Hash) common.Hash {
  67. var a uint256.Int
  68. a.SetBytes32(h[:])
  69. a.AddUint64(&a, 1)
  70. return common.Hash(a.Bytes32())
  71. }