core/state: lazily init snapshot storage map

This commit is contained in:
Martin Holst Swende 2020-02-05 13:12:09 +01:00
parent dd88bd82c9
commit cf82fca583
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -303,16 +303,8 @@ func (s *stateObject) updateTrie(db Database) Trie {
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
} }
// Retrieve the snapshot storage map for the object // The snapshot storage map for the object
var storage map[common.Hash][]byte var storage map[common.Hash][]byte
if s.db.snap != nil {
// Retrieve the old storage map, if available, create a new one otherwise
storage = s.db.snapStorage[s.addrHash]
if storage == nil {
storage = make(map[common.Hash][]byte)
s.db.snapStorage[s.addrHash] = storage
}
}
// Insert all the pending updates into the trie // Insert all the pending updates into the trie
tr := s.getTrie(db) tr := s.getTrie(db)
for key, value := range s.pendingStorage { for key, value := range s.pendingStorage {
@ -331,7 +323,14 @@ func (s *stateObject) updateTrie(db Database) Trie {
s.setError(tr.TryUpdate(key[:], v)) s.setError(tr.TryUpdate(key[:], v))
} }
// If state snapshotting is active, cache the data til commit // If state snapshotting is active, cache the data til commit
if storage != nil { if s.db.snap != nil {
if storage == nil {
// Retrieve the old storage map, if available, create a new one otherwise
if storage = s.db.snapStorage[s.addrHash]; storage == nil {
storage = make(map[common.Hash][]byte)
s.db.snapStorage[s.addrHash] = storage
}
}
storage[crypto.Keccak256Hash(key[:])] = v // v will be nil if value is 0x00 storage[crypto.Keccak256Hash(key[:])] = v // v will be nil if value is 0x00
} }
} }