Skip to content

Commit 460e0f4

Browse files
authored
Fix locking of funds for mixing (#3194)
* Fix locking of funds for mixing Lock funds earlier and actually lock mixing collaterals * Streamline the locking logic in PrepareDenominate
1 parent 415b81e commit 460e0f4

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/privatesend/privatesend-client.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,6 @@ bool CPrivateSendClientSession::SendDenominate(const std::vector<std::pair<CTxDS
450450
return false;
451451
}
452452

453-
// lock the funds we're going to use
454-
for (const auto& txin : txMyCollateral.vin) {
455-
vecOutPointLocked.push_back(txin.prevout);
456-
}
457-
458-
for (const auto& pair : vecPSInOutPairsIn) {
459-
vecOutPointLocked.push_back(pair.first.prevout);
460-
}
461-
462453
// we should already be connected to a Masternode
463454
if (!nSessionID) {
464455
LogPrint(BCLog::PRIVATESEND, "CPrivateSendClientSession::SendDenominate -- No Masternode has been selected yet.\n");
@@ -922,6 +913,11 @@ bool CPrivateSendClientSession::DoAutomaticDenominating(CConnman& connman, bool
922913
}
923914
}
924915
}
916+
// lock the funds we're going to use for our collateral
917+
for (const auto& txin : txMyCollateral.vin) {
918+
vpwallets[0]->LockCoin(txin.prevout);
919+
vecOutPointLocked.push_back(txin.prevout);
920+
}
925921
} // LOCK2(cs_main, vpwallets[0]->cs_wallet);
926922

927923
// Always attempt to join an existing queue
@@ -1291,16 +1287,15 @@ bool CPrivateSendClientSession::SelectDenominate(std::string& strErrorRet, std::
12911287

12921288
bool CPrivateSendClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds, std::string& strErrorRet, const std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsIn, std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet, bool fDryRun)
12931289
{
1290+
AssertLockHeld(cs_main);
1291+
AssertLockHeld(vpwallets[0]->cs_wallet);
1292+
12941293
std::vector<int> vecBits;
12951294
if (!CPrivateSend::GetDenominationsBits(nSessionDenom, vecBits)) {
12961295
strErrorRet = "Incorrect session denom";
12971296
return false;
12981297
}
12991298

1300-
for (const auto& pair : vecPSInOutPairsIn) {
1301-
vpwallets[0]->LockCoin(pair.first.prevout);
1302-
}
1303-
13041299
// NOTE: No need to randomize order of inputs because they were
13051300
// initially shuffled in CWallet::SelectPSInOutPairsByDenominations already.
13061301
int nDenomResult{0};
@@ -1312,8 +1307,6 @@ bool CPrivateSendClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds
13121307
// Try to add up to PRIVATESEND_ENTRY_MAX_SIZE of every needed denomination
13131308
for (const auto& pair : vecPSInOutPairsIn) {
13141309
if (pair.second.nRounds < nMinRounds || pair.second.nRounds > nMaxRounds) {
1315-
// unlock unused coins
1316-
vpwallets[0]->UnlockCoin(pair.first.prevout);
13171310
continue;
13181311
}
13191312
bool fFound = false;
@@ -1343,22 +1336,23 @@ bool CPrivateSendClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds
13431336
break;
13441337
}
13451338
}
1346-
if (!fFound || fDryRun) {
1347-
// unlock unused coins and if we are not going to mix right away
1348-
vpwallets[0]->UnlockCoin(pair.first.prevout);
1349-
}
13501339
}
13511340

13521341
if (nDenomResult != nSessionDenom) {
1353-
// unlock used coins on failure
1354-
for (const auto& pair : vecPSInOutPairsRet) {
1355-
vpwallets[0]->UnlockCoin(pair.first.prevout);
1356-
}
13571342
keyHolderStorage.ReturnAll();
13581343
strErrorRet = "Can't prepare current denominated outputs";
13591344
return false;
13601345
}
13611346

1347+
if (fDryRun) {
1348+
return true;
1349+
}
1350+
1351+
for (const auto& pair : vecPSInOutPairsRet) {
1352+
vpwallets[0]->LockCoin(pair.first.prevout);
1353+
vecOutPointLocked.push_back(pair.first.prevout);
1354+
}
1355+
13621356
return true;
13631357
}
13641358

0 commit comments

Comments
 (0)