From 2a08b49f4984184e5bc5912908df1bbd57029d3c Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Fri, 3 Aug 2018 15:44:37 +0800 Subject: [PATCH] consensus: polish a bit --- consensus/clique/clique.go | 2 +- consensus/consensus.go | 2 +- consensus/ethash/api.go | 32 +++++++++++++++----------------- consensus/ethash/ethash.go | 21 ++++++++++----------- consensus/ethash/sealer.go | 18 ++++++++---------- 5 files changed, 35 insertions(+), 40 deletions(-) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 5963900c97..59bb3d40b4 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -672,7 +672,7 @@ func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int { return new(big.Int).Set(diffNoTurn) } -// Close implements consensus.Engine, returning internal error and close the clique. +// Close implements consensus.Engine. It's a noop for clique as there is are no background threads. func (c *Clique) Close() error { return nil } diff --git a/consensus/consensus.go b/consensus/consensus.go index ae0fefb490..8271754445 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -97,7 +97,7 @@ type Engine interface { // APIs returns the RPC APIs this consensus engine provides. APIs(chain ChainReader) []rpc.API - // Close closes the consensus engine. + // Close terminates any background threads maintained by the consensus engine. Close() error } diff --git a/consensus/ethash/api.go b/consensus/ethash/api.go index b950fbd92c..a04ea235d9 100644 --- a/consensus/ethash/api.go +++ b/consensus/ethash/api.go @@ -24,10 +24,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" ) -var ( - errEthashStopped = errors.New("ethash stopped") - errAPINotSupported = errors.New("the current ethash running mode does not support this API") -) +var errEthashStopped = errors.New("ethash stopped") // API exposes ethash related methods for the RPC interface. type API struct { @@ -42,25 +39,26 @@ type API struct { // result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty func (api *API) GetWork() ([3]string, error) { if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest { - return [3]string{}, errAPINotSupported + return [3]string{}, errors.New("not supported") } var ( workCh = make(chan [3]string, 1) - errCh = make(chan error, 1) - err error + errc = make(chan error, 1) ) select { - case api.ethash.fetchWorkCh <- &sealWork{errCh: errCh, resCh: workCh}: + case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}: case <-api.ethash.exitCh: return [3]string{}, errEthashStopped } - if err = <-errCh; err == nil { - return <-workCh, nil + select { + case work := <-workCh: + return work, nil + case err := <-errc: + return [3]string{}, err } - return [3]string{}, err } // SubmitWork can be used by external miner to submit their POW solution. @@ -71,20 +69,20 @@ func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) boo return false } - var errCh = make(chan error, 1) + var errc = make(chan error, 1) select { case api.ethash.submitWorkCh <- &mineResult{ nonce: nonce, mixDigest: digest, hash: hash, - errCh: errCh, + errc: errc, }: case <-api.ethash.exitCh: return false } - err := <-errCh + err := <-errc return err == nil } @@ -99,16 +97,16 @@ func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool { return false } - var doneCh = make(chan struct{}, 1) + var done = make(chan struct{}, 1) select { - case api.ethash.submitRateCh <- &hashrate{done: doneCh, rate: uint64(rate), id: id}: + case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}: case <-api.ethash.exitCh: return false } // Block until hash rate submitted successfully. - <-doneCh + <-done return true } diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go index 3fff266410..0cb3059b9d 100644 --- a/consensus/ethash/ethash.go +++ b/consensus/ethash/ethash.go @@ -397,7 +397,7 @@ type mineResult struct { mixDigest common.Hash hash common.Hash - errCh chan error + errc chan error } // hashrate wraps the hash rate submitted by the remote sealer. @@ -411,8 +411,8 @@ type hashrate struct { // sealWork wraps a seal work package for remote sealer. type sealWork struct { - errCh chan error - resCh chan [3]string + errc chan error + res chan [3]string } // Ethash is a consensus engine based on proof-of-work implementing the ethash @@ -447,7 +447,7 @@ type Ethash struct { exitCh chan chan error // Notification channel to exiting backend threads } -// New creates a full sized ethash PoW scheme. +// New creates a full sized ethash PoW scheme and starts a background thread for remote mining. func New(config Config) *Ethash { if config.CachesInMem <= 0 { log.Warn("One ethash cache must always be in memory", "requested", config.CachesInMem) @@ -557,9 +557,9 @@ func (ethash *Ethash) Close() error { if ethash.exitCh == nil { return } - errCh := make(chan error) - ethash.exitCh <- errCh - err = <-errCh + errc := make(chan error) + ethash.exitCh <- errc + err = <-errc close(ethash.exitCh) }) return err @@ -644,18 +644,17 @@ func (ethash *Ethash) Hashrate() float64 { if ethash.config.PowMode != ModeNormal && ethash.config.PowMode != ModeTest { return ethash.hashrate.Rate1() } - var resCh = make(chan uint64, 1) + var res = make(chan uint64, 1) select { - case ethash.fetchRateCh <- resCh: + case ethash.fetchRateCh <- res: case <-ethash.exitCh: // Return local hashrate only if ethash is stopped. return ethash.hashrate.Rate1() } // Gather total submitted hash rate of remote sealers. - total := <-resCh - return ethash.hashrate.Rate1() + float64(total) + return ethash.hashrate.Rate1() + float64(<-res) } // APIs implements consensus.Engine, returning the user facing RPC APIs. diff --git a/consensus/ethash/sealer.go b/consensus/ethash/sealer.go index 539e783a6c..a9449d4060 100644 --- a/consensus/ethash/sealer.go +++ b/consensus/ethash/sealer.go @@ -235,7 +235,6 @@ func (ethash *Ethash) remote() { ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() -running: for { select { case block := <-ethash.workCh: @@ -251,18 +250,17 @@ running: // Return current mining work to remote miner. miningWork, err := getWork() if err != nil { - work.errCh <- err + work.errc <- err } else { - close(work.errCh) - work.resCh <- miningWork + work.res <- miningWork } case result := <-ethash.submitWorkCh: // Verify submitted PoW solution based on maintained mining blocks. if submitWork(result.nonce, result.mixDigest, result.hash) { - close(result.errCh) + result.errc <- nil } else { - result.errCh <- errInvalidSealResult + result.errc <- errInvalidSealResult } case result := <-ethash.submitRateCh: @@ -287,11 +285,11 @@ running: } } - case errCh := <-ethash.exitCh: + case errc := <-ethash.exitCh: // Exit remote loop if ethash is closed and return relevant error. - errCh <- nil - break running + errc <- nil + log.Trace("Ethash remote sealer is exiting") + return } } - log.Trace("Ethash remote sealer is exiting") }