Skip to content

Commit f445196

Browse files
committed
eth/gasprice: add configurable threshold to gas price oracle (ethereum#22752)
1 parent 2b365e8 commit f445196

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

cmd/XDC/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ var (
128128
//utils.GpoBlocksFlag,
129129
//utils.GpoPercentileFlag,
130130
utils.GpoMaxGasPriceFlag,
131+
utils.GpoIgnoreGasPriceFlag,
131132
//utils.ExtraDataFlag,
132133
configFileFlag,
133134
utils.AnnounceTxsFlag,

cmd/XDC/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ var AppHelpFlagGroups = []flagGroup{
196196
// utils.GpoBlocksFlag,
197197
// utils.GpoPercentileFlag,
198198
// utils.GpoMaxGasPriceFlag,
199+
// utils.GpoIgnoreGasPriceFlag,
199200
// },
200201
//},
201202
//{

cmd/utils/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ var (
560560
Usage: "Maximum gas price will be recommended by gpo",
561561
Value: ethconfig.Defaults.GPO.MaxPrice.Int64(),
562562
}
563+
GpoIgnoreGasPriceFlag = cli.Int64Flag{
564+
Name: "gpo.ignoreprice",
565+
Usage: "Gas price below which gpo will ignore transactions",
566+
Value: ethconfig.Defaults.GPO.IgnorePrice.Int64(),
567+
}
563568
WhisperEnabledFlag = cli.BoolFlag{
564569
Name: "shh",
565570
Usage: "Enable Whisper",
@@ -999,6 +1004,9 @@ func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) {
9991004
if ctx.GlobalIsSet(GpoMaxGasPriceFlag.Name) {
10001005
cfg.MaxPrice = big.NewInt(ctx.GlobalInt64(GpoMaxGasPriceFlag.Name))
10011006
}
1007+
if ctx.GlobalIsSet(GpoIgnoreGasPriceFlag.Name) {
1008+
cfg.IgnorePrice = big.NewInt(ctx.GlobalInt64(GpoIgnoreGasPriceFlag.Name))
1009+
}
10021010
}
10031011

10041012
func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {

eth/ethconfig/config.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ import (
3636

3737
// FullNodeGPO contains default gasprice oracle settings for full node.
3838
var FullNodeGPO = gasprice.Config{
39-
Blocks: 20,
40-
Percentile: 60,
41-
MaxPrice: gasprice.DefaultMaxPrice,
39+
Blocks: 20,
40+
Percentile: 60,
41+
MaxPrice: gasprice.DefaultMaxPrice,
42+
IgnorePrice: gasprice.DefaultIgnorePrice,
4243
}
4344

4445
// LightClientGPO contains default gasprice oracle settings for light client.
4546
var LightClientGPO = gasprice.Config{
46-
Blocks: 2,
47-
Percentile: 60,
48-
MaxPrice: gasprice.DefaultMaxPrice,
47+
Blocks: 2,
48+
Percentile: 60,
49+
MaxPrice: gasprice.DefaultMaxPrice,
50+
IgnorePrice: gasprice.DefaultIgnorePrice,
4951
}
5052

5153
// Defaults contains default settings for use on the Ethereum main net.

eth/gasprice/gasprice.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ import (
3232
const sampleNumber = 3 // Number of transactions sampled in a block
3333

3434
var DefaultMaxPrice = big.NewInt(500 * params.GWei)
35+
var DefaultIgnorePrice = big.NewInt(2 * params.Wei)
3536

3637
type Config struct {
37-
Blocks int
38-
Percentile int
39-
Default *big.Int `toml:",omitempty"`
40-
MaxPrice *big.Int `toml:",omitempty"`
38+
Blocks int
39+
Percentile int
40+
Default *big.Int `toml:",omitempty"`
41+
MaxPrice *big.Int `toml:",omitempty"`
42+
IgnorePrice *big.Int `toml:",omitempty"`
4143
}
4244

4345
// OracleBackend includes all necessary background APIs for oracle.
@@ -50,12 +52,13 @@ type OracleBackend interface {
5052
// Oracle recommends gas prices based on the content of recent
5153
// blocks. Suitable for both light and full clients.
5254
type Oracle struct {
53-
backend OracleBackend
54-
lastHead common.Hash
55-
lastPrice *big.Int
56-
maxPrice *big.Int
57-
cacheLock sync.RWMutex
58-
fetchLock sync.Mutex
55+
backend OracleBackend
56+
lastHead common.Hash
57+
lastPrice *big.Int
58+
maxPrice *big.Int
59+
ignorePrice *big.Int
60+
cacheLock sync.RWMutex
61+
fetchLock sync.Mutex
5962

6063
checkBlocks int
6164
percentile int
@@ -83,10 +86,18 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
8386
maxPrice = DefaultMaxPrice
8487
log.Warn("Sanitizing invalid gasprice oracle price cap", "provided", params.MaxPrice, "updated", maxPrice)
8588
}
89+
ignorePrice := params.IgnorePrice
90+
if ignorePrice == nil || ignorePrice.Int64() <= 0 {
91+
ignorePrice = DefaultIgnorePrice
92+
log.Warn("Sanitizing invalid gasprice oracle ignore price", "provided", params.IgnorePrice, "updated", ignorePrice)
93+
} else if ignorePrice.Int64() > 0 {
94+
log.Info("Gasprice oracle is ignoring threshold set", "threshold", ignorePrice)
95+
}
8696
return &Oracle{
8797
backend: backend,
8898
lastPrice: params.Default,
8999
maxPrice: maxPrice,
100+
ignorePrice: ignorePrice,
90101
checkBlocks: blocks,
91102
percentile: percent,
92103
}
@@ -122,7 +133,7 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
122133
txPrices []*big.Int
123134
)
124135
for sent < gpo.checkBlocks && number > 0 {
125-
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, result, quit)
136+
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
126137
sent++
127138
exp++
128139
number--
@@ -145,7 +156,7 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
145156
// meaningful returned, try to query more blocks. But the maximum
146157
// is 2*checkBlocks.
147158
if len(res.prices) == 1 && len(txPrices)+1+exp < gpo.checkBlocks*2 && number > 0 {
148-
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, result, quit)
159+
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
149160
sent++
150161
exp++
151162
number--
@@ -189,7 +200,7 @@ func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].GasPriceCmp(t[
189200
// and sends it to the result channel. If the block is empty or all transactions
190201
// are sent by the miner itself(it doesn't make any sense to include this kind of
191202
// transaction prices for sampling), nil gasprice is returned.
192-
func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, blockNum uint64, limit int, result chan getBlockPricesResult, quit chan struct{}) {
203+
func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, blockNum uint64, limit int, ignoreUnder *big.Int, result chan getBlockPricesResult, quit chan struct{}) {
193204
block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
194205
if block == nil {
195206
select {
@@ -205,7 +216,7 @@ func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, bloc
205216

206217
var prices []*big.Int
207218
for _, tx := range txs {
208-
if tx.GasPrice().Cmp(common.Big1) <= 0 {
219+
if ignoreUnder != nil && tx.GasPrice().Cmp(ignoreUnder) == -1 {
209220
continue
210221
}
211222
sender, err := types.Sender(signer, tx)

0 commit comments

Comments
 (0)