trie: remove parameter 'fromLevel' in Prove #27512 (#1165)

This removes the feature where top nodes of the proof can be elided.
It was intended to be used by the LES server, to save bandwidth
when the client had already fetched parts of the state and only needed
some extra nodes to complete the proof. Alas, it never got implemented
in the client.

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
This commit is contained in:
Daniel Liu 2026-01-29 13:59:02 +08:00 committed by GitHub
parent 222d180a6b
commit d12f9803eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 67 additions and 71 deletions

View file

@ -220,5 +220,5 @@ func (t *XDCXTrie) getSecKeyCache() map[string][]byte {
// nodes of the longest existing prefix of the key (at least the root node), ending // nodes of the longest existing prefix of the key (at least the root node), ending
// with the node that proves the absence of the key. // with the node that proves the absence of the key.
func (t *XDCXTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { func (t *XDCXTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
return t.trie.Prove(key, fromLevel, proofDb) return t.trie.Prove(key, proofDb)
} }

View file

@ -216,5 +216,5 @@ func (t *XDCXTrie) getSecKeyCache() map[string][]byte {
// nodes of the longest existing prefix of the key (at least the root node), ending // nodes of the longest existing prefix of the key (at least the root node), ending
// with the node that proves the absence of the key. // with the node that proves the absence of the key.
func (t *XDCXTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { func (t *XDCXTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
return t.trie.Prove(key, fromLevel, proofDb) return t.trie.Prove(key, proofDb)
} }

View file

@ -123,7 +123,7 @@ type Trie interface {
// If the trie does not contain a value for key, the returned proof contains all // If the trie does not contain a value for key, the returned proof contains all
// nodes of the longest existing prefix of the key (at least the root), ending // nodes of the longest existing prefix of the key (at least the root), ending
// with the node that proves the absence of the key. // with the node that proves the absence of the key.
Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error Prove(key []byte, proofDb ethdb.KeyValueWriter) error
} }
// NewDatabase creates a backing store for state. The returned database is safe for // NewDatabase creates a backing store for state. The returned database is safe for

View file

@ -340,7 +340,7 @@ func (api *BlockChainAPI) GetTransactionAndReceiptProof(ctx context.Context, has
return nil, err return nil, err
} }
var tx_proof proofPairList var tx_proof proofPairList
if err := tx_tr.Prove(keybuf.Bytes(), 0, &tx_proof); err != nil { if err := tx_tr.Prove(keybuf.Bytes(), &tx_proof); err != nil {
return nil, err return nil, err
} }
receipts, err := api.b.GetReceipts(ctx, blockHash) receipts, err := api.b.GetReceipts(ctx, blockHash)
@ -352,7 +352,7 @@ func (api *BlockChainAPI) GetTransactionAndReceiptProof(ctx context.Context, has
} }
receipt_tr := deriveTrie(receipts) receipt_tr := deriveTrie(receipts)
var receipt_proof proofPairList var receipt_proof proofPairList
if err := receipt_tr.Prove(keybuf.Bytes(), 0, &receipt_proof); err != nil { if err := receipt_tr.Prove(keybuf.Bytes(), &receipt_proof); err != nil {
return nil, err return nil, err
} }
fields := map[string]interface{}{ fields := map[string]interface{}{

View file

@ -56,7 +56,7 @@ func TestTransactionProof(t *testing.T) {
if err := rlp.Encode(keybuf, uint(i)); err != nil { if err := rlp.Encode(keybuf, uint(i)); err != nil {
t.Fatalf("rlp.Encode fail: %v", err) t.Fatalf("rlp.Encode fail: %v", err)
} }
if err := tr.Prove(keybuf.Bytes(), 0, &proof); err != nil { if err := tr.Prove(keybuf.Bytes(), &proof); err != nil {
t.Fatal("Prove err:", err) t.Fatal("Prove err:", err)
} }
// verify the proof // verify the proof
@ -91,7 +91,7 @@ func TestReceiptProof(t *testing.T) {
if err := rlp.Encode(keybuf, uint(i)); err != nil { if err := rlp.Encode(keybuf, uint(i)); err != nil {
t.Fatalf("rlp.Encode fail: %v", err) t.Fatalf("rlp.Encode fail: %v", err)
} }
if err := tr.Prove(keybuf.Bytes(), 0, &proof); err != nil { if err := tr.Prove(keybuf.Bytes(), &proof); err != nil {
t.Fatal("Prove err:", err) t.Fatal("Prove err:", err)
} }
// verify the proof // verify the proof

View file

@ -31,9 +31,9 @@ import (
// Node and can be retrieved by verifying the proof. // Node and can be retrieved by verifying the proof.
// //
// If the trie does not contain a value for key, the returned proof contains all // If the trie does not contain a value for key, the returned proof contains all
// nodes of the longest existing prefix of the key (at least the root Node), ending // nodes of the longest existing prefix of the key (at least the root node), ending
// with the Node that proves the absence of the key. // with the node that proves the absence of the key.
func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { func (t *Trie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
// Collect all nodes on the path to key. // Collect all nodes on the path to key.
var ( var (
prefix []byte prefix []byte
@ -81,10 +81,6 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) e
defer returnHasherToPool(hasher) defer returnHasherToPool(hasher)
for i, n := range nodes { for i, n := range nodes {
if fromLevel > 0 {
fromLevel--
continue
}
var hn node var hn node
n, hn = hasher.proofHash(n) n, hn = hasher.proofHash(n)
if hash, ok := hn.(hashNode); ok || i == 0 { if hash, ok := hn.(hashNode); ok || i == 0 {
@ -107,8 +103,8 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) e
// If the trie does not contain a value for key, the returned proof contains all // If the trie does not contain a value for key, the returned proof contains all
// nodes of the longest existing prefix of the key (at least the root node), ending // nodes of the longest existing prefix of the key (at least the root node), ending
// with the node that proves the absence of the key. // with the node that proves the absence of the key.
func (t *StateTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { func (t *StateTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
return t.trie.Prove(key, fromLevel, proofDb) return t.trie.Prove(key, proofDb)
} }
// VerifyProof checks merkle proofs. The given proof must contain the value for // VerifyProof checks merkle proofs. The given proof must contain the value for

View file

@ -57,7 +57,7 @@ func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database {
// Create a direct trie based Merkle prover // Create a direct trie based Merkle prover
provers = append(provers, func(key []byte) *memorydb.Database { provers = append(provers, func(key []byte) *memorydb.Database {
proof := memorydb.New() proof := memorydb.New()
trie.Prove(key, 0, proof) trie.Prove(key, proof)
return proof return proof
}) })
// Create a leaf iterator based Merkle prover // Create a leaf iterator based Merkle prover
@ -150,7 +150,7 @@ func TestMissingKeyProof(t *testing.T) {
for i, key := range []string{"a", "j", "l", "z"} { for i, key := range []string{"a", "j", "l", "z"} {
proof := memorydb.New() proof := memorydb.New()
trie.Prove([]byte(key), 0, proof) trie.Prove([]byte(key), proof)
if proof.Len() != 1 { if proof.Len() != 1 {
t.Errorf("test %d: proof should have one element", i) t.Errorf("test %d: proof should have one element", i)
@ -179,10 +179,10 @@ func TestRangeProof(t *testing.T) {
end := mrand.Intn(len(entries)-start) + start + 1 end := mrand.Intn(len(entries)-start) + start + 1
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[end-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -230,10 +230,10 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
if bytes.Compare(last, entries[end-1].k) < 0 { if bytes.Compare(last, entries[end-1].k) < 0 {
continue continue
} }
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -251,10 +251,10 @@ func TestRangeProofWithNonExistentProof(t *testing.T) {
proof := memorydb.New() proof := memorydb.New()
first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes()
last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var k [][]byte var k [][]byte
@ -285,10 +285,10 @@ func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
first := decreaseKey(common.CopyBytes(entries[start].k)) first := decreaseKey(common.CopyBytes(entries[start].k))
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[end-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
start = 105 // Gap created start = 105 // Gap created
@ -307,10 +307,10 @@ func TestRangeProofWithInvalidNonExistentProof(t *testing.T) {
start, end = 100, 200 start, end = 100, 200
last := increaseKey(common.CopyBytes(entries[end-1].k)) last := increaseKey(common.CopyBytes(entries[end-1].k))
proof = memorydb.New() proof = memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
end = 195 // Capped slice end = 195 // Capped slice
@ -341,7 +341,7 @@ func TestOneElementRangeProof(t *testing.T) {
// point to the SAME key. // point to the SAME key.
start := 1000 start := 1000
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) 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)
@ -353,10 +353,10 @@ func TestOneElementRangeProof(t *testing.T) {
start = 1000 start = 1000
first := decreaseKey(common.CopyBytes(entries[start].k)) first := decreaseKey(common.CopyBytes(entries[start].k))
proof = memorydb.New() proof = memorydb.New()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -368,10 +368,10 @@ func TestOneElementRangeProof(t *testing.T) {
start = 1000 start = 1000
last := increaseKey(common.CopyBytes(entries[start].k)) last := increaseKey(common.CopyBytes(entries[start].k))
proof = memorydb.New() proof = memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -383,10 +383,10 @@ func TestOneElementRangeProof(t *testing.T) {
start = 1000 start = 1000
first, last = decreaseKey(common.CopyBytes(entries[start].k)), increaseKey(common.CopyBytes(entries[start].k)) first, last = decreaseKey(common.CopyBytes(entries[start].k)), increaseKey(common.CopyBytes(entries[start].k))
proof = memorydb.New() proof = memorydb.New()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -402,10 +402,10 @@ func TestOneElementRangeProof(t *testing.T) {
first = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() first = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes()
last = entry.k last = entry.k
proof = memorydb.New() proof = memorydb.New()
if err := tinyTrie.Prove(first, 0, proof); err != nil { if err := tinyTrie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := tinyTrie.Prove(last, 0, proof); err != nil { if err := tinyTrie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -437,10 +437,10 @@ func TestAllElementsProof(t *testing.T) {
// With edge proofs, it should still work. // With edge proofs, it should still work.
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[0].k, 0, proof); err != nil { if err := trie.Prove(entries[0].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[len(entries)-1].k, 0, proof); err != nil { if err := trie.Prove(entries[len(entries)-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -452,10 +452,10 @@ func TestAllElementsProof(t *testing.T) {
proof = memorydb.New() proof = memorydb.New()
first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes()
last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -479,10 +479,10 @@ func TestSingleSideRangeProof(t *testing.T) {
var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1} var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
for _, pos := range cases { for _, pos := range cases {
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(common.Hash{}.Bytes(), 0, proof); err != nil { if err := trie.Prove(common.Hash{}.Bytes(), proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[pos].k, 0, proof); err != nil { if err := trie.Prove(entries[pos].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
k := make([][]byte, 0) k := make([][]byte, 0)
@ -514,11 +514,11 @@ func TestReverseSingleSideRangeProof(t *testing.T) {
var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1} var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1}
for _, pos := range cases { for _, pos := range cases {
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[pos].k, 0, proof); err != nil { if err := trie.Prove(entries[pos].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
if err := trie.Prove(last.Bytes(), 0, proof); err != nil { if err := trie.Prove(last.Bytes(), proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
k := make([][]byte, 0) k := make([][]byte, 0)
@ -549,10 +549,10 @@ func TestBadRangeProof(t *testing.T) {
start := mrand.Intn(len(entries)) start := mrand.Intn(len(entries))
end := mrand.Intn(len(entries)-start) + start + 1 end := mrand.Intn(len(entries)-start) + start + 1
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[end-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -618,10 +618,10 @@ func TestGappedRangeProof(t *testing.T) {
} }
first, last := 2, 8 first, last := 2, 8
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[first].k, 0, proof); err != nil { if err := trie.Prove(entries[first].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[last-1].k, 0, proof); err != nil { if err := trie.Prove(entries[last-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -654,10 +654,10 @@ func TestSameSideProofs(t *testing.T) {
last := decreaseKey(common.CopyBytes(entries[pos].k)) last := decreaseKey(common.CopyBytes(entries[pos].k))
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -670,10 +670,10 @@ func TestSameSideProofs(t *testing.T) {
last = increaseKey(last) last = increaseKey(last)
proof = memorydb.New() proof = memorydb.New()
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(last, 0, proof); err != nil { if err := trie.Prove(last, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) 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)
@ -718,23 +718,23 @@ func TestHasRightElement(t *testing.T) {
) )
if c.start == -1 { if c.start == -1 {
firstKey, start = common.Hash{}.Bytes(), 0 firstKey, start = common.Hash{}.Bytes(), 0
if err := trie.Prove(firstKey, 0, proof); err != nil { if err := trie.Prove(firstKey, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
} else { } else {
firstKey = entries[c.start].k firstKey = entries[c.start].k
if err := trie.Prove(entries[c.start].k, 0, proof); err != nil { if err := trie.Prove(entries[c.start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
} }
if c.end == -1 { if c.end == -1 {
lastKey, end = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes(), len(entries) lastKey, end = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes(), len(entries)
if err := trie.Prove(lastKey, 0, proof); err != nil { if err := trie.Prove(lastKey, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
} else { } else {
lastKey = entries[c.end-1].k lastKey = entries[c.end-1].k
if err := trie.Prove(entries[c.end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[c.end-1].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
} }
@ -774,7 +774,7 @@ func TestEmptyRangeProof(t *testing.T) {
for _, c := range cases { for _, c := range cases {
proof := memorydb.New() proof := memorydb.New()
first := increaseKey(common.CopyBytes(entries[c.pos].k)) first := increaseKey(common.CopyBytes(entries[c.pos].k))
if err := trie.Prove(first, 0, proof); err != nil { if err := trie.Prove(first, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) 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)
@ -805,7 +805,7 @@ func TestBloatedProof(t *testing.T) {
// In the 'malicious' case, we add proofs for every single item // In the 'malicious' case, we add proofs for every single item
// (but only one key/value pair used as leaf) // (but only one key/value pair used as leaf)
for i, entry := range entries { for i, entry := range entries {
trie.Prove(entry.k, 0, proof) trie.Prove(entry.k, proof)
if i == 50 { if i == 50 {
keys = append(keys, entry.k) keys = append(keys, entry.k)
vals = append(vals, entry.v) vals = append(vals, entry.v)
@ -814,8 +814,8 @@ func TestBloatedProof(t *testing.T) {
// For reference, we use the same function, but _only_ prove the first // For reference, we use the same function, but _only_ prove the first
// and last element // and last element
want := memorydb.New() want := memorydb.New()
trie.Prove(keys[0], 0, want) trie.Prove(keys[0], want)
trie.Prove(keys[len(keys)-1], 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) t.Fatalf("expected bloated proof to succeed, got %v", err)
@ -848,10 +848,10 @@ func TestEmptyValueRangeProof(t *testing.T) {
start, end := 1, len(entries)-1 start, end := 1, len(entries)-1
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
t.Fatalf("Failed to prove the first node %v", err) t.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[end-1].k, proof); err != nil {
t.Fatalf("Failed to prove the last node %v", err) t.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -943,7 +943,7 @@ func BenchmarkProve(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
kv := vals[keys[i%len(keys)]] kv := vals[keys[i%len(keys)]]
proofs := memorydb.New() proofs := memorydb.New()
if trie.Prove(kv.k, 0, proofs); proofs.Len() == 0 { if trie.Prove(kv.k, proofs); proofs.Len() == 0 {
b.Fatalf("zero length proof for %x", kv.k) b.Fatalf("zero length proof for %x", kv.k)
} }
} }
@ -957,7 +957,7 @@ func BenchmarkVerifyProof(b *testing.B) {
for k := range vals { for k := range vals {
keys = append(keys, k) keys = append(keys, k)
proof := memorydb.New() proof := memorydb.New()
trie.Prove([]byte(k), 0, proof) trie.Prove([]byte(k), proof)
proofs = append(proofs, proof) proofs = append(proofs, proof)
} }
@ -986,10 +986,10 @@ func benchmarkVerifyRangeProof(b *testing.B, size int) {
start := 2 start := 2
end := start + size end := start + size
proof := memorydb.New() proof := memorydb.New()
if err := trie.Prove(entries[start].k, 0, proof); err != nil { if err := trie.Prove(entries[start].k, proof); err != nil {
b.Fatalf("Failed to prove the first node %v", err) b.Fatalf("Failed to prove the first node %v", err)
} }
if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { if err := trie.Prove(entries[end-1].k, proof); err != nil {
b.Fatalf("Failed to prove the last node %v", err) b.Fatalf("Failed to prove the last node %v", err)
} }
var keys [][]byte var keys [][]byte
@ -1088,10 +1088,10 @@ func TestRangeProofKeysWithSharedPrefix(t *testing.T) {
proof := memorydb.New() proof := memorydb.New()
start := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000") start := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")
end := common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") end := common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
if err := trie.Prove(start, 0, proof); err != nil { if err := trie.Prove(start, proof); err != nil {
t.Fatalf("failed to prove start: %v", err) t.Fatalf("failed to prove start: %v", err)
} }
if err := trie.Prove(end, 0, proof); err != nil { if err := trie.Prove(end, proof); err != nil {
t.Fatalf("failed to prove end: %v", err) t.Fatalf("failed to prove end: %v", err)
} }

View file

@ -241,7 +241,7 @@ func TestAccessListLeak(t *testing.T) {
{ {
func(tr *Trie) { func(tr *Trie) {
for _, val := range standard { for _, val := range standard {
tr.Prove([]byte(val.k), 0, rawdb.NewMemoryDatabase()) tr.Prove([]byte(val.k), rawdb.NewMemoryDatabase())
} }
}, },
}, },

View file

@ -509,7 +509,7 @@ func runRandTest(rt randTest) error {
continue continue
} }
proofDb := rawdb.NewMemoryDatabase() proofDb := rawdb.NewMemoryDatabase()
err := tr.Prove(step.key, 0, proofDb) err := tr.Prove(step.key, proofDb)
if err != nil { if err != nil {
rt[i].err = fmt.Errorf("failed for proving key %#x, %v", step.key, err) rt[i].err = fmt.Errorf("failed for proving key %#x, %v", step.key, err)
} }