Skip to content

Commit

Permalink
Add --rpc.gaspricemultiplier option
Browse files Browse the repository at this point in the history
This add command line option to adjust the
multiplier in the gas price suggestion RPC
endpoint.
  • Loading branch information
palango committed Jul 7, 2023
1 parent a72fd91 commit 34d6e65
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ var (
utils.IPCPathFlag,
utils.InsecureUnlockAllowedFlag,
utils.RPCGlobalGasInflationRateFlag,
utils.RPCGlobalGasPriceMultiplierFlag,
utils.RPCGlobalGasCapFlag,
utils.RPCGlobalTxFeeCapFlag,
}
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.GraphQLCORSDomainFlag,
utils.GraphQLVirtualHostsFlag,
utils.RPCGlobalGasInflationRateFlag,
utils.RPCGlobalGasPriceMultiplierFlag,
utils.RPCGlobalGasCapFlag,
utils.RPCGlobalTxFeeCapFlag,
utils.JSpathFlag,
Expand Down
15 changes: 15 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"math"
"math/big"
"path/filepath"
godebug "runtime/debug"
"strconv"
Expand Down Expand Up @@ -445,6 +446,11 @@ var (
Usage: "Multiplier applied to the gasEstimation rpc call (1 = gasEstimation, 1.3 = gasEstimation + 30%, etc. Defaults to 1.3)",
Value: ethconfig.Defaults.RPCGasInflationRate,
}
RPCGlobalGasPriceMultiplierFlag = cli.Float64Flag{
Name: "rpc.gaspricemultiplier",
Usage: "Multiplier applied to the gasPrice rpc call (1 = gasPrice, 1.3 = gasPrice + 30%, etc. Defaults to 2.0)",
Value: 2.0,
}
RPCGlobalGasCapFlag = cli.Uint64Flag{
Name: "rpc.gascap",
Usage: "Sets a cap on gas that can be used in eth_call/estimateGas (0=infinite)",
Expand Down Expand Up @@ -1746,6 +1752,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.GlobalIsSet(RPCGlobalGasInflationRateFlag.Name) {
cfg.RPCGasInflationRate = ctx.GlobalFloat64(RPCGlobalGasInflationRateFlag.Name)
}
if ctx.GlobalIsSet(RPCGlobalGasPriceMultiplierFlag.Name) {
floatMutliplier := ctx.GlobalFloat64(RPCGlobalGasPriceMultiplierFlag.Name)
if floatMutliplier <= 1.0 {
log.Warn("Too low RPCGasPriceMultiplier, setting to 1.0", "provided value", floatMutliplier)
floatMutliplier = 1.0
}

cfg.RPCGasPriceMultiplier = big.NewInt(int64(floatMutliplier * 100))
}
if cfg.RPCGasInflationRate < 1 {
Fatalf("The inflation rate shouldn't be less than 1: %f", cfg.RPCGasInflationRate)
}
Expand Down
8 changes: 5 additions & 3 deletions contracts/gasprice_minimum/gasprice_minimum.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (

var (
FallbackGasPriceMinimum *big.Int = big.NewInt(0) // gas price minimum to return if unable to fetch from contract
suggestionMultiplier *big.Int = big.NewInt(5) // The multiplier that we apply to the minimum when suggesting gas price
)

const (
Expand All @@ -62,9 +61,12 @@ func GetGasTipCapSuggestion(vmRunner vm.EVMRunner, currencyAddress *common.Addre

// GetGasPriceSuggestion suggests a gas price the suggestionMultiplier times higher than the GPM in the appropriate currency.
// TODO: Switch to using a caching GPM manager under high load.
func GetGasPriceSuggestion(vmRunner vm.EVMRunner, currency *common.Address) (*big.Int, error) {
func GetGasPriceSuggestion(vmRunner vm.EVMRunner, currency *common.Address, multiplier *big.Int) (*big.Int, error) {
gasPriceMinimum, err := GetGasPriceMinimum(vmRunner, currency)
return new(big.Int).Mul(gasPriceMinimum, suggestionMultiplier), err

gasPriceWithMultiplier := new(big.Int).Mul(gasPriceMinimum, multiplier)
res := new(big.Int).Div(gasPriceWithMultiplier, big.NewInt(100))
return res, err
}

func GetGasPriceMinimum(vmRunner vm.EVMRunner, currency *common.Address) (*big.Int, error) {
Expand Down
13 changes: 10 additions & 3 deletions contracts/gasprice_minimum/gasprice_minimum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGetGasPriceSuggestion(t *testing.T) {
celoAddress := common.HexToAddress("0x076")
gpmAddress := common.HexToAddress("0x090")

t.Run("should return gas price minimum multiplied by 5", func(t *testing.T) {
t.Run("should return gas price minimum multiplied with factor", func(t *testing.T) {
g := NewGomegaWithT(t)

runner := testutil.NewMockEVMRunner()
Expand All @@ -29,11 +29,18 @@ func TestGetGasPriceSuggestion(t *testing.T) {
runner.RegisterContract(gpmAddress, contract)
registry.AddContract(config.GasPriceMinimumRegistryId, gpmAddress)

suggestedGpm, err := GetGasPriceSuggestion(runner, nil)
suggestedGpm, err := GetGasPriceSuggestion(runner, nil, big.NewInt(500))
g.Expect(err).NotTo(HaveOccurred())

g.Expect(suggestedGpm.Uint64()).To(Equal(uint64(777777 * 5)))

suggestedGpm, err = GetGasPriceSuggestion(runner, nil, big.NewInt(100))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(suggestedGpm.Uint64()).To(Equal(uint64(777777)))

suggestedGpm, err = GetGasPriceSuggestion(runner, nil, big.NewInt(110))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(suggestedGpm.Uint64()).To(Equal(uint64(855554)))

})
}
func TestGetGasPriceMinimum(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (b *EthAPIBackend) SuggestPrice(ctx context.Context, currencyAddress *commo
if err != nil {
return nil, err
}
return gpm.GetGasPriceSuggestion(vmRunner, currencyAddress)
return gpm.GetGasPriceSuggestion(vmRunner, currencyAddress, b.eth.config.RPCGasPriceMultiplier)
}

func (b *EthAPIBackend) GetBlockGasLimit(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) uint64 {
Expand Down
4 changes: 4 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ type Config struct {
// RPCGasInflationRate is a global multiplier applied to the gas estimations
RPCGasInflationRate float64

// RPCGasPriceMultiplier is a global multiplier applied to the gas price
// It's a percent value, e.g. 120 means a multiplication factor of 1.2
RPCGasPriceMultiplier *big.Int

// RPCGasCap is the global gas cap for eth-call variants.
RPCGasCap uint64

Expand Down
6 changes: 6 additions & 0 deletions eth/ethconfig/gen_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (b *LesApiBackend) SuggestPrice(ctx context.Context, currencyAddress *commo
if err != nil {
return nil, err
}
return gpm.GetGasPriceSuggestion(vmRunner, currencyAddress)
return gpm.GetGasPriceSuggestion(vmRunner, currencyAddress, b.eth.config.RPCGasPriceMultiplier)
}

func (b *LesApiBackend) GetIntrinsicGasForAlternativeFeeCurrency(ctx context.Context) uint64 {
Expand Down
13 changes: 7 additions & 6 deletions test/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ var (
}

BaseEthConfig = &eth.Config{
SyncMode: downloader.FullSync,
MinSyncPeers: 1,
DatabaseCache: 256,
DatabaseHandles: 256,
TxPool: core.DefaultTxPoolConfig,
RPCEthCompatibility: true,
SyncMode: downloader.FullSync,
MinSyncPeers: 1,
DatabaseCache: 256,
DatabaseHandles: 256,
TxPool: core.DefaultTxPoolConfig,
RPCEthCompatibility: true,
RPCGasPriceMultiplier: big.NewInt(100),
Istanbul: istanbul.Config{
Validator: true,
// Set announce gossip period to 1 minute, if not set this results
Expand Down

0 comments on commit 34d6e65

Please sign in to comment.