Skip to content

Commit f23ac10

Browse files
committed
Merge bitcoin#28764: Fuzz: Check individual and package transaction invariants
fcb3069 Use CheckPackageMempoolAcceptResult for package evaluation fuzzing (Greg Sanders) 34088d6 [test util] CheckPackageMempoolAcceptResult for sanity-checking results (glozow) 651fa40 fuzz: tx_pool checks ATMP result invariants (Greg Sanders) Pull request description: Poached from bitcoin#26711 since that PR is being split apart, and modified to match current behavior. ACKs for top commit: glozow: reACK fcb3069, only whitespace changes dergoegge: ACK fcb3069 Tree-SHA512: abd687e526d8dfc8d65b3a873ece8ca35fdcbd6b0f7b93da6a723ef4e47cf85612de819e6f2b8631bdf897e1aba27cdd86f89b7bd85fc3356e74be275dcdf8cc
2 parents 9b68c9b + fcb3069 commit f23ac10

File tree

4 files changed

+145
-11
lines changed

4 files changed

+145
-11
lines changed

src/test/fuzz/package_eval.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,6 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
257257

258258
const auto result_package = WITH_LOCK(::cs_main,
259259
return ProcessNewPackage(chainstate, tx_pool, txs, /*test_accept=*/single_submit));
260-
// If something went wrong due to a package-specific policy, it might not return a
261-
// validation result for the transaction.
262-
if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
263-
auto it = result_package.m_tx_results.find(txs.back()->GetWitnessHash());
264-
Assert(it != result_package.m_tx_results.end());
265-
Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
266-
it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID ||
267-
it->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
268-
}
269260

270261
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, txs.back(), GetTime(), bypass_limits, /*test_accept=*/!single_submit));
271262
const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
@@ -281,6 +272,12 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
281272
Assert(added.size() == 1);
282273
Assert(txs.back() == *added.begin());
283274
}
275+
} else if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
276+
// We don't know anything about the validity since transactions were randomly generated, so
277+
// just use result_package.m_state here. This makes the expect_valid check meaningless, but
278+
// we can still verify that the contents of m_tx_results are consistent with m_state.
279+
const bool expect_valid{result_package.m_state.IsValid()};
280+
Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, nullptr));
284281
} else {
285282
// This is empty if it fails early checks, or "full" if transactions are looked at deeper
286283
Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());

src/test/fuzz/tx_pool.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,53 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
131131
return CTxMemPool{mempool_opts};
132132
}
133133

