Skip to content

Commit

Permalink
Add 'seenAccount' to IBS
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Aug 18, 2024
1 parent b9df3f0 commit a50e685
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
11 changes: 11 additions & 0 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type IntraBlockState struct {
stateObjects map[libcommon.Address]*stateObject
stateObjectsDirty map[libcommon.Address]struct{}

seenStateObjects map[libcommon.Address]*stateObject // State objects that have been seen at least once

nilAccounts map[libcommon.Address]struct{} // Remember non-existent account to avoid reading them again

// DB error.
Expand Down Expand Up @@ -97,6 +99,7 @@ func New(stateReader StateReader) *IntraBlockState {
return &IntraBlockState{
stateReader: stateReader,
stateObjects: map[libcommon.Address]*stateObject{},
seenStateObjects: map[libcommon.Address]*stateObject{},
stateObjectsDirty: map[libcommon.Address]struct{}{},
nilAccounts: map[libcommon.Address]struct{}{},
logs: map[libcommon.Hash][]*types.Log{},
Expand Down Expand Up @@ -418,6 +421,13 @@ func (sdb *IntraBlockState) HasLiveAccount(addr libcommon.Address) bool {
return false
}

func (sdb *IntraBlockState) SeenAccount(addr libcommon.Address) bool {
if stateObject := sdb.seenStateObjects[addr]; stateObject != nil {
return true
}
return false
}

func (sdb *IntraBlockState) HasLiveState(addr libcommon.Address, key *libcommon.Hash) bool {
if stateObject := sdb.stateObjects[addr]; stateObject != nil {
if _, ok := stateObject.originStorage[*key]; ok {
Expand Down Expand Up @@ -532,6 +542,7 @@ func (sdb *IntraBlockState) setStateObject(addr libcommon.Address, object *state
sdb.journal.append(balanceIncreaseTransfer{bi: bi})
}
sdb.stateObjects[addr] = object
sdb.seenStateObjects[addr] = object
}

// Retrieve a state object or create a new state object if nil.
Expand Down
1 change: 1 addition & 0 deletions core/vm/evmtypes/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type IntraBlockState interface {
GetState(address common.Address, slot *common.Hash, outValue *uint256.Int)
SetState(common.Address, *common.Hash, uint256.Int)
HasLiveAccount(addr common.Address) bool
SeenAccount(addr common.Address) bool
HasLiveState(addr common.Address, key *common.Hash) bool

GetTransientState(addr common.Address, key common.Hash) uint256.Int
Expand Down
35 changes: 15 additions & 20 deletions eth/tracers/native/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,25 @@ func init() {
}

type zeroTracer struct {
noopTracer // stub struct to mock not used interface methods
env *vm.EVM
tx types.TxnInfo
gasLimit uint64 // Amount of gas bought for the whole tx
interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
ctx *tracers.Context
to *libcommon.Address
txStatus uint64
addrOpCodes map[libcommon.Address]map[vm.OpCode]struct{}
wasLoadedToIBS map[libcommon.Address]bool
noopTracer // stub struct to mock not used interface methods
env *vm.EVM
tx types.TxnInfo
gasLimit uint64 // Amount of gas bought for the whole tx
interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
ctx *tracers.Context
to *libcommon.Address
txStatus uint64
addrOpCodes map[libcommon.Address]map[vm.OpCode]struct{}
}

func newZeroTracer(ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
return &zeroTracer{
tx: types.TxnInfo{
Traces: make(map[libcommon.Address]*types.TxnTrace),
},
ctx: ctx,
addrOpCodes: make(map[libcommon.Address]map[vm.OpCode]struct{}),
wasLoadedToIBS: make(map[libcommon.Address]bool),
ctx: ctx,
addrOpCodes: make(map[libcommon.Address]map[vm.OpCode]struct{}),
}, nil
}

Expand Down Expand Up @@ -178,8 +176,10 @@ func (t *zeroTracer) CaptureTxEnd(restGas uint64) {

toDelete := make([]libcommon.Address, 0)
for addr := range t.tx.Traces {
// Check again if the account was accessed through IntraBlockState
seenAccount := t.env.IntraBlockState().SeenAccount(addr)
// If an account was never accessed through IntraBlockState, it means that never there was an OpCode that read into it or checks whether it exists in the state trie, and therefore we don't need the trace of it.
if _, ok := t.wasLoadedToIBS[addr]; !ok {
if !seenAccount {
toDelete = append(toDelete, addr)
continue
}
Expand Down Expand Up @@ -344,11 +344,6 @@ func (t *zeroTracer) Stop(err error) {
}

func (t *zeroTracer) addAccountToTrace(addr libcommon.Address) {
hasLiveAccount := t.env.IntraBlockState().HasLiveAccount(addr)
if hasLiveAccount {
t.wasLoadedToIBS[addr] = true
}

if _, ok := t.tx.Traces[addr]; ok {
return
}
Expand Down

0 comments on commit a50e685

Please sign in to comment.