From 599c461cad727174ecc4c417bfbe1b2118ee3bc2 Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Fri, 29 Mar 2024 12:56:46 -0400 Subject: [PATCH] The beacon root must be processed before any transactions to have correct state The beacon root when applied in `state_processor.go` is performed right before executing transaction. That means that contract reliying on this value would query the same value found in the block header. In that spirit, it means that any tracing/operation relying on state data which touches transaction must have updated the beacon root before any transaction processing. This PR brings this where I spotted where it was important through `StateAtTransaction` and those using with `StateAtBlock` and transactions. For now I've put the beacon root update outside of `StateAtBlock`. I was reluctant to include it in `StateAtBlock` because technically the state at block doesn't include any state happening "in the block". But everywhere that `StateAtBlock` was used, it felt setting the beacon root was needed, so it would be posssible to change the semantic of the function and run any system pre-hooks. Kinda relevant with PR #29372 --- eth/state_accessor.go | 8 ++++++++ eth/tracers/api.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 770532cbfe..24d9c6b26a 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -233,6 +233,14 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, if err != nil { return nil, vm.BlockContext{}, nil, nil, err } + + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, eth.blockchain.Config(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + if txIndex == 0 && len(block.Transactions()) == 0 { return nil, vm.BlockContext{}, statedb, release, nil } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 7a7c5e48d9..e234851e37 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -376,6 +376,14 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed failed = err break } + + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, api.backend.ChainConfig(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + // Clean out any pending release functions of trace state. Note this // step must be done after constructing tracing state, because the // tracing state of block next depends on the parent state and construction @@ -518,6 +526,13 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config } defer release() + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, api.backend.ChainConfig(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + var ( roots []common.Hash signer = types.MakeSigner(api.backend.ChainConfig(), block.Number(), block.Time()) @@ -585,6 +600,13 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac } defer release() + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, api.backend.ChainConfig(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + // JS tracers have high overhead. In this case run a parallel // process that generates states in one thread and traces txes // in separate worker threads. @@ -728,6 +750,13 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } defer release() + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, api.backend.ChainConfig(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + // Retrieve the tracing configurations, or use default values var ( logConfig logger.Config @@ -916,6 +945,15 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc } defer release() + if config != nil && config.TxIndex != nil { + if beaconRoot := block.BeaconRoot(); beaconRoot != nil { + context := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) + vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, api.backend.ChainConfig(), vm.Config{}) + + core.ProcessBeaconBlockRoot(*block.BeaconRoot(), vmenv, statedb) + } + } + vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) // Apply the customization rules if required. if config != nil {