github.com/energicryptocurrency/go-energi@v1.1.7/consensus/ethash/sealer.go (about)

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