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

Code before:
    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

    // Trivial single transaction with no dust
    BOOST_CHECK(!CheckEphemeralSpends({dust_spend}, minrelay, pool).has_value());

    // Now with dust, ok because the tx has no dusty parents
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1}, minrelay, pool).has_value());

    // Dust checks pass
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool).has_value());
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool).has_value());

    auto dust_non_spend = make_tx({COutPoint{dust_txid, dust_index - 1}}, /*version=*/2);

    // Child spending non-dust only from parent should be disallowed even if dust otherwise spent
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, minrelay, pool).has_value());
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).has_value());
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).has_value());

    auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
    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
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool).has_value());

    auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2);
    // But if we spend from the parent, it must spend dust
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_non_spend_both_parents}, minrelay, pool).has_value());

    auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, minrelay, pool).has_value());

    // Spending other outputs is also correct, as long as the dusty one is spent
    const std::vector<COutPoint> all_outpoints{COutPoint(dust_txid, 0), COutPoint(dust_txid, 1), COutPoint(dust_txid, 2),
        COutPoint(dust_txid_2, 0), COutPoint(dust_txid_2, 1), COutPoint(dust_txid_2, 2)};
    auto dust_spend_all_outpoints = make_tx(all_outpoints, /*version=*/2);
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool).has_value());

    // 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);
    // Ok for parent to have dust
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool).has_value());
    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).has_value());

Review comment (suggestion):
In 60e18bb2dcd2c16a64e01023b793b0494cd6ca64:

nit: (Resolved in later commit) - `.value_or(null_txid)` seems unnecessary? Suggestion:
```C++
    BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, minrelay, pool), dust_non_spend_txid);
```
Changing to that convention makes the introduction of `null_txid` pointless.

Code after:
    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();

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

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

    // Trivial single transaction with no dust
    BOOST_CHECK(CheckEphemeralSpends({dust_spend}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, Txid());

    // Now with dust, ok because the tx has no dusty parents
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, Txid());

    // Dust checks pass
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool, child_state, child_txid));
    BOOST_CHECK(child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, Txid());
    BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, Txid());

    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()};
    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(!child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);
    child_state = TxValidationState();
    child_txid = Txid();

    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(!child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);
    child_state = TxValidationState();
    child_txid = Txid();

    BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, dustrelay, pool, child_state, child_txid));
    BOOST_CHECK(!child_state.IsValid());
    BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);