Skip to content

Commit

Permalink
refactor: deglobalization of ::coinJoinClientManagers
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Dec 16, 2023
1 parent 6dd7282 commit 9309fd7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
2 changes: 0 additions & 2 deletions src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include <memory>
#include <univalue.h>

std::unique_ptr<CJClientManager> coinJoinClientManagers;

void CCoinJoinClientQueueManager::ProcessMessage(const CNode& peer, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv)
{
if (fMasternodeMode) return;
Expand Down
4 changes: 0 additions & 4 deletions src/coinjoin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class CCoinJoinClientManager;
class CCoinJoinClientQueueManager;
class CConnman;
class CDeterministicMN;
class CJClientManager;
class CNode;
class CMasternodeSync;
class CTxMemPool;
Expand All @@ -29,9 +28,6 @@ class UniValue;

using CDeterministicMNCPtr = std::shared_ptr<const CDeterministicMN>;

// The main object for accessing mixing
extern std::unique_ptr<CJClientManager> coinJoinClientManagers;

class CPendingDsaRequest
{
private:
Expand Down
11 changes: 1 addition & 10 deletions src/coinjoin/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CJClientManager>(connman, mempool, mn_sync, queueman);
return ::coinJoinClientManagers.get();
}()
},
clientman{std::make_unique<CJClientManager>(connman, mempool, mn_sync, queueman)},
queueman {relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(connman, *clientman, mn_sync) : nullptr},
#endif // ENABLE_WALLET
server{std::make_unique<CCoinJoinServer>(chainstate, connman, mempool, mn_sync)}
{}

CJContext::~CJContext() {
#ifdef ENABLE_WALLET
::coinJoinClientManagers.reset();
#endif // ENABLE_WALLET
}
3 changes: 2 additions & 1 deletion src/coinjoin/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct CJContext {
~CJContext();

#ifdef ENABLE_WALLET
CJClientManager* const clientman;
// The main object for accessing mixing
const std::unique_ptr<CJClientManager> clientman;
const std::unique_ptr<CCoinJoinClientQueueManager> queueman;
#endif // ENABLE_WALLET
const std::unique_ptr<CCoinJoinServer> server;
Expand Down
27 changes: 15 additions & 12 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#include <coinjoin/client.h>
#include <coinjoin/coinjoin.h>
#include <coinjoin/context.h>
#include <coinjoin/options.h>

#include <univalue.h>
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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<CoinJoin::Client> coinJoinClient(const std::string& walletName) override { return std::make_unique<CoinJoinClientImpl>(walletName); }

std::unique_ptr<CoinJoin::Client> coinJoinClient(const std::string& walletName) override {
assert(m_context->cj_ctx != nullptr);
return std::make_unique<CoinJoinClientImpl>(*Assert(m_context->cj_ctx->clientman->Get(walletName)));
}
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
{
return MakeHandler(::uiInterface.InitMessage_connect(fn));
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/rpc/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/wallet/test/coinjoin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <amount.h>
#include <coinjoin/client.h>
#include <coinjoin/coinjoin.h>
#include <coinjoin/context.h>
#include <coinjoin/options.h>
#include <coinjoin/util.h>
#include <node/context.h>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9309fd7

Please sign in to comment.