diff options
author | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-09-14 22:56:33 +0800 |
---|---|---|
committer | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-09-23 00:02:26 +0800 |
commit | bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7 (patch) | |
tree | 0b31f5fd7d553e0334cea767cab8df14badba690 /core/chain_manager_test.go | |
parent | 36f46a61a7147ff9f98774c6c478eb965a9bea9c (diff) | |
download | dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar.gz dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar.bz2 dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar.lz dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar.xz dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.tar.zst dexon-bfde1a4305817e76b203c2fd6da4d6a7ca41e5f7.zip |
core: Add BadHashErr and test for BadHashes handling
Diffstat (limited to 'core/chain_manager_test.go')
-rw-r--r-- | core/chain_manager_test.go | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 0c77fc138..6cfafb8c0 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -75,7 +75,7 @@ func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big if err != nil { t.Fatal("could not make new canonical in testFork", err) } - // asert the bmans have the same block at i + // assert the bmans have the same block at i bi1 := bman.bc.GetBlockByNumber(uint64(i)).Hash() bi2 := bman2.bc.GetBlockByNumber(uint64(i)).Hash() if bi1 != bi2 { @@ -421,6 +421,59 @@ func TestReorgLongest(t *testing.T) { } } +func TestBadHashes(t *testing.T) { + db, _ := ethdb.NewMemDatabase() + genesis, err := WriteTestNetGenesisBlock(db, 0) + if err != nil { + t.Error(err) + t.FailNow() + } + bc := chm(genesis, db) + + chain := makeChainWithDiff(genesis, []int{1, 2, 4}, 10) + BadHashes[chain[2].Header().Hash()] = true + + _, err = bc.InsertChain(chain) + if !IsBadHashError(err) { + t.Errorf("error mismatch: want: BadHashError, have: %v", err) + } +} + +func TestReorgBadHashes(t *testing.T) { + db, _ := ethdb.NewMemDatabase() + genesis, err := WriteTestNetGenesisBlock(db, 0) + if err != nil { + t.Error(err) + t.FailNow() + } + bc := chm(genesis, db) + + chain := makeChainWithDiff(genesis, []int{1, 2, 3, 4}, 11) + bc.InsertChain(chain) + + if chain[3].Header().Hash() != bc.LastBlockHash() { + t.Errorf("last block hash mismatch: want: %x, have: %x", chain[3].Header().Hash(), bc.LastBlockHash()) + } + + // NewChainManager should check BadHashes when loading it db + BadHashes[chain[3].Header().Hash()] = true + + var eventMux event.TypeMux + ncm, err := NewChainManager(db, FakePow{}, &eventMux) + if err != nil { + t.Errorf("NewChainManager err: %s", err) + } + + // check it set head to (valid) parent of bad hash block + if chain[2].Header().Hash() != ncm.LastBlockHash() { + t.Errorf("last block hash mismatch: want: %x, have: %x", chain[2].Header().Hash(), ncm.LastBlockHash()) + } + + if chain[2].Header().GasLimit.Cmp(ncm.GasLimit()) != 0 { + t.Errorf("current block gasLimit mismatch: want: %x, have: %x", chain[2].Header().GasLimit, ncm.GasLimit()) + } +} + func TestReorgShortest(t *testing.T) { db, _ := ethdb.NewMemDatabase() genesis, err := WriteTestNetGenesisBlock(db, 0) |