Skip to content

Commit afec9ae

Browse files
committed
refactor: move CoinJoin server to ActiveContext
1 parent fcabb3d commit afec9ae

File tree

11 files changed

+87
-55
lines changed

11 files changed

+87
-55
lines changed

src/coinjoin/context.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
#ifdef ENABLE_WALLET
88
#include <coinjoin/client.h>
99
#endif // ENABLE_WALLET
10-
#include <coinjoin/server.h>
10+
#include <coinjoin/coinjoin.h>
1111

12-
CJContext::CJContext(ChainstateManager& chainman, CConnman& connman, CDeterministicMNManager& dmnman,
13-
CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool,
14-
const CActiveMasternodeManager* const mn_activeman, const CMasternodeSync& mn_sync,
15-
const llmq::CInstantSendManager& isman, std::unique_ptr<PeerManager>& peerman, bool relay_txes) :
16-
dstxman{std::make_unique<CDSTXManager>()},
12+
CJContext::CJContext(ChainstateManager& chainman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman,
13+
CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman,
14+
const CMasternodeSync& mn_sync, const llmq::CInstantSendManager& isman, bool relay_txes) :
1715
#ifdef ENABLE_WALLET
1816
walletman{std::make_unique<CoinJoinWalletManager>(chainman, dmnman, mn_metaman, mempool, mn_sync, isman, queueman,
1917
/*is_masternode=*/mn_activeman != nullptr)},
2018
queueman{relay_txes ? std::make_unique<CCoinJoinClientQueueManager>(*walletman, dmnman, mn_metaman, mn_sync,
2119
/*is_masternode=*/mn_activeman != nullptr)
2220
: nullptr},
2321
#endif // ENABLE_WALLET
24-
server{std::make_unique<CCoinJoinServer>(chainman, connman, dmnman, *dstxman, mn_metaman, mempool, mn_activeman,
25-
mn_sync, isman, peerman)}
22+
dstxman{std::make_unique<CDSTXManager>()}
2623
{}
2724

2825
CJContext::~CJContext() {}

src/coinjoin/context.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
#include <memory>
1313

1414
class CActiveMasternodeManager;
15-
class CBlockPolicyEstimator;
16-
class CCoinJoinServer;
17-
class CConnman;
1815
class CDeterministicMNManager;
1916
class CDSTXManager;
2017
class ChainstateManager;
2118
class CMasternodeMetaMan;
2219
class CMasternodeSync;
2320
class CTxMemPool;
24-
class PeerManager;
2521
namespace llmq {
2622
class CInstantSendManager;
2723
};
@@ -34,19 +30,17 @@ class CoinJoinWalletManager;
3430
struct CJContext {
3531
CJContext() = delete;
3632
CJContext(const CJContext&) = delete;
37-
CJContext(ChainstateManager& chainman, CConnman& connman, CDeterministicMNManager& dmnman,
38-
CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman,
39-
const CMasternodeSync& mn_sync, const llmq::CInstantSendManager& isman,
40-
std::unique_ptr<PeerManager>& peerman, bool relay_txes);
33+
CJContext(ChainstateManager& chainman, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman,
34+
CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman, const CMasternodeSync& mn_sync,
35+
const llmq::CInstantSendManager& isman, bool relay_txes);
4136
~CJContext();
4237

43-
const std::unique_ptr<CDSTXManager> dstxman;
4438
#ifdef ENABLE_WALLET
4539
// The main object for accessing mixing
4640
const std::unique_ptr<CoinJoinWalletManager> walletman;
4741
const std::unique_ptr<CCoinJoinClientQueueManager> queueman;
4842
#endif // ENABLE_WALLET
49-
const std::unique_ptr<CCoinJoinServer> server;
43+
const std::unique_ptr<CDSTXManager> dstxman;
5044
};
5145

5246
#endif // BITCOIN_COINJOIN_CONTEXT_H

src/init.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
21472147
node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(),
21482148
chainman, *node.mempool, *node.mn_metaman, *node.mn_sync,
21492149
*node.govman, *node.sporkman, node.mn_activeman.get(), node.dmnman,
2150-
node.cj_ctx, node.llmq_ctx, ignores_incoming_txs);
2150+
node.active_ctx, node.cj_ctx, node.llmq_ctx, ignores_incoming_txs);
21512151
RegisterValidationInterface(node.peerman.get());
21522152

