Skip to content

Commit dadd689

Browse files
committed
miner: fix data race on setting etherbase/extradata
1 parent b750cab commit dadd689

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
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/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)