From eb6bb71151cdd2d4db6783247dd59d7962813e50 Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Sun, 10 Sep 2023 21:01:55 +0200 Subject: [PATCH 1/6] Avoid catch(...) when possible --- src/batchproof_container.cpp | 6 +++--- src/bip47/account.cpp | 2 +- src/bip47/bip47utils.cpp | 2 +- src/chainparams.cpp | 6 +++--- src/elysium/elysium.cpp | 2 +- src/elysium/rpctx.cpp | 2 +- src/elysium/wallet.cpp | 2 +- src/hdmint/tracker.cpp | 4 ++-- src/hdmint/wallet.cpp | 2 +- src/lelantus.cpp | 10 +++++----- src/liblelantus/lelantus_prover.cpp | 2 +- src/libspark/coin.cpp | 4 ++-- src/libspark/hash.cpp | 4 ++-- src/libspark/transcript.cpp | 2 +- src/libspark/util.cpp | 2 +- src/qt/transactiondesc.cpp | 2 +- src/qt/transactionrecord.cpp | 2 +- src/rpc/rawtransaction.cpp | 4 ++-- src/sigma.cpp | 2 +- src/spark/sparkwallet.cpp | 10 +++++----- src/spark/state.cpp | 20 ++++++++++---------- src/validation.cpp | 22 +++++++++++----------- src/wallet/rpcwallet.cpp | 16 ++++++++-------- src/wallet/wallet.cpp | 26 +++++++++++++------------- 24 files changed, 78 insertions(+), 78 deletions(-) diff --git a/src/batchproof_container.cpp b/src/batchproof_container.cpp index 9b89c69b25..d82e4638ea 100644 --- a/src/batchproof_container.cpp +++ b/src/batchproof_container.cpp @@ -213,7 +213,7 @@ void BatchProofContainer::batch_sigma() { try { if (!sigmaVerifier.batch_verify(anonymity_set, serials, fPadding, setSizes, proofs)) return false; - } catch (...) { + } catch (const std::exception &) { return false; } return true; @@ -316,7 +316,7 @@ void BatchProofContainer::batch_lelantus() { try { if (!sigmaVerifier.batchverify(anonymity_set, challenges, serials, setSizes, proofs)) return false; - } catch (...) { + } catch (const std::exception &) { return false; } return true; @@ -431,7 +431,7 @@ void BatchProofContainer::batch_spark() { bool passed; try { passed = spark::SpendTransaction::verify(params, sparkTransactions, cover_sets); - } catch (...) { + } catch (const std::exception &) { passed = false; } diff --git a/src/bip47/account.cpp b/src/bip47/account.cpp index 09ef4ce7b2..adb0c7fac6 100644 --- a/src/bip47/account.cpp +++ b/src/bip47/account.cpp @@ -254,7 +254,7 @@ bool CAccountReceiver::acceptMaskedPayload(std::vector const & ma std::unique_ptr jsplit; try { jsplit = lelantus::ParseLelantusJoinSplit(tx); - }catch (...) { + }catch (const std::exception &) { return false; } if (!jsplit) diff --git a/src/bip47/bip47utils.cpp b/src/bip47/bip47utils.cpp index 6b2e482e0d..8dacc6359b 100644 --- a/src/bip47/bip47utils.cpp +++ b/src/bip47/bip47utils.cpp @@ -170,7 +170,7 @@ GroupElement GeFromPubkey(CPubKey const & pubKey) serializedGe.push_back(0x0); try { result.deserialize(&serializedGe[0]); - } catch (...) { + } catch (const std::exception &) { result = GroupElement(); } return result; diff --git a/src/chainparams.cpp b/src/chainparams.cpp index f8ef8588a5..98f3a19028 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -425,7 +425,7 @@ class CMainParams : public CChainParams { GroupElement coin; try { coin.deserialize(ParseHex(str).data()); - } catch (...) { + } catch (const std::exception &) { continue; } consensus.lelantusBlacklist.insert(coin); @@ -435,7 +435,7 @@ class CMainParams : public CChainParams { GroupElement coin; try { coin.deserialize(ParseHex(str).data()); - } catch (...) { + } catch (const std::exception &) { continue; } consensus.sigmaBlacklist.insert(coin); @@ -728,7 +728,7 @@ class CTestNetParams : public CChainParams { GroupElement coin; try { coin.deserialize(ParseHex(str).data()); - } catch (...) { + } catch (const std::exception &) { continue; } consensus.lelantusBlacklist.insert(coin); diff --git a/src/elysium/elysium.cpp b/src/elysium/elysium.cpp index 0f30f1c643..4d8091f491 100644 --- a/src/elysium/elysium.cpp +++ b/src/elysium/elysium.cpp @@ -2329,7 +2329,7 @@ int elysium::WalletTxBuilder( case InputMode::SIGMA: try { if (!pwalletMain->CommitSigmaTransaction(wtxNew, sigmaSelected, sigmaChanges)) return MP_ERR_COMMIT_TX; - } catch (...) { + } catch (const std::exception &) { return MP_ERR_COMMIT_TX; } break; diff --git a/src/elysium/rpctx.cpp b/src/elysium/rpctx.cpp index 723bdb3661..eb30d4a504 100644 --- a/src/elysium/rpctx.cpp +++ b/src/elysium/rpctx.cpp @@ -1689,7 +1689,7 @@ UniValue elysium_sendmint(const JSONRPCRequest& request) if (result != 0) { throw JSONRPCError(result, error_str(result)); } - } catch (...) { + } catch (const std::exception &) { for (auto& id : ids) { wallet->DeleteUnconfirmedSigmaMint(id); } diff --git a/src/elysium/wallet.cpp b/src/elysium/wallet.cpp index f1771ea0fe..f8686dfcbe 100644 --- a/src/elysium/wallet.cpp +++ b/src/elysium/wallet.cpp @@ -197,7 +197,7 @@ SigmaPrivateKey Wallet::GetKey(const SigmaMint &mint) // Try all mint wallets try { return mintWalletV1.GeneratePrivateKey(mint.seedId); - } catch (...) { + } catch (const std::exception &) { return mintWalletV0.GeneratePrivateKey(mint.seedId); } } diff --git a/src/hdmint/tracker.cpp b/src/hdmint/tracker.cpp index c394e721d5..677e88c817 100644 --- a/src/hdmint/tracker.cpp +++ b/src/hdmint/tracker.cpp @@ -546,7 +546,7 @@ bool CHDMintTracker::IsMempoolSpendOurs(const std::set& setMempool, con uint32_t pubcoinId; try { std::tie(spend, pubcoinId) = sigma::ParseSigmaSpend(txin); - } catch (...) { + } catch (const std::exception &) { return false; } @@ -560,7 +560,7 @@ bool CHDMintTracker::IsMempoolSpendOurs(const std::set& setMempool, con std::unique_ptr joinsplit; try { joinsplit = lelantus::ParseLelantusJoinSplit(tx); - } catch (...) { + } catch (const std::exception &) { return false; } diff --git a/src/hdmint/wallet.cpp b/src/hdmint/wallet.cpp index 235995427c..98444fd720 100644 --- a/src/hdmint/wallet.cpp +++ b/src/hdmint/wallet.cpp @@ -1183,7 +1183,7 @@ bool CHDMintWallet::TxOutToPublicCoin(const CTxOut& txout, sigma::PublicCoin& pu secp_primitives::GroupElement publicSigma; try { publicSigma.deserialize(&coin_serialised[0]); - } catch (...) { + } catch (const std::exception &) { return state.DoS(100, error("TxOutToPublicCoin : deserialize failed")); } diff --git a/src/lelantus.cpp b/src/lelantus.cpp index 9c71202e1b..92161c8d5c 100644 --- a/src/lelantus.cpp +++ b/src/lelantus.cpp @@ -399,7 +399,7 @@ bool CheckLelantusJoinSplitTransaction( REJECT_MALFORMED, "CheckLelantusJoinSplitTransaction: invalid joinsplit transaction"); } - catch (...) { + catch (const std::exception &) { return state.DoS(100, false, REJECT_MALFORMED, @@ -812,7 +812,7 @@ void RemoveLelantusJoinSplitReferencingBlock(CTxMemPool& pool, CBlockIndex* bloc try { joinsplit = ParseLelantusJoinSplit(tx); } - catch (...) { + catch (const std::exception &) { txn_to_remove.push_back(tx); break; } @@ -851,7 +851,7 @@ std::vector GetLelantusJoinSplitSerialNumbers(const CTransaction &tx, co try { return ParseLelantusJoinSplit(tx)->getCoinSerialNumbers(); } - catch (...) { + catch (const std::exception &) { return std::vector(); } } @@ -863,7 +863,7 @@ std::vector GetLelantusJoinSplitIds(const CTransaction &tx, const CTxI try { return ParseLelantusJoinSplit(tx)->getCoinGroupIds(); } - catch (...) { + catch (const std::exception &) { return std::vector(); } } @@ -1003,7 +1003,7 @@ bool GetOutPointFromBlock(COutPoint& outPoint, const GroupElement &pubCoinValue, try { ParseLelantusMintScript(txout.scriptPubKey, txPubCoinValue); } - catch (...) { + catch (const std::exception &) { continue; } if(pubCoinValue==txPubCoinValue){ diff --git a/src/liblelantus/lelantus_prover.cpp b/src/liblelantus/lelantus_prover.cpp index 96d908b676..3154b51ea6 100644 --- a/src/liblelantus/lelantus_prover.cpp +++ b/src/liblelantus/lelantus_prover.cpp @@ -155,7 +155,7 @@ void LelantusProver::generate_sigma_proofs( parallelTasks.emplace_back(threadPool.PostTask([&]() { try { prover.sigma_commit(commits, index, rA_i, rB_i, rC_i, rD_i, a_i, Tk_i, Pk_i, Yk_i, sigma_i, proof); - } catch (...) { + } catch (const std::exception &) { return false; } return true; diff --git a/src/libspark/coin.cpp b/src/libspark/coin.cpp index 05f271bfb8..e11d7814ce 100644 --- a/src/libspark/coin.cpp +++ b/src/libspark/coin.cpp @@ -127,7 +127,7 @@ IdentifiedCoinData Coin::identify(const IncomingViewKey& incoming_view_key) { // Decrypt recipient data CDataStream stream = AEAD::decrypt_and_verify(this->K*incoming_view_key.get_s1(), "Mint coin data", this->r_); stream >> r; - } catch (...) { + } catch (const std::exception &) { throw std::runtime_error("Unable to identify coin"); } @@ -142,7 +142,7 @@ IdentifiedCoinData Coin::identify(const IncomingViewKey& incoming_view_key) { // Decrypt recipient data CDataStream stream = AEAD::decrypt_and_verify(this->K*incoming_view_key.get_s1(), "Spend coin data", this->r_); stream >> r; - } catch (...) { + } catch (const std::exception &) { throw std::runtime_error("Unable to identify coin"); } diff --git a/src/libspark/hash.cpp b/src/libspark/hash.cpp index c37d29a1ea..2c6d71317d 100644 --- a/src/libspark/hash.cpp +++ b/src/libspark/hash.cpp @@ -83,7 +83,7 @@ Scalar Hash::finalize_scalar() { EVP_MD_CTX_free(state_finalize); return candidate; - } catch (...) { + } catch (const std::exception &) { counter++; } } @@ -144,7 +144,7 @@ GroupElement Hash::finalize_group() { EVP_MD_CTX_free(state_finalize); return candidate; - } catch (...) { + } catch (const std::exception &) { counter++; } } diff --git a/src/libspark/transcript.cpp b/src/libspark/transcript.cpp index 8cada15b2e..5cd67c63c0 100644 --- a/src/libspark/transcript.cpp +++ b/src/libspark/transcript.cpp @@ -139,7 +139,7 @@ Scalar Transcript::challenge(const std::string label) { EVP_MD_CTX_free(state_finalize); return candidate; - } catch (...) { + } catch (const std::exception &) { counter++; } } diff --git a/src/libspark/util.cpp b/src/libspark/util.cpp index cb3bed31fe..4547251320 100644 --- a/src/libspark/util.cpp +++ b/src/libspark/util.cpp @@ -125,7 +125,7 @@ GroupElement SparkUtils::hash_generator(const std::string label) { EVP_MD_CTX_free(state_finalize); return candidate; - } catch (...) { + } catch (const std::exception &) { counter++; } } diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index 5cef9772f6..0e99e63a71 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -244,7 +244,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco try { nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee(); } - catch (...) { + catch (const std::exception &) { //do nothing } } diff --git a/src/qt/transactionrecord.cpp b/src/qt/transactionrecord.cpp index bbec4463d3..b69a7ba894 100644 --- a/src/qt/transactionrecord.cpp +++ b/src/qt/transactionrecord.cpp @@ -65,7 +65,7 @@ QList TransactionRecord::decomposeTransaction(const CWallet * if (isAllJoinSplitFromMe && wtx.tx->vin.size() > 0) { try { nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee(); - } catch (...) { + } catch (const std::exception &) { // do nothing } } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 38606f55ed..c2fe430098 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -127,7 +127,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) try { jsplit = lelantus::ParseLelantusJoinSplit(tx); } - catch (...) { + catch (const std::exception &) { continue; } in.push_back(Pair("nFees", ValueFromAmount(jsplit->getFee()))); @@ -143,7 +143,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) try { sparkSpend = std::make_unique(spark::ParseSparkSpend(tx)); } - catch (...) { + catch (const std::exception &) { continue; } in.push_back(Pair("nFees", ValueFromAmount(sparkSpend->getFee()))); diff --git a/src/sigma.cpp b/src/sigma.cpp index 1958c19e02..44617859d1 100644 --- a/src/sigma.cpp +++ b/src/sigma.cpp @@ -663,7 +663,7 @@ bool GetOutPointFromBlock(COutPoint& outPoint, const GroupElement &pubCoinValue, txout.scriptPubKey.end()); try { txPubCoinValue.deserialize(&coin_serialised[0]); - } catch (...) { + } catch (const std::exception &) { return false; } if(pubCoinValue==txPubCoinValue){ diff --git a/src/spark/sparkwallet.cpp b/src/spark/sparkwallet.cpp index 5b9372308e..e96fd8fb16 100644 --- a/src/spark/sparkwallet.cpp +++ b/src/spark/sparkwallet.cpp @@ -260,7 +260,7 @@ bool CSparkWallet::isAddressMine(const std::string& encodedAddr) { spark::Address address(params); try { address.decode(encodedAddr); - } catch (...) { + } catch (const std::exception &) { return false; } @@ -273,7 +273,7 @@ bool CSparkWallet::isAddressMine(const std::string& encodedAddr) { try { d = viewKey.get_diversifier(address.get_d()); - } catch (...) { + } catch (const std::exception &) { return false; } @@ -424,7 +424,7 @@ bool CSparkWallet::getMintAmount(spark::Coin coin, CAmount& amount) { spark::IdentifiedCoinData identifiedCoinData; try { identifiedCoinData = coin.identify(this->viewKey); - } catch (...) { + } catch (const std::exception &) { return false; } amount = identifiedCoinData.v; @@ -488,7 +488,7 @@ void CSparkWallet::UpdateSpendStateFromBlock(const CBlock& block) { uint256 lTagHash = primitives::GetLTagHash(txLTag); UpdateSpendState(txLTag, lTagHash, txHash); } - } catch (...) { + } catch (const std::exception &) { } } } @@ -498,7 +498,7 @@ void CSparkWallet::UpdateSpendStateFromBlock(const CBlock& block) { bool CSparkWallet::isMine(spark::Coin coin) const { try { spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey); - } catch (...) { + } catch (const std::exception &) { return false; } diff --git a/src/spark/state.cpp b/src/spark/state.cpp index 9cce5ff48c..2eb6248ccc 100644 --- a/src/spark/state.cpp +++ b/src/spark/state.cpp @@ -129,7 +129,7 @@ void ParseSparkMintTransaction(const std::vector& scripts, MintTransact } try { mintTransaction.setMintTransaction(serializedCoins); - } catch (...) { + } catch (const std::exception &) { throw std::invalid_argument("Unable to deserialize Spark mint transaction"); } } @@ -152,7 +152,7 @@ void ParseSparkMintCoin(const CScript& script, spark::Coin& txCoin) try { stream >> txCoin; - } catch (...) { + } catch (const std::exception &) { throw std::invalid_argument("Unable to deserialize Spark mint"); } } @@ -184,7 +184,7 @@ std::vector GetSparkUsedTags(const CTransaction &tx) spark::SpendTransaction spendTransaction(params); try { spendTransaction = ParseSparkSpend(tx); - } catch (...) { + } catch (const std::exception &) { return std::vector(); } @@ -205,7 +205,7 @@ std::vector GetSparkMintCoins(const CTransaction &tx) ParseSparkMintCoin(script, coin); coin.setSerialContext(serial_context); result.push_back(coin); - } catch (...) { + } catch (const std::exception &) { //Continue } } @@ -332,7 +332,7 @@ void RemoveSpendReferencingBlock(CTxMemPool& pool, CBlockIndex* blockIndex) { try { sparkSpend = std::make_unique(ParseSparkSpend(tx)); } - catch (...) { + catch (const std::exception &) { txn_to_remove.push_back(tx); break; } @@ -470,7 +470,7 @@ bool CheckSparkSMintTransaction( spark::Coin coin(Params::get_default()); ParseSparkMintCoin(script, coin); out_coins.push_back(coin); - } catch (...) { + } catch (const std::exception &) { return state.DoS(100, false, REJECT_INVALID, @@ -529,7 +529,7 @@ bool CheckSparkSpendTransaction( REJECT_MALFORMED, "CheckSparkSpendTransaction: invalid spend transaction"); } - catch (...) { + catch (const std::exception &) { return state.DoS(100, false, REJECT_MALFORMED, @@ -653,7 +653,7 @@ bool CheckSparkSpendTransaction( } else { try { passVerify = spark::SpendTransaction::verify(*spend, cover_sets); - } catch (...) { + } catch (const std::exception &) { passVerify = false; } } @@ -810,7 +810,7 @@ bool GetOutPointFromBlock(COutPoint& outPoint, const spark::Coin& coin, const CB try { ParseSparkMintCoin(txout.scriptPubKey, txCoin); } - catch (...) { + catch (const std::exception &) { continue; } if (coin == txCoin) { @@ -830,7 +830,7 @@ std::vector getSerialContext(const CTransaction &tx) { try { spark::SpendTransaction spend = ParseSparkSpend(tx); serialContextStream << spend.getUsedLTags(); - } catch (...) { + } catch (const std::exception &) { return std::vector(); } } else { diff --git a/src/validation.cpp b/src/validation.cpp index b8edbf0320..ceec9589ec 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -860,7 +860,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C catch (CBadTxIn&) { return state.Invalid(false, REJECT_CONFLICT, "txn-invalid-lelantus-joinsplit"); } - catch (...) { + catch (const std::exception &) { return state.Invalid(false, REJECT_CONFLICT, "failed to deserialize joinsplit"); } @@ -903,7 +903,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C try { sparkUsedLTags = spark::GetSparkUsedTags(tx); } - catch (...) { + catch (const std::exception &) { return state.Invalid(false, REJECT_CONFLICT, "failed to deserialize spark spend"); } @@ -951,7 +951,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C try { sparkMintCoins = spark::GetSparkMintCoins(tx); } - catch (...) { + catch (const std::exception &) { return state.Invalid(false, REJECT_CONFLICT, "failed to deserialize spark mint"); } @@ -1147,7 +1147,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C catch (CBadTxIn&) { return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit"); } - catch (...) { + catch (const std::exception &) { return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit"); } } else { @@ -1157,7 +1157,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C catch (CBadTxIn&) { return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit"); } - catch (...) { + catch (const std::exception &) { return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit"); } } @@ -2083,7 +2083,7 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins catch (CBadTxIn&) { return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit"); } - catch (...) { + catch (const std::exception &) { return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit"); } } @@ -2520,7 +2520,7 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s try { nFees += lelantus::ParseLelantusJoinSplit(tx)->getFee(); } - catch (...) { + catch (const std::exception &) { // do nothing } } @@ -2528,7 +2528,7 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s try { nFees = spark::ParseSparkSpend(tx).getFee(); } - catch (...) { + catch (const std::exception &) { // do nothing } } @@ -2918,7 +2918,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin catch (CBadTxIn&) { return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit"); } - catch (...) { + catch (const std::exception &) { return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit"); } } @@ -2930,7 +2930,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin catch (CBadTxIn&) { return state.DoS(0, false, REJECT_INVALID, "unable to parse spark spend"); } - catch (...) { + catch (const std::exception &) { return state.DoS(0, false, REJECT_INVALID, "failed to deserialize spark spend"); } } @@ -3486,7 +3486,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara try { joinsplit = lelantus::ParseLelantusJoinSplit(*tx); } - catch (...) { + catch (const std::exception &) { continue; } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 96fd91a14d..152ecf3626 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2082,14 +2082,14 @@ UniValue gettransaction(const JSONRPCRequest& request) try { nFee = (0 - lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee()); } - catch (...) { + catch (const std::exception &) { // do nothing } } else if (wtx.tx->IsSparkSpend()) { try { nFee = (0 - spark::ParseSparkSpend(*wtx.tx).getFee()); } - catch (...) { + catch (const std::exception &) { // do nothing } } @@ -3437,7 +3437,7 @@ UniValue getsparkaddressbalance(const JSONRPCRequest& request) { unsigned char coinNetwork; try { coinNetwork = address.decode(strAddress); - } catch (...) { + } catch (const std::exception &) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Spark address: ")+strAddress); } @@ -3559,7 +3559,7 @@ UniValue mintspark(const JSONRPCRequest& request) unsigned char coinNetwork; try { coinNetwork = address.decode(name_); - } catch (...) { + } catch (const std::exception &) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Spark address: ")+name_); } @@ -3694,7 +3694,7 @@ UniValue spendspark(const JSONRPCRequest& request) isSparkAddress = true; if (coinNetwork != network) throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid address, wrong network type: ")+name_); - } catch (...) { + } catch (const std::exception &) { isSparkAddress = false; } @@ -3768,7 +3768,7 @@ UniValue spendspark(const JSONRPCRequest& request) CWalletTx wtx; try { wtx = pwallet->SpendAndStoreSpark(recipients, privateRecipients, fee); - } catch (...) { + } catch (const std::exception &) { throw JSONRPCError(RPC_WALLET_ERROR, "Spark spend creation failed."); } @@ -3800,7 +3800,7 @@ UniValue lelantustospark(const JSONRPCRequest& request) { bool passed = false; try { passed = pwallet->LelantusToSpark(strFailReason); - } catch (...) { + } catch (const std::exception &) { throw JSONRPCError(RPC_WALLET_ERROR, "Lelantus to Spark failed!"); } if (!passed || strFailReason != "") @@ -4751,7 +4751,7 @@ UniValue listlelantusjoinsplits(const JSONRPCRequest& request) { std::unique_ptr joinsplit; try { joinsplit = lelantus::ParseLelantusJoinSplit(*pwtx->tx); - } catch (...) { + } catch (const std::exception &) { continue; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 769c9e7b49..331c5547d9 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1458,7 +1458,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx) try { joinsplit = lelantus::ParseLelantusJoinSplit(*wtx.tx); } - catch (...) { + catch (const std::exception &) { continue; } @@ -1484,7 +1484,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx) spark::SpendTransaction spend = spark::ParseSparkSpend(*wtx.tx); lTags = spend.getUsedLTags(); } - catch (...) { + catch (const std::exception &) { continue; } @@ -1639,7 +1639,7 @@ isminetype CWallet::IsMine(const CTxIn &txin, const CTransaction& tx) const try { joinsplit = lelantus::ParseLelantusJoinSplit(tx); } - catch (...) { + catch (const std::exception &) { return ISMINE_NO; } @@ -1654,7 +1654,7 @@ isminetype CWallet::IsMine(const CTxIn &txin, const CTransaction& tx) const spark::SpendTransaction spend = spark::ParseSparkSpend(tx); lTags = spend.getUsedLTags(); } - catch (...) { + catch (const std::exception &) { return ISMINE_NO; } if (!sparkWallet) @@ -1712,7 +1712,7 @@ CAmount CWallet::GetDebit(const CTxIn &txin, const CTransaction& tx, const ismin try { joinsplit = lelantus::ParseLelantusJoinSplit(tx); } - catch (...) { + catch (const std::exception &) { goto end; } @@ -1734,7 +1734,7 @@ CAmount CWallet::GetDebit(const CTxIn &txin, const CTransaction& tx, const ismin spark::SpendTransaction spend = spark::ParseSparkSpend(tx); lTags = spend.getUsedLTags(); } - catch (...) { + catch (const std::exception &) { goto end; } if (!sparkWallet) @@ -1896,7 +1896,7 @@ CAmount CWallet::GetChange(const uint256& tx, const CTxOut &txout) const try { spark::ParseSparkMintCoin(txout.scriptPubKey, coin); coin.setSerialContext(serial_context); - } catch (...) { + } catch (const std::exception &) { return 0; } return sparkWallet->getMyCoinV(coin); @@ -2256,14 +2256,14 @@ void CWalletTx::GetAmounts(std::list& listReceived, try { nFee = lelantus::ParseLelantusJoinSplit(*tx)->getFee(); } - catch (...) { + catch (const std::exception &) { // do nothing } } else if (tx->IsSparkSpend()) { try { nFee = spark::ParseSparkSpend(*tx).getFee(); } - catch (...) { + catch (const std::exception &) { // do nothing } } else { @@ -2712,7 +2712,7 @@ bool CWalletTx::IsChange(uint32_t out) const { try { spark::ParseSparkMintCoin(tx->vout[out].scriptPubKey, coin); coin.setSerialContext(serial_context); - } catch (...) { + } catch (const std::exception &) { return false; } return pwallet->sparkWallet->getMyCoinIsChange(coin); @@ -5590,7 +5590,7 @@ bool CWallet::CommitSigmaTransaction(CWalletTx& wtxNew, std::vector CValidationState state; CReserveKey reserveKey(this); CommitTransaction(wtxNew, reserveKey, g_connman.get(), state); - } catch (...) { + } catch (const std::exception &) { auto error = _( "Error: The transaction was rejected! This might happen if some of " "the coins in your wallet were already spent, such as if you used " @@ -5739,7 +5739,7 @@ CWalletTx CWallet::SpendAndStoreSpark( CValidationState state; CReserveKey reserveKey(this); CommitTransaction(result, reserveKey, g_connman.get(), state); - } catch (...) { + } catch (const std::exception &) { auto error = _( "Error: The transaction was rejected! This might happen if some of " "the coins in your wallet were already spent, such as if you used " @@ -5890,7 +5890,7 @@ bool CWallet::CommitLelantusTransaction(CWalletTx& wtxNew, std::vector Date: Sat, 16 Sep 2023 14:40:42 +0200 Subject: [PATCH 2/6] Add wrappers around major sigma/lelantus/spark calls to catch exceptions --- src/lelantus.cpp | 22 ++++++++++++++++------ src/sigma.cpp | 22 ++++++++++++++++------ src/spark/state.cpp | 24 +++++++++++++++++------- src/validation.cpp | 9 ++++++++- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/lelantus.cpp b/src/lelantus.cpp index 92161c8d5c..a4204186cf 100644 --- a/src/lelantus.cpp +++ b/src/lelantus.cpp @@ -764,8 +764,13 @@ bool CheckLelantusTransaction( if (allowLelantus && !isVerifyDB) { for (const CTxOut &txout : tx.vout) { if (!txout.scriptPubKey.empty() && txout.scriptPubKey.IsLelantusMint()) { - if (!CheckLelantusMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, lelantusTxInfo)) - return false; + try { + if (!CheckLelantusMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, lelantusTxInfo)) + return false; + } + catch (const std::exception &x) { + return state.Error(x.what()); + } } } } @@ -786,10 +791,15 @@ bool CheckLelantusTransaction( } if (!isVerifyDB) { - if (!CheckLelantusJoinSplitTransaction( - tx, state, hashTx, isVerifyDB, nHeight, realHeight, - isCheckWallet, fStatefulSigmaCheck, sigmaTxInfo, lelantusTxInfo)) { - return false; + try { + if (!CheckLelantusJoinSplitTransaction( + tx, state, hashTx, isVerifyDB, nHeight, realHeight, + isCheckWallet, fStatefulSigmaCheck, sigmaTxInfo, lelantusTxInfo)) { + return false; + } + } + catch (const std::exception &x) { + return state.Error(x.what()); } } } diff --git a/src/sigma.cpp b/src/sigma.cpp index 44617859d1..7395e166fd 100644 --- a/src/sigma.cpp +++ b/src/sigma.cpp @@ -455,8 +455,13 @@ bool CheckSigmaTransaction( if (allowSigma) { for (const CTxOut &txout : tx.vout) { if (!txout.scriptPubKey.empty() && txout.scriptPubKey.IsSigmaMint()) { - if (!CheckSigmaMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, sigmaTxInfo)) - return false; + try { + if (!CheckSigmaMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, sigmaTxInfo)) + return false; + } + catch (const std::exception &x) { + return state.Error(x.what()); + } } } } @@ -506,10 +511,15 @@ bool CheckSigmaTransaction( // Check vOut // Only one loop, we checked on the format before entering this case if (!isVerifyDB) { - if (!CheckSigmaSpendTransaction( - tx, denominations, state, hashTx, isVerifyDB, nHeight, realHeight, - isCheckWallet, fStatefulSigmaCheck, sigmaTxInfo)) { - return false; + try { + if (!CheckSigmaSpendTransaction( + tx, denominations, state, hashTx, isVerifyDB, nHeight, realHeight, + isCheckWallet, fStatefulSigmaCheck, sigmaTxInfo)) { + return false; + } + } + catch (const std::exception &x) { + return state.Error(x.what()); } } } diff --git a/src/spark/state.cpp b/src/spark/state.cpp index 2eb6248ccc..3b0707fd78 100644 --- a/src/spark/state.cpp +++ b/src/spark/state.cpp @@ -730,9 +730,14 @@ bool CheckSparkTransaction( } } if (!txOuts.empty()) { - if (!CheckSparkMintTransaction(txOuts, state, hashTx, fStatefulSigmaCheck, sparkTxInfo)) { - LogPrintf("CheckSparkTransaction::Mint verification failed.\n"); - return false; + try { + if (!CheckSparkMintTransaction(txOuts, state, hashTx, fStatefulSigmaCheck, sparkTxInfo)) { + LogPrintf("CheckSparkTransaction::Mint verification failed.\n"); + return false; + } + } + catch (const std::exception &x) { + return state.Error(x.what()); } } else { return state.DoS(100, false, @@ -750,10 +755,15 @@ bool CheckSparkTransaction( } if (!isVerifyDB) { - if (!CheckSparkSpendTransaction( - tx, state, hashTx, isVerifyDB, nHeight, - isCheckWallet, fStatefulSigmaCheck, sparkTxInfo)) { - return false; + try { + if (!CheckSparkSpendTransaction( + tx, state, hashTx, isVerifyDB, nHeight, + isCheckWallet, fStatefulSigmaCheck, sparkTxInfo)) { + return false; + } + } + catch (const std::exception &x) { + return state.Error(x.what()); } } } diff --git a/src/validation.cpp b/src/validation.cpp index ceec9589ec..a874b46aed 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1591,7 +1591,14 @@ bool AcceptToMemoryPoolWithTime(CTxMemPool& pool, CValidationState &state, const { LogPrintf("AcceptToMemoryPool(), transaction: %s\n", tx->GetHash().ToString()); std::vector coins_to_uncache; - bool res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee, coins_to_uncache, isCheckWalletTransaction, markFiroSpendTransactionSerial); + bool res = false; + try { + res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee, coins_to_uncache, isCheckWalletTransaction, markFiroSpendTransactionSerial); + } + catch (const std::exception &x) { + state.Error(x.what()); + res = false; + } if (!res) { BOOST_FOREACH(const COutPoint& hashTx, coins_to_uncache) pcoinsTip->Uncache(hashTx); From 7f91fce56861647506bbf6858a365855627bdcc8 Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Wed, 18 Oct 2023 20:59:46 +0200 Subject: [PATCH 3/6] Catch exception thrown from CheckLelantusJMintTransaction --- src/lelantus.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lelantus.cpp b/src/lelantus.cpp index a4204186cf..3fafa8b722 100644 --- a/src/lelantus.cpp +++ b/src/lelantus.cpp @@ -441,8 +441,13 @@ bool CheckLelantusJoinSplitTransaction( for (const CTxOut &txout : tx.vout) { if (!txout.scriptPubKey.empty() && txout.scriptPubKey.IsLelantusJMint()) { - if (!CheckLelantusJMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, Cout, lelantusTxInfo)) - return false; + try { + if (!CheckLelantusJMintTransaction(txout, state, hashTx, fStatefulSigmaCheck, Cout, lelantusTxInfo)) + return false; + } + catch (const std::exception &x) { + return state.Error(x.what()); + } } else if(txout.scriptPubKey.IsLelantusMint()) { return false; //putting regular mints at JoinSplit transactions is not allowed } else { From 008bc575e070cd2111f6626f938590eba66a0c13 Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Tue, 24 Oct 2023 13:58:15 +0200 Subject: [PATCH 4/6] Bug fix --- src/lelantus.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lelantus.cpp b/src/lelantus.cpp index 3fafa8b722..0caa4ac415 100644 --- a/src/lelantus.cpp +++ b/src/lelantus.cpp @@ -1143,13 +1143,11 @@ void CLelantusState::Containers::RemoveMint(lelantus::PublicCoin const & pubCoin } void CLelantusState::Containers::AddSpend(Scalar const & serial, int coinGroupId) { - if (!mintMetaInfo.count(coinGroupId)) { - throw std::invalid_argument("group id doesn't exist"); + if (mintMetaInfo.count(coinGroupId) > 0) { + usedCoinSerials[serial] = coinGroupId; + spendMetaInfo[coinGroupId] += 1; + CheckSurgeCondition(); } - - usedCoinSerials[serial] = coinGroupId; - spendMetaInfo[coinGroupId] += 1; - CheckSurgeCondition(); } void CLelantusState::Containers::RemoveSpend(Scalar const & serial) { From acdee392ded9b0ddd1088957e4d2568fff815750 Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Tue, 24 Oct 2023 16:17:39 +0200 Subject: [PATCH 5/6] Fixed tests --- src/test/lelantus_state_tests.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/lelantus_state_tests.cpp b/src/test/lelantus_state_tests.cpp index a493271952..a48dc54c0e 100644 --- a/src/test/lelantus_state_tests.cpp +++ b/src/test/lelantus_state_tests.cpp @@ -187,9 +187,6 @@ BOOST_AUTO_TEST_CASE(serial_adding) BOOST_CHECK(!lelantusState->IsUsedCoinSerial(serial2)); BOOST_CHECK(!lelantusState->IsUsedCoinSerialHash(receivedSerial, serialHash2)); - - // add serials to group that doesn't exist, should fail - BOOST_CHECK_THROW(lelantusState->AddSpend(Scalar(1), 100), std::invalid_argument); } BOOST_AUTO_TEST_CASE(mempool) From 6ce118ed9a133a1e5420bb4582df239dd0123b0b Mon Sep 17 00:00:00 2001 From: Peter Shugalev Date: Fri, 17 Nov 2023 08:55:15 +0100 Subject: [PATCH 6/6] Disable failing elysium test --- qa/pull-tester/rpc-tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qa/pull-tester/rpc-tests.py b/qa/pull-tester/rpc-tests.py index 3b9174e6a7..dd5a541074 100755 --- a/qa/pull-tester/rpc-tests.py +++ b/qa/pull-tester/rpc-tests.py @@ -110,7 +110,7 @@ 'lelantus_spend_gettransaction.py', 'elysium_create_denomination.py', 'elysium_property_creation_fee.py', - 'elysium_sendmint.py', +# 'elysium_sendmint.py', 'elysium_sendmint_wallet_encryption.py', 'elysium_sendspend.py', 'elysium_sendspend_wallet_encryption.py',