Skip to content

Commit b06e20b

Browse files
Arachnidfjl
authored andcommitted
eth/gasprice: set default percentile to 60%, count blocks instead of transactions (#15828)
The first should address a long term issue where we recommend a gas price that is greater than that required for 50% of transactions in recent blocks, which can lead to gas price inflation as people take this figure and add a margin to it, resulting in a positive feedback loop.
1 parent 3a5a559 commit b06e20b

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

‎eth/config.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ var DefaultConfig = Config{
4949

5050
TxPool: core.DefaultTxPoolConfig,
5151
GPO: gasprice.Config{
52-
Blocks: 10,
53-
Percentile: 50,
52+
Blocks: 20,
53+
Percentile: 60,
5454
},
5555
}
5656

‎eth/gasprice/gasprice.go‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sync"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/types"
2627
"github.com/ethereum/go-ethereum/internal/ethapi"
2728
"github.com/ethereum/go-ethereum/params"
2829
"github.com/ethereum/go-ethereum/rpc"
@@ -101,9 +102,9 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
101102
ch := make(chan getBlockPricesResult, gpo.checkBlocks)
102103
sent := 0
103104
exp := 0
104-
var txPrices []*big.Int
105+
var blockPrices []*big.Int
105106
for sent < gpo.checkBlocks && blockNum > 0 {
106-
go gpo.getBlockPrices(ctx, blockNum, ch)
107+
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(blockNum))), blockNum, ch)
107108
sent++
108109
exp++
109110
blockNum--
@@ -115,25 +116,25 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
115116
return lastPrice, res.err
116117
}
117118
exp--
118-
if len(res.prices) > 0 {
119-
txPrices = append(txPrices, res.prices...)
119+
if res.price != nil {
120+
blockPrices = append(blockPrices, res.price)
120121
continue
121122
}
122123
if maxEmpty > 0 {
123124
maxEmpty--
124125
continue
125126
}
126127
if blockNum > 0 && sent < gpo.maxBlocks {
127-
go gpo.getBlockPrices(ctx, blockNum, ch)
128+
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(blockNum))), blockNum, ch)
128129
sent++
129130
exp++
130131
blockNum--
131132
}
132133
}
133134
price := lastPrice
134-
if len(txPrices) > 0 {
135-
sort.Sort(bigIntArray(txPrices))
136-
price = txPrices[(len(txPrices)-1)*gpo.percentile/100]
135+
if len(blockPrices) > 0 {
136+
sort.Sort(bigIntArray(blockPrices))
137+
price = blockPrices[(len(blockPrices)-1)*gpo.percentile/100]
137138
}
138139
if price.Cmp(maxPrice) > 0 {
139140
price = new(big.Int).Set(maxPrice)
@@ -147,24 +148,38 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
147148
}
148149

149150
type getBlockPricesResult struct {
150-
prices []*big.Int
151-
err error
151+
price *big.Int
152+
err error
152153
}
153154

154-
// getLowestPrice calculates the lowest transaction gas price in a given block
155+
type transactionsByGasPrice []*types.Transaction
156+
157+
func (t transactionsByGasPrice) Len() int { return len(t) }
158+
func (t transactionsByGasPrice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
159+
func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].GasPrice().Cmp(t[j].GasPrice()) < 0 }
160+
161+
// getBlockPrices calculates the lowest transaction gas price in a given block
155162
// and sends it to the result channel. If the block is empty, price is nil.
156-
func (gpo *Oracle) getBlockPrices(ctx context.Context, blockNum uint64, ch chan getBlockPricesResult) {
163+
func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, blockNum uint64, ch chan getBlockPricesResult) {
157164
block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
158165
if block == nil {
159166
ch <- getBlockPricesResult{nil, err}
160167
return
161168
}
162-
txs := block.Transactions()
163-
prices := make([]*big.Int, len(txs))
164-
for i, tx := range txs {
165-
prices[i] = tx.GasPrice()
169+
170+
blockTxs := block.Transactions()
171+
txs := make([]*types.Transaction, len(blockTxs))
172+
copy(txs, blockTxs)
173+
sort.Sort(transactionsByGasPrice(txs))
174+
175+
for _, tx := range txs {
176+
sender, err := types.Sender(signer, tx)
177+
if err == nil && sender != block.Coinbase() {
178+
ch <- getBlockPricesResult{tx.GasPrice(), nil}
179+
return
180+
}
166181
}
167-
ch <- getBlockPricesResult{prices, nil}
182+
ch <- getBlockPricesResult{nil, nil}
168183
}
169184

170185
type bigIntArray []*big.Int

0 commit comments

Comments
 (0)