Skip to content

Commit

Permalink
Connect net requests manager load, dump and maintenance job.
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Feb 10, 2022
1 parent ad60ad7 commit 42f76c9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
11 changes: 10 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,16 @@ bool AppInitMain()

// ********************************************************* Step 10: setup layer 2 data

LoadTierTwo(chain_active_height, fReindexChainState);
bool load_cache_files = !(fReindex || fReindexChainState);
{
LOCK(cs_main);
// was blocks/chainstate deleted?
if (chainActive.Tip() == nullptr) {
load_cache_files = false;
}
}

LoadTierTwo(chain_active_height, load_cache_files);
if (!InitActiveMN()) return false;
RegisterTierTwoValidationInterface();

Expand Down
5 changes: 3 additions & 2 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "budget/budgetmanager.h"
#include "masternodeman.h"
#include "netmessagemaker.h"
#include "tiertwo/netfulfilledman.h"
#include "spork.h"
#include "sync.h"
#include "tiertwo/tiertwo_sync_state.h"
Expand Down Expand Up @@ -411,13 +412,13 @@ bool CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
vRecv >> nCountNeeded;

if (Params().NetworkIDString() == CBaseChainParams::MAIN) {
if (pfrom->HasFulfilledRequest(NetMsgType::GETMNWINNERS)) {
if (g_netfulfilledman.HasFulfilledRequest(pfrom->addr, NetMsgType::GETMNWINNERS)) {
LogPrint(BCLog::MASTERNODE, "%s: mnget - peer already asked me for the list\n", __func__);
return state.DoS(20, false, REJECT_INVALID, "getmnwinners-request-already-fulfilled");
}
}

pfrom->FulfilledRequest(NetMsgType::GETMNWINNERS);
g_netfulfilledman.AddFulfilledRequest(pfrom->addr, NetMsgType::GETMNWINNERS);
Sync(pfrom, nCountNeeded);
LogPrint(BCLog::MASTERNODE, "mnget - Sent Masternode winners to peer %i\n", pfrom->GetId());
} else if (strCommand == NetMsgType::MNWINNER) {
Expand Down
24 changes: 10 additions & 14 deletions src/masternode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "masternode.h"
#include "masternodeman.h"
#include "netmessagemaker.h"
#include "tiertwo/netfulfilledman.h"
#include "spork.h"
#include "tiertwo/tiertwo_sync_state.h"
#include "util/system.h"
Expand Down Expand Up @@ -177,12 +178,7 @@ void CMasternodeSync::ProcessSyncStatusMsg(int nItemID, int nCount)

void CMasternodeSync::ClearFulfilledRequest()
{
g_connman->ForEachNode([](CNode* pnode) {
pnode->ClearFulfilledRequest("getspork");
pnode->ClearFulfilledRequest("mnsync");
pnode->ClearFulfilledRequest("mnwsync");
pnode->ClearFulfilledRequest("busync");
});
g_netfulfilledman.Clear();
}

void CMasternodeSync::Process()
Expand Down Expand Up @@ -276,8 +272,8 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
}

// Request sporks sync if we haven't requested it yet.
if (pnode->HasFulfilledRequest("getspork")) return true;
pnode->FulfilledRequest("getspork");
if (g_netfulfilledman.HasFulfilledRequest(pnode->addr, "getspork")) return true;
g_netfulfilledman.AddFulfilledRequest(pnode->addr, "getspork");

g_connman->PushMessage(pnode, msgMaker.Make(NetMsgType::GETSPORKS));
RequestedMasternodeAttempt++;
Expand Down Expand Up @@ -318,15 +314,15 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
if (RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD * 4) return false;

// Request mnb sync if we haven't requested it yet.
if (pnode->HasFulfilledRequest("mnsync")) return true;
if (g_netfulfilledman.HasFulfilledRequest(pnode->addr, "mnsync")) return true;

// Try to request MN list sync.
if (!mnodeman.RequestMnList(pnode)) {
return true; // Failed, try next peer.
}

// Mark sync requested.
pnode->FulfilledRequest("mnsync");
g_netfulfilledman.AddFulfilledRequest(pnode->addr, "mnsync");
// Increase the sync attempt count
RequestedMasternodeAttempt++;

Expand Down Expand Up @@ -369,10 +365,10 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
if (RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD * 2) return false;

// Request mnw sync if we haven't requested it yet.
if (pnode->HasFulfilledRequest("mnwsync")) return true;
if (g_netfulfilledman.HasFulfilledRequest(pnode->addr, "mnwsync")) return true;

// Mark sync requested.
pnode->FulfilledRequest("mnwsync");
g_netfulfilledman.AddFulfilledRequest(pnode->addr, "mnwsync");

// Sync mn winners
int nMnCount = mnodeman.CountEnabled(true /* only_legacy */);
Expand Down Expand Up @@ -408,10 +404,10 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
if (RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD * 3) return false;

// Request bud sync if we haven't requested it yet.
if (pnode->HasFulfilledRequest("busync")) return true;
if (g_netfulfilledman.HasFulfilledRequest(pnode->addr, "busync")) return true;

// Mark sync requested.
pnode->FulfilledRequest("busync");
g_netfulfilledman.AddFulfilledRequest(pnode->addr, "busync");

// Sync proposals, finalizations and votes
uint256 n;
Expand Down
28 changes: 0 additions & 28 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,6 @@ class CNode
mapMsgCmdSize mapSendBytesPerMsgCmd;
mapMsgCmdSize mapRecvBytesPerMsgCmd;

