From 4b434d8c143b190a515998898e15a84aea9c1df2 Mon Sep 17 00:00:00 2001 From: gary rong Date: Wed, 9 Sep 2020 23:38:47 +0800 Subject: [PATCH] cmd, eth: offer maxprice flag for overwritting price cap (#21531) * cmd, eth: offer maxprice flag for overwritting price cap * eth: rename default price cap --- cmd/geth/main.go | 1 + cmd/geth/usage.go | 1 + cmd/utils/flags.go | 8 ++++++++ eth/config.go | 2 ++ eth/gasprice/gasprice.go | 18 +++++++++++++++--- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index ccc6358996b62..677a19eed2cdb 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -154,6 +154,7 @@ var ( utils.LegacyGpoBlocksFlag, utils.GpoPercentileFlag, utils.LegacyGpoPercentileFlag, + utils.GpoMaxGasPriceFlag, utils.EWASMInterpreterFlag, utils.EVMInterpreterFlag, configFileFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 5e004c42bea9a..334a729c24d8c 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -189,6 +189,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{ Flags: []cli.Flag{ utils.GpoBlocksFlag, utils.GpoPercentileFlag, + utils.GpoMaxGasPriceFlag, }, }, { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index e6382c163498a..2c57e533edd7b 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -631,6 +631,11 @@ var ( Usage: "Suggested gas price is the given percentile of a set of recent transaction gas prices", Value: eth.DefaultConfig.GPO.Percentile, } + GpoMaxGasPriceFlag = cli.Int64Flag{ + Name: "gpo.maxprice", + Usage: "Maximum gas price will be recommended by gpo", + Value: eth.DefaultConfig.GPO.MaxPrice.Int64(), + } WhisperEnabledFlag = cli.BoolFlag{ Name: "shh", Usage: "Enable Whisper", @@ -1291,6 +1296,9 @@ func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) { if ctx.GlobalIsSet(GpoPercentileFlag.Name) { cfg.Percentile = ctx.GlobalInt(GpoPercentileFlag.Name) } + if ctx.GlobalIsSet(GpoMaxGasPriceFlag.Name) { + cfg.MaxPrice = big.NewInt(ctx.GlobalInt64(GpoMaxGasPriceFlag.Name)) + } } func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) { diff --git a/eth/config.go b/eth/config.go index 1074e5a3718df..0d99c2a3f1e61 100644 --- a/eth/config.go +++ b/eth/config.go @@ -37,12 +37,14 @@ import ( var DefaultFullGPOConfig = gasprice.Config{ Blocks: 20, Percentile: 60, + MaxPrice: gasprice.DefaultMaxPrice, } // DefaultLightGPOConfig contains default gasprice oracle settings for light client. var DefaultLightGPOConfig = gasprice.Config{ Blocks: 2, Percentile: 60, + MaxPrice: gasprice.DefaultMaxPrice, } // DefaultConfig contains default settings for use on the Ethereum main net. diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index b9eff3d11a12d..5d8be08e0b78c 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -24,18 +24,20 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" ) const sampleNumber = 3 // Number of transactions sampled in a block -var maxPrice = big.NewInt(500 * params.GWei) +var DefaultMaxPrice = big.NewInt(500 * params.GWei) type Config struct { Blocks int Percentile int Default *big.Int `toml:",omitempty"` + MaxPrice *big.Int `toml:",omitempty"` } // OracleBackend includes all necessary background APIs for oracle. @@ -51,6 +53,7 @@ type Oracle struct { backend OracleBackend lastHead common.Hash lastPrice *big.Int + maxPrice *big.Int cacheLock sync.RWMutex fetchLock sync.Mutex @@ -64,17 +67,26 @@ func NewOracle(backend OracleBackend, params Config) *Oracle { blocks := params.Blocks if blocks < 1 { blocks = 1 + log.Warn("Sanitizing invalid gasprice oracle sample blocks", "provided", params.Blocks, "updated", blocks) } percent := params.Percentile if percent < 0 { percent = 0 + log.Warn("Sanitizing invalid gasprice oracle sample percentile", "provided", params.Percentile, "updated", percent) } if percent > 100 { percent = 100 + log.Warn("Sanitizing invalid gasprice oracle sample percentile", "provided", params.Percentile, "updated", percent) + } + maxPrice := params.MaxPrice + if maxPrice == nil || maxPrice.Int64() <= 0 { + maxPrice = DefaultMaxPrice + log.Warn("Sanitizing invalid gasprice oracle price cap", "provided", params.MaxPrice, "updated", maxPrice) } return &Oracle{ backend: backend, lastPrice: params.Default, + maxPrice: maxPrice, checkBlocks: blocks, percentile: percent, } @@ -146,8 +158,8 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) { sort.Sort(bigIntArray(txPrices)) price = txPrices[(len(txPrices)-1)*gpo.percentile/100] } - if price.Cmp(maxPrice) > 0 { - price = new(big.Int).Set(maxPrice) + if price.Cmp(gpo.maxPrice) > 0 { + price = new(big.Int).Set(gpo.maxPrice) } gpo.cacheLock.Lock() gpo.lastHead = headHash