Skip to content

Commit 6cbb668

Browse files
Fix multiple bugs in CreateSparkMintTransactions
Bug fixes in sparkwallet.cpp: 1. Fix typo: mapMultiArgs.at("change") -> mapMultiArgs.at("-change") This would cause std::out_of_range exception when -change arg is used. 2. Add empty check before utxos.second.front() access to prevent undefined behavior if any entry has an empty vector. 3. Add break after finding matching UTXO entry to avoid unnecessary iterations and potential issues. 4. Fix missing strFailReason when SelectCoins fails for reasons other than insufficient funds - now sets "Unable to select coins for minting". 5. Fix missing strFailReason when function fails because valueToMint > 0 at end - now sets descriptive error message. 6. Fix misleading error message: "Signing transaction failed" changed to "Transaction not allowed in mempool" for mempool rejection. Bug fix in wallet.cpp: - Same typo fix for mapMultiArgs.at("change") in CreateLelantusMintTransactions These bugs could potentially cause crashes (especially #1 and #2) or make debugging difficult due to missing/wrong error messages. Co-authored-by: reuben <[email protected]>
1 parent 5e45b7a commit 6cbb668

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/spark/sparkwallet.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,10 @@ bool CSparkWallet::CreateSparkMintTransactions(
959959
// Choose coins to use
960960
CAmount nValueIn = 0;
961961
if (!pwalletMain->SelectCoins(itr->second, nValueToSelect, setCoins, nValueIn, coinControl)) {
962-
963962
if (nValueIn < nValueToSelect) {
964963
strFailReason = _("Insufficient funds");
964+
} else {
965+
strFailReason = _("Unable to select coins for minting");
965966
}
966967
return false;
967968
}
@@ -995,7 +996,7 @@ bool CSparkWallet::CreateSparkMintTransactions(
995996
// send change to one of the specified change addresses
996997
else if (IsArgSet("-change") && mapMultiArgs.at("-change").size() > 0) {
997998
CBitcoinAddress address(
998-
mapMultiArgs.at("change")[GetRandInt(mapMultiArgs.at("-change").size())]);
999+
mapMultiArgs.at("-change")[GetRandInt(mapMultiArgs.at("-change").size())]);
9991000
CKeyID keyID;
10001001
if (!address.GetKeyID(keyID)) {
10011002
strFailReason = _("Bad change address");
@@ -1209,7 +1210,7 @@ bool CSparkWallet::CreateSparkMintTransactions(
12091210
{
12101211
CValidationState state;
12111212
if (!mempool.IsTransactionAllowed(*wtx.tx, state)) {
1212-
strFailReason = _("Signing transaction failed");
1213+
strFailReason = _("Transaction not allowed in mempool");
12131214
return false;
12141215
}
12151216
}
@@ -1228,12 +1229,14 @@ bool CSparkWallet::CreateSparkMintTransactions(
12281229

12291230
bool added = false;
12301231
for (auto &utxos : valueAndUTXO) {
1232+
if (utxos.second.empty())
1233+
continue;
12311234
auto const &o = utxos.second.front();
12321235
if (o.tx->tx->vout[o.i].scriptPubKey == wtx.tx->vout[nChangePosInOut].scriptPubKey) {
12331236
utxos.first += val;
12341237
utxos.second.push_back(out);
1235-
12361238
added = true;
1239+
break;
12371240
}
12381241
}
12391242

@@ -1253,6 +1256,7 @@ bool CSparkWallet::CreateSparkMintTransactions(
12531256
}
12541257

12551258
if (!autoMintAll && valueToMint > 0) {
1259+
strFailReason = _("Unable to mint full amount; only partial minting was possible");
12561260
return false;
12571261
}
12581262

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4487,7 +4487,7 @@ bool CWallet::CreateLelantusMintTransactions(
44874487
// send change to one of the specified change addresses
44884488
else if (IsArgSet("-change") && mapMultiArgs.at("-change").size() > 0) {
44894489
CBitcoinAddress address(
4490-
mapMultiArgs.at("change")[GetRandInt(mapMultiArgs.at("-change").size())]);
4490+
mapMultiArgs.at("-change")[GetRandInt(mapMultiArgs.at("-change").size())]);
44914491
CKeyID keyID;
44924492
if (!address.GetKeyID(keyID)) {
44934493
strFailReason = _("Bad change address");

0 commit comments

Comments
 (0)