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

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.
pull/27471/head^2
rjl493456442 1 year ago committed by GitHub
parent 091c25d983
commit ceca4578ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      core/state/database.go
  2. 4
      core/state/snapshot/generate.go
  3. 4
      core/state/statedb.go
  4. 8
      eth/protocols/snap/handler.go
  5. 16
      eth/protocols/snap/sync_test.go
  6. 2
      internal/ethapi/api.go
  7. 8
      les/handler_test.go
  8. 4
      les/server_requests.go
  9. 2
      light/odr_test.go
  10. 2
      light/trie.go
  11. 4
      tests/fuzzers/rangeproof/rangeproof-fuzzer.go
  12. 2
      tests/fuzzers/trie/trie-fuzzer.go
  13. 10
      trie/proof.go
  14. 106
      trie/proof_test.go
  15. 2
      trie/tracer_test.go
  16. 2
      trie/trie_test.go

@ -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

@ -256,7 +256,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
if origin == nil { if origin == nil {
origin = common.Hash{}.Bytes() origin = common.Hash{}.Bytes()
} }
if err := tr.Prove(origin, 0, proof); err != nil { if err := tr.Prove(origin, proof); err != nil {
log.Debug("Failed to prove range", "kind", kind, "origin", origin, "err", err) log.Debug("Failed to prove range", "kind", kind, "origin", origin, "err", err)
return &proofResult{ return &proofResult{
keys: keys, keys: keys,
@ -267,7 +267,7 @@ func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix [
}, nil }, nil
} }
if last != nil { if last != nil {
if err := tr.Prove(last, 0, proof); err != nil { if err := tr.Prove(last, proof); err != nil {
log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err) log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err)
return &proofResult{ return &proofResult{
keys: keys, keys: keys,

@ -332,7 +332,7 @@ func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
// GetProofByHash returns the Merkle proof for a given account. // GetProofByHash returns the Merkle proof for a given account.
func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) { func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
var proof proofList var proof proofList
err := s.trie.Prove(addrHash[:], 0, &proof) err := s.trie.Prove(addrHash[:], &proof)
return proof, err return proof, err
} }
@ -346,7 +346,7 @@ func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte,
return nil, errors.New("storage trie for requested address does not exist") return nil, errors.New("storage trie for requested address does not exist")
} }
var proof proofList var proof proofList
err = trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) err = trie.Prove(crypto.Keccak256(key.Bytes()), &proof)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -322,12 +322,12 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
// Generate the Merkle proofs for the first and last account // Generate the Merkle proofs for the first and last account
proof := light.NewNodeSet() proof := light.NewNodeSet()
if err := tr.Prove(req.Origin[:], 0, proof); err != nil { if err := tr.Prove(req.Origin[:], proof); err != nil {
log.Warn("Failed to prove account range", "origin", req.Origin, "err", err) log.Warn("Failed to prove account range", "origin", req.Origin, "err", err)
return nil, nil return nil, nil
} }
if last != (common.Hash{}) { if last != (common.Hash{}) {
if err := tr.Prove(last[:], 0, proof); err != nil { if err := tr.Prove(last[:], proof); err != nil {
log.Warn("Failed to prove account range", "last", last, "err", err) log.Warn("Failed to prove account range", "last", last, "err", err)
return nil, nil return nil, nil
} }
@ -428,12 +428,12 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
return nil, nil return nil, nil
} }
proof := light.NewNodeSet() proof := light.NewNodeSet()
if err := stTrie.Prove(origin[:], 0, proof); err != nil { if err := stTrie.Prove(origin[:], proof); err != nil {
log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err) log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err)
return nil, nil return nil, nil
} }
if last != (common.Hash{}) { if last != (common.Hash{}) {
if err := stTrie.Prove(last[:], 0, proof); err != nil { if err := stTrie.Prove(last[:], proof); err != nil {
log.Warn("Failed to prove storage range", "last", last, "err", err) log.Warn("Failed to prove storage range", "last", last, "err", err)
return nil, nil return nil, nil
} }

@ -273,12 +273,12 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H
// Actually, we need to supply proofs either way! This seems to be an implementation // Actually, we need to supply proofs either way! This seems to be an implementation
// quirk in go-ethereum // quirk in go-ethereum
proof := light.NewNodeSet() proof := light.NewNodeSet()
if err := t.accountTrie.Prove(origin[:], 0, proof); err != nil { if err := t.accountTrie.Prove(origin[:], proof); err != nil {
t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err) t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err)
} }
if len(keys) > 0 { if len(keys) > 0 {
lastK := (keys[len(keys)-1])[:] lastK := (keys[len(keys)-1])[:]
if err := t.accountTrie.Prove(lastK, 0, proof); err != nil { if err := t.accountTrie.Prove(lastK, proof); err != nil {
t.logger.Error("Could not prove last item", "error", err) t.logger.Error("Could not prove last item", "error", err)
} }
} }
@ -358,12 +358,12 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
// Here's a potential gotcha: when constructing the proof, we cannot // Here's a potential gotcha: when constructing the proof, we cannot
// use the 'origin' slice directly, but must use the full 32-byte // use the 'origin' slice directly, but must use the full 32-byte
// hash form. // hash form.
if err := stTrie.Prove(originHash[:], 0, proof); err != nil { if err := stTrie.Prove(originHash[:], proof); err != nil {
t.logger.Error("Could not prove inexistence of origin", "origin", originHash, "error", err) t.logger.Error("Could not prove inexistence of origin", "origin", originHash, "error", err)
} }
if len(keys) > 0 { if len(keys) > 0 {
lastK := (keys[len(keys)-1])[:] lastK := (keys[len(keys)-1])[:]
if err := stTrie.Prove(lastK, 0, proof); err != nil { if err := stTrie.Prove(lastK, proof); err != nil {
t.logger.Error("Could not prove last item", "error", err) t.logger.Error("Could not prove last item", "error", err)
} }
} }
@ -416,13 +416,13 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
// Here's a potential gotcha: when constructing the proof, we cannot // Here's a potential gotcha: when constructing the proof, we cannot
// use the 'origin' slice directly, but must use the full 32-byte // use the 'origin' slice directly, but must use the full 32-byte
// hash form. // hash form.
if err := stTrie.Prove(origin[:], 0, proof); err != nil { if err := stTrie.Prove(origin[:], proof); err != nil {
t.logger.Error("Could not prove inexistence of origin", "origin", origin, t.logger.Error("Could not prove inexistence of origin", "origin", origin,
"error", err) "error", err)
} }
if len(keys) > 0 { if len(keys) > 0 {
lastK := (keys[len(keys)-1])[:] lastK := (keys[len(keys)-1])[:]
if err := stTrie.Prove(lastK, 0, proof); err != nil { if err := stTrie.Prove(lastK, proof); err != nil {
t.logger.Error("Could not prove last item", "error", err) t.logger.Error("Could not prove last item", "error", err)
} }
} }
@ -594,12 +594,12 @@ func TestSyncBloatedProof(t *testing.T) {
} }
// The proofs // The proofs
proof := light.NewNodeSet() proof := light.NewNodeSet()
if err := t.accountTrie.Prove(origin[:], 0, proof); err != nil { if err := t.accountTrie.Prove(origin[:], proof); err != nil {
t.logger.Error("Could not prove origin", "origin", origin, "error", err) t.logger.Error("Could not prove origin", "origin", origin, "error", err)
} }
// The bloat: add proof of every single element // The bloat: add proof of every single element
for _, entry := range t.accountValues { for _, entry := range t.accountValues {
if err := t.accountTrie.Prove(entry.k, 0, proof); err != nil { if err := t.accountTrie.Prove(entry.k, proof); err != nil {
t.logger.Error("Could not prove item", "error", err) t.logger.Error("Could not prove item", "error", err)
} }
} }

