Skip to content

Commit

Permalink
Refactor Clawback plus other minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Oct 10, 2024
1 parent eac438c commit 9d51389
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
2 changes: 1 addition & 1 deletion include/xrpl/protocol/Rate.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ STAmount
divideRound(
STAmount const& amount,
Rate const& rate,
Asset const& issue,
Asset const& asset,
bool roundUp);

namespace nft {
Expand Down
135 changes: 69 additions & 66 deletions src/xrpld/app/tx/detail/Clawback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ template <>
NotTEC
preflightHelper<Issue>(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureClawback))
return temDISABLED;

if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;

if (ctx.tx.getFlags() & tfClawbackMask)
return temINVALID_FLAG;

if (ctx.tx.isFieldPresent(sfMPTokenHolder))
return temMALFORMED;

Expand All @@ -57,31 +48,22 @@ preflightHelper<Issue>(PreflightContext const& ctx)
if (issuer == holder || isXRP(clawAmount) || clawAmount <= beast::zero)
return temBAD_AMOUNT;

return preflight2(ctx);
return tesSUCCESS;
}

template <>
NotTEC
preflightHelper<MPTIssue>(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureClawback))
if (!ctx.rules.enabled(featureMPTokensV1))
return temDISABLED;

auto const mptHolder = ctx.tx[~sfMPTokenHolder];
auto const clawAmount = ctx.tx[sfAmount];

if (!ctx.rules.enabled(featureMPTokensV1))
return temDISABLED;

if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;

if (!mptHolder)
return temMALFORMED;

if (ctx.tx.getFlags() & tfClawbackMask)
return temINVALID_FLAG;

// issuer is the same as holder
if (ctx.tx[sfAccount] == *mptHolder)
return temMALFORMED;
Expand All @@ -90,30 +72,49 @@ preflightHelper<MPTIssue>(PreflightContext const& ctx)
clawAmount <= beast::zero)
return temBAD_AMOUNT;

return tesSUCCESS;
}

NotTEC
Clawback::preflight(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureClawback))
return temDISABLED;

if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;

if (ctx.tx.getFlags() & tfClawbackMask)
return temINVALID_FLAG;

if (auto const ret = std::visit(
[&]<typename T>(T const&) { return preflightHelper<T>(ctx); },
ctx.tx[sfAmount].asset().value());
!isTesSuccess(ret))
return ret;

return preflight2(ctx);
}

template <ValidIssueType T>
static TER
preclaimHelper(PreclaimContext const& ctx);
preclaimHelper(
PreclaimContext const& ctx,
SLE const& sleIssuer,
AccountID const& issuer,
AccountID const& holder,
STAmount const& clawAmount);

