Skip to content

Commit

Permalink
improve SnapToDiffLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Nov 12, 2024
1 parent 3f4f2fa commit 548a3a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
1 change: 0 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
58 changes: 28 additions & 30 deletions core/state/stateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down Expand Up @@ -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,
Expand All @@ -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
}

0 comments on commit 548a3a7

Please sign in to comment.