diff --git a/src/Makefile.am b/src/Makefile.am index 6e346a9b93..cbb18c9099 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -175,6 +175,7 @@ DEFI_CORE_H = \ masternodes/mn_checks.h \ masternodes/mn_rpc.h \ masternodes/res.h \ + masternodes/errors.h \ masternodes/oracles.h \ masternodes/poolpairs.h \ masternodes/proposals.h \ diff --git a/src/masternodes/errors.h b/src/masternodes/errors.h new file mode 100644 index 0000000000..a4a2d958f7 --- /dev/null +++ b/src/masternodes/errors.h @@ -0,0 +1,32 @@ +// Copyright (c) 2023 The DeFi Foundation +// Distributed under the MIT software license, see the accompanying +// file LICENSE or http://www.opensource.org/licenses/mit-license.php. + +#ifndef DEFI_MASTERNODES_ERRORS_H +#define DEFI_MASTERNODES_ERRORS_H + +#include +#include + +class DeFiErrors { +public: + static Res MNInvalid(const std::string &nodeRefString) { + return Res::Err("node %s does not exists", nodeRefString); + } + + static Res MNInvalidAltMsg(const std::string &nodeRefString) { + return Res::Err("masternode %s does not exist", nodeRefString); + } + + static Res MNStateNotEnabled(const std::string &nodeRefString) { + return Res::Err("Masternode %s is not in 'ENABLED' state", nodeRefString); + } + + static Res ICXBTCBelowMinSwap(const CAmount amount, const CAmount minSwap) { + // TODO: Change error in later version to include amount. Retaining old msg for compatibility + return Res::Err("Below minimum swapable amount, must be at least %s BTC", GetDecimaleString(minSwap)); + } +}; + +#endif // DEFI_MASTERNODES_ERRORS_H + diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 44b32412f5..f3ab16e3d8 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -845,7 +846,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { Res operator()(const CResignMasterNodeMessage &obj) const { auto node = mnview.GetMasternode(obj); - Require(node, "node %s does not exists", obj.ToString()); + if (!node) return DeFiErrors::MNInvalid(obj.ToString()); Require(HasCollateralAuth(node->collateralTx.IsNull() ? static_cast(obj) : node->collateralTx)); return mnview.ResignMasternode(*node, obj, tx.GetHash(), height); @@ -861,13 +862,16 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { } auto node = mnview.GetMasternode(obj.mnId); - Require(node, "masternode %s does not exist", obj.mnId.ToString()); + if (!node) + return DeFiErrors::MNInvalidAltMsg(obj.mnId.ToString()); const auto collateralTx = node->collateralTx.IsNull() ? obj.mnId : node->collateralTx; Require(HasCollateralAuth(collateralTx)); auto state = node->GetState(height, mnview); - Require(state == CMasternode::ENABLED, "Masternode %s is not in 'ENABLED' state", obj.mnId.ToString()); + if (state != CMasternode::ENABLED) { + return DeFiErrors::MNStateNotEnabled(obj.mnId.ToString()); + } const auto attributes = mnview.GetAttributes(); assert(attributes); @@ -1556,8 +1560,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { CDataStructureV0 minSwapKey{AttributeTypes::Param, ParamIDs::DFIP2201, DFIPKeys::MinSwap}; auto minSwap = attributes->GetValue(minSwapKey, CAmount{0}); - Require(amount >= minSwap, - "Below minimum swapable amount, must be at least " + GetDecimaleString(minSwap) + " BTC"); + if (amount < minSwap) { + return DeFiErrors::ICXBTCBelowMinSwap(amount, minSwap); + } const auto token = mnview.GetToken(id); Require(token, "Specified token not found");