@@ -674,7 +674,7 @@ type CallArgs struct {
674674 Data * hexutil.Bytes `json:"data"`
675675}
676676
677- func DoCall (ctx context.Context , b Backend , args CallArgs , blockNr rpc.BlockNumber , vmCfg vm.Config , timeout time.Duration ) ([]byte , uint64 , bool , error ) {
677+ func DoCall (ctx context.Context , b Backend , args CallArgs , blockNr rpc.BlockNumber , vmCfg vm.Config , timeout time.Duration , globalGasCap * big. Int ) ([]byte , uint64 , bool , error ) {
678678 defer func (start time.Time ) { log .Debug ("Executing EVM call finished" , "runtime" , time .Since (start )) }(time .Now ())
679679
680680 state , header , err := b .StateAndHeaderByNumber (ctx , blockNr )
@@ -697,6 +697,10 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
697697 if args .Gas != nil {
698698 gas = uint64 (* args .Gas )
699699 }
700+ if globalGasCap != nil && globalGasCap .Uint64 () < gas {
701+ log .Warn ("Caller gas above allowance, capping" , "requested" , gas , "cap" , globalGasCap )
702+ gas = globalGasCap .Uint64 ()
703+ }
700704 gasPrice := new (big.Int ).SetUint64 (defaultGasPrice )
701705 if args .GasPrice != nil {
702706 gasPrice = args .GasPrice .ToInt ()
@@ -752,11 +756,11 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
752756// Call executes the given transaction on the state for the given block number.
753757// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values.
754758func (s * PublicBlockChainAPI ) Call (ctx context.Context , args CallArgs , blockNr rpc.BlockNumber ) (hexutil.Bytes , error ) {
755- result , _ , _ , err := DoCall (ctx , s .b , args , blockNr , vm.Config {}, 5 * time .Second )
759+ result , _ , _ , err := DoCall (ctx , s .b , args , blockNr , vm.Config {}, 5 * time .Second , s . b . RPCGasCap () )
756760 return (hexutil .Bytes )(result ), err
757761}
758762
759- func DoEstimateGas (ctx context.Context , b Backend , args CallArgs , blockNr rpc.BlockNumber ) (hexutil.Uint64 , error ) {
763+ func DoEstimateGas (ctx context.Context , b Backend , args CallArgs , blockNr rpc.BlockNumber , gasCap * big. Int ) (hexutil.Uint64 , error ) {
760764 // Binary search the gas requirement, as it may be higher than the amount used
761765 var (
762766 lo uint64 = params .TxGas - 1
@@ -773,13 +777,17 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl
773777 }
774778 hi = block .GasLimit ()
775779 }
780+ if gasCap != nil && hi > gasCap .Uint64 () {
781+ log .Warn ("Caller gas above allowance, capping" , "requested" , hi , "cap" , gasCap )
782+ hi = gasCap .Uint64 ()
783+ }
776784 cap = hi
777785
778786 // Create a helper to check if a gas allowance results in an executable transaction
779787 executable := func (gas uint64 ) bool {
780788 args .Gas = (* hexutil .Uint64 )(& gas )
781789
782- _ , _ , failed , err := DoCall (ctx , b , args , rpc .PendingBlockNumber , vm.Config {}, 0 )
790+ _ , _ , failed , err := DoCall (ctx , b , args , rpc .PendingBlockNumber , vm.Config {}, 0 , gasCap )
783791 if err != nil || failed {
784792 return false
785793 }
@@ -797,7 +805,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl
797805 // Reject the transaction as invalid if it still fails at the highest allowance
798806 if hi == cap {
799807 if ! executable (hi ) {
800- return 0 , fmt .Errorf ("gas required exceeds allowance or always failing transaction" )
808+ return 0 , fmt .Errorf ("gas required exceeds allowance (%d) or always failing transaction" , cap )
801809 }
802810 }
803811 return hexutil .Uint64 (hi ), nil
@@ -806,7 +814,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl
806814// EstimateGas returns an estimate of the amount of gas needed to execute the
807815// given transaction against the current pending block.
808816func (s * PublicBlockChainAPI ) EstimateGas (ctx context.Context , args CallArgs ) (hexutil.Uint64 , error ) {
809- return DoEstimateGas (ctx , s .b , args , rpc .PendingBlockNumber )
817+ return DoEstimateGas (ctx , s .b , args , rpc .PendingBlockNumber , s . b . RPCGasCap () )
810818}
811819
812820// ExecutionResult groups all structured logs emitted by the EVM
0 commit comments