Skip to content

Commit

Permalink
Create ABS related scheduled tasks
Browse files Browse the repository at this point in the history
Replcae scheduled tasks

* Fix rare crash when trying to shutdown wallet during mixing

Should stop PS and release all keys _before_ wallet is destroyed.

* fix nowallet

* update doc
  • Loading branch information
UdjinM6 authored and CryptoCentric committed Apr 25, 2019
1 parent 8f60a83 commit 8b3b9fd
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 118 deletions.
2 changes: 0 additions & 2 deletions doc/developer-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ Threads

- BitcoinMiner : Generates coins (if wallet is enabled).

- ThreadCheckDarkSendPool : Runs masternode list and sync data update loops

- Shutdown : Does an orderly shutdown of everything.

Ignoring IDE/editor files
Expand Down
2 changes: 2 additions & 0 deletions src/activemasternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class CActiveMasternode

bool UpdateSentinelPing(int version);

void DoMaintenance(CConnman &connman) { ManageState(connman); }

private:
void ManageStateInitial(CConnman& connman);
void ManageStateRemote();
Expand Down
30 changes: 23 additions & 7 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ void PrepareShutdown()
peerLogic.reset();
g_connman.reset();

// STORE DATA CACHES INTO SERIALIZED DAT FILES
if (!fLiteMode) {
#ifdef ENABLE_WALLET
// Stop PrivateSend, release keys
privateSendClient.fEnablePrivateSend = false;
privateSendClient.ResetPool();
#endif
// STORE DATA CACHES INTO SERIALIZED DAT FILES
CFlatDB<CMasternodeMan> flatdb1("mncache.dat", "magicMasternodeCache");
flatdb1.Dump(mnodeman);
CFlatDB<CMasternodePayments> flatdb2("mnpayments.dat", "magicMasternodePaymentsCache");
Expand Down Expand Up @@ -1933,15 +1938,26 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
// GetMainSignals().UpdatedBlockTip(chainActive.Tip());
pdsNotificationInterface->InitializeCurrentBlockTip();

// ********************************************************* Step 11d: start absolute-ps-<smth> threads
// ********************************************************* Step 11d: schedule Dash-specific tasks

if (!fLiteMode) {
scheduler.scheduleEvery(boost::bind(&CNetFulfilledRequestManager::DoMaintenance, boost::ref(netfulfilledman)), 60);
scheduler.scheduleEvery(boost::bind(&CMasternodeSync::DoMaintenance, boost::ref(masternodeSync), boost::ref(*g_connman)), MASTERNODE_SYNC_TICK_SECONDS);
scheduler.scheduleEvery(boost::bind(&CMasternodeMan::DoMaintenance, boost::ref(mnodeman), boost::ref(*g_connman)), 1);
scheduler.scheduleEvery(boost::bind(&CActiveMasternode::DoMaintenance, boost::ref(activeMasternode), boost::ref(*g_connman)), 1);

scheduler.scheduleEvery(boost::bind(&CMasternodePayments::DoMaintenance, boost::ref(mnpayments)), 60);
scheduler.scheduleEvery(boost::bind(&CGovernanceManager::DoMaintenance, boost::ref(governance), boost::ref(*g_connman)), 60 * 5);

threadGroup.create_thread(boost::bind(&ThreadCheckPrivateSend, boost::ref(*g_connman)));
if (fMasternodeMode)
threadGroup.create_thread(boost::bind(&ThreadCheckPrivateSendServer, boost::ref(*g_connman)));
scheduler.scheduleEvery(boost::bind(&CInstantSend::DoMaintenance, boost::ref(instantsend)), 60);

if (fMasternodeMode)
scheduler.scheduleEvery(boost::bind(&CPrivateSendServer::DoMaintenance, boost::ref(privateSendServer), boost::ref(*g_connman)), 1);
#ifdef ENABLE_WALLET
else
threadGroup.create_thread(boost::bind(&ThreadCheckPrivateSendClient, boost::ref(*g_connman)));
else
scheduler.scheduleEvery(boost::bind(&CPrivateSendClient::DoMaintenance, boost::ref(privateSendClient), boost::ref(*g_connman)), 1);
#endif // ENABLE_WALLET
}

// ********************************************************* Step 12: start node

Expand Down
2 changes: 2 additions & 0 deletions src/instantx.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class CInstantSend
void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock);

std::string ToString() const;

void DoMaintenance() { CheckAndRemove(); }
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/masternode-payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ class CMasternodePayments
int GetStorageLimit() const;

void UpdatedBlockTip(const CBlockIndex *pindex, CConnman& connman);

