Skip to content

Commit

Permalink
clang
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardAH committed Jan 26, 2025
1 parent 41b3104 commit d8ec320
Showing 1 changed file with 127 additions and 126 deletions.
253 changes: 127 additions & 126 deletions src/ripple/app/tx/impl/Transactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2039,101 +2039,100 @@ Transactor::operator()()
if (ctx_.size() > oversizeMetaDataCap)
result = tecOVERSIZE;
}

if (view().rules().enabled(featureServiceFee) &&
applied && ctx_.tx.isFieldPresent(sfServiceFee))
do
{
// Service fee is processed on a best-effort basis without affecting
// tx application. The reason is that the client completely controls
// the service fee that it submits with the user's txn, and therefore
// is already completely aware of the user's capacity to pay the fee
// and therefore enforcement logic is unnecessary chain-side.

STObject const& obj = const_cast<ripple::STTx&>(ctx_.tx)
.getField(sfServiceFee)
.downcast<STObject>();

// This should be enforced by template but doesn't hurt to
// defensively check it here.
if (!obj.isFieldPresent(sfDestination) ||
!obj.isFieldPresent(sfAmount) ||
obj.getCount() != 2)

if (view().rules().enabled(featureServiceFee) && applied &&
ctx_.tx.isFieldPresent(sfServiceFee))
do
{
JLOG(j_.warn())
<< "service fee not applied - malformed inner object.";
break;
}
// Service fee is processed on a best-effort basis without affecting
// tx application. The reason is that the client completely controls
// the service fee that it submits with the user's txn, and
// therefore is already completely aware of the user's capacity to
// pay the fee and therefore enforcement logic is unnecessary
// chain-side.

STObject const& obj = const_cast<ripple::STTx&>(ctx_.tx)
.getField(sfServiceFee)
.downcast<STObject>();

// This should be enforced by template but doesn't hurt to
// defensively check it here.
if (!obj.isFieldPresent(sfDestination) ||
!obj.isFieldPresent(sfAmount) || obj.getCount() != 2)
{
JLOG(j_.warn())
<< "service fee not applied - malformed inner object.";
break;
}

auto const src = ctx_.tx.getAccountID(sfAccount);
auto const dst = obj.getAccountID(sfDestination);

auto const amt = obj.getFieldAmount(sfAmount);
auto const src = ctx_.tx.getAccountID(sfAccount);
auto const dst = obj.getAccountID(sfDestination);

// sanity check fields
if (src == dst)
{
JLOG(j_.trace())
<< "skipping self service-fee on " << src << ".";
break;
}
auto const amt = obj.getFieldAmount(sfAmount);

if (amt <= beast::zero)
{
JLOG(j_.trace())
<< "skipping non-positive service-fee from " << src << ".";
break;
}
// sanity check fields
if (src == dst)
{
JLOG(j_.trace())
<< "skipping self service-fee on " << src << ".";
break;
}

// check if the source exists
auto const& sleSrc = view().read(keylet::account(src));
if (!sleSrc)
{
// this can happen if the account was just deleted
JLOG(j_.trace())
<< "service fee not applied because source "
<< src << " does not exist.";
break;
}
if (amt <= beast::zero)
{
JLOG(j_.trace())
<< "skipping non-positive service-fee from " << src << ".";
break;
}

// check if the destination exists
// service fee cannot be used to create accounts.
if (!view().exists(keylet::account(dst)))
{
JLOG(j_.trace())
<< "service fee not applied because destination "
<< dst << " does not exist.";
break;
}
// check if the source exists
auto const& sleSrc = view().read(keylet::account(src));
if (!sleSrc)
{
// this can happen if the account was just deleted
JLOG(j_.trace()) << "service fee not applied because source "
<< src << " does not exist.";
break;
}

if (isXRP(amt))
{
// check if there's enough left in the sender's account
auto srcBal = sleSrc.getFieldAmount(sfBalance);

// service fee will only be delivered if the account
// contains adequate balance to cover reserves, otherwise
// it is disregarded
auto after = srcBal - amt;
if (after < view().fees().accountReserve(sleSrc->getFieldU32(sfOwnerCount)))
// check if the destination exists
// service fee cannot be used to create accounts.
if (!view().exists(keylet::account(dst)))
{
JLOG(j_.trace())
<< "service fee not applied because source "
<< src << " cannot pay it (native).";
<< "service fee not applied because destination " << dst
<< " does not exist.";
break;
}

// action the transfer
if (isXRP(amt))
{
// check if there's enough left in the sender's account
auto srcBal = sleSrc.getFieldAmount(sfBalance);

// service fee will only be delivered if the account
// contains adequate balance to cover reserves, otherwise
// it is disregarded
auto after = srcBal - amt;
if (after < view().fees().accountReserve(
sleSrc->getFieldU32(sfOwnerCount)))
{
JLOG(j_.trace())
<< "service fee not applied because source " << src
<< " cannot pay it (native).";
break;
}

// action the transfer
if (TER const ter{
view().transferXRP(view(), src, dst, amt, j_))
!isTesSuccess(ter))
{
JLOG(j_.warn())
<< "service fee error transferring "
<< amt << " from " << src << " to " << dst
<< " error: " << ter << ".";
}
break;
{
JLOG(j_.warn())
<< "service fee error transferring " << amt << " from "
<< src << " to " << dst << " error: " << ter << ".";
}
break;
}

// execution to here means issued currency service fee
Expand All @@ -2146,60 +2145,62 @@ Transactor::operator()()

if (!sleLine && amt.getIssuer() != dst)
{
JLOG(j_.trace())
<< "service fee not applied because destination "
<< dst << " has no trustline for currency: "
<< amt.getCurrency()
<< " issued by: " << amt.getIssuer() << ".";
break;
JLOG(j_.trace())
<< "service fee not applied because destination " << dst
<< " has no trustline for currency: "
<< amt.getCurrency()
<< " issued by: " << amt.getIssuer() << ".";
break;
}

// action the transfer
{
PaymentSandbox pv(&view());
auto res = accountSend(
pv, src, dst, amt, j_);
PaymentSandbox pv(&view());
auto res = accountSend(pv, src, dst, amt, j_);

if (res == tesSUCCESS)
{
pv.apply(ctx_.rawView());
break;
}

JLOG(j_.trace())
<< "service fee not sent from " << src << " to " << dst << " for "
<< amt.getCurrency() " issued by " << amt.getIssuer() " because "
<< "accountSend() failed with code " << res << ".";
if (res == tesSUCCESS)
{
pv.apply(ctx_.rawView());
break;
}

JLOG(j_.trace())
<< "service fee not sent from " << src << " to " << dst
<< " for " << amt.getCurrency() " issued by "
<< amt.getIssuer() " because "
<< "accountSend() failed with code " << res << ".";
}

} while (0);
}
while (0)
;

