2015-05-26 07:28:32 -05:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const gpoProcessPastBlocks = 100
|
|
|
|
|
|
|
|
type blockPriceInfo struct {
|
|
|
|
baseGasPrice *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
type GasPriceOracle struct {
|
|
|
|
eth *Ethereum
|
|
|
|
chain *core.ChainManager
|
|
|
|
pool *core.TxPool
|
|
|
|
events event.Subscription
|
|
|
|
blocks map[uint64]*blockPriceInfo
|
|
|
|
firstProcessed, lastProcessed uint64
|
|
|
|
lastBaseMutex sync.Mutex
|
|
|
|
lastBase *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGasPriceOracle(eth *Ethereum) (self *GasPriceOracle) {
|
|
|
|
self = &GasPriceOracle{}
|
|
|
|
self.blocks = make(map[uint64]*blockPriceInfo)
|
|
|
|
self.eth = eth
|
|
|
|
self.chain = eth.chainManager
|
|
|
|
self.pool = eth.txPool
|
|
|
|
self.events = eth.EventMux().Subscribe(
|
|
|
|
core.ChainEvent{},
|
|
|
|
core.ChainSplitEvent{},
|
|
|
|
core.TxPreEvent{},
|
|
|
|
core.TxPostEvent{},
|
|
|
|
)
|
|
|
|
self.processPastBlocks()
|
|
|
|
go self.listenLoop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) processPastBlocks() {
|
2015-06-29 06:48:10 -05:00
|
|
|
last := int64(-1)
|
|
|
|
cblock := self.chain.CurrentBlock()
|
|
|
|
if cblock != nil {
|
|
|
|
last = int64(cblock.NumberU64())
|
|
|
|
}
|
|
|
|
first := int64(0)
|
2015-05-26 07:28:32 -05:00
|
|
|
if last > gpoProcessPastBlocks {
|
|
|
|
first = last - gpoProcessPastBlocks
|
|
|
|
}
|
2015-06-29 06:48:10 -05:00
|
|
|
self.firstProcessed = uint64(first)
|
2015-05-26 07:28:32 -05:00
|
|
|
for i := first; i <= last; i++ {
|
2015-06-29 06:48:10 -05:00
|
|
|
block := self.chain.GetBlockByNumber(uint64(i))
|
|
|
|
if block != nil {
|
|
|
|
self.processBlock(block)
|
|
|
|
}
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) listenLoop() {
|
|
|
|
for {
|
|
|
|
ev, isopen := <-self.events.Chan()
|
|
|
|
if !isopen {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case core.ChainEvent:
|
|
|
|
self.processBlock(ev.Block)
|
|
|
|
case core.ChainSplitEvent:
|
|
|
|
self.processBlock(ev.Block)
|
|
|
|
case core.TxPreEvent:
|
|
|
|
case core.TxPostEvent:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.events.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) processBlock(block *types.Block) {
|
|
|
|
i := block.NumberU64()
|
|
|
|
if i > self.lastProcessed {
|
|
|
|
self.lastProcessed = i
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBase := self.eth.GpoMinGasPrice
|
|
|
|
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 {
|
|
|
|
corr = self.eth.GpobaseStepUp
|
|
|
|
} else {
|
|
|
|
corr = -self.eth.GpobaseStepDown
|
|
|
|
}
|
|
|
|
|
|
|
|
crand := int64(corr * (900 + rand.Intn(201)))
|
|
|
|
newBase := new(big.Int).Mul(lastBase, big.NewInt(1000000+crand))
|
|
|
|
newBase.Div(newBase, big.NewInt(1000000))
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
glog.V(logger.Detail).Infof("Processed block #%v, base price is %v\n", block.NumberU64(), newBase.Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the lowers possible price with which a tx was or could have been included
|
|
|
|
func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
|
|
|
|
gasUsed := new(big.Int)
|
|
|
|
|
2015-07-03 19:25:04 -05:00
|
|
|
receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash())
|
|
|
|
if len(receipts) > 0 {
|
|
|
|
gasUsed = receipts[len(receipts)-1].CumulativeGasUsed
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
2015-06-26 20:08:50 -05:00
|
|
|
if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(),
|
2015-05-26 07:28:32 -05:00
|
|
|
big.NewInt(int64(self.eth.GpoFullBlockRatio)))) < 0 {
|
|
|
|
// block is not full, could have posted a tx with MinGasPrice
|
|
|
|
return self.eth.GpoMinGasPrice
|
|
|
|
}
|
|
|
|
|
2015-06-26 20:08:50 -05:00
|
|
|
txs := block.Transactions()
|
|
|
|
if len(txs) == 0 {
|
2015-05-26 07:28:32 -05:00
|
|
|
return self.eth.GpoMinGasPrice
|
|
|
|
}
|
|
|
|
// block is full, find smallest gasPrice
|
2015-06-26 20:08:50 -05:00
|
|
|
minPrice := txs[0].GasPrice()
|
|
|
|
for i := 1; i < len(txs); i++ {
|
|
|
|
price := txs[i].GasPrice()
|
2015-05-26 07:28:32 -05:00
|
|
|
if price.Cmp(minPrice) < 0 {
|
|
|
|
minPrice = price
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) SuggestPrice() *big.Int {
|
|
|
|
self.lastBaseMutex.Lock()
|
|
|
|
base := self.lastBase
|
|
|
|
self.lastBaseMutex.Unlock()
|
|
|
|
|
2015-05-26 07:39:13 -05:00
|
|
|
if base == nil {
|
|
|
|
base = self.eth.GpoMinGasPrice
|
|
|
|
}
|
2015-05-26 08:15:54 -05:00
|
|
|
if base == nil {
|
|
|
|
return big.NewInt(10000000000000) // apparently MinGasPrice is not initialized during some tests
|
|
|
|
}
|
2015-05-26 07:39:13 -05:00
|
|
|
|
2015-05-27 05:39:59 -05:00
|
|
|
baseCorr := new(big.Int).Mul(base, big.NewInt(int64(self.eth.GpobaseCorrectionFactor)))
|
2015-05-26 07:28:32 -05:00
|
|
|
baseCorr.Div(baseCorr, big.NewInt(100))
|
|
|
|
|
|
|
|
if baseCorr.Cmp(self.eth.GpoMinGasPrice) < 0 {
|
|
|
|
return self.eth.GpoMinGasPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseCorr.Cmp(self.eth.GpoMaxGasPrice) > 0 {
|
|
|
|
return self.eth.GpoMaxGasPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseCorr
|
|
|
|
}
|