Skip to content

Commit

Permalink
Catching trown exeptions, in case invalid group element
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Feb 26, 2022
1 parent bb58a10 commit c35669f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 39 deletions.
7 changes: 6 additions & 1 deletion src/bip47/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ bool CAccountReceiver::acceptMaskedPayload(std::vector<unsigned char> const & ma

bool CAccountReceiver::acceptMaskedPayload(std::vector<unsigned char> const & maskedPayload, CTransaction const & tx)
{
std::unique_ptr<lelantus::JoinSplit> jsplit = lelantus::ParseLelantusJoinSplit(tx);
std::unique_ptr<lelantus::JoinSplit> jsplit;
try {
jsplit = lelantus::ParseLelantusJoinSplit(tx);
}catch (...) {
return false;
}
if (!jsplit)
return false;
std::unique_ptr<CPaymentCode> pcode;
Expand Down
8 changes: 2 additions & 6 deletions src/hdmint/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,7 @@ bool CHDMintTracker::IsMempoolSpendOurs(const std::set<uint256>& setMempool, con
uint32_t pubcoinId;
try {
std::tie(spend, pubcoinId) = sigma::ParseSigmaSpend(txin);
} catch (CBadTxIn &) {
return false;
} catch (std::ios_base::failure &) {
} catch (...) {
return false;
}

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

Expand Down
31 changes: 15 additions & 16 deletions src/lelantus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ bool CheckLelantusJoinSplitTransaction(
REJECT_MALFORMED,
"CheckLelantusJoinSplitTransaction: invalid joinsplit transaction");
}
catch (...) {
return state.DoS(100,
false,
REJECT_MALFORMED,
"CheckLelantusJoinSplitTransaction: failed to deserialize joinsplit");
}

int jSplitVersion = joinsplit->getVersion();

Expand Down Expand Up @@ -698,18 +704,6 @@ bool CheckLelantusTransaction(
{
Consensus::Params const & consensus = ::Params().GetConsensus();


if(tx.IsLelantusJoinSplit()) {
CAmount nFees;
try {
nFees = lelantus::ParseLelantusJoinSplit(tx)->getFee();
}
catch (CBadTxIn&) {
return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit");
}

}

int realHeight = nHeight;

if (realHeight == INT_MAX) {
Expand Down Expand Up @@ -779,7 +773,7 @@ void RemoveLelantusJoinSplitReferencingBlock(CTxMemPool& pool, CBlockIndex* bloc
try {
joinsplit = ParseLelantusJoinSplit(tx);
}
catch (const std::ios_base::failure &) {
catch (...) {
txn_to_remove.push_back(tx);
break;
}
Expand Down Expand Up @@ -818,7 +812,7 @@ std::vector<Scalar> GetLelantusJoinSplitSerialNumbers(const CTransaction &tx, co
try {
return ParseLelantusJoinSplit(tx)->getCoinSerialNumbers();
}
catch (const std::ios_base::failure &) {
catch (...) {
return std::vector<Scalar>();
}
}
Expand All @@ -830,7 +824,7 @@ std::vector<uint32_t> GetLelantusJoinSplitIds(const CTransaction &tx, const CTxI
try {
return ParseLelantusJoinSplit(tx)->getCoinGroupIds();
}
catch (const std::ios_base::failure &) {
catch (...) {
return std::vector<uint32_t>();
}
}
Expand Down Expand Up @@ -961,7 +955,12 @@ bool GetOutPointFromBlock(COutPoint& outPoint, const GroupElement &pubCoinValue,
uint32_t nIndex = 0;
for (const CTxOut &txout: tx->vout) {
if (txout.scriptPubKey.IsLelantusMint() || txout.scriptPubKey.IsLelantusJMint()) {
ParseLelantusMintScript(txout.scriptPubKey, txPubCoinValue);
try {
ParseLelantusMintScript(txout.scriptPubKey, txPubCoinValue);
}
catch (...) {
continue;
}
if(pubCoinValue==txPubCoinValue){
outPoint = COutPoint(tx->GetHash(), nIndex);
return true;
Expand Down
8 changes: 7 additions & 1 deletion src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,13 @@ CTransaction AdaptJsplitTx(CTransaction const & tx)
static size_t const jsplitSerialSize = 32;

CTransaction result{tx};
std::unique_ptr<lelantus::JoinSplit> jsplit = lelantus::ParseLelantusJoinSplit(tx);
std::unique_ptr <lelantus::JoinSplit> jsplit;
try {
jsplit = lelantus::ParseLelantusJoinSplit(tx);
}
catch (...) {
return result;
}
const_cast<std::vector<CTxIn>*>(&result.vin)->clear(); //This const_cast was done intentionally as the current design allows for this way only
for (Scalar const & serial : jsplit->getCoinSerialNumbers()) {
CTxIn newin;
Expand Down
7 changes: 6 additions & 1 deletion src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();

if (wtx.tx->IsLelantusJoinSplit() && wtx.tx->vin.size() > 0) {
nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee();
try {
nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee();
}
catch (...) {
//do nothing
}
}
if (nTxFee > 0)
strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nTxFee) + "<br>";
Expand Down
6 changes: 5 additions & 1 deletion src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
if (wtx.tx->IsZerocoinSpend() || isAllSigmaSpendFromMe || isAllJoinSplitFromMe) {
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
if (isAllJoinSplitFromMe && wtx.tx->vin.size() > 0) {
nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee();
try {
nTxFee = lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee();
} catch (...) {
// do nothing
}
}

bool first = true;
Expand Down
8 changes: 7 additions & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
} else if (txin.IsLelantusJoinSplit()) {
in.push_back("joinsplit");
fillStdFields(in, txin);
std::unique_ptr<lelantus::JoinSplit> jsplit = lelantus::ParseLelantusJoinSplit(tx);
std::unique_ptr <lelantus::JoinSplit> jsplit;
try {
jsplit = lelantus::ParseLelantusJoinSplit(tx);
}
catch (...) {
continue;
}
in.push_back(Pair("nFees", ValueFromAmount(jsplit->getFee())));
UniValue serials(UniValue::VARR);
for (Scalar const & serial : jsplit->getCoinSerialNumbers()) {
Expand Down
2 changes: 1 addition & 1 deletion src/secp256k1/src/cpp/GroupElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ const unsigned char* GroupElement::deserialize(const unsigned char* buffer) {
secp256k1_gej_set_ge(reinterpret_cast<secp256k1_gej *>(g_), &result);

if (!secp256k1_ge_is_valid_var(&result) && !result.infinity) {
throw std::runtime_error("GroupElement: deserialize failed");
throw std::invalid_argument("GroupElement: deserialize failed");
}
return buffer + memoryRequired();
}
Expand Down
21 changes: 19 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
catch (CBadTxIn&) {
return state.Invalid(false, REJECT_CONFLICT, "txn-invalid-lelantus-joinsplit");
}
catch (...) {
return state.Invalid(false, REJECT_CONFLICT, "failed to deserialize joinsplit");
}

const std::vector<uint32_t> &ids = joinsplit->getCoinGroupIds();
const std::vector<Scalar>& serials = joinsplit->getCoinSerialNumbers();
Expand Down Expand Up @@ -1080,6 +1083,9 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
catch (CBadTxIn&) {
return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit");
}
catch (...) {
return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit");
}
}
// nModifiedFees includes any fee deltas from PrioritiseTransaction
CAmount nModifiedFees = nFees;
Expand Down Expand Up @@ -1965,6 +1971,9 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
catch (CBadTxIn&) {
return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit");
}
catch (...) {
return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit");
}
}
if (nTxFee < 0)
return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-negative");
Expand Down Expand Up @@ -2389,7 +2398,12 @@ static DisconnectResult DisconnectBlock(const CBlock& block, CValidationState& s
if(tx.IsSigmaSpend())
nFees += sigma::GetSigmaSpendInput(tx) - tx.GetValueOut();
else if (tx.IsLelantusJoinSplit()) {
nFees += lelantus::ParseLelantusJoinSplit(tx)->getFee();
try {
nFees += lelantus::ParseLelantusJoinSplit(tx)->getFee();
}
catch (...) {
// do nothing
}
}

dbIndexHelper.DisconnectTransactionInputs(tx, pindex->nHeight, i, view);
Expand Down Expand Up @@ -2777,6 +2791,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
catch (CBadTxIn&) {
return state.DoS(0, false, REJECT_INVALID, "unable to parse joinsplit");
}
catch (...) {
return state.DoS(0, false, REJECT_INVALID, "failed to deserialize joinsplit");
}
}

// Check transaction against signa/lelantus state
Expand Down Expand Up @@ -3290,7 +3307,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
try {
joinsplit = lelantus::ParseLelantusJoinSplit(*tx);
}
catch (CBadTxIn &) {
catch (...) {
continue;
}

Expand Down
12 changes: 9 additions & 3 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,8 +2069,14 @@ UniValue gettransaction(const JSONRPCRequest& request)
CAmount nDebit = wtx.GetDebit(filter);
CAmount nNet = nCredit - nDebit;
CAmount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit : 0);
if (wtx.tx->vin[0].IsLelantusJoinSplit())
nFee = (0 - lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee());
if (wtx.tx->vin[0].IsLelantusJoinSplit()) {
try {
nFee = (0 - lelantus::ParseLelantusJoinSplit(*wtx.tx)->getFee());
}
catch (...) {
// do nothing
}
}

entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));

Expand Down Expand Up @@ -4099,7 +4105,7 @@ UniValue listlelantusjoinsplits(const JSONRPCRequest& request) {
std::unique_ptr<lelantus::JoinSplit> joinsplit;
try {
joinsplit = lelantus::ParseLelantusJoinSplit(*pwtx->tx);
} catch (std::invalid_argument&) {
} catch (...) {
continue;
}

Expand Down
25 changes: 19 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,11 @@ bool CWallet::IsSpent(const uint256 &hash, unsigned int n) const
return meta.isUsed;
} else if (zwallet && (script.IsLelantusMint() || script.IsLelantusJMint())) {
secp_primitives::GroupElement pubcoin;
lelantus::ParseLelantusMintScript(script, pubcoin);
try {
lelantus::ParseLelantusMintScript(script, pubcoin);
} catch (std::invalid_argument &) {
return false;
}
uint256 hashPubcoin = primitives::GetPubCoinValueHash(pubcoin);
CLelantusMintMeta meta;
if(!zwallet->GetTracker().GetLelantusMetaFromPubcoin(hashPubcoin, meta)){
Expand Down Expand Up @@ -1425,7 +1429,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
try {
joinsplit = lelantus::ParseLelantusJoinSplit(*wtx.tx);
}
catch (CBadTxIn&) {
catch (...) {
continue;
}

Expand Down Expand Up @@ -1590,7 +1594,7 @@ isminetype CWallet::IsMine(const CTxIn &txin, const CTransaction& tx) const
try {
joinsplit = lelantus::ParseLelantusJoinSplit(tx);
}
catch (CBadTxIn&) {
catch (...) {
return ISMINE_NO;
}

Expand Down Expand Up @@ -1651,7 +1655,7 @@ CAmount CWallet::GetDebit(const CTxIn &txin, const CTransaction& tx, const ismin
try {
joinsplit = lelantus::ParseLelantusJoinSplit(tx);
}
catch (CBadTxIn&) {
catch (...) {
goto end;
}

Expand Down Expand Up @@ -2079,7 +2083,12 @@ void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,
nFee = nDebit - nValueOut;
}
else
nFee = lelantus::ParseLelantusJoinSplit(*tx)->getFee();
try {
nFee = lelantus::ParseLelantusJoinSplit(*tx)->getFee();
}
catch (...) {
// do nothing
}
}

// Sent/received.
Expand Down Expand Up @@ -3690,7 +3699,11 @@ void CWallet::ListAvailableLelantusMintCoins(std::vector<COutput> &vCoins, bool
if (pcoin->tx->vout[i].scriptPubKey.IsLelantusMint() || pcoin->tx->vout[i].scriptPubKey.IsLelantusJMint()) {
CTxOut txout = pcoin->tx->vout[i];
secp_primitives::GroupElement pubCoin;
lelantus::ParseLelantusMintScript(txout.scriptPubKey, pubCoin);
try {
lelantus::ParseLelantusMintScript(txout.scriptPubKey, pubCoin);
} catch (std::invalid_argument &) {
continue;
}
LogPrintf("Pubcoin=%s\n", pubCoin.tostring());
// CHECKING PROCESS
BOOST_FOREACH(const CLelantusEntry& ownCoinItem, listOwnCoins) {
Expand Down

0 comments on commit c35669f

Please sign in to comment.