2021-04-27 17:19:59 +03:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package snap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/holiman/uint256"
|
|
|
|
)
|
|
|
|
|
|
|
|
// hashRange is a utility to handle ranges of hashes, Split up the
|
|
|
|
// hash-space into sections, and 'walk' over the sections
|
|
|
|
type hashRange struct {
|
|
|
|
current *uint256.Int
|
|
|
|
step *uint256.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// newHashRange creates a new hashRange, initiated at the start position,
|
|
|
|
// and with the step set to fill the desired 'num' chunks
|
|
|
|
func newHashRange(start common.Hash, num uint64) *hashRange {
|
|
|
|
left := new(big.Int).Sub(hashSpace, start.Big())
|
|
|
|
step := new(big.Int).Div(
|
|
|
|
new(big.Int).Add(left, new(big.Int).SetUint64(num-1)),
|
|
|
|
new(big.Int).SetUint64(num),
|
|
|
|
)
|
|
|
|
step256 := new(uint256.Int)
|
|
|
|
step256.SetFromBig(step)
|
|
|
|
|
|
|
|
return &hashRange{
|
2021-05-10 13:35:07 +02:00
|
|
|
current: new(uint256.Int).SetBytes32(start[:]),
|
2021-04-27 17:19:59 +03:00
|
|
|
step: step256,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next pushes the hash range to the next interval.
|
|
|
|
func (r *hashRange) Next() bool {
|
2021-05-10 13:35:07 +02:00
|
|
|
next, overflow := new(uint256.Int).AddOverflow(r.current, r.step)
|
|
|
|
if overflow {
|
2021-04-27 17:19:59 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.current = next
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start returns the first hash in the current interval.
|
|
|
|
func (r *hashRange) Start() common.Hash {
|
|
|
|
return r.current.Bytes32()
|
|
|
|
}
|
|
|
|
|
|
|
|
// End returns the last hash in the current interval.
|
|
|
|
func (r *hashRange) End() common.Hash {
|
|
|
|
// If the end overflows (non divisible range), return a shorter interval
|
2021-05-10 13:35:07 +02:00
|
|
|
next, overflow := new(uint256.Int).AddOverflow(r.current, r.step)
|
|
|
|
if overflow {
|
eth/protocols/snap: fix snap sync failure on empty storage range (#28306)
This change addresses an issue in snap sync, specifically when the entire sync process can be halted due to an encountered empty storage range.
Currently, on the snap sync client side, the response to an empty (partial) storage range is discarded as a non-delivery. However, this response can be a valid response, when the particular range requested does not contain any slots.
For instance, consider a large contract where the entire key space is divided into 16 chunks, and there are no available slots in the last chunk [0xf] -> [end]. When the node receives a request for this particular range, the response includes:
The proof with origin [0xf]
A nil storage slot set
If we simply discard this response, the finalization of the last range will be skipped, halting the entire sync process indefinitely. The test case TestSyncWithUnevenStorage can reproduce the scenario described above.
In addition, this change also defines the common variables MaxAddress and MaxHash.
2023-10-13 15:08:26 +08:00
|
|
|
return common.MaxHash
|
2021-04-27 17:19:59 +03:00
|
|
|
}
|
2021-05-10 13:35:07 +02:00
|
|
|
return next.SubUint64(next, 1).Bytes32()
|
2021-04-27 17:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// incHash returns the next hash, in lexicographical order (a.k.a plus one)
|
|
|
|
func incHash(h common.Hash) common.Hash {
|
2021-05-10 13:35:07 +02:00
|
|
|
var a uint256.Int
|
|
|
|
a.SetBytes32(h[:])
|
|
|
|
a.AddUint64(&a, 1)
|
2021-04-27 17:19:59 +03:00
|
|
|
return common.Hash(a.Bytes32())
|
|
|
|
}
|