Add error handling in Apply

This commit is contained in:
Rez 2025-03-23 09:28:19 +11:00
parent 2a99b5532d
commit 9dae1bab9b
No known key found for this signature in database
4 changed files with 19 additions and 5 deletions

View file

@ -223,7 +223,9 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
dirtyState = opts.State.Copy() dirtyState = opts.State.Copy()
) )
if opts.BlockOverrides != nil { if opts.BlockOverrides != nil {
opts.BlockOverrides.Apply(&evmContext) if err := opts.BlockOverrides.Apply(&evmContext); err != nil {
return nil, err
}
} }
// Lower the basefee to 0 to avoid breaking EVM // Lower the basefee to 0 to avoid breaking EVM
// invariants (basefee < feecap). // invariants (basefee < feecap).

View file

@ -950,7 +950,9 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
// Apply the customization rules if required. // Apply the customization rules if required.
if config != nil { if config != nil {
config.BlockOverrides.Apply(&vmctx) if overrideErr := config.BlockOverrides.Apply(&vmctx); overrideErr != nil {
return nil, overrideErr
}
rules := api.backend.ChainConfig().Rules(vmctx.BlockNumber, vmctx.Random != nil, vmctx.Time) rules := api.backend.ChainConfig().Rules(vmctx.BlockNumber, vmctx.Random != nil, vmctx.Time)
precompiles = vm.ActivePrecompiledContracts(rules) precompiles = vm.ActivePrecompiledContracts(rules)

View file

@ -660,7 +660,9 @@ func (context *ChainContext) Config() *params.ChainConfig {
func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.StateDB, header *types.Header, overrides *override.StateOverride, blockOverrides *override.BlockOverrides, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error) { func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.StateDB, header *types.Header, overrides *override.StateOverride, blockOverrides *override.BlockOverrides, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error) {
blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil) blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil)
if blockOverrides != nil { if blockOverrides != nil {
blockOverrides.Apply(&blockCtx) if err := blockOverrides.Apply(&blockCtx); err != nil {
return nil, err
}
} }
rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time) rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time)
precompiles := vm.ActivePrecompiledContracts(rules) precompiles := vm.ActivePrecompiledContracts(rules)

View file

@ -17,6 +17,7 @@
package override package override
import ( import (
"errors"
"fmt" "fmt"
"math/big" "math/big"
@ -133,9 +134,15 @@ type BlockOverrides struct {
} }
// Apply overrides the given header fields into the given block context. // Apply overrides the given header fields into the given block context.
func (o *BlockOverrides) Apply(blockCtx *vm.BlockContext) { func (o *BlockOverrides) Apply(blockCtx *vm.BlockContext) error {
if o == nil { if o == nil {
return return nil
}
if o.BeaconRoot != nil {
return errors.New(`"beaconRoot" is not supported for this RPC method`)
}
if o.Withdrawals != nil {
return errors.New(`"withdrawals" is not supported for this RPC method`)
} }
if o.Number != nil { if o.Number != nil {
blockCtx.BlockNumber = o.Number.ToInt() blockCtx.BlockNumber = o.Number.ToInt()
@ -161,6 +168,7 @@ func (o *BlockOverrides) Apply(blockCtx *vm.BlockContext) {
if o.BlobBaseFee != nil { if o.BlobBaseFee != nil {
blockCtx.BlobBaseFee = o.BlobBaseFee.ToInt() blockCtx.BlobBaseFee = o.BlobBaseFee.ToInt()
} }
return nil
} }
// MakeHeader returns a new header object with the overridden // MakeHeader returns a new header object with the overridden