Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R]use fixed gas price when network is idle #35

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions eth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ var DefaultConfig = Config{
},
TxPool: core.DefaultTxPoolConfig,
GPO: gasprice.Config{
Blocks: 20,
Percentile: 60,
Blocks: 20,
Percentile: 60,
OracleThreshold: 1000,
},
}

Expand Down
41 changes: 26 additions & 15 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
var maxPrice = big.NewInt(500 * params.GWei)

type Config struct {
Blocks int
Percentile int
Default *big.Int `toml:",omitempty"`
Blocks int
Percentile int
Default *big.Int `toml:",omitempty"`
OracleThreshold int `toml:",omitempty"`
}

// Oracle recommends gas prices based on the content of recent
Expand All @@ -46,6 +47,9 @@ type Oracle struct {
cacheLock sync.RWMutex
fetchLock sync.Mutex

defaultPrice *big.Int
sampleTxThreshold int

checkBlocks, maxEmpty, maxBlocks int
percentile int
}
Expand All @@ -64,12 +68,14 @@ func NewOracle(backend ethapi.Backend, params Config) *Oracle {
percent = 100
}
return &Oracle{
backend: backend,
lastPrice: params.Default,
checkBlocks: blocks,
maxEmpty: blocks / 2,
maxBlocks: blocks * 5,
percentile: percent,
backend: backend,
lastPrice: params.Default,
defaultPrice: params.Default,
checkBlocks: blocks,
maxEmpty: blocks / 2,
maxBlocks: blocks * 5,
percentile: percent,
sampleTxThreshold: params.OracleThreshold,
}
}

Expand Down Expand Up @@ -103,6 +109,7 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
sent := 0
exp := 0
var blockPrices []*big.Int
var totalTxSamples int
for sent < gpo.checkBlocks && blockNum > 0 {
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(blockNum))), blockNum, ch)
sent++
Expand All @@ -118,6 +125,7 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
exp--
if res.price != nil {
blockPrices = append(blockPrices, res.price)
totalTxSamples = totalTxSamples + res.number
continue
}
if maxEmpty > 0 {
Expand All @@ -132,9 +140,11 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
}
}
price := lastPrice
if len(blockPrices) > 0 {
if len(blockPrices) > 0 && totalTxSamples > gpo.sampleTxThreshold {
sort.Sort(bigIntArray(blockPrices))
price = blockPrices[(len(blockPrices)-1)*gpo.percentile/100]
} else {
price = gpo.defaultPrice
}
if price.Cmp(maxPrice) > 0 {
price = new(big.Int).Set(maxPrice)
Expand All @@ -148,8 +158,9 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
}

type getBlockPricesResult struct {
price *big.Int
err error
number int
price *big.Int
err error
}

type transactionsByGasPrice []*types.Transaction
Expand All @@ -163,7 +174,7 @@ func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].GasPrice().Cmp
func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, blockNum uint64, ch chan getBlockPricesResult) {
block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
if block == nil {
ch <- getBlockPricesResult{nil, err}
ch <- getBlockPricesResult{0, nil, err}
return
}

Expand All @@ -175,11 +186,11 @@ func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, bloc
for _, tx := range txs {
sender, err := types.Sender(signer, tx)
if err == nil && sender != block.Coinbase() {
ch <- getBlockPricesResult{tx.GasPrice(), nil}
ch <- getBlockPricesResult{len(txs), tx.GasPrice(), nil}
return
}
}
ch <- getBlockPricesResult{nil, nil}
ch <- getBlockPricesResult{0, nil, nil}
}

type bigIntArray []*big.Int
Expand Down