From 5373881bfcfcf7543716245501b29fb4e8634700 Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Fri, 4 Apr 2025 18:17:01 +0800 Subject: [PATCH] extend custom precompiles interface --- eth/api_backend.go | 6 ++++-- eth/tracers/api.go | 10 +++++----- eth/tracers/api_test.go | 4 +++- lib/ethapi/api.go | 4 ++-- lib/ethapi/api_test.go | 4 +++- lib/ethapi/backend.go | 2 +- lib/ethapi/transaction_args_test.go | 4 +++- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index e8515bf324..0983056bca 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -259,7 +259,7 @@ func (b *EthAPIBackend) GetEVM(ctx context.Context, msg *core.Message, state vm. } else { context = core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) } - return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig, b.GetCustomPrecompiles()) + return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig, b.GetCustomPrecompiles(header.Number.Int64())) } func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { @@ -415,4 +415,6 @@ func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Blo return b.eth.stateAtTransaction(ctx, block, txIndex, reexec) } -func (b *EthAPIBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil } +func (b *EthAPIBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract { + return nil +} diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 7cca39ddbb..4882fc513f 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -89,7 +89,7 @@ type Backend interface { ChainDb() ethdb.Database StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base vm.StateDB, readOnly bool, preferDisk bool) (vm.StateDB, StateReleaseFunc, error) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, vm.StateDB, StateReleaseFunc, error) - GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract + GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract } // API is the collection of tracing APIs exposed over the private debugging endpoint. @@ -534,7 +534,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config var ( msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee()) txContext = core.NewEVMTxContext(msg) - vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}, api.backend.GetCustomPrecompiles()) + vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}, api.backend.GetCustomPrecompiles(block.Number().Int64())) ) statedb.SetTxContext(tx.Hash(), i) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil { @@ -684,7 +684,7 @@ txloop: // Generate the next state snapshot fast without tracing msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee()) statedb.SetTxContext(tx.Hash(), i) - vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}, api.backend.GetCustomPrecompiles()) + vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}, api.backend.GetCustomPrecompiles(block.Number().Int64())) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil { failed = err break txloop @@ -790,7 +790,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } } // Execute the transaction and flush any traces to disk - vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf, api.backend.GetCustomPrecompiles()) + vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf, api.backend.GetCustomPrecompiles(block.Number().Int64())) statedb.SetTxContext(tx.Hash(), i) vmConf.Tracer.OnTxStart(vmenv.GetVMContext(), tx, msg.From) vmRet, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)) @@ -988,7 +988,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor return nil, err } } - vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}, api.backend.GetCustomPrecompiles()) + vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}, api.backend.GetCustomPrecompiles(txctx.BlockNumber.Int64())) statedb.SetLogger(tracer.Hooks) // Define a meaningful timeout of a single transaction trace diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 0f4951c069..0d92b2cf1e 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -184,7 +184,9 @@ func (b *testBackend) StateAtTransaction(ctx context.Context, block *types.Block return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash()) } -func (b *testBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil } +func (b *testBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract { + return nil +} func TestTraceCall(t *testing.T) { t.Parallel() diff --git a/lib/ethapi/api.go b/lib/ethapi/api.go index 65338feaf4..2483358702 100644 --- a/lib/ethapi/api.go +++ b/lib/ethapi/api.go @@ -1199,7 +1199,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr Header: header, State: state, ErrorRatio: estimateGasErrorRatio, - CustomPrecompiles: b.GetCustomPrecompiles(), + CustomPrecompiles: b.GetCustomPrecompiles(header.Number.Int64()), } if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil { @@ -1239,7 +1239,7 @@ func DoEstimateGasAfterCalls(ctx context.Context, b Backend, args TransactionArg Header: header, State: state, ErrorRatio: estimateGasErrorRatio, - CustomPrecompiles: b.GetCustomPrecompiles(), + CustomPrecompiles: b.GetCustomPrecompiles(header.Number.Int64()), } if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil { diff --git a/lib/ethapi/api_test.go b/lib/ethapi/api_test.go index 9291243229..24772436f3 100644 --- a/lib/ethapi/api_test.go +++ b/lib/ethapi/api_test.go @@ -595,7 +595,9 @@ func (b testBackend) BloomStatus() (uint64, uint64) { panic("implement me") } func (b testBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { panic("implement me") } -func (b testBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil } +func (b testBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract { + return nil +} func TestEstimateGas(t *testing.T) { t.Parallel() diff --git a/lib/ethapi/backend.go b/lib/ethapi/backend.go index 7f1974b5d1..28e50bcbf9 100644 --- a/lib/ethapi/backend.go +++ b/lib/ethapi/backend.go @@ -97,7 +97,7 @@ type Backend interface { BloomStatus() (uint64, uint64) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) - GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract + GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract } func GetAPIs(apiBackend Backend) []rpc.API { diff --git a/lib/ethapi/transaction_args_test.go b/lib/ethapi/transaction_args_test.go index 94280797fb..de629a3ac2 100644 --- a/lib/ethapi/transaction_args_test.go +++ b/lib/ethapi/transaction_args_test.go @@ -343,4 +343,6 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) func (b *backendMock) Engine() consensus.Engine { return nil } -func (b *backendMock) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil } +func (b *backendMock) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract { + return nil +}