From 7dc17f60545291500ae711788f9695cf663aec08 Mon Sep 17 00:00:00 2001 From: pasta Date: Thu, 12 Nov 2026 23:49:46 -0600 Subject: [PATCH 1/2] fix(coinjoin): schedule auto-denomination per wallet DoMaintenance kept its tick counter and next-run tick in function statics shared by every wallet's CCoinJoinClientManager. With several mixing wallets the shared counter advanced once per wallet per scheduler tick, so only the wallet whose call happened to land on the scheduled tick ran DoAutomaticDenominating. Keep the counters per manager. Co-Authored-By: Claude Fable 5.2 --- src/coinjoin/client.cpp | 9 +++----- src/coinjoin/client.h | 3 +++ src/wallet/test/coinjoin_tests.cpp | 33 ++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 58e0d0dbfec6..4868984530b9 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -2443,15 +2443,12 @@ void CCoinJoinClientManager::DoMaintenance(ChainstateManager& chainman, CConnman if (!m_mn_sync.IsBlockchainSynced() || ShutdownRequested()) return; - static int nTick = 0; - static int nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN; - - nTick++; + m_tick++; CheckTimeout(); ProcessPendingDsaRequest(connman); - if (nDoAutoNextRun == nTick) { + if (m_do_auto_next_run == m_tick) { DoAutomaticDenominating(chainman, connman, mempool); - nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN + GetRand(/*nMax=*/COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN); + m_do_auto_next_run = m_tick + COINJOIN_AUTO_TIMEOUT_MIN + GetRand(/*nMax=*/COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN); } } diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index 124dbc8aa93b..7eebf4433e34 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -227,6 +227,9 @@ class CCoinJoinClientManager : public interfaces::CoinJoin::Client // Keep track of current block height int nCachedBlockHeight{0}; + int m_tick{0}; + int m_do_auto_next_run{COINJOIN_AUTO_TIMEOUT_MIN}; + bool fCreateAutoBackups{true}; // builtin support for automatic backups bool WaitForAnotherBlock() const; diff --git a/src/wallet/test/coinjoin_tests.cpp b/src/wallet/test/coinjoin_tests.cpp index a799fda06072..266a618688c9 100644 --- a/src/wallet/test/coinjoin_tests.cpp +++ b/src/wallet/test/coinjoin_tests.cpp @@ -232,6 +232,39 @@ class CTransactionBuilderTestSetup : public TestChain100Setup } }; +BOOST_FIXTURE_TEST_CASE(coinjoin_maintenance_runs_for_every_wallet, CTransactionBuilderTestSetup) +{ + struct RestoreOptions { + const bool enabled{CCoinJoinClientOptions::IsEnabled()}; + const int backups{nWalletBackups}; + ~RestoreOptions() + { + CCoinJoinClientOptions::SetEnabled(enabled); + nWalletBackups = backups; + } + } restore_options; + CCoinJoinClientOptions::SetEnabled(true); + nWalletBackups = 0; + m_node.mn_sync->SwitchToNextAsset(); + BOOST_REQUIRE(m_node.mn_sync->IsBlockchainSynced()); + + auto other_wallet{std::make_shared(m_node.chain.get(), "other", CreateMockableWalletDatabase())}; + other_wallet->SetupLegacyScriptPubKeyMan(); + CCoinJoinClientManager first(wallet, *m_node.dmnman, *m_node.mn_metaman, *m_node.mn_sync, *m_node.isman, nullptr); + CCoinJoinClientManager second(other_wallet, *m_node.dmnman, *m_node.mn_metaman, *m_node.mn_sync, *m_node.isman, nullptr); + BOOST_REQUIRE(first.startMixing()); + BOOST_REQUIRE(second.startMixing()); + for (int tick{0}; tick < COINJOIN_AUTO_TIMEOUT_MIN; ++tick) { + BOOST_CHECK(first.isMixing()); + BOOST_CHECK(second.isMixing()); + first.DoMaintenance(*m_node.chainman, *m_node.connman, *m_node.mempool); + second.DoMaintenance(*m_node.chainman, *m_node.connman, *m_node.mempool); + } + // Each wallet's first automatic attempt stops mixing when automatic backups are disabled. + BOOST_CHECK(!first.isMixing()); + BOOST_CHECK(!second.isMixing()); +} + BOOST_FIXTURE_TEST_CASE(coinjoin_pending_observation_tests, CTransactionBuilderTestSetup) { // 0.100001 DASH, a valid CoinJoin denomination From 51134ab743fa07ee5ac3a48c093308eda56f4a75 Mon Sep 17 00:00:00 2001 From: pasta Date: Thu, 12 Nov 2026 23:49:46 -0600 Subject: [PATCH 2/2] fix(coinjoin): don't dereference an empty round list in SubmitDenominate Input selection excludes fully mixed coins using the rounds setting at selection time, while the dry-run loop reads GetRounds() again. SetRounds (the setcoinjoinrounds RPC or the GUI) does not take cs_wallet, so lowering the target between the two can leave every selected input outside the loop's range. vecInputsByRounds then stays empty and begin()->first is undefined behaviour. Skip the per-round attempt in that case and fall through to the existing all-rounds attempt, which fails cleanly. Co-Authored-By: Claude Fable 5.2 --- src/coinjoin/client.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 4868984530b9..f005d4367b01 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -1794,10 +1794,12 @@ bool CCoinJoinClientSession::SubmitDenominate(CConnman& connman) WalletCJLogPrint(m_wallet, "vecInputsByRounds: rounds: %d, inputs: %d\n", pair.first, pair.second); } - int nRounds = vecInputsByRounds.begin()->first; - if (PrepareDenominate(nRounds, nRounds, strError, vecTxDSIn, vecPSInOutPairsTmp)) { - WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, success\n", nRounds); - return SendDenominate(vecPSInOutPairsTmp, connman); + if (!vecInputsByRounds.empty()) { + int nRounds = vecInputsByRounds.begin()->first; + if (PrepareDenominate(nRounds, nRounds, strError, vecTxDSIn, vecPSInOutPairsTmp)) { + WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, success\n", nRounds); + return SendDenominate(vecPSInOutPairsTmp, connman); + } } // We failed? That's strange but let's just make final attempt and try to mix everything