test: Add test coverage for `PartiallySignedTransaction::Merge()` #36224

pull nebula-21 wants to merge 3 commits into bitcoin:master from nebula-21:merge-psbt-coverage changing 1 files +108 −6
  1. nebula-21 commented at 11:24 PM on September 10, 2026: contributor

    This PR adds test coverage for PartiallySignedTransaction::Merge() divided into 3 tests:

    Only merge PSBTs with same version: The test can be triggered with this mutant as an example:

    diff --git a/src/psbt.cpp b/src/psbt.cpp
    index 51fb19591a..cf5eef0089 100644
    --- a/src/psbt.cpp
    +++ b/src/psbt.cpp
    @@ -43,7 +43,7 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
             return false;
         }
         if (GetVersion() != psbt.GetVersion()) {
    -        return false;
    +        return true;
         }
    
         for (unsigned int i = 0; i < inputs.size(); ++i) {
    

    Merge PSBT fallback_locktime coverage: If the psbt has no fallback locktime, it adopts the one from the psbt it is merging. The test can be triggered with this mutant as an example:

    diff --git a/src/psbt.cpp b/src/psbt.cpp
    index 51fb19591a..ce909eb062 100644
    --- a/src/psbt.cpp
    +++ b/src/psbt.cpp
    @@ -53,7 +53,7 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
             outputs[i].Merge(psbt.outputs[i]);
         }
         MergeGlobalXPubs(psbt);
    -    if (fallback_locktime == std::nullopt && psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime;
    +    if (fallback_locktime == std::nullopt || psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime;
     
         // Set m_tx_modifiable only if either PSBT had it set
         if (m_tx_modifiable.has_value() || psbt.m_tx_modifiable.has_value()) {
    

    While writing this test I had a question: If both PSBTs have a fallback_locktime value, the "this" (receiving side) wins silently. Is this the expected behavior? Why not keep the max or directly fail merging if we have two conflicting fallback_locktimes? The test can be triggered with this mutant as one of the various examples:

    diff --git a/src/psbt.cpp b/src/psbt.cpp
    index 51fb19591a..29eb910014 100644
    --- a/src/psbt.cpp
    +++ b/src/psbt.cpp
    @@ -56,7 +56,7 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
         if (fallback_locktime == std::nullopt && psbt.fallback_locktime != std::nullopt) fallback_locktime = psbt.fallback_locktime;
     
         // Set m_tx_modifiable only if either PSBT had it set
    -    if (m_tx_modifiable.has_value() || psbt.m_tx_modifiable.has_value()) {
    +    if (m_tx_modifiable.has_value() && psbt.m_tx_modifiable.has_value()) {
             // In general, we AND the modifiable flags
             std::bitset<8> this_modifiable = m_tx_modifiable.value_or(0);
             std::bitset<8> psbt_modifiable = psbt.m_tx_modifiable.value_or(0);
    
  2. DrahtBot added the label Tests on Sep 10, 2026
  3. DrahtBot commented at 11:24 PM on September 10, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK polespinasa, fametrano, sedited

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36122 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36122.svg"></sub> (BIP460: CISA for Taproot key path spends by fjahr)
    • #35747 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35747.svg"></sub> (wallet: Fix FillPSBT failing to sign owned inputs when UTXOs disagree by nervana21)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. nebula-21 force-pushed on Sep 10, 2026
  5. DrahtBot added the label CI failed on Sep 10, 2026
  6. DrahtBot commented at 11:33 PM on September 10, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task lint: https://github.com/bitcoin/bitcoin/actions/runs/34542023845/job/103086555994</sub> <sub>LLM reason (✨ experimental): CI failed due to a trailing_whitespace lint check error (trailing space detected in src/test/psbt_tests.cpp).</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  7. DrahtBot removed the label CI failed on Sep 11, 2026
  8. sedited requested review from polespinasa on Sep 25, 2026
  9. in src/test/psbt_tests.cpp:321 in 0d21098e27
     312 | @@ -313,4 +313,14 @@ BOOST_AUTO_TEST_CASE(update_psbt_output_taproot)
     313 |      }
     314 |  }
     315 |  
     316 | +BOOST_AUTO_TEST_CASE(psbt_merge_only_same_version)
     317 | +{
     318 | +    CMutableTransaction mtx;
     319 | +    PartiallySignedTransaction psbt_v0(mtx, /*version*/0);
     320 | +    PartiallySignedTransaction psbt_v2(mtx, /*version*/2);
     321 | +    BOOST_REQUIRE(*psbt_v0.GetUniqueID() == *psbt_v2.GetUniqueID());
    


    polespinasa commented at 7:56 PM on September 28, 2026:

    in 0d21098e27dff9dc930a1d1ce9f72d82f37aef89 test: only merge PSBTs with same version

        BOOST_REQUIRE(psbt_v0.GetUniqueID() == psbt_v2.GetUniqueID());
    
    
  10. in src/test/psbt_tests.cpp:363 in 4a5a082d30 outdated
     359 | @@ -360,4 +360,43 @@ BOOST_AUTO_TEST_CASE(psbt_merge_fallback_locktime)
     360 |      BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 21);
     361 |  }
     362 |  
     363 | +BOOST_AUTO_TEST_CASE(psbt_merge_modifiable)
    


    polespinasa commented at 8:00 PM on September 28, 2026:

    in 4a5a082d306d9b4eab4ea9db6643efc307a9e961 test: merge PSBT m_tx_modifiable coverage

    maybe can increase the coverage with positive and case?

    psbt1.m_tx_modifiable.emplace();
    psbt1.m_tx_modifiable->set(0, true);
    psbt1.m_tx_modifiable->set(1, true);
    psbt2.m_tx_modifiable.emplace();
    psbt2.m_tx_modifiable->set(0, true);
    psbt2.m_tx_modifiable->set(1, true);
    BOOST_CHECK(psbt1.Merge(psbt2));
    BOOST_CHECK(psbt1.m_tx_modifiable->test(0) && psbt1.m_tx_modifiable->test(1));
    

    nebula-21 commented at 11:21 AM on October 1, 2026:

    Thanks! I’ve made the change and added you as a co-author.

  11. polespinasa commented at 8:02 PM on September 28, 2026: member

    reviewed 4a5a082d306d9b4eab4ea9db6643efc307a9e961

  12. test: only merge PSBTs with same version 6fc2ebd521
  13. nebula-21 force-pushed on Oct 1, 2026
  14. test: merge PSBT `fallback_locktime` coverage 776fb26375
  15. nebula-21 force-pushed on Oct 1, 2026
  16. nebula-21 force-pushed on Oct 1, 2026
  17. DrahtBot added the label CI failed on Oct 1, 2026
  18. DrahtBot commented at 11:25 AM on October 1, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task lint: https://github.com/bitcoin/bitcoin/actions/runs/36854851942/job/110344798532</sub> <sub>LLM reason (✨ experimental): CI failed due to a commit_msg lint error: commit subject line (60edcd1ea8…) was not followed by a blank line before the body.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  19. DrahtBot removed the label CI failed on Oct 1, 2026
  20. sedited requested review from polespinasa on Oct 21, 2026
  21. polespinasa commented at 7:02 PM on November 3, 2026: member

    ACK 9b78edce891f717c562c8c62d082c3a3facb43fa

    One non-blocking comment that I think is worth taking :) The m_tx_modifiable->set(..., ...) call is not self-explanatori at all, it is difficult to know what each value is without needing to go check BIP370. I think it would be more readable with something like the following diff:

    <details> <summary>diff</summary>

    $ git diff
    diff --git a/src/test/psbt_tests.cpp b/src/test/psbt_tests.cpp
    index 7af93dd484..d7f4b9d2ff 100644
    --- a/src/test/psbt_tests.cpp
    +++ b/src/test/psbt_tests.cpp
    @@ -15,11 +15,17 @@
     
     #include <boost/test/unit_test.hpp>
     
    +#include <cstddef>
     #include <string>
     #include <vector>
     
     BOOST_FIXTURE_TEST_SUITE(psbt_tests, BasicTestingSetup)
     
    +// Bit positions in BIP 370's transaction modifiable flags.
    +static constexpr std::size_t INPUTS_MODIFIABLE{0};
    +static constexpr std::size_t OUTPUTS_MODIFIABLE{1};
    +static constexpr std::size_t HAS_SIGHASH_SINGLE{2};
    +
     static PSBTProprietary MakeProprietary(uint64_t subtype, uint8_t key_data, uint8_t value)
     {
         return PSBTProprietary{
    @@ -70,7 +76,7 @@ BOOST_AUTO_TEST_CASE(psbt2_addinput)
         CMutableTransaction mtx;
         PartiallySignedTransaction psbt(mtx, /*version=*/2);
         psbt.m_tx_modifiable.emplace();
    -    psbt.m_tx_modifiable->set(0, true);
    +    psbt.m_tx_modifiable->set(INPUTS_MODIFIABLE);
         BOOST_CHECK_EQUAL(psbt.inputs.size(), 0);
     
         // Same PSBT version is required
    @@ -95,12 +101,12 @@ BOOST_AUTO_TEST_CASE(psbt2_addinput)
         BOOST_CHECK_EQUAL(psbt.inputs.size(), 2);
     
         // Disabling inputs modifiable flag prevents adding new inputs
    -    psbt.m_tx_modifiable->set(0, false);
    +    psbt.m_tx_modifiable->reset(INPUTS_MODIFIABLE);
         rng.fillrand(MakeWritableByteSpan(txid));
         PSBTInput psbtin3(/*psbt_version=*/2, Txid::FromUint256(txid), /*prev_out=*/0);
         BOOST_CHECK(!psbt.AddInput(psbtin3));
         BOOST_CHECK_EQUAL(psbt.inputs.size(), 2);
    -    psbt.m_tx_modifiable->set(0, true);
    +    psbt.m_tx_modifiable->set(INPUTS_MODIFIABLE);
     
         // Make sure that timelock compatibility checks are working
         // No previous required timelocks, new input with both height and time timelocks is allowed
    @@ -167,7 +173,7 @@ BOOST_AUTO_TEST_CASE(psbt2_addoutput)
         CMutableTransaction mtx;
         PartiallySignedTransaction psbt(mtx, /*version=*/2);
         psbt.m_tx_modifiable.emplace();
    -    psbt.m_tx_modifiable->set(1, true);
    +    psbt.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
         BOOST_CHECK_EQUAL(psbt.outputs.size(), 0);
     
         // Same PSBT version is required
    @@ -179,11 +185,11 @@ BOOST_AUTO_TEST_CASE(psbt2_addoutput)
         BOOST_CHECK_EQUAL(psbt.outputs.size(), 1);
     
         // Disabling outputs modifiable flag prevents adding new outputs
    -    psbt.m_tx_modifiable->set(1, false);
    +    psbt.m_tx_modifiable->reset(OUTPUTS_MODIFIABLE);
         PSBTOutput psbtout2(/*psbt_version=*/2, /*amount=*/1, CScript());
         BOOST_CHECK(!psbt.AddOutput(psbtout2));
         BOOST_CHECK_EQUAL(psbt.outputs.size(), 1);
    -    psbt.m_tx_modifiable->set(1, true);
    +    psbt.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
         PSBTOutput psbtout3(/*psbt_version=*/2, /*amount=*/1, CScript());
         BOOST_CHECK(psbt.AddOutput(psbtout3));
         BOOST_CHECK_EQUAL(psbt.outputs.size(), 2);
    @@ -329,10 +335,10 @@ BOOST_AUTO_TEST_CASE(psbt_merge_fallback_locktime)
         PartiallySignedTransaction psbt1(mtx, /*version*/2);
         PartiallySignedTransaction psbt2(mtx, /*version*/2);
         psbt1.m_tx_modifiable.emplace();
    -    psbt1.m_tx_modifiable->set(0, true);
    +    psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
         BOOST_REQUIRE_EQUAL(psbt1.inputs.size(), 0);
         psbt2.m_tx_modifiable.emplace();
    -    psbt2.m_tx_modifiable->set(0, true);
    +    psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
         BOOST_REQUIRE_EQUAL(psbt2.inputs.size(), 0);
     
     
    @@ -370,43 +376,43 @@ BOOST_AUTO_TEST_CASE(psbt_merge_modifiable)
         BOOST_CHECK(!psbt1.m_tx_modifiable.has_value());
     
         psbt1.m_tx_modifiable.emplace();
    -    psbt1.m_tx_modifiable->set(0, true);
    +    psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
         BOOST_CHECK(psbt1.Merge(psbt2));
         BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
    -    BOOST_CHECK(!psbt1.m_tx_modifiable->test(0));
    +    BOOST_CHECK(!psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE));
     
         psbt1.m_tx_modifiable.reset();
         psbt2.m_tx_modifiable.emplace();
    -    psbt2.m_tx_modifiable->set(0, true);
    -    psbt2.m_tx_modifiable->set(2, true);
    +    psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
    +    psbt2.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
         BOOST_CHECK(psbt1.Merge(psbt2));
         BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
    -    BOOST_CHECK(!psbt1.m_tx_modifiable->test(0));
    -    BOOST_CHECK(psbt1.m_tx_modifiable->test(2));
    +    BOOST_CHECK(!psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE));
    +    BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
     
         psbt1.m_tx_modifiable.emplace();
    -    psbt1.m_tx_modifiable->set(2, true);
    +    psbt1.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
         psbt2.m_tx_modifiable.reset();
         BOOST_CHECK(psbt1.Merge(psbt2));
         BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
    -    BOOST_CHECK(psbt1.m_tx_modifiable->test(2));
    +    BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
     
         psbt1.m_tx_modifiable.reset();
         psbt2.m_tx_modifiable.emplace();
    -    psbt2.m_tx_modifiable->set(2, true);
    +    psbt2.m_tx_modifiable->set(HAS_SIGHASH_SINGLE);
         BOOST_CHECK(psbt1.Merge(psbt2));
         BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
    -    BOOST_CHECK(psbt1.m_tx_modifiable->test(2));
    +    BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
     
         psbt1.m_tx_modifiable.emplace();
    -    psbt1.m_tx_modifiable->set(0, true);
    -    psbt1.m_tx_modifiable->set(1, true);
    +    psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
    +    psbt1.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
         psbt2.m_tx_modifiable.emplace();
    -    psbt2.m_tx_modifiable->set(0, true);
    -    psbt2.m_tx_modifiable->set(1, true);
    +    psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
    +    psbt2.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
         BOOST_CHECK(psbt1.Merge(psbt2));
         BOOST_REQUIRE(psbt1.m_tx_modifiable.has_value());
    -    BOOST_CHECK(psbt1.m_tx_modifiable->test(0) && psbt1.m_tx_modifiable->test(1));
    +    BOOST_CHECK(psbt1.m_tx_modifiable->test(INPUTS_MODIFIABLE) && psbt1.m_tx_modifiable->test(OUTPUTS_MODIFIABLE));
     }
     
     BOOST_AUTO_TEST_SUITE_END()
    

    </details>

  22. fametrano commented at 9:06 PM on November 5, 2026: contributor

    tACK 9b78edce891f717c562c8c62d082c3a3facb43fa

    On your fallback_locktime question: which value is kept does not change the transaction. GetUniqueID() hashes nLockTime, so where both fallbacks set it and differ, Merge returns false. Different fallbacks get through only when a required lock time on an input sets the merged nLockTime, and then the fallback is not used.

    I combined 5000 random PSBTv2 pairs in both orders with combinepsbt and with btclib-wallet. Both refused all 155 pairs with different fallbacks and no required lock time. In the 1342 accepted pairs with different fallbacks, the order changed the kept fallback but never nLockTime (script and output).

  23. sedited commented at 11:51 AM on November 6, 2026: contributor

    @nebula-21 do you want to take @polespinasa's suggestion?

  24. test: merge PSBT `m_tx_modifiable` coverage and renaming bit positions with BIP370 flags
    Co-authored-by: polespinasa <pol.espinasa@uab.cat>
    7b12418439
  25. nebula-21 force-pushed on Nov 6, 2026
  26. nebula-21 commented at 7:38 PM on November 6, 2026: contributor

    @nebula-21 do you want to take @polespinasa's suggestion?

    yup took the suggestion to improve readability :)

  27. polespinasa commented at 12:36 PM on November 9, 2026: member

    ACK 7b124184394e016d6e3c119c50ff08d1512dfab9

  28. DrahtBot requested review from fametrano on Nov 9, 2026
  29. fametrano commented at 9:35 PM on November 10, 2026: contributor

    reACK 7b124184394e016d6e3c119c50ff08d1512dfab9

    The only change since 9b78edce is polespinasa's suggested diff, applied as posted: the bit indices of m_tx_modifiable become named constants. Their values match BIP370 (bit 0 inputs modifiable, bit 1 outputs modifiable, bit 2 has SIGHASH_SINGLE) and the bits psbt.cpp and decodepsbt read. psbt_tests pass on this head, and no non-test code changed, so the earlier combinepsbt comparison still holds.

  30. sedited approved
  31. sedited commented at 1:54 PM on November 12, 2026: contributor

    ACK 7b124184394e016d6e3c119c50ff08d1512dfab9

  32. sedited merged this on Nov 12, 2026
  33. sedited closed this on Nov 12, 2026

  34. sedited commented at 2:12 PM on November 12, 2026: contributor

    @nebula-21 please de-slop your pull request descriptions in the future. It contained a bunch of redundant and unnecessary descriptions and the formatting is annoying to read on a terminal. Remember that the description gets added to the git log.


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-11-16 00:51 UTC

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