@ -705,7 +705,7 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
continue continue
} }
var proof proofList var proof proofList
if err := storageTrie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof); err != nil { if err := storageTrie.Prove(crypto.Keccak256(key.Bytes()), &proof); err != nil {
return nil, err return nil, err
} }
storageProof[i] = StorageResult{storageKeys[i], storageProof[i] = StorageResult{storageKeys[i],

@ -414,7 +414,7 @@ func testGetProofs(t *testing.T, protocol int) {
Key: crypto.Keccak256(acc[:]), Key: crypto.Keccak256(acc[:]),
} }
proofreqs = append(proofreqs, req) proofreqs = append(proofreqs, req)
trie.Prove(crypto.Keccak256(acc[:]), 0, proofsV2) trie.Prove(crypto.Keccak256(acc[:]), proofsV2)
} }
} }
// Send the proof request and verify the response // Send the proof request and verify the response
@ -458,7 +458,7 @@ func testGetStaleProof(t *testing.T, protocol int) {
if wantOK { if wantOK {
proofsV2 := light.NewNodeSet() proofsV2 := light.NewNodeSet()
t, _ := trie.New(trie.StateTrieID(header.Root), trie.NewDatabase(server.db)) t, _ := trie.New(trie.StateTrieID(header.Root), trie.NewDatabase(server.db))
t.Prove(account, 0, proofsV2) t.Prove(account, proofsV2)
expected = proofsV2.NodeList() expected = proofsV2.NodeList()
} }
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil { if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil {
@ -514,7 +514,7 @@ func testGetCHTProofs(t *testing.T, protocol int) {
} }
root := light.GetChtRoot(server.db, 0, bc.GetHeaderByNumber(config.ChtSize-1).Hash()) root := light.GetChtRoot(server.db, 0, bc.GetHeaderByNumber(config.ChtSize-1).Hash())
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.ChtTablePrefix)))) trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.ChtTablePrefix))))
trie.Prove(key, 0, &proofsV2.Proofs) trie.Prove(key, &proofsV2.Proofs)
// Assemble the requests for the different protocols // Assemble the requests for the different protocols
requestsV2 := []HelperTrieReq{{ requestsV2 := []HelperTrieReq{{
Type: htCanonical, Type: htCanonical,
@ -579,7 +579,7 @@ func testGetBloombitsProofs(t *testing.T, protocol int) {
root := light.GetBloomTrieRoot(server.db, 0, bc.GetHeaderByNumber(config.BloomTrieSize-1).Hash()) root := light.GetBloomTrieRoot(server.db, 0, bc.GetHeaderByNumber(config.BloomTrieSize-1).Hash())
trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.BloomTrieTablePrefix)))) trie, _ := trie.New(trie.TrieID(root), trie.NewDatabase(rawdb.NewTable(server.db, string(rawdb.BloomTrieTablePrefix))))
trie.Prove(key, 0, &proofs.Proofs) trie.Prove(key, &proofs.Proofs)
// Send the proof request and verify the response // Send the proof request and verify the response
sendRequest(rawPeer.app, GetHelperTrieProofsMsg, 42, requests) sendRequest(rawPeer.app, GetHelperTrieProofsMsg, 42, requests)

@ -436,7 +436,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
} }
} }
// Prove the user's request from the account or storage trie // Prove the user's request from the account or storage trie
if err := trie.Prove(request.Key, request.FromLevel, nodes); err != nil { if err := trie.Prove(request.Key, nodes); err != nil {
p.Log().Warn("Failed to prove state request", "block", header.Number, "hash", header.Hash(), "err", err) p.Log().Warn("Failed to prove state request", "block", header.Number, "hash", header.Hash(), "err", err)
continue continue
} }
@ -480,7 +480,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
// the headers with no valid proof. Keep the compatibility for // the headers with no valid proof. Keep the compatibility for
// legacy les protocol and drop this hack when the les2/3 are // legacy les protocol and drop this hack when the les2/3 are
// not supported. // not supported.
err := auxTrie.Prove(request.Key, request.FromLevel, nodes) err := auxTrie.Prove(request.Key, nodes)
if p.version >= lpv4 && err != nil { if p.version >= lpv4 && err != nil {
return nil return nil
} }