template <>
TER
preclaimHelper<Issue>(PreclaimContext const& ctx)
preclaimHelper<Issue>(
PreclaimContext const& ctx,
SLE const& sleIssuer,
AccountID const& issuer,
AccountID const& holder,
STAmount const& clawAmount)
{
AccountID const issuer = ctx.tx[sfAccount];
STAmount const clawAmount = ctx.tx[sfAmount];
AccountID const& holder = clawAmount.getIssuer();

auto const sleIssuer = ctx.view.read(keylet::account(issuer));
auto const sleHolder = ctx.view.read(keylet::account(holder));
if (!sleIssuer || !sleHolder)
return terNO_ACCOUNT;

if (sleHolder->isFieldPresent(sfAMMID))
return tecAMM_ACCOUNT;

std::uint32_t const issuerFlagsIn = sleIssuer->getFieldU32(sfFlags);
std::uint32_t const issuerFlagsIn = sleIssuer.getFieldU32(sfFlags);

// If AllowTrustLineClawback is not set or NoFreeze is set, return no
// permission
Expand Down Expand Up @@ -159,20 +160,13 @@ preclaimHelper<Issue>(PreclaimContext const& ctx)

template <>
TER
preclaimHelper<MPTIssue>(PreclaimContext const& ctx)
preclaimHelper<MPTIssue>(
PreclaimContext const& ctx,
SLE const& sleIssuer,
AccountID const& issuer,
AccountID const& holder,
STAmount const& clawAmount)
{
AccountID const issuer = ctx.tx[sfAccount];
auto const clawAmount = ctx.tx[sfAmount];
AccountID const& holder = ctx.tx[sfMPTokenHolder];

auto const sleIssuer = ctx.view.read(keylet::account(issuer));
auto const sleHolder = ctx.view.read(keylet::account(holder));
if (!sleIssuer || !sleHolder)
return terNO_ACCOUNT;

if (sleHolder->isFieldPresent(sfAMMID))
return tecAMM_ACCOUNT;

auto const issuanceKey =
keylet::mptIssuance(clawAmount.get<MPTIssue>().getMptID());
auto const sleIssuance = ctx.view.read(issuanceKey);
Expand Down Expand Up @@ -200,6 +194,31 @@ preclaimHelper<MPTIssue>(PreclaimContext const& ctx)
return tesSUCCESS;
}

TER
Clawback::preclaim(PreclaimContext const& ctx)
{
AccountID const issuer = ctx.tx[sfAccount];
auto const clawAmount = ctx.tx[sfAmount];
AccountID const holder = clawAmount.holds<Issue>()
? clawAmount.getIssuer()
: ctx.tx[sfMPTokenHolder];

auto const sleIssuer = ctx.view.read(keylet::account(issuer));
auto const sleHolder = ctx.view.read(keylet::account(holder));
if (!sleIssuer || !sleHolder)
return terNO_ACCOUNT;

if (sleHolder->isFieldPresent(sfAMMID))
return tecAMM_ACCOUNT;

return std::visit(
[&]<typename T>(T const&) {
return preclaimHelper<T>(
ctx, *sleIssuer, issuer, holder, clawAmount);
},
ctx.tx[sfAmount].asset().value());
}

template <ValidIssueType T>
static TER
applyHelper(ApplyContext& ctx);
Expand All @@ -208,7 +227,7 @@ template <>
TER
applyHelper<Issue>(ApplyContext& ctx)
{
AccountID const& issuer = ctx.tx[sfAccount];
AccountID const issuer = ctx.tx[sfAccount];
STAmount clawAmount = ctx.tx[sfAmount];
AccountID const holder = clawAmount.getIssuer(); // cannot be reference

Expand Down Expand Up @@ -239,7 +258,7 @@ template <>
TER
applyHelper<MPTIssue>(ApplyContext& ctx)
{
AccountID const& issuer = ctx.tx[sfAccount];
AccountID const issuer = ctx.tx[sfAccount];
auto clawAmount = ctx.tx[sfAmount];
AccountID const holder = ctx.tx[sfMPTokenHolder];

Expand All @@ -260,22 +279,6 @@ applyHelper<MPTIssue>(ApplyContext& ctx)
ctx.journal);
}

NotTEC
Clawback::preflight(PreflightContext const& ctx)
{
return std::visit(
[&]<typename T>(T const&) { return preflightHelper<T>(ctx); },
ctx.tx[sfAmount].asset().value());
}

TER
Clawback::preclaim(PreclaimContext const& ctx)
{
return std::visit(
[&]<typename T>(T const&) { return preclaimHelper<T>(ctx); },
ctx.tx[sfAmount].asset().value());
}

TER
Clawback::doApply()
{
Expand Down
3 changes: 1 addition & 2 deletions src/xrpld/ledger/detail/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1385,8 +1385,7 @@ rippleSendMPT(

// Sending 3rd party MPTs: transit.
if (auto const sle =
view.read(keylet::mptIssuance(saAmount.get<MPTIssue>().getMptID()));
sle != nullptr)
view.read(keylet::mptIssuance(saAmount.get<MPTIssue>().getMptID())))
{
saActual = (waiveFee == WaiveTransferFee::Yes)
? saAmount
Expand Down

0 comments on commit 9d51389

Please sign in to comment.