test: harden arbitrary-parent block creation #36109

pull l0rinc wants to merge 4 commits into bitcoin:master from l0rinc:l0rinc/test-build-valid-fork-blocks changing 3 files +32 −15
  1. l0rinc commented at 12:17 AM on August 28, 2026: contributor

    Problem: While reviewing #35847, a few hardening opportunities came up in the test helpers that attach active-tip block templates to arbitrary parents. The copied work target and subsidy may describe a different child, and rebuilding a coinbase input script from heights 1 through 16 without padding violates its minimum length. Existing callers already avoid these cases or handle them locally, so no current test fails.

    Fix: Share the parent-dependent reconstruction between BuildChain() and MinerTestingSetup::Block(). Use the caller-selected timestamp, recalculate the work target and subsidy for the actual parent and height, and append a dummy OP_0 to low-height coinbase input scripts. Require the validation setup's completed block to retain the reconstructed locktime, script, and subsidy.

  2. test: extract fork block reconstruction
    `BuildChain` creates a block template for the active tip and then rewrites it for the requested parent.
    Move the existing parent-dependent field rewrites into a helper while passing the existing timestamp explicitly, without changing the generated fields or behavior.
    afcf26f0ef
  3. test: derive fork difficulty from parent
    `BuildChain` copies the work target from a block template for the active tip before attaching the block to its requested parent.
    Recalculate `nBits` from that parent and the rebuilt header so the target describes the resulting child.
    
    Current callers use regtest with retargeting disabled, so this prevents future invalid test cases rather than fixing an existing failure.
    4eee9d5275
  4. test: derive fork subsidy from parent
    `BuildChain` and `validation_block_tests::MinerTestingSetup::Block` attach active-tip templates to arbitrary parents, but retain the template subsidy.
    Derive the resulting height and subsidy in the shared reconstruction so both builders pay the correct coinbase reward.
    
    `MinerTestingSetup::Block` passes its existing monotonic timestamp into the reconstruction and asserts that the rebuilt coinbase retains the parent-height `nLockTime` and subsidy after customizing its outputs.
    
    Current callers do not cross a subsidy halving, so this prevents future invalid test cases rather than fixing an existing failure.
    b82a725a6c
  5. test: pad low-height fork coinbases
    `RebuildBlockForParent` replaces the template coinbase input script with the child height, which encodes to one byte at heights 1 through 16.
    Append the dummy `OP_0` in the shared reconstruction and replace the validation helper's duplicate padding with an assertion so both builders satisfy the two-byte minimum.
    6052be3135
  6. DrahtBot added the label Tests on Aug 28, 2026
  7. DrahtBot commented at 12:17 AM on August 28, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36109.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-08-31 17:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me