eth/gasprice: fixed naming issues

This commit is contained in:
Zsolt Felfoldi 2017-04-05 20:37:17 +02:00
parent ab698a5912
commit e8fd5cb5cb
6 changed files with 69 additions and 69 deletions

View file

@ -415,8 +415,8 @@ var (
Value: 5, Value: 5,
} }
GpoPercentileFlag = cli.IntFlag{ GpoPercentileFlag = cli.IntFlag{
Name: "gpopercent", Name: "gpopercentile",
Usage: "Percentile of recent transaction gas prices", Usage: "Suggested gas price is the given percentile of a set of recent transaction gas prices",
Value: 50, Value: 50,
} }
) )

View file

@ -39,7 +39,7 @@ import (
// EthApiBackend implements ethapi.Backend for full nodes // EthApiBackend implements ethapi.Backend for full nodes
type EthApiBackend struct { type EthApiBackend struct {
eth *Ethereum eth *Ethereum
gpo *gasprice.GasPriceOracle gpo *gasprice.Oracle
} }
func (b *EthApiBackend) ChainConfig() *params.ChainConfig { func (b *EthApiBackend) ChainConfig() *params.ChainConfig {

View file

@ -215,12 +215,12 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
eth.miner.SetExtra(config.ExtraData) eth.miner.SetExtra(config.ExtraData)
eth.ApiBackend = &EthApiBackend{eth, nil} eth.ApiBackend = &EthApiBackend{eth, nil}
gpoParams := gasprice.GpoParams{ gpoParams := gasprice.Config{
GpoBlocks: config.GpoBlocks, Blocks: config.GpoBlocks,
GpoPercentile: config.GpoPercentile, Percentile: config.GpoPercentile,
GpoDefault: config.GasPrice, Default: config.GasPrice,
} }
eth.ApiBackend.gpo = gasprice.NewGasPriceOracle(eth.ApiBackend, gpoParams) eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams)
return eth, nil return eth, nil
} }

View file

