mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
5.9 KiB
230 lines
5.9 KiB
10 years ago
|
// Copyright 2015 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
10 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
10 years ago
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
10 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
|
||
9 years ago
|
package gasprice
|
||
10 years ago
|
|
||
|
import (
|
||
|
"math/big"
|
||
|
"math/rand"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/core"
|
||
|
"github.com/ethereum/go-ethereum/core/types"
|
||
9 years ago
|
"github.com/ethereum/go-ethereum/ethdb"
|
||
|
"github.com/ethereum/go-ethereum/event"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/logger"
|
||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||
|
)
|
||
|
|
||
9 years ago
|
const (
|
||
|
gpoProcessPastBlocks = 100
|
||
|
|
||
|
// for testing
|
||
|
gpoDefaultBaseCorrectionFactor = 110
|
||
|
gpoDefaultMinGasPrice = 10000000000000
|
||
|
)
|
||
10 years ago
|
|
||
|
type blockPriceInfo struct {
|
||
|
baseGasPrice *big.Int
|
||
|
}
|
||
|
|
||
9 years ago
|
type GpoParams struct {
|
||
|
GpoMinGasPrice *big.Int
|
||
|
GpoMaxGasPrice *big.Int
|
||
|
GpoFullBlockRatio int
|
||
|
GpobaseStepDown int
|
||
|
GpobaseStepUp int
|
||
|
GpobaseCorrectionFactor int
|
||
|
}
|
||
|
|
||
9 years ago
|
// GasPriceOracle recommends gas prices based on the content of recent
|
||
|
// blocks.
|
||
10 years ago
|
type GasPriceOracle struct {
|
||
9 years ago
|
chain *core.BlockChain
|
||
|
db ethdb.Database
|
||
|
evmux *event.TypeMux
|
||
|
params *GpoParams
|
||
9 years ago
|
initOnce sync.Once
|
||
|
minPrice *big.Int
|
||
|
lastBaseMutex sync.Mutex
|
||
|
lastBase *big.Int
|
||
|
|
||
|
// state of listenLoop
|
||
10 years ago
|
blocks map[uint64]*blockPriceInfo
|
||
|
firstProcessed, lastProcessed uint64
|
||
9 years ago
|
minBase *big.Int
|
||
|
}
|
||
|
|
||
|
// NewGasPriceOracle returns a new oracle.
|
||
9 years ago
|
func NewGasPriceOracle(chain *core.BlockChain, db ethdb.Database, evmux *event.TypeMux, params *GpoParams) *GasPriceOracle {
|
||
|
minprice := params.GpoMinGasPrice
|
||
9 years ago
|
if minprice == nil {
|
||
|
minprice = big.NewInt(gpoDefaultMinGasPrice)
|
||
|
}
|
||
|
minbase := new(big.Int).Mul(minprice, big.NewInt(100))
|
||
9 years ago
|
if params.GpobaseCorrectionFactor > 0 {
|
||
|
minbase = minbase.Div(minbase, big.NewInt(int64(params.GpobaseCorrectionFactor)))
|
||
9 years ago
|
}
|
||
|
return &GasPriceOracle{
|
||
9 years ago
|
chain: chain,
|
||
|
db: db,
|
||
|
evmux: evmux,
|
||
|
params: params,
|
||
9 years ago
|
blocks: make(map[uint64]*blockPriceInfo),
|
||
|
minBase: minbase,
|
||
|
minPrice: minprice,
|
||
|
lastBase: minprice,
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func (gpo *GasPriceOracle) init() {
|
||
|
gpo.initOnce.Do(func() {
|
||
9 years ago
|
gpo.processPastBlocks()
|
||
9 years ago
|
go gpo.listenLoop()
|
||
|
})
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func (self *GasPriceOracle) processPastBlocks() {
|
||
10 years ago
|
last := int64(-1)
|
||
9 years ago
|
cblock := self.chain.CurrentBlock()
|
||
10 years ago
|
if cblock != nil {
|
||
|
last = int64(cblock.NumberU64())
|
||
|
}
|
||
|
first := int64(0)
|
||
10 years ago
|
if last > gpoProcessPastBlocks {
|
||
|
first = last - gpoProcessPastBlocks
|
||
|
}
|
||
10 years ago
|
self.firstProcessed = uint64(first)
|
||
10 years ago
|
for i := first; i <= last; i++ {
|
||
9 years ago
|
block := self.chain.GetBlockByNumber(uint64(i))
|
||
10 years ago
|
if block != nil {
|
||
|
self.processBlock(block)
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (self *GasPriceOracle) listenLoop() {
|
||
9 years ago
|
events := self.evmux.Subscribe(core.ChainEvent{}, core.ChainSplitEvent{})
|
||
9 years ago
|
defer events.Unsubscribe()
|
||
9 years ago
|
|
||
9 years ago
|
for event := range events.Chan() {
|
||
9 years ago
|
switch event := event.Data.(type) {
|
||
10 years ago
|
case core.ChainEvent:
|
||
9 years ago
|
self.processBlock(event.Block)
|
||
10 years ago
|
case core.ChainSplitEvent:
|
||
9 years ago
|
self.processBlock(event.Block)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (self *GasPriceOracle) processBlock(block *types.Block) {
|
||
|
i := block.NumberU64()
|
||
|
if i > self.lastProcessed {
|
||
|
self.lastProcessed = i
|
||
|
}
|
||
|
|
||
9 years ago
|
lastBase := self.minPrice
|
||
10 years ago
|
bpl := self.blocks[i-1]
|
||
|
if bpl != nil {
|
||
|
lastBase = bpl.baseGasPrice
|
||
|
}
|
||
|
if lastBase == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var corr int
|
||
|
lp := self.lowestPrice(block)
|
||
|
if lp == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if lastBase.Cmp(lp) < 0 {
|
||
9 years ago
|
corr = self.params.GpobaseStepUp
|
||
10 years ago
|
} else {
|
||
9 years ago
|
corr = -self.params.GpobaseStepDown
|
||
10 years ago
|
}
|
||
|
|
||
|
crand := int64(corr * (900 + rand.Intn(201)))
|
||
|
newBase := new(big.Int).Mul(lastBase, big.NewInt(1000000+crand))
|
||
|
newBase.Div(newBase, big.NewInt(1000000))
|
||
|
|
||
9 years ago
|
if newBase.Cmp(self.minBase) < 0 {
|
||
|
newBase = self.minBase
|
||
|
}
|
||
|
|
||
10 years ago
|
bpi := self.blocks[i]
|
||
|
if bpi == nil {
|
||
|
bpi = &blockPriceInfo{}
|
||
|
self.blocks[i] = bpi
|
||
|
}
|
||
|
bpi.baseGasPrice = newBase
|
||
|
self.lastBaseMutex.Lock()
|
||
|
self.lastBase = newBase
|
||
|
self.lastBaseMutex.Unlock()
|
||
|
|
||
9 years ago
|
glog.V(logger.Detail).Infof("Processed block #%v, base price is %v\n", i, newBase.Int64())
|
||
10 years ago
|
}
|
||
|
|
||
|
// returns the lowers possible price with which a tx was or could have been included
|
||
|
func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
|
||
9 years ago
|
gasUsed := big.NewInt(0)
|
||
10 years ago
|
|
||
9 years ago
|
receipts := core.GetBlockReceipts(self.db, block.Hash(), block.NumberU64())
|
||
10 years ago
|
if len(receipts) > 0 {
|
||
10 years ago
|
if cgu := receipts[len(receipts)-1].CumulativeGasUsed; cgu != nil {
|
||
|
gasUsed = receipts[len(receipts)-1].CumulativeGasUsed
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(),
|
||
9 years ago
|
big.NewInt(int64(self.params.GpoFullBlockRatio)))) < 0 {
|
||
10 years ago
|
// block is not full, could have posted a tx with MinGasPrice
|
||
9 years ago
|
return big.NewInt(0)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
txs := block.Transactions()
|
||
|
if len(txs) == 0 {
|
||
9 years ago
|
return big.NewInt(0)
|
||
10 years ago
|
}
|
||
|
// block is full, find smallest gasPrice
|
||
10 years ago
|
minPrice := txs[0].GasPrice()
|
||
|
for i := 1; i < len(txs); i++ {
|
||
|
price := txs[i].GasPrice()
|
||
10 years ago
|
if price.Cmp(minPrice) < 0 {
|
||
|
minPrice = price
|
||
|
}
|
||
|
}
|
||
|
return minPrice
|
||
|
}
|
||
|
|
||
9 years ago
|
// SuggestPrice returns the recommended gas price.
|
||
10 years ago
|
func (self *GasPriceOracle) SuggestPrice() *big.Int {
|
||
9 years ago
|
self.init()
|
||
10 years ago
|
self.lastBaseMutex.Lock()
|
||
9 years ago
|
price := new(big.Int).Set(self.lastBase)
|
||
10 years ago
|
self.lastBaseMutex.Unlock()
|
||
|
|
||
9 years ago
|
price.Mul(price, big.NewInt(int64(self.params.GpobaseCorrectionFactor)))
|
||
9 years ago
|
price.Div(price, big.NewInt(100))
|
||
|
if price.Cmp(self.minPrice) < 0 {
|
||
|
price.Set(self.minPrice)
|
||
9 years ago
|
} else if self.params.GpoMaxGasPrice != nil && price.Cmp(self.params.GpoMaxGasPrice) > 0 {
|
||
|
price.Set(self.params.GpoMaxGasPrice)
|
||
10 years ago
|
}
|
||
9 years ago
|
return price
|
||
10 years ago
|
}
|