From 58ebc106a685a7bf1444a568ca47042439651c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Tue, 8 Jul 2025 15:48:58 +0300 Subject: [PATCH] core: track and expose the dynamic gas calculation time --- core/blockchain.go | 3 ++- core/state_processor.go | 2 ++ core/types.go | 4 ++++ core/vm/evm.go | 4 ++++ core/vm/interpreter.go | 3 +++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index 2290b6d3cd..b30cb53191 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -97,6 +97,7 @@ var ( blockValidationTimer = metrics.NewRegisteredResettingTimer("chain/validation", nil) blockCrossValidationTimer = metrics.NewRegisteredResettingTimer("chain/crossvalidation", nil) blockExecutionTimer = metrics.NewRegisteredResettingTimer("chain/execution", nil) + dynamicGasTimer = metrics.NewRegisteredResettingTimer("chain/execution/dynamicgas", nil) blockWriteTimer = metrics.NewRegisteredResettingTimer("chain/write", nil) blockReorgMeter = metrics.NewRegisteredMeter("chain/reorg/executes", nil) @@ -2101,7 +2102,7 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s blockExecutionTimer.Update(ptime - (statedb.AccountReads + statedb.StorageReads)) // The time spent on EVM processing blockValidationTimer.Update(vtime - (triehash + trieUpdate)) // The time spent on block validation blockCrossValidationTimer.Update(xvtime) // The time spent on stateless cross validation - + dynamicGasTimer.Update(res.dynamicGasCalculation) // Write the block to the chain and get the status. var ( wstart = time.Now() diff --git a/core/state_processor.go b/core/state_processor.go index ee98326467..f169728976 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -130,6 +130,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg Requests: requests, Logs: allLogs, GasUsed: *usedGas, + + dynamicGasCalculation: evm.DynamicGasCalculation, }, nil } diff --git a/core/types.go b/core/types.go index bed20802ab..9900fa1ef3 100644 --- a/core/types.go +++ b/core/types.go @@ -18,6 +18,7 @@ package core import ( "sync/atomic" + "time" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" @@ -57,4 +58,7 @@ type ProcessResult struct { Requests [][]byte Logs []*types.Log GasUsed uint64 + + // Time measurements + dynamicGasCalculation time.Duration } diff --git a/core/vm/evm.go b/core/vm/evm.go index b45a434545..2c57adc93b 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -20,6 +20,7 @@ import ( "errors" "math/big" "sync/atomic" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" @@ -125,6 +126,9 @@ type EVM struct { // jumpDests is the aggregated result of JUMPDEST analysis made through // the life cycle of EVM. jumpDests map[common.Hash]bitvec + + // Time measurements + DynamicGasCalculation time.Duration } // NewEVM constructs an EVM instance with the supplied block context, state diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 34d19008da..a2498c2c16 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -18,6 +18,7 @@ package vm import ( "fmt" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" @@ -269,6 +270,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( // All ops with a dynamic memory usage also has a dynamic gas cost. var memorySize uint64 if operation.dynamicGas != nil { + dynamicGasCalculationStart := time.Now() // calculate the new memory size and expand the memory to fit // the operation // Memory check needs to be done prior to evaluating the dynamic gas portion, @@ -289,6 +291,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( var dynamicCost uint64 dynamicCost, err = operation.dynamicGas(in.evm, contract, stack, mem, memorySize) cost += dynamicCost // for tracing + in.evm.DynamicGasCalculation += time.Since(dynamicGasCalculationStart) if err != nil { return nil, fmt.Errorf("%w: %v", ErrOutOfGas, err) }