Skip to content

Commit b0b20cd

Browse files
gzliudanwanwiset25
authored andcommitted
core, internal: support various eth_call invocations post 1559 (ethereum#23027)
1 parent 85e161f commit b0b20cd

File tree

9 files changed

+73
-61
lines changed

9 files changed

+73
-61
lines changed

core/state_transition.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,26 @@ func (st *StateTransition) preCheck() error {
223223
}
224224
// Make sure that transaction gasFeeCap is greater than the baseFee (post london)
225225
if st.evm.ChainConfig().IsEIP1559(st.evm.Context.BlockNumber) {
226-
if l := st.gasFeeCap.BitLen(); l > 256 {
227-
return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh,
228-
msg.From().Hex(), l)
229-
}
230-
if l := st.gasTipCap.BitLen(); l > 256 {
231-
return fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh,
232-
msg.From().Hex(), l)
233-
}
234-
if st.gasFeeCap.Cmp(st.gasTipCap) < 0 {
235-
return fmt.Errorf("%w: address %v, maxPriorityFeePerGas: %s, maxFeePerGas: %s", ErrTipAboveFeeCap,
236-
msg.From().Hex(), st.gasTipCap, st.gasFeeCap)
237-
}
238-
// This will panic if baseFee is nil, but basefee presence is verified
239-
// as part of header validation.
240-
if st.gasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 {
241-
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", ErrFeeCapTooLow,
242-
msg.From().Hex(), st.gasFeeCap, st.evm.Context.BaseFee)
226+
// Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call)
227+
if !st.evm.Config.NoBaseFee || st.gasFeeCap.BitLen() > 0 || st.gasTipCap.BitLen() > 0 {
228+
if l := st.gasFeeCap.BitLen(); l > 256 {
229+
return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh,
230+
msg.From().Hex(), l)
231+
}
232+
if l := st.gasTipCap.BitLen(); l > 256 {
233+
return fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh,
234+
msg.From().Hex(), l)
235+
}
236+
if st.gasFeeCap.Cmp(st.gasTipCap) < 0 {
237+
return fmt.Errorf("%w: address %v, maxPriorityFeePerGas: %s, maxFeePerGas: %s", ErrTipAboveFeeCap,
238+
msg.From().Hex(), st.gasTipCap, st.gasFeeCap)
239+
}
240+
// This will panic if baseFee is nil, but basefee presence is verified
241+
// as part of header validation.
242+
if st.gasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 {
243+
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", ErrFeeCapTooLow,
244+
msg.From().Hex(), st.gasFeeCap, st.evm.Context.BaseFee)
245+
}
243246
}
244247
}
245248
return st.buyGas()

