github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/consensus/ethash/sealer.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus 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/ebakus/go-ebakus/common"
    33  	"github.com/ebakus/go-ebakus/common/hexutil"
    34  	"github.com/ebakus/go-ebakus/consensus"
    35  	"github.com/ebakus/go-ebakus/core/types"
    36  	"github.com/ebakus/go-ebakus/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  		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  		// target  = new(big.Int).Div(two256, header.Difficulty)
   136  		number  = header.Number.Uint64()
   137  		dataset = ethash.dataset(number, false)
   138  	)
   139  	// Start generating random nonces until we abort or find a good one
   140  	var (
   141  		attempts = int64(0)
   142  		nonce    = seed
   143  	)
   144  	logger := log.New("miner", id)
   145  	logger.Trace("Started ethash search for new nonces", "seed", seed)
   146  search:
   147  	for {
   148  		select {
   149  		case <-abort:
   150  			// Mining terminated, update stats and abort
   151  			logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed)
   152  			ethash.hashrate.Mark(attempts)
   153  			break search
   154  
   155  		default:
   156  			// We don't have to update hash rate on every nonce, so update after after 2^X nonces
   157  			attempts++
   158  			if (attempts % (1 << 15)) == 0 {
   159  				ethash.hashrate.Mark(attempts)
   160  				attempts = 0
   161  			}
   162  
   163  			nonce++
   164  		}
   165  	}
   166  	// Datasets are unmapped in a finalizer. Ensure that the dataset stays live
   167  	// during sealing so it's not unmapped while being read.
   168  	runtime.KeepAlive(dataset)
   169  }
   170  
   171  // remote is a standalone goroutine to handle remote mining related stuff.
   172  func (ethash *Ethash) remote(notify []string, noverify bool) {
   173  	var (
   174  		works = make(map[common.Hash]*types.Block)
   175  		rates = make(map[common.Hash]hashrate)
   176  
   177  		results      chan<- *types.Block
   178  		currentBlock *types.Block
   179  		currentWork  [4]string
   180  
   181  		notifyTransport = &http.Transport{}
   182  		notifyClient    = &http.Client{
   183  			Transport: notifyTransport,
   184  			Timeout:   time.Second,
   185  		}
   186  		notifyReqs = make([]*http.Request, len(notify))
   187  	)
   188  	// notifyWork notifies all the specified mining endpoints of the availability of
   189  	// new work to be processed.
   190  	notifyWork := func() {
   191  		work := currentWork
   192  		blob, _ := json.Marshal(work)
   193  
   194  		for i, url := range notify {
   195  			// Terminate any previously pending request and create the new work
   196  			if notifyReqs[i] != nil {
   197  				notifyTransport.CancelRequest(notifyReqs[i])
   198  			}
   199  			notifyReqs[i], _ = http.NewRequest("POST", url, bytes.NewReader(blob))
   200  			notifyReqs[i].Header.Set("Content-Type", "application/json")
   201  
   202  			// Push the new work concurrently to all the remote nodes
   203  			go func(req *http.Request, url string) {
   204  				res, err := notifyClient.Do(req)
   205  				if err != nil {
   206  					log.Warn("Failed to notify remote miner", "err", err)
   207  				} else {
   208  					log.Trace("Notified remote miner", "miner", url, "hash", log.Lazy{Fn: func() common.Hash { return common.HexToHash(work[0]) }}, "target", work[2])
   209  					res.Body.Close()
   210  				}
   211  			}(notifyReqs[i], url)
   212  		}
   213  	}
   214  	// makeWork creates a work package for external miner.
   215  	//
   216  	// The work package consists of 3 strings:
   217  	//   result[0], 32 bytes hex encoded current block header pow-hash
   218  	//   result[1], 32 bytes hex encoded seed hash used for DAG
   219  	//   result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
   220  	//   result[3], hex encoded block number
   221  	makeWork := func(block *types.Block) {
   222  		hash := ethash.SealHash(block.Header())
   223  
   224  		currentWork[0] = hash.Hex()
   225  		currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
   226  		// currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
   227  		currentWork[3] = hexutil.EncodeBig(block.Number())
   228  
   229  		// Trace the seal work fetched by remote sealer.
   230  		currentBlock = block
   231  		works[hash] = block
   232  	}
   233  	// submitWork verifies the submitted pow solution, returning
   234  	// whether the solution was accepted or not (not can be both a bad pow as well as
   235  	// any other error, like no pending work or stale mining result).
   236  	submitWork := func(mixDigest common.Hash, sealhash common.Hash) bool {
   237  		if currentBlock == nil {
   238  			log.Error("Pending work without block", "sealhash", sealhash)
   239  			return false
   240  		}
   241  		// Make sure the work submitted is present
   242  		block := works[sealhash]
   243  		if block == nil {
   244  			log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", currentBlock.NumberU64())
   245  			return false
   246  		}
   247  		// Verify the correctness of submitted result.
   248  		header := block.Header()
   249  
   250  		start := time.Now()
   251  		if !noverify {
   252  			if err := ethash.verifySeal(nil, header, true); err != nil {
   253  				log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
   254  				return false
   255  			}
   256  		}
   257  		// Make sure the result channel is assigned.
   258  		if results == nil {
   259  			log.Warn("Ethash result channel is empty, submitted mining result is rejected")
   260  			return false
   261  		}
   262  		log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)))
   263  
   264  		// Solutions seems to be valid, return to the miner and notify acceptance.
   265  		solution := block.WithSeal(header)
   266  
   267  		// The submitted solution is within the scope of acceptance.
   268  		if solution.NumberU64()+staleThreshold > currentBlock.NumberU64() {
   269  			select {
   270  			case results <- solution:
   271  				log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   272  				return true
   273  			default:
   274  				log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash)
   275  				return false
   276  			}
   277  		}
   278  		// The submitted block is too old to accept, drop it.
   279  		log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   280  		return false
   281  	}
   282  
   283  	ticker := time.NewTicker(5 * time.Second)
   284  	defer ticker.Stop()
   285  
   286  	for {
   287  		select {
   288  		case work := <-ethash.workCh:
   289  			// Update current work with new received block.
   290  			// Note same work can be past twice, happens when changing CPU threads.
   291  			results = work.results
   292  
   293  			makeWork(work.block)
   294  
   295  			// Notify and requested URLs of the new work availability
   296  			notifyWork()
   297  
   298  		case work := <-ethash.fetchWorkCh:
   299  			// Return current mining work to remote miner.
   300  			if currentBlock == nil {
   301  				work.errc <- errNoMiningWork
   302  			} else {
   303  				work.res <- currentWork
   304  			}
   305  
   306  		case result := <-ethash.submitWorkCh:
   307  			// Verify submitted PoW solution based on maintained mining blocks.
   308  			if submitWork(result.mixDigest, result.hash) {
   309  				result.errc <- nil
   310  			} else {
   311  				result.errc <- errInvalidSealResult
   312  			}
   313  
   314  		case result := <-ethash.submitRateCh:
   315  			// Trace remote sealer's hash rate by submitted value.
   316  			rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
   317  			close(result.done)
   318  
   319  		case req := <-ethash.fetchRateCh:
   320  			// Gather all hash rate submitted by remote sealer.
   321  			var total uint64
   322  			for _, rate := range rates {
   323  				// this could overflow
   324  				total += rate.rate
   325  			}
   326  			req <- total
   327  
   328  		case <-ticker.C:
   329  			// Clear stale submitted hash rate.
   330  			for id, rate := range rates {
   331  				if time.Since(rate.ping) > 10*time.Second {
   332  					delete(rates, id)
   333  				}
   334  			}
   335  			// Clear stale pending blocks
   336  			if currentBlock != nil {
   337  				for hash, block := range works {
   338  					if block.NumberU64()+staleThreshold <= currentBlock.NumberU64() {
   339  						delete(works, hash)
   340  					}
   341  				}
   342  			}
   343  
   344  		case errc := <-ethash.exitCh:
   345  			// Exit remote loop if ethash is closed and return relevant error.
   346  			errc <- nil
   347  			log.Trace("Ethash remote sealer is exiting")
   348  			return
   349  		}
   350  	}
   351  }