Skip to content

Commit

Permalink
miner: add WaitGroup in Miner
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed May 25, 2021
1 parent c75d924 commit 22bcb4b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion miner/miner.go
Expand Up @@ -20,6 +20,7 @@ package miner
import (
"fmt"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -63,6 +64,8 @@ type Miner struct {
exitCh chan struct{}
startCh chan common.Address
stopCh chan struct{}

wg sync.WaitGroup
}

func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool) *Miner {
Expand All @@ -75,8 +78,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
stopCh: make(chan struct{}),
worker: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock, true),
}
miner.wg.Add(1)
go miner.update()

return miner
}

Expand All @@ -85,6 +88,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
// 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 (miner *Miner) update() {
defer miner.wg.Done()

events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
defer func() {
if !events.Closed() {
Expand Down Expand Up @@ -154,6 +159,7 @@ func (miner *Miner) Stop() {

func (miner *Miner) Close() {
close(miner.exitCh)
miner.wg.Wait()
}

func (miner *Miner) Mining() bool {
Expand Down

0 comments on commit 22bcb4b

Please sign in to comment.