Skip to content

Commit

Permalink
Merge branch 'feature/erigon-live-tracer-port' into feat/firehose-liv…
Browse files Browse the repository at this point in the history
…e-tracer
  • Loading branch information
dhyaniarun1993 committed Nov 28, 2023
2 parents 6766b46 + 0c765e7 commit dfcc4d0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
55 changes: 33 additions & 22 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,24 +329,27 @@ func (sdb *IntraBlockState) AddBalance(addr libcommon.Address, amount *uint256.I
if sdb.trace {
fmt.Printf("AddBalance %x, %d\n", addr, amount)
}
// If this account has not been read, add to the balance increment map
_, needAccount := sdb.stateObjects[addr]
if !needAccount && addr == ripemd && amount.IsZero() {
needAccount = true
}
if !needAccount {
sdb.journal.append(balanceIncrease{
account: &addr,
increase: *amount,
})
bi, ok := sdb.balanceInc[addr]
if !ok {
bi = &BalanceIncrease{}
sdb.balanceInc[addr] = bi

if sdb.logger == nil {
// If this account has not been read, add to the balance increment map
_, needAccount := sdb.stateObjects[addr]
if !needAccount && addr == ripemd && amount.IsZero() {
needAccount = true
}
if !needAccount {
sdb.journal.append(balanceIncrease{
account: &addr,
increase: *amount,
})
bi, ok := sdb.balanceInc[addr]
if !ok {
bi = &BalanceIncrease{}
sdb.balanceInc[addr] = bi
}
bi.increase.Add(&bi.increase, amount)
bi.count++
return
}
bi.increase.Add(&bi.increase, amount)
bi.count++
return
}

stateObject := sdb.GetOrNewStateObject(addr)
Expand Down Expand Up @@ -434,11 +437,18 @@ func (sdb *IntraBlockState) Selfdestruct(addr libcommon.Address) bool {
if stateObject == nil || stateObject.deleted {
return false
}

prevBalance := *stateObject.Balance()
sdb.journal.append(selfdestructChange{
account: &addr,
prev: stateObject.selfdestructed,
prevbalance: *stateObject.Balance(),
prevbalance: prevBalance,
})

if sdb.logger != nil && !prevBalance.IsZero() {
sdb.logger.OnBalanceChange(addr, &prevBalance, uint256.NewInt(0), evmtypes.BalanceChangeSuicideWithdraw)
}

stateObject.markSelfdestructed()
stateObject.createdContract = false
stateObject.data.Balance.Clear()
Expand Down Expand Up @@ -551,13 +561,14 @@ func (sdb *IntraBlockState) createObject(addr libcommon.Address, previous *state
newobj.setNonce(0) // sets the object to dirty
if previous == nil {
sdb.journal.append(createObjectChange{account: &addr})
// TODO: add isPrecompile check
if sdb.logger != nil {
sdb.logger.OnNewAccount(addr)
}
} else {
sdb.journal.append(resetObjectChange{account: &addr, prev: previous})
}

if sdb.logger != nil {
sdb.logger.OnNewAccount(addr)
}

newobj.newlyCreated = true
sdb.setStateObject(addr, newobj)
return newobj
Expand Down
12 changes: 6 additions & 6 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,18 @@ func (evm *EVM) call(typ OpCode, caller ContractRef, addr libcommon.Address, inp
depth := evm.interpreter.Depth()
p, isPrecompile := evm.precompile(addr)

var code []byte
if !isPrecompile {
code = evm.intraBlockState.GetCode(addr)
}

// Invoke tracer hooks that signal entering/exiting a call frame
if evm.Config().Tracer != nil {
v := value
if typ == STATICCALL {
v = nil
}
evm.captureBegin(depth == 0, typ, caller.Address(), addr, isPrecompile, input, gas, v, nil)
evm.captureBegin(depth == 0, typ, caller.Address(), addr, isPrecompile, input, gas, v, code)
defer func(startGas uint64) {
evm.captureEnd(depth == 0, typ, startGas, leftOverGas, ret, err)
}(gas)
Expand All @@ -190,11 +195,6 @@ func (evm *EVM) call(typ OpCode, caller ContractRef, addr libcommon.Address, inp
}
}

var code []byte
if !isPrecompile {
code = evm.intraBlockState.GetCode(addr)
}

snapshot := evm.intraBlockState.Snapshot()

if typ == CALL {
Expand Down
4 changes: 2 additions & 2 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
in.depth++
defer func() {
// first: capture data/memory/state/depth/etc... then clenup them
if in.cfg.Debug && err != nil {
if in.cfg.Tracer != nil && err != nil {
if !logged {
in.cfg.Tracer.CaptureState(pcCopy, op, gasCopy, cost, callContext, in.returnData, in.depth, VMErrorFromErr(err)) //nolint:errcheck
} else {
Expand Down Expand Up @@ -297,7 +297,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
mem.Resize(memorySize)
}
}
if in.cfg.Debug {
if in.cfg.Tracer != nil {
in.cfg.Tracer.OnGasChange(gasCopy, gasCopy-cost, GasChangeCallOpCode)
in.cfg.Tracer.CaptureState(_pc, op, gasCopy, cost, callContext, in.returnData, in.depth, VMErrorFromErr(err)) //nolint:errcheck
logged = true
Expand Down
4 changes: 2 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
terseLogger.SetHandler(log.LvlFilterHandler(log.LvlWarn, log.StderrHandler))
// Needs its own notifications to not update RPC daemon and txpool about pending blocks
stateSync := stages2.NewInMemoryExecution(backend.sentryCtx, backend.chainDB, config, backend.sentriesClient,
dirs, notifications, blockReader, blockWriter, backend.agg, backend.silkworm, terseLogger)
dirs, notifications, blockReader, blockWriter, backend.agg, backend.silkworm, terseLogger, nil)
chainReader := stagedsync.NewChainReaderImpl(chainConfig, batch, blockReader, logger)
// We start the mining step
if err := stages2.StateStep(ctx, chainReader, backend.engine, batch, backend.blockWriter, stateSync, backend.sentriesClient.Bd, header, body, unwindPoint, headersChain, bodiesChain, config.HistoryV3); err != nil {
Expand Down Expand Up @@ -782,7 +782,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
hook := stages2.NewHook(backend.sentryCtx, backend.chainDB, backend.notifications, backend.stagedSync, backend.blockReader, backend.chainConfig, backend.logger, backend.sentriesClient.UpdateHead)

checkStateRoot := true
pipelineStages := stages2.NewPipelineStages(ctx, chainKv, config, backend.sentriesClient, backend.notifications, backend.downloaderClient, blockReader, blockRetire, backend.agg, backend.silkworm, backend.forkValidator, logger, checkStateRoot)
pipelineStages := stages2.NewPipelineStages(ctx, chainKv, config, backend.sentriesClient, backend.notifications, backend.downloaderClient, blockReader, blockRetire, backend.agg, backend.silkworm, backend.forkValidator, logger, tracer, checkStateRoot)
backend.pipelineStagedSync = stagedsync.New(pipelineStages, stagedsync.PipelineUnwindOrder, stagedsync.PipelinePruneOrder, logger)
backend.eth1ExecutionServer = eth1.NewEthereumExecutionModule(blockReader, chainKv, backend.pipelineStagedSync, backend.forkValidator, chainConfig, assembleBlockPOS, hook, backend.notifications.Accumulator, backend.notifications.StateChangesConsumer, logger, backend.engine, config.HistoryV3)
executionRpc := direct.NewExecutionClientDirect(backend.eth1ExecutionServer)
Expand Down
4 changes: 2 additions & 2 deletions turbo/stages/mock/mock_sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
terseLogger.SetHandler(log.LvlFilterHandler(log.LvlWarn, log.StderrHandler))
// Needs its own notifications to not update RPC daemon and txpool about pending blocks
stateSync := stages2.NewInMemoryExecution(mock.Ctx, mock.DB, &cfg, mock.sentriesClient,
dirs, notifications, mock.BlockReader, blockWriter, mock.agg, nil, terseLogger)
dirs, notifications, mock.BlockReader, blockWriter, mock.agg, nil, terseLogger, nil)
chainReader := stagedsync.NewChainReaderImpl(mock.ChainConfig, batch, mock.BlockReader, logger)
// We start the mining step
if err := stages2.StateStep(ctx, chainReader, mock.Engine, batch, blockWriter, stateSync, mock.sentriesClient.Bd, header, body, unwindPoint, headersChain, bodiesChain, histV3); err != nil {
Expand Down Expand Up @@ -461,7 +461,7 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK

cfg.Genesis = gspec
pipelineStages := stages2.NewPipelineStages(mock.Ctx, db, &cfg, mock.sentriesClient, mock.Notifications,
snapshotsDownloader, mock.BlockReader, blockRetire, mock.agg, nil, forkValidator, logger, checkStateRoot)
snapshotsDownloader, mock.BlockReader, blockRetire, mock.agg, nil, forkValidator, logger, nil, checkStateRoot)
mock.posStagedSync = stagedsync.New(pipelineStages, stagedsync.PipelineUnwindOrder, stagedsync.PipelinePruneOrder, logger)

mock.Eth1ExecutionService = eth1.NewEthereumExecutionModule(mock.BlockReader, mock.DB, mock.posStagedSync, forkValidator, mock.ChainConfig, assembleBlockPOS, nil, mock.Notifications.Accumulator, mock.Notifications.StateChangesConsumer, logger, engine, histV3)
Expand Down
7 changes: 4 additions & 3 deletions turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func NewPipelineStages(ctx context.Context,
silkworm *silkworm.Silkworm,
forkValidator *engine_helpers.ForkValidator,
logger log.Logger,
tracer tracers.Tracer,
checkStateRoot bool,
) []*stagedsync.Stage {
dirs := cfg.Dirs
Expand All @@ -549,7 +550,7 @@ func NewPipelineStages(ctx context.Context,
nil,
controlServer.ChainConfig,
controlServer.Engine,
&vm.Config{},
&vm.Config{Tracer: tracer},
notifications.Accumulator,
cfg.StateStream,
/*stateStream=*/ false,
Expand All @@ -574,7 +575,7 @@ func NewPipelineStages(ctx context.Context,

func NewInMemoryExecution(ctx context.Context, db kv.RwDB, cfg *ethconfig.Config, controlServer *sentry_multi_client.MultiClient,
dirs datadir.Dirs, notifications *shards.Notifications, blockReader services.FullBlockReader, blockWriter *blockio.BlockWriter, agg *state.AggregatorV3,
silkworm *silkworm.Silkworm, logger log.Logger) *stagedsync.Sync {
silkworm *silkworm.Silkworm, logger log.Logger, tracer tracers.Tracer) *stagedsync.Sync {
return stagedsync.New(
stagedsync.StateStages(ctx,
stagedsync.StageHeadersCfg(db, controlServer.Hd, controlServer.Bd, *controlServer.ChainConfig, controlServer.SendHeaderRequest, controlServer.PropagateNewBlockHashes, controlServer.Penalize, cfg.BatchSize, false, blockReader, blockWriter, dirs.Tmp, nil, nil, nil),
Expand All @@ -588,7 +589,7 @@ func NewInMemoryExecution(ctx context.Context, db kv.RwDB, cfg *ethconfig.Config
nil,
controlServer.ChainConfig,
controlServer.Engine,
&vm.Config{},
&vm.Config{Tracer: tracer},
notifications.Accumulator,
cfg.StateStream,
true,
Expand Down

0 comments on commit dfcc4d0

Please sign in to comment.