Skip to content

Commit

Permalink
Spark runaway exceptions (#1344)
Browse files Browse the repository at this point in the history
* Avoid catch(...) when possible

* Add wrappers around major sigma/lelantus/spark calls to catch exceptions

* Catch exception thrown from CheckLelantusJMintTransaction

* Bug fix

* Fixed tests

* Disable failing elysium test
  • Loading branch information
psolstice authored and firstcryptoman committed Jan 13, 2024
1 parent 2edf00f commit 1204a72
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 104 deletions.
6 changes: 3 additions & 3 deletions src/batchproof_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bip47/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool CAccountReceiver::acceptMaskedPayload(std::vector<unsigned char> const & ma
std::unique_ptr<lelantus::JoinSplit> jsplit;
try {
jsplit = lelantus::ParseLelantusJoinSplit(tx);
}catch (...) {
}catch (const std::exception &) {
return false;
}
if (!jsplit)
Expand Down
2 changes: 1 addition & 1 deletion src/bip47/bip47utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class CMainParams : public CChainParams {
GroupElement coin;
try {
coin.deserialize(ParseHex(str).data());
} catch (...) {
} catch (const std::exception &) {
continue;
}
consensus.lelantusBlacklist.insert(coin);
Expand All @@ -436,7 +436,7 @@ class CMainParams : public CChainParams {
GroupElement coin;
try {
coin.deserialize(ParseHex(str).data());
} catch (...) {
} catch (const std::exception &) {
continue;
}
consensus.sigmaBlacklist.insert(coin);
Expand Down Expand Up @@ -735,7 +735,7 @@ class CTestNetParams : public CChainParams {
GroupElement coin;
try {
coin.deserialize(ParseHex(str).data());
} catch (...) {
} catch (const std::exception &) {
continue;
}
consensus.lelantusBlacklist.insert(coin);
Expand Down
4 changes: 2 additions & 2 deletions src/hdmint/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ bool CHDMintTracker::IsMempoolSpendOurs(const std::set<uint256>& setMempool, con
uint32_t pubcoinId;
try {
std::tie(spend, pubcoinId) = sigma::ParseSigmaSpend(txin);
} catch (...) {
} catch (const std::exception &) {
return false;
}

Expand All @@ -560,7 +560,7 @@ bool CHDMintTracker::IsMempoolSpendOurs(const std::set<uint256>& setMempool, con
std::unique_ptr<lelantus::JoinSplit> joinsplit;
try {
joinsplit = lelantus::ParseLelantusJoinSplit(tx);
} catch (...) {
} catch (const std::exception &) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hdmint/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
51 changes: 32 additions & 19 deletions src/lelantus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ bool CheckLelantusJoinSplitTransaction(
REJECT_MALFORMED,
"CheckLelantusJoinSplitTransaction: invalid joinsplit transaction");
}
catch (...) {
catch (const std::exception &) {
return state.DoS(100,
false,
REJECT_MALFORMED,
Expand Down Expand Up @@ -444,8 +444,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 {
Expand Down Expand Up @@ -774,8 +779,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());
}
}
}
}
Expand All @@ -796,10 +806,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());
}
}
}
Expand All @@ -822,7 +837,7 @@ void RemoveLelantusJoinSplitReferencingBlock(CTxMemPool& pool, CBlockIndex* bloc
try {
joinsplit = ParseLelantusJoinSplit(tx);
}
catch (...) {
catch (const std::exception &) {
txn_to_remove.push_back(tx);
break;
}
Expand Down Expand Up @@ -861,7 +876,7 @@ std::vector<Scalar> GetLelantusJoinSplitSerialNumbers(const CTransaction &tx, co
try {
return ParseLelantusJoinSplit(tx)->getCoinSerialNumbers();
}
catch (...) {
catch (const std::exception &) {
return std::vector<Scalar>();
}
}
Expand All @@ -873,7 +888,7 @@ std::vector<uint32_t> GetLelantusJoinSplitIds(const CTransaction &tx, const CTxI
try {
return ParseLelantusJoinSplit(tx)->getCoinGroupIds();
}
catch (...) {
catch (const std::exception &) {
return std::vector<uint32_t>();
}
}
Expand Down Expand Up @@ -1015,7 +1030,7 @@ bool GetOutPointFromBlock(COutPoint& outPoint, const GroupElement &pubCoinValue,
try {
ParseLelantusMintScript(txout.scriptPubKey, txPubCoinValue);
}
catch (...) {
catch (const std::exception &) {
continue;
}
if(pubCoinValue==txPubCoinValue){
Expand Down Expand Up @@ -1140,13 +1155,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) {
Expand Down
2 changes: 1 addition & 1 deletion src/liblelantus/lelantus_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/libspark/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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");
}

Expand All @@ -151,7 +151,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");
}

Expand Down
4 changes: 2 additions & 2 deletions src/libspark/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Scalar Hash::finalize_scalar() {
EVP_MD_CTX_free(state_finalize);

return candidate;
} catch (...) {
} catch (const std::exception &) {
counter++;
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ GroupElement Hash::finalize_group() {
EVP_MD_CTX_free(state_finalize);

return candidate;
} catch (...) {
} catch (const std::exception &) {
counter++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/transcript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ GroupElement SparkUtils::hash_generator(const std::string label) {
EVP_MD_CTX_free(state_finalize);

return candidate;
} catch (...) {
} catch (const std::exception &) {
counter++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand All @@ -143,7 +143,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
try {
sparkSpend = std::make_unique<spark::SpendTransaction>(spark::ParseSparkSpend(tx));
}
catch (...) {
catch (const std::exception &) {
continue;
}
in.push_back(Pair("nFees", ValueFromAmount(sparkSpend->getFee())));
Expand Down
24 changes: 17 additions & 7 deletions src/sigma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,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());
}
}
}
}
Expand Down Expand Up @@ -508,10 +513,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());
}
}
}
Expand Down Expand Up @@ -667,7 +677,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){
Expand Down
10 changes: 5 additions & 5 deletions src/spark/sparkwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -437,7 +437,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;
Expand Down Expand Up @@ -501,7 +501,7 @@ void CSparkWallet::UpdateSpendStateFromBlock(const CBlock& block) {
uint256 lTagHash = primitives::GetLTagHash(txLTag);
UpdateSpendState(txLTag, lTagHash, txHash);
}
} catch (...) {
} catch (const std::exception &) {
}
}
}
Expand All @@ -511,7 +511,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;
}

Expand Down
Loading

0 comments on commit 1204a72

Please sign in to comment.