Skip to content

Commit

Permalink
Add Offer invariant for consumed not to exceed the offer amounts. Fix…
Browse files Browse the repository at this point in the history
… AMMOffer invariant for consumed.
  • Loading branch information
gregtatcam committed Nov 11, 2024
1 parent 6d5fb12 commit 71ec9cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/xrpld/app/paths/detail/AMMOffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ AMMOffer<TIn, TOut>::checkInvariant(
TAmounts<TIn, TOut> const& consumed,
beast::Journal j) const
{
if (isFeatureEnabled(fixAMMv1_3))
return true;

if (consumed.in > amounts_.in || consumed.out > amounts_.out)
{
JLOG(j.error()) << "AMMOffer::checkInvariant failed: consumed "
Expand All @@ -155,6 +152,10 @@ AMMOffer<TIn, TOut>::checkInvariant(
return false;
}

// The invariant check below is moved to InvariantCheck
if (isFeatureEnabled(fixAMMv1_3))
return true;

Number const product = balances_.in * balances_.out;
auto const newBalances = TAmounts<TIn, TOut>{
balances_.in + consumed.in, balances_.out - consumed.out};
Expand Down
19 changes: 18 additions & 1 deletion src/xrpld/app/tx/detail/Offer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_APP_BOOK_OFFER_H_INCLUDED

#include <xrpld/ledger/View.h>
#include <xrpl/basics/Log.h>
#include <xrpl/basics/contract.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/Rules.h>
Expand Down Expand Up @@ -169,8 +170,24 @@ class TOffer : private TOfferBase<TIn, TOut>
* always returns true.
*/
bool
checkInvariant(TAmounts<TIn, TOut> const&, beast::Journal j) const
checkInvariant(TAmounts<TIn, TOut> const& consumed, beast::Journal j) const
{
if (!isFeatureEnabled(fixAMMv1_3))
return true;

if (consumed.in > m_amounts.in || consumed.out > m_amounts.out)
{
// LCOV_EXCL_START
JLOG(j.error())
<< "AMMOffer::checkInvariant failed: consumed "
<< to_string(consumed.in) << " " << to_string(consumed.out)
<< " amounts " << to_string(m_amounts.in) << " "
<< to_string(m_amounts.out);

return false;
// LCOV_EXCL_STOP
}

return true;
}
};
Expand Down

0 comments on commit 71ec9cb

Please sign in to comment.