Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miner: fix staticcheck warnings #20375

Merged
merged 1 commit into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -96,89 +96,89 @@ 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.
//
// 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)
}
6 changes: 5 additions & 1 deletion miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package miner

import (
"fmt"
"math/big"
"math/rand"
"sync/atomic"
Expand Down Expand Up @@ -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{})
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use t.Fatalf here? Is it against the static check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it advises against passing a testing.T pointer to a different goroutine

}
newBlock <- struct{}{}
}
Expand All @@ -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")
Expand Down