Skip to content

Commit

Permalink
option to reject transactions with a low gas price (#1539)
Browse files Browse the repository at this point in the history
when compared to the response from eth_gasPrice
  • Loading branch information
hexoscott authored Dec 4, 2024
1 parent 9634f40 commit feca211
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
72 changes: 37 additions & 35 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,23 +331,24 @@ func (api *BaseAPI) pruneMode(tx kv.Tx) (*prune.Mode, error) {
// APIImpl is implementation of the EthAPI interface based on remote Db access
type APIImpl struct {
*BaseAPI
ethBackend rpchelper.ApiBackend
txPool txpool.TxpoolClient
mining txpool.MiningClient
gasCache *GasPriceCache
db kv.RoDB
GasCap uint64
ReturnDataLimit int
ZkRpcUrl string
PoolManagerUrl string
AllowFreeTransactions bool
AllowPreEIP155Transactions bool
L1RpcUrl string
DefaultGasPrice uint64
MaxGasPrice uint64
GasPriceFactor float64
L1GasPrice L1GasPrice
VirtualCountersSmtReduction float64
ethBackend rpchelper.ApiBackend
txPool txpool.TxpoolClient
mining txpool.MiningClient
gasCache *GasPriceCache
db kv.RoDB
GasCap uint64
ReturnDataLimit int
ZkRpcUrl string
PoolManagerUrl string
AllowFreeTransactions bool
AllowPreEIP155Transactions bool
L1RpcUrl string
DefaultGasPrice uint64
MaxGasPrice uint64
GasPriceFactor float64
L1GasPrice L1GasPrice
VirtualCountersSmtReduction float64
RejectLowGasPriceTransactions bool
}

// NewEthAPI returns APIImpl instance
Expand All @@ -357,24 +358,25 @@ func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpoo
}

return &APIImpl{
BaseAPI: base,
db: db,
ethBackend: eth,
txPool: txPool,
mining: mining,
gasCache: NewGasPriceCache(),
GasCap: gascap,
ReturnDataLimit: returnDataLimit,
ZkRpcUrl: ethCfg.L2RpcUrl,
PoolManagerUrl: ethCfg.PoolManagerUrl,
AllowFreeTransactions: ethCfg.AllowFreeTransactions,
AllowPreEIP155Transactions: ethCfg.AllowPreEIP155Transactions,
L1RpcUrl: ethCfg.L1RpcUrl,
DefaultGasPrice: ethCfg.DefaultGasPrice,
MaxGasPrice: ethCfg.MaxGasPrice,
GasPriceFactor: ethCfg.GasPriceFactor,
L1GasPrice: L1GasPrice{},
VirtualCountersSmtReduction: ethCfg.VirtualCountersSmtReduction,
BaseAPI: base,
db: db,
ethBackend: eth,
txPool: txPool,
mining: mining,
gasCache: NewGasPriceCache(),
GasCap: gascap,
ReturnDataLimit: returnDataLimit,
ZkRpcUrl: ethCfg.L2RpcUrl,
PoolManagerUrl: ethCfg.PoolManagerUrl,
AllowFreeTransactions: ethCfg.AllowFreeTransactions,
AllowPreEIP155Transactions: ethCfg.AllowPreEIP155Transactions,
L1RpcUrl: ethCfg.L1RpcUrl,
DefaultGasPrice: ethCfg.DefaultGasPrice,
MaxGasPrice: ethCfg.MaxGasPrice,
GasPriceFactor: ethCfg.GasPriceFactor,
L1GasPrice: L1GasPrice{},
VirtualCountersSmtReduction: ethCfg.VirtualCountersSmtReduction,
RejectLowGasPriceTransactions: ethCfg.RejectLowGasPriceTransactions,
}
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/rpcdaemon/commands/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/gateway-fm/cdk-erigon-lib/common/hexutility"
txPoolProto "github.com/gateway-fm/cdk-erigon-lib/gointerfaces/txpool"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/params"
Expand Down Expand Up @@ -49,6 +50,20 @@ func (api *APIImpl) SendRawTransaction(ctx context.Context, encodedTx hexutility
return common.Hash{}, err
}

// check if the transaction price is lower than the current network price and
// reject the tx if it is
if api.RejectLowGasPriceTransactions && !api.BaseAPI.gasless {
txnPrice := txn.GetPrice()
networkPrice, err := api.GasPrice_nonRedirected(ctx)
if err != nil {
return common.Hash{}, err
}
networkPrice256, _ := uint256.FromBig(networkPrice.ToInt())
if txnPrice.Cmp(networkPrice256) < 0 {
return common.Hash{}, fmt.Errorf("transaction price is lower than the current network price")
}
}

if txn.Type() != types.LegacyTxType {
latestBlock, err := api.blockByNumber(ctx, rpc.LatestBlockNumber, tx)

Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ var (
Usage: "Mock the witness generation",
Value: false,
}
RejectLowGasPriceTransactions = cli.BoolFlag{
Name: "zkevm.reject-low-gas-price-transactions",
Usage: "Reject transactions with a gas price lower than the current network price",
Value: false,
}
ACLPrintHistory = cli.IntFlag{
Name: "acl.print-history",
Usage: "Number of entries to print from the ACL history on node start up",
Expand Down
1 change: 1 addition & 0 deletions eth/ethconfig/config_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Zk struct {
BadBatches []uint64
SealBatchImmediatelyOnOverflow bool
MockWitnessGeneration bool
RejectLowGasPriceTransactions bool
}

var DefaultZkConfig = &Zk{}
Expand Down
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,5 @@ var DefaultFlags = []cli.Flag{
&utils.BadBatches,
&utils.SealBatchImmediatelyOnOverflow,
&utils.MockWitnessGeneration,
&utils.RejectLowGasPriceTransactions,
}
1 change: 1 addition & 0 deletions turbo/cli/flags_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
BadBatches: badBatches,
SealBatchImmediatelyOnOverflow: ctx.Bool(utils.SealBatchImmediatelyOnOverflow.Name),
MockWitnessGeneration: ctx.Bool(utils.MockWitnessGeneration.Name),
RejectLowGasPriceTransactions: ctx.Bool(utils.RejectLowGasPriceTransactions.Name),
}

utils2.EnableTimer(cfg.DebugTimers)
Expand Down

0 comments on commit feca211

Please sign in to comment.