Skip to content

Commit

Permalink
core: tests for forked blocks retrievable by hash (ethereum#23695)
Browse files Browse the repository at this point in the history
* Update tests to showcase that forked blocks can still be looked up by their hash
  • Loading branch information
mirokuratczyk authored and zzyalbert committed Nov 26, 2021
1 parent d6edb38 commit 8c705f5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 8c705f5

Please sign in to comment.