diff options
author | Martin Holst Swende <martin@swende.se> | 2019-02-04 18:54:39 +0800 |
---|---|---|
committer | Martin Holst Swende <martin@swende.se> | 2019-02-10 00:44:15 +0800 |
commit | 3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e (patch) | |
tree | 8f9c6ea86658c435b71d644ea19f3b15046704bb /core/blockchain_test.go | |
parent | 18f702faf7bcb9c7ca95a082b78aece2a4ed664f (diff) | |
download | dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar.gz dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar.bz2 dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar.lz dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar.xz dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.tar.zst dexon-3ab9dcc3bd17cdbb731ca7da829a3bd7b5e1bc1e.zip |
core: repro #18977
Diffstat (limited to 'core/blockchain_test.go')
-rw-r--r-- | core/blockchain_test.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 5ab29e205..641f1ecd4 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -1483,3 +1483,51 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) { benchmarkLargeNumberOfValueToNonexisting(b, numTxs, numBlocks, recipientFn, dataFn) } + +// Tests that importing a very large side fork, which is larger than the canon chain, +// but where the difficulty per block is kept low: this means that it will not +// overtake the 'canon' chain until after it's passed canon by about 200 blocks. +func TestLargeOldSidechainWithALowTdChain(t *testing.T) { + + // Generate a canonical chain to act as the main dataset + engine := ethash.NewFaker() + + db := ethdb.NewMemDatabase() + genesis := new(Genesis).MustCommit(db) + // We must use a pretty long chain to ensure that the fork + // doesn't overtake us until after at least 128 blocks post tip + blocks, _ := generateChain(params.TestChainConfig, genesis, engine, db, 6*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }, makeHeaderWithLargeDifficulty) + + // Import the canonical chain + diskdb := ethdb.NewMemDatabase() + new(Genesis).MustCommit(diskdb) + + chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil) + if err != nil { + t.Fatalf("failed to create tester chain: %v", err) + } + for i := 0; i < len(blocks); i++ { + if _, err := chain.InsertChain(blocks[i : i+1]); err != nil { + t.Fatalf("block %d: failed to insert into chain: %v", i, err) + } + } + // Dereference all the recent tries and ensure no past trie is left in + for i := 0; i < triesInMemory; i++ { + chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root()) + } + + // Generate fork chain, starting from an early block + parent := blocks[10] + fork, _ := generateChain(params.TestChainConfig, parent, engine, db, 256+6*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }, makeHeaderWithSmallDifficulty) + + // And now import the fork + if i, err := chain.InsertChain(fork); err != nil { + t.Fatalf("block %d: failed to insert into chain: %v", i, err) + } + head := chain.CurrentBlock() + if got := fork[len(fork)-1].Hash(); got != head.Hash() { + t.Fatalf("head wrong, expected %x got %x", head.Hash(), got) + } + td := chain.GetTd(head.Hash(), head.NumberU64()) + fmt.Printf("td %v", td) +} |