Skip to content

Commit f06ae5c

Browse files
gballetfjl
authored andcommitted
miner: fix staticcheck warnings (#20375)
1 parent 3a0480e commit f06ae5c

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

miner/miner.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
8484
// It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
8585
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
8686
// and halt your mining operation for as long as the DOS continues.
87-
func (self *Miner) update() {
88-
events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
87+
func (miner *Miner) update() {
88+
events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
8989
defer events.Unsubscribe()
9090

9191
for {
@@ -96,89 +96,89 @@ func (self *Miner) update() {
9696
}
9797
switch ev.Data.(type) {
9898
case downloader.StartEvent:
99-
atomic.StoreInt32(&self.canStart, 0)
100-
if self.Mining() {
101-
self.Stop()
102-
atomic.StoreInt32(&self.shouldStart, 1)
99+
atomic.StoreInt32(&miner.canStart, 0)
100+
if miner.Mining() {
101+
miner.Stop()
102+
atomic.StoreInt32(&miner.shouldStart, 1)
103103
log.Info("Mining aborted due to sync")
104104
}
105105
case downloader.DoneEvent, downloader.FailedEvent:
106-
shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
106+
shouldStart := atomic.LoadInt32(&miner.shouldStart) == 1
107107

108-
atomic.StoreInt32(&self.canStart, 1)
109-
atomic.StoreInt32(&self.shouldStart, 0)
108+
atomic.StoreInt32(&miner.canStart, 1)
109+
atomic.StoreInt32(&miner.shouldStart, 0)
110110
if shouldStart {
111-
self.Start(self.coinbase)
111+
miner.Start(miner.coinbase)
112112
}
113113
// stop immediately and ignore all further pending events
114114
return
115115
}
116-
case <-self.exitCh:
116+
case <-miner.exitCh:
117117
return
118118
}
119119
}
120120
}
121121

122-
func (self *Miner) Start(coinbase common.Address) {
123-
atomic.StoreInt32(&self.shouldStart, 1)
124-
self.SetEtherbase(coinbase)
122+
func (miner *Miner) Start(coinbase common.Address) {
123+
atomic.StoreInt32(&miner.shouldStart, 1)
124+
miner.SetEtherbase(coinbase)
125125

126-
if atomic.LoadInt32(&self.canStart) == 0 {
126+
if atomic.LoadInt32(&miner.canStart) == 0 {
127127
log.Info("Network syncing, will start miner afterwards")
128128
return
129129
}
130-
self.worker.start()
130+
miner.worker.start()
131131
}
132132

133-
func (self *Miner) Stop() {
134-
self.worker.stop()
135-
atomic.StoreInt32(&self.shouldStart, 0)
133+
func (miner *Miner) Stop() {
134+
miner.worker.stop()
135+
atomic.StoreInt32(&miner.shouldStart, 0)
136136
}
137137

138-
func (self *Miner) Close() {
139-
self.worker.close()
140-
close(self.exitCh)
138+
func (miner *Miner) Close() {
139+
miner.worker.close()
140+
close(miner.exitCh)
141141
}
142142

143-
func (self *Miner) Mining() bool {
144-
return self.worker.isRunning()
143+
func (miner *Miner) Mining() bool {
144+
return miner.worker.isRunning()
145145
}
146146

147-
func (self *Miner) HashRate() uint64 {
148-
if pow, ok := self.engine.(consensus.PoW); ok {
147+
func (miner *Miner) HashRate() uint64 {
148+
if pow, ok := miner.engine.(consensus.PoW); ok {
149149
return uint64(pow.Hashrate())
150150
}
151151
return 0
152152
}
153153

154-
func (self *Miner) SetExtra(extra []byte) error {
154+
func (miner *Miner) SetExtra(extra []byte) error {
155155
if uint64(len(extra)) > params.MaximumExtraDataSize {
156-
return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
156+
return fmt.Errorf("extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
157157
}
158-
self.worker.setExtra(extra)
158+
miner.worker.setExtra(extra)
159159
return nil
160160
}
161161

162162
// SetRecommitInterval sets the interval for sealing work resubmitting.
163-
func (self *Miner) SetRecommitInterval(interval time.Duration) {
164-
self.worker.setRecommitInterval(interval)
163+
func (miner *Miner) SetRecommitInterval(interval time.Duration) {
164+
miner.worker.setRecommitInterval(interval)
165165
}
166166

167167
// Pending returns the currently pending block and associated state.
168-
func (self *Miner) Pending() (*types.Block, *state.StateDB) {
169-
return self.worker.pending()
168+
func (miner *Miner) Pending() (*types.Block, *state.StateDB) {
169+
return miner.worker.pending()
170170
}
171171

172172
// PendingBlock returns the currently pending block.
173173
//
174174
// Note, to access both the pending block and the pending state
175175
// simultaneously, please use Pending(), as the pending state can
176176
// change between multiple method calls
177-
func (self *Miner) PendingBlock() *types.Block {
178-
return self.worker.pendingBlock()
177+
func (miner *Miner) PendingBlock() *types.Block {
178+
return miner.worker.pendingBlock()
179179
}
180180

181-
func (self *Miner) SetEtherbase(addr common.Address) {
182-
self.coinbase = addr
183-
self.worker.setEtherbase(addr)
181+
func (miner *Miner) SetEtherbase(addr common.Address) {
182+
miner.coinbase = addr
183+
miner.worker.setEtherbase(addr)
184184
}

miner/worker_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package miner
1818

1919
import (
20+
"fmt"
2021
"math/big"
2122
"math/rand"
2223
"sync/atomic"
@@ -217,6 +218,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
217218
chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil)
218219
defer chain.Stop()
219220

221+
loopErr := make(chan error)
220222
newBlock := make(chan struct{})
221223
listenNewBlock := func() {
222224
sub := w.mux.Subscribe(core.NewMinedBlockEvent{})
@@ -226,7 +228,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
226228
block := item.Data.(core.NewMinedBlockEvent).Block
227229
_, err := chain.InsertChain([]*types.Block{block})
228230
if err != nil {
229-
t.Fatalf("Failed to insert new mined block:%d, error:%v", block.NumberU64(), err)
231+
loopErr <- fmt.Errorf("failed to insert new mined block:%d, error:%v", block.NumberU64(), err)
230232
}
231233
newBlock <- struct{}{}
232234
}
@@ -244,6 +246,8 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
244246
b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}})
245247
b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}})
246248
select {
249+
case e := <-loopErr:
250+
t.Fatal(e)
247251
case <-newBlock:
248252
case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes.
249253
t.Fatalf("timeout")

0 commit comments

Comments
 (0)