From cf8a6d61736b2985086eaf74e67299ba75aeaafa Mon Sep 17 00:00:00 2001 From: Miro Date: Mon, 11 Oct 2021 13:25:21 -0400 Subject: [PATCH] core: tests for forked blocks retrievable by hash (#23695) * Update tests to showcase that forked blocks can still be looked up by their hash --- core/blockchain_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 4c3291dc3..fd2f21471 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -2055,6 +2055,7 @@ func getLongAndShortChains() (bc *BlockChain, longChain []*types.Block, heavyCha // 1. Have a chain [0 ... N .. X] // 2. Reorg to shorter but heavier chain [0 ... N ... Y] // 3. Then there should be no canon mapping for the block at height X +// 4. The forked block should still be retrievable by hash func TestReorgToShorterRemovesCanonMapping(t *testing.T) { chain, canonblocks, sideblocks, err := getLongAndShortChains() if err != nil { @@ -2064,6 +2065,7 @@ func TestReorgToShorterRemovesCanonMapping(t *testing.T) { t.Fatalf("block %d: failed to insert into chain: %v", n, err) } canonNum := chain.CurrentBlock().NumberU64() + canonHash := chain.CurrentBlock().Hash() _, err = chain.InsertChain(sideblocks) if err != nil { t.Errorf("Got error, %v", err) @@ -2079,6 +2081,12 @@ func TestReorgToShorterRemovesCanonMapping(t *testing.T) { if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil { t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64()) } + if blockByHash := chain.GetBlockByHash(canonHash); blockByHash == nil { + t.Errorf("expected block to be present: %x", blockByHash.Hash()) + } + if headerByHash := chain.GetHeaderByHash(canonHash); headerByHash == nil { + t.Errorf("expected header to be present: %x", headerByHash.Hash()) + } } // TestReorgToShorterRemovesCanonMappingHeaderChain is the same scenario @@ -2098,6 +2106,7 @@ func TestReorgToShorterRemovesCanonMappingHeaderChain(t *testing.T) { t.Fatalf("header %d: failed to insert into chain: %v", n, err) } canonNum := chain.CurrentHeader().Number.Uint64() + canonHash := chain.CurrentBlock().Hash() sideHeaders := make([]*types.Header, len(sideblocks)) for i, block := range sideblocks { sideHeaders[i] = block.Header() @@ -2116,6 +2125,12 @@ func TestReorgToShorterRemovesCanonMappingHeaderChain(t *testing.T) { if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil { t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64()) } + if blockByHash := chain.GetBlockByHash(canonHash); blockByHash == nil { + t.Errorf("expected block to be present: %x", blockByHash.Hash()) + } + if headerByHash := chain.GetHeaderByHash(canonHash); headerByHash == nil { + t.Errorf("expected header to be present: %x", headerByHash.Hash()) + } } func TestTransactionIndices(t *testing.T) {