mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
trie: make range proof verifier return trie
This commit is contained in:
parent
7153c2368d
commit
cebe0b0c92
4 changed files with 49 additions and 46 deletions
|
|
@ -272,7 +272,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
|
|||
}
|
||||
// Verify the snapshot segment with range prover, ensure that all flat states
|
||||
// in this range correspond to merkle trie.
|
||||
cont, err := trie.VerifyRangeProof(root, origin, last, keys, vals, proof)
|
||||
cont, _, err := trie.VerifyRangeProof(root, origin, last, keys, vals, proof)
|
||||
return &proofResult{
|
||||
keys: keys,
|
||||
vals: vals,
|
||||
|
|
|
|||
|
|
@ -2404,7 +2404,7 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco
|
|||
if len(keys) > 0 {
|
||||
end = keys[len(keys)-1]
|
||||
}
|
||||
cont, err := trie.VerifyRangeProof(root, req.origin[:], end, keys, accounts, proofdb)
|
||||
cont, _, err := trie.VerifyRangeProof(root, req.origin[:], end, keys, accounts, proofdb)
|
||||
if err != nil {
|
||||
logger.Warn("Account range failed proof", "err", err)
|
||||
// Signal this request as failed, and ready for rescheduling
|
||||
|
|
@ -2664,7 +2664,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
|
|||
if len(keys) > 0 {
|
||||
end = keys[len(keys)-1]
|
||||
}
|
||||
cont, err = trie.VerifyRangeProof(req.roots[i], req.origin[:], end, keys, slots[i], proofdb)
|
||||
cont, _, err = trie.VerifyRangeProof(req.roots[i], req.origin[:], end, keys, slots[i], proofdb)
|
||||
if err != nil {
|
||||
s.scheduleRevertStorageRequest(req) // reschedule request
|
||||
logger.Warn("Storage range failed proof", "err", err)
|
||||
|
|
|
|||
|
|
@ -481,24 +481,24 @@ func hasRightElement(node node, key []byte) bool {
|
|||
// Note: This method does not verify that the proof is of minimal form. If the input
|
||||
// proofs are 'bloated' with neighbour leaves or random data, aside from the 'useful'
|
||||
// data, then the proof will still be accepted.
|
||||
func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, error) {
|
||||
func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, *Trie, error) {
|
||||
if proof == nil {
|
||||
// Special case, there is no edge proof at all. The given range is expected
|
||||
// to be the whole leaf-set in the trie.
|
||||
return false, VerifyStandaloneRange(rootHash, keys, values)
|
||||
return false, nil, VerifyStandaloneRange(rootHash, keys, values)
|
||||
}
|
||||
if len(keys) != len(values) {
|
||||
return false, fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
|
||||
return false, nil, fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
|
||||
}
|
||||
// Ensure the received batch is monotonic increasing and contains no deletions
|
||||
for i := 0; i < len(keys)-1; i++ {
|
||||
if bytes.Compare(keys[i], keys[i+1]) >= 0 {
|
||||
return false, errors.New("range is not monotonically increasing")
|
||||
return false, nil, errors.New("range is not monotonically increasing")
|
||||
}
|
||||
}
|
||||
for _, value := range values {
|
||||
if len(value) == 0 {
|
||||
return false, errors.New("range contains deletion")
|
||||
return false, nil, errors.New("range contains deletion")
|
||||
}
|
||||
}
|
||||
// Special case, there is a provided edge proof but zero key/value
|
||||
|
|
@ -506,56 +506,59 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
|
|||
if len(keys) == 0 {
|
||||
root, val, err := proofToPath(rootHash, nil, firstKey, proof, true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
if val != nil || hasRightElement(root, firstKey) {
|
||||
return false, errors.New("more entries available")
|
||||
return false, nil, errors.New("more entries available")
|
||||
}
|
||||
return false, nil
|
||||
tr := &Trie{root: root, reader: newEmptyReader(), tracer: newTracer()}
|
||||
return false, tr, nil
|
||||
}
|
||||
// Special case, there is only one element and two edge keys are same.
|
||||
// In this case, we can't construct two edge paths. So handle it here.
|
||||
if len(keys) == 1 && bytes.Equal(firstKey, lastKey) {
|
||||
root, val, err := proofToPath(rootHash, nil, firstKey, proof, false)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
if !bytes.Equal(firstKey, keys[0]) {
|
||||
return false, errors.New("correct proof but invalid key")
|
||||
return false, nil, errors.New("correct proof but invalid key")
|
||||
}
|
||||
if !bytes.Equal(val, values[0]) {
|
||||
return false, errors.New("correct proof but invalid data")
|
||||
return false, nil, errors.New("correct proof but invalid data")
|
||||
}
|
||||
return hasRightElement(root, firstKey), nil
|
||||
tr := &Trie{root: root, reader: newEmptyReader(), tracer: newTracer()}
|
||||
tr.Update(keys[0], values[0])
|
||||
return hasRightElement(root, firstKey), tr, nil
|
||||
}
|
||||
// Ok, in all other cases, we require two edge paths available.
|
||||
// First check the validity of edge keys.
|
||||
if bytes.Compare(firstKey, lastKey) >= 0 {
|
||||
return false, errors.New("invalid edge keys")
|
||||
return false, nil, errors.New("invalid edge keys")
|
||||
}
|
||||
// todo(rjl493456442) different length edge keys should be supported
|
||||
if len(firstKey) != len(lastKey) {
|
||||
return false, errors.New("inconsistent edge keys")
|
||||
return false, nil, errors.New("inconsistent edge keys")
|
||||
}
|
||||
// Convert the edge proofs to edge trie paths. Then we can
|
||||
// have the same tree architecture with the original one.
|
||||
// For the first edge proof, non-existent proof is allowed.
|
||||
root, _, err := proofToPath(rootHash, nil, firstKey, proof, true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
// Pass the root node here, the second path will be merged
|
||||
// with the first one. For the last edge proof, non-existent
|
||||
// proof is also allowed.
|
||||
root, _, err = proofToPath(rootHash, root, lastKey, proof, true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
// Remove all internal references. All the removed parts should
|
||||
// be re-filled(or re-constructed) by the given leaves range.
|
||||
empty, err := unsetInternal(root, firstKey, lastKey)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
// Rebuild the trie with the leaf stream, the shape of trie
|
||||
// should be same with the original one.
|
||||
|
|
@ -567,9 +570,9 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, key
|
|||
tr.Update(key, values[index])
|
||||
}
|
||||
if tr.Hash() != rootHash {
|
||||
return false, fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, tr.Hash())
|
||||
return false, nil, fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, tr.Hash())
|
||||
}
|
||||
return hasRightElement(tr.root, keys[len(keys)-1]), nil
|
||||
return hasRightElement(tr.root, keys[len(keys)-1]), tr, nil
|
||||
}
|
||||
|
||||
// VerifyStandaloneRange checks whether a trie built with the given leaf nodes
|
||||
|
|
|
|||
|
|
@ -191,7 +191,7 @@ func TestRangeProof(t *testing.T) {
|
|||
keys = append(keys, entries[i].k)
|
||||
vals = append(vals, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
|
||||
}
|
||||
|
|
@ -242,7 +242,7 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
|
|||
keys = append(keys, entries[i].k)
|
||||
vals = append(vals, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
|
||||
}
|
||||
|
|
@ -263,7 +263,7 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to verify whole rang with non-existent edges")
|
||||
}
|
||||
|
|
@ -298,7 +298,7 @@ func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, k[len(k)-1], k, v, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, k[len(k)-1], k, v, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected to detect the error, got nil")
|
||||
}
|
||||
|
|
@ -320,7 +320,7 @@ func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), k[0], last, k, v, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), k[0], last, k, v, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected to detect the error, got nil")
|
||||
}
|
||||
|
|
@ -344,7 +344,7 @@ func TestOneElementRangeProof(t *testing.T) {
|
|||
if err := trie.Prove(entries[start].k, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the first node %v", err)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), entries[start].k, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), entries[start].k, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -359,7 +359,7 @@ func TestOneElementRangeProof(t *testing.T) {
|
|||
if err := trie.Prove(entries[start].k, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), first, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), first, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -374,7 +374,7 @@ func TestOneElementRangeProof(t *testing.T) {
|
|||
if err := trie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), entries[start].k, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), entries[start].k, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -389,7 +389,7 @@ func TestOneElementRangeProof(t *testing.T) {
|
|||
if err := trie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -408,7 +408,7 @@ func TestOneElementRangeProof(t *testing.T) {
|
|||
if err := tinyTrie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(tinyTrie.Hash(), first, last, [][]byte{entry.k}, [][]byte{entry.v}, proof)
|
||||
_, _, err = VerifyRangeProof(tinyTrie.Hash(), first, last, [][]byte{entry.k}, [][]byte{entry.v}, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -443,7 +443,7 @@ func TestAllElementsProof(t *testing.T) {
|
|||
if err := trie.Prove(entries[len(entries)-1].k, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), k[0], k[len(k)-1], k, v, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), k[0], k[len(k)-1], k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -458,7 +458,7 @@ func TestAllElementsProof(t *testing.T) {
|
|||
if err := trie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), first, last, k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -491,7 +491,7 @@ func TestSingleSideRangeProof(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k[len(k)-1], k, v, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k[len(k)-1], k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -527,7 +527,7 @@ func TestReverseSingleSideRangeProof(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), k[0], last.Bytes(), k, v, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), k[0], last.Bytes(), k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -599,7 +599,7 @@ func TestBadRangeProof(t *testing.T) {
|
|||
index = mrand.Intn(end - start)
|
||||
vals[index] = nil
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("%d Case %d index %d range: (%d->%d) expect error, got nil", i, testcase, index, start, end-1)
|
||||
}
|
||||
|
|
@ -633,7 +633,7 @@ func TestGappedRangeProof(t *testing.T) {
|
|||
keys = append(keys, entries[i].k)
|
||||
vals = append(vals, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
if err == nil {
|
||||
t.Fatal("expect error, got nil")
|
||||
}
|
||||
|
|
@ -660,7 +660,7 @@ func TestSameSideProofs(t *testing.T) {
|
|||
if err := trie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error, got nil")
|
||||
}
|
||||
|
|
@ -676,7 +676,7 @@ func TestSameSideProofs(t *testing.T) {
|
|||
if err := trie.Prove(last, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the last node %v", err)
|
||||
}
|
||||
_, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
|
||||
_, _, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error, got nil")
|
||||
}
|
||||
|
|
@ -744,7 +744,7 @@ func TestHasRightElement(t *testing.T) {
|
|||
k = append(k, entries[i].k)
|
||||
v = append(v, entries[i].v)
|
||||
}
|
||||
hasMore, err := VerifyRangeProof(trie.Hash(), firstKey, lastKey, k, v, proof)
|
||||
hasMore, _, err := VerifyRangeProof(trie.Hash(), firstKey, lastKey, k, v, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
|
@ -777,7 +777,7 @@ func TestEmptyRangeProof(t *testing.T) {
|
|||
if err := trie.Prove(first, proof); err != nil {
|
||||
t.Fatalf("Failed to prove the first node %v", err)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), first, nil, nil, nil, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), first, nil, nil, nil, proof)
|
||||
if c.err && err == nil {
|
||||
t.Fatalf("Expected error, got nil")
|
||||
}
|
||||
|
|
@ -817,7 +817,7 @@ func TestBloatedProof(t *testing.T) {
|
|||
trie.Prove(keys[0], want)
|
||||
trie.Prove(keys[len(keys)-1], want)
|
||||
|
||||
if _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof); err != nil {
|
||||
if _, _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof); err != nil {
|
||||
t.Fatalf("expected bloated proof to succeed, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -860,7 +860,7 @@ func TestEmptyValueRangeProof(t *testing.T) {
|
|||
keys = append(keys, entries[i].k)
|
||||
vals = append(vals, entries[i].v)
|
||||
}
|
||||
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected failure on noop entry")
|
||||
}
|
||||
|
|
@ -1000,7 +1000,7 @@ func benchmarkVerifyRangeProof(b *testing.B, size int) {
|
|||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, values, proof)
|
||||
_, _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, values, proof)
|
||||
if err != nil {
|
||||
b.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
|
||||
}
|
||||
|
|
@ -1093,7 +1093,7 @@ func TestRangeProofKeysWithSharedPrefix(t *testing.T) {
|
|||
t.Fatalf("failed to prove end: %v", err)
|
||||
}
|
||||
|
||||
more, err := VerifyRangeProof(root, start, end, keys, vals, proof)
|
||||
more, _, err := VerifyRangeProof(root, start, end, keys, vals, proof)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to verify range proof: %v", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue