Merge bitcoin/bitcoin#31279: policy: ephemeral dust followups
Some checks are pending
CI / ASan + LSan + UBSan + integer, no depends, USDT (push) Waiting to run
CI / test each commit (push) Waiting to run
CI / macOS 14 native, arm64, no depends, sqlite only, gui (push) Waiting to run
CI / Win64 native, VS 2022 (push) Waiting to run

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
This commit is contained in:
glozow 2024-11-25 13:47:31 -05:00
commit f7144b24be
No known key found for this signature in database
GPG key ID: BA03F4DBE0C63FB4
15 changed files with 210 additions and 146 deletions

View file

@ -44,7 +44,7 @@ static void MempoolCheckEphemeralSpends(benchmark::Bench& bench)
} }
// Tx with many outputs // Tx with many outputs
CMutableTransaction tx1 = CMutableTransaction(); CMutableTransaction tx1;
tx1.vin.resize(1); tx1.vin.resize(1);
tx1.vout.resize(number_outputs); tx1.vout.resize(number_outputs);
for (size_t i = 0; i < tx1.vout.size(); i++) { for (size_t i = 0; i < tx1.vout.size(); i++) {
@ -56,7 +56,7 @@ static void MempoolCheckEphemeralSpends(benchmark::Bench& bench)
const auto& parent_txid = tx1.GetHash(); const auto& parent_txid = tx1.GetHash();
// Spends all outputs of tx1, other details don't matter // Spends all outputs of tx1, other details don't matter
CMutableTransaction tx2 = CMutableTransaction(); CMutableTransaction tx2;
tx2.vin.resize(tx1.vout.size()); tx2.vin.resize(tx1.vout.size());
for (size_t i = 0; i < tx2.vin.size(); i++) { for (size_t i = 0; i < tx2.vin.size(); i++) {
tx2.vin[0].prevout.hash = parent_txid; tx2.vin[0].prevout.hash = parent_txid;
@ -74,9 +74,12 @@ static void MempoolCheckEphemeralSpends(benchmark::Bench& bench)
uint32_t iteration{0}; uint32_t iteration{0};
TxValidationState dummy_state;
Txid dummy_txid;
bench.run([&]() NO_THREAD_SAFETY_ANALYSIS { bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
CheckEphemeralSpends({tx2_r}, /*dust_relay_rate=*/CFeeRate(iteration * COIN / 10), pool); CheckEphemeralSpends({tx2_r}, /*dust_relay_rate=*/CFeeRate(iteration * COIN / 10), pool, dummy_state, dummy_txid);
iteration++; iteration++;
}); });
} }

View file

@ -2,29 +2,39 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <consensus/validation.h>
#include <policy/ephemeral_policy.h> #include <policy/ephemeral_policy.h>
#include <policy/feerate.h>
#include <policy/packages.h>
#include <policy/policy.h> #include <policy/policy.h>
#include <primitives/transaction.h>
#include <txmempool.h>
#include <util/check.h>
#include <util/hasher.h>
bool HasDust(const CTransactionRef& tx, CFeeRate dust_relay_rate) #include <algorithm>
{ #include <cstdint>
return std::any_of(tx->vout.cbegin(), tx->vout.cend(), [&](const auto& output) { return IsDust(output, dust_relay_rate); }); #include <map>
} #include <memory>
#include <unordered_set>
#include <utility>
#include <vector>
bool CheckValidEphemeralTx(const CTransactionRef& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state) bool PreCheckEphemeralTx(const CTransaction& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state)
{ {
// We never want to give incentives to mine this transaction alone // We never want to give incentives to mine this transaction alone
if ((base_fee != 0 || mod_fee != 0) && HasDust(tx, dust_relay_rate)) { if ((base_fee != 0 || mod_fee != 0) && !GetDust(tx, dust_relay_rate).empty()) {
return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "dust", "tx with dust output must be 0-fee"); return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "dust", "tx with dust output must be 0-fee");
} }
return true; return true;
} }
std::optional<Txid> CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool) bool CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool, TxValidationState& out_child_state, Txid& out_child_txid)
{ {
if (!Assume(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}))) { if (!Assume(std::ranges::all_of(package, [](const auto& tx){return tx != nullptr;}))) {
// Bail out of spend checks if caller gave us an invalid package // Bail out of spend checks if caller gave us an invalid package
return std::nullopt; return true;
} }
std::map<Txid, CTransactionRef> map_txid_ref; std::map<Txid, CTransactionRef> map_txid_ref;
@ -33,7 +43,6 @@ std::optional<Txid> CheckEphemeralSpends(const Package& package, CFeeRate dust_r
} }
for (const auto& tx : package) { for (const auto& tx : package) {
Txid txid = tx->GetHash();
std::unordered_set<Txid, SaltedTxidHasher> processed_parent_set; std::unordered_set<Txid, SaltedTxidHasher> processed_parent_set;
std::unordered_set<COutPoint, SaltedOutpointHasher> unspent_parent_dust; std::unordered_set<COutPoint, SaltedOutpointHasher> unspent_parent_dust;
@ -63,6 +72,10 @@ std::optional<Txid> CheckEphemeralSpends(const Package& package, CFeeRate dust_r
processed_parent_set.insert(parent_txid); processed_parent_set.insert(parent_txid);
} }
if (unspent_parent_dust.empty()) {
continue;
}
// Now that we have gathered parents' dust, make sure it's spent // Now that we have gathered parents' dust, make sure it's spent
// by the child // by the child
for (const auto& tx_input : tx->vin) { for (const auto& tx_input : tx->vin) {
@ -70,9 +83,12 @@ std::optional<Txid> CheckEphemeralSpends(const Package& package, CFeeRate dust_r
} }
if (!unspent_parent_dust.empty()) { if (!unspent_parent_dust.empty()) {
return txid; out_child_txid = tx->GetHash();
out_child_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
strprintf("tx %s did not spend parent's ephemeral dust", out_child_txid.ToString()));
return false;
} }
} }
return std::nullopt; return true;
} }

