Skip to content

Commit 9c6173c

Browse files
committed
[Budget] Introduce mapFeeTxToProposal and mapFeeTxToBudget
Keep track of the collateral txid for current proposals/budgets. Update in AddProposal/AddFinalizedBudget - CheckAndRemove and database it with CBudgetManager. Also rename mapCollateralTxids to mapUnconfirmedFeeTx to avoid confusion
1 parent ab460a5 commit 9c6173c

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

src/masternode-budget.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void CBudgetManager::SubmitFinalBudget()
199199
}
200200

201201
// See if collateral tx exists
202-
if (!mapCollateralTxids.count(budgetHash)) {
202+
if (!mapUnconfirmedFeeTx.count(budgetHash)) {
203203
// create the collateral tx, send it to the network and return
204204
CWalletTx wtx;
205205
// Get our change address
@@ -210,20 +210,21 @@ void CBudgetManager::SubmitFinalBudget()
210210
}
211211
// Send the tx to the network. Do NOT use SwiftTx, locking might need too much time to propagate, especially for testnet
212212
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, keyChange, g_connman.get(), "NO-ix");
213-
if (res.status == CWallet::CommitStatus::OK)
214-
mapCollateralTxids.emplace(budgetHash, wtx.GetHash());
213+
if (res.status == CWallet::CommitStatus::OK) {
214+
const uint256& collateraltxid = wtx.GetHash();
215+
mapUnconfirmedFeeTx.emplace(budgetHash, collateraltxid);
216+
LogPrint(BCLog::MNBUDGET,"%s: Collateral sent. txid: %s\n", __func__, collateraltxid.ToString());
217+
}
215218
return;
216219
}
217220

218221
// Collateral tx already exists, see if it's mature enough.
219-
CFinalizedBudget fb(strBudgetName, nBlockStart, vecTxBudgetPayments, mapCollateralTxids.at(budgetHash));
222+
CFinalizedBudget fb(strBudgetName, nBlockStart, vecTxBudgetPayments, mapUnconfirmedFeeTx.at(budgetHash));
220223
if (!AddFinalizedBudget(fb)) {
221224
return;
222225
}
223226
fb.Relay();
224227
nSubmittedHeight = nCurrentHeight;
225-
// Remove collateral tx from map
226-
mapCollateralTxids.erase(budgetHash);
227228
LogPrint(BCLog::MNBUDGET,"%s: Done! %s\n", __func__, budgetHash.ToString());
228229
}
229230

@@ -445,9 +446,10 @@ bool CBudgetManager::AddFinalizedBudget(CFinalizedBudget& finalizedBudget)
445446

446447
std::string strError;
447448
int nCurrentHeight = GetBestHeight();
448-
if (!CheckCollateral(finalizedBudget.GetFeeTXHash(), nHash, strError, finalizedBudget.nTime, nCurrentHeight, true)) {
449-
LogPrint(BCLog::MNBUDGET,"%s: invalid finalized budget (%s) collateral - %s\n",
450-
__func__, nHash.ToString(), strError);
449+
const uint256& feeTxId = finalizedBudget.GetFeeTXHash();
450+
if (!CheckCollateral(feeTxId, nHash, strError, finalizedBudget.nTime, nCurrentHeight, true)) {
451+
LogPrint(BCLog::MNBUDGET,"%s: invalid finalized budget (%s) collateral id=%s - %s\n",
452+
__func__, nHash.ToString(), feeTxId.ToString(), strError);
451453
return false;
452454
}
453455

@@ -458,7 +460,15 @@ bool CBudgetManager::AddFinalizedBudget(CFinalizedBudget& finalizedBudget)
458460
}
459461

460462
SetBudgetProposalsStr(finalizedBudget);
461-
WITH_LOCK(cs_budgets, mapFinalizedBudgets.emplace(nHash, finalizedBudget); );
463+
{
464+
LOCK(cs_budgets);
465+
mapFinalizedBudgets.emplace(nHash, finalizedBudget);
466+
// Add to feeTx index
467+
mapFeeTxToBudget.emplace(feeTxId, nHash);
468+
// Remove the budget from the unconfirmed map, if it was there
469+
if (mapUnconfirmedFeeTx.count(nHash))
470+
mapUnconfirmedFeeTx.erase(nHash);
471+
}
462472
LogPrint(BCLog::MNBUDGET,"%s: finalized budget %s [%s (%s)] added\n",
463473
__func__, nHash.ToString(), finalizedBudget.GetName(), finalizedBudget.GetProposalsStr());
464474
return true;
@@ -481,9 +491,10 @@ bool CBudgetManager::AddProposal(CBudgetProposal& budgetProposal)
481491

482492
std::string strError;
483493
int nCurrentHeight = GetBestHeight();
484-
if (!CheckCollateral(budgetProposal.GetFeeTXHash(), nHash, strError, budgetProposal.nTime, nCurrentHeight, false)) {
485-
LogPrint(BCLog::MNBUDGET,"%s: invalid budget proposal (%s) collateral - %s\n",
486-
__func__, nHash.ToString(), strError);
494+
const uint256& feeTxId = budgetProposal.GetFeeTXHash();
495+
if (!CheckCollateral(feeTxId, nHash, strError, budgetProposal.nTime, nCurrentHeight, false)) {
496+
LogPrint(BCLog::MNBUDGET,"%s: invalid budget proposal (%s) collateral id=%s - %s\n",
497+
__func__, nHash.ToString(), feeTxId.ToString(), strError);
487498
return false;
488499
}
489500

@@ -493,8 +504,14 @@ bool CBudgetManager::AddProposal(CBudgetProposal& budgetProposal)
493504
return false;
494505
}
495506

