Skip to content

Commit 31e40a4

Browse files
committed
Budget sync: Remove useless extra item sync p2p msgs roundtrip (no need to broadcast an inv if the peer is asking for the item directly..) and stop walking through the two maps (budgets and proposals), locking their mutexes, when the peer is requesting a single proposal/budfin.
1 parent 5f596b8 commit 31e40a4

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/budget/budgetmanager.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,15 +1327,46 @@ void CBudgetManager::SetSynced(bool synced)
13271327
}
13281328
}
13291329

1330+
template<typename Map, typename Item>
1331+
static bool relayItemIfFound(const uint256& itemHash, CNode* pfrom, RecursiveMutex& cs, Map& map, bool fPartial, const char* type)
1332+
{
1333+
CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1334+
LOCK(cs);
1335+
const auto& it = map.find(itemHash);
1336+
if (it == map.end()) return false;
1337+
Item* item = &(it->second);
1338+
if (!item->IsValid()) return true; // don't broadcast invalid items
1339+
g_connman->PushMessage(pfrom, msgMaker.Make(type, item->GetBroadcast()));
1340+
int nInvCount = 1;
1341+
item->SyncVotes(pfrom, fPartial, nInvCount);
1342+
LogPrint(BCLog::MNBUDGET, "%s: single %s sent %d items\n", __func__, type, nInvCount);
1343+
return true;
1344+
}
1345+
13301346
void CBudgetManager::Sync(CNode* pfrom, const uint256& nProp, bool fPartial)
13311347
{
1348+
// Single item request
1349+
if (!nProp.IsNull()) {
1350+
// Try first to relay a proposal
1351+
if (relayItemIfFound<std::map<uint256, CBudgetProposal>, CBudgetProposal>(nProp, pfrom, cs_proposals, mapProposals, fPartial, NetMsgType::BUDGETPROPOSAL)) {
1352+
return;
1353+
}
1354+
// Try now to relay a finalization
1355+
if (relayItemIfFound<std::map<uint256, CFinalizedBudget>, CFinalizedBudget>(nProp, pfrom, cs_budgets, mapFinalizedBudgets, fPartial, NetMsgType::FINALBUDGET)) {
1356+
return;
1357+
}
1358+
LogPrint(BCLog::MNBUDGET, "%s: single request budget item not found\n", __func__);
1359+
return;
1360+
}
1361+
1362+
// Full budget sync request.
13321363
CNetMsgMaker msgMaker(pfrom->GetSendVersion());
13331364
int nInvCount = 0;
13341365
{
13351366
LOCK(cs_proposals);
13361367
for (auto& it: mapProposals) {
13371368
CBudgetProposal* pbudgetProposal = &(it.second);
1338-
if (pbudgetProposal && pbudgetProposal->IsValid() && (nProp.IsNull() || it.first == nProp)) {
1369+
if (pbudgetProposal && pbudgetProposal->IsValid()) {
13391370
pfrom->PushInventory(CInv(MSG_BUDGET_PROPOSAL, it.second.GetHash()));
13401371
nInvCount++;
13411372
pbudgetProposal->SyncVotes(pfrom, fPartial, nInvCount);
@@ -1350,7 +1381,7 @@ void CBudgetManager::Sync(CNode* pfrom, const uint256& nProp, bool fPartial)
13501381
LOCK(cs_budgets);
13511382
for (auto& it: mapFinalizedBudgets) {
13521383
CFinalizedBudget* pfinalizedBudget = &(it.second);
1353-
if (pfinalizedBudget && pfinalizedBudget->IsValid() && (nProp.IsNull() || it.first == nProp)) {
1384+
if (pfinalizedBudget && pfinalizedBudget->IsValid()) {
13541385
pfrom->PushInventory(CInv(MSG_BUDGET_FINALIZED, it.second.GetHash()));
13551386
nInvCount++;
13561387
pfinalizedBudget->SyncVotes(pfrom, fPartial, nInvCount);

0 commit comments

Comments
 (0)