diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 58e0d0dbfec6..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 @@ -2443,15 +2445,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