Skip to content

Commit

Permalink
Check AMM/OfferCreate for MPT amounts
Browse files Browse the repository at this point in the history
* Add type restriction to StepImp and flow()
* Add temMPT_NOT_SUPPORTED_BY_TX
  • Loading branch information
gregtatcam committed Feb 22, 2024
1 parent 078e23f commit 6675244
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/ripple/app/paths/AMMLiquidity.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ripple/app/misc/AMMHelpers.h"
#include "ripple/app/misc/AMMUtils.h"
#include "ripple/app/paths/AMMContext.h"
#include "ripple/app/paths/AMMOffer.h"
#include "ripple/basics/Log.h"
#include "ripple/ledger/ReadView.h"
#include "ripple/ledger/View.h"
Expand All @@ -31,9 +32,6 @@

namespace ripple {

template <typename TIn, typename TOut>
class AMMOffer;

/** AMMLiquidity class provides AMM offers to BookStep class.
* The offers are generated in two ways. If there are multiple
* paths specified to the payment transaction then the offers
Expand Down
6 changes: 5 additions & 1 deletion src/ripple/app/paths/AMMOffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ template <typename TIn, typename TOut>
class AMMLiquidity;
class QualityFunction;

template <typename A>
concept OfferAmount =
!(std::is_same_v<A, STAmount> || std::is_same_v<A, MPTAmount>);

/** Represents synthetic AMM offer in BookStep. AMMOffer mirrors TOffer
* methods for use in generic BookStep methods. AMMOffer amounts
* are changed indirectly in BookStep limiting steps.
*/
template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
class AMMOffer
{
private:
Expand Down
1 change: 0 additions & 1 deletion src/ripple/app/paths/impl/AMMLiquidity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ AMMLiquidity<TIn, TOut>::getOffer(
return std::nullopt;
}

template class AMMLiquidity<STAmount, STAmount>;
template class AMMLiquidity<IOUAmount, IOUAmount>;
template class AMMLiquidity<XRPAmount, IOUAmount>;
template class AMMLiquidity<IOUAmount, XRPAmount>;
Expand Down
19 changes: 9 additions & 10 deletions src/ripple/app/paths/impl/AMMOffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace ripple {

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
AMMOffer<TIn, TOut>::AMMOffer(
AMMLiquidity<TIn, TOut> const& ammLiquidity,
TAmounts<TIn, TOut> const& amounts,
Expand All @@ -37,35 +37,35 @@ AMMOffer<TIn, TOut>::AMMOffer(
{
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
Issue const&
AMMOffer<TIn, TOut>::issueIn() const
{
return ammLiquidity_.issueIn();
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
Issue const&
AMMOffer<TIn, TOut>::issueOut() const
{
return ammLiquidity_.issueOut();
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
AccountID const&
AMMOffer<TIn, TOut>::owner() const
{
return ammLiquidity_.ammAccount();
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
TAmounts<TIn, TOut> const&
AMMOffer<TIn, TOut>::amount() const
{
return amounts_;
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
void
AMMOffer<TIn, TOut>::consume(
ApplyView& view,
Expand All @@ -83,7 +83,7 @@ AMMOffer<TIn, TOut>::consume(
ammLiquidity_.context().setAMMUsed();
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
TAmounts<TIn, TOut>
AMMOffer<TIn, TOut>::limitOut(
TAmounts<TIn, TOut> const& offrAmt,
Expand Down Expand Up @@ -113,7 +113,7 @@ AMMOffer<TIn, TOut>::limitOut(
return {swapAssetOut(*balances_, limit, ammLiquidity_.tradingFee()), limit};
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
TAmounts<TIn, TOut>
AMMOffer<TIn, TOut>::limitIn(
TAmounts<TIn, TOut> const& offrAmt,
Expand All @@ -125,7 +125,7 @@ AMMOffer<TIn, TOut>::limitIn(
return {limit, swapAssetIn(*balances_, limit, ammLiquidity_.tradingFee())};
}

template <typename TIn, typename TOut>
template <OfferAmount TIn, OfferAmount TOut>
QualityFunction
AMMOffer<TIn, TOut>::getQualityFunc() const
{
Expand All @@ -135,7 +135,6 @@ AMMOffer<TIn, TOut>::getQualityFunc() const
*balances_, ammLiquidity_.tradingFee(), QualityFunction::AMMTag{}};
}

template class AMMOffer<STAmount, STAmount>;
template class AMMOffer<IOUAmount, IOUAmount>;
template class AMMOffer<XRPAmount, IOUAmount>;
template class AMMOffer<IOUAmount, XRPAmount>;
Expand Down
6 changes: 5 additions & 1 deletion src/ripple/app/paths/impl/Steps.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,12 @@ toStrands(
AMMContext& ammContext,
beast::Journal j);

template <typename A>
concept StepAsset =
!(std::is_same_v<A, STAmount> || std::is_same_v<A, MPTAmount>);

/// @cond INTERNAL
template <class TIn, class TOut, class TDerived>
template <StepAsset TIn, StepAsset TOut, class TDerived>
struct StepImp : public Step
{
explicit StepImp() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/paths/impl/StrandFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ class ActiveStrands
@return Actual amount in and out from the strands, errors, and payment
sandbox
*/
template <class TInAmt, class TOutAmt>
template <StepAsset TInAmt, StepAsset TOutAmt>
FlowResult<TInAmt, TOutAmt>
flow(
PaymentSandbox const& baseView,
Expand Down
6 changes: 6 additions & 0 deletions src/ripple/app/tx/impl/AMMCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ AMMCreate::preflight(PreflightContext const& ctx)
auto const amount = ctx.tx[sfAmount];
auto const amount2 = ctx.tx[sfAmount2];

if (amount.isMPT() || amount2.isIssue())
{
JLOG(ctx.j.debug()) << "AMM Instance: MPT is not supported.";
return temMPT_NOT_SUPPORTED_BY_TX;
}

if (amount.issue() == amount2.issue())
{
JLOG(ctx.j.debug())
Expand Down
3 changes: 3 additions & 0 deletions src/ripple/app/tx/impl/CreateOffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ CreateOffer::preflight(PreflightContext const& ctx)
STAmount saTakerPays = tx[sfTakerPays];
STAmount saTakerGets = tx[sfTakerGets];

if (saTakerPays.isMPT() || saTakerGets.isMPT())
return temMPT_NOT_SUPPORTED_BY_TX;

if (!isLegalNet(saTakerPays) || !isLegalNet(saTakerGets))
return temBAD_AMOUNT;

Expand Down
1 change: 1 addition & 0 deletions src/ripple/app/tx/impl/Payment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ripple/app/tx/impl/Payment.h>
#include <ripple/basics/Log.h>
#include <ripple/core/Config.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/jss.h>
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/protocol/TER.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ enum TEMcodes : TERUnderlyingType {
temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT,

temEMPTY_DID,

temMPT_NOT_SUPPORTED_BY_TX
};

//------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/ripple/protocol/impl/TER.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ transResults()
MAKE_ERROR(temEMPTY_DID, "Malformed: No DID data provided."),
MAKE_ERROR(temINVALID, "The transaction is ill-formed."),
MAKE_ERROR(temINVALID_FLAG, "The transaction has an invalid flag."),
MAKE_ERROR(temMPT_NOT_SUPPORTED_BY_TX, "MPT is not supported by the transaction."),
MAKE_ERROR(temREDUNDANT, "The transaction is redundant."),
MAKE_ERROR(temRIPPLE_EMPTY, "PathSet with no paths."),
MAKE_ERROR(temUNCERTAIN, "In process of determining result. Never returned."),
Expand Down
3 changes: 3 additions & 0 deletions src/ripple/rpc/impl/TransactionSign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ checkPayment(
{
if (!amountFromJsonNoThrow(sendMax, tx_json[jss::SendMax]))
return RPC::invalid_field_error("tx_json.SendMax");
if (sendMax.isMPT())
return RPC::make_error(
rpcINVALID_PARAMS, "MPT is invalid in SendMax");
}
else
{
Expand Down

0 comments on commit 6675244

Please sign in to comment.