From 877b6cf7d66a7805923f8223bee9a426cf6e9ef5 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 31 Oct 2026 03:48:28 +0000 Subject: [PATCH] fix(wallet): fill a CoinJoin promotion with sibling coins when needed GetDenominationCounts() counted every fully-mixed coin, but SelectFullyMixedForPromotion() took at most one coin per parent transaction. Standard mixing clusters up to 9 of a wallet's coins in each mixing transaction, so wallets routinely held PROMOTION_RATIO fully-mixed coins spread over fewer than PROMOTION_RATIO transactions. Such a wallet reported a promotion opportunity on every attempt, failed selection every time, never promoted, and skipped the "nothing to do" exits it would otherwise take. Keep preferring coins from distinct transactions, but fill the rest with siblings of coins already taken. The one-per-parent rule comes from standard mixing, where a round weakened by sibling inputs would still be credited as a full round. A promotion output starts mixing over at 0 rounds, and spending siblings together reveals no more than sending them would. Co-Authored-By: Claude Opus 5.5 --- src/wallet/coinjoin.cpp | 22 ++++++--- src/wallet/test/coinjoin_tests.cpp | 74 ++++++++++++++++++++++++++++++ src/wallet/wallet.h | 6 +-- 3 files changed, 93 insertions(+), 9 deletions(-) diff --git a/src/wallet/coinjoin.cpp b/src/wallet/coinjoin.cpp index 1421251034d4..8effeb405daf 100644 --- a/src/wallet/coinjoin.cpp +++ b/src/wallet/coinjoin.cpp @@ -464,12 +464,15 @@ std::vector CWallet::SelectFullyMixedForPromotion(int nDenom, int nCo LOCK(cs_wallet); - // Mirror SelectTxDSInsByDenomination: spendable coins only, shuffled, and at most one - // coin per transaction. Inputs sharing a parent tx are publicly linked on-chain, and a - // demotion in particular leaves 10 sibling outputs of the target denomination that must - // never be promoted together - they would be identifiable as one group in the final tx. + // Spendable coins only, shuffled, preferring coins from distinct transactions. Unlike + // SelectTxDSInsByDenomination this falls back to siblings: standard mixing leaves a + // wallet's fully-mixed coins clustered up to COINJOIN_ENTRY_MAX_SIZE per mixing tx, so a + // strict one-per-tx rule would rarely find PROMOTION_RATIO of them. Spending siblings + // together links them in their last mixing round, as sending them would, but no round is + // credited for it: the promotion output starts mixing over at 0 rounds. CCoinControl coin_control(CoinType::ONLY_FULLY_MIXED); std::set setRecentTxIds; + std::vector vecSiblings; std::vector vCoins{AvailableCoinsListUnspent(*this, &coin_control).All()}; WalletCJLogPrint(this, "CWallet::%s -- vCoins.size(): %d\n", __func__, vCoins.size()); @@ -479,8 +482,15 @@ std::vector CWallet::SelectFullyMixedForPromotion(int nDenom, int nCo for (const auto& out : vCoins) { if (static_cast(vecRet.size()) >= nCount) break; if (out.txout.nValue != nDenomAmount) continue; - if (!setRecentTxIds.insert(out.outpoint.hash).second) continue; // no duplicate txids - vecRet.push_back(out.outpoint); + if (setRecentTxIds.insert(out.outpoint.hash).second) { + vecRet.push_back(out.outpoint); + } else { + vecSiblings.push_back(out.outpoint); + } + } + for (const auto& outpoint : vecSiblings) { + if (static_cast(vecRet.size()) >= nCount) break; + vecRet.push_back(outpoint); } return vecRet; diff --git a/src/wallet/test/coinjoin_tests.cpp b/src/wallet/test/coinjoin_tests.cpp index ed25629ecd1d..33ff44406984 100644 --- a/src/wallet/test/coinjoin_tests.cpp +++ b/src/wallet/test/coinjoin_tests.cpp @@ -597,6 +597,80 @@ BOOST_FIXTURE_TEST_CASE(coinjoin_rebalance_rounds_reset_tests, CTransactionBuild } } +BOOST_FIXTURE_TEST_CASE(coinjoin_promotion_prefers_coins_from_distinct_txs_tests, CTransactionBuilderTestSetup) +{ + constexpr CAmount nSmallerAmount{10000100}; + constexpr CAmount nLargerAmount{nSmallerAmount * CoinJoin::PROMOTION_RATIO}; + const int nSmallerDenom{CoinJoin::AmountToDenomination(nSmallerAmount)}; + const int nLargerDenom{CoinJoin::AmountToDenomination(nLargerAmount)}; + const size_t idxSmaller = CoinJoin::GetDenominationIndex(nSmallerDenom); + const CScript scriptOurs = GetScriptForRawPubKey(coinbaseKey.GetPubKey()); + + const bool fEnabledPrev{CCoinJoinClientOptions::IsEnabled()}; + const int nRoundsPrev{CCoinJoinClientOptions::GetRounds()}; + const int nGoalPrev{CCoinJoinClientOptions::GetDenomsGoal()}; + CCoinJoinClientOptions::SetEnabled(true); + CCoinJoinClientOptions::SetRounds(MIN_COINJOIN_ROUNDS); + CCoinJoinClientOptions::SetDenomsGoal(MIN_COINJOIN_DENOMS_GOAL); + + // Mines zero-fee self-mixing txs of nInputsPerTx inputs and as many outputs each, until + // vecIn is fully mixed whatever the salt decides + const auto mix_fully = [&](std::vector vecIn, size_t nInputsPerTx) { + BOOST_REQUIRE(nInputsPerTx > 0 && vecIn.size() % nInputsPerTx == 0); + for (int i = 0; i < CCoinJoinClientOptions::GetRounds() + CCoinJoinClientOptions::GetRandomRounds(); ++i) { + std::vector vecOut; + for (auto it = vecIn.begin(); it != vecIn.end(); it += nInputsPerTx) { + CMutableTransaction mtx; + for (auto in = it; in != it + nInputsPerTx; ++in) { + mtx.vin.emplace_back(*in); + mtx.vout.emplace_back(nSmallerAmount, scriptOurs); + } + BOOST_REQUIRE(WITH_LOCK(wallet->cs_wallet, return wallet->SignTransaction(mtx))); + BOOST_REQUIRE(wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{})); + AddTxToChain(mtx.GetHash()); + for (uint32_t n = 0; n < mtx.vout.size(); ++n) { + vecOut.emplace_back(mtx.GetHash(), n); + } + } + vecIn = vecOut; + } + for (const auto& outpoint : vecIn) { + BOOST_REQUIRE(wallet->IsFullyMixed(outpoint)); + } + }; + const auto count_parents = [](const std::vector& vecOutpoints) { + std::set setParents; + for (const auto& outpoint : vecOutpoints) { + setParents.insert(outpoint.hash); + } + return setParents.size(); + }; + + // A full mixing entry's worth of fully-mixed coins sharing one parent tx, as standard mixing + // clusters them, plus one coin of its own: the promotion is reported, and selection takes + // the lone coin and fills the rest with siblings + mix_fully(GetTallyItem(std::vector(COINJOIN_ENTRY_MAX_SIZE, nSmallerAmount)).outpoints, + COINJOIN_ENTRY_MAX_SIZE); + mix_fully(GetTallyItem({nSmallerAmount}).outpoints, 1); + const auto counts = wallet->GetDenominationCounts(); + BOOST_CHECK_EQUAL(counts.fully_mixed[idxSmaller], CoinJoin::PROMOTION_RATIO); + BOOST_CHECK(CCoinJoinClientManager::ShouldPromote(nSmallerDenom, nLargerDenom, counts)); + auto vecSelected = wallet->SelectFullyMixedForPromotion(nSmallerDenom, CoinJoin::PROMOTION_RATIO); + BOOST_CHECK_EQUAL(vecSelected.size(), static_cast(CoinJoin::PROMOTION_RATIO)); + BOOST_CHECK_EQUAL(count_parents(vecSelected), 2U); + + // Enough coins of their own for a whole promotion: selection takes every distinct parent + // before any sibling, so only one coin of the cluster is used + mix_fully(GetTallyItem(std::vector(CoinJoin::PROMOTION_RATIO - 2, nSmallerAmount)).outpoints, 1); + vecSelected = wallet->SelectFullyMixedForPromotion(nSmallerDenom, CoinJoin::PROMOTION_RATIO); + BOOST_CHECK_EQUAL(vecSelected.size(), static_cast(CoinJoin::PROMOTION_RATIO)); + BOOST_CHECK_EQUAL(count_parents(vecSelected), static_cast(CoinJoin::PROMOTION_RATIO)); + + CCoinJoinClientOptions::SetDenomsGoal(nGoalPrev); + CCoinJoinClientOptions::SetRounds(nRoundsPrev); + CCoinJoinClientOptions::SetEnabled(fEnabledPrev); +} + BOOST_FIXTURE_TEST_CASE(coinjoin_rounds_cache_unknown_tx_tests, CTransactionBuilderTestSetup) { constexpr CAmount nDenomAmount{10000100}; // 0.100001 DASH diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 6bbab3cee417..a7b95637dad9 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -659,9 +659,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati CoinJoinDenomCounts GetDenominationCounts() const; /** - * Select fully-mixed coins for promotion (post-V24 feature). Selection mirrors - * SelectTxDSInsByDenomination: spendable coins only, shuffled, at most one coin - * per transaction. + * Select fully-mixed coins for promotion (post-V24 feature): spendable coins only, + * shuffled, preferring coins from distinct transactions and only then taking + * siblings of an already selected coin. * @param nDenom The denomination to select (bitshifted integer) * @param nCount Maximum number of coins to select * @return Vector of outpoints for selected coins