View file

@ -5,17 +5,22 @@
#ifndef BITCOIN_POLICY_EPHEMERAL_POLICY_H #ifndef BITCOIN_POLICY_EPHEMERAL_POLICY_H
#define BITCOIN_POLICY_EPHEMERAL_POLICY_H #define BITCOIN_POLICY_EPHEMERAL_POLICY_H
#include <consensus/amount.h>
#include <policy/packages.h> #include <policy/packages.h>
#include <policy/policy.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <txmempool.h>
#include <optional>
class CFeeRate;
class CTxMemPool;
class TxValidationState;
/** These utility functions ensure that ephemeral dust is safely /** These utility functions ensure that ephemeral dust is safely
* created and spent without unduly risking them entering the utxo * created and spent without unduly risking them entering the utxo
* set. * set.
* This is ensured by requiring: * This is ensured by requiring:
* - CheckValidEphemeralTx checks are respected * - PreCheckEphemeralTx checks are respected
* - The parent has no child (and 0-fee as implied above to disincentivize mining) * - The parent has no child (and 0-fee as implied above to disincentivize mining)
* - OR the parent transaction has exactly one child, and the dust is spent by that child * - OR the parent transaction has exactly one child, and the dust is spent by that child
* *
@ -34,22 +39,20 @@
* are the only way to bring fees. * are the only way to bring fees.
*/ */
/** Returns true if transaction contains dust */
bool HasDust(const CTransactionRef& tx, CFeeRate dust_relay_rate);
/* All the following checks are only called if standardness rules are being applied. */ /* All the following checks are only called if standardness rules are being applied. */
/** Must be called for each transaction once transaction fees are known. /** Must be called for each transaction once transaction fees are known.
* Does context-less checks about a single transaction. * Does context-less checks about a single transaction.
* Returns false if the fee is non-zero and dust exists, populating state. True otherwise. * @returns false if the fee is non-zero and dust exists, populating state. True otherwise.
*/ */
bool CheckValidEphemeralTx(const CTransactionRef& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state); bool PreCheckEphemeralTx(const CTransaction& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state);
/** Must be called for each transaction(package) if any dust is in the package. /** Must be called for each transaction(package) if any dust is in the package.
* Checks that each transaction's parents have their dust spent by the child, * Checks that each transaction's parents have their dust spent by the child,
* where parents are either in the mempool or in the package itself. * where parents are either in the mempool or in the package itself.
* The function returns std::nullopt if all dust is properly spent, or the txid of the violating child spend. * Sets out_child_state and out_child_txid on failure.
* @returns true if all dust is properly spent.
*/ */
std::optional<Txid> CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool); bool CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool, TxValidationState& out_child_state, Txid& out_child_txid);
#endif // BITCOIN_POLICY_EPHEMERAL_POLICY_H #endif // BITCOIN_POLICY_EPHEMERAL_POLICY_H

View file

@ -67,6 +67,15 @@ bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn)); return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
} }
std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate)
{
std::vector<uint32_t> dust_outputs;
for (uint32_t i{0}; i < tx.vout.size(); ++i) {
if (IsDust(tx.vout[i], dust_relay_rate)) dust_outputs.push_back(i);
}
return dust_outputs;
}
bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType) bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType)
{ {
std::vector<std::vector<unsigned char> > vSolutions; std::vector<std::vector<unsigned char> > vSolutions;
@ -129,7 +138,6 @@ bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_dat
} }
unsigned int nDataOut = 0; unsigned int nDataOut = 0;
unsigned int num_dust_outputs{0};
TxoutType whichType; TxoutType whichType;
for (const CTxOut& txout : tx.vout) { for (const CTxOut& txout : tx.vout) {
if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) { if (!::IsStandard(txout.scriptPubKey, max_datacarrier_bytes, whichType)) {
@ -142,13 +150,11 @@ bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_dat
else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) { else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
reason = "bare-multisig"; reason = "bare-multisig";
return false; return false;
} else if (IsDust(txout, dust_relay_fee)) {
num_dust_outputs++;
} }
} }
// Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust) // Only MAX_DUST_OUTPUTS_PER_TX dust is permitted(on otherwise valid ephemeral dust)
if (num_dust_outputs > MAX_DUST_OUTPUTS_PER_TX) { if (GetDust(tx, dust_relay_fee).size() > MAX_DUST_OUTPUTS_PER_TX) {
reason = "dust"; reason = "dust";
return false; return false;
} }

