Skip to content

Commit 96d8674

Browse files
committed
Merge pull request bnb-chain#2005 from zsfelfoldi/light-trie
Trie error handling
2 parents 23031b1 + 52904ae commit 96d8674

7 files changed

Lines changed: 343 additions & 68 deletions

File tree

‎trie/arc.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ func newARC(c int) *arc {
6262
}
6363
}
6464

65+
// Clear clears the cache
66+
func (a *arc) Clear() {
67+
a.mutex.Lock()
68+
defer a.mutex.Unlock()
69+
a.p = 0
70+
a.t1 = list.New()
71+
a.b1 = list.New()
72+
a.t2 = list.New()
73+
a.b2 = list.New()
74+
a.cache = make(map[string]*entry, a.c)
75+
}
76+
6577
// Put inserts a new key-value pair into the cache.
6678
// This optimizes future access to this entry (side effect).
6779
func (a *arc) Put(key hashNode, value node) bool {

‎trie/errors.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package trie
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
)
24+
25+
// MissingNodeError is returned by the trie functions (TryGet, TryUpdate, TryDelete)
26+
// in the case where a trie node is not present in the local database. Contains
27+
// information necessary for retrieving the missing node through an ODR service.
28+
//
29+
// NodeHash is the hash of the missing node
30+
// RootHash is the original root of the trie that contains the node
31+
// KeyPrefix is the prefix that leads from the root to the missing node (hex encoded)
32+
// KeySuffix (optional) contains the rest of the key we were looking for, gives a
33+
// hint on which further nodes should also be retrieved (hex encoded)
34+
type MissingNodeError struct {
35+
RootHash, NodeHash common.Hash
36+
KeyPrefix, KeySuffix []byte
37+
}
38+
39+
func (err *MissingNodeError) Error() string {
40+
return fmt.Sprintf("Missing trie node %064x", err.NodeHash)
41+
}

‎trie/iterator.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package trie
1818

19-
import "bytes"
19+
import (
20+
"bytes"
21+
22+
"github.com/ethereum/go-ethereum/logger"
23+
"github.com/ethereum/go-ethereum/logger/glog"
24+
)
2025

2126
type Iterator struct {
2227
trie *Trie
@@ -100,7 +105,11 @@ func (self *Iterator) next(node interface{}, key []byte, isIterStart bool) []byt
100105
}
101106

102107
case hashNode:
103-
return self.next(self.trie.resolveHash(node), key, isIterStart)
108+
rn, err := self.trie.resolveHash(node, nil, nil)
109+
if err != nil && glog.V(logger.Error) {
110+
glog.Errorf("Unhandled trie error: %v", err)
111+
}
112+
return self.next(rn, key, isIterStart)
104113
}
105114
return nil
106115
}
@@ -127,7 +136,11 @@ func (self *Iterator) key(node interface{}) []byte {
127136
}
128137
}
129138
case hashNode:
130-
return self.key(self.trie.resolveHash(node))
139+
rn, err := self.trie.resolveHash(node, nil, nil)
140+
if err != nil && glog.V(logger.Error) {
141+
glog.Errorf("Unhandled trie error: %v", err)
142+
}
143+
return self.key(rn)
131144
}
132145

133146
return nil

‎trie/proof.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/crypto/sha3"
10+
"github.com/ethereum/go-ethereum/logger"
11+
"github.com/ethereum/go-ethereum/logger/glog"
1012
"github.com/ethereum/go-ethereum/rlp"
1113
)
1214

@@ -39,7 +41,14 @@ func (t *Trie) Prove(key []byte) []rlp.RawValue {
3941
case nil:
4042
return nil
4143
case hashNode:
42-
tn = t.resolveHash(n)
44+
var err error
45+
tn, err = t.resolveHash(n, nil, nil)
46+
if err != nil {
47+
if glog.V(logger.Error) {
48+
glog.Errorf("Unhandled trie error: %v", err)
49+
}
50+
return nil
51+
}
4352
default:
4453
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
4554
}

‎trie/secure_trie.go‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/crypto/sha3"
24+
"github.com/ethereum/go-ethereum/logger"
25+
"github.com/ethereum/go-ethereum/logger/glog"
2426
)
2527

2628
var secureKeyPrefix = []byte("secure-key-")
@@ -46,8 +48,8 @@ type SecureTrie struct {
4648
// NewSecure creates a trie with an existing root node from db.
4749
//
4850
// If root is the zero hash or the sha3 hash of an empty string, the
49-
// trie is initially empty. Otherwise, New will panics if db is nil
50-
// and returns ErrMissingRoot if the root node cannpt be found.
51+
// trie is initially empty. Otherwise, New will panic if db is nil
52+
// and returns MissingNodeError if the root node cannot be found.
5153
// Accessing the trie loads nodes from db on demand.
5254
func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
5355
if db == nil {
@@ -63,7 +65,18 @@ func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
6365
// Get returns the value for key stored in the trie.
6466
// The value bytes must not be modified by the caller.
6567
func (t *SecureTrie) Get(key []byte) []byte {
66-
return t.Trie.Get(t.hashKey(key))
68+
res, err := t.TryGet(key)
69+
if err != nil && glog.V(logger.Error) {
70+
glog.Errorf("Unhandled trie error: %v", err)
71+
}
72+
return res
73+
}
74+
75+
// TryGet returns the value for key stored in the trie.
76+
// The value bytes must not be modified by the caller.
77+
// If a node was not found in the database, a MissingNodeError is returned.
78+
func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
79+
return t.Trie.TryGet(t.hashKey(key))
6780
}
6881

6982
// Update associates key with value in the trie. Subsequent calls to
@@ -73,14 +86,40 @@ func (t *SecureTrie) Get(key []byte) []byte {
7386
// The value bytes must not be modified by the caller while they are
7487
// stored in the trie.
7588
func (t *SecureTrie) Update(key, value []byte) {
89+
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
90+
glog.Errorf("Unhandled trie error: %v", err)
91+
}
92+
}
93+
94+
// TryUpdate associates key with value in the trie. Subsequent calls to
95+
// Get will return value. If value has length zero, any existing value
96+
// is deleted from the trie and calls to Get will return nil.
97+
//
98+
// The value bytes must not be modified by the caller while they are
99+
// stored in the trie.
100+
//
101+
// If a node was not found in the database, a MissingNodeError is returned.
102+
func (t *SecureTrie) TryUpdate(key, value []byte) error {
76103
hk := t.hashKey(key)
77-
t.Trie.Update(hk, value)
104+
err := t.Trie.TryUpdate(hk, value)
105+
if err != nil {
106+
return err
107+
}
78108
t.Trie.db.Put(t.secKey(hk), key)
109+
return nil
79110
}
80111

81112
// Delete removes any existing value for key from the trie.
82113
func (t *SecureTrie) Delete(key []byte) {
83-
t.Trie.Delete(t.hashKey(key))
114+
if err := t.TryDelete(key); err != nil && glog.V(logger.Error) {
115+
glog.Errorf("Unhandled trie error: %v", err)
116+
}
117+
}
118+
119+
// TryDelete removes any existing value for key from the trie.
120+
// If a node was not found in the database, a MissingNodeError is returned.
121+
func (t *SecureTrie) TryDelete(key []byte) error {
122+
return t.Trie.TryDelete(t.hashKey(key))
84123
}
85124

86125
// GetKey returns the sha3 preimage of a hashed key that was

0 commit comments

Comments
 (0)