Poll-based miner

This commit is contained in:
Luke Williams 2016-06-21 18:43:38 +10:00
parent b2d29f9e9a
commit 5b0255f05d

View file

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"sync/atomic" "sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -103,7 +104,26 @@ func (m *Miner) SetGasPrice(price *big.Int) {
func (self *Miner) Start(coinbase common.Address, threads int) { func (self *Miner) Start(coinbase common.Address, threads int) {
self.sub = self.mux.Subscribe(core.TxPreEvent{}, core.NewBlockEvent{}, core.NewMinedBlockEvent{}) self.sub = self.mux.Subscribe(core.TxPreEvent{}, core.NewBlockEvent{}, core.NewMinedBlockEvent{})
go self.filterLoop(coinbase, threads) //go self.filterLoop(coinbase, threads)
go self.runLoop(coinbase, threads)
}
func (self *Miner) runLoop(coinbase common.Address, threads int) {
for true {
if len(self.worker.txQueue) == 0 {
if self.Mining() {
glog.V(logger.Info).Infoln("Nothing to do. The miner sleeps.")
self.Stop()
}
} else {
if self.Mining() == false {
glog.V(logger.Info).Infoln("A transaction awaits! Waking up miner.")
self.Start(coinbase, threads)
}
time.Sleep(3000 * time.Millisecond)
}
}
} }
func (self *Miner) filterLoop(coinbase common.Address, threads int) { func (self *Miner) filterLoop(coinbase common.Address, threads int) {
@ -149,8 +169,8 @@ func (self *Miner) actuallyStart(coinbase common.Address, threads int) {
} }
func (self *Miner) Stop() { func (self *Miner) Stop() {
if(self.sub != nil) { if self.sub != nil {
self.sub.Unsubscribe(); self.sub.Unsubscribe()
} }
self.worker.stop() self.worker.stop()
atomic.StoreInt32(&self.mining, 0) atomic.StoreInt32(&self.mining, 0)