@@ -3,6 +3,7 @@ package core
33import (
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+ }
0 commit comments