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