@@ -256,6 +256,19 @@ func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common
256
256
return receipt , nil
257
257
}
258
258
259
+ // HeaderByNumber returns a block header from the current canonical chain. If number is
260
+ // nil, the latest known header is returned.
261
+ func (b * SimulatedBackend ) HeaderByNumber (ctx context.Context , block * big.Int ) (* types.Header , error ) {
262
+ b .mu .Lock ()
263
+ defer b .mu .Unlock ()
264
+
265
+ if block == nil || block .Cmp (b .pendingBlock .Number ()) == 0 {
266
+ return b .blockchain .CurrentHeader (), nil
267
+ }
268
+
269
+ return b .blockchain .GetHeaderByNumber (uint64 (block .Int64 ())), nil
270
+ }
271
+
259
272
// PendingCodeAt returns the code associated with an account in the pending state.
260
273
func (b * SimulatedBackend ) PendingCodeAt (ctx context.Context , contract common.Address ) ([]byte , error ) {
261
274
b .mu .Lock ()
@@ -300,8 +313,17 @@ func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Ad
300
313
}
301
314
302
315
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
303
- // chain doens 't have miners, we just return a gas price of 1 for any call.
316
+ // chain doesn 't have miners, we just return a gas price of 1 for any call.
304
317
func (b * SimulatedBackend ) SuggestGasPrice (ctx context.Context ) (* big.Int , error ) {
318
+ if b .pendingBlock .Header ().BaseFee != nil {
319
+ return b .pendingBlock .Header ().BaseFee , nil
320
+ }
321
+ return big .NewInt (1 ), nil
322
+ }
323
+
324
+ // SuggestGasTipCap implements ContractTransactor.SuggestGasTipCap. Since the simulated
325
+ // chain doesn't have miners, we just return a gas tip of 1 for any call.
326
+ func (b * SimulatedBackend ) SuggestGasTipCap (ctx context.Context ) (* big.Int , error ) {
305
327
return big .NewInt (1 ), nil
306
328
}
307
329
@@ -358,10 +380,38 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call XDPoSChain.Call
358
380
// callContract implements common code between normal and pending contract calls.
359
381
// state is modified during execution, make sure to copy it if necessary.
360
382
func (b * SimulatedBackend ) callContract (ctx context.Context , call XDPoSChain.CallMsg , block * types.Block , statedb * state.StateDB ) (ret []byte , usedGas uint64 , failed bool , err error ) {
361
- // Ensure message is initialized properly.
362
- if call .GasPrice == nil {
363
- call .GasPrice = big .NewInt (1 )
383
+ // Gas prices post 1559 need to be initialized
384
+ if call .GasPrice != nil && (call .GasFeeCap != nil || call .GasTipCap != nil ) {
385
+ return nil , 0 , false , errors .New ("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified" )
386
+ }
387
+ head := b .blockchain .CurrentHeader ()
388
+ if ! b .blockchain .Config ().IsEIP1559 (head .Number ) {
389
+ // If there's no basefee, then it must be a non-1559 execution
390
+ if call .GasPrice == nil {
391
+ call .GasPrice = new (big.Int )
392
+ }
393
+ call .GasFeeCap , call .GasTipCap = call .GasPrice , call .GasPrice
394
+ } else {
395
+ // A basefee is provided, necessitating 1559-type execution
396
+ if call .GasPrice != nil {
397
+ // User specified the legacy gas field, convert to 1559 gas typing
398
+ call .GasFeeCap , call .GasTipCap = call .GasPrice , call .GasPrice
399
+ } else {
400
+ // User specified 1559 gas feilds (or none), use those
401
+ if call .GasFeeCap == nil {
402
+ call .GasFeeCap = new (big.Int )
403
+ }
404
+ if call .GasTipCap == nil {
405
+ call .GasTipCap = new (big.Int )
406
+ }
407
+ // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
408
+ call .GasPrice = new (big.Int )
409
+ if call .GasFeeCap .BitLen () > 0 || call .GasTipCap .BitLen () > 0 {
410
+ call .GasPrice = math .BigMin (new (big.Int ).Add (call .GasTipCap , head .BaseFee ), call .GasFeeCap )
411
+ }
412
+ }
364
413
}
414
+ // Ensure message is initialized properly.
365
415
if call .Gas == 0 {
366
416
call .Gas = 50000000
367
417
}
@@ -384,7 +434,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call XDPoSChain.Cal
384
434
evmContext := core .NewEVMBlockContext (block .Header (), b .blockchain , nil )
385
435
// Create a new environment which holds all relevant information
386
436
// about the transaction and calling mechanisms.
387
- vmenv := vm .NewEVM (evmContext , txContext , statedb , nil , b .config , vm.Config {})
437
+ vmenv := vm .NewEVM (evmContext , txContext , statedb , nil , b .config , vm.Config {NoBaseFee : true })
388
438
gaspool := new (core.GasPool ).AddGas (math .MaxUint64 )
389
439
owner := common.Address {}
390
440
ret , usedGas , failed , err , _ = core .NewStateTransition (vmenv , msg , gaspool ).TransitionDb (owner )
@@ -522,9 +572,11 @@ type callMsg struct {
522
572
523
573
func (m callMsg ) From () common.Address { return m .CallMsg .From }
524
574
func (m callMsg ) Nonce () uint64 { return 0 }
525
- func (m callMsg ) CheckNonce () bool { return false }
575
+ func (m callMsg ) IsFake () bool { return true }
526
576
func (m callMsg ) To () * common.Address { return m .CallMsg .To }
527
577
func (m callMsg ) GasPrice () * big.Int { return m .CallMsg .GasPrice }
578
+ func (m callMsg ) GasFeeCap () * big.Int { return m .CallMsg .GasFeeCap }
579
+ func (m callMsg ) GasTipCap () * big.Int { return m .CallMsg .GasTipCap }
528
580
func (m callMsg ) Gas () uint64 { return m .CallMsg .Gas }
529
581
func (m callMsg ) Value () * big.Int { return m .CallMsg .Value }
530
582
func (m callMsg ) Data () []byte { return m .CallMsg .Data }
@@ -554,7 +606,11 @@ func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*t
554
606
}
555
607
556
608
func (fb * filterBackend ) GetReceipts (ctx context.Context , hash common.Hash ) (types.Receipts , error ) {
557
- return core .GetBlockReceipts (fb .db , hash , core .GetBlockNumber (fb .db , hash )), nil
609
+ number := rawdb .ReadHeaderNumber (fb .db , hash )
610
+ if number == nil {
611
+ return nil , nil
612
+ }
613
+ return rawdb .ReadReceipts (fb .db , hash , * number , fb .bc .Config ()), nil
558
614
}
559
615
560
616
func (fb * filterBackend ) GetBody (ctx context.Context , hash common.Hash , number rpc.BlockNumber ) (* types.Body , error ) {
0 commit comments