Skip to content

Commit 223fe3f

Browse files
authored
Merge pull request #16229 from karalabe/evm-call-fix
cmd/evm, core/vm, internal/ethapi: don't disable call gas metering
2 parents 478143d + b7e57ca commit 223fe3f

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

cmd/evm/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ var (
8686
Name: "create",
8787
Usage: "indicates the action should be create rather than call",
8888
}
89-
DisableGasMeteringFlag = cli.BoolFlag{
90-
Name: "nogasmetering",
91-
Usage: "disable gas metering",
92-
}
9389
GenesisFlag = cli.StringFlag{
9490
Name: "prestate",
9591
Usage: "JSON file with prestate (genesis) config",
@@ -128,7 +124,6 @@ func init() {
128124
ValueFlag,
129125
DumpFlag,
130126
InputFlag,
131-
DisableGasMeteringFlag,
132127
MemProfileFlag,
133128
CPUProfileFlag,
134129
StatDumpFlag,

cmd/evm/runner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ func runCmd(ctx *cli.Context) error {
161161
GasPrice: utils.GlobalBig(ctx, PriceFlag.Name),
162162
Value: utils.GlobalBig(ctx, ValueFlag.Name),
163163
EVMConfig: vm.Config{
164-
Tracer: tracer,
165-
Debug: ctx.GlobalBool(DebugFlag.Name) || ctx.GlobalBool(MachineFlag.Name),
166-
DisableGasMetering: ctx.GlobalBool(DisableGasMeteringFlag.Name),
164+
Tracer: tracer,
165+
Debug: ctx.GlobalBool(DebugFlag.Name) || ctx.GlobalBool(MachineFlag.Name),
167166
},
168167
}
169168

core/vm/interpreter.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ type Config struct {
3737
// NoRecursion disabled Interpreter call, callcode,
3838
// delegate call and create.
3939
NoRecursion bool
40-
// Disable gas metering
41-
DisableGasMetering bool
4240
// Enable recording of SHA3/keccak preimages
4341
EnablePreimageRecording bool
4442
// JumpTable contains the EVM instruction table. This
@@ -189,14 +187,11 @@ func (in *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err er
189187
return nil, errGasUintOverflow
190188
}
191189
}
192-
193-
if !in.cfg.DisableGasMetering {
194-
// consume the gas and return an error if not enough gas is available.
195-
// cost is explicitly set so that the capture state defer method cas get the proper cost
196-
cost, err = operation.gasCost(in.gasTable, in.evm, contract, stack, mem, memorySize)
197-
if err != nil || !contract.UseGas(cost) {
198-
return nil, ErrOutOfGas
199-
}
190+
// consume the gas and return an error if not enough gas is available.
191+
// cost is explicitly set so that the capture state defer method cas get the proper cost
192+
cost, err = operation.gasCost(in.gasTable, in.evm, contract, stack, mem, memorySize)
193+
if err != nil || !contract.UseGas(cost) {
194+
return nil, ErrOutOfGas
200195
}
201196
if memorySize > 0 {
202197
mem.Resize(memorySize)

internal/ethapi/api.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ type CallArgs struct {
611611
Data hexutil.Bytes `json:"data"`
612612
}
613613

614-
func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config) ([]byte, uint64, bool, error) {
614+
func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config, timeout time.Duration) ([]byte, uint64, bool, error) {
615615
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
616616

617617
state, header, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
@@ -630,7 +630,7 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
630630
// Set default gas & gas price if none were set
631631
gas, gasPrice := uint64(args.Gas), args.GasPrice.ToInt()
632632
if gas == 0 {
633-
gas = 50000000
633+
gas = math.MaxUint64 / 2
634634
}
635635
if gasPrice.Sign() == 0 {
636636
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
@@ -642,14 +642,14 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
642642
// Setup context so it may be cancelled the call has completed
643643
// or, in case of unmetered gas, setup a context with a timeout.
644644
var cancel context.CancelFunc
645-
if vmCfg.DisableGasMetering {
646-
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
645+
if timeout > 0 {
646+
ctx, cancel = context.WithTimeout(ctx, timeout)
647647
} else {
648648
ctx, cancel = context.WithCancel(ctx)
649649
}
650650
// Make sure the context is cancelled when the call has completed
651651
// this makes sure resources are cleaned up.
652-
defer func() { cancel() }()
652+
defer cancel()
653653

654654
// Get a new instance of the EVM.
655655
evm, vmError, err := s.b.GetEVM(ctx, msg, state, header, vmCfg)
@@ -676,7 +676,7 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
676676
// Call executes the given transaction on the state for the given block number.
677677
// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values.
678678
func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (hexutil.Bytes, error) {
679-
result, _, _, err := s.doCall(ctx, args, blockNr, vm.Config{DisableGasMetering: true})
679+
result, _, _, err := s.doCall(ctx, args, blockNr, vm.Config{}, 5*time.Second)
680680
return (hexutil.Bytes)(result), err
681681
}
682682

@@ -705,7 +705,7 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (h
705705
executable := func(gas uint64) bool {
706706
args.Gas = hexutil.Uint64(gas)
707707

708-
_, _, failed, err := s.doCall(ctx, args, rpc.PendingBlockNumber, vm.Config{})
708+
_, _, failed, err := s.doCall(ctx, args, rpc.PendingBlockNumber, vm.Config{}, 0)
709709
if err != nil || failed {
710710
return false
711711
}

0 commit comments

Comments
 (0)