Repository: bitcoin/bitcoin
File: src/test/txvalidation_tests.cpp (C++)

Code before:
    CMutableTransaction mtx = CMutableTransaction{};
    mtx.version = version;
    mtx.vin.resize(inputs.size());
    mtx.vout.resize(25);
    for (size_t i{0}; i < inputs.size(); ++i) {
        mtx.vin[i].prevout = inputs[i];
    }
    for (auto i{0}; i < 25; ++i) {
        mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
        mtx.vout[i].nValue = 10000;
    }
    return MakeTransactionRef(mtx);
}

// Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
{
    CMutableTransaction mtx = CMutableTransaction{};
    mtx.version = version;
    mtx.vin.resize(inputs.size());
    mtx.vout.resize(3);
    for (size_t i{0}; i < inputs.size(); ++i) {
        mtx.vin[i].prevout = inputs[i];
    }
    for (auto i{0}; i < 3; ++i) {
        mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
        mtx.vout[i].nValue = (i == 2) ? 0 : 10000;
    }
    return MakeTransactionRef(mtx);
}

BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
{
    CTxMemPool& pool = *Assert(m_node.mempool);
    LOCK2(cs_main, pool.cs);
    TestMemPoolEntryHelper entry;
    CTxMemPool::setEntries empty_ancestors;

    CFeeRate minrelay(1000);

    // Basic transaction with dust
    auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
    const auto dust_txid = grandparent_tx_1->GetHash();

    uint32_t dust_index = 2;

    // Child transaction spending dust
    auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);

    // We first start with nothing "in the mempool", using package checks


Review comment (suggestion):
Should further link `dust_index` with the value used by callers of the function, as current version has a hidden dependency.

<details>
<summary>
Sending as parameter
</summary>

```diff
diff --git a/src/test/txvalidation_tests.cpp b/src/test/txvalidation_tests.cpp
index b362abfa1d..0a51ff411f 100644
--- a/src/test/txvalidation_tests.cpp
+++ b/src/test/txvalidation_tests.cpp
@@ -91,8 +91,10 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
 }
 
 // Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
-static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
+static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version, uint32_t dust_index)
 {
+    const uint32_t num_outputs{3};
+    assert(dust_index < num_outputs);
     CMutableTransaction mtx = CMutableTransaction{};
     mtx.version = version;
     mtx.vin.resize(inputs.size());
@@ -100,9 +102,7 @@ static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& in
     for (size_t i{0}; i < inputs.size(); ++i) {
         mtx.vin[i].prevout = inputs[i];
     }
-    int num_outputs{3};
-    int dust_index{num_outputs - 1};
-    for (auto i{0}; i < num_outputs; ++i) {
+    for (uint32_t i{0}; i < num_outputs; ++i) {
         mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
         mtx.vout[i].nValue = (i == dust_index) ? 0 : 10000;
     }
@@ -118,12 +118,12 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
 
     CFeeRate minrelay(1000);
 
+    const uint32_t dust_index = 2;
+
     // Basic transaction with dust
-    auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
+    auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2, dust_index);
     const auto dust_txid = grandparent_tx_1->GetHash();
 
-    uint32_t dust_index = 2;
-
     // Child transaction spending dust
     auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);
 
@@ -149,7 +149,7 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
     BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
 
-    auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
+    auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2, dust_index);
     const auto dust_txid_2 = grandparent_tx_2->GetHash();
 
     // Spend dust from one but not another is ok, as long as second grandparent has no child
@@ -169,14 +169,14 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool));
 
     // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
-    auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
+    auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2, dust_index);
     // Ok for parent to have dust
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
     auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
 
     // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
-    auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
+    auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2, dust_index);
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
 
     // Tests with parents in mempool
```
</details>

<details>
<summary>
File-local constant
</summary>

