Skip to content

Commit

Permalink
refactor: begin to de-globalize masternodeSync (#5103)
Browse files Browse the repository at this point in the history
<!--
*** Please remove the following help text before submitting: ***

Provide a general summary of your changes in the Title above

Pull requests without a rationale and clear improvement may be closed
immediately.

Please provide clear motivation for your patch and explain how it
improves
Dash Core user experience or Dash Core developer experience
significantly:

* Any test improvements or new tests that improve coverage are always
welcome.
* All other changes should have accompanying unit tests (see
`src/test/`) or
functional tests (see `test/`). Contributors should note which tests
cover
modified code. If no tests exist for a region of modified code, new
tests
  should accompany the change.
* Bug fixes are most welcome when they come with steps to reproduce or
an
explanation of the potential issue as well as reasoning for the way the
bug
  was fixed.
* Features are welcome, but might be rejected due to design or scope
issues.
If a feature is based on a lot of dependencies, contributors should
first
  consider building the system outside of Dash Core, if possible.
-->

## Issue being fixed or feature implemented
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
minimizing global uses

## What was done?
<!--- Describe your changes in detail -->
Started the deglobalization, a future PR should be done to continue this
deglobalization

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->


## Breaking Changes
<!--- Please describe any breaking changes your code introduces -->
none

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added or updated relevant unit/integration/functional/e2e
tests
- [x] I have made corresponding changes to the documentation

**For repository code-owners and collaborators only**
- [x] I have assigned this pull request to a milestone

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
  • Loading branch information
PastaPastaPasta and UdjinM6 authored Jan 4, 2023
1 parent d85c897 commit c9161e2
Show file tree
Hide file tree
Showing 34 changed files with 146 additions and 124 deletions.
24 changes: 12 additions & 12 deletions src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void CCoinJoinClientQueueManager::ProcessMessage(const CNode& peer, std::string_
{
if (fMasternodeMode) return;
if (!CCoinJoinClientOptions::IsEnabled()) return;
if (!masternodeSync->IsBlockchainSynced()) return;
if (!m_mn_sync->IsBlockchainSynced()) return;

if (!CheckDiskSpace(GetDataDir())) {
LogPrint(BCLog::COINJOIN, "CCoinJoinClientQueueManager::ProcessMessage -- Not enough disk space, disabling CoinJoin.\n");
Expand Down Expand Up @@ -117,7 +117,7 @@ void CCoinJoinClientManager::ProcessMessage(CNode& peer, std::string_view msg_ty
{
if (fMasternodeMode) return;
if (!CCoinJoinClientOptions::IsEnabled()) return;
if (!masternodeSync->IsBlockchainSynced()) return;
if (!m_mn_sync->IsBlockchainSynced()) return;

if (!CheckDiskSpace(GetDataDir())) {
ResetPool();
Expand All @@ -141,7 +141,7 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, std::string_view msg_ty
{
if (fMasternodeMode) return;
if (!CCoinJoinClientOptions::IsEnabled()) return;
if (!masternodeSync->IsBlockchainSynced()) return;
if (!m_mn_sync->IsBlockchainSynced()) return;

if (msg_type == NetMsgType::DSSTATUSUPDATE) {
if (!mixingMasternode) return;
Expand Down Expand Up @@ -272,7 +272,7 @@ bilingual_str CCoinJoinClientSession::GetStatus(bool fWaitForBlock) const
nStatusMessageProgress += 10;
std::string strSuffix;

if (fWaitForBlock || !masternodeSync->IsBlockchainSynced()) {
if (fWaitForBlock || !m_mn_sync->IsBlockchainSynced()) {
return strAutoDenomResult;
}

Expand Down Expand Up @@ -658,7 +658,7 @@ void CCoinJoinClientManager::UpdatedSuccessBlock()

bool CCoinJoinClientManager::WaitForAnotherBlock() const
{
if (!masternodeSync->IsBlockchainSynced()) return true;
if (!m_mn_sync->IsBlockchainSynced()) return true;

if (CCoinJoinClientOptions::IsMultiSessionEnabled()) return false;

Expand Down Expand Up @@ -740,7 +740,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
if (fMasternodeMode) return false; // no client-side mixing on masternodes
if (nState != POOL_STATE_IDLE) return false;

if (!masternodeSync->IsBlockchainSynced()) {
if (!m_mn_sync->IsBlockchainSynced()) {
strAutoDenomResult = _("Can't mix while sync in progress.");
return false;
}
Expand Down Expand Up @@ -920,7 +920,7 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, bool fDr
if (fMasternodeMode) return false; // no client-side mixing on masternodes
if (!CCoinJoinClientOptions::IsEnabled() || !IsMixing()) return false;

if (!masternodeSync->IsBlockchainSynced()) {
if (!m_mn_sync->IsBlockchainSynced()) {
strAutoDenomResult = _("Can't mix while sync in progress.");
return false;
}
Expand All @@ -946,7 +946,7 @@ bool CCoinJoinClientManager::DoAutomaticDenominating(CConnman& connman, bool fDr
AssertLockNotHeld(cs_deqsessions);
LOCK(cs_deqsessions);
if (int(deqSessions.size()) < CCoinJoinClientOptions::GetSessions()) {
deqSessions.emplace_back(mixingWallet);
deqSessions.emplace_back(mixingWallet, m_mn_sync);
}
for (auto& session : deqSessions) {
if (!CheckAutomaticBackup()) return false;
Expand Down Expand Up @@ -1786,21 +1786,21 @@ void CCoinJoinClientManager::UpdatedBlockTip(const CBlockIndex* pindex)
void CCoinJoinClientQueueManager::DoMaintenance()
{
if (!CCoinJoinClientOptions::IsEnabled()) return;
if (masternodeSync == nullptr) return;
if (m_mn_sync == nullptr) return;
if (fMasternodeMode) return; // no client-side mixing on masternodes

if (!masternodeSync->IsBlockchainSynced() || ShutdownRequested()) return;
if (!m_mn_sync->IsBlockchainSynced() || ShutdownRequested()) return;

CheckQueue();
}

void CCoinJoinClientManager::DoMaintenance(CConnman& connman)
{
if (!CCoinJoinClientOptions::IsEnabled()) return;
if (masternodeSync == nullptr) return;
if (m_mn_sync == nullptr) return;
if (fMasternodeMode) return; // no client-side mixing on masternodes

if (!masternodeSync->IsBlockchainSynced() || ShutdownRequested()) return;
if (!m_mn_sync->IsBlockchainSynced() || ShutdownRequested()) return;

static int nTick = 0;
static int nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN;
Expand Down
18 changes: 12 additions & 6 deletions src/coinjoin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CConnman;
class CNode;

class UniValue;
class CMasternodeSync;


// The main object for accessing mixing
Expand Down Expand Up @@ -70,6 +71,8 @@ class CPendingDsaRequest
class CCoinJoinClientSession : public CCoinJoinBaseSession
{
private:
const std::unique_ptr<CMasternodeSync>& m_mn_sync;

std::vector<COutPoint> vecOutPointLocked;

bilingual_str strLastMessage;
Expand Down Expand Up @@ -118,8 +121,8 @@ class CCoinJoinClientSession : public CCoinJoinBaseSession
void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);

public:
explicit CCoinJoinClientSession(CWallet& pwallet) :
mixingWallet(pwallet)
explicit CCoinJoinClientSession(CWallet& pwallet, const std::unique_ptr<CMasternodeSync>& mn_sync) :
mixingWallet(pwallet), m_mn_sync(mn_sync)
{
}

Expand Down Expand Up @@ -152,10 +155,11 @@ class CCoinJoinClientQueueManager : public CCoinJoinBaseManager
{
private:
CConnman& connman;
const std::unique_ptr<CMasternodeSync>& m_mn_sync;

public:
explicit CCoinJoinClientQueueManager(CConnman& _connman) :
connman(_connman) {};
explicit CCoinJoinClientQueueManager(CConnman& _connman, const std::unique_ptr<CMasternodeSync>& mn_sync) :
connman(_connman), m_mn_sync(mn_sync) {};

void ProcessMessage(const CNode& peer, std::string_view msg_type, CDataStream& vRecv) LOCKS_EXCLUDED(cs_vecqueue);
void ProcessDSQueue(const CNode& peer, CDataStream& vRecv);
Expand All @@ -170,6 +174,8 @@ class CCoinJoinClientManager
// Keep track of the used Masternodes
std::vector<COutPoint> vecMasternodesUsed;

const std::unique_ptr<CMasternodeSync>& m_mn_sync;

mutable Mutex cs_deqsessions;
// TODO: or map<denom, CCoinJoinClientSession> ??
std::deque<CCoinJoinClientSession> deqSessions GUARDED_BY(cs_deqsessions);
Expand Down Expand Up @@ -198,8 +204,8 @@ class CCoinJoinClientManager
CCoinJoinClientManager(CCoinJoinClientManager const&) = delete;
CCoinJoinClientManager& operator=(CCoinJoinClientManager const&) = delete;

explicit CCoinJoinClientManager(CWallet& wallet) :
mixingWallet(wallet) {}
explicit CCoinJoinClientManager(CWallet& wallet, const std::unique_ptr<CMasternodeSync>& mn_sync) :
mixingWallet(wallet), m_mn_sync(mn_sync) {}

void ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman) LOCKS_EXCLUDED(cs_deqsessions);

Expand Down
8 changes: 4 additions & 4 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,16 @@ void CCoinJoin::CheckDSTXes(const CBlockIndex* pindex, const llmq::CChainLocksHa
LogPrint(BCLog::COINJOIN, "CCoinJoin::CheckDSTXes -- mapDSTX.size()=%llu\n", mapDSTX.size());
}

void CCoinJoin::UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler)
void CCoinJoin::UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync)
{
if (pindex && masternodeSync->IsBlockchainSynced()) {
if (pindex && mn_sync->IsBlockchainSynced()) {
CheckDSTXes(pindex, clhandler);
}
}

void CCoinJoin::NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler)
void CCoinJoin::NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync)
{
if (pindex && masternodeSync->IsBlockchainSynced()) {
if (pindex && mn_sync->IsBlockchainSynced()) {
CheckDSTXes(pindex, clhandler);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CCoinJoin;
class CConnman;
class CBLSPublicKey;
class CBlockIndex;
class CMasternodeSync;

namespace llmq {
class CChainLocksHandler;
Expand Down Expand Up @@ -482,8 +483,8 @@ class CCoinJoin
static void AddDSTX(const CCoinJoinBroadcastTx& dstx) LOCKS_EXCLUDED(cs_mapdstx);
static CCoinJoinBroadcastTx GetDSTX(const uint256& hash) LOCKS_EXCLUDED(cs_mapdstx);

static void UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler);
static void NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler);
static void UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync);
static void NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync);

static void UpdateDSTXConfirmedHeight(const CTransactionRef& tx, int nHeight);
static void TransactionAddedToMempool(const CTransactionRef& tx) LOCKS_EXCLUDED(cs_mapdstx);
Expand Down
4 changes: 2 additions & 2 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ constexpr static CAmount DEFAULT_MAX_RAW_TX_FEE{COIN / 10};
void CCoinJoinServer::ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv)
{
if (!fMasternodeMode) return;
if (!masternodeSync->IsBlockchainSynced()) return;
if (!m_mn_sync->IsBlockchainSynced()) return;

if (msg_type == NetMsgType::DSACCEPT) {
ProcessDSACCEPT(peer, vRecv);
Expand Down Expand Up @@ -862,7 +862,7 @@ void CCoinJoinServer::SetState(PoolState nStateNew)
void CCoinJoinServer::DoMaintenance() const
{
if (!fMasternodeMode) return; // only run on masternodes
if (masternodeSync == nullptr || !masternodeSync->IsBlockchainSynced()) return;
if (m_mn_sync == nullptr || !m_mn_sync->IsBlockchainSynced()) return;
if (ShutdownRequested()) return;

if (!coinJoinServer) return;
Expand Down
6 changes: 4 additions & 2 deletions src/coinjoin/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public CCoinJoinBaseManager
{
private:
CConnman& connman;
const std::unique_ptr<CMasternodeSync>& m_mn_sync;

// Mixing uses collateral transactions to trust parties entering the pool
// to behave honestly. If they don't it takes their money.
Expand Down Expand Up @@ -74,10 +75,11 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public CCoinJoinBaseManager
void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);

public:
explicit CCoinJoinServer(CConnman& _connman) :
explicit CCoinJoinServer(CConnman& _connman, const std::unique_ptr<CMasternodeSync>& mn_sync) :
vecSessionCollaterals(),
fUnitTest(false),
connman(_connman) {};
connman(_connman),
m_mn_sync(mn_sync) {};

void ProcessMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv);

Expand Down
16 changes: 8 additions & 8 deletions src/dsnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include <llmq/quorums.h>

CDSNotificationInterface::CDSNotificationInterface(CConnman& _connman,
std::unique_ptr<CMasternodeSync>& _mnsync, std::unique_ptr<CDeterministicMNManager>& _dmnman,
std::unique_ptr<CMasternodeSync>& _mn_sync, std::unique_ptr<CDeterministicMNManager>& _dmnman,
std::unique_ptr<CGovernanceManager>& _govman, std::unique_ptr<LLMQContext>& _llmq_ctx
) : connman(_connman), mnsync(_mnsync), dmnman(_dmnman), govman(_govman), llmq_ctx(_llmq_ctx) {}
) : connman(_connman), m_mn_sync(_mn_sync), dmnman(_dmnman), govman(_govman), llmq_ctx(_llmq_ctx) {}

void CDSNotificationInterface::InitializeCurrentBlockTip()
{
Expand All @@ -35,14 +35,14 @@ void CDSNotificationInterface::InitializeCurrentBlockTip()
void CDSNotificationInterface::AcceptedBlockHeader(const CBlockIndex *pindexNew)
{
llmq_ctx->clhandler->AcceptedBlockHeader(pindexNew);
if (mnsync != nullptr) {
mnsync->AcceptedBlockHeader(pindexNew);
if (m_mn_sync != nullptr) {
m_mn_sync->AcceptedBlockHeader(pindexNew);
}
}

void CDSNotificationInterface::NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload)
{
mnsync->NotifyHeaderTip(pindexNew, fInitialDownload);
m_mn_sync->NotifyHeaderTip(pindexNew, fInitialDownload);
}

void CDSNotificationInterface::SynchronousUpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Expand All @@ -58,15 +58,15 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
if (pindexNew == pindexFork) // blocks were disconnected without any new ones
return;

mnsync->UpdatedBlockTip(pindexNew, fInitialDownload);
m_mn_sync->UpdatedBlockTip(pindexNew, fInitialDownload);

// Update global DIP0001 activation status
fDIP0001ActiveAtTip = pindexNew->nHeight >= Params().GetConsensus().DIP0001Height;

if (fInitialDownload)
return;

CCoinJoin::UpdatedBlockTip(pindexNew, *llmq_ctx->clhandler);
CCoinJoin::UpdatedBlockTip(pindexNew, *llmq_ctx->clhandler, m_mn_sync);
#ifdef ENABLE_WALLET
for (auto& pair : coinJoinClientManagers) {
pair.second->UpdatedBlockTip(pindexNew);
Expand Down Expand Up @@ -125,5 +125,5 @@ void CDSNotificationInterface::NotifyMasternodeListChanged(bool undo, const CDet
void CDSNotificationInterface::NotifyChainLock(const CBlockIndex* pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig)
{
llmq_ctx->isman->NotifyChainLock(pindex);
CCoinJoin::NotifyChainLock(pindex, *llmq_ctx->clhandler);
CCoinJoin::NotifyChainLock(pindex, *llmq_ctx->clhandler, m_mn_sync);
}
4 changes: 2 additions & 2 deletions src/dsnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CDSNotificationInterface : public CValidationInterface
{
public:
explicit CDSNotificationInterface(CConnman& _connman,
std::unique_ptr<CMasternodeSync>& _mnsync, std::unique_ptr<CDeterministicMNManager>& _dmnman,
std::unique_ptr<CMasternodeSync>& _mn_sync, std::unique_ptr<CDeterministicMNManager>& _dmnman,
std::unique_ptr<CGovernanceManager>& _govman, std::unique_ptr<LLMQContext>& _llmq_ctx);
virtual ~CDSNotificationInterface() = default;

Expand All @@ -40,7 +40,7 @@ class CDSNotificationInterface : public CValidationInterface
private:
CConnman& connman;

std::unique_ptr<CMasternodeSync>& mnsync;
std::unique_ptr<CMasternodeSync>& m_mn_sync;
std::unique_ptr<CDeterministicMNManager>& dmnman;
std::unique_ptr<CGovernanceManager>& govman;

Expand Down
2 changes: 1 addition & 1 deletion src/evo/mnauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip)

void CMNAuth::ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv, CConnman& connman)
{
if (msg_type != NetMsgType::MNAUTH || !masternodeSync->IsBlockchainSynced()) {
if (msg_type != NetMsgType::MNAUTH || !::masternodeSync->IsBlockchainSynced()) {
// we can't verify MNAUTH messages when we don't have the latest MN list
return;
}
Expand Down
Loading

0 comments on commit c9161e2

Please sign in to comment.