miner: fix staticcheck warnings (#20375)

This commit is contained in:
Guillaume Ballet 2019-11-24 20:46:34 +01:00 committed by Felix Lange
parent 3a0480e07d
commit f06ae5ca6a
2 changed files with 43 additions and 39 deletions

View File

@ -84,8 +84,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
// It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and // It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks // the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
// and halt your mining operation for as long as the DOS continues. // and halt your mining operation for as long as the DOS continues.
func (self *Miner) update() { func (miner *Miner) update() {
events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{}) events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
defer events.Unsubscribe() defer events.Unsubscribe()
for { for {
@ -96,77 +96,77 @@ func (self *Miner) update() {
} }
switch ev.Data.(type) { switch ev.Data.(type) {
case downloader.StartEvent: case downloader.StartEvent:
atomic.StoreInt32(&self.canStart, 0) atomic.StoreInt32(&miner.canStart, 0)
if self.Mining() { if miner.Mining() {
self.Stop() miner.Stop()
atomic.StoreInt32(&self.shouldStart, 1) atomic.StoreInt32(&miner.shouldStart, 1)
log.Info("Mining aborted due to sync") log.Info("Mining aborted due to sync")
} }
case downloader.DoneEvent, downloader.FailedEvent: case downloader.DoneEvent, downloader.FailedEvent:
shouldStart := atomic.LoadInt32(&self.shouldStart) == 1 shouldStart := atomic.LoadInt32(&miner.shouldStart) == 1
atomic.StoreInt32(&self.canStart, 1) atomic.StoreInt32(&miner.canStart, 1)
atomic.StoreInt32(&self.shouldStart, 0) atomic.StoreInt32(&miner.shouldStart, 0)
if shouldStart { if shouldStart {
self.Start(self.coinbase) miner.Start(miner.coinbase)
} }
// stop immediately and ignore all further pending events // stop immediately and ignore all further pending events
return return
} }
case <-self.exitCh: case <-miner.exitCh:
return return
} }
} }
} }
func (self *Miner) Start(coinbase common.Address) { func (miner *Miner) Start(coinbase common.Address) {
atomic.StoreInt32(&self.shouldStart, 1) atomic.StoreInt32(&miner.shouldStart, 1)
self.SetEtherbase(coinbase) miner.SetEtherbase(coinbase)
if atomic.LoadInt32(&self.canStart) == 0 { if atomic.LoadInt32(&miner.canStart) == 0 {
log.Info("Network syncing, will start miner afterwards") log.Info("Network syncing, will start miner afterwards")
return return
} }
self.worker.start() miner.worker.start()
} }
func (self *Miner) Stop() { func (miner *Miner) Stop() {
self.worker.stop() miner.worker.stop()
atomic.StoreInt32(&self.shouldStart, 0) atomic.StoreInt32(&miner.shouldStart, 0)
} }
func (self *Miner) Close() { func (miner *Miner) Close() {
self.worker.close() miner.worker.close()
close(self.exitCh) close(miner.exitCh)
} }
func (self *Miner) Mining() bool { func (miner *Miner) Mining() bool {
return self.worker.isRunning() return miner.worker.isRunning()
} }
func (self *Miner) HashRate() uint64 { func (miner *Miner) HashRate() uint64 {
if pow, ok := self.engine.(consensus.PoW); ok { if pow, ok := miner.engine.(consensus.PoW); ok {
return uint64(pow.Hashrate()) return uint64(pow.Hashrate())
} }
return 0 return 0
} }
func (self *Miner) SetExtra(extra []byte) error { func (miner *Miner) SetExtra(extra []byte) error {
if uint64(len(extra)) > params.MaximumExtraDataSize { if uint64(len(extra)) > params.MaximumExtraDataSize {
return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize) return fmt.Errorf("extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
} }
self.worker.setExtra(extra) miner.worker.setExtra(extra)
return nil return nil
} }
// SetRecommitInterval sets the interval for sealing work resubmitting. // SetRecommitInterval sets the interval for sealing work resubmitting.
func (self *Miner) SetRecommitInterval(interval time.Duration) { func (miner *Miner) SetRecommitInterval(interval time.Duration) {
self.worker.setRecommitInterval(interval) miner.worker.setRecommitInterval(interval)
} }
// Pending returns the currently pending block and associated state. // Pending returns the currently pending block and associated state.
func (self *Miner) Pending() (*types.Block, *state.StateDB) { func (miner *Miner) Pending() (*types.Block, *state.StateDB) {
return self.worker.pending() return miner.worker.pending()
} }
// PendingBlock returns the currently pending block. // PendingBlock returns the currently pending block.
@ -174,11 +174,11 @@ func (self *Miner) Pending() (*types.Block, *state.StateDB) {
// Note, to access both the pending block and the pending state // Note, to access both the pending block and the pending state
// simultaneously, please use Pending(), as the pending state can // simultaneously, please use Pending(), as the pending state can
// change between multiple method calls // change between multiple method calls
func (self *Miner) PendingBlock() *types.Block { func (miner *Miner) PendingBlock() *types.Block {
return self.worker.pendingBlock() return miner.worker.pendingBlock()
} }
func (self *Miner) SetEtherbase(addr common.Address) { func (miner *Miner) SetEtherbase(addr common.Address) {
self.coinbase = addr miner.coinbase = addr
self.worker.setEtherbase(addr) miner.worker.setEtherbase(addr)
} }

View File

@ -17,6 +17,7 @@
package miner package miner
import ( import (
"fmt"
"math/big" "math/big"
"math/rand" "math/rand"
"sync/atomic" "sync/atomic"
@ -217,6 +218,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil) chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil)
defer chain.Stop() defer chain.Stop()
loopErr := make(chan error)
newBlock := make(chan struct{}) newBlock := make(chan struct{})
listenNewBlock := func() { listenNewBlock := func() {
sub := w.mux.Subscribe(core.NewMinedBlockEvent{}) sub := w.mux.Subscribe(core.NewMinedBlockEvent{})
@ -226,7 +228,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
block := item.Data.(core.NewMinedBlockEvent).Block block := item.Data.(core.NewMinedBlockEvent).Block
_, err := chain.InsertChain([]*types.Block{block}) _, err := chain.InsertChain([]*types.Block{block})
if err != nil { if err != nil {
t.Fatalf("Failed to insert new mined block:%d, error:%v", block.NumberU64(), err) loopErr <- fmt.Errorf("failed to insert new mined block:%d, error:%v", block.NumberU64(), err)
} }
newBlock <- struct{}{} newBlock <- struct{}{}
} }
@ -244,6 +246,8 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}}) b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}})
b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}}) b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}})
select { select {
case e := <-loopErr:
t.Fatal(e)
case <-newBlock: case <-newBlock:
case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes. case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes.
t.Fatalf("timeout") t.Fatalf("timeout")