Skip to content

Commit 15b6cbf

Browse files
committed
unit test: make dust index less magical
1 parent 5fbcfd1 commit 15b6cbf

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/test/txvalidation_tests.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,22 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
9090
return MakeTransactionRef(mtx);
9191
}
9292

93+
static constexpr auto NUM_EPHEMERAL_TX_OUTPUTS = 3;
94+
static constexpr auto EPHEMERAL_DUST_INDEX = NUM_EPHEMERAL_TX_OUTPUTS - 1;
95+
9396
// Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
9497
static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
9598
{
9699
CMutableTransaction mtx = CMutableTransaction{};
97100
mtx.version = version;
98101
mtx.vin.resize(inputs.size());
99-
mtx.vout.resize(3);
100102
for (size_t i{0}; i < inputs.size(); ++i) {
101103
mtx.vin[i].prevout = inputs[i];
102104
}
103-
for (auto i{0}; i < 3; ++i) {
105+
mtx.vout.resize(NUM_EPHEMERAL_TX_OUTPUTS);
106+
for (auto i{0}; i < NUM_EPHEMERAL_TX_OUTPUTS; ++i) {
104107
mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
105-
mtx.vout[i].nValue = (i == 2) ? 0 : 10000;
108+
mtx.vout[i].nValue = (i == EPHEMERAL_DUST_INDEX) ? 0 : 10000;
106109
}
107110
return MakeTransactionRef(mtx);
108111
}
@@ -120,10 +123,8 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
120123
auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
121124
const auto dust_txid = grandparent_tx_1->GetHash();
122125

123-
uint32_t dust_index = 2;
124-
125126
// Child transaction spending dust
126-
auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2);
127+
auto dust_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
127128

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

@@ -137,7 +138,7 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
137138
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool));
138139
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool));
139140

140-
auto dust_non_spend = make_tx({COutPoint{dust_txid, dust_index - 1}}, /*version=*/2);
141+
auto dust_non_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
141142

142143
// Child spending non-dust only from parent should be disallowed even if dust otherwise spent
143144
const auto dust_non_spend_txid{dust_non_spend->GetHash()};
@@ -153,11 +154,11 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
153154
// Spend dust from one but not another is ok, as long as second grandparent has no child
154155
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool));
155156

156-
auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2);
157+
auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
157158
// But if we spend from the parent, it must spend dust
158159
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());
159160

160-
auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
161+
auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
161162
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, minrelay, pool));
162163

163164
// Spending other outputs is also correct, as long as the dusty one is spent
@@ -167,14 +168,14 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
167168
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool));
168169

169170
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
170-
auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
171+
auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
171172
// Ok for parent to have dust
172173
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
173-
auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
174+
auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
174175
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
175176

176177
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
177-
auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
178+
auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
178179
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
179180

180181
// Tests with parents in mempool

0 commit comments

Comments
 (0)