Skip to content

Commit 5fbcfd1

Browse files
committed
unit test: assert txid returned on CheckEphemeralSpends failures
Simplify nullopt checks
1 parent ef94d84 commit 5fbcfd1

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/test/txvalidation_tests.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,85 +128,88 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
128128
// We first start with nothing "in the mempool", using package checks
129129

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

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

136136
// Dust checks pass
137-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool).has_value());
138-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool).has_value());
137+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool));
138+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool));
139139

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

142142
// Child spending non-dust only from parent should be disallowed even if dust otherwise spent
143-
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, minrelay, pool).has_value());
144-
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).has_value());
145-
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).has_value());
143+
const auto dust_non_spend_txid{dust_non_spend->GetHash()};
144+
const Txid null_txid;
145+
assert(dust_non_spend_txid != null_txid);
146+
BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
147+
BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
148+
BOOST_CHECK_EQUAL(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).value_or(null_txid), dust_non_spend_txid);
146149

147150
auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
148151
const auto dust_txid_2 = grandparent_tx_2->GetHash();
149152

150153
// Spend dust from one but not another is ok, as long as second grandparent has no child
151-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool).has_value());
154+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool));
152155

153156
auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2);
154157
// But if we spend from the parent, it must spend dust
155-
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_non_spend_both_parents}, minrelay, pool).has_value());
158+
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());
156159

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

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

166169
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
167170
auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2);
168171
// Ok for parent to have dust
169-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool).has_value());
172+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool));
170173
auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
171-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool).has_value());
174+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool));
172175

173176
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
174177
auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2);
175-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool).has_value());
178+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool));
176179

177180
// Tests with parents in mempool
178181

179182
// Nothing in mempool, this should pass for any transaction
180-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1}, minrelay, pool).has_value());
183+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1}, minrelay, pool));
181184

182185
// Add first grandparent to mempool and fetch entry
183186
pool.addUnchecked(entry.FromTx(grandparent_tx_1));
184187

185188
// Ignores ancestors that aren't direct parents
186-
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool).has_value());
189+
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool));
187190

188191
// Valid spend of dust with grandparent in mempool
189-
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool).has_value());
192+
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool));
190193

191194
// Second grandparent in same package
192-
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust, grandparent_tx_2}, minrelay, pool).has_value());
195+
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust, grandparent_tx_2}, minrelay, pool));
193196
// Order in package doesn't matter
194-
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_2, parent_with_dust}, minrelay, pool).has_value());
197+
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_2, parent_with_dust}, minrelay, pool));
195198

196199
// Add second grandparent to mempool
197200
pool.addUnchecked(entry.FromTx(grandparent_tx_2));
198201

199202
// Only spends single dust out of two direct parents
200-
BOOST_CHECK(CheckEphemeralSpends({dust_non_spend_both_parents}, minrelay, pool).has_value());
203+
BOOST_CHECK_EQUAL(CheckEphemeralSpends({dust_non_spend_both_parents}, minrelay, pool).value_or(null_txid), dust_non_spend_both_parents->GetHash());
201204

202205
// Spends both parents' dust
203-
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool).has_value());
206+
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool));
204207

205208
// Now add dusty parent to mempool
206209
pool.addUnchecked(entry.FromTx(parent_with_dust));
207210

208211
// Passes dust checks even with non-parent ancestors
209-
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool).has_value());
212+
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool));
210213
}
211214

212215
BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup)

0 commit comments

Comments
 (0)