std::vector<std::string> vecRequestsFulfilled; //keep track of what client has asked for

public:
uint256 hashContinue;
std::atomic<int> nStartingHeight;
Expand Down Expand Up @@ -892,32 +890,6 @@ class CNode
// inv response received, clear it from the waiting inv set.
void AskForInvReceived(const uint256& invHash);

bool HasFulfilledRequest(std::string strRequest)
{
for (std::string& type : vecRequestsFulfilled) {
if (type == strRequest) return true;
}
return false;
}

void ClearFulfilledRequest(std::string strRequest)
{
std::vector<std::string>::iterator it = vecRequestsFulfilled.begin();
while (it != vecRequestsFulfilled.end()) {
if ((*it) == strRequest) {
vecRequestsFulfilled.erase(it);
return;
}
++it;
}
}

void FulfilledRequest(std::string strRequest)
{
if (HasFulfilledRequest(strRequest)) return;
vecRequestsFulfilled.push_back(strRequest);
}

void CloseSocketDisconnect();
bool DisconnectOldProtocol(int nVersionIn, int nVersionRequired);

Expand Down
28 changes: 23 additions & 5 deletions src/tiertwo/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "llmq/quorums_init.h"
#include "scheduler.h"
#include "tiertwo/masternode_meta_manager.h"
#include "tiertwo/netfulfilledman.h"
#include "validation.h"
#include "wallet/wallet.h"

Expand Down Expand Up @@ -65,7 +67,7 @@ static void LoadBlockHashesCache(CMasternodeMan& man)
}
}

bool LoadTierTwo(int chain_active_height, bool fReindexChainState)
bool LoadTierTwo(int chain_active_height, bool load_cache_files)
{
// ################################# //
// ## Legacy Masternodes Manager ### //
Expand Down Expand Up @@ -118,11 +120,9 @@ bool LoadTierTwo(int chain_active_height, bool fReindexChainState)
// ############################## //
// ## Net MNs Metadata Manager ## //
// ############################## //
bool fLoadCacheFiles = !(fReindex || fReindexChainState);
fs::path pathDB = GetDataDir();
uiInterface.InitMessage(_("Loading masternode cache..."));
CFlatDB<CMasternodeMetaMan> metadb(MN_META_CACHE_FILENAME, MN_META_CACHE_FILE_ID);
if (fLoadCacheFiles) {
if (load_cache_files) {
if (!metadb.Load(g_mmetaman)) {
return UIError(strprintf(_("Failed to load masternode metadata cache from: %s"), metadb.GetDbPath().string()));
}
Expand All @@ -133,6 +133,22 @@ bool LoadTierTwo(int chain_active_height, bool fReindexChainState)
}
}

// ############################## //
// ## Network Requests Manager ## //
// ############################## //
uiInterface.InitMessage(_("Loading network requests cache..."));
CFlatDB<CNetFulfilledRequestManager> netRequestsDb(NET_REQUESTS_CACHE_FILENAME, NET_REQUESTS_CACHE_FILE_ID);
if (load_cache_files) {
if (!netRequestsDb.Load(g_netfulfilledman)) {
LogPrintf("Failed to load network requests cache from %s", netRequestsDb.GetDbPath().string());
}
} else {
CNetFulfilledRequestManager netfulfilledmanTmp;
if (!netRequestsDb.Dump(netfulfilledmanTmp)) {
LogPrintf("Failed to clear network requests cache at %s", netRequestsDb.GetDbPath().string());
}
}

return true;
}

Expand All @@ -148,7 +164,8 @@ void DumpTierTwo()
DumpMasternodes();
DumpBudgets(g_budgetman);
DumpMasternodePayments();
CFlatDB<CMasternodeMetaMan>("mnmetacache.dat", "magicMasternodeMetaCache").Dump(g_mmetaman);
CFlatDB<CMasternodeMetaMan>(MN_META_CACHE_FILENAME, MN_META_CACHE_FILE_ID).Dump(g_mmetaman);
CFlatDB<CNetFulfilledRequestManager>(NET_REQUESTS_CACHE_FILENAME, NET_REQUESTS_CACHE_FILE_ID).Dump(g_netfulfilledman);
}

void SetBudgetFinMode(const std::string& mode)
Expand Down Expand Up @@ -240,6 +257,7 @@ bool InitActiveMN()
void StartTierTwoThreadsAndScheduleJobs(boost::thread_group& threadGroup, CScheduler& scheduler)
{
threadGroup.create_thread(std::bind(&ThreadCheckMasternodes));
scheduler.scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(g_netfulfilledman)), 60 * 1000);

// Start LLMQ system
if (gArgs.GetBoolArg("-disabledkg", false)) {
Expand Down
3 changes: 2 additions & 1 deletion src/tiertwo/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define PIVX_TIERTWO_INIT_H

#include <string>
#include "fs.h"

static const bool DEFAULT_MASTERNODE = false;
static const bool DEFAULT_MNCONFLOCK = true;
Expand All @@ -24,7 +25,7 @@ void InitTierTwoPreChainLoad(bool fReindex);
void InitTierTwoPostCoinsCacheLoad();

/** Loads from disk all the tier two related objects */
bool LoadTierTwo(int chain_active_height, bool fReindexChainState);
bool LoadTierTwo(int chain_active_height, bool load_cache_files);

/** Register all tier two objects */
void RegisterTierTwoValidationInterface();
Expand Down

0 comments on commit 42f76c9

Please sign in to comment.