2017-04-04 17:16:29 -05:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ethash
|
|
|
|
|
|
|
|
import (
|
2018-08-08 04:15:08 -05:00
|
|
|
"bytes"
|
2019-11-28 03:51:57 -06:00
|
|
|
"context"
|
2017-04-04 17:16:29 -05:00
|
|
|
crand "crypto/rand"
|
2018-08-08 04:15:08 -05:00
|
|
|
"encoding/json"
|
2018-08-03 03:33:37 -05:00
|
|
|
"errors"
|
2017-04-04 17:16:29 -05:00
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2018-08-08 04:15:08 -05:00
|
|
|
"net/http"
|
2017-04-04 17:16:29 -05:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
2018-08-03 03:33:37 -05:00
|
|
|
"time"
|
2017-04-04 17:16:29 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-11-08 09:08:57 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-04-04 17:16:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
2018-08-28 08:59:05 -05:00
|
|
|
const (
|
|
|
|
// staleThreshold is the maximum depth of the acceptable stale but valid ethash solution.
|
|
|
|
staleThreshold = 7
|
|
|
|
)
|
|
|
|
|
2018-08-03 03:33:37 -05:00
|
|
|
var (
|
|
|
|
errNoMiningWork = errors.New("no mining work available yet")
|
|
|
|
errInvalidSealResult = errors.New("invalid or stale proof-of-work solution")
|
|
|
|
)
|
|
|
|
|
2017-04-04 17:16:29 -05:00
|
|
|
// Seal implements consensus.Engine, attempting to find a nonce that satisfies
|
|
|
|
// the block's difficulty requirements.
|
2020-07-28 10:02:35 -05:00
|
|
|
func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
|
2017-04-04 17:16:29 -05:00
|
|
|
// If we're running a fake PoW, simply return a 0 nonce immediately
|
2017-11-24 08:10:27 -06:00
|
|
|
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
|
2017-04-04 17:16:29 -05:00
|
|
|
header := block.Header()
|
|
|
|
header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
|
2018-08-28 08:59:05 -05:00
|
|
|
select {
|
|
|
|
case results <- block.WithSeal(header):
|
|
|
|
default:
|
2019-11-28 03:51:57 -06:00
|
|
|
ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", ethash.SealHash(block.Header()))
|
2018-08-28 08:59:05 -05:00
|
|
|
}
|
|
|
|
return nil
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
// If we're running a shared PoW, delegate sealing to it
|
|
|
|
if ethash.shared != nil {
|
2018-08-28 08:59:05 -05:00
|
|
|
return ethash.shared.Seal(chain, block, results, stop)
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
// Create a runner and the multiple search threads it directs
|
|
|
|
abort := make(chan struct{})
|
|
|
|
|
|
|
|
ethash.lock.Lock()
|
|
|
|
threads := ethash.threads
|
|
|
|
if ethash.rand == nil {
|
|
|
|
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
|
|
|
|
if err != nil {
|
|
|
|
ethash.lock.Unlock()
|
2018-08-28 08:59:05 -05:00
|
|
|
return err
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
ethash.rand = rand.New(rand.NewSource(seed.Int64()))
|
|
|
|
}
|
|
|
|
ethash.lock.Unlock()
|
|
|
|
if threads == 0 {
|
|
|
|
threads = runtime.NumCPU()
|
|
|
|
}
|
2017-04-07 09:22:06 -05:00
|
|
|
if threads < 0 {
|
|
|
|
threads = 0 // Allows disabling local mining without extra logic around local/remote
|
|
|
|
}
|
2018-08-03 03:33:37 -05:00
|
|
|
// Push new work to remote sealer
|
2019-11-28 03:51:57 -06:00
|
|
|
if ethash.remote != nil {
|
|
|
|
ethash.remote.workCh <- &sealTask{block: block, results: results}
|
2018-08-03 03:33:37 -05:00
|
|
|
}
|
2018-08-28 08:59:05 -05:00
|
|
|
var (
|
|
|
|
pend sync.WaitGroup
|
|
|
|
locals = make(chan *types.Block)
|
|
|
|
)
|
2017-04-04 17:16:29 -05:00
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
pend.Add(1)
|
|
|
|
go func(id int, nonce uint64) {
|
|
|
|
defer pend.Done()
|
2018-08-28 08:59:05 -05:00
|
|
|
ethash.mine(block, id, nonce, abort, locals)
|
2017-04-04 17:16:29 -05:00
|
|
|
}(i, uint64(ethash.rand.Int63()))
|
|
|
|
}
|
|
|
|
// Wait until sealing is terminated or a nonce is found
|
2018-08-28 08:59:05 -05:00
|
|
|
go func() {
|
|
|
|
var result *types.Block
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
// Outside abort, stop all miner threads
|
|
|
|
close(abort)
|
|
|
|
case result = <-locals:
|
|
|
|
// One of the threads found a block, abort all others
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
default:
|
2019-11-28 03:51:57 -06:00
|
|
|
ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "local", "sealhash", ethash.SealHash(block.Header()))
|
2018-08-28 08:59:05 -05:00
|
|
|
}
|
|
|
|
close(abort)
|
|
|
|
case <-ethash.update:
|
|
|
|
// Thread count was changed on user request, restart
|
|
|
|
close(abort)
|
|
|
|
if err := ethash.Seal(chain, block, results, stop); err != nil {
|
2019-11-28 03:51:57 -06:00
|
|
|
ethash.config.Log.Error("Failed to restart sealing after update", "err", err)
|
2018-08-28 08:59:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wait for all miners to terminate and return the block
|
2017-04-04 17:16:29 -05:00
|
|
|
pend.Wait()
|
2018-08-28 08:59:05 -05:00
|
|
|
}()
|
|
|
|
return nil
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// mine is the actual proof-of-work miner that searches for a nonce starting from
|
|
|
|
// seed that results in correct final block difficulty.
|
|
|
|
func (ethash *Ethash) mine(block *types.Block, id int, seed uint64, abort chan struct{}, found chan *types.Block) {
|
|
|
|
// Extract some data from the header
|
|
|
|
var (
|
2018-01-23 04:05:30 -06:00
|
|
|
header = block.Header()
|
2018-08-23 08:02:57 -05:00
|
|
|
hash = ethash.SealHash(header).Bytes()
|
2018-08-08 04:15:08 -05:00
|
|
|
target = new(big.Int).Div(two256, header.Difficulty)
|
2017-04-04 17:16:29 -05:00
|
|
|
number = header.Number.Uint64()
|
2018-08-15 05:50:16 -05:00
|
|
|
dataset = ethash.dataset(number, false)
|
2017-04-04 17:16:29 -05:00
|
|
|
)
|
|
|
|
// Start generating random nonces until we abort or find a good one
|
|
|
|
var (
|
|
|
|
attempts = int64(0)
|
|
|
|
nonce = seed
|
|
|
|
)
|
2019-11-28 03:51:57 -06:00
|
|
|
logger := ethash.config.Log.New("miner", id)
|
2017-04-04 17:16:29 -05:00
|
|
|
logger.Trace("Started ethash search for new nonces", "seed", seed)
|
2018-01-23 04:05:30 -06:00
|
|
|
search:
|
2017-04-04 17:16:29 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-abort:
|
|
|
|
// Mining terminated, update stats and abort
|
|
|
|
logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed)
|
|
|
|
ethash.hashrate.Mark(attempts)
|
2018-01-23 04:05:30 -06:00
|
|
|
break search
|
2017-04-04 17:16:29 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
// We don't have to update hash rate on every nonce, so update after after 2^X nonces
|
|
|
|
attempts++
|
|
|
|
if (attempts % (1 << 15)) == 0 {
|
|
|
|
ethash.hashrate.Mark(attempts)
|
|
|
|
attempts = 0
|
|
|
|
}
|
|
|
|
// Compute the PoW value of this nonce
|
2018-01-23 04:05:30 -06:00
|
|
|
digest, result := hashimotoFull(dataset.dataset, hash, nonce)
|
2017-04-04 17:16:29 -05:00
|
|
|
if new(big.Int).SetBytes(result).Cmp(target) <= 0 {
|
|
|
|
// Correct nonce found, create a new header with it
|
|
|
|
header = types.CopyHeader(header)
|
|
|
|
header.Nonce = types.EncodeNonce(nonce)
|
|
|
|
header.MixDigest = common.BytesToHash(digest)
|
|
|
|
|
|
|
|
// Seal and return a block (if still needed)
|
|
|
|
select {
|
|
|
|
case found <- block.WithSeal(header):
|
|
|
|
logger.Trace("Ethash nonce found and reported", "attempts", nonce-seed, "nonce", nonce)
|
|
|
|
case <-abort:
|
|
|
|
logger.Trace("Ethash nonce found but discarded", "attempts", nonce-seed, "nonce", nonce)
|
|
|
|
}
|
2018-01-23 04:05:30 -06:00
|
|
|
break search
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
nonce++
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 04:05:30 -06:00
|
|
|
// Datasets are unmapped in a finalizer. Ensure that the dataset stays live
|
|
|
|
// during sealing so it's not unmapped while being read.
|
|
|
|
runtime.KeepAlive(dataset)
|
2017-04-04 17:16:29 -05:00
|
|
|
}
|
2018-08-03 03:33:37 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
// This is the timeout for HTTP requests to notify external miners.
|
|
|
|
const remoteSealerTimeout = 1 * time.Second
|
2018-08-08 04:15:08 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
type remoteSealer struct {
|
|
|
|
works map[common.Hash]*types.Block
|
|
|
|
rates map[common.Hash]hashrate
|
|
|
|
currentBlock *types.Block
|
|
|
|
currentWork [4]string
|
|
|
|
notifyCtx context.Context
|
|
|
|
cancelNotify context.CancelFunc // cancels all notification requests
|
|
|
|
reqWG sync.WaitGroup // tracks notification request goroutines
|
2018-08-08 04:15:08 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
ethash *Ethash
|
|
|
|
noverify bool
|
|
|
|
notifyURLs []string
|
|
|
|
results chan<- *types.Block
|
|
|
|
workCh chan *sealTask // Notification channel to push new work and relative result channel to remote sealer
|
|
|
|
fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work
|
|
|
|
submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result
|
|
|
|
fetchRateCh chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer.
|
|
|
|
submitRateCh chan *hashrate // Channel used for remote sealer to submit their mining hashrate
|
|
|
|
requestExit chan struct{}
|
|
|
|
exitCh chan struct{}
|
|
|
|
}
|
2018-08-03 03:33:37 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
// sealTask wraps a seal block with relative result channel for remote sealer thread.
|
|
|
|
type sealTask struct {
|
|
|
|
block *types.Block
|
|
|
|
results chan<- *types.Block
|
|
|
|
}
|
2018-08-28 08:59:05 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
// mineResult wraps the pow solution parameters for the specified block.
|
|
|
|
type mineResult struct {
|
|
|
|
nonce types.BlockNonce
|
|
|
|
mixDigest common.Hash
|
|
|
|
hash common.Hash
|
|
|
|
|
|
|
|
errc chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// hashrate wraps the hash rate submitted by the remote sealer.
|
|
|
|
type hashrate struct {
|
|
|
|
id common.Hash
|
|
|
|
ping time.Time
|
|
|
|
rate uint64
|
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sealWork wraps a seal work package for remote sealer.
|
|
|
|
type sealWork struct {
|
|
|
|
errc chan error
|
|
|
|
res chan [4]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRemoteSealer(ethash *Ethash, urls []string, noverify bool) *remoteSealer {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
s := &remoteSealer{
|
|
|
|
ethash: ethash,
|
|
|
|
noverify: noverify,
|
|
|
|
notifyURLs: urls,
|
|
|
|
notifyCtx: ctx,
|
|
|
|
cancelNotify: cancel,
|
|
|
|
works: make(map[common.Hash]*types.Block),
|
|
|
|
rates: make(map[common.Hash]hashrate),
|
|
|
|
workCh: make(chan *sealTask),
|
|
|
|
fetchWorkCh: make(chan *sealWork),
|
|
|
|
submitWorkCh: make(chan *mineResult),
|
|
|
|
fetchRateCh: make(chan chan uint64),
|
|
|
|
submitRateCh: make(chan *hashrate),
|
|
|
|
requestExit: make(chan struct{}),
|
|
|
|
exitCh: make(chan struct{}),
|
2018-08-03 03:33:37 -05:00
|
|
|
}
|
2019-11-28 03:51:57 -06:00
|
|
|
go s.loop()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *remoteSealer) loop() {
|
|
|
|
defer func() {
|
|
|
|
s.ethash.config.Log.Trace("Ethash remote sealer is exiting")
|
|
|
|
s.cancelNotify()
|
|
|
|
s.reqWG.Wait()
|
|
|
|
close(s.exitCh)
|
|
|
|
}()
|
2018-08-03 03:33:37 -05:00
|
|
|
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-28 03:51:57 -06:00
|
|
|
case work := <-s.workCh:
|
2018-08-03 03:33:37 -05:00
|
|
|
// Update current work with new received block.
|
|
|
|
// Note same work can be past twice, happens when changing CPU threads.
|
2019-11-28 03:51:57 -06:00
|
|
|
s.results = work.results
|
|
|
|
s.makeWork(work.block)
|
|
|
|
s.notifyWork()
|
2018-08-28 08:59:05 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
case work := <-s.fetchWorkCh:
|
2018-08-03 03:33:37 -05:00
|
|
|
// Return current mining work to remote miner.
|
2019-11-28 03:51:57 -06:00
|
|
|
if s.currentBlock == nil {
|
2018-08-08 04:15:08 -05:00
|
|
|
work.errc <- errNoMiningWork
|
2018-08-03 03:33:37 -05:00
|
|
|
} else {
|
2019-11-28 03:51:57 -06:00
|
|
|
work.res <- s.currentWork
|
2018-08-03 03:33:37 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
case result := <-s.submitWorkCh:
|
2018-08-03 03:33:37 -05:00
|
|
|
// Verify submitted PoW solution based on maintained mining blocks.
|
2019-11-28 03:51:57 -06:00
|
|
|
if s.submitWork(result.nonce, result.mixDigest, result.hash) {
|
2018-08-03 03:33:37 -05:00
|
|
|
result.errc <- nil
|
|
|
|
} else {
|
|
|
|
result.errc <- errInvalidSealResult
|
|
|
|
}
|
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
case result := <-s.submitRateCh:
|
2018-08-03 03:33:37 -05:00
|
|
|
// Trace remote sealer's hash rate by submitted value.
|
2019-11-28 03:51:57 -06:00
|
|
|
s.rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
|
2018-08-03 03:33:37 -05:00
|
|
|
close(result.done)
|
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
case req := <-s.fetchRateCh:
|
2018-08-03 03:33:37 -05:00
|
|
|
// Gather all hash rate submitted by remote sealer.
|
|
|
|
var total uint64
|
2019-11-28 03:51:57 -06:00
|
|
|
for _, rate := range s.rates {
|
2018-08-03 03:33:37 -05:00
|
|
|
// this could overflow
|
|
|
|
total += rate.rate
|
|
|
|
}
|
|
|
|
req <- total
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
// Clear stale submitted hash rate.
|
2019-11-28 03:51:57 -06:00
|
|
|
for id, rate := range s.rates {
|
2018-08-03 03:33:37 -05:00
|
|
|
if time.Since(rate.ping) > 10*time.Second {
|
2019-11-28 03:51:57 -06:00
|
|
|
delete(s.rates, id)
|
2018-08-03 03:33:37 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 08:59:05 -05:00
|
|
|
// Clear stale pending blocks
|
2019-11-28 03:51:57 -06:00
|
|
|
if s.currentBlock != nil {
|
|
|
|
for hash, block := range s.works {
|
|
|
|
if block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() {
|
|
|
|
delete(s.works, hash)
|
2018-08-28 08:59:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 03:33:37 -05:00
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
case <-s.requestExit:
|
2018-08-03 03:33:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 03:51:57 -06:00
|
|
|
|
|
|
|
// makeWork creates a work package for external miner.
|
|
|
|
//
|
|
|
|
// The work package consists of 3 strings:
|
|
|
|
// result[0], 32 bytes hex encoded current block header pow-hash
|
|
|
|
// result[1], 32 bytes hex encoded seed hash used for DAG
|
|
|
|
// result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
|
|
|
|
// result[3], hex encoded block number
|
|
|
|
func (s *remoteSealer) makeWork(block *types.Block) {
|
|
|
|
hash := s.ethash.SealHash(block.Header())
|
|
|
|
s.currentWork[0] = hash.Hex()
|
|
|
|
s.currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
|
|
|
|
s.currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
|
|
|
|
s.currentWork[3] = hexutil.EncodeBig(block.Number())
|
|
|
|
|
|
|
|
// Trace the seal work fetched by remote sealer.
|
|
|
|
s.currentBlock = block
|
|
|
|
s.works[hash] = block
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifyWork notifies all the specified mining endpoints of the availability of
|
|
|
|
// new work to be processed.
|
|
|
|
func (s *remoteSealer) notifyWork() {
|
|
|
|
work := s.currentWork
|
2021-03-26 12:30:10 -05:00
|
|
|
|
|
|
|
// Encode the JSON payload of the notification. When NotifyFull is set,
|
|
|
|
// this is the complete block header, otherwise it is a JSON array.
|
|
|
|
var blob []byte
|
|
|
|
if s.ethash.config.NotifyFull {
|
|
|
|
blob, _ = json.Marshal(s.currentBlock.Header())
|
|
|
|
} else {
|
|
|
|
blob, _ = json.Marshal(work)
|
|
|
|
}
|
|
|
|
|
2019-11-28 03:51:57 -06:00
|
|
|
s.reqWG.Add(len(s.notifyURLs))
|
|
|
|
for _, url := range s.notifyURLs {
|
|
|
|
go s.sendNotification(s.notifyCtx, url, blob, work)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *remoteSealer) sendNotification(ctx context.Context, url string, json []byte, work [4]string) {
|
|
|
|
defer s.reqWG.Done()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(json))
|
|
|
|
if err != nil {
|
|
|
|
s.ethash.config.Log.Warn("Can't create remote miner notification", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, remoteSealerTimeout)
|
|
|
|
defer cancel()
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
s.ethash.config.Log.Warn("Failed to notify remote miner", "err", err)
|
|
|
|
} else {
|
|
|
|
s.ethash.config.Log.Trace("Notified remote miner", "miner", url, "hash", work[0], "target", work[2])
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// submitWork verifies the submitted pow solution, returning
|
|
|
|
// whether the solution was accepted or not (not can be both a bad pow as well as
|
|
|
|
// any other error, like no pending work or stale mining result).
|
|
|
|
func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool {
|
|
|
|
if s.currentBlock == nil {
|
|
|
|
s.ethash.config.Log.Error("Pending work without block", "sealhash", sealhash)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Make sure the work submitted is present
|
|
|
|
block := s.works[sealhash]
|
|
|
|
if block == nil {
|
|
|
|
s.ethash.config.Log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", s.currentBlock.NumberU64())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Verify the correctness of submitted result.
|
|
|
|
header := block.Header()
|
|
|
|
header.Nonce = nonce
|
|
|
|
header.MixDigest = mixDigest
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
if !s.noverify {
|
|
|
|
if err := s.ethash.verifySeal(nil, header, true); err != nil {
|
|
|
|
s.ethash.config.Log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure the result channel is assigned.
|
|
|
|
if s.results == nil {
|
|
|
|
s.ethash.config.Log.Warn("Ethash result channel is empty, submitted mining result is rejected")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s.ethash.config.Log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
|
|
|
|
// Solutions seems to be valid, return to the miner and notify acceptance.
|
|
|
|
solution := block.WithSeal(header)
|
|
|
|
|
|
|
|
// The submitted solution is within the scope of acceptance.
|
|
|
|
if solution.NumberU64()+staleThreshold > s.currentBlock.NumberU64() {
|
|
|
|
select {
|
|
|
|
case s.results <- solution:
|
|
|
|
s.ethash.config.Log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
s.ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The submitted block is too old to accept, drop it.
|
|
|
|
s.ethash.config.Log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
|
|
|
|
return false
|
|
|
|
}
|