diff options
author | Bas van Kervel <bas@ethdev.com> | 2016-07-26 22:37:04 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2016-08-16 21:21:22 +0800 |
commit | bb8059f6aa86d1052d7c2dd75a6985982cb278f4 (patch) | |
tree | 44c57f98592518652f9791655be98c75fc198760 /core/blockchain_test.go | |
parent | 4c2cc32f2e279baa3059603b8c8a4329f31606f6 (diff) | |
download | dexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.gz dexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.zst dexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.zip |
core: ensure the canonical block is written before the canonical hash is set
Diffstat (limited to 'core/blockchain_test.go')
-rw-r--r-- | core/blockchain_test.go | 38 |
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]}) + } +} |