2017-04-04 22:16:29 +00: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 09:15:08 +00:00
|
|
|
"bytes"
|
2017-04-04 22:16:29 +00:00
|
|
|
crand "crypto/rand"
|
2018-08-08 09:15:08 +00:00
|
|
|
"encoding/json"
|
2018-08-03 08:33:37 +00:00
|
|
|
"errors"
|
2017-04-04 22:16:29 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2018-08-08 09:15:08 +00:00
|
|
|
"net/http"
|
2017-04-04 22:16:29 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
2018-08-03 08:33:37 +00:00
|
|
|
"time"
|
2017-04-04 22:16:29 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2018-08-03 08:33:37 +00:00
|
|
|
var (
|
|
|
|
errNoMiningWork = errors.New("no mining work available yet")
|
|
|
|
errInvalidSealResult = errors.New("invalid or stale proof-of-work solution")
|
|
|
|
)
|
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
// Seal implements consensus.Engine, attempting to find a nonce that satisfies
|
|
|
|
// the block's difficulty requirements.
|
|
|
|
func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
|
|
|
|
// If we're running a fake PoW, simply return a 0 nonce immediately
|
2017-11-24 14:10:27 +00:00
|
|
|
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
|
2017-04-04 22:16:29 +00:00
|
|
|
header := block.Header()
|
|
|
|
header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
|
|
|
|
return block.WithSeal(header), nil
|
|
|
|
}
|
|
|
|
// If we're running a shared PoW, delegate sealing to it
|
|
|
|
if ethash.shared != nil {
|
|
|
|
return ethash.shared.Seal(chain, block, stop)
|
|
|
|
}
|
|
|
|
// 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()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ethash.rand = rand.New(rand.NewSource(seed.Int64()))
|
|
|
|
}
|
|
|
|
ethash.lock.Unlock()
|
|
|
|
if threads == 0 {
|
|
|
|
threads = runtime.NumCPU()
|
|
|
|
}
|
2017-04-07 14:22:06 +00:00
|
|
|
if threads < 0 {
|
|
|
|
threads = 0 // Allows disabling local mining without extra logic around local/remote
|
|
|
|
}
|
2018-08-03 08:33:37 +00:00
|
|
|
// Push new work to remote sealer
|
|
|
|
if ethash.workCh != nil {
|
|
|
|
ethash.workCh <- block
|
|
|
|
}
|
2017-04-04 22:16:29 +00:00
|
|
|
var pend sync.WaitGroup
|
|
|
|
for i := 0; i < threads; i++ {
|
|
|
|
pend.Add(1)
|
|
|
|
go func(id int, nonce uint64) {
|
|
|
|
defer pend.Done()
|
2018-08-03 08:33:37 +00:00
|
|
|
ethash.mine(block, id, nonce, abort, ethash.resultCh)
|
2017-04-04 22:16:29 +00:00
|
|
|
}(i, uint64(ethash.rand.Int63()))
|
|
|
|
}
|
|
|
|
// Wait until sealing is terminated or a nonce is found
|
|
|
|
var result *types.Block
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
// Outside abort, stop all miner threads
|
|
|
|
close(abort)
|
2018-08-03 08:33:37 +00:00
|
|
|
case result = <-ethash.resultCh:
|
2017-04-04 22:16:29 +00:00
|
|
|
// One of the threads found a block, abort all others
|
|
|
|
close(abort)
|
|
|
|
case <-ethash.update:
|
|
|
|
// Thread count was changed on user request, restart
|
|
|
|
close(abort)
|
|
|
|
pend.Wait()
|
|
|
|
return ethash.Seal(chain, block, stop)
|
|
|
|
}
|
|
|
|
// Wait for all miners to terminate and return the block
|
|
|
|
pend.Wait()
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 10:05:30 +00:00
|
|
|
header = block.Header()
|
2018-08-23 13:02:57 +00:00
|
|
|
hash = ethash.SealHash(header).Bytes()
|
2018-08-08 09:15:08 +00:00
|
|
|
target = new(big.Int).Div(two256, header.Difficulty)
|
2017-04-04 22:16:29 +00:00
|
|
|
number = header.Number.Uint64()
|
2018-08-15 10:50:16 +00:00
|
|
|
dataset = ethash.dataset(number, false)
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
|
|
|
// Start generating random nonces until we abort or find a good one
|
|
|
|
var (
|
|
|
|
attempts = int64(0)
|
|
|
|
nonce = seed
|
|
|
|
)
|
|
|
|
logger := log.New("miner", id)
|
|
|
|
logger.Trace("Started ethash search for new nonces", "seed", seed)
|
2018-01-23 10:05:30 +00:00
|
|
|
search:
|
2017-04-04 22:16:29 +00: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 10:05:30 +00:00
|
|
|
break search
|
2017-04-04 22:16:29 +00: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 10:05:30 +00:00
|
|
|
digest, result := hashimotoFull(dataset.dataset, hash, nonce)
|
2017-04-04 22:16:29 +00: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 10:05:30 +00:00
|
|
|
break search
|
2017-04-04 22:16:29 +00:00
|
|
|
}
|
|
|
|
nonce++
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 10:05:30 +00: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 22:16:29 +00:00
|
|
|
}
|
2018-08-03 08:33:37 +00:00
|
|
|
|
2018-08-08 09:15:08 +00:00
|
|
|
// remote is a standalone goroutine to handle remote mining related stuff.
|
|
|
|
func (ethash *Ethash) remote(notify []string) {
|
2018-08-03 08:33:37 +00:00
|
|
|
var (
|
2018-08-08 09:15:08 +00:00
|
|
|
works = make(map[common.Hash]*types.Block)
|
|
|
|
rates = make(map[common.Hash]hashrate)
|
|
|
|
|
|
|
|
currentBlock *types.Block
|
|
|
|
currentWork [3]string
|
|
|
|
|
|
|
|
notifyTransport = &http.Transport{}
|
|
|
|
notifyClient = &http.Client{
|
|
|
|
Transport: notifyTransport,
|
|
|
|
Timeout: time.Second,
|
|
|
|
}
|
|
|
|
notifyReqs = make([]*http.Request, len(notify))
|
2018-08-03 08:33:37 +00:00
|
|
|
)
|
2018-08-08 09:15:08 +00:00
|
|
|
// notifyWork notifies all the specified mining endpoints of the availability of
|
|
|
|
// new work to be processed.
|
|
|
|
notifyWork := func() {
|
|
|
|
work := currentWork
|
|
|
|
blob, _ := json.Marshal(work)
|
2018-08-03 08:33:37 +00:00
|
|
|
|
2018-08-08 09:15:08 +00:00
|
|
|
for i, url := range notify {
|
|
|
|
// Terminate any previously pending request and create the new work
|
|
|
|
if notifyReqs[i] != nil {
|
|
|
|
notifyTransport.CancelRequest(notifyReqs[i])
|
|
|
|
}
|
|
|
|
notifyReqs[i], _ = http.NewRequest("POST", url, bytes.NewReader(blob))
|
|
|
|
notifyReqs[i].Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// Push the new work concurrently to all the remote nodes
|
|
|
|
go func(req *http.Request, url string) {
|
|
|
|
res, err := notifyClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Failed to notify remote miner", "err", err)
|
|
|
|
} else {
|
|
|
|
log.Trace("Notified remote miner", "miner", url, "hash", log.Lazy{Fn: func() common.Hash { return common.HexToHash(work[0]) }}, "target", work[2])
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
}(notifyReqs[i], url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// makeWork creates a work package for external miner.
|
2018-08-03 08:33:37 +00:00
|
|
|
//
|
|
|
|
// 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
|
2018-08-08 09:15:08 +00:00
|
|
|
makeWork := func(block *types.Block) {
|
2018-08-23 13:02:57 +00:00
|
|
|
hash := ethash.SealHash(block.Header())
|
2018-08-03 08:33:37 +00:00
|
|
|
|
2018-08-08 09:15:08 +00:00
|
|
|
currentWork[0] = hash.Hex()
|
|
|
|
currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
|
|
|
|
currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
|
2018-08-03 08:33:37 +00:00
|
|
|
|
|
|
|
// Trace the seal work fetched by remote sealer.
|
2018-08-08 09:15:08 +00:00
|
|
|
currentBlock = block
|
|
|
|
works[hash] = block
|
2018-08-03 08:33:37 +00:00
|
|
|
}
|
|
|
|
// 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).
|
|
|
|
submitWork := func(nonce types.BlockNonce, mixDigest common.Hash, hash common.Hash) bool {
|
|
|
|
// Make sure the work submitted is present
|
|
|
|
block := works[hash]
|
|
|
|
if block == nil {
|
|
|
|
log.Info("Work submitted but none pending", "hash", hash)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Verify the correctness of submitted result.
|
|
|
|
header := block.Header()
|
|
|
|
header.Nonce = nonce
|
|
|
|
header.MixDigest = mixDigest
|
2018-08-15 10:50:16 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
if err := ethash.verifySeal(nil, header, true); err != nil {
|
|
|
|
log.Warn("Invalid proof-of-work submitted", "hash", hash, "elapsed", time.Since(start), "err", err)
|
2018-08-03 08:33:37 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Make sure the result channel is created.
|
|
|
|
if ethash.resultCh == nil {
|
|
|
|
log.Warn("Ethash result channel is empty, submitted mining result is rejected")
|
|
|
|
return false
|
|
|
|
}
|
2018-08-15 10:50:16 +00:00
|
|
|
log.Trace("Verified correct proof-of-work", "hash", hash, "elapsed", time.Since(start))
|
2018-08-03 08:33:37 +00:00
|
|
|
|
|
|
|
// Solutions seems to be valid, return to the miner and notify acceptance.
|
|
|
|
select {
|
|
|
|
case ethash.resultCh <- block.WithSeal(header):
|
|
|
|
delete(works, hash)
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
log.Info("Work submitted is stale", "hash", hash)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case block := <-ethash.workCh:
|
2018-08-08 09:15:08 +00:00
|
|
|
if currentBlock != nil && block.ParentHash() != currentBlock.ParentHash() {
|
2018-08-03 08:33:37 +00:00
|
|
|
// Start new round mining, throw out all previous work.
|
|
|
|
works = make(map[common.Hash]*types.Block)
|
|
|
|
}
|
|
|
|
// Update current work with new received block.
|
|
|
|
// Note same work can be past twice, happens when changing CPU threads.
|
2018-08-08 09:15:08 +00:00
|
|
|
makeWork(block)
|
|
|
|
|
|
|
|
// Notify and requested URLs of the new work availability
|
|
|
|
notifyWork()
|
2018-08-03 08:33:37 +00:00
|
|
|
|
|
|
|
case work := <-ethash.fetchWorkCh:
|
|
|
|
// Return current mining work to remote miner.
|
2018-08-08 09:15:08 +00:00
|
|
|
if currentBlock == nil {
|
|
|
|
work.errc <- errNoMiningWork
|
2018-08-03 08:33:37 +00:00
|
|
|
} else {
|
2018-08-08 09:15:08 +00:00
|
|
|
work.res <- currentWork
|
2018-08-03 08:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case result := <-ethash.submitWorkCh:
|
|
|
|
// Verify submitted PoW solution based on maintained mining blocks.
|
|
|
|
if submitWork(result.nonce, result.mixDigest, result.hash) {
|
|
|
|
result.errc <- nil
|
|
|
|
} else {
|
|
|
|
result.errc <- errInvalidSealResult
|
|
|
|
}
|
|
|
|
|
|
|
|
case result := <-ethash.submitRateCh:
|
|
|
|
// Trace remote sealer's hash rate by submitted value.
|
|
|
|
rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
|
|
|
|
close(result.done)
|
|
|
|
|
|
|
|
case req := <-ethash.fetchRateCh:
|
|
|
|
// Gather all hash rate submitted by remote sealer.
|
|
|
|
var total uint64
|
|
|
|
for _, rate := range rates {
|
|
|
|
// this could overflow
|
|
|
|
total += rate.rate
|
|
|
|
}
|
|
|
|
req <- total
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
// Clear stale submitted hash rate.
|
|
|
|
for id, rate := range rates {
|
|
|
|
if time.Since(rate.ping) > 10*time.Second {
|
|
|
|
delete(rates, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case errc := <-ethash.exitCh:
|
|
|
|
// Exit remote loop if ethash is closed and return relevant error.
|
|
|
|
errc <- nil
|
|
|
|
log.Trace("Ethash remote sealer is exiting")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|