Should further link dust_index
with the value used by callers of the function, as current version has a hidden dependency.
0diff --git a/src/test/txvalidation_tests.cpp b/src/test/txvalidation_tests.cpp
1index b362abfa1d..0a51ff411f 100644
2--- a/src/test/txvalidation_tests.cpp
3+++ b/src/test/txvalidation_tests.cpp
4@@ -91,8 +91,10 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
5 }
6
7 // Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
8-static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
9+static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version, uint32_t dust_index)
10 {
11+ const uint32_t num_outputs{3};
12+ assert(dust_index < num_outputs);
13 CMutableTransaction mtx = CMutableTransaction{};
14 mtx.version = version;
15 mtx.vin.resize(inputs.size());
16@@ -100,9 +102,7 @@ static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& in
17 for (size_t i{0}; i < inputs.size(); ++i) {
18 mtx.vin[i].prevout = inputs[i];
19 }
20- int num_outputs{3};
21- int dust_index{num_outputs - 1};
22- for (auto i{0}; i < num_outputs; ++i) {
23+ for (uint32_t i{0}; i < num_outputs; ++i) {
24 mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
25 mtx.vout[i].nValue = (i == dust_index) ? 0 : 10000;
26 }
27@@ -118,12 +118,12 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
28
29 CFeeRate minrelay(1000);
30
31+ const uint32_t dust_index = 2;
32+
33 // Basic transaction with dust
34- auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
35+ auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2, dust_index);
36 const auto dust_txid = grandparent_tx_1->GetHash();
37
38- uint32_t dust_index = 2;
39-
40 // Child transaction spending dust
41 auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);
42
43@@ -149,7 +149,7 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
44 BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
45 BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
46
47- auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
48+ auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2, dust_index);
49 const auto dust_txid_2 = grandparent_tx_2->GetHash();
50
51 // Spend dust from one but not another is ok, as long as second grandparent has no child
52@@ -169,14 +169,14 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
53 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool));
54
55 // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
56- auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
57+ auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2, dust_index);
58 // Ok for parent to have dust
59 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
60 auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
61 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
62
63 // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
64- auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
65+ auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2, dust_index);
66 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
67
68 // Tests with parents in mempool
0diff --git a/src/test/txvalidation_tests.cpp b/src/test/txvalidation_tests.cpp
1index b362abfa1d..2309cd4f4d 100644
2--- a/src/test/txvalidation_tests.cpp
3+++ b/src/test/txvalidation_tests.cpp
4@@ -90,6 +90,8 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
5 return MakeTransactionRef(mtx);
6 }
7
8+static constexpr auto EPHEMERAL_DUST_INDEX = 2;
9+
10 // Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
11 static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
12 {
13@@ -100,11 +102,11 @@ static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& in
14 for (size_t i{0}; i < inputs.size(); ++i) {
15 mtx.vin[i].prevout = inputs[i];
16 }
17- int num_outputs{3};
18- int dust_index{num_outputs - 1};
19- for (auto i{0}; i < num_outputs; ++i) {
20+ constexpr uint32_t num_outputs{3};
21+ static_assert(EPHEMERAL_DUST_INDEX < num_outputs);
22+ for (uint32_t i{0}; i < num_outputs; ++i) {
23 mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
24- mtx.vout[i].nValue = (i == dust_index) ? 0 : 10000;
25+ mtx.vout[i].nValue = (i == EPHEMERAL_DUST_INDEX) ? 0 : 10000;
26 }
27 return MakeTransactionRef(mtx);
28 }
29@@ -122,10 +124,8 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
30 auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
31 const auto dust_txid = grandparent_tx_1->GetHash();
32
33- uint32_t dust_index = 2;
34-
35 // Child transaction spending dust
36- auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);
37+ auto dust_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
38
39 // We first start with nothing "in the mempool", using package checks
40
41@@ -139,7 +139,7 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
42 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool));
43 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool));
44
45- auto dust_non_spend = make_tx({COutPoint{dust_txid, dust_index - 1}}, /*version=*/2);
46+ auto dust_non_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
47
48 // Child spending non-dust only from parent should be disallowed even if dust otherwise spent
49 const auto dust_non_spend_txid{dust_non_spend->GetHash()};
50@@ -155,11 +155,11 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
51 // Spend dust from one but not another is ok, as long as second grandparent has no child
52 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool));
53
54- auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2);
55+ auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
56 // But if we spend from the parent, it must spend dust
57 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());
58
59- auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
60+ auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
61 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, minrelay, pool));
62
63 // Spending other outputs is also correct, as long as the dusty one is spent
64@@ -169,14 +169,14 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
65 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool));
66
67 // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
68- auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
69+ auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
70 // Ok for parent to have dust
71 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
72- auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
73+ auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
74 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
75
76 // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
77- auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
78+ auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
79 BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
80
81 // Tests with parents in mempool