git.luolix.top/ethereum/go-ethereum@v1.9.7/consensus/ethash/sealer.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethash
    18  
    19  import (
    20  	"bytes"
    21  	crand "crypto/rand"
    22  	"encoding/json"
    23  	"errors"
    24  	"math"
    25  	"math/big"
    26  	"math/rand"
    27  	"net/http"
    28  	"runtime"
    29  	"sync"
    30  	"time"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/common/hexutil"
    34  	"github.com/ethereum/go-ethereum/consensus"
    35  	"github.com/ethereum/go-ethereum/core/types"
    36  	"github.com/ethereum/go-ethereum/log"
    37  )
    38  
    39  const (
    40  	// staleThreshold is the maximum depth of the acceptable stale but valid ethash solution.
    41  	staleThreshold = 7
    42  )
    43  
    44  var (
    45  	errNoMiningWork      = errors.New("no mining work available yet")
    46  	errInvalidSealResult = errors.New("invalid or stale proof-of-work solution")
    47  )
    48  
    49  // Seal implements consensus.Engine, attempting to find a nonce that satisfies
    50  // the block's difficulty requirements.
    51  func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
    52  	// If we're running a fake PoW, simply return a 0 nonce immediately
    53  	if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
    54  		header := block.Header()
    55  		header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
    56  		select {
    57  		case results <- block.WithSeal(header):
    58  		default:
    59  			log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", ethash.SealHash(block.Header()))
    60  		}
    61  		return nil
    62  	}
    63  	// If we're running a shared PoW, delegate sealing to it
    64  	if ethash.shared != nil {
    65  		return ethash.shared.Seal(chain, block, results, stop)
    66  	}
    67  	// Create a runner and the multiple search threads it directs
    68  	abort := make(chan struct{})
    69  
    70  	ethash.lock.Lock()
    71  	threads := ethash.threads
    72  	if ethash.rand == nil {
    73  		seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
    74  		if err != nil {
    75  			ethash.lock.Unlock()
    76  			return err
    77  		}
    78  		ethash.rand = rand.New(rand.NewSource(seed.Int64()))
    79  	}
    80  	ethash.lock.Unlock()
    81  	if threads == 0 {
    82  		threads = runtime.NumCPU()
    83  	}
    84  	if threads < 0 {
    85  		threads = 0 // Allows disabling local mining without extra logic around local/remote
    86  	}
    87  	// Push new work to remote sealer
    88  	if ethash.workCh != nil {
    89  		ethash.workCh <- &sealTask{block: block, results: results}
    90  	}
    91  	var (
    92  		pend   sync.WaitGroup
    93  		locals = make(chan *types.Block)
    94  	)
    95  	for i := 0; i < threads; i++ {
    96  		pend.Add(1)
    97  		go func(id int, nonce uint64) {
    98  			defer pend.Done()
    99  			ethash.mine(block, id, nonce, abort, locals)
   100  		}(i, uint64(ethash.rand.Int63()))
   101  	}
   102  	// Wait until sealing is terminated or a nonce is found
   103  	go func() {
   104  		var result *types.Block
   105  		select {
   106  		case <-stop:
   107  			// Outside abort, stop all miner threads
   108  			close(abort)
   109  		case result = <-locals:
   110  			// One of the threads found a block, abort all others
   111  			select {
   112  			case results <- result:
   113  			default:
   114  				log.Warn("Sealing result is not read by miner", "mode", "local", "sealhash", ethash.SealHash(block.Header()))
   115  			}
   116  			close(abort)
   117  		case <-ethash.update:
   118  			// Thread count was changed on user request, restart
   119  			close(abort)
   120  			if err := ethash.Seal(chain, block, results, stop); err != nil {
   121  				log.Error("Failed to restart sealing after update", "err", err)
   122  			}
   123  		}
   124  		// Wait for all miners to terminate and return the block
   125  		pend.Wait()
   126  	}()
   127  	return nil
   128  }
   129  
   130  // mine is the actual proof-of-work miner that searches for a nonce starting from
   131  // seed that results in correct final block difficulty.
   132  func (ethash *Ethash) mine(block *types.Block, id int, seed uint64, abort chan struct{}, found chan *types.Block) {
   133  	// Extract some data from the header
   134  	var (
   135  		header  = block.Header()
   136  		hash    = ethash.SealHash(header).Bytes()
   137  		target  = new(big.Int).Div(two256, header.Difficulty)
   138  		number  = header.Number.Uint64()
   139  		dataset = ethash.dataset(number, false)
   140  	)
   141  	// Start generating random nonces until we abort or find a good one
   142  	var (
   143  		attempts = int64(0)
   144  		nonce    = seed
   145  	)
   146  	logger := log.New("miner", id)
   147  	logger.Trace("Started ethash search for new nonces", "seed", seed)
   148  search:
   149  	for {
   150  		select {
   151  		case <-abort:
   152  			// Mining terminated, update stats and abort
   153  			logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed)
   154  			ethash.hashrate.Mark(attempts)
   155  			break search
   156  
   157  		default:
   158  			// We don't have to update hash rate on every nonce, so update after after 2^X nonces
   159  			attempts++
   160  			if (attempts % (1 << 15)) == 0 {
   161  				ethash.hashrate.Mark(attempts)
   162  				attempts = 0
   163  			}
   164  			// Compute the PoW value of this nonce
   165  			digest, result := hashimotoFull(dataset.dataset, hash, nonce)
   166  			if new(big.Int).SetBytes(result).Cmp(target) <= 0 {
   167  				// Correct nonce found, create a new header with it
   168  				header = types.CopyHeader(header)
   169  				header.Nonce = types.EncodeNonce(nonce)
   170  				header.MixDigest = common.BytesToHash(digest)
   171  
   172  				// Seal and return a block (if still needed)
   173  				select {
   174  				case found <- block.WithSeal(header):
   175  					logger.Trace("Ethash nonce found and reported", "attempts", nonce-seed, "nonce", nonce)
   176  				case <-abort:
   177  					logger.Trace("Ethash nonce found but discarded", "attempts", nonce-seed, "nonce", nonce)
   178  				}
   179  				break search
   180  			}
   181  			nonce++
   182  		}
   183  	}
   184  	// Datasets are unmapped in a finalizer. Ensure that the dataset stays live
   185  	// during sealing so it's not unmapped while being read.
   186  	runtime.KeepAlive(dataset)
   187  }
   188  
   189  // remote is a standalone goroutine to handle remote mining related stuff.
   190  func (ethash *Ethash) remote(notify []string, noverify bool) {
   191  	var (
   192  		works = make(map[common.Hash]*types.Block)
   193  		rates = make(map[common.Hash]hashrate)
   194  
   195  		results      chan<- *types.Block
   196  		currentBlock *types.Block
   197  		currentWork  [4]string
   198  
   199  		notifyTransport = &http.Transport{}
   200  		notifyClient    = &http.Client{
   201  			Transport: notifyTransport,
   202  			Timeout:   time.Second,
   203  		}
   204  		notifyReqs = make([]*http.Request, len(notify))
   205  	)
   206  	// notifyWork notifies all the specified mining endpoints of the availability of
   207  	// new work to be processed.
   208  	notifyWork := func() {
   209  		work := currentWork
   210  		blob, _ := json.Marshal(work)
   211  
   212  		for i, url := range notify {
   213  			// Terminate any previously pending request and create the new work
   214  			if notifyReqs[i] != nil {
   215  				notifyTransport.CancelRequest(notifyReqs[i])
   216  			}
   217  			notifyReqs[i], _ = http.NewRequest("POST", url, bytes.NewReader(blob))
   218  			notifyReqs[i].Header.Set("Content-Type", "application/json")
   219  
   220  			// Push the new work concurrently to all the remote nodes
   221  			go func(req *http.Request, url string) {
   222  				res, err := notifyClient.Do(req)
   223  				if err != nil {
   224  					log.Warn("Failed to notify remote miner", "err", err)
   225  				} else {
   226  					log.Trace("Notified remote miner", "miner", url, "hash", log.Lazy{Fn: func() common.Hash { return common.HexToHash(work[0]) }}, "target", work[2])
   227  					res.Body.Close()
   228  				}
   229  			}(notifyReqs[i], url)
   230  		}
   231  	}
   232  	// makeWork creates a work package for external miner.
   233  	//
   234  	// The work package consists of 3 strings:
   235  	//   result[0], 32 bytes hex encoded current block header pow-hash
   236  	//   result[1], 32 bytes hex encoded seed hash used for DAG
   237  	//   result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
   238  	//   result[3], hex encoded block number
   239  	makeWork := func(block *types.Block) {
   240  		hash := ethash.SealHash(block.Header())
   241  
   242  		currentWork[0] = hash.Hex()
   243  		currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
   244  		currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
   245  		currentWork[3] = hexutil.EncodeBig(block.Number())
   246  
   247  		// Trace the seal work fetched by remote sealer.
   248  		currentBlock = block
   249  		works[hash] = block
   250  	}
   251  	// submitWork verifies the submitted pow solution, returning
   252  	// whether the solution was accepted or not (not can be both a bad pow as well as
   253  	// any other error, like no pending work or stale mining result).
   254  	submitWork := func(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool {
   255  		if currentBlock == nil {
   256  			log.Error("Pending work without block", "sealhash", sealhash)
   257  			return false
   258  		}
   259  		// Make sure the work submitted is present
   260  		block := works[sealhash]
   261  		if block == nil {
   262  			log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", currentBlock.NumberU64())
   263  			return false
   264  		}
   265  		// Verify the correctness of submitted result.
   266  		header := block.Header()
   267  		header.Nonce = nonce
   268  		header.MixDigest = mixDigest
   269  
   270  		start := time.Now()
   271  		if !noverify {
   272  			if err := ethash.verifySeal(nil, header, true); err != nil {
   273  				log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
   274  				return false
   275  			}
   276  		}
   277  		// Make sure the result channel is assigned.
   278  		if results == nil {
   279  			log.Warn("Ethash result channel is empty, submitted mining result is rejected")
   280  			return false
   281  		}
   282  		log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)))
   283  
   284  		// Solutions seems to be valid, return to the miner and notify acceptance.
   285  		solution := block.WithSeal(header)
   286  
   287  		// The submitted solution is within the scope of acceptance.
   288  		if solution.NumberU64()+staleThreshold > currentBlock.NumberU64() {
   289  			select {
   290  			case results <- solution:
   291  				log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   292  				return true
   293  			default:
   294  				log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash)
   295  				return false
   296  			}
   297  		}
   298  		// The submitted block is too old to accept, drop it.
   299  		log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   300  		return false
   301  	}
   302  
   303  	ticker := time.NewTicker(5 * time.Second)
   304  	defer ticker.Stop()
   305  
   306  	for {
   307  		select {
   308  		case work := <-ethash.workCh:
   309  			// Update current work with new received block.
   310  			// Note same work can be past twice, happens when changing CPU threads.
   311  			results = work.results
   312  
   313  			makeWork(work.block)
   314  
   315  			// Notify and requested URLs of the new work availability
   316  			notifyWork()
   317  
   318  		case work := <-ethash.fetchWorkCh:
   319  			// Return current mining work to remote miner.
   320  			if currentBlock == nil {
   321  				work.errc <- errNoMiningWork
   322  			} else {
   323  				work.res <- currentWork
   324  			}
   325  
   326  		case result := <-ethash.submitWorkCh:
   327  			// Verify submitted PoW solution based on maintained mining blocks.
   328  			if submitWork(result.nonce, result.mixDigest, result.hash) {
   329  				result.errc <- nil
   330  			} else {
   331  				result.errc <- errInvalidSealResult
   332  			}
   333  
   334  		case result := <-ethash.submitRateCh:
   335  			// Trace remote sealer's hash rate by submitted value.
   336  			rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
   337  			close(result.done)
   338  
   339  		case req := <-ethash.fetchRateCh:
   340  			// Gather all hash rate submitted by remote sealer.
   341  			var total uint64
   342  			for _, rate := range rates {
   343  				// this could overflow
   344  				total += rate.rate
   345  			}
   346  			req <- total
   347  
   348  		case <-ticker.C:
   349  			// Clear stale submitted hash rate.
   350  			for id, rate := range rates {
   351  				if time.Since(rate.ping) > 10*time.Second {
   352  					delete(rates, id)
   353  				}
   354  			}
   355  			// Clear stale pending blocks
   356  			if currentBlock != nil {
   357  				for hash, block := range works {
   358  					if block.NumberU64()+staleThreshold <= currentBlock.NumberU64() {
   359  						delete(works, hash)
   360  					}
   361  				}
   362  			}
   363  
   364  		case errc := <-ethash.exitCh:
   365  			// Exit remote loop if ethash is closed and return relevant error.
   366  			errc <- nil
   367  			log.Trace("Ethash remote sealer is exiting")
   368  			return
   369  		}
   370  	}
   371  }