@@ -24,7 +24,6 @@ import (
24
24
"github.com/ethereum/go-ethereum/crypto"
25
25
"github.com/ethereum/go-ethereum/ethdb"
26
26
"github.com/ethereum/go-ethereum/log"
27
- "golang.org/x/crypto/sha3"
28
27
)
29
28
30
29
// HashScheme is the legacy hash-based state scheme with which trie nodes are
@@ -50,7 +49,7 @@ const PathScheme = "path"
50
49
type hasher struct { sha crypto.KeccakState }
51
50
52
51
var hasherPool = sync.Pool {
53
- New : func () interface {} { return & hasher {sha : sha3 . NewLegacyKeccak256 ().( crypto.KeccakState )} },
52
+ New : func () interface {} { return & hasher {sha : crypto .NewKeccakState ( )} },
54
53
}
55
54
56
55
func newHasher () * hasher {
@@ -65,33 +64,15 @@ func (h *hasher) release() {
65
64
hasherPool .Put (h )
66
65
}
67
66
68
- // ReadAccountTrieNode retrieves the account trie node and the associated node
69
- // hash with the specified node path.
70
- func ReadAccountTrieNode (db ethdb.KeyValueReader , path []byte ) ([]byte , common.Hash ) {
71
- data , err := db .Get (accountTrieNodeKey (path ))
72
- if err != nil {
73
- return nil , common.Hash {}
74
- }
75
- h := newHasher ()
76
- defer h .release ()
77
- return data , h .hash (data )
78
- }
79
-
80
- // HasAccountTrieNode checks the account trie node presence with the specified
81
- // node path and the associated node hash.
82
- func HasAccountTrieNode (db ethdb.KeyValueReader , path []byte , hash common.Hash ) bool {
83
- data , err := db .Get (accountTrieNodeKey (path ))
84
- if err != nil {
85
- return false
86
- }
87
- h := newHasher ()
88
- defer h .release ()
89
- return h .hash (data ) == hash
67
+ // ReadAccountTrieNode retrieves the account trie node with the specified node path.
68
+ func ReadAccountTrieNode (db ethdb.KeyValueReader , path []byte ) []byte {
69
+ data , _ := db .Get (accountTrieNodeKey (path ))
70
+ return data
90
71
}
91
72
92
- // ExistsAccountTrieNode checks the presence of the account trie node with the
73
+ // HasAccountTrieNode checks the presence of the account trie node with the
93
74
// specified node path, regardless of the node hash.
94
- func ExistsAccountTrieNode (db ethdb.KeyValueReader , path []byte ) bool {
75
+ func HasAccountTrieNode (db ethdb.KeyValueReader , path []byte ) bool {
95
76
has , err := db .Has (accountTrieNodeKey (path ))
96
77
if err != nil {
97
78
return false
@@ -113,33 +94,15 @@ func DeleteAccountTrieNode(db ethdb.KeyValueWriter, path []byte) {
113
94
}
114
95
}
115
96
116
- // ReadStorageTrieNode retrieves the storage trie node and the associated node
117
- // hash with the specified node path.
118
- func ReadStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) ([]byte , common.Hash ) {
119
- data , err := db .Get (storageTrieNodeKey (accountHash , path ))
120
- if err != nil {
121
- return nil , common.Hash {}
122
- }
123
- h := newHasher ()
124
- defer h .release ()
125
- return data , h .hash (data )
126
- }
127
-
128
- // HasStorageTrieNode checks the storage trie node presence with the provided
129
- // node path and the associated node hash.
130
- func HasStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte , hash common.Hash ) bool {
131
- data , err := db .Get (storageTrieNodeKey (accountHash , path ))
132
- if err != nil {
133
- return false
134
- }
135
- h := newHasher ()
136
- defer h .release ()
137
- return h .hash (data ) == hash
97
+ // ReadStorageTrieNode retrieves the storage trie node with the specified node path.
98
+ func ReadStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) []byte {
99
+ data , _ := db .Get (storageTrieNodeKey (accountHash , path ))
100
+ return data
138
101
}
139
102
140
- // ExistsStorageTrieNode checks the presence of the storage trie node with the
103
+ // HasStorageTrieNode checks the presence of the storage trie node with the
141
104
// specified account hash and node path, regardless of the node hash.
142
- func ExistsStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) bool {
105
+ func HasStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) bool {
143
106
has , err := db .Has (storageTrieNodeKey (accountHash , path ))
144
107
if err != nil {
145
108
return false
@@ -198,54 +161,54 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c
198
161
case HashScheme :
199
162
return HasLegacyTrieNode (db , hash )
200
163
case PathScheme :
164
+ var blob []byte
201
165
if owner == (common.Hash {}) {
202
- return HasAccountTrieNode (db , path , hash )
166
+ blob = ReadAccountTrieNode (db , path )
167
+ } else {
168
+ blob = ReadStorageTrieNode (db , owner , path )
203
169
}
204
- return HasStorageTrieNode (db , owner , path , hash )
170
+ if len (blob ) == 0 {
171
+ return false
172
+ }
173
+ h := newHasher ()
174
+ defer h .release ()
175
+ return h .hash (blob ) == hash // exists but not match
205
176
default :
206
177
panic (fmt .Sprintf ("Unknown scheme %v" , scheme ))
207
178
}
208
179
}
209
180
210
181
// ReadTrieNode retrieves the trie node from database with the provided node info
211
182
// and associated node hash.
212
- // hashScheme-based lookup requires the following:
213
- // - hash
214
- //
215
- // pathScheme-based lookup requires the following:
216
- // - owner
217
- // - path
218
183
func ReadTrieNode (db ethdb.KeyValueReader , owner common.Hash , path []byte , hash common.Hash , scheme string ) []byte {
219
184
switch scheme {
220
185
case HashScheme :
221
186
return ReadLegacyTrieNode (db , hash )
222
187
case PathScheme :
223
- var (
224
- blob []byte
225
- nHash common.Hash
226
- )
188
+ var blob []byte
227
189
if owner == (common.Hash {}) {
228
- blob , nHash = ReadAccountTrieNode (db , path )
190
+ blob = ReadAccountTrieNode (db , path )
229
191
} else {
230
- blob , nHash = ReadStorageTrieNode (db , owner , path )
192
+ blob = ReadStorageTrieNode (db , owner , path )
231
193
}
232
- if nHash != hash {
194
+ if len ( blob ) == 0 {
233
195
return nil
234
196
}
197
+ h := newHasher ()
198
+ defer h .release ()
199
+ if h .hash (blob ) != hash {
200
+ return nil // exists but not match
201
+ }
235
202
return blob
236
203
default :
237
204
panic (fmt .Sprintf ("Unknown scheme %v" , scheme ))
238
205
}
239
206
}
240
207
241
- // WriteTrieNode writes the trie node into database with the provided node info
242
- // and associated node hash.
243
- // hashScheme-based lookup requires the following:
244
- // - hash
208
+ // WriteTrieNode writes the trie node into database with the provided node info.
245
209
//
246
- // pathScheme-based lookup requires the following:
247
- // - owner
248
- // - path
210
+ // hash-scheme requires the node hash as the identifier.
211
+ // path-scheme requires the node owner and path as the identifier.
249
212
func WriteTrieNode (db ethdb.KeyValueWriter , owner common.Hash , path []byte , hash common.Hash , node []byte , scheme string ) {
250
213
switch scheme {
251
214
case HashScheme :
@@ -261,14 +224,10 @@ func WriteTrieNode(db ethdb.KeyValueWriter, owner common.Hash, path []byte, hash
261
224
}
262
225
}
263
226
264
- // DeleteTrieNode deletes the trie node from database with the provided node info
265
- // and associated node hash.
266
- // hashScheme-based lookup requires the following:
267
- // - hash
227
+ // DeleteTrieNode deletes the trie node from database with the provided node info.
268
228
//
269
- // pathScheme-based lookup requires the following:
270
- // - owner
271
- // - path
229
+ // hash-scheme requires the node hash as the identifier.
230
+ // path-scheme requires the node owner and path as the identifier.
272
231
func DeleteTrieNode (db ethdb.KeyValueWriter , owner common.Hash , path []byte , hash common.Hash , scheme string ) {
273
232
switch scheme {
274
233
case HashScheme :
@@ -287,9 +246,8 @@ func DeleteTrieNode(db ethdb.KeyValueWriter, owner common.Hash, path []byte, has
287
246
// ReadStateScheme reads the state scheme of persistent state, or none
288
247
// if the state is not present in database.
289
248
func ReadStateScheme (db ethdb.Reader ) string {
290
- // Check if state in path-based scheme is present
291
- blob , _ := ReadAccountTrieNode (db , nil )
292
- if len (blob ) != 0 {
249
+ // Check if state in path-based scheme is present.
250
+ if HasAccountTrieNode (db , nil ) {
293
251
return PathScheme
294
252
}
295
253
// The root node might be deleted during the initial snap sync, check
@@ -304,8 +262,7 @@ func ReadStateScheme(db ethdb.Reader) string {
304
262
if header == nil {
305
263
return "" // empty datadir
306
264
}
307
- blob = ReadLegacyTrieNode (db , header .Root )
308
- if len (blob ) == 0 {
265
+ if ! HasLegacyTrieNode (db , header .Root ) {
309
266
return "" // no state in disk
310
267
}
311
268
return HashScheme
0 commit comments