trie: remove Key in MissingNodeError

The key was constructed from nibbles, which isn't possible for all
nodes. Remove the only use of Key in LightTrie by always retrying with
the original key that was looked up.
This commit is contained in:
Felix Lange 2017-04-18 13:08:17 +02:00
parent c7a4d9cf8a
commit a31d268b76
3 changed files with 4 additions and 17 deletions

@ -19,6 +19,7 @@ package light
import ( import (
"context" "context"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
) )
@ -46,26 +47,18 @@ func NewLightTrie(id *TrieID, odr OdrBackend, useFakeMap bool) *LightTrie {
// retrieveKey retrieves a single key, returns true and stores nodes in local // retrieveKey retrieves a single key, returns true and stores nodes in local
// database if successful // database if successful
func (t *LightTrie) retrieveKey(ctx context.Context, key []byte) bool { func (t *LightTrie) retrieveKey(ctx context.Context, key []byte) bool {
r := &TrieRequest{Id: t.id, Key: key} r := &TrieRequest{Id: t.id, Key: crypto.Keccak256(key)}
return t.odr.Retrieve(ctx, r) == nil return t.odr.Retrieve(ctx, r) == nil
} }
// do tries and retries to execute a function until it returns with no error or // do tries and retries to execute a function until it returns with no error or
// an error type other than MissingNodeError // an error type other than MissingNodeError
func (t *LightTrie) do(ctx context.Context, fallbackKey []byte, fn func() error) error { func (t *LightTrie) do(ctx context.Context, key []byte, fn func() error) error {
err := fn() err := fn()
for err != nil { for err != nil {
mn, ok := err.(*trie.MissingNodeError) if _, ok := err.(*trie.MissingNodeError); !ok {
if !ok {
return err return err
} }
var key []byte
if mn.PrefixLen+mn.SuffixLen > 0 {
key = mn.Key
} else {
key = fallbackKey
}
if !t.retrieveKey(ctx, key) { if !t.retrieveKey(ctx, key) {
break break
} }

@ -30,10 +30,6 @@ import (
// //
// RootHash is the original root of the trie that contains the node // RootHash is the original root of the trie that contains the node
// //
// Key is a binary-encoded key that contains the prefix that leads to the first
// missing node and optionally a suffix that hints on which further nodes should
// also be retrieved
//
// PrefixLen is the nibble length of the key prefix that leads from the root to // PrefixLen is the nibble length of the key prefix that leads from the root to
// the missing node // the missing node
// //
@ -42,7 +38,6 @@ import (
// such hints in the error message) // such hints in the error message)
type MissingNodeError struct { type MissingNodeError struct {
RootHash, NodeHash common.Hash RootHash, NodeHash common.Hash
Key []byte
PrefixLen, SuffixLen int PrefixLen, SuffixLen int
} }

@ -450,7 +450,6 @@ func (t *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) {
return nil, &MissingNodeError{ return nil, &MissingNodeError{
RootHash: t.originalRoot, RootHash: t.originalRoot,
NodeHash: common.BytesToHash(n), NodeHash: common.BytesToHash(n),
Key: compactHexEncode(append(prefix, suffix...)),
PrefixLen: len(prefix), PrefixLen: len(prefix),
SuffixLen: len(suffix), SuffixLen: len(suffix),
} }