aboutsummaryrefslogtreecommitdiffstats
path: root/core/blockchain_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/blockchain_test.go')
-rw-r--r--core/blockchain_test.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/core/blockchain_test.go b/core/blockchain_test.go
index c3e4d352d..de3ef0a9c 100644
--- a/core/blockchain_test.go
+++ b/core/blockchain_test.go
@@ -1090,3 +1090,41 @@ done:
}
}
+
+// Tests if the canonical block can be fetched from the database during chain insertion.
+func TestCanonicalBlockRetrieval(t *testing.T) {
+ var (
+ db, _ = ethdb.NewMemDatabase()
+ genesis = WriteGenesisBlockForTesting(db)
+ )
+
+ evmux := &event.TypeMux{}
+ blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
+
+ chain, _ := GenerateChain(nil, genesis, db, 10, func(i int, gen *BlockGen) {})
+
+ for i, _ := range chain {
+ go func(block *types.Block) {
+ // try to retrieve a block by its canonical hash and see if the block data can be retrieved.
+ for {
+ ch := GetCanonicalHash(db, block.NumberU64())
+ if ch == (common.Hash{}) {
+ continue // busy wait for canonical hash to be written
+ }
+ if ch != block.Hash() {
+ t.Fatalf("unknown canonical hash, want %s, got %s", block.Hash().Hex(), ch.Hex())
+ }
+ fb := GetBlock(db, ch, block.NumberU64())
+ if fb == nil {
+ t.Fatalf("unable to retrieve block %d for canonical hash: %s", block.NumberU64(), ch.Hex())
+ }
+ if fb.Hash() != block.Hash() {
+ t.Fatalf("invalid block hash for block %d, want %s, got %s", block.NumberU64(), block.Hash().Hex(), fb.Hash().Hex())
+ }
+ return
+ }
+ }(chain[i])
+
+ blockchain.InsertChain(types.Blocks{chain[i]})
+ }
+}