Skip to content

Commit 56a319b

Browse files
authored
cmd, eth, internal, les: add txfee cap (#21212)
* cmd, eth, internal, les: add gasprice cap * cmd/utils, eth: add default value for gasprice cap * all: use txfee cap * cmd, eth: add fix * cmd, internal: address comments
1 parent bcf19bc commit 56a319b

9 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ var (
186186
utils.IPCPathFlag,
187187
utils.InsecureUnlockAllowedFlag,
188188
utils.RPCGlobalGasCap,
189+
utils.RPCGlobalTxFeeCap,
189190
}
190191

191192
whisperFlags = []cli.Flag{

‎cmd/geth/usage.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ var AppHelpFlagGroups = []flagGroup{
176176
utils.GraphQLCORSDomainFlag,
177177
utils.GraphQLVirtualHostsFlag,
178178
utils.RPCGlobalGasCap,
179+
utils.RPCGlobalTxFeeCap,
179180
utils.JSpathFlag,
180181
utils.ExecFlag,
181182
utils.PreloadJSFlag,

‎cmd/utils/flags.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ var (
478478
Name: "rpc.gascap",
479479
Usage: "Sets a cap on gas that can be used in eth_call/estimateGas",
480480
}
481+
RPCGlobalTxFeeCap = cli.Float64Flag{
482+
Name: "rpc.txfeecap",
483+
Usage: "Sets a cap on transaction fee (in ether) that can be sent via the RPC APIs (0 = no cap)",
484+
Value: eth.DefaultConfig.RPCTxFeeCap,
485+
}
481486
// Logging and debug settings
482487
EthStatsURLFlag = cli.StringFlag{
483488
Name: "ethstats",
@@ -1560,6 +1565,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15601565
if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
15611566
cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
15621567
}
1568+
if ctx.GlobalIsSet(RPCGlobalTxFeeCap.Name) {
1569+
cfg.RPCTxFeeCap = ctx.GlobalFloat64(RPCGlobalTxFeeCap.Name)
1570+
}
15631571
if ctx.GlobalIsSet(DNSDiscoveryFlag.Name) {
15641572
urls := ctx.GlobalString(DNSDiscoveryFlag.Name)
15651573
if urls == "" {

‎eth/api_backend.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ func (b *EthAPIBackend) RPCGasCap() *big.Int {
293293
return b.eth.config.RPCGasCap
294294
}
295295

296+
func (b *EthAPIBackend) RPCTxFeeCap() float64 {
297+
return b.eth.config.RPCTxFeeCap
298+
}
299+
296300
func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
297301
sections, _, _ := b.eth.bloomIndexer.Sections()
298302
return params.BloomBitsBlocks, sections

‎eth/config.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var DefaultConfig = Config{
6464
Blocks: 20,
6565
Percentile: 60,
6666
},
67+
RPCTxFeeCap: 1, // 1 ether
6768
}
6869

6970
func init() {
@@ -159,6 +160,10 @@ type Config struct {
159160
// RPCGasCap is the global gas cap for eth-call variants.
160161
RPCGasCap *big.Int `toml:",omitempty"`
161162

163+
// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
164+
// send-transction variants. The unit is ether.
165+
RPCTxFeeCap float64 `toml:",omitempty"`
166+
162167
// Checkpoint is a hardcoded checkpoint which can be nil.
163168
Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
164169

‎eth/gen_config.go‎

Lines changed: 12 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,13 @@ func (args *SendTxArgs) toTransaction() *types.Transaction {
15201520

15211521
// SubmitTransaction is a helper function that submits tx to txPool and logs a message.
15221522
func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) {
1523+
// If the transaction fee cap is already specified, ensure the
1524+
// fee of the given transaction is _reasonable_.
1525+
feeEth := new(big.Float).Quo(new(big.Float).SetInt(new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))), new(big.Float).SetInt(big.NewInt(params.Ether)))
1526+
feeFloat, _ := feeEth.Float64()
1527+
if b.RPCTxFeeCap() != 0 && feeFloat > b.RPCTxFeeCap() {
1528+
return common.Hash{}, fmt.Errorf("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)", feeFloat, b.RPCTxFeeCap())
1529+
}
15231530
if err := b.SendTx(ctx, tx); err != nil {
15241531
return common.Hash{}, err
15251532
}

‎internal/ethapi/backend.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +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
48+
RPCGasCap() *big.Int // global gas cap for eth_call over rpc: DoS protection
49+
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
4950

5051
// Blockchain API
5152
SetHead(number uint64)

‎les/api_backend.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ func (b *LesApiBackend) RPCGasCap() *big.Int {
262262
return b.eth.config.RPCGasCap
263263
}
264264

265+
func (b *LesApiBackend) RPCTxFeeCap() float64 {
266+
return b.eth.config.RPCTxFeeCap
267+
}
268+
265269
func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
266270
if b.eth.bloomIndexer == nil {
267271
return 0, 0

0 commit comments

Comments
 (0)