if (applied)
{
// Transaction succeeded fully or (retries are not allowed and the
// transaction could claim a fee)

// The transactor and invariant checkers guarantee that this will
// *never* trigger but if it, somehow, happens, don't allow a tx
// that charges a negative fee.
if (fee < beast::zero)
Throw<std::logic_error>("fee charged is negative!");

// Charge whatever fee they specified. The fee has already been
// deducted from the balance of the account that issued the
// transaction. We just need to account for it in the ledger
// header.
if (!view().open() && fee != beast::zero)
ctx_.destroyXRP(fee);

// Once we call apply, we will no longer be able to look at view()
ctx_.apply(result);
}
if (applied)
{
// Transaction succeeded fully or (retries are not allowed and
// the transaction could claim a fee)

// The transactor and invariant checkers guarantee that this
// will *never* trigger but if it, somehow, happens, don't allow
// a tx that charges a negative fee.
if (fee < beast::zero)
Throw<std::logic_error>("fee charged is negative!");

// Charge whatever fee they specified. The fee has already been
// deducted from the balance of the account that issued the
// transaction. We just need to account for it in the ledger
// header.
if (!view().open() && fee != beast::zero)
ctx_.destroyXRP(fee);

// Once we call apply, we will no longer be able to look at
// view()
ctx_.apply(result);
}

JLOG(j_.trace()) << (applied ? "applied" : "not applied")
<< transToken(result);
JLOG(j_.trace())
<< (applied ? "applied" : "not applied") << transToken(result);

return {result, applied};
}
return {result, applied};
}

} // namespace ripple

0 comments on commit d8ec320

Please sign in to comment.