134+
void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
135+
{
136+
137+
switch (res.m_result_type) {
138+
case MempoolAcceptResult::ResultType::VALID:
139+
{
140+
Assert(txid_in_mempool);
141+
Assert(wtxid_in_mempool);
142+
Assert(res.m_state.IsValid());
143+
Assert(!res.m_state.IsInvalid());
144+
Assert(res.m_replaced_transactions);
145+
Assert(res.m_vsize);
146+
Assert(res.m_base_fees);
147+
Assert(res.m_effective_feerate);
148+
Assert(res.m_wtxids_fee_calculations);
149+
Assert(!res.m_other_wtxid);
150+
break;
151+
}
152+
case MempoolAcceptResult::ResultType::INVALID:
153+
{
154+
// It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
155+
Assert(!res.m_state.IsValid());
156+
Assert(res.m_state.IsInvalid());
157+
Assert(!res.m_replaced_transactions);
158+
Assert(!res.m_vsize);
159+
Assert(!res.m_base_fees);
160+
// Unable or unwilling to calculate fees
161+
Assert(!res.m_effective_feerate);
162+
Assert(!res.m_wtxids_fee_calculations);
163+
Assert(!res.m_other_wtxid);
164+
break;
165+
}
166+
case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
167+
{
168+
// ATMP never sets this; only set in package settings
169+
Assert(false);
170+
break;
171+
}
172+
case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
173+
{
174+
// ATMP never sets this; only set in package settings
175+
Assert(false);
176+
break;
177+
}
178+
}
179+
}
180+
134181
FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
135182
{
136183
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
@@ -258,9 +305,11 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
258305
SyncWithValidationInterfaceQueue();
259306
UnregisterSharedValidationInterface(txr);
260307

308+
bool txid_in_mempool = tx_pool.exists(GenTxid::Txid(tx->GetHash()));
309+
bool wtxid_in_mempool = tx_pool.exists(GenTxid::Wtxid(tx->GetWitnessHash()));
310+
CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
311+
261312
Assert(accepted != added.empty());
262-
Assert(accepted == res.m_state.IsValid());
263-
Assert(accepted != res.m_state.IsInvalid());
264313
if (accepted) {
265314
Assert(added.size() == 1); // For now, no package acceptance
266315
Assert(tx == *added.begin());

src/test/util/txmempool.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <util/check.h>
1212
#include <util/time.h>
1313
#include <util/translation.h>
14+
#include <validation.h>
1415

1516
using node::NodeContext;
1617

@@ -36,3 +37,80 @@ CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
3637
{
3738
return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
3839
}
40+
41+
std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
42+
const PackageMempoolAcceptResult& result,
43+
bool expect_valid,
44+
const CTxMemPool* mempool)
45+
{
46+
if (expect_valid) {
47+
if (result.m_state.IsInvalid()) {
48+
return strprintf("Package validation unexpectedly failed: %s", result.m_state.ToString());
49+
}
50+
} else {
51+
if (result.m_state.IsValid()) {
52+
strprintf("Package validation unexpectedly succeeded. %s", result.m_state.ToString());
53+
}
54+
}
55+
if (result.m_state.GetResult() != PackageValidationResult::PCKG_POLICY && txns.size() != result.m_tx_results.size()) {
56+
strprintf("txns size %u does not match tx results size %u", txns.size(), result.m_tx_results.size());
57+
}
58+
for (const auto& tx : txns) {
59+
const auto& wtxid = tx->GetWitnessHash();
60+
if (result.m_tx_results.count(wtxid) == 0) {
61+
return strprintf("result not found for tx %s", wtxid.ToString());
62+
}
63+
64+
const auto& atmp_result = result.m_tx_results.at(wtxid);
65+
const bool valid{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID};
66+
if (expect_valid && atmp_result.m_state.IsInvalid()) {
67+
return strprintf("tx %s unexpectedly failed: %s", wtxid.ToString(), atmp_result.m_state.ToString());
68+
}
69+
70+
//m_replaced_transactions should exist iff the result was VALID
71+
if (atmp_result.m_replaced_transactions.has_value() != valid) {
72+
return strprintf("tx %s result should %shave m_replaced_transactions",
73+
wtxid.ToString(), valid ? "" : "not ");
74+
}
75+
76+
// m_vsize and m_base_fees should exist iff the result was VALID or MEMPOOL_ENTRY
77+
const bool mempool_entry{atmp_result.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY};
78+
if (atmp_result.m_base_fees.has_value() != (valid || mempool_entry)) {
79+
return strprintf("tx %s result should %shave m_base_fees", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
80+
}
81+
if (atmp_result.m_vsize.has_value() != (valid || mempool_entry)) {
82+
return strprintf("tx %s result should %shave m_vsize", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
83+
}
84+
85+
// m_other_wtxid should exist iff the result was DIFFERENT_WITNESS
86+
const bool diff_witness{atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS};
87+
if (atmp_result.m_other_wtxid.has_value() != diff_witness) {
88+
return strprintf("tx %s result should %shave m_other_wtxid", wtxid.ToString(), diff_witness ? "" : "not ");
89+
}
90+
91+
// m_effective_feerate and m_wtxids_fee_calculations should exist iff the result was valid
92+
if (atmp_result.m_effective_feerate.has_value() != valid) {
93+
return strprintf("tx %s result should %shave m_effective_feerate",
94+
wtxid.ToString(), valid ? "" : "not ");
95+
}
96+
if (atmp_result.m_wtxids_fee_calculations.has_value() != valid) {
97+
return strprintf("tx %s result should %shave m_effective_feerate",
98+
wtxid.ToString(), valid ? "" : "not ");
99+
}
100+
101+
if (mempool) {
102+
// The tx by txid should be in the mempool iff the result was not INVALID.
103+
const bool txid_in_mempool{atmp_result.m_result_type != MempoolAcceptResult::ResultType::INVALID};
104+
if (mempool->exists(GenTxid::Txid(tx->GetHash())) != txid_in_mempool) {
105+
strprintf("tx %s should %sbe in mempool", wtxid.ToString(), txid_in_mempool ? "" : "not ");
106+
}
107+
// Additionally, if the result was DIFFERENT_WITNESS, we shouldn't be able to find the tx in mempool by wtxid.
108+
if (tx->HasWitness() && atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS) {
109+
if (mempool->exists(GenTxid::Wtxid(wtxid))) {
110+
strprintf("wtxid %s should not be in mempool", wtxid.ToString());
111+
}
112+
}
113+
}
114+
}
115+
return std::nullopt;
116+
}

src/test/util/txmempool.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#ifndef BITCOIN_TEST_UTIL_TXMEMPOOL_H
66
#define BITCOIN_TEST_UTIL_TXMEMPOOL_H
77

8+
#include <policy/packages.h>
89
#include <txmempool.h>
910
#include <util/time.h>
1011

1112
namespace node {
1213
struct NodeContext;
1314
}
15+
struct PackageMempoolAcceptResult;
1416

1517
CTxMemPool::Options MemPoolOptionsForTest(const node::NodeContext& node);
1618

@@ -36,4 +38,12 @@ struct TestMemPoolEntryHelper {
3638
TestMemPoolEntryHelper& SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
3739
};
3840

41+
/** Check expected properties for every PackageMempoolAcceptResult, regardless of value. Returns
42+
* a string if an error occurs with error populated, nullopt otherwise. If mempool is provided,
43+
* checks that the expected transactions are in mempool (this should be set to nullptr for a test_accept).
44+
*/
45+
std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
46+
const PackageMempoolAcceptResult& result,
47+
bool expect_valid,
48+
const CTxMemPool* mempool);
3949
#endif // BITCOIN_TEST_UTIL_TXMEMPOOL_H

0 commit comments

Comments
 (0)