Skip to content

Commit f15828e

Browse files
authored
Merge pull request #3431 from karalabe/miner-race-fixes
Miner race fixes
2 parents a98e8c0 + dadd689 commit f15828e

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

miner/miner.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,22 @@ func (m *Miner) SetGasPrice(price *big.Int) {
119119

120120
func (self *Miner) Start(coinbase common.Address, threads int) {
121121
atomic.StoreInt32(&self.shouldStart, 1)
122-
self.threads = threads
123-
self.worker.coinbase = coinbase
122+
self.worker.setEtherbase(coinbase)
124123
self.coinbase = coinbase
124+
self.threads = threads
125125

126126
if atomic.LoadInt32(&self.canStart) == 0 {
127127
glog.V(logger.Info).Infoln("Can not start mining operation due to network sync (starts when finished)")
128128
return
129129
}
130-
131130
atomic.StoreInt32(&self.mining, 1)
132131

133132
for i := 0; i < threads; i++ {
134133
self.worker.register(NewCpuAgent(i, self.pow))
135134
}
136135

137136
glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
138-
139137
self.worker.start()
140-
141138
self.worker.commitNewWork()
142139
}
143140

@@ -177,8 +174,7 @@ func (self *Miner) SetExtra(extra []byte) error {
177174
if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
178175
return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
179176
}
180-
181-
self.worker.extra = extra
177+
self.worker.setExtra(extra)
182178
return nil
183179
}
184180

@@ -188,9 +184,9 @@ func (self *Miner) Pending() (*types.Block, *state.StateDB) {
188184
}
189185

190186
// PendingBlock returns the currently pending block.
191-
//
192-
// Note, to access both the pending block and the pending state
193-
// simultaneously, please use Pending(), as the pending state can
187+
//
188+
// Note, to access both the pending block and the pending state
189+
// simultaneously, please use Pending(), as the pending state can
194190
// change between multiple method calls
195191
func (self *Miner) PendingBlock() *types.Block {
196192
return self.worker.pendingBlock()

miner/remote_agent.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type hashrate struct {
3737
type RemoteAgent struct {
3838
mu sync.Mutex
3939

40-
quit chan struct{}
40+
quitCh chan struct{}
4141
workCh chan *Work
4242
returnCh chan<- *Result
4343

@@ -76,18 +76,16 @@ func (a *RemoteAgent) Start() {
7676
if !atomic.CompareAndSwapInt32(&a.running, 0, 1) {
7777
return
7878
}
79-
80-
a.quit = make(chan struct{})
79+
a.quitCh = make(chan struct{})
8180
a.workCh = make(chan *Work, 1)
82-
go a.maintainLoop()
81+
go a.loop(a.workCh, a.quitCh)
8382
}
8483

8584
func (a *RemoteAgent) Stop() {
8685
if !atomic.CompareAndSwapInt32(&a.running, 1, 0) {
8786
return
8887
}
89-
90-
close(a.quit)
88+
close(a.quitCh)
9189
close(a.workCh)
9290
}
9391

@@ -148,15 +146,20 @@ func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, hash common.Hash) bool
148146
return false
149147
}
150148

151-
func (a *RemoteAgent) maintainLoop() {
149+
// loop monitors mining events on the work and quit channels, updating the internal
150+
// state of the rmeote miner until a termination is requested.
151+
//
152+
// Note, the reason the work and quit channels are passed as parameters is because
153+
// RemoteAgent.Start() constantly recreates these channels, so the loop code cannot
154+
// assume data stability in these member fields.
155+
func (a *RemoteAgent) loop(workCh chan *Work, quitCh chan struct{}) {
152156
ticker := time.Tick(5 * time.Second)
153157

154-
out:
155158
for {
156159
select {
157-
case <-a.quit:
158-
break out
159-
case work := <-a.workCh:
160+
case <-quitCh:
161+
return
162+
case work := <-workCh:
160163
a.mu.Lock()
161164
a.currentWork = work
162165
a.mu.Unlock()

miner/worker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ func (self *worker) setEtherbase(addr common.Address) {
161161
self.coinbase = addr
162162
}
163163

164+
func (self *worker) setExtra(extra []byte) {
165+
self.mu.Lock()
166+
defer self.mu.Unlock()
167+
self.extra = extra
168+
}
169+
164170
func (self *worker) pending() (*types.Block, *state.StateDB) {
165171
self.currentMu.Lock()
166172
defer self.currentMu.Unlock()

0 commit comments

Comments
 (0)