提交 2497f28a 编写于 作者: J Jeffrey Wilcke

Merge pull request #1627 from zsfelfoldi/gpo

GPO update
...@@ -37,12 +37,11 @@ type blockPriceInfo struct { ...@@ -37,12 +37,11 @@ type blockPriceInfo struct {
type GasPriceOracle struct { type GasPriceOracle struct {
eth *Ethereum eth *Ethereum
chain *core.ChainManager chain *core.ChainManager
pool *core.TxPool
events event.Subscription events event.Subscription
blocks map[uint64]*blockPriceInfo blocks map[uint64]*blockPriceInfo
firstProcessed, lastProcessed uint64 firstProcessed, lastProcessed uint64
lastBaseMutex sync.Mutex lastBaseMutex sync.Mutex
lastBase *big.Int lastBase, minBase *big.Int
} }
func NewGasPriceOracle(eth *Ethereum) (self *GasPriceOracle) { func NewGasPriceOracle(eth *Ethereum) (self *GasPriceOracle) {
...@@ -50,13 +49,15 @@ func NewGasPriceOracle(eth *Ethereum) (self *GasPriceOracle) { ...@@ -50,13 +49,15 @@ func NewGasPriceOracle(eth *Ethereum) (self *GasPriceOracle) {
self.blocks = make(map[uint64]*blockPriceInfo) self.blocks = make(map[uint64]*blockPriceInfo)
self.eth = eth self.eth = eth
self.chain = eth.chainManager self.chain = eth.chainManager
self.pool = eth.txPool
self.events = eth.EventMux().Subscribe( self.events = eth.EventMux().Subscribe(
core.ChainEvent{}, core.ChainEvent{},
core.ChainSplitEvent{}, core.ChainSplitEvent{},
core.TxPreEvent{},
core.TxPostEvent{},
) )
minbase := new(big.Int).Mul(self.eth.GpoMinGasPrice, big.NewInt(100))
minbase = minbase.Div(minbase, big.NewInt(int64(self.eth.GpobaseCorrectionFactor)))
self.minBase = minbase
self.processPastBlocks() self.processPastBlocks()
go self.listenLoop() go self.listenLoop()
return return
...@@ -93,8 +94,6 @@ func (self *GasPriceOracle) listenLoop() { ...@@ -93,8 +94,6 @@ func (self *GasPriceOracle) listenLoop() {
self.processBlock(ev.Block) self.processBlock(ev.Block)
case core.ChainSplitEvent: case core.ChainSplitEvent:
self.processBlock(ev.Block) self.processBlock(ev.Block)
case core.TxPreEvent:
case core.TxPostEvent:
} }
} }
self.events.Unsubscribe() self.events.Unsubscribe()
...@@ -131,6 +130,10 @@ func (self *GasPriceOracle) processBlock(block *types.Block) { ...@@ -131,6 +130,10 @@ func (self *GasPriceOracle) processBlock(block *types.Block) {
newBase := new(big.Int).Mul(lastBase, big.NewInt(1000000+crand)) newBase := new(big.Int).Mul(lastBase, big.NewInt(1000000+crand))
newBase.Div(newBase, big.NewInt(1000000)) newBase.Div(newBase, big.NewInt(1000000))
if newBase.Cmp(self.minBase) < 0 {
newBase = self.minBase
}
bpi := self.blocks[i] bpi := self.blocks[i]
if bpi == nil { if bpi == nil {
bpi = &blockPriceInfo{} bpi = &blockPriceInfo{}
...@@ -146,7 +149,7 @@ func (self *GasPriceOracle) processBlock(block *types.Block) { ...@@ -146,7 +149,7 @@ func (self *GasPriceOracle) processBlock(block *types.Block) {
// returns the lowers possible price with which a tx was or could have been included // returns the lowers possible price with which a tx was or could have been included
func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int { func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
gasUsed := new(big.Int) gasUsed := big.NewInt(0)
receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash()) receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash())
if len(receipts) > 0 { if len(receipts) > 0 {
...@@ -158,12 +161,12 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int { ...@@ -158,12 +161,12 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(), if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(),
big.NewInt(int64(self.eth.GpoFullBlockRatio)))) < 0 { big.NewInt(int64(self.eth.GpoFullBlockRatio)))) < 0 {
// block is not full, could have posted a tx with MinGasPrice // block is not full, could have posted a tx with MinGasPrice
return self.eth.GpoMinGasPrice return big.NewInt(0)
} }
txs := block.Transactions() txs := block.Transactions()
if len(txs) == 0 { if len(txs) == 0 {
return self.eth.GpoMinGasPrice return big.NewInt(0)
} }
// block is full, find smallest gasPrice // block is full, find smallest gasPrice
minPrice := txs[0].GasPrice() minPrice := txs[0].GasPrice()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册