github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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  	"context"
    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/kisexp/xdchain/common"
    34  	"github.com/kisexp/xdchain/common/hexutil"
    35  	"github.com/kisexp/xdchain/consensus"
    36  	"github.com/kisexp/xdchain/core/types"
    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.ChainHeaderReader, 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  	// 如果我们正在运行一个伪PoW,只需立即返回一个0
    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 <- block.WithSeal(header):
    59  		default:
    60  			ethash.config.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.remote != nil {
    90  		ethash.remote.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 <- result:
   114  			default:
   115  				ethash.config.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  				ethash.config.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 := ethash.config.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  // This is the timeout for HTTP requests to notify external miners.
   191  const remoteSealerTimeout = 1 * time.Second
   192  
   193  type remoteSealer struct {
   194  	works        map[common.Hash]*types.Block
   195  	rates        map[common.Hash]hashrate
   196  	currentBlock *types.Block
   197  	currentWork  [4]string
   198  	notifyCtx    context.Context
   199  	cancelNotify context.CancelFunc // cancels all notification requests
   200  	reqWG        sync.WaitGroup     // tracks notification request goroutines
   201  
   202  	ethash       *Ethash
   203  	noverify     bool
   204  	notifyURLs   []string
   205  	results      chan<- *types.Block
   206  	workCh       chan *sealTask   // Notification channel to push new work and relative result channel to remote sealer
   207  	fetchWorkCh  chan *sealWork   // Channel used for remote sealer to fetch mining work
   208  	submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result
   209  	fetchRateCh  chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer.
   210  	submitRateCh chan *hashrate   // Channel used for remote sealer to submit their mining hashrate
   211  	requestExit  chan struct{}
   212  	exitCh       chan struct{}
   213  }
   214  
   215  // sealTask wraps a seal block with relative result channel for remote sealer thread.
   216  type sealTask struct {
   217  	block   *types.Block
   218  	results chan<- *types.Block
   219  }
   220  
   221  // mineResult wraps the pow solution parameters for the specified block.
   222  type mineResult struct {
   223  	nonce     types.BlockNonce
   224  	mixDigest common.Hash
   225  	hash      common.Hash
   226  
   227  	errc chan error
   228  }
   229  
   230  // hashrate wraps the hash rate submitted by the remote sealer.
   231  type hashrate struct {
   232  	id   common.Hash
   233  	ping time.Time
   234  	rate uint64
   235  
   236  	done chan struct{}
   237  }
   238  
   239  // sealWork wraps a seal work package for remote sealer.
   240  type sealWork struct {
   241  	errc chan error
   242  	res  chan [4]string
   243  }
   244  
   245  func startRemoteSealer(ethash *Ethash, urls []string, noverify bool) *remoteSealer {
   246  	ctx, cancel := context.WithCancel(context.Background())
   247  	s := &remoteSealer{
   248  		ethash:       ethash,
   249  		noverify:     noverify,
   250  		notifyURLs:   urls,
   251  		notifyCtx:    ctx,
   252  		cancelNotify: cancel,
   253  		works:        make(map[common.Hash]*types.Block),
   254  		rates:        make(map[common.Hash]hashrate),
   255  		workCh:       make(chan *sealTask),
   256  		fetchWorkCh:  make(chan *sealWork),
   257  		submitWorkCh: make(chan *mineResult),
   258  		fetchRateCh:  make(chan chan uint64),
   259  		submitRateCh: make(chan *hashrate),
   260  		requestExit:  make(chan struct{}),
   261  		exitCh:       make(chan struct{}),
   262  	}
   263  	go s.loop()
   264  	return s
   265  }
   266  
   267  func (s *remoteSealer) loop() {
   268  	defer func() {
   269  		s.ethash.config.Log.Trace("Ethash remote sealer is exiting")
   270  		s.cancelNotify()
   271  		s.reqWG.Wait()
   272  		close(s.exitCh)
   273  	}()
   274  
   275  	ticker := time.NewTicker(5 * time.Second)
   276  	defer ticker.Stop()
   277  
   278  	for {
   279  		select {
   280  		case work := <-s.workCh:
   281  			// Update current work with new received block.
   282  			// Note same work can be past twice, happens when changing CPU threads.
   283  			s.results = work.results
   284  			s.makeWork(work.block)
   285  			s.notifyWork()
   286  
   287  		case work := <-s.fetchWorkCh:
   288  			// Return current mining work to remote miner.
   289  			if s.currentBlock == nil {
   290  				work.errc <- errNoMiningWork
   291  			} else {
   292  				work.res <- s.currentWork
   293  			}
   294  
   295  		case result := <-s.submitWorkCh:
   296  			// Verify submitted PoW solution based on maintained mining blocks.
   297  			if s.submitWork(result.nonce, result.mixDigest, result.hash) {
   298  				result.errc <- nil
   299  			} else {
   300  				result.errc <- errInvalidSealResult
   301  			}
   302  
   303  		case result := <-s.submitRateCh:
   304  			// Trace remote sealer's hash rate by submitted value.
   305  			s.rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
   306  			close(result.done)
   307  
   308  		case req := <-s.fetchRateCh:
   309  			// Gather all hash rate submitted by remote sealer.
   310  			var total uint64
   311  			for _, rate := range s.rates {
   312  				// this could overflow
   313  				total += rate.rate
   314  			}
   315  			req <- total
   316  
   317  		case <-ticker.C:
   318  			// Clear stale submitted hash rate.
   319  			for id, rate := range s.rates {
   320  				if time.Since(rate.ping) > 10*time.Second {
   321  					delete(s.rates, id)
   322  				}
   323  			}
   324  			// Clear stale pending blocks
   325  			if s.currentBlock != nil {
   326  				for hash, block := range s.works {
   327  					if block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() {
   328  						delete(s.works, hash)
   329  					}
   330  				}
   331  			}
   332  
   333  		case <-s.requestExit:
   334  			return
   335  		}
   336  	}
   337  }
   338  
   339  // makeWork creates a work package for external miner.
   340  //
   341  // The work package consists of 3 strings:
   342  //   result[0], 32 bytes hex encoded current block header pow-hash
   343  //   result[1], 32 bytes hex encoded seed hash used for DAG
   344  //   result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
   345  //   result[3], hex encoded block number
   346  func (s *remoteSealer) makeWork(block *types.Block) {
   347  	hash := s.ethash.SealHash(block.Header())
   348  	s.currentWork[0] = hash.Hex()
   349  	s.currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
   350  	s.currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
   351  	s.currentWork[3] = hexutil.EncodeBig(block.Number())
   352  
   353  	// Trace the seal work fetched by remote sealer.
   354  	s.currentBlock = block
   355  	s.works[hash] = block
   356  }
   357  
   358  // notifyWork notifies all the specified mining endpoints of the availability of
   359  // new work to be processed.
   360  func (s *remoteSealer) notifyWork() {
   361  	work := s.currentWork
   362  	blob, _ := json.Marshal(work)
   363  	s.reqWG.Add(len(s.notifyURLs))
   364  	for _, url := range s.notifyURLs {
   365  		go s.sendNotification(s.notifyCtx, url, blob, work)
   366  	}
   367  }
   368  
   369  func (s *remoteSealer) sendNotification(ctx context.Context, url string, json []byte, work [4]string) {
   370  	defer s.reqWG.Done()
   371  
   372  	req, err := http.NewRequest("POST", url, bytes.NewReader(json))
   373  	if err != nil {
   374  		s.ethash.config.Log.Warn("Can't create remote miner notification", "err", err)
   375  		return
   376  	}
   377  	ctx, cancel := context.WithTimeout(ctx, remoteSealerTimeout)
   378  	defer cancel()
   379  	req = req.WithContext(ctx)
   380  	req.Header.Set("Content-Type", "application/json")
   381  
   382  	resp, err := http.DefaultClient.Do(req)
   383  	if err != nil {
   384  		s.ethash.config.Log.Warn("Failed to notify remote miner", "err", err)
   385  	} else {
   386  		s.ethash.config.Log.Trace("Notified remote miner", "miner", url, "hash", work[0], "target", work[2])
   387  		resp.Body.Close()
   388  	}
   389  }
   390  
   391  // submitWork verifies the submitted pow solution, returning
   392  // whether the solution was accepted or not (not can be both a bad pow as well as
   393  // any other error, like no pending work or stale mining result).
   394  func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool {
   395  	if s.currentBlock == nil {
   396  		s.ethash.config.Log.Error("Pending work without block", "sealhash", sealhash)
   397  		return false
   398  	}
   399  	// Make sure the work submitted is present
   400  	block := s.works[sealhash]
   401  	if block == nil {
   402  		s.ethash.config.Log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", s.currentBlock.NumberU64())
   403  		return false
   404  	}
   405  	// Verify the correctness of submitted result.
   406  	header := block.Header()
   407  	header.Nonce = nonce
   408  	header.MixDigest = mixDigest
   409  
   410  	start := time.Now()
   411  	if !s.noverify {
   412  		if err := s.ethash.verifySeal(nil, header, true); err != nil {
   413  			s.ethash.config.Log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
   414  			return false
   415  		}
   416  	}
   417  	// Make sure the result channel is assigned.
   418  	if s.results == nil {
   419  		s.ethash.config.Log.Warn("Ethash result channel is empty, submitted mining result is rejected")
   420  		return false
   421  	}
   422  	s.ethash.config.Log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)))
   423  
   424  	// Solutions seems to be valid, return to the miner and notify acceptance.
   425  	solution := block.WithSeal(header)
   426  
   427  	// The submitted solution is within the scope of acceptance.
   428  	if solution.NumberU64()+staleThreshold > s.currentBlock.NumberU64() {
   429  		select {
   430  		case s.results <- solution:
   431  			s.ethash.config.Log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   432  			return true
   433  		default:
   434  			s.ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash)
   435  			return false
   436  		}
   437  	}
   438  	// The submitted block is too old to accept, drop it.
   439  	s.ethash.config.Log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
   440  	return false
   441  }