Skip to content

Commit

Permalink
Return refund from TransactionFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
sisuresh committed Mar 14, 2024
1 parent 356b074 commit 4739b5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
9 changes: 2 additions & 7 deletions src/transactions/FeeBumpTransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ FeeBumpTransactionFrame::processPostApply(Application& app,
AbstractLedgerTxn& ltx,
TransactionMetaFrame& meta)
{
int64_t preRefundFeeCharged = mInnerTx->getResult().feeCharged;

// We must forward the Fee-bump source so the refund is applied to the
// correct account
mInnerTx->processPostApply(app, ltx, meta, getFeeSourceID());
int64_t refund = mInnerTx->processRefund(app, ltx, meta, getFeeSourceID());

#ifdef ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
// The result codes and a feeCharged without the refund are set in
Expand All @@ -179,10 +177,7 @@ FeeBumpTransactionFrame::processPostApply(Application& app,
auto& innerRes = irp.result;
innerRes.feeCharged = mInnerTx->getResult().feeCharged;

// Now set update feeCharged on the fee bump. We don't have access to
// the refund value here, but we can calculate it using the pre and post
// feeCharged of mInnerTx.
int64_t refund = preRefundFeeCharged - mInnerTx->getResult().feeCharged;
// Now set the updated feeCharged on the fee bump.
mResult.feeCharged -= refund;
}
#endif
Expand Down
27 changes: 15 additions & 12 deletions src/transactions/TransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ TransactionFrame::validateSorobanResources(SorobanNetworkConfig const& config,
return true;
}

void
int64_t
TransactionFrame::refundSorobanFee(AbstractLedgerTxn& ltxOuter,
AccountID const& feeSource)
{
Expand All @@ -803,7 +803,7 @@ TransactionFrame::refundSorobanFee(AbstractLedgerTxn& ltxOuter,
auto const feeRefund = mSorobanExtension->mFeeRefund;
if (feeRefund == 0)
{
return;
return 0;
}

LedgerTxn ltx(ltxOuter);
Expand All @@ -814,19 +814,21 @@ TransactionFrame::refundSorobanFee(AbstractLedgerTxn& ltxOuter,
if (!feeSourceAccount)
{
// Account was merged (shouldn't be possible)
return;
return 0;
}

if (!addBalance(header, feeSourceAccount, feeRefund))
{
// Liabilities in the way of the refund, just skip.
return;
return 0;
}

getResult().feeCharged -= feeRefund;

header.current().feePool -= feeRefund;
ltx.commit();

return feeRefund;
}

void
Expand Down Expand Up @@ -1943,29 +1945,30 @@ TransactionFrame::processPostApply(Application& app,
AbstractLedgerTxn& ltxOuter,
TransactionMetaFrame& meta)
{
processPostApply(app, ltxOuter, meta, getSourceID());
processRefund(app, ltxOuter, meta, getSourceID());
}

// This is a TransactionFrame specific function that should only be used by
// FeeBumpTransactionFrame to forward a different account for the refund.
void
TransactionFrame::processPostApply(Application& app,
AbstractLedgerTxn& ltxOuter,
TransactionMetaFrame& meta,
AccountID const& feeSource)
int64_t
TransactionFrame::processRefund(Application& app, AbstractLedgerTxn& ltxOuter,
TransactionMetaFrame& meta,
AccountID const& feeSource)
{
ZoneScoped;

if (!isSoroban())
{
return;
return 0;
}
// Process Soroban resource fee refund (this is independent of the
// transaction success).
LedgerTxn ltx(ltxOuter);
refundSorobanFee(ltx, feeSource);
int64_t refund = refundSorobanFee(ltx, feeSource);
meta.pushTxChangesAfter(ltx.getChanges());
ltx.commit();

return refund;
}

StellarMessage
Expand Down
8 changes: 5 additions & 3 deletions src/transactions/TransactionFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ class TransactionFrame : public TransactionFrameBase
bool validateSorobanResources(SorobanNetworkConfig const& config,
Config const& appConfig,
uint32_t protocolVersion);
void refundSorobanFee(AbstractLedgerTxn& ltx, AccountID const& feeSource);
int64_t refundSorobanFee(AbstractLedgerTxn& ltx,
AccountID const& feeSource);
void updateSorobanMetrics(Application& app);
#ifdef BUILD_TESTS
public:
Expand Down Expand Up @@ -282,8 +283,9 @@ class TransactionFrame : public TransactionFrameBase
TransactionMetaFrame& meta) override;

// TransactionFrame specific function that allows fee bumps to forward a
// different account for the refund.
void processPostApply(Application& app, AbstractLedgerTxn& ltx,
// different account for the refund. It also returns the refund so
// FeeBumpTransactionFrame can adjust feeCharged.
int64_t processRefund(Application& app, AbstractLedgerTxn& ltx,
TransactionMetaFrame& meta,
AccountID const& feeSource);

Expand Down

0 comments on commit 4739b5e

Please sign in to comment.