```diff
diff --git a/src/test/txvalidation_tests.cpp b/src/test/txvalidation_tests.cpp
index b362abfa1d..2309cd4f4d 100644
--- a/src/test/txvalidation_tests.cpp
+++ b/src/test/txvalidation_tests.cpp
@@ -90,6 +90,8 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
     return MakeTransactionRef(mtx);
 }
 
+static constexpr auto EPHEMERAL_DUST_INDEX = 2;
+
 // Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
 static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
 {
@@ -100,11 +102,11 @@ static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& in
     for (size_t i{0}; i < inputs.size(); ++i) {
         mtx.vin[i].prevout = inputs[i];
     }
-    int num_outputs{3};
-    int dust_index{num_outputs - 1};
-    for (auto i{0}; i < num_outputs; ++i) {
+    constexpr uint32_t num_outputs{3};
+    static_assert(EPHEMERAL_DUST_INDEX < num_outputs);
+    for (uint32_t i{0}; i < num_outputs; ++i) {
         mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
-        mtx.vout[i].nValue = (i == dust_index) ? 0 : 10000;
+        mtx.vout[i].nValue = (i == EPHEMERAL_DUST_INDEX) ? 0 : 10000;
     }
     return MakeTransactionRef(mtx);
 }
@@ -122,10 +124,8 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
     const auto dust_txid = grandparent_tx_1->GetHash();
 
-    uint32_t dust_index = 2;
-
     // Child transaction spending dust
-    auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);
+    auto dust_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
 
     // We first start with nothing "in the mempool", using package checks
 
@@ -139,7 +139,7 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool));
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool));
 
-    auto dust_non_spend = make_tx({COutPoint{dust_txid, dust_index - 1}}, /*version=*/2);
+    auto dust_non_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
 
     // Child spending non-dust only from parent should be disallowed even if dust otherwise spent
     const auto dust_non_spend_txid{dust_non_spend->GetHash()};
@@ -155,11 +155,11 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     // Spend dust from one but not another is ok, as long as second grandparent has no child
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool));
 
-    auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2);
+    auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
     // But if we spend from the parent, it must spend dust
     BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_non_spend_both_parents}, minrelay, pool).value_or(null_txid), dust_non_spend_both_parents->GetHash());
 
-    auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
+    auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, minrelay, pool));
 
     // Spending other outputs is also correct, as long as the dusty one is spent
@@ -169,14 +169,14 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool));
 
     // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
-    auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
+    auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
     // Ok for parent to have dust
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
-    auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
+    auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
 
     // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
-    auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
+    auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
     BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
 
     // Tests with parents in mempool
```

</details>

Code after:
    CMutableTransaction mtx = CMutableTransaction{};
    mtx.version = version;
    mtx.vin.resize(inputs.size());
    mtx.vout.resize(25);
    for (size_t i{0}; i < inputs.size(); ++i) {
        mtx.vin[i].prevout = inputs[i];
    }
    for (auto i{0}; i < 25; ++i) {
        mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
        mtx.vout[i].nValue = 10000;
    }
    return MakeTransactionRef(mtx);
}

static constexpr auto NUM_EPHEMERAL_TX_OUTPUTS = 3;
static constexpr auto EPHEMERAL_DUST_INDEX = NUM_EPHEMERAL_TX_OUTPUTS - 1;

// Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
{
    CMutableTransaction mtx = CMutableTransaction{};
    mtx.version = version;
    mtx.vin.resize(inputs.size());
    for (size_t i{0}; i < inputs.size(); ++i) {
        mtx.vin[i].prevout = inputs[i];
    }
    mtx.vout.resize(NUM_EPHEMERAL_TX_OUTPUTS);
    for (auto i{0}; i < NUM_EPHEMERAL_TX_OUTPUTS; ++i) {
        mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
        mtx.vout[i].nValue = (i == EPHEMERAL_DUST_INDEX) ? 0 : 10000;
    }
    return MakeTransactionRef(mtx);
}

BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
{
    CTxMemPool& pool = *Assert(m_node.mempool);
    LOCK2(cs_main, pool.cs);
    TestMemPoolEntryHelper entry;
    CTxMemPool::setEntries empty_ancestors;

    TxValidationState child_state;
    Txid child_txid;

    // Arbitrary non-0 feerate for these tests
    CFeeRate dustrelay(DUST_RELAY_TX_FEE);

    // Basic transaction with dust
    auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
    const auto dust_txid = grandparent_tx_1->GetHash();