@@ -74,8 +74,6 @@ type Database struct {
74
74
oldest common.Hash // Oldest tracked node, flush-list head
75
75
newest common.Hash // Newest tracked node, flush-list tail
76
76
77
- preimages map [common.Hash ][]byte // Preimages of nodes from the secure trie
78
-
79
77
gctime time.Duration // Time spent on garbage collection since last commit
80
78
gcnodes uint64 // Nodes garbage collected since last commit
81
79
gcsize common.StorageSize // Data storage garbage collected since last commit
@@ -84,9 +82,9 @@ type Database struct {
84
82
flushnodes uint64 // Nodes flushed since last commit
85
83
flushsize common.StorageSize // Data storage flushed since last commit
86
84
87
- dirtiesSize common.StorageSize // Storage size of the dirty node cache (exc. metadata)
88
- childrenSize common.StorageSize // Storage size of the external children tracking
89
- preimagesSize common. StorageSize // Storage size of the preimages cache
85
+ dirtiesSize common.StorageSize // Storage size of the dirty node cache (exc. metadata)
86
+ childrenSize common.StorageSize // Storage size of the external children tracking
87
+ preimages * preimageStore // The store for caching preimages
90
88
91
89
lock sync.RWMutex
92
90
}
@@ -287,15 +285,17 @@ func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database
287
285
cleans = fastcache .LoadFromFileOrNew (config .Journal , config .Cache * 1024 * 1024 )
288
286
}
289
287
}
288
+ var preimage * preimageStore
289
+ if config != nil && config .Preimages {
290
+ preimage = newPreimageStore (diskdb )
291
+ }
290
292
db := & Database {
291
293
diskdb : diskdb ,
292
294
cleans : cleans ,
293
295
dirties : map [common.Hash ]* cachedNode {{}: {
294
296
children : make (map [common.Hash ]uint16 ),
295
297
}},
296
- }
297
- if config == nil || config .Preimages { // TODO(karalabe): Flip to default off in the future
298
- db .preimages = make (map [common.Hash ][]byte )
298
+ preimages : preimage ,
299
299
}
300
300
return db
301
301
}
@@ -341,24 +341,6 @@ func (db *Database) insert(hash common.Hash, size int, node node) {
341
341
db .dirtiesSize += common .StorageSize (common .HashLength + entry .size )
342
342
}
343
343
344
- // insertPreimage writes a new trie node pre-image to the memory database if it's
345
- // yet unknown. The method will NOT make a copy of the slice,
346
- // only use if the preimage will NOT be changed later on.
347
- //
348
- // Note, this method assumes that the database's lock is held!
349
- func (db * Database ) insertPreimage (hash common.Hash , preimage []byte ) {
350
- // Short circuit if preimage collection is disabled
351
- if db .preimages == nil {
352
- return
353
- }
354
- // Track the preimage if a yet unknown one
355
- if _ , ok := db .preimages [hash ]; ok {
356
- return
357
- }
358
- db .preimages [hash ] = preimage
359
- db .preimagesSize += common .StorageSize (common .HashLength + len (preimage ))
360
- }
361
-
362
344
// node retrieves a cached trie node from memory, or returns nil if none can be
363
345
// found in the memory cache.
364
346
func (db * Database ) node (hash common.Hash ) node {
@@ -435,24 +417,6 @@ func (db *Database) Node(hash common.Hash) ([]byte, error) {
435
417
return nil , errors .New ("not found" )
436
418
}
437
419
438
- // preimage retrieves a cached trie node pre-image from memory. If it cannot be
439
- // found cached, the method queries the persistent database for the content.
440
- func (db * Database ) preimage (hash common.Hash ) []byte {
441
- // Short circuit if preimage collection is disabled
442
- if db .preimages == nil {
443
- return nil
444
- }
445
- // Retrieve the node from cache if available
446
- db .lock .RLock ()
447
- preimage := db .preimages [hash ]
448
- db .lock .RUnlock ()
449
-
450
- if preimage != nil {
451
- return preimage
452
- }
453
- return rawdb .ReadPreimage (db .diskdb , hash )
454
- }
455
-
456
420
// Nodes retrieves the hashes of all the nodes cached within the memory database.
457
421
// This method is extremely expensive and should only be used to validate internal
458
422
// states in test code.
@@ -597,19 +561,8 @@ func (db *Database) Cap(limit common.StorageSize) error {
597
561
598
562
// If the preimage cache got large enough, push to disk. If it's still small
599
563
// leave for later to deduplicate writes.
600
- flushPreimages := db .preimagesSize > 4 * 1024 * 1024
601
- if flushPreimages {
602
- if db .preimages == nil {
603
- log .Error ("Attempted to write preimages whilst disabled" )
604
- } else {
605
- rawdb .WritePreimages (batch , db .preimages )
606
- if batch .ValueSize () > ethdb .IdealBatchSize {
607
- if err := batch .Write (); err != nil {
608
- return err
609
- }
610
- batch .Reset ()
611
- }
612
- }
564
+ if db .preimages != nil {
565
+ db .preimages .commit (false )
613
566
}
614
567
// Keep committing nodes from the flush-list until we're below allowance
615
568
oldest := db .oldest
@@ -644,13 +597,6 @@ func (db *Database) Cap(limit common.StorageSize) error {
644
597
db .lock .Lock ()
645
598
defer db .lock .Unlock ()
646
599
647
- if flushPreimages {
648
- if db .preimages == nil {
649
- log .Error ("Attempted to reset preimage cache whilst disabled" )
650
- } else {
651
- db .preimages , db .preimagesSize = make (map [common.Hash ][]byte ), 0
652
- }
653
- }
654
600
for db .oldest != oldest {
655
601
node := db .dirties [db .oldest ]
656
602
delete (db .dirties , db .oldest )
@@ -694,13 +640,7 @@ func (db *Database) Commit(node common.Hash, report bool, callback func(common.H
694
640
695
641
// Move all of the accumulated preimages into a write batch
696
642
if db .preimages != nil {
697
- rawdb .WritePreimages (batch , db .preimages )
698
- // Since we're going to replay trie node writes into the clean cache, flush out
699
- // any batched pre-images before continuing.
700
- if err := batch .Write (); err != nil {
701
- return err
702
- }
703
- batch .Reset ()
643
+ db .preimages .commit (true )
704
644
}
705
645
// Move the trie itself into the batch, flushing if enough data is accumulated
706
646
nodes , storage := len (db .dirties ), db .dirtiesSize
@@ -723,9 +663,6 @@ func (db *Database) Commit(node common.Hash, report bool, callback func(common.H
723
663
batch .Reset ()
724
664
725
665
// Reset the storage counters and bumped metrics
726
- if db .preimages != nil {
727
- db .preimages , db .preimagesSize = make (map [common.Hash ][]byte ), 0
728
- }
729
666
memcacheCommitTimeTimer .Update (time .Since (start ))
730
667
memcacheCommitSizeMeter .Mark (int64 (storage - db .dirtiesSize ))
731
668
memcacheCommitNodesMeter .Mark (int64 (nodes - len (db .dirties )))
@@ -837,7 +774,11 @@ func (db *Database) Size() (common.StorageSize, common.StorageSize) {
837
774
// counted.
838
775
var metadataSize = common .StorageSize ((len (db .dirties ) - 1 ) * cachedNodeSize )
839
776
var metarootRefs = common .StorageSize (len (db .dirties [common.Hash {}].children ) * (common .HashLength + 2 ))
840
- return db .dirtiesSize + db .childrenSize + metadataSize - metarootRefs , db .preimagesSize
777
+ var preimageSize common.StorageSize
778
+ if db .preimages != nil {
779
+ preimageSize = db .preimages .size ()
780
+ }
781
+ return db .dirtiesSize + db .childrenSize + metadataSize - metarootRefs , preimageSize
841
782
}
842
783
843
784
// saveCache saves clean state cache to given directory path
0 commit comments