Merge bitcoin/bitcoin#36224: test: Add test coverage for `PartiallySignedTransaction::Merge()`
What changed, and why it matters
This commit only adds new unit tests for an existing PSBT merging function and renames some hard-coded bit numbers to named constants. It does not change any production code, so it cannot introduce a security vulnerability or fix one directly.
No security action required; treat as normal test-only improvement. Reviewers may optionally verify the test assertions accurately reflect the intended BIP370 merge semantics documented in src/psbt.cpp.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff is confined to src/test/psbt_tests.cpp. It introduces three new BOOST_AUTO_TEST_CASEs exercising PartiallySignedTransaction::Merge(): version mismatch rejection, fallback_locktime adoption rules, and m_tx_modifiable flag combination behavior. It also replaces literal bit indices 0/1/2 with named BIP370 constants. No changes are made to src/psbt.cpp or any other runtime code.
Changed components
src/test/psbt_tests.cppInspect captured patch +108 / −6
### src/test/psbt_tests.cpp
@@ -16,11 +16,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{
@@ -71,7 +77,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
@@ -96,12 +102,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
@@ -168,7 +174,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
@@ -180,11 +186,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);
@@ -314,4 +320,100 @@ BOOST_AUTO_TEST_CASE(update_psbt_output_taproot)
}
}
+BOOST_AUTO_TEST_CASE(psbt_merge_only_same_version)
+{
+ CMutableTransaction mtx;
+ PartiallySignedTransaction psbt_v0(mtx, /*version*/0);
+ PartiallySignedTransaction psbt_v2(mtx, /*version*/2);
+ BOOST_REQUIRE(psbt_v0.GetUniqueID() == psbt_v2.GetUniqueID());
+ BOOST_CHECK(!psbt_v0.Merge(psbt_v2));
+ BOOST_CHECK(!psbt_v2.Merge(psbt_v0));
+}
+
+BOOST_AUTO_TEST_CASE(psbt_merge_fallback_locktime)
+{
+ CMutableTransaction mtx;
+ PartiallySignedTransaction psbt1(mtx, /*version*/2);
+ PartiallySignedTransaction psbt2(mtx, /*version*/2);
+ psbt1.m_tx_modifiable.emplace();
+ psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
+ BOOST_REQUIRE_EQUAL(psbt1.inputs.size(), 0);
+ psbt2.m_tx_modifiable.emplace();
+ psbt2.m_tx_modifiable->set(INPUTS_MODIFIABLE);
+ BOOST_REQUIRE_EQUAL(psbt2.inputs.size(), 0);
+
+
+ PSBTInput psbtin(/*psbt_version=*/2, Txid::FromUint256(uint256::ONE), /*prev_out=*/0);
+ psbtin.height_locktime = 100;
+ BOOST_REQUIRE(psbt1.AddInput(psbtin));
+ BOOST_REQUIRE(psbt2.AddInput(psbtin));
+
+ psbt1.fallback_locktime = std::nullopt;
+ psbt2.fallback_locktime = 21;
+ BOOST_CHECK(psbt1.Merge(psbt2));
+ BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
+ BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 21);
+
+ psbt1.fallback_locktime = 5;
+ psbt2.fallback_locktime = 19;
+ BOOST_CHECK(psbt1.Merge(psbt2));
+ BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
+ BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 5);
+
+ psbt1.fallback_locktime = 21;
+ psbt2.fallback_locktime = std::nullopt;
+ BOOST_CHECK(psbt1.Merge(psbt2));
+ BOOST_REQUIRE(psbt1.fallback_locktime.has_value());
+ BOOST_CHECK_EQUAL(*psbt1.fallback_locktime, 21);
+}
+
+BOOST_AUTO_TEST_CASE(psbt_merge_modifiable)
+{
+ CMutableTransaction mtx;
+ PartiallySignedTransaction psbt1(mtx, /*version*/2);
+ PartiallySignedTransaction psbt2(mtx, /*version*/2);
+
+ BOOST_CHECK(psbt1.Merge(psbt2));
+ BOOST_CHECK(!psbt1.m_tx_modifiable.has_value());
+
+ psbt1.m_tx_modifiable.emplace();
+ 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(INPUTS_MODIFIABLE));
+
+ psbt1.m_tx_modifiable.reset();
+ psbt2.m_tx_modifiable.emplace();
+ 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(INPUTS_MODIFIABLE));
+ BOOST_CHECK(psbt1.m_tx_modifiable->test(HAS_SIGHASH_SINGLE));
+
+ psbt1.m_tx_modifiable.emplace();
+ 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(HAS_SIGHASH_SINGLE));
+
+ psbt1.m_tx_modifiable.reset();
+ psbt2.m_tx_modifiable.emplace();
+ 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(HAS_SIGHASH_SINGLE));
+
+ psbt1.m_tx_modifiable.emplace();
+ psbt1.m_tx_modifiable->set(INPUTS_MODIFIABLE);
+ psbt1.m_tx_modifiable->set(OUTPUTS_MODIFIABLE);
+ psbt2.m_tx_modifiable.emplace();
+ 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(INPUTS_MODIFIABLE) && psbt1.m_tx_modifiable->test(OUTPUTS_MODIFIABLE));
+}
+
BOOST_AUTO_TEST_SUITE_END()Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.