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

@ -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
// 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.
func (self *Miner) update() {
events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
func (miner *Miner) update() {
events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
defer events.Unsubscribe()
for {
@ -96,77 +96,77 @@ func (self *Miner) update() {
}
switch ev.Data.(type) {
case downloader.StartEvent:
atomic.StoreInt32(&self.canStart, 0)
if self.Mining() {
self.Stop()
atomic.StoreInt32(&self.shouldStart, 1)
atomic.StoreInt32(&miner.canStart, 0)
if miner.Mining() {
miner.Stop()
atomic.StoreInt32(&miner.shouldStart, 1)
log.Info("Mining aborted due to sync")
}
case downloader.DoneEvent, downloader.FailedEvent:
shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
shouldStart := atomic.LoadInt32(&miner.shouldStart) == 1
atomic.StoreInt32(&self.canStart, 1)
atomic.StoreInt32(&self.shouldStart, 0)
atomic.StoreInt32(&miner.canStart, 1)
atomic.StoreInt32(&miner.shouldStart, 0)
if shouldStart {
self.Start(self.coinbase)
miner.Start(miner.coinbase)
}
// stop immediately and ignore all further pending events
return
}
case <-self.exitCh:
case <-miner.exitCh:
return
}
}
}
func (self *Miner) Start(coinbase common.Address) {
atomic.StoreInt32(&self.shouldStart, 1)
self.SetEtherbase(coinbase)
func (miner *Miner) Start(coinbase common.Address) {
atomic.StoreInt32(&miner.shouldStart, 1)
miner.SetEtherbase(coinbase)
if atomic.LoadInt32(&self.canStart) == 0 {
if atomic.LoadInt32(&miner.canStart) == 0 {
log.Info("Network syncing, will start miner afterwards")
return
}
self.worker.start()
miner.worker.start()
}
func (self *Miner) Stop() {
self.worker.stop()
atomic.StoreInt32(&self.shouldStart, 0)
func (miner *Miner) Stop() {
miner.worker.stop()
atomic.StoreInt32(&miner.shouldStart, 0)
}
func (self *Miner) Close() {
self.worker.close()
close(self.exitCh)
func (miner *Miner) Close() {
miner.worker.close()
close(miner.exitCh)
}
func (self *Miner) Mining() bool {
return self.worker.isRunning()
func (miner *Miner) Mining() bool {
return miner.worker.isRunning()
}
func (self *Miner) HashRate() uint64 {
if pow, ok := self.engine.(consensus.PoW); ok {
func (miner *Miner) HashRate() uint64 {
if pow, ok := miner.engine.(consensus.PoW); ok {
return uint64(pow.Hashrate())
}
return 0
}
func (self *Miner) SetExtra(extra []byte) error {
func (miner *Miner) SetExtra(extra []byte) error {
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
}
// SetRecommitInterval sets the interval for sealing work resubmitting.
func (self *Miner) SetRecommitInterval(interval time.Duration) {
self.worker.setRecommitInterval(interval)
func (miner *Miner) SetRecommitInterval(interval time.Duration) {
miner.worker.setRecommitInterval(interval)
}
// Pending returns the currently pending block and associated state.
func (self *Miner) Pending() (*types.Block, *state.StateDB) {
return self.worker.pending()
func (miner *Miner) Pending() (*types.Block, *state.StateDB) {
return miner.worker.pending()
}
// 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
// simultaneously, please use Pending(), as the pending state can
// change between multiple method calls
func (self *Miner) PendingBlock() *types.Block {
return self.worker.pendingBlock()
func (miner *Miner) PendingBlock() *types.Block {
return miner.worker.pendingBlock()
}
func (self *Miner) SetEtherbase(addr common.Address) {
self.coinbase = addr
self.worker.setEtherbase(addr)
func (miner *Miner) SetEtherbase(addr common.Address) {
miner.coinbase = addr
miner.worker.setEtherbase(addr)
}

@ -17,6 +17,7 @@
package miner
import (
"fmt"
"math/big"
"math/rand"
"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)
defer chain.Stop()
loopErr := make(chan error)
newBlock := make(chan struct{})
listenNewBlock := func() {
sub := w.mux.Subscribe(core.NewMinedBlockEvent{})
@ -226,7 +228,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
block := item.Data.(core.NewMinedBlockEvent).Block
_, err := chain.InsertChain([]*types.Block{block})
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{}{}
}
@ -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()}})
select {
case e := <-loopErr:
t.Fatal(e)
case <-newBlock:
case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes.
t.Fatalf("timeout")