View file

@ -131,6 +131,8 @@ bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType); bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType);
/** Get the vout index numbers of all dust outputs */
std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate);
// Changing the default transaction version requires a two step process: first // Changing the default transaction version requires a two step process: first
// adapting relay policy by bumping TX_MAX_STANDARD_VERSION, and then later // adapting relay policy by bumping TX_MAX_STANDARD_VERSION, and then later

View file

@ -496,7 +496,7 @@ static RPCHelpMan prioritisetransaction()
// Non-0 fee dust transactions are not allowed for entry, and modification not allowed afterwards // Non-0 fee dust transactions are not allowed for entry, and modification not allowed afterwards
const auto& tx = mempool.get(hash); const auto& tx = mempool.get(hash);
if (tx && HasDust(tx, mempool.m_opts.dust_relay_feerate)) { if (mempool.m_opts.require_standard && tx && !GetDust(*tx, mempool.m_opts.dust_relay_feerate).empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is not supported for transactions with dust outputs."); throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is not supported for transactions with dust outputs.");
} }

View file

@ -167,7 +167,7 @@ std::optional<COutPoint> GetChildEvictingPrevout(const CTxMemPool& tx_pool)
LOCK(tx_pool.cs); LOCK(tx_pool.cs);
for (const auto& tx_info : tx_pool.infoAll()) { for (const auto& tx_info : tx_pool.infoAll()) {
const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash())); const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
std::vector<uint32_t> dust_indexes{GetDustIndexes(tx_info.tx, tx_pool.m_opts.dust_relay_feerate)}; std::vector<uint32_t> dust_indexes{GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate)};
if (!dust_indexes.empty()) { if (!dust_indexes.empty()) {
const auto& children = entry.GetMemPoolChildrenConst(); const auto& children = entry.GetMemPoolChildrenConst();
if (!children.empty()) { if (!children.empty()) {
@ -210,37 +210,33 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
chainstate.SetMempool(&tx_pool); chainstate.SetMempool(&tx_pool);
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300) LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
{ {
Assert(!mempool_outpoints.empty()); Assert(!mempool_outpoints.empty());
std::vector<CTransactionRef> txs; std::vector<CTransactionRef> txs;
// Find something we may want to double-spend with two input single tx // Find something we may want to double-spend with two input single tx
std::optional<COutPoint> outpoint_to_rbf{GetChildEvictingPrevout(tx_pool)}; std::optional<COutPoint> outpoint_to_rbf{fuzzed_data_provider.ConsumeBool() ? GetChildEvictingPrevout(tx_pool) : std::nullopt};
bool should_rbf_eph_spend = outpoint_to_rbf && fuzzed_data_provider.ConsumeBool();
// Make small packages // Make small packages
const auto num_txs = should_rbf_eph_spend ? 1 : (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4); const auto num_txs = outpoint_to_rbf ? 1 : (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
std::set<COutPoint> package_outpoints; std::set<COutPoint> package_outpoints;
while (txs.size() < num_txs) { while (txs.size() < num_txs) {
// Last transaction in a package needs to be a child of parents to get further in validation
// so the last transaction to be generated(in a >1 package) must spend all package-made outputs
// Note that this test currently only spends package outputs in last transaction.
bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
// Create transaction to add to the mempool // Create transaction to add to the mempool
const CTransactionRef tx = [&] { txs.emplace_back([&] {
CMutableTransaction tx_mut; CMutableTransaction tx_mut;
tx_mut.version = CTransaction::CURRENT_VERSION; tx_mut.version = CTransaction::CURRENT_VERSION;
tx_mut.nLockTime = 0; tx_mut.nLockTime = 0;
// Last tx will sweep half or more of all outpoints from package // Last transaction in a package needs to be a child of parents to get further in validation
const auto num_in = should_rbf_eph_spend ? 2 : // so the last transaction to be generated(in a >1 package) must spend all package-made outputs
// Note that this test currently only spends package outputs in last transaction.
bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
const auto num_in = outpoint_to_rbf ? 2 :
last_tx ? fuzzed_data_provider.ConsumeIntegralInRange<int>(package_outpoints.size()/2 + 1, package_outpoints.size()) : last_tx ? fuzzed_data_provider.ConsumeIntegralInRange<int>(package_outpoints.size()/2 + 1, package_outpoints.size()) :
fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4); fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
auto num_out = should_rbf_eph_spend ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4); const auto num_out = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
auto& outpoints = last_tx ? package_outpoints : mempool_outpoints; auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
@ -248,12 +244,13 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
CAmount amount_in{0}; CAmount amount_in{0};
for (int i = 0; i < num_in; ++i) { for (int i = 0; i < num_in; ++i) {
// Pop random outpoint // Pop random outpoint. We erase them to avoid double-spending
// while in this loop, but later add them back (unless last_tx).
auto pop = outpoints.begin(); auto pop = outpoints.begin();
std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1)); std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
auto outpoint = *pop; auto outpoint = *pop;
if (i == 0 && should_rbf_eph_spend) { if (i == 0 && outpoint_to_rbf) {
outpoint = *outpoint_to_rbf; outpoint = *outpoint_to_rbf;
outpoints.erase(outpoint); outpoints.erase(outpoint);
} else { } else {
@ -277,7 +274,7 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
} }
// Note output amounts can naturally drop to dust on their own. // Note output amounts can naturally drop to dust on their own.
if (!should_rbf_eph_spend && fuzzed_data_provider.ConsumeBool()) { if (!outpoint_to_rbf && fuzzed_data_provider.ConsumeBool()) {
uint32_t dust_index = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, num_out); uint32_t dust_index = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, num_out);
tx_mut.vout.insert(tx_mut.vout.begin() + dust_index, CTxOut(0, P2WSH_EMPTY)); tx_mut.vout.insert(tx_mut.vout.begin() + dust_index, CTxOut(0, P2WSH_EMPTY));
} }
@ -298,8 +295,7 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue; outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
} }
return tx; return tx;
}(); }());
txs.push_back(tx);
} }
if (fuzzed_data_provider.ConsumeBool()) { if (fuzzed_data_provider.ConsumeBool()) {
@ -308,20 +304,15 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
PickValue(fuzzed_data_provider, mempool_outpoints).hash; PickValue(fuzzed_data_provider, mempool_outpoints).hash;
const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN); const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
// We only prioritise out of mempool transactions since PrioritiseTransaction doesn't // We only prioritise out of mempool transactions since PrioritiseTransaction doesn't
// filter for ephemeral dust GetEntry // filter for ephemeral dust
if (tx_pool.exists(GenTxid::Txid(txid))) { if (tx_pool.exists(GenTxid::Txid(txid))) {
const auto tx_info{tx_pool.info(GenTxid::Txid(txid))}; const auto tx_info{tx_pool.info(GenTxid::Txid(txid))};
if (GetDustIndexes(tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) { if (GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
tx_pool.PrioritiseTransaction(txid.ToUint256(), delta); tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
} }
} }
} }
// Remember all added transactions
std::set<CTransactionRef> added;
auto txr = std::make_shared<TransactionsDelta>(added);
node.validation_signals->RegisterSharedValidationInterface(txr);
auto single_submit = txs.size() == 1; auto single_submit = txs.size() == 1;
const auto result_package = WITH_LOCK(::cs_main, const auto result_package = WITH_LOCK(::cs_main,
@ -339,7 +330,6 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
} }
node.validation_signals->SyncWithValidationInterfaceQueue(); node.validation_signals->SyncWithValidationInterfaceQueue();
node.validation_signals->UnregisterSharedValidationInterface(txr);
CheckMempoolEphemeralInvariants(tx_pool); CheckMempoolEphemeralInvariants(tx_pool);
} }
@ -374,7 +364,7 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
chainstate.SetMempool(&tx_pool); chainstate.SetMempool(&tx_pool);
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300) LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
{ {
Assert(!mempool_outpoints.empty()); Assert(!mempool_outpoints.empty());
@ -384,18 +374,15 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
const auto num_txs = (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 26); const auto num_txs = (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 26);
std::set<COutPoint> package_outpoints; std::set<COutPoint> package_outpoints;
while (txs.size() < num_txs) { while (txs.size() < num_txs) {
// Last transaction in a package needs to be a child of parents to get further in validation
// so the last transaction to be generated(in a >1 package) must spend all package-made outputs
// Note that this test currently only spends package outputs in last transaction.
bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
// Create transaction to add to the mempool // Create transaction to add to the mempool
const CTransactionRef tx = [&] { txs.emplace_back([&] {
CMutableTransaction tx_mut; CMutableTransaction tx_mut;
tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION; tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>(); tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
// Last tx will sweep all outpoints in package // Last transaction in a package needs to be a child of parents to get further in validation
// so the last transaction to be generated(in a >1 package) must spend all package-made outputs
// Note that this test currently only spends package outputs in last transaction.
bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
const auto num_in = last_tx ? package_outpoints.size() : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size()); const auto num_in = last_tx ? package_outpoints.size() : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size());
auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size() * 2); auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size() * 2);
@ -405,7 +392,8 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
CAmount amount_in{0}; CAmount amount_in{0};
for (size_t i = 0; i < num_in; ++i) { for (size_t i = 0; i < num_in; ++i) {
// Pop random outpoint // Pop random outpoint. We erase them to avoid double-spending
// while in this loop, but later add them back (unless last_tx).
auto pop = outpoints.begin(); auto pop = outpoints.begin();
std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1)); std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
const auto outpoint = *pop; const auto outpoint = *pop;
@ -468,8 +456,7 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue; outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
} }
return tx; return tx;
}(); }());
txs.push_back(tx);
} }
if (fuzzed_data_provider.ConsumeBool()) { if (fuzzed_data_provider.ConsumeBool()) {

View file

@ -814,9 +814,11 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
CAmount nDustThreshold = 182 * g_dust.GetFeePerK() / 1000; CAmount nDustThreshold = 182 * g_dust.GetFeePerK() / 1000;
BOOST_CHECK_EQUAL(nDustThreshold, 546); BOOST_CHECK_EQUAL(nDustThreshold, 546);
// Add dust output to take dust slot, still standard! // Add dust outputs up to allowed maximum, still standard!
t.vout.emplace_back(0, t.vout[0].scriptPubKey); for (size_t i{0}; i < MAX_DUST_OUTPUTS_PER_TX; ++i) {
CheckIsStandard(t); t.vout.emplace_back(0, t.vout[0].scriptPubKey);
CheckIsStandard(t);
}
// dust: // dust:
t.vout[0].nValue = nDustThreshold - 1; t.vout[0].nValue = nDustThreshold - 1;
@ -974,9 +976,9 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
CheckIsNotStandard(t, "bare-multisig"); CheckIsNotStandard(t, "bare-multisig");
g_bare_multi = DEFAULT_PERMIT_BAREMULTISIG; g_bare_multi = DEFAULT_PERMIT_BAREMULTISIG;
// Add dust output to take dust slot // Add dust outputs up to allowed maximum
assert(t.vout.size() == 1); assert(t.vout.size() == 1);
t.vout.emplace_back(0, t.vout[0].scriptPubKey); t.vout.insert(t.vout.end(), MAX_DUST_OUTPUTS_PER_TX, {0, t.vout[0].scriptPubKey});
// Check compressed P2PK outputs dust threshold (must have leading 02 or 03) // Check compressed P2PK outputs dust threshold (must have leading 02 or 03)
t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG; t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG;

View file

@ -90,19 +90,22 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
return MakeTransactionRef(mtx); return MakeTransactionRef(mtx);
} }
static constexpr auto NUM_EPHEMERAL_TX_OUTPUTS = 3;
static constexpr auto EPHEMERAL_DUST_INDEX = NUM_EPHEMERAL_TX_OUTPUTS - 1;
// Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout // Same as make_tx but adds 2 normal outputs and 0-value dust to end of vout
static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version) static inline CTransactionRef make_ephemeral_tx(const std::vector<COutPoint>& inputs, int32_t version)
{ {
CMutableTransaction mtx = CMutableTransaction{}; CMutableTransaction mtx = CMutableTransaction{};
mtx.version = version; mtx.version = version;
mtx.vin.resize(inputs.size()); mtx.vin.resize(inputs.size());
mtx.vout.resize(3);
for (size_t i{0}; i < inputs.size(); ++i) { for (size_t i{0}; i < inputs.size(); ++i) {
mtx.vin[i].prevout = inputs[i]; mtx.vin[i].prevout = inputs[i];
} }
for (auto i{0}; i < 3; ++i) { mtx.vout.resize(NUM_EPHEMERAL_TX_OUTPUTS);
for (auto i{0}; i < NUM_EPHEMERAL_TX_OUTPUTS; ++i) {
mtx.vout[i].scriptPubKey = CScript() << OP_TRUE; mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
mtx.vout[i].nValue = (i == 2) ? 0 : 10000; mtx.vout[i].nValue = (i == EPHEMERAL_DUST_INDEX) ? 0 : 10000;
} }
return MakeTransactionRef(mtx); return MakeTransactionRef(mtx);
} }
@ -114,99 +117,159 @@ BOOST_FIXTURE_TEST_CASE(ephemeral_tests, RegTestingSetup)
TestMemPoolEntryHelper entry; TestMemPoolEntryHelper entry;
CTxMemPool::setEntries empty_ancestors; CTxMemPool::setEntries empty_ancestors;
CFeeRate minrelay(1000); TxValidationState child_state;
Txid child_txid;
// Arbitrary non-0 feerate for these tests
CFeeRate dustrelay(DUST_RELAY_TX_FEE);
// Basic transaction with dust // Basic transaction with dust
auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2); auto grandparent_tx_1 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
const auto dust_txid = grandparent_tx_1->GetHash(); const auto dust_txid = grandparent_tx_1->GetHash();
uint32_t dust_index = 2;
// Child transaction spending dust // Child transaction spending dust
auto dust_spend = make_tx({COutPoint{dust_txid, dust_index}}, /*version=*/2); auto dust_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
// We first start with nothing "in the mempool", using package checks // We first start with nothing "in the mempool", using package checks
// Trivial single transaction with no dust // Trivial single transaction with no dust
BOOST_CHECK(!CheckEphemeralSpends({dust_spend}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({dust_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Now with dust, ok because the tx has no dusty parents // Now with dust, ok because the tx has no dusty parents
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Dust checks pass // Dust checks pass
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend}, CFeeRate(0), pool, child_state, child_txid));
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend}, minrelay, pool).has_value()); BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
auto dust_non_spend = make_tx({COutPoint{dust_txid, dust_index - 1}}, /*version=*/2); auto dust_non_spend = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
// Child spending non-dust only from parent should be disallowed even if dust otherwise spent // Child spending non-dust only from parent should be disallowed even if dust otherwise spent
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, minrelay, pool).has_value()); const auto dust_non_spend_txid{dust_non_spend->GetHash()};
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, minrelay, pool).has_value()); BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_non_spend, dust_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, minrelay, pool).has_value()); BOOST_CHECK(!child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);
child_state = TxValidationState();
child_txid = Txid();
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_spend, dust_non_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(!child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);
child_state = TxValidationState();
child_txid = Txid();
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, dust_non_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(!child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, dust_non_spend_txid);
child_state = TxValidationState();
child_txid = Txid();
auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2); auto grandparent_tx_2 = make_ephemeral_tx(random_outpoints(1), /*version=*/2);
const auto dust_txid_2 = grandparent_tx_2->GetHash(); const auto dust_txid_2 = grandparent_tx_2->GetHash();
// Spend dust from one but not another is ok, as long as second grandparent has no child // Spend dust from one but not another is ok, as long as second grandparent has no child
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index - 1}}, /*version=*/2); auto dust_non_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX - 1}}, /*version=*/2);
// But if we spend from the parent, it must spend dust // But if we spend from the parent, it must spend dust
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_non_spend_both_parents}, minrelay, pool).has_value()); BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_non_spend_both_parents}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(!child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, dust_non_spend_both_parents->GetHash());
child_state = TxValidationState();
child_txid = Txid();
auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2); auto dust_spend_both_parents = make_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_both_parents}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Spending other outputs is also correct, as long as the dusty one is spent // Spending other outputs is also correct, as long as the dusty one is spent
const std::vector<COutPoint> all_outpoints{COutPoint(dust_txid, 0), COutPoint(dust_txid, 1), COutPoint(dust_txid, 2), const std::vector<COutPoint> all_outpoints{COutPoint(dust_txid, 0), COutPoint(dust_txid, 1), COutPoint(dust_txid, 2),
COutPoint(dust_txid_2, 0), COutPoint(dust_txid_2, 1), COutPoint(dust_txid_2, 2)}; COutPoint(dust_txid_2, 0), COutPoint(dust_txid_2, 1), COutPoint(dust_txid_2, 2)};
auto dust_spend_all_outpoints = make_tx(all_outpoints, /*version=*/2); auto dust_spend_all_outpoints = make_tx(all_outpoints, /*version=*/2);
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, dust_spend_all_outpoints}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with no dust
auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, dust_index}, COutPoint{dust_txid_2, dust_index}}, /*version=*/2); auto parent_with_dust = make_ephemeral_tx({COutPoint{dust_txid, EPHEMERAL_DUST_INDEX}, COutPoint{dust_txid_2, EPHEMERAL_DUST_INDEX}}, /*version=*/2);
// Ok for parent to have dust // Ok for parent to have dust
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust}, dustrelay, pool, child_state, child_txid));
auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2); BOOST_CHECK(child_state.IsValid());
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, minrelay, pool).has_value()); BOOST_CHECK_EQUAL(child_txid, Txid());
auto child_no_dust = make_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_no_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust // 2 grandparents with dust <- 1 dust-spending parent with dust <- child with dust
auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), dust_index}}, /*version=*/2); auto child_with_dust = make_ephemeral_tx({COutPoint{parent_with_dust->GetHash(), EPHEMERAL_DUST_INDEX}}, /*version=*/2);
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1, grandparent_tx_2, parent_with_dust, child_with_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Tests with parents in mempool // Tests with parents in mempool
// Nothing in mempool, this should pass for any transaction // Nothing in mempool, this should pass for any transaction
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_1}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_1}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Add first grandparent to mempool and fetch entry // Add first grandparent to mempool and fetch entry
AddToMempool(pool, entry.FromTx(grandparent_tx_1)); AddToMempool(pool, entry.FromTx(grandparent_tx_1));
// Ignores ancestors that aren't direct parents // Ignores ancestors that aren't direct parents
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({child_no_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Valid spend of dust with grandparent in mempool // Valid spend of dust with grandparent in mempool
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({parent_with_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Second grandparent in same package // Second grandparent in same package
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust, grandparent_tx_2}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({parent_with_dust, grandparent_tx_2}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Order in package doesn't matter // Order in package doesn't matter
BOOST_CHECK(!CheckEphemeralSpends({grandparent_tx_2, parent_with_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({grandparent_tx_2, parent_with_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Add second grandparent to mempool // Add second grandparent to mempool
AddToMempool(pool, entry.FromTx(grandparent_tx_2)); AddToMempool(pool, entry.FromTx(grandparent_tx_2));
// Only spends single dust out of two direct parents // Only spends single dust out of two direct parents
BOOST_CHECK(CheckEphemeralSpends({dust_non_spend_both_parents}, minrelay, pool).has_value()); BOOST_CHECK(!CheckEphemeralSpends({dust_non_spend_both_parents}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(!child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, dust_non_spend_both_parents->GetHash());
child_state = TxValidationState();
child_txid = Txid();
// Spends both parents' dust // Spends both parents' dust
BOOST_CHECK(!CheckEphemeralSpends({parent_with_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({parent_with_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
// Now add dusty parent to mempool // Now add dusty parent to mempool
AddToMempool(pool, entry.FromTx(parent_with_dust)); AddToMempool(pool, entry.FromTx(parent_with_dust));
// Passes dust checks even with non-parent ancestors // Passes dust checks even with non-parent ancestors
BOOST_CHECK(!CheckEphemeralSpends({child_no_dust}, minrelay, pool).has_value()); BOOST_CHECK(CheckEphemeralSpends({child_no_dust}, dustrelay, pool, child_state, child_txid));
BOOST_CHECK(child_state.IsValid());
BOOST_CHECK_EQUAL(child_txid, Txid());
} }
BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup) BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup)

View file

@ -141,24 +141,13 @@ std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
return std::nullopt; return std::nullopt;
} }
std::vector<uint32_t> GetDustIndexes(const CTransactionRef& tx_ref, CFeeRate dust_relay_rate)
{
std::vector<uint32_t> dust_indexes;
for (size_t i = 0; i < tx_ref->vout.size(); ++i) {
const auto& output = tx_ref->vout[i];
if (IsDust(output, dust_relay_rate)) dust_indexes.push_back(i);
}
return dust_indexes;
}
void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool) void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool)
{ {
LOCK(tx_pool.cs); LOCK(tx_pool.cs);
for (const auto& tx_info : tx_pool.infoAll()) { for (const auto& tx_info : tx_pool.infoAll()) {
const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash())); const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
std::vector<uint32_t> dust_indexes = GetDustIndexes(tx_info.tx, tx_pool.m_opts.dust_relay_feerate); std::vector<uint32_t> dust_indexes = GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate);
Assert(dust_indexes.size() < 2); Assert(dust_indexes.size() < 2);

View file

@ -54,11 +54,6 @@ std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
*/ */
void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool); void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool);
/** Return indexes of the transaction's outputs that are considered dust
* at given dust_relay_rate.
*/
std::vector<uint32_t> GetDustIndexes(const CTransactionRef& tx_ref, CFeeRate dust_relay_rate);
/** For every transaction in tx_pool, check TRUC invariants: /** For every transaction in tx_pool, check TRUC invariants:
* - a TRUC tx's ancestor count must be within TRUC_ANCESTOR_LIMIT * - a TRUC tx's ancestor count must be within TRUC_ANCESTOR_LIMIT
* - a TRUC tx's descendant count must be within TRUC_DESCENDANT_LIMIT * - a TRUC tx's descendant count must be within TRUC_DESCENDANT_LIMIT

View file

@ -913,8 +913,8 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
// Enforces 0-fee for dust transactions, no incentive to be mined alone // Enforces 0-fee for dust transactions, no incentive to be mined alone
if (m_pool.m_opts.require_standard) { if (m_pool.m_opts.require_standard) {
if (!CheckValidEphemeralTx(ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) { if (!PreCheckEphemeralTx(*ptx, m_pool.m_opts.dust_relay_feerate, ws.m_base_fees, ws.m_modified_fees, state)) {
return false; // state filled in by CheckValidEphemeralTx return false; // state filled in by PreCheckEphemeralTx
} }
} }
@ -1436,11 +1436,8 @@ MempoolAcceptResult MemPoolAccept::AcceptSingleTransaction(const CTransactionRef
} }
if (m_pool.m_opts.require_standard) { if (m_pool.m_opts.require_standard) {
if (const auto ephemeral_violation{CheckEphemeralSpends(/*package=*/{ptx}, m_pool.m_opts.dust_relay_feerate, m_pool)}) { Txid dummy_txid;
const Txid& txid = ephemeral_violation.value(); if (!CheckEphemeralSpends(/*package=*/{ptx}, m_pool.m_opts.dust_relay_feerate, m_pool, ws.m_state, dummy_txid)) {
Assume(txid == ptx->GetHash());
ws.m_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
strprintf("tx %s did not spend parent's ephemeral dust", txid.ToString()));
return MempoolAcceptResult::Failure(ws.m_state); return MempoolAcceptResult::Failure(ws.m_state);
} }
} }
@ -1592,11 +1589,9 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::
// Now that we've bounded the resulting possible ancestry count, check package for dust spends // Now that we've bounded the resulting possible ancestry count, check package for dust spends
if (m_pool.m_opts.require_standard) { if (m_pool.m_opts.require_standard) {
if (const auto ephemeral_violation{CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool)}) { TxValidationState child_state;
const Txid& child_txid = ephemeral_violation.value(); Txid child_txid;
TxValidationState child_state; if (!CheckEphemeralSpends(txns, m_pool.m_opts.dust_relay_feerate, m_pool, child_state, child_txid)) {
child_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
strprintf("tx %s did not spend parent's ephemeral dust", child_txid.ToString()));
package_state.Invalid(PackageValidationResult::PCKG_TX, "unspent-dust"); package_state.Invalid(PackageValidationResult::PCKG_TX, "unspent-dust");
results.emplace(child_txid, MempoolAcceptResult::Failure(child_state)); results.emplace(child_txid, MempoolAcceptResult::Failure(child_state));
return PackageMempoolAcceptResult(package_state, std::move(results)); return PackageMempoolAcceptResult(package_state, std::move(results));

View file

@ -78,8 +78,8 @@ class DustRelayFeeTest(BitcoinTestFramework):
assert_equal(self.nodes[0].getrawmempool(), []) assert_equal(self.nodes[0].getrawmempool(), [])
# Double dust, both unspent, with fees. Would have failed individual checks. # Create two dust outputs. Transaction has zero fees. both dust outputs are unspent, and would have failed individual checks.
# Dust is 1 satoshi create_self_transfer_multi disallows 0 # The amount is 1 satoshi because create_self_transfer_multi disallows 0.
dusty_tx = self.wallet.create_self_transfer_multi(fee_per_output=1000, amount_per_output=1, num_outputs=2) dusty_tx = self.wallet.create_self_transfer_multi(fee_per_output=1000, amount_per_output=1, num_outputs=2)
dust_txid = self.nodes[0].sendrawtransaction(hexstring=dusty_tx["hex"], maxfeerate=0) dust_txid = self.nodes[0].sendrawtransaction(hexstring=dusty_tx["hex"], maxfeerate=0)

View file

@ -61,7 +61,7 @@ class EphemeralDustTest(BitcoinTestFramework):
self.test_non_truc() self.test_non_truc()
self.test_unspent_ephemeral() self.test_unspent_ephemeral()
self.test_reorgs() self.test_reorgs()
self.test_free_relay() self.test_no_minrelay_fee()
def test_normal_dust(self): def test_normal_dust(self):
self.log.info("Create 0-value dusty output, show that it works inside truc when spent in package") self.log.info("Create 0-value dusty output, show that it works inside truc when spent in package")
@ -363,7 +363,7 @@ class EphemeralDustTest(BitcoinTestFramework):
self.nodes[0].invalidateblock(block_res["hash"]) self.nodes[0].invalidateblock(block_res["hash"])
assert_mempool_contents(self, self.nodes[0], expected=[dusty_tx["tx"]], sync=False) assert_mempool_contents(self, self.nodes[0], expected=[dusty_tx["tx"]], sync=False)
# Also should happen if dust is swept # Should re-enter if dust is swept
sweep_tx_2 = self.wallet.create_self_transfer_multi(fee_per_output=0, utxos_to_spend=dusty_tx["new_utxos"], version=3) sweep_tx_2 = self.wallet.create_self_transfer_multi(fee_per_output=0, utxos_to_spend=dusty_tx["new_utxos"], version=3)
self.add_output_to_create_multi_result(sweep_tx_2) self.add_output_to_create_multi_result(sweep_tx_2)
assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, sweep_tx_2["hex"]) assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, sweep_tx_2["hex"])
@ -401,7 +401,7 @@ class EphemeralDustTest(BitcoinTestFramework):
self.sync_all() self.sync_all()
# N.B. this extra_args can be removed post cluster mempool # N.B. this extra_args can be removed post cluster mempool
def test_free_relay(self): def test_no_minrelay_fee(self):
self.log.info("Test that ephemeral dust works in non-TRUC contexts when there's no minrelay requirement") self.log.info("Test that ephemeral dust works in non-TRUC contexts when there's no minrelay requirement")
# Note: since minrelay is 0, it is not testing 1P1C relay # Note: since minrelay is 0, it is not testing 1P1C relay
@ -462,15 +462,17 @@ class EphemeralDustTest(BitcoinTestFramework):
# Sweeps all dust, where all dusty txs are already in-mempool # Sweeps all dust, where all dusty txs are already in-mempool
sweep_tx = self.wallet.create_self_transfer_multi(fee_per_output=25000, utxos_to_spend=all_parent_utxos, version=2) sweep_tx = self.wallet.create_self_transfer_multi(fee_per_output=25000, utxos_to_spend=all_parent_utxos, version=2)
# N.B. Since we have multiple parents these are not propagating via 1P1C relay.
# minrelay being zero allows them to propagate on their own.
res = self.nodes[0].submitpackage([dusty_tx["hex"] for dusty_tx in dusty_txs] + [sweep_tx["hex"]]) res = self.nodes[0].submitpackage([dusty_tx["hex"] for dusty_tx in dusty_txs] + [sweep_tx["hex"]])
assert_equal(res['package_msg'], "success") assert_equal(res['package_msg'], "success")
assert_mempool_contents(self, self.nodes[0], expected=[dusty_tx["tx"] for dusty_tx in dusty_txs] + [sweep_tx["tx"], cancel_sweep["tx"]]) assert_mempool_contents(self, self.nodes[0], expected=[dusty_tx["tx"] for dusty_tx in dusty_txs] + [sweep_tx["tx"], cancel_sweep["tx"]])
self.generate(self.nodes[0], 25) self.generate(self.nodes[0], 1)
self.wallet.rescan_utxos() self.wallet.rescan_utxos()
assert_equal(self.nodes[0].getrawmempool(), []) assert_equal(self.nodes[0].getrawmempool(), [])
# Other topology tests require relaxation of submitpackage topology # Other topology tests (e.g., grandparents and parents both with dust) require relaxation of submitpackage topology
self.restart_node(0, extra_args=[]) self.restart_node(0, extra_args=[])
self.restart_node(1, extra_args=[]) self.restart_node(1, extra_args=[])

View file

@ -30,6 +30,7 @@ def assert_mempool_contents(test_framework, node, expected=None, sync=True):
test_framework.sync_mempools() test_framework.sync_mempools()
if not expected: if not expected:
expected = [] expected = []
assert_equal(len(expected), len(set(expected)))
mempool = node.getrawmempool(verbose=False) mempool = node.getrawmempool(verbose=False)
assert_equal(len(mempool), len(expected)) assert_equal(len(mempool), len(expected))
for tx in expected: for tx in expected: