diff --git a/core/state/statedb.go b/core/state/statedb.go index e3f5b9e1a0..9349ef043f 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1052,9 +1052,9 @@ func (s *StateDB) deleteStorage(addr common.Address, addrHash common.Hash, root // with their values be tracked as original value. func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*accountDelete, []*trienode.NodeSet, error) { var ( - nodes []*trienode.NodeSet - buf = crypto.NewKeccakState() - deletes = make(map[common.Hash]*accountDelete) + nodes = make([]*trienode.NodeSet, 0, len(s.stateObjectsDestruct)) + hasher = crypto.NewKeccakState() + deletes = make(map[common.Hash]*accountDelete, len(s.stateObjectsDestruct)) ) for addr, prevObj := range s.stateObjectsDestruct { prev := prevObj.origin @@ -1063,12 +1063,12 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco // of block. It can be either case (a) or (b) and will be interpreted as // null->null state transition. // - for (a), skip it without doing anything - // - for (b), the resurrected account with nil as original will be handled afterwards + // - for (b), the resurrected account with nil as original will be handled afterward if prev == nil { continue } // The account was existent, it can be either case (c) or (d). - addrHash := crypto.HashData(buf, addr.Bytes()) + addrHash := crypto.HashData(hasher, addr.Bytes()) op := &accountDelete{ address: addr, origin: types.SlimAccountRLP(*prev), diff --git a/triedb/pathdb/states.go b/triedb/pathdb/states.go index 0a83b2f2cc..c7a5cbb78b 100644 --- a/triedb/pathdb/states.go +++ b/triedb/pathdb/states.go @@ -338,12 +338,10 @@ func (s *stateSet) encode(w io.Writer) error { AddrHashes []common.Hash Accounts [][]byte } - var enc accounts - for addrHash, blob := range s.accountData { - enc.AddrHashes = append(enc.AddrHashes, addrHash) - enc.Accounts = append(enc.Accounts, blob) - } - if err := rlp.Encode(w, enc); err != nil { + if err := rlp.Encode(w, accounts{ + AddrHashes: slices.Collect(maps.Keys(s.accountData)), + Accounts: slices.Collect(maps.Values(s.accountData)), + }); err != nil { return err } // Encode storages @@ -354,16 +352,10 @@ func (s *stateSet) encode(w io.Writer) error { } storages := make([]Storage, 0, len(s.storageData)) for addrHash, slots := range s.storageData { - keys := make([]common.Hash, 0, len(slots)) - vals := make([][]byte, 0, len(slots)) - for key, val := range slots { - keys = append(keys, key) - vals = append(vals, val) - } storages = append(storages, Storage{ AddrHash: addrHash, - Keys: keys, - Vals: vals, + Keys: slices.Collect(maps.Keys(slots)), + Vals: slices.Collect(maps.Values(slots)), }) } return rlp.Encode(w, storages) @@ -498,12 +490,10 @@ func (s *StateSetWithOrigin) encode(w io.Writer) error { Addresses []common.Address Accounts [][]byte } - var accounts Accounts - for address, blob := range s.accountOrigin { - accounts.Addresses = append(accounts.Addresses, address) - accounts.Accounts = append(accounts.Accounts, blob) - } - if err := rlp.Encode(w, accounts); err != nil { + if err := rlp.Encode(w, Accounts{ + Addresses: slices.Collect(maps.Keys(s.accountOrigin)), + Accounts: slices.Collect(maps.Values(s.accountOrigin)), + }); err != nil { return err } // Encode storages @@ -514,13 +504,11 @@ func (s *StateSetWithOrigin) encode(w io.Writer) error { } storages := make([]Storage, 0, len(s.storageOrigin)) for address, slots := range s.storageOrigin { - keys := make([]common.Hash, 0, len(slots)) - vals := make([][]byte, 0, len(slots)) - for key, val := range slots { - keys = append(keys, key) - vals = append(vals, val) - } - storages = append(storages, Storage{Address: address, Keys: keys, Vals: vals}) + storages = append(storages, Storage{ + Address: address, + Keys: slices.Collect(maps.Keys(slots)), + Vals: slices.Collect(maps.Values(slots)), + }) } return rlp.Encode(w, storages) }