21532153
g_ds_notification_interface = std::make_unique<CDSNotificationInterface>(
@@ -2157,14 +2157,16 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
21572157

21582158
// ********************************************************* Step 7c: Setup CoinJoin
21592159

2160-
node.cj_ctx = std::make_unique<CJContext>(chainman, *node.connman, *node.dmnman, *node.mn_metaman, *node.mempool,
2161-
node.mn_activeman.get(), *node.mn_sync, *node.llmq_ctx->isman, node.peerman,
2160+
node.cj_ctx = std::make_unique<CJContext>(chainman, *node.dmnman, *node.mn_metaman, *node.mempool,
2161+
node.mn_activeman.get(), *node.mn_sync, *node.llmq_ctx->isman,
21622162
!ignores_incoming_txs);
21632163

21642164
// ********************************************************* Step 7d: Setup masternode mode
21652165
assert(!node.active_ctx);
21662166
if (node.mn_activeman) {
2167-
node.active_ctx = std::make_unique<ActiveContext>(chainman.ActiveChainstate(), *node.llmq_ctx, *node.sporkman, *node.mempool, *node.mn_sync);
2167+
node.active_ctx = std::make_unique<ActiveContext>(chainman, *node.connman, *node.dmnman, *node.cj_ctx->dstxman, *node.mn_metaman,
2168+
*node.llmq_ctx, *node.sporkman, *node.mempool, *node.mn_activeman, *node.mn_sync,
2169+
node.peerman);
21682170
}
21692171

21702172
// ********************************************************* Step 7e: Setup other Dash services
@@ -2276,7 +2278,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
22762278
}
22772279

22782280
if (node.mn_activeman) {
2279-
node.scheduler->scheduleEvery(std::bind(&CCoinJoinServer::DoMaintenance, std::ref(*node.cj_ctx->server)), std::chrono::seconds{1});
2281+
node.scheduler->scheduleEvery(std::bind(&CCoinJoinServer::DoMaintenance, std::ref(*node.active_ctx->cj_server)), std::chrono::seconds{1});
22802282
node.scheduler->scheduleEvery(std::bind(&llmq::CDKGSessionManager::CleanupOldContributions, std::ref(*node.llmq_ctx->qdkgsman)), std::chrono::hours{1});
22812283
#ifdef ENABLE_WALLET
22822284
} else if (!ignores_incoming_txs) {

src/masternode/active/context.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66

77
#include <chainlock/chainlock.h>
88
#include <chainlock/signing.h>
9+
#include <coinjoin/server.h>
910
#include <instantsend/instantsend.h>
1011
#include <instantsend/signing.h>
1112
#include <llmq/context.h>
13+
#include <validation.h>
1214

13-
ActiveContext::ActiveContext(CChainState& chainstate, LLMQContext& llmq_ctx, CSporkManager& sporkman,
14-
CTxMemPool& mempool, const CMasternodeSync& mn_sync) :
15+
ActiveContext::ActiveContext(ChainstateManager& chainman, CConnman& connman, CDeterministicMNManager& dmnman,
16+
CDSTXManager& dstxman, CMasternodeMetaMan& mn_metaman, LLMQContext& llmq_ctx,
17+
CSporkManager& sporkman, CTxMemPool& mempool, const CActiveMasternodeManager& mn_activeman,
18+
const CMasternodeSync& mn_sync, std::unique_ptr<PeerManager>& peerman) :
1519
m_llmq_ctx{llmq_ctx},
16-
cl_signer{std::make_unique<chainlock::ChainLockSigner>(chainstate, *llmq_ctx.clhandler, *llmq_ctx.sigman,
17-
*llmq_ctx.shareman, sporkman, mn_sync)},
18-
is_signer{std::make_unique<instantsend::InstantSendSigner>(chainstate, *llmq_ctx.clhandler, *llmq_ctx.isman,
19-
*llmq_ctx.sigman, *llmq_ctx.shareman, *llmq_ctx.qman,
20-
sporkman, mempool, mn_sync)}
20+
cl_signer{std::make_unique<chainlock::ChainLockSigner>(chainman.ActiveChainstate(), *llmq_ctx.clhandler,
21+
*llmq_ctx.sigman, *llmq_ctx.shareman, sporkman, mn_sync)},
22+
is_signer{std::make_unique<instantsend::InstantSendSigner>(chainman.ActiveChainstate(), *llmq_ctx.clhandler,
23+
*llmq_ctx.isman, *llmq_ctx.sigman, *llmq_ctx.shareman,
24+
*llmq_ctx.qman, sporkman, mempool, mn_sync)},
25+
cj_server{std::make_unique<CCoinJoinServer>(chainman, connman, dmnman, dstxman, mn_metaman, mempool, &mn_activeman,
26+
mn_sync, *llmq_ctx.isman, peerman)}
2127
{
2228
m_llmq_ctx.clhandler->ConnectSigner(cl_signer.get());
2329
m_llmq_ctx.isman->ConnectSigner(is_signer.get());

src/masternode/active/context.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77

88
#include <memory>
99

10-
class CChainState;
10+
class CActiveMasternodeManager;
11+
class ChainstateManager;
12+
class CCoinJoinServer;
13+
class CConnman;
14+
class CDeterministicMNManager;
15+
class CDSTXManager;
16+
class CMasternodeMetaMan;
1117
class CMasternodeSync;
1218
class CSporkManager;
1319
class CTxMemPool;
20+
class PeerManager;
1421
struct LLMQContext;
1522
namespace chainlock {
1623
class ChainLockSigner;
@@ -21,6 +28,7 @@ class InstantSendSigner;
2128

2229
struct ActiveContext {
2330
private:
31+
// TODO: Switch to references to members when migration is finished
2432
LLMQContext& m_llmq_ctx;
2533

2634
/*
@@ -33,9 +41,18 @@ struct ActiveContext {
3341
public:
3442
ActiveContext() = delete;
3543
ActiveContext(const ActiveContext&) = delete;
36-
ActiveContext(CChainState& chainstate, LLMQContext& llmq_ctx, CSporkManager& sporkman, CTxMemPool& mempool,
37-
const CMasternodeSync& mn_sync);
44+
ActiveContext(ChainstateManager& chainman, CConnman& connman, CDeterministicMNManager& dmnman,
45+
CDSTXManager& dstxman, CMasternodeMetaMan& mn_metaman, LLMQContext& llmq_ctx, CSporkManager& sporkman,
46+
CTxMemPool& mempool, const CActiveMasternodeManager& mn_activeman, const CMasternodeSync& mn_sync,
47+
std::unique_ptr<PeerManager>& peerman);
3848
~ActiveContext();
49+
50+
/*
51+
* Entities that are only utilized when masternode mode is enabled
52+
* and are accessible in their own right
53+
* TODO: Move CActiveMasternodeManager here when dependents have been migrated
54+
*/
55+
const std::unique_ptr<CCoinJoinServer> cj_server;
3956
};
4057

4158
#endif // BITCOIN_MASTERNODE_ACTIVE_CONTEXT_H

src/net_processing.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
#include <spork.h>
5353
#include <governance/governance.h>
54+
#include <masternode/active/context.h>
5455
#include <masternode/sync.h>
5556
#include <masternode/meta.h>
5657
#ifdef ENABLE_WALLET
@@ -595,6 +596,7 @@ class PeerManagerImpl final : public PeerManager
595596
CGovernanceManager& govman, CSporkManager& sporkman,
596597
const CActiveMasternodeManager* const mn_activeman,
597598
const std::unique_ptr<CDeterministicMNManager>& dmnman,
599+
const std::unique_ptr<ActiveContext>& active_ctx,
598600
const std::unique_ptr<CJContext>& cj_ctx,
599601
const std::unique_ptr<LLMQContext>& llmq_ctx,
600602
bool ignore_incoming_txs);
@@ -776,6 +778,7 @@ class PeerManagerImpl final : public PeerManager
776778
CTxMemPool& m_mempool;
777779
std::unique_ptr<TxReconciliationTracker> m_txreconciliation;
778780
const std::unique_ptr<CDeterministicMNManager>& m_dmnman;
781+
const std::unique_ptr<ActiveContext>& m_active_ctx;
779782
const std::unique_ptr<CJContext>& m_cj_ctx;
780783
const std::unique_ptr<LLMQContext>& m_llmq_ctx;
781784
CMasternodeMetaMan& m_mn_metaman;
@@ -1948,10 +1951,11 @@ std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams,
19481951
CGovernanceManager& govman, CSporkManager& sporkman,
19491952
const CActiveMasternodeManager* const mn_activeman,
19501953
const std::unique_ptr<CDeterministicMNManager>& dmnman,
1954+
const std::unique_ptr<ActiveContext>& active_ctx,
19511955
const std::unique_ptr<CJContext>& cj_ctx,
19521956
const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs)
19531957
{
1954-
return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, chainman, pool, mn_metaman, mn_sync, govman, sporkman, mn_activeman, dmnman, cj_ctx, llmq_ctx, ignore_incoming_txs);
1958+
return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, chainman, pool, mn_metaman, mn_sync, govman, sporkman, mn_activeman, dmnman, active_ctx, cj_ctx, llmq_ctx, ignore_incoming_txs);
19551959
}
19561960

