Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/wallet/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,15 @@ std::vector<COutPoint> 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<uint256> setRecentTxIds;
std::vector<COutPoint> vecSiblings;
std::vector<COutput> vCoins{AvailableCoinsListUnspent(*this, &coin_control).All()};

WalletCJLogPrint(this, "CWallet::%s -- vCoins.size(): %d\n", __func__, vCoins.size());
Expand All @@ -479,8 +482,15 @@ std::vector<COutPoint> CWallet::SelectFullyMixedForPromotion(int nDenom, int nCo
for (const auto& out : vCoins) {
if (static_cast<int>(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);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
for (const auto& outpoint : vecSiblings) {
if (static_cast<int>(vecRet.size()) >= nCount) break;
vecRet.push_back(outpoint);
}

return vecRet;
Expand Down
74 changes: 74 additions & 0 deletions src/wallet/test/coinjoin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<COutPoint> vecIn, size_t nInputsPerTx) {
BOOST_REQUIRE(nInputsPerTx > 0 && vecIn.size() % nInputsPerTx == 0);
for (int i = 0; i < CCoinJoinClientOptions::GetRounds() + CCoinJoinClientOptions::GetRandomRounds(); ++i) {
std::vector<COutPoint> 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<COutPoint>& vecOutpoints) {
std::set<uint256> 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<CAmount>(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<size_t>(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<CAmount>(CoinJoin::PROMOTION_RATIO - 2, nSmallerAmount)).outpoints, 1);
vecSelected = wallet->SelectFullyMixedForPromotion(nSmallerDenom, CoinJoin::PROMOTION_RATIO);
BOOST_CHECK_EQUAL(vecSelected.size(), static_cast<size_t>(CoinJoin::PROMOTION_RATIO));
BOOST_CHECK_EQUAL(count_parents(vecSelected), static_cast<size_t>(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
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading