Skip to content

Commit

Permalink
trie: address comments from martin
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Jul 19, 2022
1 parent 9665058 commit cbb3b1e
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions trie/preimages.go
Expand Up @@ -43,53 +43,53 @@ func newPreimageStore(disk ethdb.KeyValueStore) *preimageStore {
// insertPreimage writes a new trie node pre-image to the memory database if it's
// yet unknown. The method will NOT make a copy of the slice, only use if the
// preimage will NOT be changed later on.
func (db *preimageStore) insertPreimage(preimages map[common.Hash][]byte) {
db.lock.Lock()
defer db.lock.Unlock()
func (store *preimageStore) insertPreimage(preimages map[common.Hash][]byte) {
store.lock.Lock()
defer store.lock.Unlock()

for hash, preimage := range preimages {
if _, ok := db.preimages[hash]; ok {
if _, ok := store.preimages[hash]; ok {
continue
}
db.preimages[hash] = preimage
db.preimagesSize += common.StorageSize(common.HashLength + len(preimage))
store.preimages[hash] = preimage
store.preimagesSize += common.StorageSize(common.HashLength + len(preimage))
}
}

// preimage retrieves a cached trie node pre-image from memory. If it cannot be
// found cached, the method queries the persistent database for the content.
func (db *preimageStore) preimage(hash common.Hash) []byte {
db.lock.RLock()
preimage := db.preimages[hash]
db.lock.RUnlock()
func (store *preimageStore) preimage(hash common.Hash) []byte {
store.lock.RLock()
preimage := store.preimages[hash]
store.lock.RUnlock()

if preimage != nil {
return preimage
}
return rawdb.ReadPreimage(db.disk, hash)
return rawdb.ReadPreimage(store.disk, hash)
}

// commit flushes the cached preimages into the disk.
func (db *preimageStore) commit(force bool) error {
db.lock.Lock()
defer db.lock.Unlock()
func (store *preimageStore) commit(force bool) error {
store.lock.Lock()
defer store.lock.Unlock()

if db.preimagesSize <= 4*1024*1024 || force {
if store.preimagesSize <= 4*1024*1024 && !force {
return nil
}
batch := db.disk.NewBatch()
rawdb.WritePreimages(batch, db.preimages)
batch := store.disk.NewBatch()
rawdb.WritePreimages(batch, store.preimages)
if err := batch.Write(); err != nil {
return err
}
db.preimages, db.preimagesSize = make(map[common.Hash][]byte), 0
store.preimages, store.preimagesSize = make(map[common.Hash][]byte), 0
return nil
}

// size returns the current storage size of accumulated preimages.
func (db *preimageStore) size() common.StorageSize {
db.lock.RLock()
defer db.lock.RUnlock()
func (store *preimageStore) size() common.StorageSize {
store.lock.RLock()
defer store.lock.RUnlock()

return db.preimagesSize
return store.preimagesSize
}

0 comments on commit cbb3b1e

Please sign in to comment.