Skip to content

Commit 68e858f

Browse files
krychlickiUdjinM6
authored andcommitted
PrivateSend: dont waste keys from keypool on failure in CreateDenominated (#1473)
* dont waste keys from keypool on failure in CreateDenominated * bug fix - log actual number of total outputs, comment error * log number of total outputs as separate value * add lock so no one can spend outputs used for denominations
1 parent 18c83f5 commit 68e858f

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/privatesend-client.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "txmempool.h"
1414
#include "util.h"
1515
#include "utilmoneystr.h"
16+
#include <memory>
1617

1718
CPrivateSendClient privateSendClient;
1819

@@ -1198,6 +1199,8 @@ bool CPrivateSendClient::MakeCollateralAmounts(const CompactTallyItem& tallyItem
11981199
// Create denominations by looping through inputs grouped by addresses
11991200
bool CPrivateSendClient::CreateDenominated()
12001201
{
1202+
LOCK2(cs_main, pwalletMain->cs_wallet);
1203+
12011204
std::vector<CompactTallyItem> vecTally;
12021205
if(!pwalletMain->SelectCoinsGrouppedByAddresses(vecTally)) {
12031206
LogPrint("privatesend", "CPrivateSendClient::CreateDenominated -- SelectCoinsGrouppedByAddresses can't find any inputs!\n");
@@ -1241,7 +1244,7 @@ bool CPrivateSendClient::CreateDenominated(const CompactTallyItem& tallyItem, bo
12411244
// ****** Add denoms ************ /
12421245

12431246
// make our denom addresses
1244-
CReserveKey reservekeyDenom(pwalletMain);
1247+
std::vector<std::shared_ptr<CReserveKey>> reservekeyDenomVec;
12451248

12461249
// try few times - skipping smallest denoms first if there are too much already, if failed - use them
12471250
int nOutputsTotal = 0;
@@ -1268,22 +1271,23 @@ bool CPrivateSendClient::CreateDenominated(const CompactTallyItem& tallyItem, bo
12681271

12691272
int nOutputs = 0;
12701273

1271-
// add each output up to 10 times until it can't be added again
1274+
// add each output up to 11 times until it can't be added again
12721275
while(nValueLeft - nDenomValue >= 0 && nOutputs <= 10) {
12731276
CScript scriptDenom;
12741277
CPubKey vchPubKey;
12751278
//use a unique change address
1276-
assert(reservekeyDenom.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
1279+
std::shared_ptr<CReserveKey> reservekeyDenom = std::make_shared<CReserveKey>(pwalletMain);
1280+
reservekeyDenomVec.push_back(reservekeyDenom);
1281+
1282+
assert(reservekeyDenom->GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
12771283
scriptDenom = GetScriptForDestination(vchPubKey.GetID());
1278-
// TODO: do not keep reservekeyDenom here
1279-
reservekeyDenom.KeepKey();
12801284

12811285
vecSend.push_back((CRecipient){ scriptDenom, nDenomValue, false });
12821286

12831287
//increment outputs and subtract denomination amount
12841288
nOutputs++;
12851289
nValueLeft -= nDenomValue;
1286-
LogPrintf("CreateDenominated1: nOutputsTotal: %d, nOutputs: %d, nValueLeft: %f\n", nOutputsTotal, nOutputs, (float)nValueLeft/COIN);
1290+
LogPrintf("CreateDenominated1: totalOutputs: %d, nOutputsTotal: %d, nOutputs: %d, nValueLeft: %f\n", nOutputsTotal + nOutputs, nOutputsTotal, nOutputs, (float)nValueLeft/COIN);
12871291
}
12881292

12891293
nOutputsTotal += nOutputs;
@@ -1316,13 +1320,17 @@ bool CPrivateSendClient::CreateDenominated(const CompactTallyItem& tallyItem, bo
13161320
nFeeRet, nChangePosRet, strFail, &coinControl, true, ONLY_NONDENOMINATED_NOT1000IFMN);
13171321
if(!fSuccess) {
13181322
LogPrintf("CPrivateSendClient::CreateDenominated -- Error: %s\n", strFail);
1319-
// TODO: return reservekeyDenom here
1323+
for(auto key : reservekeyDenomVec)
1324+
key->ReturnKey();
13201325
reservekeyCollateral.ReturnKey();
1326+
LogPrintf("CPrivateSendClient::CreateDenominated -- %d keys returned\n", reservekeyDenomVec.size() + 1);
13211327
return false;
13221328
}
13231329

1324-
// TODO: keep reservekeyDenom here
1330+
for(auto key : reservekeyDenomVec)
1331+
key->KeepKey();
13251332
reservekeyCollateral.KeepKey();
1333+
LogPrintf("CPrivateSendClient::CreateDenominated -- %d keys keeped\n", reservekeyDenomVec.size() + 1);
13261334

13271335
if(!pwalletMain->CommitTransaction(wtx, reservekeyChange)) {
13281336
LogPrintf("CPrivateSendClient::CreateDenominated -- CommitTransaction failed!\n");

0 commit comments

Comments
 (0)