19571961
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, BanMan* banman,
@@ -1960,6 +1964,7 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn
19601964
CGovernanceManager& govman, CSporkManager& sporkman,
19611965
const CActiveMasternodeManager* const mn_activeman,
19621966
const std::unique_ptr<CDeterministicMNManager>& dmnman,
1967+
const std::unique_ptr<ActiveContext>& active_ctx,
19631968
const std::unique_ptr<CJContext>& cj_ctx,
19641969
const std::unique_ptr<LLMQContext>& llmq_ctx,
19651970
bool ignore_incoming_txs)
@@ -1970,6 +1975,7 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn
19701975
m_chainman(chainman),
19711976
m_mempool(pool),
19721977
m_dmnman(dmnman),
1978+
m_active_ctx(active_ctx),
19731979
m_cj_ctx(cj_ctx),
19741980
m_llmq_ctx(llmq_ctx),
19751981
m_mn_metaman(mn_metaman),
@@ -2260,9 +2266,9 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
22602266
return m_llmq_ctx->isman->AlreadyHave(inv);
22612267
case MSG_DSQ:
22622268
#ifdef ENABLE_WALLET
2263-
return m_cj_ctx->server->HasQueue(inv.hash) || m_cj_ctx->queueman->HasQueue(inv.hash);
2269+
return m_cj_ctx->queueman->HasQueue(inv.hash) || (m_active_ctx && m_active_ctx->cj_server->HasQueue(inv.hash));
22642270
#else
2265-
return m_cj_ctx->server->HasQueue(inv.hash);
2271+
return m_active_ctx && m_active_ctx->cj_server->HasQueue(inv.hash);
22662272
#endif
22672273
}
22682274

