aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2023-08-08 18:25:40 +0100
committerGreg Sanders <gsanders87@gmail.com>2023-11-02 09:33:47 -0400
commit34088d6c9ed4ed99bb6b7fc83795da01ec9f3c97 (patch)
treeae4e2c43bb7d81107b3f05c193c8d0ef451bc76d
parent651fa404e454e31f8e9d830aa292eb3b456b54fb (diff)
[test util] CheckPackageMempoolAcceptResult for sanity-checking results
-rw-r--r--src/test/util/txmempool.cpp78
-rw-r--r--src/test/util/txmempool.h10
2 files changed, 88 insertions, 0 deletions
diff --git a/src/test/util/txmempool.cpp b/src/test/util/txmempool.cpp
index c945f35d79..c4fbc8dbb3 100644
--- a/src/test/util/txmempool.cpp
+++ b/src/test/util/txmempool.cpp
@@ -11,6 +11,7 @@
#include <util/check.h>
#include <util/time.h>
#include <util/translation.h>
+#include <validation.h>
using node::NodeContext;
@@ -36,3 +37,80 @@ CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
{
return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
}
+
+std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
+ const PackageMempoolAcceptResult& result,
+ bool expect_valid,
+ const CTxMemPool* mempool)
+{
+ if (expect_valid) {
+ if (result.m_state.IsInvalid()) {
+ return strprintf("Package validation unexpectedly failed: %s", result.m_state.ToString());
+ }
+ } else {
+ if (result.m_state.IsValid()) {
+ strprintf("Package validation unexpectedly succeeded. %s", result.m_state.ToString());
+ }
+ }
+ if (result.m_state.GetResult() != PackageValidationResult::PCKG_POLICY && txns.size() != result.m_tx_results.size()) {
+ strprintf("txns size %u does not match tx results size %u", txns.size(), result.m_tx_results.size());
+ }
+ for (const auto& tx : txns) {
+ const auto& wtxid = tx->GetWitnessHash();
+ if (result.m_tx_results.count(wtxid) == 0) {
+ return strprintf("result not found for tx %s", wtxid.ToString());
+ }
+
+ const auto& atmp_result = result.m_tx_results.at(wtxid);
+ const bool valid{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID};
+ if (expect_valid && atmp_result.m_state.IsInvalid()) {
+ return strprintf("tx %s unexpectedly failed: %s", wtxid.ToString(), atmp_result.m_state.ToString());
+ }
+
+ //m_replaced_transactions should exist iff the result was VALID
+ if (atmp_result.m_replaced_transactions.has_value() != valid) {
+ return strprintf("tx %s result should %shave m_replaced_transactions",
+ wtxid.ToString(), valid ? "" : "not ");
+ }
+
+ // m_vsize and m_base_fees should exist iff the result was VALID or MEMPOOL_ENTRY
+ const bool mempool_entry{atmp_result.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY};
+ if (atmp_result.m_base_fees.has_value() != (valid || mempool_entry)) {
+ return strprintf("tx %s result should %shave m_base_fees", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
+ }
+ if (atmp_result.m_vsize.has_value() != (valid || mempool_entry)) {
+ return strprintf("tx %s result should %shave m_vsize", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
+ }
+
+ // m_other_wtxid should exist iff the result was DIFFERENT_WITNESS
+ const bool diff_witness{atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS};
+ if (atmp_result.m_other_wtxid.has_value() != diff_witness) {
+ return strprintf("tx %s result should %shave m_other_wtxid", wtxid.ToString(), diff_witness ? "" : "not ");
+ }
+
+ // m_effective_feerate and m_wtxids_fee_calculations should exist iff the result was valid
+ if (atmp_result.m_effective_feerate.has_value() != valid) {
+ return strprintf("tx %s result should %shave m_effective_feerate",
+ wtxid.ToString(), valid ? "" : "not ");
+ }
+ if (atmp_result.m_wtxids_fee_calculations.has_value() != valid) {
+ return strprintf("tx %s result should %shave m_effective_feerate",
+ wtxid.ToString(), valid ? "" : "not ");
+ }
+
+ if (mempool) {
+ // The tx by txid should be in the mempool iff the result was not INVALID.
+ const bool txid_in_mempool{atmp_result.m_result_type != MempoolAcceptResult::ResultType::INVALID};
+ if (mempool->exists(GenTxid::Txid(tx->GetHash())) != txid_in_mempool) {
+ strprintf("tx %s should %sbe in mempool", wtxid.ToString(), txid_in_mempool ? "" : "not ");
+ }
+ // Additionally, if the result was DIFFERENT_WITNESS, we shouldn't be able to find the tx in mempool by wtxid.
+ if (tx->HasWitness() && atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS) {
+ if (mempool->exists(GenTxid::Wtxid(wtxid))) {
+ strprintf("wtxid %s should not be in mempool", wtxid.ToString());
+ }
+ }
+ }
+ }
+ return std::nullopt;
+}
diff --git a/src/test/util/txmempool.h b/src/test/util/txmempool.h
index 4b0daf0d42..a866d1ce74 100644
--- a/src/test/util/txmempool.h
+++ b/src/test/util/txmempool.h
@@ -5,12 +5,14 @@
#ifndef BITCOIN_TEST_UTIL_TXMEMPOOL_H
#define BITCOIN_TEST_UTIL_TXMEMPOOL_H
+#include <policy/packages.h>
#include <txmempool.h>
#include <util/time.h>
namespace node {
struct NodeContext;
}
+struct PackageMempoolAcceptResult;
CTxMemPool::Options MemPoolOptionsForTest(const node::NodeContext& node);
@@ -36,4 +38,12 @@ struct TestMemPoolEntryHelper {
TestMemPoolEntryHelper& SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
};
+/** Check expected properties for every PackageMempoolAcceptResult, regardless of value. Returns
+ * a string if an error occurs with error populated, nullopt otherwise. If mempool is provided,
+ * checks that the expected transactions are in mempool (this should be set to nullptr for a test_accept).
+*/
+std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
+ const PackageMempoolAcceptResult& result,
+ bool expect_valid,
+ const CTxMemPool* mempool);
#endif // BITCOIN_TEST_UTIL_TXMEMPOOL_H