Skip to content

Commit

Permalink
Merge pull request #20 from junnmm/dev
Browse files Browse the repository at this point in the history
eth: close miner on exit (instead of just stopping) (ethereum#21992)
  • Loading branch information
viaweb3 committed Jun 28, 2022
2 parents 555b10c + 83d034b commit 260973e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion eth/backend.go
Expand Up @@ -600,7 +600,7 @@ func (s *Ethereum) Stop() error {
s.bloomIndexer.Close()
close(s.closeBloomHandler)
s.txPool.Stop()
s.miner.Stop()
s.miner.Close()
s.blockchain.Stop()
s.engine.Close()
rawdb.PopUncleanShutdownMarker(s.chainDb)
Expand Down
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 @@ -62,6 +63,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 @@ -74,8 +77,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 @@ -84,6 +87,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 @@ -153,6 +158,7 @@ func (miner *Miner) Stop() {

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

func (miner *Miner) Mining() bool {
Expand Down
8 changes: 8 additions & 0 deletions miner/worker.go
Expand Up @@ -150,6 +150,8 @@ type worker struct {
resubmitIntervalCh chan time.Duration
resubmitAdjustCh chan *intervalAdjust

wg sync.WaitGroup

current *environment // An environment for current running cycle.
localUncles map[common.Hash]*types.Block // A set of side blocks generated locally as the possible uncle blocks.
remoteUncles map[common.Hash]*types.Block // A set of side blocks as the possible uncle blocks.
Expand Down Expand Up @@ -224,6 +226,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
recommit = minRecommitInterval
}

worker.wg.Add(4)
go worker.mainLoop()
go worker.newWorkLoop(recommit)
go worker.resultLoop()
Expand Down Expand Up @@ -308,6 +311,7 @@ func (w *worker) close() {
}
atomic.StoreInt32(&w.running, 0)
close(w.exitCh)
w.wg.Wait()
}

// recalcRecommit recalculates the resubmitting interval upon feedback.
Expand All @@ -334,6 +338,7 @@ func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) t

// newWorkLoop is a standalone goroutine to submit new mining work upon received events.
func (w *worker) newWorkLoop(recommit time.Duration) {
defer w.wg.Done()
var (
interrupt *int32
minRecommit = recommit // minimal resubmit interval specified by user.
Expand Down Expand Up @@ -431,6 +436,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {

// mainLoop is a standalone goroutine to regenerate the sealing task based on the received event.
func (w *worker) mainLoop() {
defer w.wg.Done()
defer w.txsSub.Unsubscribe()
defer w.chainHeadSub.Unsubscribe()
defer w.chainSideSub.Unsubscribe()
Expand Down Expand Up @@ -533,6 +539,7 @@ func (w *worker) mainLoop() {
// taskLoop is a standalone goroutine to fetch sealing task from the generator and
// push them to consensus engine.
func (w *worker) taskLoop() {
defer w.wg.Done()
var (
stopCh chan struct{}
prev common.Hash
Expand Down Expand Up @@ -580,6 +587,7 @@ func (w *worker) taskLoop() {
// resultLoop is a standalone goroutine to handle sealing result submitting
// and flush relative data to the database.
func (w *worker) resultLoop() {
defer w.wg.Done()
for {
select {
case block := <-w.resultCh:
Expand Down

0 comments on commit 260973e

Please sign in to comment.