Skip to content

Commit bfde1a4

Browse files
author
Gustav Simonsson
committed
core: Add BadHashErr and test for BadHashes handling
1 parent 36f46a6 commit bfde1a4

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

core/chain_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
642642
}
643643

644644
if BadHashes[block.Hash()] {
645-
err := fmt.Errorf("Found known bad hash in chain %x", block.Hash())
645+
err := BadHashError(block.Hash())
646646
blockErr(block, err)
647647
return i, err
648648
}

core/chain_manager_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big
7575
if err != nil {
7676
t.Fatal("could not make new canonical in testFork", err)
7777
}
78-
// asert the bmans have the same block at i
78+
// assert the bmans have the same block at i
7979
bi1 := bman.bc.GetBlockByNumber(uint64(i)).Hash()
8080
bi2 := bman2.bc.GetBlockByNumber(uint64(i)).Hash()
8181
if bi1 != bi2 {
@@ -421,6 +421,59 @@ func TestReorgLongest(t *testing.T) {
421421
}
422422
}
423423

424+
func TestBadHashes(t *testing.T) {
425+
db, _ := ethdb.NewMemDatabase()
426+
genesis, err := WriteTestNetGenesisBlock(db, 0)
427+
if err != nil {
428+
t.Error(err)
429+
t.FailNow()
430+
}
431+
bc := chm(genesis, db)
432+
433+
chain := makeChainWithDiff(genesis, []int{1, 2, 4}, 10)
434+
BadHashes[chain[2].Header().Hash()] = true
435+
436+
_, err = bc.InsertChain(chain)
437+
if !IsBadHashError(err) {
438+
t.Errorf("error mismatch: want: BadHashError, have: %v", err)
439+
}
440+
}
441+
442+
func TestReorgBadHashes(t *testing.T) {
443+
db, _ := ethdb.NewMemDatabase()
444+
genesis, err := WriteTestNetGenesisBlock(db, 0)
445+
if err != nil {
446+
t.Error(err)
447+
t.FailNow()
448+
}
449+
bc := chm(genesis, db)
450+
451+
chain := makeChainWithDiff(genesis, []int{1, 2, 3, 4}, 11)
452+
bc.InsertChain(chain)
453+
454+
if chain[3].Header().Hash() != bc.LastBlockHash() {
455+
t.Errorf("last block hash mismatch: want: %x, have: %x", chain[3].Header().Hash(), bc.LastBlockHash())
456+
}
457+
458+
// NewChainManager should check BadHashes when loading it db
459+
BadHashes[chain[3].Header().Hash()] = true
460+
461+
var eventMux event.TypeMux
462+
ncm, err := NewChainManager(db, FakePow{}, &eventMux)
463+
if err != nil {
464+
t.Errorf("NewChainManager err: %s", err)
465+
}
466+
467+
// check it set head to (valid) parent of bad hash block
468+
if chain[2].Header().Hash() != ncm.LastBlockHash() {
469+
t.Errorf("last block hash mismatch: want: %x, have: %x", chain[2].Header().Hash(), ncm.LastBlockHash())
470+
}
471+
472+
if chain[2].Header().GasLimit.Cmp(ncm.GasLimit()) != 0 {
473+
t.Errorf("current block gasLimit mismatch: want: %x, have: %x", chain[2].Header().GasLimit, ncm.GasLimit())
474+
}
475+
}
476+
424477
func TestReorgShortest(t *testing.T) {
425478
db, _ := ethdb.NewMemDatabase()
426479
genesis, err := WriteTestNetGenesisBlock(db, 0)

core/error.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,14 @@ func IsValueTransferErr(e error) bool {
177177
_, ok := e.(*ValueTransferError)
178178
return ok
179179
}
180+
181+
type BadHashError common.Hash
182+
183+
func (h BadHashError) Error() string {
184+
return fmt.Sprintf("Found known bad hash in chain %x", h)
185+
}
186+
187+
func IsBadHashError(err error) bool {
188+
_, ok := err.(BadHashError)
189+
return ok
190+
}

0 commit comments

Comments
 (0)