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

Function testReorg

core/blockchain_test.go:324–354  ·  view source on GitHub ↗
(t *testing.T, first, second []int64, td int64)

Source from the content-addressed store, hash-verified

322}
323
324func testReorg(t *testing.T, first, second []int64, td int64) {
325 // Create a pristine chain and database
326 db := database.NewMemDatabase()
327 blockchain, err := newCanonical(fakeDpor(db), 0, db)
328 if err != nil {
329 t.Fatalf("failed to create pristine chain: %v", err)
330 }
331 defer blockchain.Stop()
332
333 remoteDB := database.NewIpfsDbWithAdapter(database.NewFakeIpfsAdapter())
334 // Insert an easy and a difficult chain afterwards
335 easyBlocks, _ := GenerateChain(configs.TestChainConfig, blockchain.CurrentBlock(), fakeDpor(db), db, remoteDB, len(first), func(i int, b *BlockGen) {
336 b.OffsetTime(first[i])
337 })
338 diffBlocks, _ := GenerateChain(configs.TestChainConfig, blockchain.CurrentBlock(), fakeDpor(db), db, remoteDB, len(second), func(i int, b *BlockGen) {
339 b.OffsetTime(second[i])
340 })
341 if _, err := blockchain.InsertChain(easyBlocks); err != nil {
342 t.Fatalf("failed to insert easy chain: %v", err)
343 }
344 if _, err := blockchain.InsertChain(diffBlocks); err != nil {
345 t.Fatalf("failed to insert difficult chain: %v", err)
346 }
347 // Check that the chain is valid number and link wise
348 prev := blockchain.CurrentBlock()
349 for block := blockchain.GetBlockByNumber(blockchain.CurrentBlock().NumberU64() - 1); block.NumberU64() != 0; prev, block = block, blockchain.GetBlockByNumber(block.NumberU64()-1) {
350 if prev.ParentHash() != block.Hash() {
351 t.Errorf("parent block hash mismatch: have %x, want %x", prev.ParentHash(), block.Hash())
352 }
353 }
354}
355
356// Tests that the insertion functions detect banned hashes.
357

Callers 1

TestReorgShortBlocksFunction · 0.85

Calls 11

newCanonicalFunction · 0.85
fakeDporFunction · 0.85
GenerateChainFunction · 0.85
OffsetTimeMethod · 0.80
NumberU64Method · 0.80
ParentHashMethod · 0.80
StopMethod · 0.65
CurrentBlockMethod · 0.65
InsertChainMethod · 0.65
GetBlockByNumberMethod · 0.65
HashMethod · 0.65

Tested by

no test coverage detected