Files
bitcoin/src/test/util/txmempool.cpp
glozow f7144b24be Merge bitcoin/bitcoin#31279: policy: ephemeral dust followups
466e4df3fb assert_mempool_contents: assert not duplicates expected (Greg Sanders)
ea5db2f269 functional: only generate required blocks for test (Greg Sanders)
d033acb608 fuzz: package_eval: let fuzzer run out input in main tx creation loop (Greg Sanders)
ba35a570c5 CheckEphemeralSpends: return boolean, and set child state and txid outparams (Greg Sanders)
cf0cee1617 func: add note about lack of 1P1C propagation in tree submitpackage (Greg Sanders)
8424290304 unit test: ephemeral_tests is using a dust relay rate, not minrelay (Greg Sanders)
d9cfa5fc4e CheckEphemeralSpends: no need to iterate inputs if no parent dust (Greg Sanders)
87b26e3dc0 func: rename test_free_relay to test_no_minrelay_fee (Greg Sanders)
e5709a4a41 func: slight elaboration on submitpackage restriction (Greg Sanders)
08e969bd10 RPC: only enforce dust rules on priority when standardness active (Greg Sanders)
ca050d12e7 unit test: adapt to changing MAX_DUST_OUTPUTS_PER_TX (Greg Sanders)
7c3490169c fuzz: package_eval: move last_tx inside txn ctor (Greg Sanders)
445eaed182 fuzz: use optional status instead of should_rbf_eph_spend (Greg Sanders)
4dfdf615b9 fuzz: remove unused TransactionsDelta validation interface (Greg Sanders)
09ce926e4a func: cleanup reorg test comment (Greg Sanders)
768a0c1889 func: cleanup test_dustrelay comments (Greg Sanders)
bedca1cb66 fuzz: Directly place transactions in vector (Greg Sanders)
c041ad6ecc fuzz: explain package eval coin tracking better (Greg Sanders)
bc0d98ea61 fuzz: remove dangling reference to GetEntry (Greg Sanders)
15b6cbf07f unit test: make dust index less magical (Greg Sanders)
5fbcfd12b8 unit test: assert txid returned on CheckEphemeralSpends failures (Greg Sanders)
ef94d84b4e bench: remove unnecessary CMTxn constructors (Greg Sanders)
c5c10fd317 ephemeral policy doxygen cleanup (Greg Sanders)
dd9044b8d4 ephemeral policy: IWYU (Greg Sanders)
c6859ce2de Move+rename GetDustIndexes -> GetDust (Greg Sanders)
62016b3230 Use std::ranges for ephemeral policy checks (Greg Sanders)
3ed930a1f4 Have HasDust and PreCheckValidEphemeralTx take CTransaction (Greg Sanders)
04a614bf9a Rename CheckValidEphemeralTx to PreCheckEphemeralTx (Greg Sanders)
cbf1a47d60 CheckEphemeralSpends: only compute txid of tx when needed (Greg Sanders)

Pull request description:

  Follow-up to https://github.com/bitcoin/bitcoin/pull/30239

  Here are the parent PR's comments that should be addressed by this PR:

  https://github.com/bitcoin/bitcoin/pull/30239/files#r1834529646
  https://github.com/bitcoin/bitcoin/pull/30239/files#r1831247308
  https://github.com/bitcoin/bitcoin/pull/30239/files#r1832622481
  https://github.com/bitcoin/bitcoin/pull/30239/files#r1831195216
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1835805164
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1835805164
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834639096
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834624976
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834619709
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834610434
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834504436
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834500036
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832985488
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830929809
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832376920
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832755799
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832492686
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832980576
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832784278
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1837989979
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830996993
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830997947
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830012890
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830037288
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1830977092
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832622481
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1834726168
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1832453654
  https://github.com/bitcoin/bitcoin/pull/30239#discussion_r1848488226

ACKs for top commit:
  naumenkogs:
    ACK 466e4df3fb
  hodlinator:
    ACK 466e4df3fb
  theStack:
    lgtm ACK 466e4df3fb
  glozow:
    utACK 466e4df3fb

Tree-SHA512: 89106f695755c238b84e0996b89446c0733e10a94c867f656d516d26697d2efe38dfc332188b8589a0a26a3d2bd2c88c6ab70c108e187ce5bfcb91bbf3fb0391
2024-11-25 13:47:44 -05:00

221 lines
10 KiB
C++

// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/txmempool.h>
#include <chainparams.h>
#include <node/context.h>
#include <node/mempool_args.h>
#include <policy/rbf.h>
#include <policy/truc_policy.h>
#include <txmempool.h>
#include <util/check.h>
#include <util/time.h>
#include <util/translation.h>
#include <validation.h>
using node::NodeContext;
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext& node)
{
CTxMemPool::Options mempool_opts{
// Default to always checking mempool regardless of
// chainparams.DefaultConsistencyChecks for tests
.check_ratio = 1,
.signals = node.validation_signals.get(),
};
const auto result{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
Assert(result);
return mempool_opts;
}
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
{
return FromTx(MakeTransactionRef(tx));
}
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()) {
return 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()) {
return 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());
}
// Each subpackage is allowed MAX_REPLACEMENT_CANDIDATES replacements (only checking individually here)
if (atmp_result.m_replaced_transactions.size() > MAX_REPLACEMENT_CANDIDATES) {
return strprintf("tx %s result replaced too many transactions",
wtxid.ToString());
}
// Replacements can't happen for subpackages larger than 2
if (!atmp_result.m_replaced_transactions.empty() &&
atmp_result.m_wtxids_fee_calculations.has_value() && atmp_result.m_wtxids_fee_calculations.value().size() > 2) {
return strprintf("tx %s was part of a too-large package RBF subpackage",
wtxid.ToString());
}
if (!atmp_result.m_replaced_transactions.empty() && mempool) {
LOCK(mempool->cs);
// If replacements occurred and it used 2 transactions, this is a package RBF and should result in a cluster of size 2
if (atmp_result.m_wtxids_fee_calculations.has_value() && atmp_result.m_wtxids_fee_calculations.value().size() == 2) {
const auto cluster = mempool->GatherClusters({tx->GetHash()});
if (cluster.size() != 2) return strprintf("tx %s has too many ancestors or descendants for a package rbf", wtxid.ToString());
}
}
// 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
// or if the failure was TX_RECONSIDERABLE
const bool valid_or_reconsiderable{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID ||
atmp_result.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
if (atmp_result.m_effective_feerate.has_value() != valid_or_reconsiderable) {
return strprintf("tx %s result should %shave m_effective_feerate",
wtxid.ToString(), valid ? "" : "not ");
}
if (atmp_result.m_wtxids_fee_calculations.has_value() != valid_or_reconsiderable) {
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) {
return 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))) {
return strprintf("wtxid %s should not be in mempool", wtxid.ToString());
}
}
for (const auto& tx_ref : atmp_result.m_replaced_transactions) {
if (mempool->exists(GenTxid::Txid(tx_ref->GetHash()))) {
return strprintf("tx %s should not be in mempool as it was replaced", tx_ref->GetWitnessHash().ToString());
}
}
}
}
return std::nullopt;
}
void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool)
{
LOCK(tx_pool.cs);
for (const auto& tx_info : tx_pool.infoAll()) {
const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
std::vector<uint32_t> dust_indexes = GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate);
Assert(dust_indexes.size() < 2);
if (dust_indexes.empty()) continue;
// Transaction must have no base fee
Assert(entry.GetFee() == 0 && entry.GetModifiedFee() == 0);
// Transaction has single dust; make sure it's swept or will not be mined
const auto& children = entry.GetMemPoolChildrenConst();
// Multiple children should never happen as non-dust-spending child
// can get mined as package
Assert(children.size() < 2);
if (children.empty()) {
// No children and no fees; modified fees aside won't get mined so it's fine
// Happens naturally if child spend is RBF cycled away.
continue;
}
// Only-child should be spending the dust
const auto& only_child = children.begin()->get().GetTx();
COutPoint dust_outpoint{tx_info.tx->GetHash(), dust_indexes[0]};
Assert(std::any_of(only_child.vin.begin(), only_child.vin.end(), [&dust_outpoint](const CTxIn& txin) {
return txin.prevout == dust_outpoint;
}));
}
}
void CheckMempoolTRUCInvariants(const CTxMemPool& tx_pool)
{
LOCK(tx_pool.cs);
for (const auto& tx_info : tx_pool.infoAll()) {
const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
if (tx_info.tx->version == TRUC_VERSION) {
// Check that special maximum virtual size is respected
Assert(entry.GetTxSize() <= TRUC_MAX_VSIZE);
// Check that special TRUC ancestor/descendant limits and rules are always respected
Assert(entry.GetCountWithDescendants() <= TRUC_DESCENDANT_LIMIT);
Assert(entry.GetCountWithAncestors() <= TRUC_ANCESTOR_LIMIT);
Assert(entry.GetSizeWithDescendants() <= TRUC_MAX_VSIZE + TRUC_CHILD_MAX_VSIZE);
Assert(entry.GetSizeWithAncestors() <= TRUC_MAX_VSIZE + TRUC_CHILD_MAX_VSIZE);
// If this transaction has at least 1 ancestor, it's a "child" and has restricted weight.
if (entry.GetCountWithAncestors() > 1) {
Assert(entry.GetTxSize() <= TRUC_CHILD_MAX_VSIZE);
// All TRUC transactions must only have TRUC unconfirmed parents.
const auto& parents = entry.GetMemPoolParentsConst();
Assert(parents.begin()->get().GetSharedTx()->version == TRUC_VERSION);
}
} else if (entry.GetCountWithAncestors() > 1) {
// All non-TRUC transactions must only have non-TRUC unconfirmed parents.
for (const auto& parent : entry.GetMemPoolParentsConst()) {
Assert(parent.get().GetSharedTx()->version != TRUC_VERSION);
}
}
}
}
void AddToMempool(CTxMemPool& tx_pool, const CTxMemPoolEntry& entry)
{
LOCK2(cs_main, tx_pool.cs);
auto changeset = tx_pool.GetChangeSet();
changeset->StageAddition(entry.GetSharedTx(), entry.GetFee(),
entry.GetTime().count(), entry.GetHeight(), entry.GetSequence(),
entry.GetSpendsCoinbase(), entry.GetSigOpCost(), entry.GetLockPoints());
changeset->Apply();
}