From eb7aecc4e36537fcb86e9c67ecbb79a934d0da8b Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 5 Nov 2024 11:23:28 -0800 Subject: [PATCH] Remove redundant writes when a state object is reverted --- core/state/intra_block_state.go | 6 ++++++ core/vm/evmtypes/evmtypes.go | 1 + eth/tracers/native/zero.go | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/state/intra_block_state.go b/core/state/intra_block_state.go index e56462ed418..1ef95aee77f 100644 --- a/core/state/intra_block_state.go +++ b/core/state/intra_block_state.go @@ -426,6 +426,12 @@ func (sdb *IntraBlockState) SeenAccount(addr libcommon.Address) bool { return ok } +func (sdb *IntraBlockState) IsDirty(addr libcommon.Address) bool { + _, dirtyObj := sdb.stateObjectsDirty[addr] + _, dirtyJournal := sdb.journal.dirties[addr] + return dirtyObj || dirtyJournal +} + func (sdb *IntraBlockState) HasLiveState(addr libcommon.Address, key *libcommon.Hash) bool { if stateObject := sdb.stateObjects[addr]; stateObject != nil { if _, ok := stateObject.originStorage[*key]; ok { diff --git a/core/vm/evmtypes/evmtypes.go b/core/vm/evmtypes/evmtypes.go index f4d10657fe3..0d05346f1ad 100644 --- a/core/vm/evmtypes/evmtypes.go +++ b/core/vm/evmtypes/evmtypes.go @@ -82,6 +82,7 @@ type IntraBlockState interface { SetState(common.Address, *common.Hash, uint256.Int) HasLiveAccount(addr common.Address) bool SeenAccount(addr common.Address) bool + IsDirty(addr common.Address) bool HasLiveState(addr common.Address, key *common.Hash) bool GetTransientState(addr common.Address, key common.Hash) uint256.Int diff --git a/eth/tracers/native/zero.go b/eth/tracers/native/zero.go index 65c32c30069..436201f07ef 100644 --- a/eth/tracers/native/zero.go +++ b/eth/tracers/native/zero.go @@ -220,7 +220,7 @@ func (t *zeroTracer) CaptureTxEnd(restGas uint64) { trace.StorageRead = nil } - if len(trace.StorageWritten) == 0 || !hasLiveAccount { + if len(trace.StorageWritten) == 0 || !hasLiveAccount || !t.env.IntraBlockState().IsDirty(addr) { trace.StorageWritten = nil } else { // A slot write could be reverted if the transaction is reverted. We will need to read the value from the statedb again to get the correct value.