40
40
//
41
41
// Note, the prefetcher's API is not thread safe.
42
42
type triePrefetcher struct {
43
+ verkle bool // Flag whether the prefetcher is in verkle mode
43
44
db Database // Database to fetch trie nodes through
44
45
root common.Hash // Root hash of the account trie for metrics
45
46
fetchers map [string ]* subfetcher // Subfetchers for each trie
@@ -66,6 +67,7 @@ type triePrefetcher struct {
66
67
func newTriePrefetcher (db Database , root common.Hash , namespace string , noreads bool ) * triePrefetcher {
67
68
prefix := triePrefetchMetricsPrefix + namespace
68
69
return & triePrefetcher {
70
+ verkle : db .TrieDB ().IsVerkle (),
69
71
db : db ,
70
72
root : root ,
71
73
fetchers : make (map [string ]* subfetcher ), // Active prefetchers use the fetchers map
@@ -196,12 +198,18 @@ func (p *triePrefetcher) trie(owner common.Hash, root common.Hash) Trie {
196
198
func (p * triePrefetcher ) used (owner common.Hash , root common.Hash , used [][]byte ) {
197
199
if fetcher := p .fetchers [p .trieID (owner , root )]; fetcher != nil {
198
200
fetcher .wait () // ensure the fetcher's idle before poking in its internals
199
- fetcher .used = used
201
+ fetcher .used = append ( fetcher . used , used ... )
200
202
}
201
203
}
202
204
203
205
// trieID returns an unique trie identifier consists the trie owner and root hash.
204
206
func (p * triePrefetcher ) trieID (owner common.Hash , root common.Hash ) string {
207
+ // The trie in verkle is only identified by state root
208
+ if p .verkle {
209
+ return p .root .Hex ()
210
+ }
211
+ // The trie in merkle is either identified by state root (account trie),
212
+ // or identified by the owner and trie root (storage trie)
205
213
trieID := make ([]byte , common .HashLength * 2 )
206
214
copy (trieID , owner .Bytes ())
207
215
copy (trieID [common .HashLength :], root .Bytes ())
@@ -320,29 +328,47 @@ func (sf *subfetcher) terminate(async bool) {
320
328
<- sf .term
321
329
}
322
330
331
+ // openTrie resolves the target trie from database for prefetching.
332
+ func (sf * subfetcher ) openTrie () error {
333
+ // Open the verkle tree if the sub-fetcher is in verkle mode. Note, there is
334
+ // only a single fetcher for verkle.
335
+ if sf .db .TrieDB ().IsVerkle () {
336
+ tr , err := sf .db .OpenTrie (sf .state )
337
+ if err != nil {
338
+ log .Warn ("Trie prefetcher failed opening verkle trie" , "root" , sf .root , "err" , err )
339
+ return err
340
+ }
341
+ sf .trie = tr
342
+ return nil
343
+ }
344
+ // Open the merkle tree if the sub-fetcher is in merkle mode
345
+ if sf .owner == (common.Hash {}) {
346
+ tr , err := sf .db .OpenTrie (sf .state )
347
+ if err != nil {
348
+ log .Warn ("Trie prefetcher failed opening account trie" , "root" , sf .root , "err" , err )
349
+ return err
350
+ }
351
+ sf .trie = tr
352
+ return nil
353
+ }
354
+ tr , err := sf .db .OpenStorageTrie (sf .state , sf .addr , sf .root , nil )
355
+ if err != nil {
356
+ log .Warn ("Trie prefetcher failed opening storage trie" , "root" , sf .root , "err" , err )
357
+ return err
358
+ }
359
+ sf .trie = tr
360
+ return nil
361
+ }
362
+
323
363
// loop loads newly-scheduled trie tasks as they are received and loads them, stopping
324
364
// when requested.
325
365
func (sf * subfetcher ) loop () {
326
366
// No matter how the loop stops, signal anyone waiting that it's terminated
327
367
defer close (sf .term )
328
368
329
- // Start by opening the trie and stop processing if it fails
330
- if sf .owner == (common.Hash {}) {
331
- trie , err := sf .db .OpenTrie (sf .root )
332
- if err != nil {
333
- log .Warn ("Trie prefetcher failed opening trie" , "root" , sf .root , "err" , err )
334
- return
335
- }
336
- sf .trie = trie
337
- } else {
338
- trie , err := sf .db .OpenStorageTrie (sf .state , sf .addr , sf .root , nil )
339
- if err != nil {
340
- log .Warn ("Trie prefetcher failed opening trie" , "root" , sf .root , "err" , err )
341
- return
342
- }
343
- sf .trie = trie
369
+ if err := sf .openTrie (); err != nil {
370
+ return
344
371
}
345
- // Trie opened successfully, keep prefetching items
346
372
for {
347
373
select {
348
374
case <- sf .wake :
0 commit comments