@ -95,7 +95,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
panic(err) panic(err)
} }
nodes := NewNodeSet() nodes := NewNodeSet()
t.Prove(req.Key, 0, nodes) t.Prove(req.Key, nodes)
req.Proof = nodes req.Proof = nodes
case *CodeRequest: case *CodeRequest:
req.Data = rawdb.ReadCode(odr.sdb, req.Hash) req.Data = rawdb.ReadCode(odr.sdb, req.Hash)

@ -192,7 +192,7 @@ func (t *odrTrie) GetKey(sha []byte) []byte {
return nil return nil
} }
func (t *odrTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { func (t *odrTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
return errors.New("not implemented, needs client/server interface split") return errors.New("not implemented, needs client/server interface split")
} }

@ -113,10 +113,10 @@ func (f *fuzzer) fuzz() int {
break break
} }
proof := memorydb.New() proof := memorydb.New()
if err := tr.Prove(entries[start].k, 0, proof); err != nil { if err := tr.Prove(entries[start].k, proof); err != nil {
panic(fmt.Sprintf("Failed to prove the first node %v", err)) panic(fmt.Sprintf("Failed to prove the first node %v", err))
} }
if err := tr.Prove(entries[end-1].k, 0, proof); err != nil { if err := tr.Prove(entries[end-1].k, proof); err != nil {
panic(fmt.Sprintf("Failed to prove the last node %v", err)) panic(fmt.Sprintf("Failed to prove the last node %v", err))
} }
var keys [][]byte var keys [][]byte

@ -187,7 +187,7 @@ func runRandTest(rt randTest) error {
return errors.New("hash mismatch in opItercheckhash") return errors.New("hash mismatch in opItercheckhash")
} }
case opProve: case opProve:
rt[i].err = tr.Prove(step.key, 0, proofDb{}) rt[i].err = tr.Prove(step.key, proofDb{})
} }
// Abort the test on error. // Abort the test on error.
if rt[i].err != nil { if rt[i].err != nil {

@ -33,7 +33,7 @@ import (
// 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

@ -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)
} }

@ -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())
} }
}, },
}, },

@ -490,7 +490,7 @@ func runRandTest(rt randTest) bool {
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)
} }

Loading…
Cancel
Save