void DoMaintenance() { CheckAndRemove(); }
};

#endif
2 changes: 1 addition & 1 deletion src/masternode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void CMasternodeSync::ProcessMessage(CNode* pfrom, const std::string& strCommand
void CMasternodeSync::ProcessTick(CConnman& connman)
{
static int nTick = 0;
if(nTick++ % MASTERNODE_SYNC_TICK_SECONDS != 0) return;
nTick++;

// reset the sync process if the last call to this function was more than 60 minutes ago (client was in sleep mode)
static int64_t nTimeLastProcess = GetTime();
Expand Down
2 changes: 2 additions & 0 deletions src/masternode-sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class CMasternodeSync
void AcceptedBlockHeader(const CBlockIndex *pindexNew);
void NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload, CConnman& connman);
void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload, CConnman& connman);

void DoMaintenance(CConnman &connman) { ProcessTick(connman); }
};

#endif
29 changes: 29 additions & 0 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "addrman.h"
#include "alert.h"
#include "clientversion.h"
#include "init.h"
#include "governance.h"
#include "masternode-payments.h"
#include "masternode-sync.h"
Expand Down Expand Up @@ -1758,3 +1759,31 @@ void CMasternodeMan::NotifyMasternodeUpdates(CConnman& connman)
fMasternodesAdded = false;
fMasternodesRemoved = false;
}

void CMasternodeMan::DoMaintenance(CConnman& connman)
{
if(fLiteMode) return; // disable all Dash specific functionality

if(!masternodeSync.IsBlockchainSynced() || ShutdownRequested())
return;

static unsigned int nTick = 0;

nTick++;

// make sure to check all masternodes first
mnodeman.Check();

mnodeman.ProcessPendingMnbRequests(connman);
mnodeman.ProcessPendingMnvRequests(connman);

if(nTick % 60 == 0) {
mnodeman.ProcessMasternodeConnections(connman);
mnodeman.CheckAndRemove(connman);
mnodeman.WarnMasternodeDaemonUpdates();
}

if(fMasternodeMode && (nTick % (60 * 5) == 0)) {
mnodeman.DoFullVerificationStep(connman);
}
}
1 change: 1 addition & 0 deletions src/masternodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class CMasternodeMan
*/
void NotifyMasternodeUpdates(CConnman& connman);

void DoMaintenance(CConnman &connman);
};

#endif
2 changes: 2 additions & 0 deletions src/netfulfilledman.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CNetFulfilledRequestManager
void Clear();

std::string ToString() const;

void DoMaintenance() { CheckAndRemove(); }
};

#endif
34 changes: 11 additions & 23 deletions src/privatesend-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,34 +1450,22 @@ void CPrivateSendClient::UpdatedBlockTip(const CBlockIndex *pindex)

}

//TODO: Rename/move to core
void ThreadCheckPrivateSendClient(CConnman& connman)
void CPrivateSendClient::DoMaintenance(CConnman& connman)
{
if(fLiteMode) return; // disable all absolute specific functionality
if(fMasternodeMode) return; // no client-side mixing on masternodes

static bool fOneThread;
if(fOneThread) return;
fOneThread = true;
if(!masternodeSync.IsBlockchainSynced() || ShutdownRequested())
return;

// Make this thread recognisable as the PrivateSend thread
RenameThread("absolute-ps-client");
static unsigned int nTick = 0;
static unsigned int nDoAutoNextRun = nTick + PRIVATESEND_AUTO_TIMEOUT_MIN;

unsigned int nTick = 0;
unsigned int nDoAutoNextRun = nTick + PRIVATESEND_AUTO_TIMEOUT_MIN;

while (true)
{
MilliSleep(1000);

if(masternodeSync.IsBlockchainSynced() && !ShutdownRequested()) {
nTick++;
privateSendClient.CheckTimeout();
privateSendClient.ProcessPendingDsaRequest(connman);
if(nDoAutoNextRun == nTick) {
privateSendClient.DoAutomaticDenominating(connman);
nDoAutoNextRun = nTick + PRIVATESEND_AUTO_TIMEOUT_MIN + GetRandInt(PRIVATESEND_AUTO_TIMEOUT_MAX - PRIVATESEND_AUTO_TIMEOUT_MIN);
}
}
nTick++;
privateSendClient.CheckTimeout();
privateSendClient.ProcessPendingDsaRequest(connman);
if(nDoAutoNextRun == nTick) {
privateSendClient.DoAutomaticDenominating(connman);
nDoAutoNextRun = nTick + PRIVATESEND_AUTO_TIMEOUT_MIN + GetRandInt(PRIVATESEND_AUTO_TIMEOUT_MAX - PRIVATESEND_AUTO_TIMEOUT_MIN);
}
}
4 changes: 2 additions & 2 deletions src/privatesend-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class CPrivateSendClient : public CPrivateSendBase
void CheckTimeout();

void UpdatedBlockTip(const CBlockIndex *pindex);
};

