Skip to content

Commit

Permalink
Merge pull request #43 from sei-protocol/tony/fix-gasestimator
Browse files Browse the repository at this point in the history
Pass custom precompiles in gasestimator
  • Loading branch information
codchen authored Mar 4, 2025
2 parents a9c1e92 + 596b350 commit 7e26589
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
11 changes: 6 additions & 5 deletions eth/gasestimator/gasestimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 12 additions & 10 deletions lib/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lib/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions lib/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 7e26589

Please sign in to comment.