496-
WITH_LOCK(cs_proposals, mapProposals.emplace(nHash, budgetProposal); );
497-
LogPrint(BCLog::MNBUDGET,"%s: proposal %s [%s] added\n", __func__, nHash.ToString(), budgetProposal.GetName());
507+
{
508+
LOCK(cs_proposals);
509+
mapProposals.emplace(nHash, budgetProposal);
510+
// Add to feeTx index
511+
mapFeeTxToProposal.emplace(feeTxId, nHash);
512+
}
513+
LogPrint(BCLog::MNBUDGET,"%s: budget proposal %s [%s] added\n", __func__, nHash.ToString(), budgetProposal.GetName());
514+
498515
return true;
499516
}
500517

@@ -511,6 +528,7 @@ void CBudgetManager::CheckAndRemove()
511528
CFinalizedBudget* pfinalizedBudget = &(it.second);
512529
if (!pfinalizedBudget->UpdateValid(nCurrentHeight)) {
513530
LogPrint(BCLog::MNBUDGET,"%s: Invalid finalized budget %s %s\n", __func__, (it.first).ToString(), pfinalizedBudget->IsInvalidLogStr());
531+
mapFeeTxToBudget.erase(pfinalizedBudget->GetFeeTXHash());
514532
} else {
515533
LogPrint(BCLog::MNBUDGET,"%s: Found valid finalized budget: %s %s\n", __func__,
516534
pfinalizedBudget->GetName(), pfinalizedBudget->GetFeeTXHash().ToString());
@@ -530,6 +548,7 @@ void CBudgetManager::CheckAndRemove()
530548
CBudgetProposal* pbudgetProposal = &(it.second);
531549
if (!pbudgetProposal->UpdateValid(nCurrentHeight)) {
532550
LogPrint(BCLog::MNBUDGET,"%s: Invalid budget proposal %s %s\n", __func__, (it.first).ToString(), pbudgetProposal->IsInvalidLogStr());
551+
mapFeeTxToProposal.erase(pbudgetProposal->GetFeeTXHash());
533552
} else {
534553
LogPrint(BCLog::MNBUDGET,"%s: Found valid budget proposal: %s %s\n", __func__,
535554
pbudgetProposal->GetName(), pbudgetProposal->GetFeeTXHash().ToString());

src/masternode-budget.h

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ class CBudgetManager
200200
{
201201
private:
202202
// map budget hash --> CollTx hash.
203-
// hold finalized-budgets collateral txes until they mature enough to use
204-
std::map<uint256, uint256> mapCollateralTxids;
203+
// hold unconfirmed finalized-budgets collateral txes until they mature enough to use
204+
std::map<uint256, uint256> mapUnconfirmedFeeTx; // guarded by cs_budgets
205+
206+
// map CollTx hash --> budget hash
207+
// keep track of collaterals for valid budgets/proposals (for reorgs)
208+
std::map<uint256, uint256> mapFeeTxToProposal; // guarded by cs_proposals
209+
std::map<uint256, uint256> mapFeeTxToBudget; // guarded by cs_budgets
205210

206211
std::map<uint256, CBudgetProposal> mapProposals; // guarded by cs_proposals
207212
std::map<uint256, CFinalizedBudget> mapFinalizedBudgets; // guarded by cs_budgets
@@ -234,6 +239,8 @@ class CBudgetManager
234239
LOCK2(cs_budgets, cs_proposals);
235240
mapProposals.clear();
236241
mapFinalizedBudgets.clear();
242+
mapFeeTxToProposal.clear();
243+
mapFeeTxToBudget.clear();
237244
}
238245

239246
void ClearSeen()
@@ -302,8 +309,17 @@ class CBudgetManager
302309
void CheckOrphanVotes();
303310
void Clear()
304311
{
305-
WITH_LOCK(cs_proposals, mapProposals.clear(); );
306-
WITH_LOCK(cs_budgets, mapFinalizedBudgets.clear(); );
312+
{
313+
LOCK(cs_proposals);
314+
mapProposals.clear();
315+
mapFeeTxToProposal.clear();
316+
}
317+
{
318+
LOCK(cs_budgets);
319+
mapFinalizedBudgets.clear();
320+
mapFeeTxToBudget.clear();
321+
mapUnconfirmedFeeTx.clear();
322+
}
307323
{
308324
LOCK(cs_votes);
309325
mapSeenProposalVotes.clear();
@@ -323,13 +339,22 @@ class CBudgetManager
323339
template <typename Stream, typename Operation>
324340
inline void SerializationOp(Stream& s, Operation ser_action)
325341
{
326-
WITH_LOCK(cs_proposals, READWRITE(mapProposals); );
342+
{
343+
LOCK(cs_proposals);
344+
READWRITE(mapProposals);
345+
READWRITE(mapFeeTxToProposal);
346+
}
327347
{
328348
LOCK(cs_votes);
329349
READWRITE(mapSeenProposalVotes);
330350
READWRITE(mapOrphanProposalVotes);
331351
}
332-
WITH_LOCK(cs_budgets, READWRITE(mapFinalizedBudgets); );
352+
{
353+
LOCK(cs_budgets);
354+
READWRITE(mapFinalizedBudgets);
355+
READWRITE(mapFeeTxToBudget);
356+
READWRITE(mapUnconfirmedFeeTx);
357+
}
333358
{
334359
LOCK(cs_finalizedvotes);
335360
READWRITE(mapSeenFinalizedBudgetVotes);

0 commit comments

Comments
 (0)