@@ -35,14 +35,14 @@ func NewSecure(owner common.Hash, root common.Hash, db *Database) (*SecureTrie,
35
35
return NewStateTrie (owner , root , db )
36
36
}
37
37
38
- // StateTrie wraps a trie with key hashing. In a secure trie, all
38
+ // StateTrie wraps a trie with key hashing. In a stateTrie trie, all
39
39
// access operations hash the key using keccak256. This prevents
40
40
// calling code from creating long chains of nodes that
41
41
// increase the access time.
42
42
//
43
43
// Contrary to a regular trie, a StateTrie can only be created with
44
44
// New and must have an attached database. The database also stores
45
- // the preimage of each key.
45
+ // the preimage of each key if preimage recording is enabled .
46
46
//
47
47
// StateTrie is not safe for concurrent use.
48
48
type StateTrie struct {
@@ -53,17 +53,11 @@ type StateTrie struct {
53
53
secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
54
54
}
55
55
56
- // NewStateTrie creates a trie with an existing root node from a backing database
57
- // and optional intermediate in-memory node pool.
56
+ // NewStateTrie creates a trie with an existing root node from a backing database.
58
57
//
59
58
// If root is the zero hash or the sha3 hash of an empty string, the
60
59
// trie is initially empty. Otherwise, New will panic if db is nil
61
60
// and returns MissingNodeError if the root node cannot be found.
62
- //
63
- // Accessing the trie loads nodes from the database or node pool on demand.
64
- // Loaded nodes are kept around until their 'cache generation' expires.
65
- // A new cache generation is created by each call to Commit.
66
- // cachelimit sets the number of past cache generations to keep.
67
61
func NewStateTrie (owner common.Hash , root common.Hash , db * Database ) (* StateTrie , error ) {
68
62
if db == nil {
69
63
panic ("trie.NewSecure called without a database" )
@@ -87,63 +81,46 @@ func (t *StateTrie) Get(key []byte) []byte {
87
81
88
82
// TryGet returns the value for key stored in the trie.
89
83
// The value bytes must not be modified by the caller.
90
- // If a node was not found in the database, a MissingNodeError is returned.
84
+ // If the specified node is not in the trie, nil will be returned.
85
+ // If a trie node is not found in the database, a MissingNodeError is returned.
91
86
func (t * StateTrie ) TryGet (key []byte ) ([]byte , error ) {
92
87
return t .trie .TryGet (t .hashKey (key ))
93
88
}
94
89
90
+ // TryGetAccount attempts to retrieve an account with provided trie path.
91
+ // If the specified account is not in the trie, nil will be returned.
92
+ // If a trie node is not found in the database, a MissingNodeError is returned.
95
93
func (t * StateTrie ) TryGetAccount (key []byte ) (* types.StateAccount , error ) {
96
- var ret types.StateAccount
97
94
res , err := t .trie .TryGet (t .hashKey (key ))
98
- if err != nil {
99
- log .Error (fmt .Sprintf ("Unhandled trie error: %v" , err ))
100
- return & ret , err
101
- }
102
- if res == nil {
103
- return nil , nil
95
+ if res == nil || err != nil {
96
+ return nil , err
104
97
}
105
- err = rlp .DecodeBytes (res , & ret )
106
- return & ret , err
98
+ ret := new (types.StateAccount )
99
+ err = rlp .DecodeBytes (res , ret )
100
+ return ret , err
107
101
}
108
102
109
103
// TryGetAccountWithPreHashedKey does the same thing as TryGetAccount, however
110
104
// it expects a key that is already hashed. This constitutes an abstraction leak,
111
105
// since the client code needs to know the key format.
112
106
func (t * StateTrie ) TryGetAccountWithPreHashedKey (key []byte ) (* types.StateAccount , error ) {
113
- var ret types.StateAccount
114
107
res , err := t .trie .TryGet (key )
115
- if err != nil {
116
- log .Error (fmt .Sprintf ("Unhandled trie error: %v" , err ))
117
- return & ret , err
118
- }
119
- if res == nil {
120
- return nil , nil
108
+ if res == nil || err != nil {
109
+ return nil , err
121
110
}
122
- err = rlp .DecodeBytes (res , & ret )
123
- return & ret , err
111
+ ret := new (types.StateAccount )
112
+ err = rlp .DecodeBytes (res , ret )
113
+ return ret , err
124
114
}
125
115
126
116
// TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
127
117
// possible to use keybyte-encoding as the path might contain odd nibbles.
118
+ // If the specified trie node is not in the trie, nil will be returned.
119
+ // If a trie node is not found in the database, a MissingNodeError is returned.
128
120
func (t * StateTrie ) TryGetNode (path []byte ) ([]byte , int , error ) {
129
121
return t .trie .TryGetNode (path )
130
122
}
131
123
132
- // TryUpdateAccount account will abstract the write of an account to the
133
- // secure trie.
134
- func (t * StateTrie ) TryUpdateAccount (key []byte , acc * types.StateAccount ) error {
135
- hk := t .hashKey (key )
136
- data , err := rlp .EncodeToBytes (acc )
137
- if err != nil {
138
- return err
139
- }
140
- if err := t .trie .TryUpdate (hk , data ); err != nil {
141
- return err
142
- }
143
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
144
- return nil
145
- }
146
-
147
124
// Update associates key with value in the trie. Subsequent calls to
148
125
// Get will return value. If value has length zero, any existing value
149
126
// is deleted from the trie and calls to Get will return nil.
@@ -163,7 +140,7 @@ func (t *StateTrie) Update(key, value []byte) {
163
140
// The value bytes must not be modified by the caller while they are
164
141
// stored in the trie.
165
142
//
166
- // If a node was not found in the database, a MissingNodeError is returned.
143
+ // If a node is not found in the database, a MissingNodeError is returned.
167
144
func (t * StateTrie ) TryUpdate (key , value []byte ) error {
168
145
hk := t .hashKey (key )
169
146
err := t .trie .TryUpdate (hk , value )
@@ -174,6 +151,21 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
174
151
return nil
175
152
}
176
153
154
+ // TryUpdateAccount account will abstract the write of an account to the
155
+ // secure trie.
156
+ func (t * StateTrie ) TryUpdateAccount (key []byte , acc * types.StateAccount ) error {
157
+ hk := t .hashKey (key )
158
+ data , err := rlp .EncodeToBytes (acc )
159
+ if err != nil {
160
+ return err
161
+ }
162
+ if err := t .trie .TryUpdate (hk , data ); err != nil {
163
+ return err
164
+ }
165
+ t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
166
+ return nil
167
+ }
168
+
177
169
// Delete removes any existing value for key from the trie.
178
170
func (t * StateTrie ) Delete (key []byte ) {
179
171
if err := t .TryDelete (key ); err != nil {
@@ -182,14 +174,15 @@ func (t *StateTrie) Delete(key []byte) {
182
174
}
183
175
184
176
// TryDelete removes any existing value for key from the trie.
185
- // If a node was not found in the database, a MissingNodeError is returned.
177
+ // If the specified trie node is not in the trie, nothing will be changed.
178
+ // If a node is not found in the database, a MissingNodeError is returned.
186
179
func (t * StateTrie ) TryDelete (key []byte ) error {
187
180
hk := t .hashKey (key )
188
181
delete (t .getSecKeyCache (), string (hk ))
189
182
return t .trie .TryDelete (hk )
190
183
}
191
184
192
- // TryDeleteACcount abstracts an account deletion from the trie.
185
+ // TryDeleteAccount abstracts an account deletion from the trie.
193
186
func (t * StateTrie ) TryDeleteAccount (key []byte ) error {
194
187
hk := t .hashKey (key )
195
188
delete (t .getSecKeyCache (), string (hk ))
0 commit comments