Skip to content

Commit f62c58f

Browse files
authored
trie: make rhs-proof align with last key in range proofs (#28311)
During snap-sync, we request ranges of values: either a range of accounts or a range of storage values. For any large trie, e.g. the main account trie or a large storage trie, we cannot fetch everything at once. Short version; we split it up and request in multiple stages. To do so, we use an origin field, to say "Give me all storage key/values where key > 0x20000000000000000". When the server fulfils this, the server provides the first key after origin, let's say 0x2e030000000000000 -- never providing the exact origin. However, the client-side needs to be able to verify that the 0x2e03.. indeed is the first one after 0x2000.., and therefore the attached proof concerns the origin, not the first key. So, short-short version: the left-hand side of the proof relates to the origin, and is free-standing from the first leaf. On the other hand, (pun intended), the right-hand side, there's no such 'gap' between "along what path does the proof walk" and the last provided leaf. The proof must prove the last element (unless there are no elements). Therefore, we can simplify the semantics for trie.VerifyRangeProof by removing an argument. This doesn't make much difference in practice, but makes it so that we can remove some tests. The reason I am raising this is that the upcoming stacktrie-based verifier does not support such fancy features as standalone right-hand borders.
1 parent 31b566f commit f62c58f

File tree

6 files changed

+47
-168
lines changed

6 files changed

+47
-168
lines changed

cmd/devp2p/internal/ethtest/snap.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,7 @@ func (s *Suite) snapGetAccountRange(t *utesting.T, tc *accRangeTest) error {
536536
}
537537
proofdb := nodes.Set()
538538

539-
var end []byte
540-
if len(keys) > 0 {
541-
end = keys[len(keys)-1]
542-
}
543-
_, err = trie.VerifyRangeProof(tc.root, tc.origin[:], end, keys, accounts, proofdb)
539+
_, err = trie.VerifyRangeProof(tc.root, tc.origin[:], keys, accounts, proofdb)
544540
return err
545541
}
546542

core/state/snapshot/generate.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
247247
ctx.stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
248248
return nil, errMissingTrie
249249
}
250-
// Firstly find out the key of last iterated element.
251-
var last []byte
252-
if len(keys) > 0 {
253-
last = keys[len(keys)-1]
254-
}
255250
// Generate the Merkle proofs for the first and last element
256251
if origin == nil {
257252
origin = common.Hash{}.Bytes()
@@ -266,9 +261,9 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
266261
tr: tr,
267262
}, nil
268263
}
269-
if last != nil {
270-
if err := tr.Prove(last, proof); err != nil {
271-
log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err)
264+
if len(keys) > 0 {
265+
if err := tr.Prove(keys[len(keys)-1], proof); err != nil {
266+
log.Debug("Failed to prove range", "kind", kind, "last", keys[len(keys)-1], "err", err)
272267
return &proofResult{
273268
keys: keys,
274269
vals: vals,
@@ -280,7 +275,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
280275
}
281276
// Verify the snapshot segment with range prover, ensure that all flat states
282277
// in this range correspond to merkle trie.
283-
cont, err := trie.VerifyRangeProof(root, origin, last, keys, vals, proof)
278+
cont, err := trie.VerifyRangeProof(root, origin, keys, vals, proof)
284279
return &proofResult{
285280
keys: keys,
286281
vals: vals,

eth/protocols/snap/sync.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,13 +2401,7 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco
24012401
for i, node := range proof {
24022402
nodes[i] = node
24032403
}
2404-
proofdb := nodes.Set()
2405-
2406-
var end []byte
2407-
if len(keys) > 0 {
2408-
end = keys[len(keys)-1]
2409-
}
2410-
cont, err := trie.VerifyRangeProof(root, req.origin[:], end, keys, accounts, proofdb)
2404+
cont, err := trie.VerifyRangeProof(root, req.origin[:], keys, accounts, nodes.Set())
24112405
if err != nil {
24122406
logger.Warn("Account range failed proof", "err", err)
24132407
// Signal this request as failed, and ready for rescheduling
@@ -2659,7 +2653,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
26592653
if len(nodes) == 0 {
26602654
// No proof has been attached, the response must cover the entire key
26612655
// space and hash to the origin root.
2662-
_, err = trie.VerifyRangeProof(req.roots[i], nil, nil, keys, slots[i], nil)
2656+
_, err = trie.VerifyRangeProof(req.roots[i], nil, keys, slots[i], nil)
26632657
if err != nil {
26642658
s.scheduleRevertStorageRequest(req) // reschedule request
26652659
logger.Warn("Storage slots failed proof", "err", err)
@@ -2670,11 +2664,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
26702664
// returned data is indeed part of the storage trie
26712665
proofdb := nodes.Set()
26722666

2673-
var end []byte
2674-
if len(keys) > 0 {
2675-
end = keys[len(keys)-1]
2676-
}
2677-
cont, err = trie.VerifyRangeProof(req.roots[i], req.origin[:], end, keys, slots[i], proofdb)
2667+
cont, err = trie.VerifyRangeProof(req.roots[i], req.origin[:], keys, slots[i], proofdb)
26782668
if err != nil {
26792669
s.scheduleRevertStorageRequest(req) // reschedule request
26802670
logger.Warn("Storage range failed proof", "err", err)

tests/fuzzers/rangeproof/rangeproof-fuzzer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (f *fuzzer) fuzz() int {
128128
if len(keys) == 0 {
129129
return 0
130130
}
131-
var first, last = keys[0], keys[len(keys)-1]
131+
var first = keys[0]
132132
testcase %= 6
133133
switch testcase {
134134
case 0:
@@ -165,7 +165,7 @@ func (f *fuzzer) fuzz() int {
165165
}
166166
ok = 1
167167
//nodes, subtrie
168-
hasMore, err := trie.VerifyRangeProof(tr.Hash(), first, last, keys, vals, proof)
168+
hasMore, err := trie.VerifyRangeProof(tr.Hash(), first, keys, vals, proof)
169169
if err != nil {
170170
if hasMore {
171171
panic("err != nil && hasMore == true")

trie/proof.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func hasRightElement(node node, key []byte) bool {
481481
// Note: This method does not verify that the proof is of minimal form. If the input
482482
// proofs are 'bloated' with neighbour leaves or random data, aside from the 'useful'
483483
// data, then the proof will still be accepted.
484-
func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, error) {
484+
func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, error) {
485485
if len(keys) != len(values) {
486486
return false, fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
487487
}
@@ -520,6 +520,7 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
520520
}
521521
return false, nil
522522
}
523+
var lastKey = keys[len(keys)-1]
523524
// Special case, there is only one element and two edge keys are same.
524525
// In this case, we can't construct two edge paths. So handle it here.
525526
if len(keys) == 1 && bytes.Equal(firstKey, lastKey) {

0 commit comments

Comments
 (0)