Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixReducedOffersV1: curtail the occurrence of order books that are blocked by reduced offers #4512

Merged
merged 7 commits into from
Jun 23, 2023
1 change: 1 addition & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ if (tests)
src/test/app/PseudoTx_test.cpp
src/test/app/RCLCensorshipDetector_test.cpp
src/test/app/RCLValidations_test.cpp
src/test/app/ReducedOffer_test.cpp
src/test/app/Regression_test.cpp
src/test/app/SHAMapStore_test.cpp
src/test/app/SetAuth_test.cpp
Expand Down
30 changes: 25 additions & 5 deletions src/ripple/app/paths/impl/BookStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,22 @@ limitStepOut(
TOut& ownerGives,
std::uint32_t transferRateIn,
std::uint32_t transferRateOut,
TOut const& limit)
TOut const& limit,
Rules const& rules)
{
if (limit < stpAmt.out)
{
stpAmt.out = limit;
ownerGives = mulRatio(
stpAmt.out, transferRateOut, QUALITY_ONE, /*roundUp*/ false);
ofrAmt = ofrQ.ceil_out(ofrAmt, stpAmt.out);
if (rules.enabled(fixReducedOffersV1))
// It turns out that the ceil_out implementation has some slop in
// it. ceil_out_strict removes that slop. But removing that slop
// affects transaction outcomes, so the change must be made using
// an amendment.
ofrAmt = ofrQ.ceil_out_strict(ofrAmt, stpAmt.out, /*roundUp*/ true);
else
ofrAmt = ofrQ.ceil_out(ofrAmt, stpAmt.out);
stpAmt.in =
mulRatio(ofrAmt.in, transferRateIn, QUALITY_ONE, /*roundUp*/ true);
}
Expand Down Expand Up @@ -577,6 +585,7 @@ BookStep<TIn, TOut, TDerived>::forEachOffer(
sb, afView, book_, sb.parentCloseTime(), counter, j_);

bool const flowCross = afView.rules().enabled(featureFlowCross);
bool const fixReduced = afView.rules().enabled(fixReducedOffersV1);
bool offerAttempted = false;
std::optional<Quality> ofrQ;
while (offers.step())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment for line 590 else if (*ofrQ != offer.quality()) below (doesn't look like github allows commenting on collapsed lines). I'm not sure if this is a problem but judging from the PR comment

This commit adjusts some rounding modes so that the quality of a reduced offer is always at least as good (from the taker's perspective) as the original offer.

I assume it's possible that the reduced offer written back to the ledger has a better quality than the original offer, in which case this offer might block the order book since the check on line 590 would fail for this offer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for you to point out.

The way I understand this code (and @seelabs may correct me on this), forEachOffer() is intended to run through all offers that have the same Quality in an order book. So we get the Quality of the offer at the tip of the order book -- that's how the optional is filled in. If any subsequent offers in the order book have a different Quality, then we return from forEachOffer().

But that doesn't mean we're done. It just means that we've done as much as we can with this Strand for now. If the Quality from this go-round is good enough, then the payment engine will try this stand again and get some more offers out of this path.

One more source of confusion is that the code works with two different Qualitys here. There is the Quality that is encoded in the offer book directory page -- that should be the Quality of the offer as initially placed (not necessarily reflecting the actual Quality of a reduced offer). I'm pretty sure that's the Quality being used in the place you pointed out. There are other places that deal in the actual Quality of an offer.

At any rate, the determination that a particular order book is blocked happens further up the call stack here: https://github.com/XRPLF/rippled/blob/develop/src/ripple/app/paths/impl/StrandFlow.h#L638-L644

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I missed that this better quality offer or the next one after it are going to be picked on the next payment engine iteration if there is more output left.

Expand Down Expand Up @@ -654,7 +663,16 @@ BookStep<TIn, TOut, TDerived>::forEachOffer(
ownerGives = funds;
stpAmt.out = mulRatio(
ownerGives, QUALITY_ONE, ofrOutRate, /*roundUp*/ false);
ofrAmt = ofrQ->ceil_out(ofrAmt, stpAmt.out);

// It turns out we can prevent order book blocking by (strictly)
// rounding down the ceil_out() result. This adjustment changes
// transaction outcomes, so it must be made under an amendment.
if (fixReduced)
ofrAmt = ofrQ->ceil_out_strict(
ofrAmt, stpAmt.out, /* roundUp */ false);
else
ofrAmt = ofrQ->ceil_out(ofrAmt, stpAmt.out);

stpAmt.in =
mulRatio(ofrAmt.in, ofrInRate, QUALITY_ONE, /*roundUp*/ true);
}
Expand Down Expand Up @@ -770,7 +788,8 @@ BookStep<TIn, TOut, TDerived>::revImp(
ownerGivesAdj,
transferRateIn,
transferRateOut,
remainingOut);
remainingOut,
afView.rules());
remainingOut = beast::zero;
savedIns.insert(stpAdjAmt.in);
savedOuts.insert(remainingOut);
Expand Down Expand Up @@ -922,7 +941,8 @@ BookStep<TIn, TOut, TDerived>::fwdImp(
ownerGivesAdjRev,
transferRateIn,
transferRateOut,
remainingOut);
remainingOut,
afView.rules());

if (stpAdjAmtRev.in == remainingIn)
{
Expand Down
18 changes: 16 additions & 2 deletions src/ripple/app/tx/impl/CreateOffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,22 @@ CreateOffer::flowCross(
// what is a good threshold to check?
afterCross.in.clear();

afterCross.out = divRound(
afterCross.in, rate, takerAmount.out.issue(), true);
afterCross.out = [&]() {
// Careful analysis showed that rounding up this
// divRound result could lead to placing a reduced
// offer in the ledger that blocks order books. So
// the fixReducedOffersV1 amendment changes the
// behavior to round down instead.
if (psb.rules().enabled(fixReducedOffersV1))
return divRoundStrict(
afterCross.in,
rate,
takerAmount.out.issue(),
false);

return divRound(
afterCross.in, rate, takerAmount.out.issue(), true);
}();
}
else
{
Expand Down
18 changes: 17 additions & 1 deletion src/ripple/app/tx/impl/OfferStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,33 @@ TOfferStreamBase<TIn, TOut>::shouldRmSmallIncreasedQOffer() const
}

TTakerGets const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
bool const fixReduced = view_.rules().enabled(fixReducedOffersV1);

auto const effectiveAmounts = [&] {
if (offer_.owner() != offer_.issueOut().account &&
ownerFunds < ofrAmts.out)
{
// adjust the amounts by owner funds
// adjust the amounts by owner funds.
//
// It turns out we can prevent order book blocking by rounding down
// the ceil_out() result. This adjustment changes transaction
// results, so it must be made under an amendment.
if (fixReduced)
return offer_.quality().ceil_out_strict(
ofrAmts, ownerFunds, /* roundUp */ false);

return offer_.quality().ceil_out(ofrAmts, ownerFunds);
}
return ofrAmts;
}();

// If either the effective in or out are zero then remove the offer.
// This can happen with fixReducedOffersV1 since it rounds down.
if (fixReduced &&
(effectiveAmounts.in.signum() <= 0 ||
effectiveAmounts.out.signum() <= 0))
return true;

if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
return false;

Expand Down
3 changes: 2 additions & 1 deletion src/ripple/protocol/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace detail {
// Feature.cpp. Because it's only used to reserve storage, and determine how
// large to make the FeatureBitset, it MAY be larger. It MUST NOT be less than
// the actual number of amendments. A LogicError on startup will verify this.
static constexpr std::size_t numFeatures = 58;
static constexpr std::size_t numFeatures = 59;

/** Amendments that this server supports and the default voting behavior.
Whether they are enabled depends on the Rules defined in the validated
Expand Down Expand Up @@ -345,6 +345,7 @@ extern uint256 const featureXRPFees;
extern uint256 const fixUniversalNumber;
extern uint256 const fixNonFungibleTokensV1_2;
extern uint256 const fixNFTokenRemint;
extern uint256 const fixReducedOffersV1;

} // namespace ripple

Expand Down
23 changes: 23 additions & 0 deletions src/ripple/protocol/Quality.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ class Quality
toAmount<In>(stRes.in), toAmount<Out>(stRes.out));
}

Amounts
ceil_out_strict(Amounts const& amount, STAmount const& limit, bool roundUp)
const;

template <class In, class Out>
TAmounts<In, Out>
ceil_out_strict(
TAmounts<In, Out> const& amount,
Out const& limit,
bool roundUp) const
{
if (amount.out <= limit)
return amount;

// Use the existing STAmount implementation for now, but consider
// replacing with code specific to IOUAMount and XRPAmount
Amounts stAmt(toSTAmount(amount.in), toSTAmount(amount.out));
STAmount stLim(toSTAmount(limit));
auto const stRes = ceil_out_strict(stAmt, stLim, roundUp);
return TAmounts<In, Out>(
toAmount<In>(stRes.in), toAmount<Out>(stRes.out));
}

/** Returns `true` if lhs is lower quality than `rhs`.
Lower quality means the taker receives a worse deal.
Higher quality is better for the taker.
Expand Down
19 changes: 18 additions & 1 deletion src/ripple/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,38 @@ divide(STAmount const& v1, STAmount const& v2, Issue const& issue);
STAmount
multiply(STAmount const& v1, STAmount const& v2, Issue const& issue);

// multiply, or divide rounding result in specified direction
// multiply rounding result in specified direction
STAmount
mulRound(
STAmount const& v1,
STAmount const& v2,
Issue const& issue,
bool roundUp);

// multiply following the rounding directions more precisely.
STAmount
mulRoundStrict(
STAmount const& v1,
STAmount const& v2,
Issue const& issue,
bool roundUp);

// divide rounding result in specified direction
STAmount
divRound(
STAmount const& v1,
STAmount const& v2,
Issue const& issue,
bool roundUp);

// divide following the rounding directions more precisely.
STAmount
divRoundStrict(
STAmount const& v1,
STAmount const& v2,
Issue const& issue,
bool roundUp);

// Someone is offering X for Y, what is the rate?
// Rate: smaller is better, the taker wants the most out: in/out
// VFALCO TODO Return a Quality object
Expand Down
1 change: 1 addition & 0 deletions src/ripple/protocol/impl/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ REGISTER_FEATURE(XRPFees, Supported::yes, VoteBehavior::De
REGISTER_FIX (fixUniversalNumber, Supported::yes, VoteBehavior::DefaultNo);
REGISTER_FIX (fixNonFungibleTokensV1_2, Supported::yes, VoteBehavior::DefaultNo);
REGISTER_FIX (fixNFTokenRemint, Supported::yes, VoteBehavior::DefaultNo);
REGISTER_FIX (fixReducedOffersV1, Supported::yes, VoteBehavior::DefaultNo);

// The following amendments are obsolete, but must remain supported
// because they could potentially get enabled.
Expand Down
29 changes: 26 additions & 3 deletions src/ripple/protocol/impl/Quality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,20 @@ Quality::ceil_in(Amounts const& amount, STAmount const& limit) const
return amount;
}

Amounts
Quality::ceil_out(Amounts const& amount, STAmount const& limit) const
template <STAmount (
*MulRoundFunc)(STAmount const&, STAmount const&, Issue const&, bool)>
static Amounts
ceil_out_impl(
Amounts const& amount,
STAmount const& limit,
bool roundUp,
Quality const& quality)
{
if (amount.out > limit)
{
Amounts result(mulRound(limit, rate(), amount.in.issue(), true), limit);
Amounts result(
MulRoundFunc(limit, quality.rate(), amount.in.issue(), roundUp),
limit);
// Clamp in
if (result.in > amount.in)
result.in = amount.in;
Expand All @@ -97,6 +105,21 @@ Quality::ceil_out(Amounts const& amount, STAmount const& limit) const
return amount;
}

Amounts
Quality::ceil_out(Amounts const& amount, STAmount const& limit) const
{
return ceil_out_impl<mulRound>(amount, limit, /* roundUp */ true, *this);
}

Amounts
Quality::ceil_out_strict(
Amounts const& amount,
STAmount const& limit,
bool roundUp) const
{
return ceil_out_impl<mulRoundStrict>(amount, limit, roundUp, *this);
}

Quality
composed_quality(Quality const& lhs, Quality const& rhs)
{
Expand Down
Loading