Skip to content

Commit

Permalink
use fixed gas price since bsc network is idle
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Nov 17, 2020
1 parent bfb73f8 commit e0685dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
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
sampleThreshold 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,
sampleThreshold: 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 totalSamples 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)
totalSamples = totalSamples + 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 && totalSamples > gpo.sampleThreshold {
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

0 comments on commit e0685dc

Please sign in to comment.