Skip to content

Commit e5ad197

Browse files
committed
Budget: Decouple the all-in-one Budget::Sync() into two functions: (1) single items sync requests and (2) full budget sync requests.
Github-Pull: #2659 Rebased-From: 62ae606
1 parent 5fd74e4 commit e5ad197

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/budget/budgetmanager.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ void CBudgetManager::NewBlock()
953953
CBudgetManager* manager = this;
954954
g_connman->ForEachNode([manager](CNode* pnode){
955955
if (pnode->nVersion >= ActiveProtocol())
956-
manager->Sync(pnode, UINT256_ZERO, true);
956+
manager->Sync(pnode, true);
957957
});
958958
MarkSynced();
959959
}
@@ -1022,7 +1022,8 @@ int CBudgetManager::ProcessBudgetVoteSync(const uint256& nProp, CNode* pfrom)
10221022
}
10231023
}
10241024

1025-
Sync(pfrom, nProp);
1025+
if (nProp.IsNull()) Sync(pfrom, false /* fPartial */);
1026+
else SyncSingleItem(pfrom, nProp);
10261027
LogPrint(BCLog::MNBUDGET, "mnvs - Sent Masternode votes to peer %i\n", pfrom->GetId());
10271028
return 0;
10281029
}
@@ -1350,31 +1351,31 @@ void CBudgetManager::SetSynced(bool synced)
13501351
}
13511352
}
13521353

1353-
template<typename Map, typename Item>
1354-
static bool relayItemIfFound(const uint256& itemHash, CNode* pfrom, RecursiveMutex& cs, Map& map, bool fPartial, const char* type)
1354+
template<typename T>
1355+
static bool relayItemIfFound(const uint256& itemHash, CNode* pfrom, RecursiveMutex& cs, std::map<uint256, T>& map, const char* type)
13551356
{
13561357
CNetMsgMaker msgMaker(pfrom->GetSendVersion());
13571358
LOCK(cs);
13581359
const auto& it = map.find(itemHash);
13591360
if (it == map.end()) return false;
1360-
Item* item = &(it->second);
1361+
T* item = &(it->second);
13611362
if (!item->IsValid()) return true; // don't broadcast invalid items
13621363
g_connman->PushMessage(pfrom, msgMaker.Make(type, item->GetBroadcast()));
13631364
int nInvCount = 1;
1364-
item->SyncVotes(pfrom, fPartial, nInvCount);
1365+
item->SyncVotes(pfrom, false /* fPartial */, nInvCount);
13651366
LogPrint(BCLog::MNBUDGET, "%s: single %s sent %d items\n", __func__, type, nInvCount);
13661367
return true;
13671368
}
13681369

1369-
template<typename Map, typename Item>
1370-
static void relayValidItems(CNode* pfrom, RecursiveMutex& cs, Map& map, bool fPartial, GetDataMsg invType, const int mn_sync_budget_type)
1370+
template<typename T>
1371+
static void relayInventoryItems(CNode* pfrom, RecursiveMutex& cs, std::map<uint256, T>& map, bool fPartial, GetDataMsg invType, const int mn_sync_budget_type)
13711372
{
13721373
CNetMsgMaker msgMaker(pfrom->GetSendVersion());
13731374
int nInvCount = 0;
13741375
{
13751376
LOCK(cs);
13761377
for (auto& it: map) {
1377-
Item* item = &(it.second);
1378+
T* item = &(it.second);
13781379
if (item && item->IsValid()) {
13791380
pfrom->PushInventory(CInv(invType, item->GetHash()));
13801381
nInvCount++;
@@ -1386,25 +1387,26 @@ static void relayValidItems(CNode* pfrom, RecursiveMutex& cs, Map& map, bool fPa
13861387
LogPrint(BCLog::MNBUDGET, "%s: sent %d items\n", __func__, nInvCount);
13871388
}
13881389

1389-
void CBudgetManager::Sync(CNode* pfrom, const uint256& nProp, bool fPartial)
1390+
void CBudgetManager::SyncSingleItem(CNode* pfrom, const uint256& nProp)
13901391
{
1391-
// Single item request
1392-
if (!nProp.IsNull()) {
1393-
// Try first to relay a proposal
1394-
if (relayItemIfFound<std::map<uint256, CBudgetProposal>, CBudgetProposal>(nProp, pfrom, cs_proposals, mapProposals, fPartial, NetMsgType::BUDGETPROPOSAL)) {
1395-
return;
1396-
}
1397-
// Try now to relay a finalization
1398-
if (relayItemIfFound<std::map<uint256, CFinalizedBudget>, CFinalizedBudget>(nProp, pfrom, cs_budgets, mapFinalizedBudgets, fPartial, NetMsgType::FINALBUDGET)) {
1399-
return;
1400-
}
1401-
LogPrint(BCLog::MNBUDGET, "%s: single request budget item not found\n", __func__);
1392+
if (nProp.IsNull()) return;
1393+
// Try first to relay a proposal
1394+
if (relayItemIfFound<CBudgetProposal>(nProp, pfrom, cs_proposals, mapProposals, NetMsgType::BUDGETPROPOSAL)) {
14021395
return;
14031396
}
1397+
// Try now to relay a finalization
1398+
if (relayItemIfFound<CFinalizedBudget>(nProp, pfrom, cs_budgets, mapFinalizedBudgets, NetMsgType::FINALBUDGET)) {
1399+
return;
1400+
}
1401+
LogPrint(BCLog::MNBUDGET, "%s: single request budget item not found\n", __func__);
1402+
}
14041403

1404+
1405+
void CBudgetManager::Sync(CNode* pfrom, bool fPartial)
1406+
{
14051407
// Full budget sync request.
1406-
relayValidItems<std::map<uint256, CBudgetProposal>, CBudgetProposal>(pfrom, cs_proposals, mapProposals, fPartial, MSG_BUDGET_PROPOSAL, MASTERNODE_SYNC_BUDGET_PROP);
1407-
relayValidItems<std::map<uint256, CFinalizedBudget>, CFinalizedBudget>(pfrom, cs_budgets, mapFinalizedBudgets, fPartial, MSG_BUDGET_FINALIZED, MASTERNODE_SYNC_BUDGET_FIN);
1408+
relayInventoryItems<CBudgetProposal>(pfrom, cs_proposals, mapProposals, fPartial, MSG_BUDGET_PROPOSAL, MASTERNODE_SYNC_BUDGET_PROP);
1409+
relayInventoryItems<CFinalizedBudget>(pfrom, cs_budgets, mapFinalizedBudgets, fPartial, MSG_BUDGET_FINALIZED, MASTERNODE_SYNC_BUDGET_FIN);
14081410

14091411
if (!fPartial) {
14101412
// Now that budget full sync request was handled, mark it as completed.

src/budget/budgetmanager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ class CBudgetManager : public CValidationInterface
9898

9999
void ResetSync() { SetSynced(false); }
100100
void MarkSynced() { SetSynced(true); }
101-
void Sync(CNode* node, const uint256& nProp, bool fPartial = false);
101+
// Respond to full budget sync requests and internally triggered partial budget items relay
102+
void Sync(CNode* node, bool fPartial);
103+
// Respond to single budget item requests (proposals / budget finalization)
104+
void SyncSingleItem(CNode* pfrom, const uint256& nProp);
102105
void SetBestHeight(int height) { nBestHeight.store(height, std::memory_order_release); };
103106
int GetBestHeight() const { return nBestHeight.load(std::memory_order_acquire); }
104107

0 commit comments

Comments
 (0)