From ae246f98aa6b036733c42a03497479af3644a591 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sat, 16 Dec 2023 01:35:04 +0700 Subject: [PATCH] refactor: deglobalization of coinJoinClientManagers --- src/coinjoin/client.cpp | 2 -- src/coinjoin/client.h | 4 ---- src/coinjoin/context.cpp | 11 +---------- src/coinjoin/context.h | 3 ++- src/node/interfaces.cpp | 27 +++++++++++++++------------ src/rpc/coinjoin.cpp | 10 +++++----- src/wallet/test/coinjoin_tests.cpp | 5 +++-- 7 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index e6d59ffa1b90d9..d8ef4918af8ca9 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -29,8 +29,6 @@ #include #include -std::unique_ptr coinJoinClientManagers; - void CCoinJoinClientQueueManager::ProcessMessage(const CNode& peer, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv) { if (fMasternodeMode) return; diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index b777c602c41ae2..f5c45a02404ec9 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -19,7 +19,6 @@ class CCoinJoinClientManager; class CCoinJoinClientQueueManager; class CConnman; class CDeterministicMN; -class CJClientManager; class CNode; class CMasternodeSync; class CTxMemPool; @@ -29,9 +28,6 @@ class UniValue; using CDeterministicMNCPtr = std::shared_ptr; -// The main object for accessing mixing -extern std::unique_ptr coinJoinClientManagers; - class CPendingDsaRequest { private: diff --git a/src/coinjoin/context.cpp b/src/coinjoin/context.cpp index 4e8c7253208c4d..784b81a723e058 100644 --- a/src/coinjoin/context.cpp +++ b/src/coinjoin/context.cpp @@ -15,20 +15,11 @@ CJContext::CJContext(CChainState& chainstate, CConnman& connman, CTxMemPool& mempool, const CMasternodeSync& mn_sync, bool relay_txes) : #ifdef ENABLE_WALLET - clientman { - [&]() -> CJClientManager* const { - assert(::coinJoinClientManagers == nullptr); - ::coinJoinClientManagers = std::make_unique(connman, mempool, mn_sync, queueman); - return ::coinJoinClientManagers.get(); - }() - }, + clientman{std::make_unique(connman, mempool, mn_sync, queueman)}, queueman {relay_txes ? std::make_unique(connman, *clientman, mn_sync) : nullptr}, #endif // ENABLE_WALLET server{std::make_unique(chainstate, connman, mempool, mn_sync)} {} CJContext::~CJContext() { -#ifdef ENABLE_WALLET - ::coinJoinClientManagers.reset(); -#endif // ENABLE_WALLET } diff --git a/src/coinjoin/context.h b/src/coinjoin/context.h index d05da33e3c7c8a..3e9ea08eaec528 100644 --- a/src/coinjoin/context.h +++ b/src/coinjoin/context.h @@ -30,7 +30,8 @@ struct CJContext { ~CJContext(); #ifdef ENABLE_WALLET - CJClientManager* const clientman; + // The main object for accessing mixing + const std::unique_ptr clientman; const std::unique_ptr queueman; #endif // ENABLE_WALLET const std::unique_ptr server; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index d14002fc2de1f7..89400ec142719a 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -57,6 +57,7 @@ #include #include +#include #include #include @@ -222,8 +223,8 @@ class CoinJoinClientImpl : public CoinJoin::Client CCoinJoinClientManager& m_manager; public: - CoinJoinClientImpl(const std::string& walletName) - : m_manager(*Assert(::coinJoinClientManagers->Get(walletName))) {} + CoinJoinClientImpl(CCoinJoinClientManager& clientManager) + : m_manager(clientManager) {} void resetCachedBlocks() override { @@ -497,8 +498,10 @@ class NodeImpl : public Node LLMQ& llmq() override { return m_llmq; } Masternode::Sync& masternodeSync() override { return m_masternodeSync; } CoinJoin::Options& coinJoinOptions() override { return m_coinjoin; } - std::unique_ptr coinJoinClient(const std::string& walletName) override { return std::make_unique(walletName); } - + std::unique_ptr coinJoinClient(const std::string& walletName) override { + assert(m_context->cj_ctx != nullptr); + return std::make_unique(*Assert(m_context->cj_ctx->clientman->Get(walletName))); + } std::unique_ptr handleInitMessage(InitMessageFn fn) override { return MakeHandler(::uiInterface.InitMessage_connect(fn)); @@ -965,26 +968,26 @@ class ChainImpl : public Chain #ifdef ENABLE_WALLET void cjAddWallet(CWallet& wallet) override { - assert(::coinJoinClientManagers != nullptr); - ::coinJoinClientManagers->Add(wallet); + assert(m_node.cj_ctx != nullptr); + m_node.cj_ctx->clientman->Add(wallet); } void cjRemoveWallet(const std::string& name) override { - assert(::coinJoinClientManagers != nullptr); - ::coinJoinClientManagers->Remove(name); + assert(m_node.cj_ctx != nullptr); + m_node.cj_ctx->clientman->Remove(name); } void cjFlushWallet(const CWallet& wallet) override { - assert(::coinJoinClientManagers != nullptr); - auto cj_clientman = ::coinJoinClientManagers->Get(wallet); + assert(m_node.cj_ctx != nullptr); + auto cj_clientman = m_node.cj_ctx->clientman->Get(wallet); assert(cj_clientman != nullptr); cj_clientman->ResetPool(); cj_clientman->StopMixing(); } void cjStopMixingWallet(const CWallet& wallet) override { - assert(::coinJoinClientManagers != nullptr); - auto cj_clientman = ::coinJoinClientManagers->Get(wallet); + assert(m_node.cj_ctx != nullptr); + auto cj_clientman = m_node.cj_ctx->clientman->Get(wallet); if (cj_clientman != nullptr) cj_clientman->StopMixing(); } #endif // ENABLE_WALLET diff --git a/src/rpc/coinjoin.cpp b/src/rpc/coinjoin.cpp index 2dd16529eb52e1..209b29e4006a15 100644 --- a/src/rpc/coinjoin.cpp +++ b/src/rpc/coinjoin.cpp @@ -51,7 +51,8 @@ static UniValue coinjoin(const JSONRPCRequest& request) } } - auto cj_clientman = ::coinJoinClientManagers->Get(*wallet); + const NodeContext& node = EnsureAnyNodeContext(request.context); + auto cj_clientman = node.cj_ctx->clientman->Get(wallet->GetName()); CHECK_NONFATAL(cj_clientman != nullptr); if (request.params[0].get_str() == "start") { @@ -61,13 +62,12 @@ static UniValue coinjoin(const JSONRPCRequest& request) throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please unlock wallet for mixing with walletpassphrase first."); } + CTxMemPool& mempool = EnsureMemPool(node); + CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(node); if (!cj_clientman->StartMixing()) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Mixing has been started already."); } - const NodeContext& node = EnsureAnyNodeContext(request.context); - CTxMemPool& mempool = EnsureMemPool(node); - CBlockPolicyEstimator& fee_estimator = EnsureFeeEstimator(node); bool result = cj_clientman->DoAutomaticDenominating(*node.connman, fee_estimator, mempool); return "Mixing " + (result ? "started successfully" : ("start failed: " + cj_clientman->GetStatuses().original + ", will retry")); } @@ -163,7 +163,7 @@ static UniValue getcoinjoininfo(const JSONRPCRequest& request) return obj; } - auto manager = ::coinJoinClientManagers->Get(*wallet); + auto manager = node.cj_ctx->clientman->Get(wallet->GetName()); CHECK_NONFATAL(manager != nullptr); manager->GetJsonInfo(obj); diff --git a/src/wallet/test/coinjoin_tests.cpp b/src/wallet/test/coinjoin_tests.cpp index 81ff7aa702f41e..ae4d48f1fb4a3c 100644 --- a/src/wallet/test/coinjoin_tests.cpp +++ b/src/wallet/test/coinjoin_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -207,8 +208,8 @@ class CTransactionBuilderTestSetup : public TestChain100Setup BOOST_FIXTURE_TEST_CASE(coinjoin_manager_start_stop_tests, CTransactionBuilderTestSetup) { - BOOST_CHECK_EQUAL(::coinJoinClientManagers->raw().size(), 1); - auto& cj_man = ::coinJoinClientManagers->raw().begin()->second; + BOOST_CHECK_EQUAL(m_node.cj_ctx->clientman->raw().size(), 1); + auto& cj_man = m_node.cj_ctx->clientman->raw().begin()->second; BOOST_CHECK_EQUAL(cj_man->IsMixing(), false); BOOST_CHECK_EQUAL(cj_man->StartMixing(), true); BOOST_CHECK_EQUAL(cj_man->IsMixing(), true);