aboutsummaryrefslogtreecommitdiffstats
path: root/consensus/ethash
diff options
context:
space:
mode:
Diffstat (limited to 'consensus/ethash')
-rw-r--r--consensus/ethash/algorithm_test.go2
-rw-r--r--consensus/ethash/ethash.go33
-rw-r--r--consensus/ethash/ethash_test.go40
-rw-r--r--consensus/ethash/sealer.go134
-rw-r--r--consensus/ethash/sealer_test.go94
5 files changed, 215 insertions, 88 deletions
diff --git a/consensus/ethash/algorithm_test.go b/consensus/ethash/algorithm_test.go
index db22cccd0..c58479e28 100644
--- a/consensus/ethash/algorithm_test.go
+++ b/consensus/ethash/algorithm_test.go
@@ -729,7 +729,7 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) {
go func(idx int) {
defer pend.Done()
- ethash := New(Config{cachedir, 0, 1, "", 0, 0, ModeNormal}, nil)
+ ethash := New(Config{cachedir, 0, 1, "", 0, 0, ModeNormal}, nil, false)
defer ethash.Close()
if err := ethash.VerifySeal(nil, block.Header()); err != nil {
t.Errorf("proc %d: block verification failed: %v", idx, err)
diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go
index d98c3371c..b4819ca38 100644
--- a/consensus/ethash/ethash.go
+++ b/consensus/ethash/ethash.go
@@ -50,7 +50,7 @@ var (
two256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
// sharedEthash is a full instance that can be shared between multiple users.
- sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal}, nil)
+ sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal}, nil, false)
// algorithmRevision is the data structure version used for file naming.
algorithmRevision = 23
@@ -405,6 +405,12 @@ type Config struct {
PowMode Mode
}
+// sealTask wraps a seal block with relative result channel for remote sealer thread.
+type sealTask struct {
+ block *types.Block
+ results chan<- *types.Block
+}
+
// mineResult wraps the pow solution parameters for the specified block.
type mineResult struct {
nonce types.BlockNonce
@@ -444,12 +450,11 @@ type Ethash struct {
hashrate metrics.Meter // Meter tracking the average hashrate
// Remote sealer related fields
- workCh chan *types.Block // Notification channel to push new work to remote sealer
- resultCh chan *types.Block // Channel used by mining threads to return result
- 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
+ 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
// The fields below are hooks for testing
shared *Ethash // Shared PoW verifier to avoid cache regeneration
@@ -464,7 +469,7 @@ type Ethash struct {
// New creates a full sized ethash PoW scheme and starts a background thread for
// remote mining, also optionally notifying a batch of remote services of new work
// packages.
-func New(config Config, notify []string) *Ethash {
+func New(config Config, notify []string, noverify bool) *Ethash {
if config.CachesInMem <= 0 {
log.Warn("One ethash cache must always be in memory", "requested", config.CachesInMem)
config.CachesInMem = 1
@@ -481,36 +486,34 @@ func New(config Config, notify []string) *Ethash {
datasets: newlru("dataset", config.DatasetsInMem, newDataset),
update: make(chan struct{}),
hashrate: metrics.NewMeter(),
- workCh: make(chan *types.Block),
- resultCh: make(chan *types.Block),
+ workCh: make(chan *sealTask),
fetchWorkCh: make(chan *sealWork),
submitWorkCh: make(chan *mineResult),
fetchRateCh: make(chan chan uint64),
submitRateCh: make(chan *hashrate),
exitCh: make(chan chan error),
}
- go ethash.remote(notify)
+ go ethash.remote(notify, noverify)
return ethash
}
// NewTester creates a small sized ethash PoW scheme useful only for testing
// purposes.
-func NewTester(notify []string) *Ethash {
+func NewTester(notify []string, noverify bool) *Ethash {
ethash := &Ethash{
config: Config{PowMode: ModeTest},
caches: newlru("cache", 1, newCache),
datasets: newlru("dataset", 1, newDataset),
update: make(chan struct{}),
hashrate: metrics.NewMeter(),
- workCh: make(chan *types.Block),
- resultCh: make(chan *types.Block),
+ workCh: make(chan *sealTask),
fetchWorkCh: make(chan *sealWork),
submitWorkCh: make(chan *mineResult),
fetchRateCh: make(chan chan uint64),
submitRateCh: make(chan *hashrate),
exitCh: make(chan chan error),
}
- go ethash.remote(notify)
+ go ethash.remote(notify, noverify)
return ethash
}
diff --git a/consensus/ethash/ethash_test.go b/consensus/ethash/ethash_test.go
index b190d63d6..8eded2ca8 100644
--- a/consensus/ethash/ethash_test.go
+++ b/consensus/ethash/ethash_test.go
@@ -34,17 +34,23 @@ import (
func TestTestMode(t *testing.T) {
header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
- ethash := NewTester(nil)
+ ethash := NewTester(nil, false)
defer ethash.Close()
- block, err := ethash.Seal(nil, types.NewBlockWithHeader(header), nil)
+ results := make(chan *types.Block)
+ err := ethash.Seal(nil, types.NewBlockWithHeader(header), results, nil)
if err != nil {
t.Fatalf("failed to seal block: %v", err)
}
- header.Nonce = types.EncodeNonce(block.Nonce())
- header.MixDigest = block.MixDigest()
- if err := ethash.VerifySeal(nil, header); err != nil {
- t.Fatalf("unexpected verification error: %v", err)
+ select {
+ case block := <-results:
+ header.Nonce = types.EncodeNonce(block.Nonce())
+ header.MixDigest = block.MixDigest()
+ if err := ethash.VerifySeal(nil, header); err != nil {
+ t.Fatalf("unexpected verification error: %v", err)
+ }
+ case <-time.NewTimer(time.Second).C:
+ t.Error("sealing result timeout")
}
}
@@ -56,7 +62,7 @@ func TestCacheFileEvict(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
- e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}, nil)
+ e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest}, nil, false)
defer e.Close()
workers := 8
@@ -85,7 +91,7 @@ func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
}
func TestRemoteSealer(t *testing.T) {
- ethash := NewTester(nil)
+ ethash := NewTester(nil, false)
defer ethash.Close()
api := &API{ethash}
@@ -97,7 +103,8 @@ func TestRemoteSealer(t *testing.T) {
sealhash := ethash.SealHash(header)
// Push new work.
- ethash.Seal(nil, block, nil)
+ results := make(chan *types.Block)
+ ethash.Seal(nil, block, results, nil)
var (
work [3]string
@@ -114,20 +121,11 @@ func TestRemoteSealer(t *testing.T) {
header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
block = types.NewBlockWithHeader(header)
sealhash = ethash.SealHash(header)
- ethash.Seal(nil, block, nil)
+ ethash.Seal(nil, block, results, nil)
if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
t.Error("expect to return the latest pushed work")
}
- // Push block with higher block number.
- newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)}
- newBlock := types.NewBlockWithHeader(newHead)
- newSealhash := ethash.SealHash(newHead)
- ethash.Seal(nil, newBlock, nil)
-
- if res := api.SubmitWork(types.BlockNonce{}, newSealhash, common.Hash{}); res {
- t.Error("expect to return false when submit a stale solution")
- }
}
func TestHashRate(t *testing.T) {
@@ -136,7 +134,7 @@ func TestHashRate(t *testing.T) {
expect uint64
ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
)
- ethash := NewTester(nil)
+ ethash := NewTester(nil, false)
defer ethash.Close()
if tot := ethash.Hashrate(); tot != 0 {
@@ -156,7 +154,7 @@ func TestHashRate(t *testing.T) {
}
func TestClosedRemoteSealer(t *testing.T) {
- ethash := NewTester(nil)
+ ethash := NewTester(nil, false)
time.Sleep(1 * time.Second) // ensure exit channel is listening
ethash.Close()
diff --git a/consensus/ethash/sealer.go b/consensus/ethash/sealer.go
index a458c60f6..06c98a781 100644
--- a/consensus/ethash/sealer.go
+++ b/consensus/ethash/sealer.go
@@ -35,6 +35,11 @@ import (
"github.com/ethereum/go-ethereum/log"
)
+const (
+ // staleThreshold is the maximum depth of the acceptable stale but valid ethash solution.
+ staleThreshold = 7
+)
+
var (
errNoMiningWork = errors.New("no mining work available yet")
errInvalidSealResult = errors.New("invalid or stale proof-of-work solution")
@@ -42,16 +47,21 @@ var (
// 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) {
+func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
// If we're running a fake PoW, simply return a 0 nonce immediately
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
header := block.Header()
header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
- return block.WithSeal(header), nil
+ select {
+ case results <- block.WithSeal(header):
+ default:
+ log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", ethash.SealHash(block.Header()))
+ }
+ return nil
}
// If we're running a shared PoW, delegate sealing to it
if ethash.shared != nil {
- return ethash.shared.Seal(chain, block, stop)
+ return ethash.shared.Seal(chain, block, results, stop)
}
// Create a runner and the multiple search threads it directs
abort := make(chan struct{})
@@ -62,7 +72,7 @@ func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, stop
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
ethash.lock.Unlock()
- return nil, err
+ return err
}
ethash.rand = rand.New(rand.NewSource(seed.Int64()))
}
@@ -75,34 +85,45 @@ func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, stop
}
// Push new work to remote sealer
if ethash.workCh != nil {
- ethash.workCh <- block
+ ethash.workCh <- &sealTask{block: block, results: results}
}
- var pend sync.WaitGroup
+ var (
+ pend sync.WaitGroup
+ locals = make(chan *types.Block)
+ )
for i := 0; i < threads; i++ {
pend.Add(1)
go func(id int, nonce uint64) {
defer pend.Done()
- ethash.mine(block, id, nonce, abort, ethash.resultCh)
+ ethash.mine(block, id, nonce, abort, locals)
}(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)
- case result = <-ethash.resultCh:
- // 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)
+ 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:
+ log.Warn("Sealing result is not read by miner", "mode", "local", "sealhash", ethash.SealHash(block.Header()))
+ }
+ 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 {
+ log.Error("Failed to restart sealing after update", "err", err)
+ }
+ }
+ // Wait for all miners to terminate and return the block
pend.Wait()
- return ethash.Seal(chain, block, stop)
- }
- // Wait for all miners to terminate and return the block
- pend.Wait()
- return result, nil
+ }()
+ return nil
}
// mine is the actual proof-of-work miner that searches for a nonce starting from
@@ -165,11 +186,12 @@ search:
}
// remote is a standalone goroutine to handle remote mining related stuff.
-func (ethash *Ethash) remote(notify []string) {
+func (ethash *Ethash) remote(notify []string, noverify bool) {
var (
works = make(map[common.Hash]*types.Block)
rates = make(map[common.Hash]hashrate)
+ results chan<- *types.Block
currentBlock *types.Block
currentWork [3]string
@@ -226,11 +248,15 @@ func (ethash *Ethash) remote(notify []string) {
// 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 {
+ submitWork := func(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool {
+ if currentBlock == nil {
+ log.Error("Pending work without block", "sealhash", sealhash)
+ return false
+ }
// Make sure the work submitted is present
- block := works[hash]
+ block := works[sealhash]
if block == nil {
- log.Info("Work submitted but none pending", "hash", hash)
+ log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", currentBlock.NumberU64())
return false
}
// Verify the correctness of submitted result.
@@ -239,26 +265,36 @@ func (ethash *Ethash) remote(notify []string) {
header.MixDigest = mixDigest
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)
- return false
+ if !noverify {
+ if err := ethash.verifySeal(nil, header, true); err != nil {
+ log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", time.Since(start), "err", err)
+ return false
+ }
}
- // Make sure the result channel is created.
- if ethash.resultCh == nil {
+ // Make sure the result channel is assigned.
+ if results == nil {
log.Warn("Ethash result channel is empty, submitted mining result is rejected")
return false
}
- log.Trace("Verified correct proof-of-work", "hash", hash, "elapsed", time.Since(start))
+ log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", time.Since(start))
// 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
+ solution := block.WithSeal(header)
+
+ // The submitted solution is within the scope of acceptance.
+ if solution.NumberU64()+staleThreshold > currentBlock.NumberU64() {
+ select {
+ case results <- solution:
+ log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
+ return true
+ default:
+ 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.
+ log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
+ return false
}
ticker := time.NewTicker(5 * time.Second)
@@ -266,14 +302,12 @@ func (ethash *Ethash) remote(notify []string) {
for {
select {
- case block := <-ethash.workCh:
- if currentBlock != nil && block.ParentHash() != currentBlock.ParentHash() {
- // Start new round mining, throw out all previous work.
- works = make(map[common.Hash]*types.Block)
- }
+ case work := <-ethash.workCh:
// Update current work with new received block.
// Note same work can be past twice, happens when changing CPU threads.
- makeWork(block)
+ results = work.results
+
+ makeWork(work.block)
// Notify and requested URLs of the new work availability
notifyWork()
@@ -315,6 +349,14 @@ func (ethash *Ethash) remote(notify []string) {
delete(rates, id)
}
}
+ // Clear stale pending blocks
+ if currentBlock != nil {
+ for hash, block := range works {
+ if block.NumberU64()+staleThreshold <= currentBlock.NumberU64() {
+ delete(works, hash)
+ }
+ }
+ }
case errc := <-ethash.exitCh:
// Exit remote loop if ethash is closed and return relevant error.
diff --git a/consensus/ethash/sealer_test.go b/consensus/ethash/sealer_test.go
index d1b66f9cf..31d18b67c 100644
--- a/consensus/ethash/sealer_test.go
+++ b/consensus/ethash/sealer_test.go
@@ -41,14 +41,14 @@ func TestRemoteNotify(t *testing.T) {
go server.Serve(listener)
// Create the custom ethash engine
- ethash := NewTester([]string{"http://" + listener.Addr().String()})
+ ethash := NewTester([]string{"http://" + listener.Addr().String()}, false)
defer ethash.Close()
// Stream a work task and ensure the notification bubbles out
header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)
- ethash.Seal(nil, block, nil)
+ ethash.Seal(nil, block, nil, nil)
select {
case work := <-sink:
if want := ethash.SealHash(header).Hex(); work[0] != want {
@@ -66,7 +66,7 @@ func TestRemoteNotify(t *testing.T) {
}
}
-// Tests that pushing work packages fast to the miner doesn't cause any daa race
+// Tests that pushing work packages fast to the miner doesn't cause any data race
// issues in the notifications.
func TestRemoteMultiNotify(t *testing.T) {
// Start a simple webserver to capture notifications
@@ -95,7 +95,7 @@ func TestRemoteMultiNotify(t *testing.T) {
go server.Serve(listener)
// Create the custom ethash engine
- ethash := NewTester([]string{"http://" + listener.Addr().String()})
+ ethash := NewTester([]string{"http://" + listener.Addr().String()}, false)
defer ethash.Close()
// Stream a lot of work task and ensure all the notifications bubble out
@@ -103,7 +103,7 @@ func TestRemoteMultiNotify(t *testing.T) {
header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)
- ethash.Seal(nil, block, nil)
+ ethash.Seal(nil, block, nil, nil)
}
for i := 0; i < cap(sink); i++ {
select {
@@ -113,3 +113,87 @@ func TestRemoteMultiNotify(t *testing.T) {
}
}
}
+
+// Tests whether stale solutions are correctly processed.
+func TestStaleSubmission(t *testing.T) {
+ ethash := NewTester(nil, true)
+ defer ethash.Close()
+ api := &API{ethash}
+
+ fakeNonce, fakeDigest := types.BlockNonce{0x01, 0x02, 0x03}, common.HexToHash("deadbeef")
+
+ testcases := []struct {
+ headers []*types.Header
+ submitIndex int
+ submitRes bool
+ }{
+ // Case1: submit solution for the latest mining package
+ {
+ []*types.Header{
+ {ParentHash: common.BytesToHash([]byte{0xa}), Number: big.NewInt(1), Difficulty: big.NewInt(100000000)},
+ },
+ 0,
+ true,
+ },
+ // Case2: submit solution for the previous package but have same parent.
+ {
+ []*types.Header{
+ {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000000)},
+ {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000001)},
+ },
+ 0,
+ true,
+ },
+ // Case3: submit stale but acceptable solution
+ {
+ []*types.Header{
+ {ParentHash: common.BytesToHash([]byte{0xc}), Number: big.NewInt(3), Difficulty: big.NewInt(100000000)},
+ {ParentHash: common.BytesToHash([]byte{0xd}), Number: big.NewInt(9), Difficulty: big.NewInt(100000000)},
+ },
+ 0,
+ true,
+ },
+ // Case4: submit very old solution
+ {
+ []*types.Header{
+ {ParentHash: common.BytesToHash([]byte{0xe}), Number: big.NewInt(10), Difficulty: big.NewInt(100000000)},
+ {ParentHash: common.BytesToHash([]byte{0xf}), Number: big.NewInt(17), Difficulty: big.NewInt(100000000)},
+ },
+ 0,
+ false,
+ },
+ }
+ results := make(chan *types.Block, 16)
+
+ for id, c := range testcases {
+ for _, h := range c.headers {
+ ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil)
+ }
+ if res := api.SubmitWork(fakeNonce, ethash.SealHash(c.headers[c.submitIndex]), fakeDigest); res != c.submitRes {
+ t.Errorf("case %d submit result mismatch, want %t, get %t", id+1, c.submitRes, res)
+ }
+ if !c.submitRes {
+ continue
+ }
+ select {
+ case res := <-results:
+ if res.Header().Nonce != fakeNonce {
+ t.Errorf("case %d block nonce mismatch, want %s, get %s", id+1, fakeNonce, res.Header().Nonce)
+ }
+ if res.Header().MixDigest != fakeDigest {
+ t.Errorf("case %d block digest mismatch, want %s, get %s", id+1, fakeDigest, res.Header().MixDigest)
+ }
+ if res.Header().Difficulty.Uint64() != c.headers[c.submitIndex].Difficulty.Uint64() {
+ t.Errorf("case %d block difficulty mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Difficulty, res.Header().Difficulty)
+ }
+ if res.Header().Number.Uint64() != c.headers[c.submitIndex].Number.Uint64() {
+ t.Errorf("case %d block number mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Number.Uint64(), res.Header().Number.Uint64())
+ }
+ if res.Header().ParentHash != c.headers[c.submitIndex].ParentHash {
+ t.Errorf("case %d block parent hash mismatch, want %s, get %s", id+1, c.headers[c.submitIndex].ParentHash.Hex(), res.Header().ParentHash.Hex())
+ }
+ case <-time.NewTimer(time.Second).C:
+ t.Errorf("case %d fetch ethash result timeout", id+1)
+ }
+ }
+}