From 596b3509f4024774778b474660fa5d1d0733a16c Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Tue, 4 Mar 2025 11:10:12 +0800 Subject: [PATCH] Pass custom precompiles in gasestimator --- eth/gasestimator/gasestimator.go | 11 ++++++----- lib/ethapi/api.go | 22 ++++++++++++---------- lib/ethapi/api_test.go | 1 + lib/ethapi/backend.go | 2 ++ lib/ethapi/transaction_args_test.go | 2 ++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/eth/gasestimator/gasestimator.go b/eth/gasestimator/gasestimator.go index 4b235de269..4ef81f4a68 100644 --- a/eth/gasestimator/gasestimator.go +++ b/eth/gasestimator/gasestimator.go @@ -37,10 +37,11 @@ import ( // these together, it would be excessively hard to test. Splitting the parts out // allows testing without needing a proper live chain. type Options struct { - Config *params.ChainConfig // Chain configuration for hard fork selection - Chain core.ChainContext // Chain context to access past block hashes - Header *types.Header // Header defining the block context to execute in - State vm.StateDB // Pre-state on top of which to estimate the gas + Config *params.ChainConfig // Chain configuration for hard fork selection + Chain core.ChainContext // Chain context to access past block hashes + Header *types.Header // Header defining the block context to execute in + State vm.StateDB // Pre-state on top of which to estimate the gas + CustomPrecompiles map[common.Address]vm.PrecompiledContract ErrorRatio float64 // Allowed overestimation ratio for faster estimation termination } @@ -217,7 +218,7 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio evmContext = core.NewEVMBlockContext(opts.Header, opts.Chain, nil) dirtyState = opts.State.Copy() - evm = vm.NewEVM(evmContext, msgContext, dirtyState, opts.Config, vm.Config{NoBaseFee: true}, nil) + evm = vm.NewEVM(evmContext, msgContext, dirtyState, opts.Config, vm.Config{NoBaseFee: true}, opts.CustomPrecompiles) ) dirtyState.SetEVM(evm) diff --git a/lib/ethapi/api.go b/lib/ethapi/api.go index 1a4e78224d..65338feaf4 100644 --- a/lib/ethapi/api.go +++ b/lib/ethapi/api.go @@ -1194,11 +1194,12 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr } // Construct the gas estimator option from the user input opts := &gasestimator.Options{ - Config: b.ChainConfig(), - Chain: NewChainContext(ctx, b), - Header: header, - State: state, - ErrorRatio: estimateGasErrorRatio, + Config: b.ChainConfig(), + Chain: NewChainContext(ctx, b), + Header: header, + State: state, + ErrorRatio: estimateGasErrorRatio, + CustomPrecompiles: b.GetCustomPrecompiles(), } if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil { @@ -1233,11 +1234,12 @@ func DoEstimateGasAfterCalls(ctx context.Context, b Backend, args TransactionArg } // Construct the gas estimator option from the user input opts := &gasestimator.Options{ - Config: b.ChainConfig(), - Chain: NewChainContext(ctx, b), - Header: header, - State: state, - ErrorRatio: estimateGasErrorRatio, + Config: b.ChainConfig(), + Chain: NewChainContext(ctx, b), + Header: header, + State: state, + ErrorRatio: estimateGasErrorRatio, + CustomPrecompiles: b.GetCustomPrecompiles(), } 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 80f6cfe261..9291243229 100644 --- a/lib/ethapi/api_test.go +++ b/lib/ethapi/api_test.go @@ -595,6 +595,7 @@ 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 TestEstimateGas(t *testing.T) { t.Parallel() diff --git a/lib/ethapi/backend.go b/lib/ethapi/backend.go index fc3ba3f85c..7f1974b5d1 100644 --- a/lib/ethapi/backend.go +++ b/lib/ethapi/backend.go @@ -96,6 +96,8 @@ type Backend interface { SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription BloomStatus() (uint64, uint64) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) + + GetCustomPrecompiles() 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 fce79da655..94280797fb 100644 --- a/lib/ethapi/transaction_args_test.go +++ b/lib/ethapi/transaction_args_test.go @@ -342,3 +342,5 @@ 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 }