@@ -2,33 +2,63 @@ package miner
22
33import (
44 "math/big"
5+ "sync/atomic"
56
67 "github.com/ethereum/go-ethereum/common"
78 "github.com/ethereum/go-ethereum/core"
89 "github.com/ethereum/go-ethereum/core/state"
910 "github.com/ethereum/go-ethereum/core/types"
11+ "github.com/ethereum/go-ethereum/eth/downloader"
12+ "github.com/ethereum/go-ethereum/event"
1013 "github.com/ethereum/go-ethereum/logger"
1114 "github.com/ethereum/go-ethereum/logger/glog"
1215 "github.com/ethereum/go-ethereum/pow"
1316)
1417
1518type Miner struct {
19+ mux * event.TypeMux
20+
1621 worker * worker
1722
1823 MinAcceptedGasPrice * big.Int
1924
20- threads int
21- mining bool
22- eth core.Backend
23- pow pow.PoW
25+ threads int
26+ coinbase common.Address
27+ mining int32
28+ eth core.Backend
29+ pow pow.PoW
30+
31+ canStart int32 // can start indicates whether we can start the mining operation
32+ shouldStart int32 // should start indicates whether we should start after sync
2433}
2534
26- func New (eth core.Backend , pow pow.PoW ) * Miner {
27- return & Miner {eth : eth , pow : pow , worker : newWorker (common.Address {}, eth )}
35+ func New (eth core.Backend , mux * event.TypeMux , pow pow.PoW ) * Miner {
36+ miner := & Miner {eth : eth , mux : mux , pow : pow , worker : newWorker (common.Address {}, eth ), canStart : 1 }
37+ go miner .update ()
38+
39+ return miner
2840}
2941
30- func (self * Miner ) Mining () bool {
31- return self .mining
42+ func (self * Miner ) update () {
43+ events := self .mux .Subscribe (downloader.StartEvent {}, downloader.DoneEvent {}, downloader.FailedEvent {})
44+ for ev := range events .Chan () {
45+ switch ev .(type ) {
46+ case downloader.StartEvent :
47+ atomic .StoreInt32 (& self .canStart , 0 )
48+ if self .Mining () {
49+ self .Stop ()
50+ glog .V (logger .Info ).Infoln ("Mining operation aborted due to sync operation" )
51+ }
52+ case downloader.DoneEvent , downloader.FailedEvent :
53+ shouldStart := atomic .LoadInt32 (& self .shouldStart ) == 1
54+
55+ atomic .StoreInt32 (& self .canStart , 1 )
56+ atomic .StoreInt32 (& self .shouldStart , 0 )
57+ if shouldStart {
58+ self .Start (self .coinbase , self .threads )
59+ }
60+ }
61+ }
3262}
3363
3464func (m * Miner ) SetGasPrice (price * big.Int ) {
@@ -41,34 +71,46 @@ func (m *Miner) SetGasPrice(price *big.Int) {
4171}
4272
4373func (self * Miner ) Start (coinbase common.Address , threads int ) {
74+ atomic .StoreInt32 (& self .shouldStart , 1 )
75+ self .threads = threads
76+ self .worker .coinbase = coinbase
77+
78+ if atomic .LoadInt32 (& self .canStart ) == 0 {
79+ glog .V (logger .Info ).Infoln ("Can not start mining operation due to network sync (starts when finished)" )
80+ return
81+ }
4482
45- self .mining = true
83+ atomic . StoreInt32 ( & self .mining , 1 )
4684
4785 for i := 0 ; i < threads ; i ++ {
4886 self .worker .register (NewCpuAgent (i , self .pow ))
4987 }
50- self .threads = threads
5188
5289 glog .V (logger .Info ).Infof ("Starting mining operation (CPU=%d TOT=%d)\n " , threads , len (self .worker .agents ))
5390
54- self .worker .coinbase = coinbase
5591 self .worker .start ()
92+
5693 self .worker .commitNewWork ()
5794}
5895
5996func (self * Miner ) Stop () {
6097 self .worker .stop ()
61- self .mining = false
98+ atomic .StoreInt32 (& self .mining , 0 )
99+ atomic .StoreInt32 (& self .shouldStart , 0 )
62100}
63101
64102func (self * Miner ) Register (agent Agent ) {
65- if self .mining {
103+ if atomic . LoadInt32 ( & self .mining ) == 0 {
66104 agent .Start ()
67105 }
68106
69107 self .worker .register (agent )
70108}
71109
110+ func (self * Miner ) Mining () bool {
111+ return atomic .LoadInt32 (& self .mining ) > 0
112+ }
113+
72114func (self * Miner ) HashRate () int64 {
73115 return self .pow .GetHashrate ()
74116}
0 commit comments