core/state: deduplicate code in state update

This commit is contained in:
weiihann 2025-11-11 13:57:30 +08:00
parent ca91254259
commit 5be4b4e36e
3 changed files with 9 additions and 9 deletions

View file

@ -218,7 +218,7 @@ func calSizeStats(update *stateUpdate) (SizeStats, error) {
// this deviation is negligible and acceptable for measurement purposes. // this deviation is negligible and acceptable for measurement purposes.
for _, code := range update.codes { for _, code := range update.codes {
stats.ContractCodes += 1 stats.ContractCodes += 1
stats.ContractCodeBytes += codeKeySize + int64(len(code.blob)) stats.ContractCodeBytes += codeKeySize + int64(len(code))
} }
return stats, nil return stats, nil
} }

View file

@ -1324,8 +1324,8 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorag
// Commit dirty contract code if any exists // Commit dirty contract code if any exists
if db := s.db.TrieDB().Disk(); db != nil && len(ret.codes) > 0 { if db := s.db.TrieDB().Disk(); db != nil && len(ret.codes) > 0 {
batch := db.NewBatch() batch := db.NewBatch()
for _, code := range ret.codes { for hash, code := range ret.codes {
rawdb.WriteCode(batch, code.hash, code.blob) rawdb.WriteCode(batch, hash, code)
} }
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
return nil, err return nil, err

View file

@ -82,8 +82,8 @@ type stateUpdate struct {
storagesOrigin map[common.Address]map[common.Hash][]byte storagesOrigin map[common.Address]map[common.Hash][]byte
rawStorageKey bool rawStorageKey bool
codes map[common.Address]contractCode // codes contains the set of dirty codes codes map[common.Hash][]byte // codes contains the set of dirty codes
nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes
} }
// empty returns a flag indicating the state transition is empty or not. // empty returns a flag indicating the state transition is empty or not.
@ -99,11 +99,11 @@ func (sc *stateUpdate) empty() bool {
// the hash of the slot key for constructing state update object. // the hash of the slot key for constructing state update object.
func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash, blockNumber uint64, deletes map[common.Hash]*accountDelete, updates map[common.Hash]*accountUpdate, nodes *trienode.MergedNodeSet) *stateUpdate { func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash, blockNumber uint64, deletes map[common.Hash]*accountDelete, updates map[common.Hash]*accountUpdate, nodes *trienode.MergedNodeSet) *stateUpdate {
var ( var (
accounts = make(map[common.Hash][]byte) accounts = make(map[common.Hash][]byte, len(updates)+len(deletes))
accountsOrigin = make(map[common.Address][]byte) accountsOrigin = make(map[common.Address][]byte, len(updates)+len(deletes))
storages = make(map[common.Hash]map[common.Hash][]byte) storages = make(map[common.Hash]map[common.Hash][]byte)
storagesOrigin = make(map[common.Address]map[common.Hash][]byte) storagesOrigin = make(map[common.Address]map[common.Hash][]byte)
codes = make(map[common.Address]contractCode) codes = make(map[common.Hash][]byte)
) )
// Since some accounts might be destroyed and recreated within the same // Since some accounts might be destroyed and recreated within the same
// block, deletions must be aggregated first. // block, deletions must be aggregated first.
@ -125,7 +125,7 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash
// Aggregate dirty contract codes if they are available. // Aggregate dirty contract codes if they are available.
addr := op.address addr := op.address
if op.code != nil { if op.code != nil {
codes[addr] = *op.code codes[op.code.hash] = op.code.blob
} }
accounts[addrHash] = op.data accounts[addrHash] = op.data