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.
for _, code := range update.codes {
stats.ContractCodes += 1
stats.ContractCodeBytes += codeKeySize + int64(len(code.blob))
stats.ContractCodeBytes += codeKeySize + int64(len(code))
}
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
if db := s.db.TrieDB().Disk(); db != nil && len(ret.codes) > 0 {
batch := db.NewBatch()
for _, code := range ret.codes {
rawdb.WriteCode(batch, code.hash, code.blob)
for hash, code := range ret.codes {
rawdb.WriteCode(batch, hash, code)
}
if err := batch.Write(); err != nil {
return nil, err

View file

@ -82,7 +82,7 @@ type stateUpdate struct {
storagesOrigin map[common.Address]map[common.Hash][]byte
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
}
@ -99,11 +99,11 @@ func (sc *stateUpdate) empty() bool {
// 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 {
var (
accounts = make(map[common.Hash][]byte)
accountsOrigin = make(map[common.Address][]byte)
accounts = make(map[common.Hash][]byte, len(updates)+len(deletes))
accountsOrigin = make(map[common.Address][]byte, len(updates)+len(deletes))
storages = make(map[common.Hash]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
// 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.
addr := op.address
if op.code != nil {
codes[addr] = *op.code
codes[op.code.hash] = op.code.blob
}
accounts[addrHash] = op.data