2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
package gasprice
|
2015-05-26 07:28:32 -05:00
|
|
|
|
|
|
|
import (
|
2017-02-22 06:10:07 -06:00
|
|
|
"fmt"
|
2015-05-26 07:28:32 -05:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-12-15 21:26:23 -06:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2015-05-26 07:28:32 -05:00
|
|
|
)
|
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
const (
|
|
|
|
gpoProcessPastBlocks = 100
|
|
|
|
|
|
|
|
// for testing
|
|
|
|
gpoDefaultBaseCorrectionFactor = 110
|
|
|
|
gpoDefaultMinGasPrice = 10000000000000
|
|
|
|
)
|
2015-05-26 07:28:32 -05:00
|
|
|
|
|
|
|
type blockPriceInfo struct {
|
|
|
|
baseGasPrice *big.Int
|
|
|
|
}
|
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
type GpoParams struct {
|
|
|
|
GpoMinGasPrice *big.Int
|
|
|
|
GpoMaxGasPrice *big.Int
|
|
|
|
GpoFullBlockRatio int
|
|
|
|
GpobaseStepDown int
|
|
|
|
GpobaseStepUp int
|
|
|
|
GpobaseCorrectionFactor int
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
// GasPriceOracle recommends gas prices based on the content of recent
|
|
|
|
// blocks.
|
2015-05-26 07:28:32 -05:00
|
|
|
type GasPriceOracle struct {
|
2015-12-15 21:26:23 -06:00
|
|
|
chain *core.BlockChain
|
|
|
|
db ethdb.Database
|
|
|
|
evmux *event.TypeMux
|
|
|
|
params *GpoParams
|
2015-10-26 15:42:24 -05:00
|
|
|
initOnce sync.Once
|
|
|
|
minPrice *big.Int
|
|
|
|
lastBaseMutex sync.Mutex
|
|
|
|
lastBase *big.Int
|
|
|
|
|
|
|
|
// state of listenLoop
|
2015-05-26 07:28:32 -05:00
|
|
|
blocks map[uint64]*blockPriceInfo
|
|
|
|
firstProcessed, lastProcessed uint64
|
2015-10-26 15:42:24 -05:00
|
|
|
minBase *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGasPriceOracle returns a new oracle.
|
2015-12-15 21:26:23 -06:00
|
|
|
func NewGasPriceOracle(chain *core.BlockChain, db ethdb.Database, evmux *event.TypeMux, params *GpoParams) *GasPriceOracle {
|
|
|
|
minprice := params.GpoMinGasPrice
|
2015-10-26 15:42:24 -05:00
|
|
|
if minprice == nil {
|
|
|
|
minprice = big.NewInt(gpoDefaultMinGasPrice)
|
|
|
|
}
|
|
|
|
minbase := new(big.Int).Mul(minprice, big.NewInt(100))
|
2015-12-15 21:26:23 -06:00
|
|
|
if params.GpobaseCorrectionFactor > 0 {
|
|
|
|
minbase = minbase.Div(minbase, big.NewInt(int64(params.GpobaseCorrectionFactor)))
|
2015-10-26 15:42:24 -05:00
|
|
|
}
|
|
|
|
return &GasPriceOracle{
|
2015-12-15 21:26:23 -06:00
|
|
|
chain: chain,
|
|
|
|
db: db,
|
|
|
|
evmux: evmux,
|
|
|
|
params: params,
|
2015-10-26 15:42:24 -05:00
|
|
|
blocks: make(map[uint64]*blockPriceInfo),
|
|
|
|
minBase: minbase,
|
|
|
|
minPrice: minprice,
|
|
|
|
lastBase: minprice,
|
|
|
|
}
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
func (gpo *GasPriceOracle) init() {
|
|
|
|
gpo.initOnce.Do(func() {
|
2015-12-15 21:26:23 -06:00
|
|
|
gpo.processPastBlocks()
|
2015-10-26 15:42:24 -05:00
|
|
|
go gpo.listenLoop()
|
|
|
|
})
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
func (self *GasPriceOracle) processPastBlocks() {
|
2015-06-29 06:48:10 -05:00
|
|
|
last := int64(-1)
|
2015-12-15 21:26:23 -06:00
|
|
|
cblock := self.chain.CurrentBlock()
|
2015-06-29 06:48:10 -05:00
|
|
|
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-12-15 21:26:23 -06:00
|
|
|
block := self.chain.GetBlockByNumber(uint64(i))
|
2015-06-29 06:48:10 -05:00
|
|
|
if block != nil {
|
|
|
|
self.processBlock(block)
|
|
|
|
}
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) listenLoop() {
|
2015-12-15 21:26:23 -06:00
|
|
|
events := self.evmux.Subscribe(core.ChainEvent{}, core.ChainSplitEvent{})
|
2015-10-26 15:42:24 -05:00
|
|
|
defer events.Unsubscribe()
|
2015-10-12 07:04:38 -05:00
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
for event := range events.Chan() {
|
2015-10-12 07:04:38 -05:00
|
|
|
switch event := event.Data.(type) {
|
2015-05-26 07:28:32 -05:00
|
|
|
case core.ChainEvent:
|
2015-10-12 07:04:38 -05:00
|
|
|
self.processBlock(event.Block)
|
2015-05-26 07:28:32 -05:00
|
|
|
case core.ChainSplitEvent:
|
2015-10-12 07:04:38 -05:00
|
|
|
self.processBlock(event.Block)
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GasPriceOracle) processBlock(block *types.Block) {
|
|
|
|
i := block.NumberU64()
|
|
|
|
if i > self.lastProcessed {
|
|
|
|
self.lastProcessed = i
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
lastBase := self.minPrice
|
2015-05-26 07:28:32 -05:00
|
|
|
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 {
|
2015-12-15 21:26:23 -06:00
|
|
|
corr = self.params.GpobaseStepUp
|
2015-05-26 07:28:32 -05:00
|
|
|
} else {
|
2015-12-15 21:26:23 -06:00
|
|
|
corr = -self.params.GpobaseStepDown
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
crand := int64(corr * (900 + rand.Intn(201)))
|
|
|
|
newBase := new(big.Int).Mul(lastBase, big.NewInt(1000000+crand))
|
|
|
|
newBase.Div(newBase, big.NewInt(1000000))
|
|
|
|
|
2015-08-08 19:13:15 -05:00
|
|
|
if newBase.Cmp(self.minBase) < 0 {
|
|
|
|
newBase = self.minBase
|
|
|
|
}
|
|
|
|
|
2015-05-26 07:28:32 -05:00
|
|
|
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()
|
|
|
|
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Trace(fmt.Sprintf("Processed block #%v, base price is %v\n", i, newBase.Int64()))
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the lowers possible price with which a tx was or could have been included
|
|
|
|
func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
|
2015-08-08 19:13:15 -05:00
|
|
|
gasUsed := big.NewInt(0)
|
2015-05-26 07:28:32 -05:00
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
receipts := core.GetBlockReceipts(self.db, block.Hash(), block.NumberU64())
|
2015-07-03 19:25:04 -05:00
|
|
|
if len(receipts) > 0 {
|
2015-07-06 13:59:12 -05:00
|
|
|
if cgu := receipts[len(receipts)-1].CumulativeGasUsed; cgu != nil {
|
|
|
|
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-12-15 21:26:23 -06:00
|
|
|
big.NewInt(int64(self.params.GpoFullBlockRatio)))) < 0 {
|
2015-05-26 07:28:32 -05:00
|
|
|
// block is not full, could have posted a tx with MinGasPrice
|
2015-08-08 19:13:15 -05:00
|
|
|
return big.NewInt(0)
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
|
2015-06-26 20:08:50 -05:00
|
|
|
txs := block.Transactions()
|
|
|
|
if len(txs) == 0 {
|
2015-08-08 19:13:15 -05:00
|
|
|
return big.NewInt(0)
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:42:24 -05:00
|
|
|
// SuggestPrice returns the recommended gas price.
|
2015-05-26 07:28:32 -05:00
|
|
|
func (self *GasPriceOracle) SuggestPrice() *big.Int {
|
2015-10-26 15:42:24 -05:00
|
|
|
self.init()
|
2015-05-26 07:28:32 -05:00
|
|
|
self.lastBaseMutex.Lock()
|
2015-10-26 15:42:24 -05:00
|
|
|
price := new(big.Int).Set(self.lastBase)
|
2015-05-26 07:28:32 -05:00
|
|
|
self.lastBaseMutex.Unlock()
|
|
|
|
|
2015-12-15 21:26:23 -06:00
|
|
|
price.Mul(price, big.NewInt(int64(self.params.GpobaseCorrectionFactor)))
|
2015-10-26 15:42:24 -05:00
|
|
|
price.Div(price, big.NewInt(100))
|
|
|
|
if price.Cmp(self.minPrice) < 0 {
|
|
|
|
price.Set(self.minPrice)
|
2015-12-15 21:26:23 -06:00
|
|
|
} else if self.params.GpoMaxGasPrice != nil && price.Cmp(self.params.GpoMaxGasPrice) > 0 {
|
|
|
|
price.Set(self.params.GpoMaxGasPrice)
|
2015-05-26 07:39:13 -05:00
|
|
|
}
|
2015-10-26 15:42:24 -05:00
|
|
|
return price
|
2015-05-26 07:28:32 -05:00
|
|
|
}
|