diff --git a/core/state/statedb.go b/core/state/statedb.go index c861276b3e..a2e3d06060 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1451,7 +1451,6 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool) (*stateU // If snapshotting is enabled, update the snapshot tree with this new version if snap := s.db.Snapshot(); snap != nil && snap.Snapshot(ret.originRoot) != nil { start := time.Now() - ret.diffLayer.Destructs, ret.diffLayer.Accounts, ret.diffLayer.Storages = ret.SnapToDiffLayer() if err := snap.Update(ret.root, ret.originRoot, ret.destructs, ret.accounts, ret.storages); err != nil { log.Warn("Failed to update snapshot tree", "from", ret.originRoot, "to", ret.root, "err", err) } diff --git a/core/state/stateupdate.go b/core/state/stateupdate.go index 0b73e823d9..463dd470f2 100644 --- a/core/state/stateupdate.go +++ b/core/state/stateupdate.go @@ -59,8 +59,7 @@ type stateUpdate struct { codes map[common.Address]contractCode // codes contains the set of dirty codes nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes - destructsAddrs map[common.Address]struct{} - diffLayer *types.DiffLayer // snapshot diffLayer generated by current block + diffLayer *types.DiffLayer // snapshot diffLayer generated by current block } @@ -125,7 +124,7 @@ func newStateUpdate(originRoot common.Hash, root common.Hash, deletes map[common storagesOrigin[addr] = origin } } - return &stateUpdate{ + sc := &stateUpdate{ originRoot: types.TrieRootHash(originRoot), root: types.TrieRootHash(root), destructs: destructs, @@ -135,35 +134,34 @@ func newStateUpdate(originRoot common.Hash, root common.Hash, deletes map[common storagesOrigin: storagesOrigin, codes: codes, nodes: nodes, - destructsAddrs: destructsAddrs, } -} -func (sc *stateUpdate) SnapToDiffLayer() ([]common.Address, []types.DiffAccount, []types.DiffStorage) { - destructs := make([]common.Address, 0, len(sc.destructsAddrs)) - for account := range sc.destructsAddrs { - destructs = append(destructs, account) - } - accounts := make([]types.DiffAccount, 0, len(sc.accounts)) - for accountHash, account := range sc.accounts { - accounts = append(accounts, types.DiffAccount{ - Account: accountHash, - Blob: account, - }) - } - storages := make([]types.DiffStorage, 0, len(sc.storages)) - for accountHash, storage := range sc.storages { - keys := make([]common.Hash, 0, len(storage)) - values := make([][]byte, 0, len(storage)) - for k, v := range storage { - keys = append(keys, k) - values = append(values, v) + if sc.diffLayer != nil { + for account := range destructsAddrs { + sc.diffLayer.Destructs = append(sc.diffLayer.Destructs, account) + } + + for accountHash, account := range sc.accounts { + sc.diffLayer.Accounts = append(sc.diffLayer.Accounts, types.DiffAccount{ + Account: accountHash, + Blob: account, + }) + } + + for accountHash, storage := range sc.storages { + keys := make([]common.Hash, 0, len(storage)) + values := make([][]byte, 0, len(storage)) + for k, v := range storage { + keys = append(keys, k) + values = append(values, v) + } + sc.diffLayer.Storages = append(sc.diffLayer.Storages, types.DiffStorage{ + Account: accountHash, + Keys: keys, + Vals: values, + }) } - storages = append(storages, types.DiffStorage{ - Account: accountHash, - Keys: keys, - Vals: values, - }) } - return destructs, accounts, storages + + return sc }