Merge pull request #43 from sei-protocol/tony/fix-gasestimator

Pass custom precompiles in gasestimator
This commit is contained in:
codchen 2025-03-04 11:11:10 +08:00 committed by GitHub
commit 7e26589b67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 15 deletions

View file

@ -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)

View file

@ -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 {

View file

@ -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()

View file

@ -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 {

View file

@ -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 }