Skip to content

Commit cf8a6d6

Browse files
core: tests for forked blocks retrievable by hash (#23695)
* Update tests to showcase that forked blocks can still be looked up by their hash
1 parent 088bc34 commit cf8a6d6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

core/blockchain_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,7 @@ func getLongAndShortChains() (bc *BlockChain, longChain []*types.Block, heavyCha
20552055
// 1. Have a chain [0 ... N .. X]
20562056
// 2. Reorg to shorter but heavier chain [0 ... N ... Y]
20572057
// 3. Then there should be no canon mapping for the block at height X
2058+
// 4. The forked block should still be retrievable by hash
20582059
func TestReorgToShorterRemovesCanonMapping(t *testing.T) {
20592060
chain, canonblocks, sideblocks, err := getLongAndShortChains()
20602061
if err != nil {
@@ -2064,6 +2065,7 @@ func TestReorgToShorterRemovesCanonMapping(t *testing.T) {
20642065
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
20652066
}
20662067
canonNum := chain.CurrentBlock().NumberU64()
2068+
canonHash := chain.CurrentBlock().Hash()
20672069
_, err = chain.InsertChain(sideblocks)
20682070
if err != nil {
20692071
t.Errorf("Got error, %v", err)
@@ -2079,6 +2081,12 @@ func TestReorgToShorterRemovesCanonMapping(t *testing.T) {
20792081
if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil {
20802082
t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64())
20812083
}
2084+
if blockByHash := chain.GetBlockByHash(canonHash); blockByHash == nil {
2085+
t.Errorf("expected block to be present: %x", blockByHash.Hash())
2086+
}
2087+
if headerByHash := chain.GetHeaderByHash(canonHash); headerByHash == nil {
2088+
t.Errorf("expected header to be present: %x", headerByHash.Hash())
2089+
}
20822090
}
20832091

20842092
// TestReorgToShorterRemovesCanonMappingHeaderChain is the same scenario
@@ -2098,6 +2106,7 @@ func TestReorgToShorterRemovesCanonMappingHeaderChain(t *testing.T) {
20982106
t.Fatalf("header %d: failed to insert into chain: %v", n, err)
20992107
}
21002108
canonNum := chain.CurrentHeader().Number.Uint64()
2109+
canonHash := chain.CurrentBlock().Hash()
21012110
sideHeaders := make([]*types.Header, len(sideblocks))
21022111
for i, block := range sideblocks {
21032112
sideHeaders[i] = block.Header()
@@ -2116,6 +2125,12 @@ func TestReorgToShorterRemovesCanonMappingHeaderChain(t *testing.T) {
21162125
if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil {
21172126
t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64())
21182127
}
2128+
if blockByHash := chain.GetBlockByHash(canonHash); blockByHash == nil {
2129+
t.Errorf("expected block to be present: %x", blockByHash.Hash())
2130+
}
2131+
if headerByHash := chain.GetHeaderByHash(canonHash); headerByHash == nil {
2132+
t.Errorf("expected header to be present: %x", headerByHash.Hash())
2133+
}
21192134
}
21202135

21212136
func TestTransactionIndices(t *testing.T) {

0 commit comments

Comments
 (0)