@@ -2867,7 +2873,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
28672873
}
28682874
}
28692875
if (!push && inv.type == MSG_DSQ) {
2870-
auto opt_dsq = m_cj_ctx->server->GetQueueFromHash(inv.hash);
2876+
auto opt_dsq = m_active_ctx ? m_active_ctx->cj_server->GetQueueFromHash(inv.hash) : std::nullopt;
28712877
#ifdef ENABLE_WALLET
28722878
if (!opt_dsq.has_value()) {
28732879
opt_dsq = m_cj_ctx->queueman->GetQueueFromHash(inv.hash);
@@ -5249,7 +5255,9 @@ void PeerManagerImpl::ProcessMessage(
52495255
clientman->ProcessMessage(pfrom, m_chainman.ActiveChainstate(), m_connman, m_mempool, msg_type, vRecv);
52505256
});
52515257
#endif // ENABLE_WALLET
5252-
PostProcessMessage(m_cj_ctx->server->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId());
5258+
if (m_active_ctx) {
5259+
PostProcessMessage(m_active_ctx->cj_server->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId());
5260+
}
52535261
PostProcessMessage(m_sporkman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom.GetId());
52545262
m_mn_sync.ProcessMessage(pfrom, msg_type, vRecv);
52555263
PostProcessMessage(m_govman.ProcessMessage(pfrom, m_connman, *this, msg_type, vRecv), pfrom.GetId());

src/net_processing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CGovernanceManager;
2525
class CInv;
2626
class CSporkManager;
2727
class CTransaction;
28+
struct ActiveContext;
2829
struct CJContext;
2930
struct LLMQContext;
3031

@@ -60,6 +61,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
6061
CGovernanceManager& govman, CSporkManager& sporkman,
6162
const CActiveMasternodeManager* const mn_activeman,
6263
const std::unique_ptr<CDeterministicMNManager>& dmnman,
64+
const std::unique_ptr<ActiveContext>& active_ctx,
6365
const std::unique_ptr<CJContext>& cj_ctx,
6466
const std::unique_ptr<LLMQContext>& llmq_ctx, bool ignore_incoming_txs);
6567
virtual ~PeerManager() { }

src/rpc/coinjoin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <coinjoin/context.h>
66
#include <coinjoin/server.h>
7+
#include <masternode/active/context.h>
78
#include <node/context.h>
89
#include <rpc/server.h>
910
#include <rpc/server_util.h>
@@ -466,7 +467,7 @@ static RPCHelpMan getcoinjoininfo()
466467

467468
const NodeContext& node = EnsureAnyNodeContext(request.context);
468469
if (node.mn_activeman) {
469-
node.cj_ctx->server->GetJsonInfo(obj);
470+
node.active_ctx->cj_server->GetJsonInfo(obj);
470471
return obj;
471472
}
472473

0 commit comments

Comments
 (0)