core/vm/evm.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type EVM struct {
140140
chainRules params.Rules
141141
// virtual machine configuration options used to initialise the
142142
// evm.
143-
vmConfig Config
143+
Config Config
144144
// global (to this context) ethereum virtual machine
145145
// used throughout the execution of the tx.
146146
interpreters []Interpreter
@@ -156,21 +156,21 @@ type EVM struct {
156156

157157
// NewEVM returns a new EVM. The returned EVM is not thread safe and should
158158
// only ever be used *once*.
159-
func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, tradingStateDB *tradingstate.TradingStateDB, chainConfig *params.ChainConfig, vmConfig Config) *EVM {
159+
func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, tradingStateDB *tradingstate.TradingStateDB, chainConfig *params.ChainConfig, config Config) *EVM {
160160
evm := &EVM{
161161
Context: blockCtx,
162162
TxContext: txCtx,
163163
StateDB: statedb,
164164
tradingStateDB: tradingStateDB,
165-
vmConfig: vmConfig,
165+
Config: config,
166166
chainConfig: chainConfig,
167167
chainRules: chainConfig.Rules(blockCtx.BlockNumber),
168168
interpreters: make([]Interpreter, 0, 1),
169169
}
170170

171171
// vmConfig.EVMInterpreter will be used by EVM-C, it won't be checked here
172172
// as we always want to have the built-in EVM as the failover option.
173-
evm.interpreters = append(evm.interpreters, NewEVMInterpreter(evm, vmConfig))
173+
evm.interpreters = append(evm.interpreters, NewEVMInterpreter(evm, config))
174174
evm.interpreter = evm.interpreters[0]
175175

176176
return evm
@@ -218,13 +218,13 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
218218
if !evm.StateDB.Exist(addr) {
219219
if !isPrecompile && evm.chainRules.IsEIP158 && value.Sign() == 0 {
220220
// Calling a non existing account, don't do anything, but ping the tracer
221-
if evm.vmConfig.Debug {
221+
if evm.Config.Debug {
222222
if evm.depth == 0 {
223-
evm.vmConfig.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
224-
evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil)
223+
evm.Config.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
224+
evm.Config.Tracer.CaptureEnd(ret, 0, 0, nil)
225225
} else {
226-
evm.vmConfig.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
227-
evm.vmConfig.Tracer.CaptureExit(ret, 0, nil)
226+
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
227+
evm.Config.Tracer.CaptureExit(ret, 0, nil)
228228
}
229229
}
230230
return nil, gas, nil
@@ -234,18 +234,18 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
234234
evm.Context.Transfer(evm.StateDB, caller.Address(), addr, value)
235235

236236
// Capture the tracer start/end events in debug mode
237-
if evm.vmConfig.Debug {
237+
if evm.Config.Debug {
238238
if evm.depth == 0 {
239-
evm.vmConfig.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
239+
evm.Config.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
240240

241241
defer func(startGas uint64, startTime time.Time) { // Lazy evaluation of the parameters
242-
evm.vmConfig.Tracer.CaptureEnd(ret, startGas-gas, time.Since(startTime), err)
242+
evm.Config.Tracer.CaptureEnd(ret, startGas-gas, time.Since(startTime), err)
243243
}(gas, time.Now())
244244
} else {
245245
// Handle tracer events for entering and exiting a call frame
246-
evm.vmConfig.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
246+
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
247247
defer func(startGas uint64) {
248-
evm.vmConfig.Tracer.CaptureExit(ret, startGas-gas, err)
248+
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
249249
}(gas)
250250
}
251251
}
@@ -305,10 +305,10 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
305305
var snapshot = evm.StateDB.Snapshot()
306306

307307
// Invoke tracer hooks that signal entering/exiting a call frame
308-
if evm.vmConfig.Debug {
309-
evm.vmConfig.Tracer.CaptureEnter(CALLCODE, caller.Address(), addr, input, gas, value)
308+
if evm.Config.Debug {
309+
evm.Config.Tracer.CaptureEnter(CALLCODE, caller.Address(), addr, input, gas, value)
310310
defer func(startGas uint64) {
311-
evm.vmConfig.Tracer.CaptureExit(ret, startGas-gas, err)
311+
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
312312
}(gas)
313313
}
314314

@@ -346,10 +346,10 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
346346
var snapshot = evm.StateDB.Snapshot()
347347

348348
// Invoke tracer hooks that signal entering/exiting a call frame
349-
if evm.vmConfig.Debug {
350-
evm.vmConfig.Tracer.CaptureEnter(DELEGATECALL, caller.Address(), addr, input, gas, nil)
349+
if evm.Config.Debug {
350+
evm.Config.Tracer.CaptureEnter(DELEGATECALL, caller.Address(), addr, input, gas, nil)
351351
defer func(startGas uint64) {
352-
evm.vmConfig.Tracer.CaptureExit(ret, startGas-gas, err)
352+
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
353353
}(gas)
354354
}
355355

@@ -396,10 +396,10 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
396396
evm.StateDB.AddBalance(addr, big0)
397397

398398
// Invoke tracer hooks that signal entering/exiting a call frame
399-
if evm.vmConfig.Debug {
400-
evm.vmConfig.Tracer.CaptureEnter(STATICCALL, caller.Address(), addr, input, gas, nil)
399+
if evm.Config.Debug {
400+
evm.Config.Tracer.CaptureEnter(STATICCALL, caller.Address(), addr, input, gas, nil)
401401
defer func(startGas uint64) {
402-
evm.vmConfig.Tracer.CaptureExit(ret, startGas-gas, err)
402+
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
403403
}(gas)
404404
}
405405

@@ -480,11 +480,11 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
480480
contract := NewContract(caller, AccountRef(address), value, gas)
481481
contract.SetCodeOptionalHash(&address, codeAndHash)
482482

483-
if evm.vmConfig.Debug {
483+
if evm.Config.Debug {
484484
if evm.depth == 0 {
485-
evm.vmConfig.Tracer.CaptureStart(evm, caller.Address(), address, true, codeAndHash.code, gas, value)
485+
evm.Config.Tracer.CaptureStart(evm, caller.Address(), address, true, codeAndHash.code, gas, value)
486486
} else {
487-
evm.vmConfig.Tracer.CaptureEnter(typ, caller.Address(), address, codeAndHash.code, gas, value)
487+
evm.Config.Tracer.CaptureEnter(typ, caller.Address(), address, codeAndHash.code, gas, value)
488488
}
489489
}
490490
start := time.Now()
@@ -524,11 +524,11 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
524524
}
525525
}
526526

527-
if evm.vmConfig.Debug {
527+
if evm.Config.Debug {
528528
if evm.depth == 0 {
529-
evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
529+
evm.Config.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
530530
} else {
531-
evm.vmConfig.Tracer.CaptureExit(ret, gas-contract.Gas, err)
531+
evm.Config.Tracer.CaptureExit(ret, gas-contract.Gas, err)
532532
}
533533
}
534534
return ret, address, contract.Gas, err

core/vm/instructions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
246246
interpreter.hasher.Read(interpreter.hasherBuf[:])
247247

248248
evm := interpreter.evm
249-
if evm.vmConfig.EnablePreimageRecording {
249+
if evm.Config.EnablePreimageRecording {
250250
evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
251251
}
252252

core/vm/instructions_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestAddMod(t *testing.T) {
194194
var (
195195
env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
196196
stack = newstack()
197-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
197+
evmInterpreter = NewEVMInterpreter(env, env.Config)
198198
pc = uint64(0)
199199
)
200200
tests := []struct {
@@ -290,7 +290,7 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
290290
env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
291291
stack = newstack()
292292
scope = &ScopeContext{nil, stack, nil}
293-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
293+
evmInterpreter = NewEVMInterpreter(env, env.Config)
294294
)
295295

296296
env.interpreter = evmInterpreter
@@ -531,7 +531,7 @@ func TestOpMstore(t *testing.T) {
531531
env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
532532
stack = newstack()
533533
mem = NewMemory()
534-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
534+
evmInterpreter = NewEVMInterpreter(env, env.Config)
535535
)
536536

537537
env.interpreter = evmInterpreter
@@ -557,7 +557,7 @@ func BenchmarkOpMstore(bench *testing.B) {
557557
env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
558558
stack = newstack()
559559
mem = NewMemory()
560-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
560+
evmInterpreter = NewEVMInterpreter(env, env.Config)
561561
)
562562

563563
env.interpreter = evmInterpreter
@@ -579,7 +579,7 @@ func BenchmarkOpKeccak256(bench *testing.B) {
579579
env = NewEVM(BlockContext{}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
580580
stack = newstack()
581581
mem = NewMemory()
582-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
582+
evmInterpreter = NewEVMInterpreter(env, env.Config)
583583
)
584584
env.interpreter = evmInterpreter
585585
mem.Resize(32)
@@ -683,7 +683,7 @@ func TestRandom(t *testing.T) {
683683
env = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, nil, params.TestChainConfig, Config{})
684684
stack = newstack()
685685
pc = uint64(0)
686-
evmInterpreter = NewEVMInterpreter(env, env.vmConfig)
686+
evmInterpreter = NewEVMInterpreter(env, env.Config)
687687
)
688688
opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
689689
if len(stack.data) != 1 {

core/vm/interpreter.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
type Config struct {
2828
Debug bool // Enables debugging
2929
Tracer EVMLogger // Opcode logger
30+
NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls)
3031
EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages
3132

3233
JumpTable *JumpTable // EVM instruction table, automatically populated if unset

internal/ethapi/api.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ func (s *PublicBlockChainAPI) getCandidatesFromSmartContract() ([]utils.Masterno
12631263
return candidatesWithStakeInfo, nil
12641264
}
12651265

1266-
func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, vmCfg vm.Config, timeout time.Duration, globalGasCap uint64) ([]byte, uint64, bool, error, error) {
1266+
func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, timeout time.Duration, globalGasCap uint64) ([]byte, uint64, bool, error, error) {
12671267
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
12681268

12691269
statedb, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
@@ -1312,7 +1312,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
13121312
}
13131313

13141314
// Get a new instance of the EVM.
1315-
evm, vmError, err := b.GetEVM(ctx, msg, statedb, XDCxState, header, &vmCfg)
1315+
evm, vmError, err := b.GetEVM(ctx, msg, statedb, XDCxState, header, &vm.Config{NoBaseFee: true})
13161316
if err != nil {
13171317
return nil, 0, false, err, nil
13181318
}
@@ -1382,7 +1382,7 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args TransactionArgs, bl
13821382
if args.To != nil && *args.To == common.MasternodeVotingSMCBinary {
13831383
timeout = 0
13841384
}
1385-
result, _, failed, err, vmErr := DoCall(ctx, s.b, args, *blockNrOrHash, overrides, vm.Config{}, timeout, s.b.RPCGasCap())
1385+
result, _, failed, err, vmErr := DoCall(ctx, s.b, args, *blockNrOrHash, overrides, timeout, s.b.RPCGasCap())
13861386
if err != nil {
13871387
return nil, err
13881388
}
@@ -1438,7 +1438,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
14381438
executable := func(gas uint64) (bool, []byte, error, error) {
14391439
args.Gas = (*hexutil.Uint64)(&gas)
14401440

1441-
res, _, failed, err, vmErr := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
1441+
res, _, failed, err, vmErr := DoCall(ctx, b, args, blockNrOrHash, nil, 0, gasCap)
14421442
if err != nil {
14431443
if errors.Is(err, vm.ErrOutOfGas) || errors.Is(err, core.ErrIntrinsicGas) {
14441444
return false, nil, nil, nil // Special case, raise gas limit

internal/ethapi/transaction_args.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
159159
return nil
160160
}
161161

162-
// ToMessage converts TransactionArgs to the Message type used by the core evm
162+
// ToMessage converts th transaction arguments to the Message type used by the
163+
// core evm. This method is used in calls and traces that do not require a real
164+
// live transaction.
163165
func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap uint64, baseFee *big.Int) (types.Message, error) {
164166
// Reject invalid combinations of pre- and post-1559 fee styles
165167
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
@@ -207,9 +209,11 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap
207209
} else {
208210
// A basefee is provided, necessitating 1559-type execution
209211
if args.GasPrice != nil {
212+
// User specified the legacy gas field, convert to 1559 gas typing
210213
gasPrice = args.GasPrice.ToInt()
211214
gasFeeCap, gasTipCap = gasPrice, gasPrice
212215
} else {
216+
// User specified 1559 gas feilds (or none), use those
213217
gasFeeCap = new(big.Int)
214218
if args.MaxFeePerGas != nil {
215219
gasFeeCap = args.MaxFeePerGas.ToInt()
@@ -218,7 +222,11 @@ func (args *TransactionArgs) ToMessage(b Backend, number *big.Int, globalGasCap
218222
if args.MaxPriorityFeePerGas != nil {
219223
gasTipCap = args.MaxPriorityFeePerGas.ToInt()
220224
}
221-
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
225+
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
226+
gasPrice = new(big.Int)
227+
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
228+
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
229+
}
222230
}
223231
}
224232
value := new(big.Int)

internal/jsre/deps/bindata.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/jsre/deps/web3.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)