|
| 1 | +// Copyright (c) 2013-2026 The btcsuite developers |
| 2 | +// Use of this source code is governed by an ISC |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package blockchain |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/blockchain/internal/testhelper" |
| 11 | + "github.com/btcsuite/btcd/btcutil" |
| 12 | +) |
| 13 | + |
| 14 | +// TestMaybeAcceptBlockReusesHeaderNode ensures that when a block header is |
| 15 | +// processed first via ProcessBlockHeader and later the full block arrives via |
| 16 | +// ProcessBlock, the existing blockNode pointer is reused rather than replaced. |
| 17 | +// Replacing the pointer would orphan the entry held by bestHeader's chainView, |
| 18 | +// causing bestHeader.Contains(index.LookupNode(hash)) to return false and |
| 19 | +// breaking IsValidHeader and downstream netsync checks. |
| 20 | +func TestMaybeAcceptBlockReusesHeaderNode(t *testing.T) { |
| 21 | + chain, params, tearDown := utxoCacheTestChain( |
| 22 | + "TestMaybeAcceptBlockReusesHeaderNode") |
| 23 | + defer tearDown() |
| 24 | + |
| 25 | + // Build a base chain of 3 blocks. |
| 26 | + // |
| 27 | + // genesis -> 1 -> 2 -> 3 |
| 28 | + tip := btcutil.NewBlock(params.GenesisBlock) |
| 29 | + _, _, err := addBlocks(3, chain, tip, []*testhelper.SpendableOut{}) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("failed to build base chain: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Create block 4 without processing it. |
| 35 | + prevBlock, err := chain.BlockByHeight(3) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("failed to get block at height 3: %v", err) |
| 38 | + } |
| 39 | + block4, _, err := newBlock(chain, prevBlock, nil) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("failed to create block 4: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Process block 4's header first. |
| 45 | + block4Hash := block4.Hash() |
| 46 | + _, err = chain.ProcessBlockHeader( |
| 47 | + &block4.MsgBlock().Header, BFNone, false) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("ProcessBlockHeader fail: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Capture the header-only node pointer from the index. |
| 53 | + headerNode := chain.index.LookupNode(block4Hash) |
| 54 | + if headerNode == nil { |
| 55 | + t.Fatal("header node not found in block index") |
| 56 | + } |
| 57 | + |
| 58 | + // Now process the full block. |
| 59 | + _, _, err = chain.ProcessBlock(block4, BFNone) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("ProcessBlock fail: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + // The index must still hold the same pointer that bestHeader has. |
| 65 | + // Before the fix, maybeAcceptBlock would create a fresh node and |
| 66 | + // overwrite the index entry, orphaning the pointer in bestHeader. |
| 67 | + fullBlockNode := chain.index.LookupNode(block4Hash) |
| 68 | + if fullBlockNode != headerNode { |
| 69 | + t.Fatal("ProcessBlock replaced the header node pointer " + |
| 70 | + "instead of reusing it") |
| 71 | + } |
| 72 | + if !chain.bestHeader.Contains(fullBlockNode) { |
| 73 | + t.Fatal("node no longer in bestHeader after ProcessBlock") |
| 74 | + } |
| 75 | +} |
0 commit comments