@ -27,15 +27,15 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
type GpoParams struct { type Config struct {
GpoBlocks int Blocks int
GpoPercentile int Percentile int
GpoDefault *big.Int Default *big.Int
} }
// GasPriceOracle recommends gas prices based on the content of recent // Oracle recommends gas prices based on the content of recent
// blocks. Suitable for both light and full clients. // blocks. Suitable for both light and full clients.
type GasPriceOracle struct { type Oracle struct {
backend ethapi.Backend backend ethapi.Backend
lastHead common.Hash lastHead common.Hash
lastPrice *big.Int lastPrice *big.Int
@ -46,22 +46,22 @@ type GasPriceOracle struct {
percentile int percentile int
} }
// NewGasPriceOracle returns a new oracle. // NewOracle returns a new oracle.
func NewGasPriceOracle(backend ethapi.Backend, params GpoParams) *GasPriceOracle { func NewOracle(backend ethapi.Backend, params Config) *Oracle {
blocks := params.GpoBlocks blocks := params.Blocks
if blocks < 1 { if blocks < 1 {
blocks = 1 blocks = 1
} }
percent := params.GpoPercentile percent := params.Percentile
if percent < 0 { if percent < 0 {
percent = 0 percent = 0
} }
if percent > 100 { if percent > 100 {
percent = 100 percent = 100
} }
return &GasPriceOracle{ return &Oracle{
backend: backend, backend: backend,
lastPrice: params.GpoDefault, lastPrice: params.Default,
checkBlocks: blocks, checkBlocks: blocks,
minBlocks: (blocks + 1) / 2, minBlocks: (blocks + 1) / 2,
maxBlocks: blocks * 5, maxBlocks: blocks * 5,
@ -70,95 +70,95 @@ func NewGasPriceOracle(backend ethapi.Backend, params GpoParams) *GasPriceOracle
} }
// SuggestPrice returns the recommended gas price. // SuggestPrice returns the recommended gas price.
func (self *GasPriceOracle) SuggestPrice(ctx context.Context) (*big.Int, error) { func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
self.cacheLock.RLock() gpo.cacheLock.RLock()
lastHead := self.lastHead lastHead := gpo.lastHead
lastPrice := self.lastPrice lastPrice := gpo.lastPrice
self.cacheLock.RUnlock() gpo.cacheLock.RUnlock()
head, _ := self.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber) head, _ := gpo.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
headHash := head.Hash() headHash := head.Hash()
if headHash == lastHead { if headHash == lastHead {
return lastPrice, nil return lastPrice, nil
} }
self.fetchLock.Lock() gpo.fetchLock.Lock()
defer self.fetchLock.Unlock() defer gpo.fetchLock.Unlock()
// try checking the cache again, maybe the last fetch fetched what we need // try checking the cache again, maybe the last fetch fetched what we need
self.cacheLock.RLock() gpo.cacheLock.RLock()
lastHead = self.lastHead lastHead = gpo.lastHead
lastPrice = self.lastPrice lastPrice = gpo.lastPrice
self.cacheLock.RUnlock() gpo.cacheLock.RUnlock()
if headHash == lastHead { if headHash == lastHead {
return lastPrice, nil return lastPrice, nil
} }
blockNum := head.Number.Uint64() blockNum := head.Number.Uint64()
chn := make(chan lpResult, self.checkBlocks) ch := make(chan getBlockPricesResult, gpo.checkBlocks)
sent := 0 sent := 0
exp := 0 exp := 0
var lps bigIntArray var txPrices []*big.Int
for sent < self.checkBlocks && blockNum > 0 { for sent < gpo.checkBlocks && blockNum > 0 {
go self.getBlockPrices(ctx, blockNum, chn) go gpo.getBlockPrices(ctx, blockNum, ch)
sent++ sent++
exp++ exp++
blockNum-- blockNum--
} }
maxEmpty := self.checkBlocks - self.minBlocks maxEmpty := gpo.checkBlocks - gpo.minBlocks
for exp > 0 { for exp > 0 {
res := <-chn res := <-ch
if res.err != nil { if res.err != nil {
return lastPrice, res.err return lastPrice, res.err
} }
exp-- exp--
if len(res.prices) > 0 { if len(res.prices) > 0 {
lps = append(lps, res.prices...) txPrices = append(txPrices, res.prices...)
} else { continue
if maxEmpty > 0 { }
maxEmpty-- if maxEmpty > 0 {
} else { maxEmpty--
if blockNum > 0 && sent < self.maxBlocks { continue
go self.getBlockPrices(ctx, blockNum, chn) }
sent++ if blockNum > 0 && sent < gpo.maxBlocks {
exp++ go gpo.getBlockPrices(ctx, blockNum, ch)
blockNum-- sent++
} exp++
} blockNum--
} }
} }
price := lastPrice price := lastPrice
if len(lps) > 0 { if len(txPrices) > 0 {
sort.Sort(lps) sort.Sort(bigIntArray(txPrices))
price = lps[(len(lps)-1)*self.percentile/100] price = txPrices[(len(txPrices)-1)*gpo.percentile/100]
} }
self.cacheLock.Lock() gpo.cacheLock.Lock()
self.lastHead = headHash gpo.lastHead = headHash
self.lastPrice = price gpo.lastPrice = price
self.cacheLock.Unlock() gpo.cacheLock.Unlock()
return price, nil return price, nil
} }
type lpResult struct { type getBlockPricesResult struct {
prices []*big.Int prices []*big.Int
err error err error
} }
// getLowestPrice calculates the lowest transaction gas price in a given block // getLowestPrice calculates the lowest transaction gas price in a given block
// and sends it to the result channel. If the block is empty, price is nil. // and sends it to the result channel. If the block is empty, price is nil.
func (self *GasPriceOracle) getBlockPrices(ctx context.Context, blockNum uint64, chn chan lpResult) { func (gpo *Oracle) getBlockPrices(ctx context.Context, blockNum uint64, ch chan getBlockPricesResult) {
block, err := self.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum)) block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
if block == nil { if block == nil {
chn <- lpResult{nil, err} ch <- getBlockPricesResult{nil, err}
return return
} }
txs := block.Transactions() txs := block.Transactions()
prices := make(bigIntArray, len(txs)) prices := make([]*big.Int, len(txs))
for i, tx := range txs { for i, tx := range txs {
prices[i] = tx.GasPrice() prices[i] = tx.GasPrice()
} }
chn <- lpResult{prices, nil} ch <- getBlockPricesResult{prices, nil}
} }
type bigIntArray []*big.Int type bigIntArray []*big.Int

View file

@ -38,7 +38,7 @@ import (
type LesApiBackend struct { type LesApiBackend struct {
eth *LightEthereum eth *LightEthereum
gpo *gasprice.GasPriceOracle gpo *gasprice.Oracle
} }
func (b *LesApiBackend) ChainConfig() *params.ChainConfig { func (b *LesApiBackend) ChainConfig() *params.ChainConfig {

View file

@ -113,12 +113,12 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
relay.reqDist = eth.protocolManager.reqDist relay.reqDist = eth.protocolManager.reqDist
eth.ApiBackend = &LesApiBackend{eth, nil} eth.ApiBackend = &LesApiBackend{eth, nil}
gpoParams := gasprice.GpoParams{ gpoParams := gasprice.Config{
GpoBlocks: config.GpoBlocks, Blocks: config.GpoBlocks,
GpoPercentile: config.GpoPercentile, Percentile: config.GpoPercentile,
GpoDefault: config.GasPrice, Default: config.GasPrice,
} }
eth.ApiBackend.gpo = gasprice.NewGasPriceOracle(eth.ApiBackend, gpoParams) eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams)
return eth, nil return eth, nil
} }