Skip to content

Commit 8d94831

Browse files
holimanenriquefynn
authored andcommitted
rpc, internal/ethapi: default rpc gascap at 25M + better error message (ethereum#21229)
* rpc, internal/ethapi: default rpc gascap at 50M + better error message * eth,internal: make globalgascap uint64 * core/tests: fix compilation failure * eth/config: gascap at 25M + minor review concerns
1 parent befbc6f commit 8d94831

File tree

8 files changed

+31
-21
lines changed

8 files changed

+31
-21
lines changed

cmd/utils/flags.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ var (
476476
}
477477
RPCGlobalGasCap = cli.Uint64Flag{
478478
Name: "rpc.gascap",
479-
Usage: "Sets a cap on gas that can be used in eth_call/estimateGas",
479+
Usage: "Sets a cap on gas that can be used in eth_call/estimateGas (0=infinite)",
480+
Value: eth.DefaultConfig.RPCGasCap,
480481
}
481482
RPCGlobalTxFeeCap = cli.Float64Flag{
482483
Name: "rpc.txfeecap",
@@ -1563,7 +1564,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15631564
cfg.EVMInterpreter = ctx.GlobalString(EVMInterpreterFlag.Name)
15641565
}
15651566
if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
1566-
cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
1567+
cfg.RPCGasCap = ctx.GlobalUint64(RPCGlobalGasCap.Name)
1568+
}
1569+
if cfg.RPCGasCap != 0 {
1570+
log.Info("Set global gas cap", "cap", cfg.RPCGasCap)
1571+
} else {
1572+
log.Info("Global gas cap disabled")
15671573
}
15681574
if ctx.GlobalIsSet(RPCGlobalTxFeeCap.Name) {
15691575
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCap.Name)

core/tx_pool_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"crypto/ecdsa"
21+
"errors"
2122
"fmt"
2223
"io/ioutil"
2324
"math/big"
@@ -245,7 +246,7 @@ func TestInvalidTransactions(t *testing.T) {
245246

246247
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), tx.GasPrice()))
247248
pool.currentState.AddBalance(from, balance)
248-
if err := pool.AddRemote(tx); err != ErrIntrinsicGas {
249+
if err := pool.AddRemote(tx); !errors.Is(err, ErrIntrinsicGas) {
249250
t.Error("expected", ErrIntrinsicGas, "got", err)
250251
}
251252

eth/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (b *EthAPIBackend) ExtRPCEnabled() bool {
289289
return b.extRPCEnabled
290290
}
291291

292-
func (b *EthAPIBackend) RPCGasCap() *big.Int {
292+
func (b *EthAPIBackend) RPCGasCap() uint64 {
293293
return b.eth.config.RPCGasCap
294294
}
295295

eth/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ var DefaultConfig = Config{
5959
GasPrice: big.NewInt(params.GWei),
6060
Recommit: 3 * time.Second,
6161
},
62-
TxPool: core.DefaultTxPoolConfig,
62+
TxPool: core.DefaultTxPoolConfig,
63+
RPCGasCap: 25000000,
6364
GPO: gasprice.Config{
6465
Blocks: 20,
6566
Percentile: 60,
@@ -158,7 +159,7 @@ type Config struct {
158159
EVMInterpreter string
159160

160161
// RPCGasCap is the global gas cap for eth-call variants.
161-
RPCGasCap *big.Int `toml:",omitempty"`
162+
RPCGasCap uint64 `toml:",omitempty"`
162163

163164
// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
164165
// send-transction variants. The unit is ether.

eth/gen_config.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ethapi/api.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ type CallArgs struct {
741741
}
742742

743743
// ToMessage converts CallArgs to the Message type used by the core evm
744-
func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message {
744+
func (args *CallArgs) ToMessage(globalGasCap uint64) types.Message {
745745
// Set sender address or use zero address if none specified.
746746
var addr common.Address
747747
if args.From != nil {
@@ -753,9 +753,9 @@ func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message {
753753
if args.Gas != nil {
754754
gas = uint64(*args.Gas)
755755
}
756-
if globalGasCap != nil && globalGasCap.Uint64() < gas {
756+
if globalGasCap != 0 && globalGasCap < gas {
757757
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
758-
gas = globalGasCap.Uint64()
758+
gas = globalGasCap
759759
}
760760
gasPrice := new(big.Int)
761761
if args.GasPrice != nil {
@@ -790,7 +790,7 @@ type account struct {
790790
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"`
791791
}
792792

793-
func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]account, vmCfg vm.Config, timeout time.Duration, globalGasCap *big.Int) (*core.ExecutionResult, error) {
793+
func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]account, vmCfg vm.Config, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error) {
794794
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
795795

796796
state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
@@ -861,7 +861,10 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
861861
if evm.Cancelled() {
862862
return nil, fmt.Errorf("execution aborted (timeout = %v)", timeout)
863863
}
864-
return result, err
864+
if err != nil {
865+
return result, fmt.Errorf("err: %w (supplied gas %d)", err, msg.Gas())
866+
}
867+
return result, nil
865868
}
866869

867870
func newRevertError(result *core.ExecutionResult) *revertError {
@@ -916,7 +919,7 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
916919
return result.Return(), result.Err
917920
}
918921

919-
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap *big.Int) (hexutil.Uint64, error) {
922+
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap uint64) (hexutil.Uint64, error) {
920923
// Binary search the gas requirement, as it may be higher than the amount used
921924
var (
922925
lo uint64 = params.TxGas - 1
@@ -964,9 +967,9 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
964967
}
965968
}
966969
// Recap the highest gas allowance with specified gascap.
967-
if gasCap != nil && hi > gasCap.Uint64() {
970+
if gasCap != 0 && hi > gasCap {
968971
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", gasCap)
969-
hi = gasCap.Uint64()
972+
hi = gasCap
970973
}
971974
cap = hi
972975

@@ -976,7 +979,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
976979

977980
result, err := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
978981
if err != nil {
979-
if err == core.ErrIntrinsicGas {
982+
if errors.Is(err, core.ErrIntrinsicGas) {
980983
return true, nil, nil // Special case, raise gas limit
981984
}
982985
return true, nil, err // Bail out

internal/ethapi/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ type Backend interface {
4545
ChainDb() ethdb.Database
4646
AccountManager() *accounts.Manager
4747
ExtRPCEnabled() bool
48-
RPCGasCap() *big.Int // global gas cap for eth_call over rpc: DoS protection
4948
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
49+
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
5050

5151
// Blockchain API
5252
SetHead(number uint64)

les/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (b *LesApiBackend) ExtRPCEnabled() bool {
258258
return b.extRPCEnabled
259259
}
260260

261-
func (b *LesApiBackend) RPCGasCap() *big.Int {
261+
func (b *LesApiBackend) RPCGasCap() uint64 {
262262
return b.eth.config.RPCGasCap
263263
}
264264

0 commit comments

Comments
 (0)