@@ -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
2628var 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.
5254func 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.
6567func (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.
7588func (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.
82113func (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