Skip to content

Commit b71091e

Browse files
committed
eth, eth/downloader, miner: use download events to check miner start
1 parent 0f76a1c commit b71091e

6 files changed

Lines changed: 83 additions & 18 deletions

File tree

‎core/error.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (err *UncleErr) Error() string {
4242
return err.Message
4343
}
4444

45-
func UncleError(str string) error {
46-
return &UncleErr{Message: str}
45+
func UncleError(format string, v ...interface{}) error {
46+
return &UncleErr{Message: fmt.Sprintf(format, v...)}
4747
}
4848

4949
func IsUncleErr(err error) bool {

‎core/manager.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package core
33
import (
44
"github.com/ethereum/go-ethereum/accounts"
55
"github.com/ethereum/go-ethereum/common"
6+
"github.com/ethereum/go-ethereum/eth/downloader"
67
"github.com/ethereum/go-ethereum/event"
78
"github.com/ethereum/go-ethereum/p2p"
89
)
910

11+
// TODO move this to types?
1012
type Backend interface {
1113
AccountManager() *accounts.Manager
1214
BlockProcessor() *BlockProcessor
@@ -18,4 +20,5 @@ type Backend interface {
1820
BlockDb() common.Database
1921
StateDb() common.Database
2022
EventMux() *event.TypeMux
23+
Downloader() *downloader.Downloader
2124
}

‎eth/backend.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ func New(config *Config) (*Ethereum, error) {
265265
}
266266

267267
eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())
268-
eth.downloader = downloader.New(eth.chainManager.HasBlock, eth.chainManager.GetBlock)
268+
eth.downloader = downloader.New(eth.EventMux(), eth.chainManager.HasBlock, eth.chainManager.GetBlock)
269269
eth.pow = ethash.New()
270270
eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit)
271271
eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.txPool, eth.chainManager, eth.EventMux())
272272
eth.chainManager.SetProcessor(eth.blockProcessor)
273-
eth.miner = miner.New(eth, eth.pow)
273+
eth.miner = miner.New(eth, eth.EventMux(), eth.pow)
274274
eth.miner.SetGasPrice(config.GasPrice)
275275

276276
eth.protocolManager = NewProtocolManager(config.ProtocolVersion, config.NetworkId, eth.eventMux, eth.txPool, eth.chainManager, eth.downloader)

‎eth/downloader/downloader.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/ethereum/go-ethereum/common"
1111
"github.com/ethereum/go-ethereum/core/types"
12+
"github.com/ethereum/go-ethereum/event"
1213
"github.com/ethereum/go-ethereum/logger"
1314
"github.com/ethereum/go-ethereum/logger/glog"
1415
)
@@ -55,6 +56,8 @@ type hashPack struct {
5556
}
5657

5758
type Downloader struct {
59+
mux *event.TypeMux
60+
5861
mu sync.RWMutex
5962
queue *queue
6063
peers *peerSet
@@ -76,8 +79,9 @@ type Downloader struct {
7679
cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
7780
}
7881

79-
func New(hasBlock hashCheckFn, getBlock getBlockFn) *Downloader {
82+
func New(mux *event.TypeMux, hasBlock hashCheckFn, getBlock getBlockFn) *Downloader {
8083
downloader := &Downloader{
84+
mux: mux,
8185
queue: newQueue(),
8286
peers: newPeerSet(),
8387
hasBlock: hasBlock,
@@ -93,6 +97,11 @@ func (d *Downloader) Stats() (current int, max int) {
9397
return d.queue.Size()
9498
}
9599

100+
// Synchronising returns the state of the downloader
101+
func (d *Downloader) Synchronising() bool {
102+
return atomic.LoadInt32(&d.synchronising) > 0
103+
}
104+
96105
// RegisterPeer injects a new download peer into the set of block source to be
97106
// used for fetching hashes and blocks from.
98107
func (d *Downloader) RegisterPeer(id string, head common.Hash, getHashes hashFetcherFn, getBlocks blockFetcherFn) error {
@@ -129,6 +138,9 @@ func (d *Downloader) Synchronise(id string, hash common.Hash) error {
129138
if atomic.CompareAndSwapInt32(&d.notified, 0, 1) {
130139
glog.V(logger.Info).Infoln("Block synchronisation started")
131140
}
141+
142+
d.mux.Post(StartEvent{})
143+
132144
// Create cancel channel for aborting mid-flight
133145
d.cancelLock.Lock()
134146
d.cancelCh = make(chan struct{})
@@ -166,6 +178,9 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash) (err error) {
166178
// reset on error
167179
if err != nil {
168180
d.queue.Reset()
181+
d.mux.Post(FailedEvent{err})
182+
} else {
183+
d.mux.Post(DoneEvent{})
169184
}
170185
}()
171186

‎eth/downloader/events.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package downloader
2+
3+
type DoneEvent struct{}
4+
type StartEvent struct{}
5+
type FailedEvent struct{ Err error }

‎miner/miner.go‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,63 @@ package miner
22

33
import (
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

1518
type 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

3464
func (m *Miner) SetGasPrice(price *big.Int) {
@@ -41,34 +71,46 @@ func (m *Miner) SetGasPrice(price *big.Int) {
4171
}
4272

4373
func (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

5996
func (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

64102
func (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+
72114
func (self *Miner) HashRate() int64 {
73115
return self.pow.GetHashrate()
74116
}

0 commit comments

Comments
 (0)