MCPcopy Create free account
hub / github.com/CPChain/chain / TestCanonicalBlockRetrieval

Function TestCanonicalBlockRetrieval

core/blockchain_test.go:711–753  ·  view source on GitHub ↗

Tests if the canonical block can be fetched from the database during chain insertion.

(t *testing.T)

Source from the content-addressed store, hash-verified

709
710// Tests if the canonical block can be fetched from the database during chain insertion.
711func TestCanonicalBlockRetrieval(t *testing.T) {
712 db := database.NewMemDatabase()
713 blockchain, err := newCanonical(fakeDpor(db), 0, db)
714 if err != nil {
715 t.Fatalf("failed to create pristine chain: %v", err)
716 }
717 defer blockchain.Stop()
718
719 chain, _ := GenerateChain(blockchain.chainConfig, blockchain.genesisBlock, fakeDpor(db), blockchain.db, blockchain.remoteDB, 10, func(i int, gen *BlockGen) {})
720
721 var pend sync.WaitGroup
722 pend.Add(len(chain))
723
724 for i := range chain {
725 go func(block *types.Block) {
726 defer pend.Done()
727
728 // try to retrieve a block by its canonical hash and see if the block data can be retrieved.
729 for {
730 ch := rawdb.ReadCanonicalHash(blockchain.db, block.NumberU64())
731 if ch == (common.Hash{}) {
732 continue // busy wait for canonical hash to be written
733 }
734 if ch != block.Hash() {
735 t.Fatalf("unknown canonical hash, want %s, got %s", block.Hash().Hex(), ch.Hex())
736 }
737 fb := rawdb.ReadBlock(blockchain.db, ch, block.NumberU64())
738 if fb == nil {
739 t.Fatalf("unable to retrieve block %d for canonical hash: %s", block.NumberU64(), ch.Hex())
740 }
741 if fb.Hash() != block.Hash() {
742 t.Fatalf("invalid block hash for block %d, want %s, got %s", block.NumberU64(), block.Hash().Hex(), fb.Hash().Hex())
743 }
744 return
745 }
746 }(chain[i])
747
748 if _, err := blockchain.InsertChain(types.Blocks{chain[i]}); err != nil {
749 t.Fatalf("failed to insert block %d: %v", i, err)
750 }
751 }
752 pend.Wait()
753}
754
755// This is a regression test (i.e. as weird as it is, don't delete it ever), which
756// tests that under weird reorg conditions the blockchain and its internal header-

Callers

nothing calls this directly

Calls 9

newCanonicalFunction · 0.85
fakeDporFunction · 0.85
GenerateChainFunction · 0.85
NumberU64Method · 0.80
WaitMethod · 0.80
StopMethod · 0.65
AddMethod · 0.65
HashMethod · 0.65
InsertChainMethod · 0.65

Tested by

no test coverage detected