Skip to content

Commit 0b49391

Browse files
committed
core: fix the nonce check one more time
The block nonce verification was effectively disabled by a typo. This time, there is an actual test for it.
1 parent 43ceb0f commit 0b49391

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎core/chain_manager.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,12 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
551551
bstart := time.Now()
552552
// Wait for block i's nonce to be verified before processing
553553
// its state transition.
554-
for nonceChecked[i] {
554+
for !nonceChecked[i] {
555555
r := <-nonceDone
556556
nonceChecked[r.i] = true
557557
if !r.valid {
558-
block := chain[i]
559-
return i, ValidationError("Block (#%v / %x) nonce is invalid (= %x)", block.Number(), block.Hash(), block.Nonce)
558+
block := chain[r.i]
559+
return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()}
560560
}
561561
}
562562

‎core/chain_manager_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"fmt"
55
"math/big"
6+
"math/rand"
67
"os"
78
"path/filepath"
89
"runtime"
@@ -426,3 +427,55 @@ func TestReorgShortest(t *testing.T) {
426427
}
427428
}
428429
}
430+
431+
func TestInsertNonceError(t *testing.T) {
432+
for i := 1; i < 25 && !t.Failed(); i++ {
433+
db, _ := ethdb.NewMemDatabase()
434+
genesis := GenesisBlock(db)
435+
bc := chm(genesis, db)
436+
bc.processor = NewBlockProcessor(db, db, bc.pow, bc, bc.eventMux)
437+
blocks := makeChain(bc.processor.(*BlockProcessor), bc.currentBlock, i, db, 0)
438+
439+
fail := rand.Int() % len(blocks)
440+
failblock := blocks[fail]
441+
bc.pow = failpow{failblock.NumberU64()}
442+
n, err := bc.InsertChain(blocks)
443+
444+
// Check that the returned error indicates the nonce failure.
445+
if n != fail {
446+
t.Errorf("(i=%d) wrong failed block index: got %d, want %d", i, n, fail)
447+
}
448+
if !IsBlockNonceErr(err) {
449+
t.Fatalf("(i=%d) got %q, want a nonce error", i, err)
450+
}
451+
nerr := err.(*BlockNonceErr)
452+
if nerr.Number.Cmp(failblock.Number()) != 0 {
453+
t.Errorf("(i=%d) wrong block number in error, got %v, want %v", i, nerr.Number, failblock.Number())
454+
}
455+
if nerr.Hash != failblock.Hash() {
456+
t.Errorf("(i=%d) wrong block hash in error, got %v, want %v", i, nerr.Hash, failblock.Hash())
457+
}
458+
459+
// Check that all no blocks after the failing block have been inserted.
460+
for _, block := range blocks[fail:] {
461+
if bc.HasBlock(block.Hash()) {
462+
t.Errorf("(i=%d) invalid block %d present in chain", i, block.NumberU64())
463+
}
464+
}
465+
}
466+
}
467+
468+
// failpow returns false from Verify for a certain block number.
469+
type failpow struct{ num uint64 }
470+
471+
func (pow failpow) Search(pow.Block, <-chan struct{}) (nonce uint64, mixHash []byte) {
472+
return 0, nil
473+
}
474+
func (pow failpow) Verify(b pow.Block) bool {
475+
return b.NumberU64() != pow.num
476+
}
477+
func (pow failpow) GetHashrate() int64 {
478+
return 0
479+
}
480+
func (pow failpow) Turbo(bool) {
481+
}

‎core/error.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ func IsNonceErr(err error) bool {
9090
return ok
9191
}
9292

93+
// BlockNonceErr indicates that a block's nonce is invalid.
94+
type BlockNonceErr struct {
95+
Number *big.Int
96+
Hash common.Hash
97+
Nonce uint64
98+
}
99+
100+
func (err *BlockNonceErr) Error() string {
101+
return fmt.Sprintf("block %d (%v) nonce is invalid (got %d)", err.Number, err.Hash, err.Nonce)
102+
}
103+
104+
// IsBlockNonceErr returns true for invalid block nonce errors.
105+
func IsBlockNonceErr(err error) bool {
106+
_, ok := err.(*BlockNonceErr)
107+
return ok
108+
}
109+
93110
type InvalidTxErr struct {
94111
Message string
95112
}

0 commit comments

Comments
 (0)