mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
Conditional mining using events
This commit is contained in:
parent
fc0638f9d8
commit
45a259f105
1 changed files with 42 additions and 16 deletions
|
|
@ -49,6 +49,8 @@ type Miner struct {
|
|||
|
||||
canStart int32 // can start indicates whether we can start the mining operation
|
||||
shouldStart int32 // should start indicates whether we should start after sync
|
||||
|
||||
sub event.Subscription
|
||||
}
|
||||
|
||||
func New(eth core.Backend, config *core.ChainConfig, mux *event.TypeMux, pow pow.PoW) *Miner {
|
||||
|
|
@ -100,6 +102,29 @@ func (m *Miner) SetGasPrice(price *big.Int) {
|
|||
}
|
||||
|
||||
func (self *Miner) Start(coinbase common.Address, threads int) {
|
||||
self.sub = self.mux.Subscribe(core.TxPreEvent{}, core.NewBlockEvent{}, core.NewMinedBlockEvent{})
|
||||
go self.filterLoop(coinbase, threads)
|
||||
}
|
||||
|
||||
func (self *Miner) filterLoop(coinbase common.Address, threads int) {
|
||||
for event := range self.sub.Chan() {
|
||||
switch event.Data.(type) {
|
||||
case core.TxPreEvent:
|
||||
glog.V(logger.Info).Infoln("New pending tranny yo, start mining!")
|
||||
self.actuallyStart(coinbase, threads)
|
||||
case core.NewBlockEvent:
|
||||
glog.V(logger.Info).Infoln("New block event")
|
||||
self.Stop()
|
||||
self.Start(coinbase, threads)
|
||||
case core.NewMinedBlockEvent:
|
||||
glog.V(logger.Info).Infoln("New MINED block event")
|
||||
self.Stop()
|
||||
self.Start(coinbase, threads)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Miner) actuallyStart(coinbase common.Address, threads int) {
|
||||
atomic.StoreInt32(&self.shouldStart, 1)
|
||||
self.threads = threads
|
||||
self.worker.coinbase = coinbase
|
||||
|
|
@ -124,6 +149,7 @@ func (self *Miner) Start(coinbase common.Address, threads int) {
|
|||
}
|
||||
|
||||
func (self *Miner) Stop() {
|
||||
self.sub.Unsubscribe();
|
||||
self.worker.stop()
|
||||
atomic.StoreInt32(&self.mining, 0)
|
||||
atomic.StoreInt32(&self.shouldStart, 0)
|
||||
|
|
|
|||
Loading…
Reference in a new issue