Skip to content

Commit

Permalink
Add DeFiErrors abstraction for error messages (#1758)
Browse files Browse the repository at this point in the history
* Add DeFiErrors abstraction for error messages

* Fix lint
  • Loading branch information
prasannavl authored Feb 16, 2023
1 parent 380dceb commit c686a5b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
32 changes: 32 additions & 0 deletions src/masternodes/errors.h
Original file line number Diff line number Diff line change
@@ -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 <amount.h>
#include <masternodes/res.h>

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

15 changes: 10 additions & 5 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <masternodes/historywriter.h>
#include <masternodes/mn_checks.h>
#include <masternodes/vaulthistory.h>
#include <masternodes/errors.h>

#include <core_io.h>
#include <index/txindex.h>
Expand Down Expand Up @@ -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<uint256>(obj) : node->collateralTx));
return mnview.ResignMasternode(*node, obj, tx.GetHash(), height);
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit c686a5b

Please sign in to comment.