void ThreadCheckPrivateSendClient(CConnman& connman);
void DoMaintenance(CConnman& connman);
};

#endif
25 changes: 5 additions & 20 deletions src/privatesend-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,29 +891,14 @@ void CPrivateSendServer::SetState(PoolState nStateNew)
nState = nStateNew;
}

//TODO: Rename/move to core
void ThreadCheckPrivateSendServer(CConnman& connman)
void CPrivateSendServer::DoMaintenance(CConnman& connman)
{
if(fLiteMode) return; // disable all Dash specific functionality
if(!fMasternodeMode) return; // only run on masternodes

static bool fOneThread;
if(fOneThread) return;
fOneThread = true;

// Make this thread recognisable as the PrivateSend thread
RenameThread("absolute-ps-server");

unsigned int nTick = 0;

while (true)
{
MilliSleep(1000);
if(!masternodeSync.IsBlockchainSynced() || ShutdownRequested())
return;

if(masternodeSync.IsBlockchainSynced() && !ShutdownRequested()) {
nTick++;
privateSendServer.CheckTimeout(connman);
privateSendServer.CheckForCompleteQueue(connman);
}
}
privateSendServer.CheckTimeout(connman);
privateSendServer.CheckForCompleteQueue(connman);
}
4 changes: 2 additions & 2 deletions src/privatesend-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class CPrivateSendServer : public CPrivateSendBase

void CheckTimeout(CConnman& connman);
void CheckForCompleteQueue(CConnman& connman);
};

void ThreadCheckPrivateSendServer(CConnman& connman);
void DoMaintenance(CConnman& connman);
};

#endif
59 changes: 0 additions & 59 deletions src/privatesend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

#include "activemasternode.h"
#include "consensus/validation.h"
#include "governance.h"
#include "init.h"
#include "instantx.h"
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "messagesigner.h"
#include "netfulfilledman.h"
#include "netmessagemaker.h"
#include "script/sign.h"
#include "txmempool.h"
Expand Down Expand Up @@ -511,58 +507,3 @@ void CPrivateSend::SyncTransaction(const CTransaction& tx, const CBlockIndex *pi
mapDSTX[txHash].SetConfirmedHeight(posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK ? -1 : pindex->nHeight);
LogPrint("privatesend", "CPrivateSendClient::SyncTransaction -- txid=%s\n", txHash.ToString());
}

//TODO: Rename/move to core
void ThreadCheckPrivateSend(CConnman& connman)
{
if(fLiteMode) return; // disable all Absolute specific functionality

static bool fOneThread;
if(fOneThread) return;
fOneThread = true;

// Make this thread recognisable as the PrivateSend thread
RenameThread("absolute-ps");

unsigned int nTick = 0;

while (true)
{
MilliSleep(1000);

// try to sync from all available nodes, one step at a time
masternodeSync.ProcessTick(connman);

if(masternodeSync.IsBlockchainSynced() && !ShutdownRequested()) {

nTick++;

// make sure to check all masternodes first
mnodeman.Check();

mnodeman.ProcessPendingMnbRequests(connman);
mnodeman.ProcessPendingMnvRequests(connman);

// check if we should activate or ping every few minutes,
// slightly postpone first run to give net thread a chance to connect to some peers
if(nTick % MASTERNODE_MIN_MNP_SECONDS == 15)
activeMasternode.ManageState(connman);

if(nTick % 60 == 0) {
netfulfilledman.CheckAndRemove();
mnodeman.ProcessMasternodeConnections(connman);
mnodeman.CheckAndRemove(connman);
mnodeman.WarnMasternodeDaemonUpdates();
mnpayments.CheckAndRemove();
instantsend.CheckAndRemove();
}
if(fMasternodeMode && (nTick % (60 * 5) == 0)) {
mnodeman.DoFullVerificationStep(connman);
}

if(nTick % (60 * 5) == 0) {
governance.DoMaintenance(connman);
}
}
}
}
2 changes: 0 additions & 2 deletions src/privatesend.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,4 @@ class CPrivateSend
static void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock);
};

void ThreadCheckPrivateSend(CConnman& connman);

#endif

0 comments on commit 8b3b9fd

Please sign in to comment.