Skip to content

Commit 7e2f332

Browse files
committed
masternodesync: move seen maps to tiertwo_sync_state class and remove most of the masternode-sync dependencies.
1 parent f29f9af commit 7e2f332

File tree

10 files changed

+111
-82
lines changed

10 files changed

+111
-82
lines changed

src/budget/budgetmanager.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "consensus/validation.h"
99
#include "evo/deterministicmns.h"
10-
#include "masternode-sync.h"
1110
#include "masternodeman.h"
1211
#include "netmessagemaker.h"
1312
#include "tiertwo/tiertwo_sync_state.h"
@@ -1056,7 +1055,7 @@ void CBudgetManager::NewBlock()
10561055
cleanOrphans(cs_finalizedvotes, mapOrphanFinalizedBudgetVotes, mapSeenFinalizedBudgetVotes);
10571056

10581057
// Once every 2 weeks (1/14 * 1/1440), clean the seen maps
1059-
if (masternodeSync.IsSynced() && GetRandInt(1440) == 0) {
1058+
if (g_tiertwo_sync_state.IsSynced() && GetRandInt(1440) == 0) {
10601059
ReloadMapSeen();
10611060
}
10621061

@@ -1089,7 +1088,7 @@ int CBudgetManager::ProcessProposal(CBudgetProposal& proposal)
10891088
{
10901089
const uint256& nHash = proposal.GetHash();
10911090
if (HaveProposal(nHash)) {
1092-
masternodeSync.AddedBudgetItem(nHash);
1091+
g_tiertwo_sync_state.AddedBudgetItem(nHash);
10931092
return 0;
10941093
}
10951094
if (!AddProposal(proposal)) {
@@ -1099,7 +1098,7 @@ int CBudgetManager::ProcessProposal(CBudgetProposal& proposal)
10991098
// Relay only if we are synchronized
11001099
// Makes no sense to relay proposals to the peers from where we are syncing them.
11011100
if (g_tiertwo_sync_state.IsSynced()) proposal.Relay();
1102-
masternodeSync.AddedBudgetItem(nHash);
1101+
g_tiertwo_sync_state.AddedBudgetItem(nHash);
11031102

11041103
LogPrint(BCLog::MNBUDGET, "mprop (new) %s\n", nHash.ToString());
11051104
//We might have active votes for this proposal that are valid now
@@ -1112,7 +1111,7 @@ bool CBudgetManager::ProcessProposalVote(CBudgetVote& vote, CNode* pfrom, CValid
11121111
const uint256& voteID = vote.GetHash();
11131112

11141113
if (HaveSeenProposalVote(voteID)) {
1115-
masternodeSync.AddedBudgetItem(voteID);
1114+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
11161115
return false;
11171116
}
11181117

@@ -1150,7 +1149,7 @@ bool CBudgetManager::ProcessProposalVote(CBudgetVote& vote, CNode* pfrom, CValid
11501149
// Relay only if we are synchronized
11511150
// Makes no sense to relay votes to the peers from where we are syncing them.
11521151
if (g_tiertwo_sync_state.IsSynced()) vote.Relay();
1153-
masternodeSync.AddedBudgetItem(voteID);
1152+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
11541153
LogPrint(BCLog::MNBUDGET, "mvote - new vote (%s) for proposal %s from dmn %s\n",
11551154
voteID.ToString(), vote.GetProposalHash().ToString(), mn_protx_id);
11561155
return true;
@@ -1187,7 +1186,7 @@ bool CBudgetManager::ProcessProposalVote(CBudgetVote& vote, CNode* pfrom, CValid
11871186
// Relay only if we are synchronized
11881187
// Makes no sense to relay votes to the peers from where we are syncing them.
11891188
if (g_tiertwo_sync_state.IsSynced()) vote.Relay();
1190-
masternodeSync.AddedBudgetItem(voteID);
1189+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
11911190
LogPrint(BCLog::MNBUDGET, "mvote - new vote (%s) for proposal %s from dmn %s\n",
11921191
voteID.ToString(), vote.GetProposalHash().ToString(), voteVin.prevout.ToString());
11931192
return true;
@@ -1198,7 +1197,7 @@ int CBudgetManager::ProcessFinalizedBudget(CFinalizedBudget& finalbudget, CNode*
11981197

11991198
const uint256& nHash = finalbudget.GetHash();
12001199
if (HaveFinalizedBudget(nHash)) {
1201-
masternodeSync.AddedBudgetItem(nHash);
1200+
g_tiertwo_sync_state.AddedBudgetItem(nHash);
12021201
return 0;
12031202
}
12041203
if (!AddFinalizedBudget(finalbudget, pfrom)) {
@@ -1208,7 +1207,7 @@ int CBudgetManager::ProcessFinalizedBudget(CFinalizedBudget& finalbudget, CNode*
12081207
// Relay only if we are synchronized
12091208
// Makes no sense to relay finalizations to the peers from where we are syncing them.
12101209
if (g_tiertwo_sync_state.IsSynced()) finalbudget.Relay();
1211-
masternodeSync.AddedBudgetItem(nHash);
1210+
g_tiertwo_sync_state.AddedBudgetItem(nHash);
12121211

12131212
LogPrint(BCLog::MNBUDGET, "fbs (new) %s\n", nHash.ToString());
12141213
//we might have active votes for this budget that are now valid
@@ -1221,7 +1220,7 @@ bool CBudgetManager::ProcessFinalizedBudgetVote(CFinalizedBudgetVote& vote, CNod
12211220
const uint256& voteID = vote.GetHash();
12221221

12231222
if (HaveSeenFinalizedBudgetVote(voteID)) {
1224-
masternodeSync.AddedBudgetItem(voteID);
1223+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
12251224
return false;
12261225
}
12271226

@@ -1259,7 +1258,7 @@ bool CBudgetManager::ProcessFinalizedBudgetVote(CFinalizedBudgetVote& vote, CNod
12591258
// Relay only if we are synchronized
12601259
// Makes no sense to relay votes to the peers from where we are syncing them.
12611260
if (g_tiertwo_sync_state.IsSynced()) vote.Relay();
1262-
masternodeSync.AddedBudgetItem(voteID);
1261+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
12631262
LogPrint(BCLog::MNBUDGET, "fbvote - new vote (%s) for budget %s from dmn %s\n",
12641263
voteID.ToString(), vote.GetBudgetHash().ToString(), mn_protx_id);
12651264
return true;
@@ -1295,7 +1294,7 @@ bool CBudgetManager::ProcessFinalizedBudgetVote(CFinalizedBudgetVote& vote, CNod
12951294
// Relay only if we are synchronized
12961295
// Makes no sense to relay votes to the peers from where we are syncing them.
12971296
if (g_tiertwo_sync_state.IsSynced()) vote.Relay();
1298-
masternodeSync.AddedBudgetItem(voteID);
1297+
g_tiertwo_sync_state.AddedBudgetItem(voteID);
12991298
LogPrint(BCLog::MNBUDGET, "fbvote - new vote (%s) for budget %s from mn %s\n",
13001299
voteID.ToString(), vote.GetBudgetHash().ToString(), voteVin.prevout.ToString());
13011300
return true;

src/masternode-payments.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "evo/deterministicmns.h"
1010
#include "fs.h"
1111
#include "budget/budgetmanager.h"
12-
#include "masternode-sync.h"
1312
#include "masternodeman.h"
1413
#include "netmessagemaker.h"
1514
#include "spork.h"
@@ -446,7 +445,7 @@ bool CMasternodePayments::ProcessMNWinner(CMasternodePaymentWinner& winner, CNod
446445

447446
if (mapMasternodePayeeVotes.count(winner.GetHash())) {
448447
LogPrint(BCLog::MASTERNODE, "mnw - Already seen - %s bestHeight %d\n", winner.GetHash().ToString().c_str(), nHeight);
449-
masternodeSync.AddedMasternodeWinner(winner.GetHash());
448+
g_tiertwo_sync_state.AddedMasternodeWinner(winner.GetHash());
450449
return false;
451450
}
452451

@@ -510,7 +509,7 @@ bool CMasternodePayments::ProcessMNWinner(CMasternodePaymentWinner& winner, CNod
510509
// Relay only if we are synchronized.
511510
// Makes no sense to relay MNWinners to the peers from where we are syncing them.
512511
if (g_tiertwo_sync_state.IsSynced()) winner.Relay();
513-
masternodeSync.AddedMasternodeWinner(winner.GetHash());
512+
g_tiertwo_sync_state.AddedMasternodeWinner(winner.GetHash());
514513

515514
// valid
516515
return true;
@@ -690,7 +689,7 @@ void CMasternodePayments::CleanPaymentList(int mnCount, int nHeight)
690689

691690
if (nHeight - winner.nBlockHeight > nLimit) {
692691
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::CleanPaymentList - Removing old Masternode payment - block %d\n", winner.nBlockHeight);
693-
masternodeSync.mapSeenSyncMNW.erase((*it).first);
692+
g_tiertwo_sync_state.EraseSeenMNW((*it).first);
694693
mapMasternodePayeeVotes.erase(it++);
695694
mapMasternodeBlocks.erase(winner.nBlockHeight);
696695
} else {

src/masternode-sync.cpp

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "budget/budgetmanager.h"
1010
#include "evo/deterministicmns.h"
1111
#include "masternode-sync.h"
12-
#include "masternode-payments.h"
1312
#include "masternode.h"
1413
#include "masternodeman.h"
1514
#include "netmessagemaker.h"
@@ -60,13 +59,8 @@ bool CMasternodeSync::UpdateBlockchainSynced()
6059
void CMasternodeSync::Reset()
6160
{
6261
g_tiertwo_sync_state.SetBlockchainSync(false);
62+
g_tiertwo_sync_state.ResetData();
6363
lastProcess = 0;
64-
lastMasternodeList = 0;
65-
lastMasternodeWinner = 0;
66-
lastBudgetItem = 0;
67-
mapSeenSyncMNB.clear();
68-
mapSeenSyncMNW.clear();
69-
mapSeenSyncBudget.clear();
7064
lastFailure = 0;
7165
nCountFailures = 0;
7266
sumMasternodeList = 0;
@@ -82,35 +76,6 @@ void CMasternodeSync::Reset()
8276
nAssetSyncStarted = GetTime();
8377
}
8478

85-
static void UpdateLastTime(const uint256& hash, int64_t& last, std::map<uint256, int>& mapSeen)
86-
{
87-
auto it = mapSeen.find(hash);
88-
if (it != mapSeen.end()) {
89-
if (it->second < MASTERNODE_SYNC_THRESHOLD) {
90-
last = GetTime();
91-
it->second++;
92-
}
93-
} else {
94-
last = GetTime();
95-
mapSeen.emplace(hash, 1);
96-
}
97-
}
98-
99-
void CMasternodeSync::AddedMasternodeList(const uint256& hash)
100-
{
101-
UpdateLastTime(hash, lastMasternodeList, mapSeenSyncMNB);
102-
}
103-
104-
void CMasternodeSync::AddedMasternodeWinner(const uint256& hash)
105-
{
106-
UpdateLastTime(hash, lastMasternodeWinner, mapSeenSyncMNW);
107-
}
108-
109-
void CMasternodeSync::AddedBudgetItem(const uint256& hash)
110-
{
111-
UpdateLastTime(hash, lastBudgetItem, mapSeenSyncBudget);
112-
}
113-
11479
bool CMasternodeSync::IsBudgetPropEmpty()
11580
{
11681
return sumBudgetItemProp == 0 && countBudgetItemProp > 0;
@@ -334,6 +299,7 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
334299
return false;
335300
}
336301

302+
int lastMasternodeList = g_tiertwo_sync_state.GetlastMasternodeList();
337303
LogPrint(BCLog::MASTERNODE, "CMasternodeSync::Process() - lastMasternodeList %lld (GetTime() - MASTERNODE_SYNC_TIMEOUT) %lld\n", lastMasternodeList, GetTime() - MASTERNODE_SYNC_TIMEOUT);
338304
if (lastMasternodeList > 0 && lastMasternodeList < GetTime() - MASTERNODE_SYNC_TIMEOUT * 8 && RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD) {
339305
// hasn't received a new item in the last 40 seconds AND has sent at least a minimum of MASTERNODE_SYNC_THRESHOLD GETMNLIST requests,
@@ -378,12 +344,13 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
378344
return false;
379345
}
380346

347+
int lastMasternodeWinner = g_tiertwo_sync_state.GetlastMasternodeWinner();
381348
if (lastMasternodeWinner > 0 && lastMasternodeWinner < GetTime() - MASTERNODE_SYNC_TIMEOUT * 2 && RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD) { //hasn't received a new item in the last five seconds, so we'll move to the
382349
SwitchToNextAsset();
383350
// in case we received a budget item while we were syncing the mnw, let's reset the last budget item received time.
384351
// reason: if we received for example a single proposal +50 seconds ago, then once the budget sync starts (right after this call),
385352
// it will look like the sync is finished, and will not wait to receive any budget data and declare the sync over.
386-
lastBudgetItem = 0;
353+
g_tiertwo_sync_state.ResetLastBudgetItem();
387354
return false;
388355
}
389356

@@ -398,7 +365,7 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
398365
// in case we received a budget item while we were syncing the mnw, let's reset the last budget item received time.
399366
// reason: if we received for example a single proposal +50 seconds ago, then once the budget sync starts (right after this call),
400367
// it will look like the sync is finished, and will not wait to receive any budget data and declare the sync over.
401-
lastBudgetItem = 0;
368+
g_tiertwo_sync_state.ResetLastBudgetItem();
402369
}
403370
return false;
404371
}
@@ -421,6 +388,7 @@ bool CMasternodeSync::SyncWithNode(CNode* pnode, bool fLegacyMnObsolete)
421388
}
422389

423390
if (RequestedMasternodeAssets == MASTERNODE_SYNC_BUDGET) {
391+
int lastBudgetItem = g_tiertwo_sync_state.GetlastBudgetItem();
424392
// We'll start rejecting votes if we accidentally get set as synced too soon
425393
if (lastBudgetItem > 0 && lastBudgetItem < GetTime() - MASTERNODE_SYNC_TIMEOUT * 10 && RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD) {
426394
// Hasn't received a new item in the last fifty seconds and more than MASTERNODE_SYNC_THRESHOLD requests were sent,

src/masternode-sync.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <map>
1515

1616
#define MASTERNODE_SYNC_TIMEOUT 5
17-
#define MASTERNODE_SYNC_THRESHOLD 2
1817

1918
class CMasternodeSync;
2019
extern CMasternodeSync masternodeSync;
@@ -31,13 +30,6 @@ struct TierTwoPeerData {
3130
class CMasternodeSync
3231
{
3332
public:
34-
std::map<uint256, int> mapSeenSyncMNB;
35-
std::map<uint256, int> mapSeenSyncMNW;
36-
std::map<uint256, int> mapSeenSyncBudget;
37-
38-
int64_t lastMasternodeList;
39-
int64_t lastMasternodeWinner;
40-
int64_t lastBudgetItem;
4133
int64_t lastFailure;
4234
int nCountFailures;
4335

@@ -62,9 +54,6 @@ class CMasternodeSync
6254

6355
CMasternodeSync();
6456

65-
void AddedMasternodeList(const uint256& hash);
66-
void AddedMasternodeWinner(const uint256& hash);
67-
void AddedBudgetItem(const uint256& hash);
6857
void SwitchToNextAsset();
6958
std::string GetSyncStatus();
7059
void ProcessSyncStatusMsg(int nItemID, int itemCount);

src/masternode.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "masternode.h"
77

88
#include "addrman.h"
9-
#include "masternode-sync.h"
109
#include "masternodeman.h"
1110
#include "netbase.h"
1211
#include "sync.h"
@@ -459,7 +458,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
459458
if (pmn->UpdateFromNewBroadcast((*this))) {
460459
if (pmn->IsEnabled()) Relay();
461460
}
462-
masternodeSync.AddedMasternodeList(GetHash());
461+
g_tiertwo_sync_state.AddedMasternodeList(GetHash());
463462
}
464463

465464
return true;

src/masternodeman.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
262262
std::map<uint256, CMasternodeBroadcast>::iterator it3 = mapSeenMasternodeBroadcast.begin();
263263
while (it3 != mapSeenMasternodeBroadcast.end()) {
264264
if (it3->second.vin.prevout == it->first) {
265-
masternodeSync.mapSeenSyncMNB.erase((*it3).first);
265+
g_tiertwo_sync_state.EraseSeenMNB((*it3).first);
266266
it3 = mapSeenMasternodeBroadcast.erase(it3);
267267
} else {
268268
++it3;
@@ -331,7 +331,7 @@ int CMasternodeMan::CheckAndRemove(bool forceExpiredRemoval)
331331
std::map<uint256, CMasternodeBroadcast>::iterator it3 = mapSeenMasternodeBroadcast.begin();
332332
while (it3 != mapSeenMasternodeBroadcast.end()) {
333333
if ((*it3).second.lastPing.sigTime < GetTime() - (MasternodeRemovalSeconds() * 2)) {
334-
masternodeSync.mapSeenSyncMNB.erase((*it3).second.GetHash());
334+
g_tiertwo_sync_state.EraseSeenMNB((*it3).second.GetHash());
335335
it3 = mapSeenMasternodeBroadcast.erase(it3);
336336
} else {
337337
++it3;
@@ -787,7 +787,7 @@ bool CMasternodeMan::CheckInputs(CMasternodeBroadcast& mnb, int nChainHeight, in
787787
LogPrint(BCLog::MASTERNODE,"mnb - Input must have at least %d confirmations\n", MasternodeCollateralMinConf());
788788
// maybe we miss few blocks, let this mnb to be checked again later
789789
mapSeenMasternodeBroadcast.erase(mnb.GetHash());
790-
masternodeSync.mapSeenSyncMNB.erase(mnb.GetHash());
790+
g_tiertwo_sync_state.EraseSeenMNB(mnb.GetHash());
791791
return false;
792792
}
793793

@@ -808,7 +808,7 @@ int CMasternodeMan::ProcessMNBroadcast(CNode* pfrom, CMasternodeBroadcast& mnb)
808808
{
809809
const uint256& mnbHash = mnb.GetHash();
810810
if (mapSeenMasternodeBroadcast.count(mnbHash)) { //seen
811-
masternodeSync.AddedMasternodeList(mnbHash);
811+
g_tiertwo_sync_state.AddedMasternodeList(mnbHash);
812812
return 0;
813813
}
814814

@@ -850,7 +850,7 @@ int CMasternodeMan::ProcessMNBroadcast(CNode* pfrom, CMasternodeBroadcast& mnb)
850850
g_connman->AddNewAddress(CAddress(mnb.addr, NODE_NETWORK), pfrom->addr, 2 * 60 * 60);
851851

852852
// Update sync status
853-
masternodeSync.AddedMasternodeList(mnbHash);
853+
g_tiertwo_sync_state.AddedMasternodeList(mnbHash);
854854

855855
// All good
856856
return 0;
@@ -1030,7 +1030,7 @@ void CMasternodeMan::UpdateMasternodeList(CMasternodeBroadcast& mnb)
10301030

10311031
mapSeenMasternodePing.emplace(mnb.lastPing.GetHash(), mnb.lastPing);
10321032
mapSeenMasternodeBroadcast.emplace(mnb.GetHash(), mnb);
1033-
masternodeSync.AddedMasternodeList(mnb.GetHash());
1033+
g_tiertwo_sync_state.AddedMasternodeList(mnb.GetHash());
10341034

10351035
LogPrint(BCLog::MASTERNODE,"%s -- masternode=%s\n", __func__, mnb.vin.prevout.ToString());
10361036

src/net_processing.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "primitives/transaction.h"
2121
#include "sporkdb.h"
2222
#include "streams.h"
23+
#include "tiertwo/tiertwo_sync_state.h"
2324
#include "validation.h"
2425
#include "util/validation.h"
2526

@@ -776,37 +777,37 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
776777
return mapSporks.count(inv.hash);
777778
case MSG_MASTERNODE_WINNER:
778779
if (masternodePayments.mapMasternodePayeeVotes.count(inv.hash)) {
779-
masternodeSync.AddedMasternodeWinner(inv.hash);
780+
g_tiertwo_sync_state.AddedMasternodeWinner(inv.hash);
780781
return true;
781782
}
782783
return false;
783784
case MSG_BUDGET_VOTE:
784785
if (g_budgetman.HaveSeenProposalVote(inv.hash)) {
785-
masternodeSync.AddedBudgetItem(inv.hash);
786+
g_tiertwo_sync_state.AddedBudgetItem(inv.hash);
786787
return true;
787788
}
788789
return false;
789790
case MSG_BUDGET_PROPOSAL:
790791
if (g_budgetman.HaveProposal(inv.hash)) {
791-
masternodeSync.AddedBudgetItem(inv.hash);
792+
g_tiertwo_sync_state.AddedBudgetItem(inv.hash);
792793
return true;
793794
}
794795
return false;
795796
case MSG_BUDGET_FINALIZED_VOTE:
796797
if (g_budgetman.HaveSeenFinalizedBudgetVote(inv.hash)) {
797-
masternodeSync.AddedBudgetItem(inv.hash);
798+
g_tiertwo_sync_state.AddedBudgetItem(inv.hash);
798799
return true;
799800
}
800801
return false;
801802
case MSG_BUDGET_FINALIZED:
802803
if (g_budgetman.HaveFinalizedBudget(inv.hash)) {
803-
masternodeSync.AddedBudgetItem(inv.hash);
804+
g_tiertwo_sync_state.AddedBudgetItem(inv.hash);
804805
return true;
805806
}
806807
return false;
807808
case MSG_MASTERNODE_ANNOUNCE:
808809
if (mnodeman.mapSeenMasternodeBroadcast.count(inv.hash)) {
809-
masternodeSync.AddedMasternodeList(inv.hash);
810+
g_tiertwo_sync_state.AddedMasternodeList(inv.hash);
810811
return true;
811812
}
812813
return false;

0 commit comments

Comments
 (0)