From e8fd5cb5cb8739b15b9027cce3be1d58afb8757a Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Wed, 5 Apr 2017 20:37:17 +0200 Subject: [PATCH] eth/gasprice: fixed naming issues --- cmd/utils/flags.go | 4 +- eth/api_backend.go | 2 +- eth/backend.go | 10 ++-- eth/gasprice/gasprice.go | 110 +++++++++++++++++++-------------------- les/api_backend.go | 2 +- les/backend.go | 10 ++-- 6 files changed, 69 insertions(+), 69 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 7df5c4a5bf..f0e80fd10d 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -415,8 +415,8 @@ var ( Value: 5, } GpoPercentileFlag = cli.IntFlag{ - Name: "gpopercent", - Usage: "Percentile of recent transaction gas prices", + Name: "gpopercentile", + Usage: "Suggested gas price is the given percentile of a set of recent transaction gas prices", Value: 50, } ) diff --git a/eth/api_backend.go b/eth/api_backend.go index 3900bc71b4..61e1844017 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -39,7 +39,7 @@ import ( // EthApiBackend implements ethapi.Backend for full nodes type EthApiBackend struct { eth *Ethereum - gpo *gasprice.GasPriceOracle + gpo *gasprice.Oracle } func (b *EthApiBackend) ChainConfig() *params.ChainConfig { diff --git a/eth/backend.go b/eth/backend.go index c3f82bf5d7..cd0b123287 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -215,12 +215,12 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { eth.miner.SetExtra(config.ExtraData) eth.ApiBackend = &EthApiBackend{eth, nil} - gpoParams := gasprice.GpoParams{ - GpoBlocks: config.GpoBlocks, - GpoPercentile: config.GpoPercentile, - GpoDefault: config.GasPrice, + gpoParams := gasprice.Config{ + Blocks: config.GpoBlocks, + Percentile: config.GpoPercentile, + Default: config.GasPrice, } - eth.ApiBackend.gpo = gasprice.NewGasPriceOracle(eth.ApiBackend, gpoParams) + eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams) return eth, nil } diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index 088253342b..3cf2146c20 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -27,15 +27,15 @@ import ( "github.com/ethereum/go-ethereum/rpc" ) -type GpoParams struct { - GpoBlocks int - GpoPercentile int - GpoDefault *big.Int +type Config struct { + Blocks int + Percentile 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. -type GasPriceOracle struct { +type Oracle struct { backend ethapi.Backend lastHead common.Hash lastPrice *big.Int @@ -46,22 +46,22 @@ type GasPriceOracle struct { percentile int } -// NewGasPriceOracle returns a new oracle. -func NewGasPriceOracle(backend ethapi.Backend, params GpoParams) *GasPriceOracle { - blocks := params.GpoBlocks +// NewOracle returns a new oracle. +func NewOracle(backend ethapi.Backend, params Config) *Oracle { + blocks := params.Blocks if blocks < 1 { blocks = 1 } - percent := params.GpoPercentile + percent := params.Percentile if percent < 0 { percent = 0 } if percent > 100 { percent = 100 } - return &GasPriceOracle{ + return &Oracle{ backend: backend, - lastPrice: params.GpoDefault, + lastPrice: params.Default, checkBlocks: blocks, minBlocks: (blocks + 1) / 2, maxBlocks: blocks * 5, @@ -70,95 +70,95 @@ func NewGasPriceOracle(backend ethapi.Backend, params GpoParams) *GasPriceOracle } // SuggestPrice returns the recommended gas price. -func (self *GasPriceOracle) SuggestPrice(ctx context.Context) (*big.Int, error) { - self.cacheLock.RLock() - lastHead := self.lastHead - lastPrice := self.lastPrice - self.cacheLock.RUnlock() +func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) { + gpo.cacheLock.RLock() + lastHead := gpo.lastHead + lastPrice := gpo.lastPrice + gpo.cacheLock.RUnlock() - head, _ := self.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber) + head, _ := gpo.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber) headHash := head.Hash() if headHash == lastHead { return lastPrice, nil } - self.fetchLock.Lock() - defer self.fetchLock.Unlock() + gpo.fetchLock.Lock() + defer gpo.fetchLock.Unlock() // try checking the cache again, maybe the last fetch fetched what we need - self.cacheLock.RLock() - lastHead = self.lastHead - lastPrice = self.lastPrice - self.cacheLock.RUnlock() + gpo.cacheLock.RLock() + lastHead = gpo.lastHead + lastPrice = gpo.lastPrice + gpo.cacheLock.RUnlock() if headHash == lastHead { return lastPrice, nil } blockNum := head.Number.Uint64() - chn := make(chan lpResult, self.checkBlocks) + ch := make(chan getBlockPricesResult, gpo.checkBlocks) sent := 0 exp := 0 - var lps bigIntArray - for sent < self.checkBlocks && blockNum > 0 { - go self.getBlockPrices(ctx, blockNum, chn) + var txPrices []*big.Int + for sent < gpo.checkBlocks && blockNum > 0 { + go gpo.getBlockPrices(ctx, blockNum, ch) sent++ exp++ blockNum-- } - maxEmpty := self.checkBlocks - self.minBlocks + maxEmpty := gpo.checkBlocks - gpo.minBlocks for exp > 0 { - res := <-chn + res := <-ch if res.err != nil { return lastPrice, res.err } exp-- if len(res.prices) > 0 { - lps = append(lps, res.prices...) - } else { - if maxEmpty > 0 { - maxEmpty-- - } else { - if blockNum > 0 && sent < self.maxBlocks { - go self.getBlockPrices(ctx, blockNum, chn) - sent++ - exp++ - blockNum-- - } - } + txPrices = append(txPrices, res.prices...) + continue + } + if maxEmpty > 0 { + maxEmpty-- + continue + } + if blockNum > 0 && sent < gpo.maxBlocks { + go gpo.getBlockPrices(ctx, blockNum, ch) + sent++ + exp++ + blockNum-- } } price := lastPrice - if len(lps) > 0 { - sort.Sort(lps) - price = lps[(len(lps)-1)*self.percentile/100] + if len(txPrices) > 0 { + sort.Sort(bigIntArray(txPrices)) + price = txPrices[(len(txPrices)-1)*gpo.percentile/100] } - self.cacheLock.Lock() - self.lastHead = headHash - self.lastPrice = price - self.cacheLock.Unlock() + gpo.cacheLock.Lock() + gpo.lastHead = headHash + gpo.lastPrice = price + gpo.cacheLock.Unlock() return price, nil } -type lpResult struct { +type getBlockPricesResult struct { prices []*big.Int err error } // 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. -func (self *GasPriceOracle) getBlockPrices(ctx context.Context, blockNum uint64, chn chan lpResult) { - block, err := self.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum)) +func (gpo *Oracle) getBlockPrices(ctx context.Context, blockNum uint64, ch chan getBlockPricesResult) { + block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum)) if block == nil { - chn <- lpResult{nil, err} + ch <- getBlockPricesResult{nil, err} return } txs := block.Transactions() - prices := make(bigIntArray, len(txs)) + prices := make([]*big.Int, len(txs)) for i, tx := range txs { prices[i] = tx.GasPrice() } - chn <- lpResult{prices, nil} + ch <- getBlockPricesResult{prices, nil} } type bigIntArray []*big.Int diff --git a/les/api_backend.go b/les/api_backend.go index 8748e2255c..67de3bcd5c 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -38,7 +38,7 @@ import ( type LesApiBackend struct { eth *LightEthereum - gpo *gasprice.GasPriceOracle + gpo *gasprice.Oracle } func (b *LesApiBackend) ChainConfig() *params.ChainConfig { diff --git a/les/backend.go b/les/backend.go index 1cf4671d2d..a456fd046f 100644 --- a/les/backend.go +++ b/les/backend.go @@ -113,12 +113,12 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { relay.reqDist = eth.protocolManager.reqDist eth.ApiBackend = &LesApiBackend{eth, nil} - gpoParams := gasprice.GpoParams{ - GpoBlocks: config.GpoBlocks, - GpoPercentile: config.GpoPercentile, - GpoDefault: config.GasPrice, + gpoParams := gasprice.Config{ + Blocks: config.GpoBlocks, + Percentile: config.GpoPercentile, + Default: config.GasPrice, } - eth.ApiBackend.gpo = gasprice.NewGasPriceOracle(eth.ApiBackend, gpoParams) + eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams) return eth, nil }