Skip to content

Commit c36683a

Browse files
authored
XRPLF review of XLS34 (XRPLF#69)
* add amendment guard for sfTransferRate * make `sfTransferRate` optional * split tests into existing / xls34 * make variables `const` * clang-format * chage error code * guard optional `sfTransferRate` * rename tests * Guard Optional sfTransferRate * clang-format * fixup tests
1 parent 5aec64c commit c36683a

File tree

8 files changed

+645
-1513
lines changed

8 files changed

+645
-1513
lines changed

src/ripple/app/tx/impl/Escrow.cpp

+25-15
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ EscrowCreate::doApply()
218218
auto const balance = STAmount((*sle)[sfBalance]).xrp();
219219
auto const reserve =
220220
ctx_.view().fees().accountReserve((*sle)[sfOwnerCount] + 1);
221-
bool isIssuer = amount.getIssuer() == account;
221+
bool const isIssuer = amount.getIssuer() == account;
222222

223223
if (balance < reserve)
224224
return tecINSUFFICIENT_RESERVE;
@@ -237,7 +237,7 @@ EscrowCreate::doApply()
237237
if (!ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
238238
return temDISABLED;
239239

240-
TER result = trustTransferAllowed(
240+
TER const result = trustTransferAllowed(
241241
ctx_.view(),
242242
{account, ctx_.tx[sfDestination]},
243243
amount.issue(),
@@ -263,7 +263,7 @@ EscrowCreate::doApply()
263263
return tecNO_LINE;
264264

265265
{
266-
TER result = trustAdjustLockedBalance(
266+
TER const result = trustAdjustLockedBalance(
267267
ctx_.view(), sleLine, amount, 1, ctx_.journal, DryRun);
268268

269269
JLOG(ctx_.journal.trace())
@@ -296,20 +296,24 @@ EscrowCreate::doApply()
296296

297297
// Create escrow in ledger. Note that we we use the value from the
298298
// sequence or ticket. For more explanation see comments in SeqProxy.h.
299-
auto xferRate = transferRate(view(), amount.getIssuer());
300299
Keylet const escrowKeylet =
301300
keylet::escrow(account, seqID(ctx_));
302301
auto const slep = std::make_shared<SLE>(escrowKeylet);
303302
(*slep)[sfAmount] = ctx_.tx[sfAmount];
304303
(*slep)[sfAccount] = account;
305-
(*slep)[sfTransferRate] = xferRate.value;
306304
(*slep)[~sfCondition] = ctx_.tx[~sfCondition];
307305
(*slep)[~sfSourceTag] = ctx_.tx[~sfSourceTag];
308306
(*slep)[sfDestination] = ctx_.tx[sfDestination];
309307
(*slep)[~sfCancelAfter] = ctx_.tx[~sfCancelAfter];
310308
(*slep)[~sfFinishAfter] = ctx_.tx[~sfFinishAfter];
311309
(*slep)[~sfDestinationTag] = ctx_.tx[~sfDestinationTag];
312310

311+
if (ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
312+
{
313+
auto const xferRate = transferRate(view(), amount.getIssuer());
314+
(*slep)[~sfTransferRate] = xferRate.value;
315+
}
316+
313317
ctx_.view().insert(slep);
314318

315319
// Add escrow to sender's owner directory
@@ -346,7 +350,7 @@ EscrowCreate::doApply()
346350
return tecNO_LINE;
347351

348352
// do the lock-up for real now
349-
TER result = trustAdjustLockedBalance(
353+
TER const result = trustAdjustLockedBalance(
350354
ctx_.view(), sleLine, amount, 1, ctx_.journal, WetRun);
351355

352356
JLOG(ctx_.journal.trace())
@@ -469,7 +473,7 @@ EscrowFinish::doApply()
469473

470474
AccountID const account = (*slep)[sfAccount];
471475
auto const sle = ctx_.view().peek(keylet::account(account));
472-
auto amount = slep->getFieldAmount(sfAmount);
476+
auto const amount = slep->getFieldAmount(sfAmount);
473477

474478
// If a cancel time is present, a finish operation should only succeed prior
475479
// to that time. fix1571 corrects a logic error in the check that would make
@@ -576,16 +580,19 @@ EscrowFinish::doApply()
576580
if (!ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
577581
return temDISABLED;
578582

583+
if (!slep->isFieldPresent(sfTransferRate))
584+
return tecINTERNAL;
585+
579586
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
580-
auto issuerAccID = amount.getIssuer();
587+
auto const issuerAccID = amount.getIssuer();
581588
auto const xferRate = transferRate(view(), issuerAccID);
582589
// update if issuer rate is less than locked rate
583590
if (xferRate < lockedRate)
584591
lockedRate = xferRate;
585592

586593
// perform a dry run of the transfer before we
587594
// change anything on-ledger
588-
TER result = trustTransferLockedBalance(
595+
TER const result = trustTransferLockedBalance(
589596
ctx_.view(),
590597
account_, // txn signing account
591598
sle, // src account
@@ -632,8 +639,11 @@ EscrowFinish::doApply()
632639
else
633640
{
634641
// compute transfer fee, if any
642+
if (!slep->isFieldPresent(sfTransferRate))
643+
return tecINTERNAL;
644+
635645
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
636-
auto issuerAccID = amount.getIssuer();
646+
auto const issuerAccID = amount.getIssuer();
637647
auto const xferRate = transferRate(view(), issuerAccID);
638648
// update if issuer rate is less than locked rate
639649
if (xferRate < lockedRate)
@@ -642,7 +652,7 @@ EscrowFinish::doApply()
642652
// all the significant complexity of checking the validity of this
643653
// transfer and ensuring the lines exist etc is hidden away in this
644654
// function, all we need to do is call it and return if unsuccessful.
645-
TER result = trustTransferLockedBalance(
655+
TER const result = trustTransferLockedBalance(
646656
ctx_.view(),
647657
account_, // txn signing account
648658
sle, // src account
@@ -733,8 +743,8 @@ EscrowCancel::doApply()
733743

734744
AccountID const account = (*slep)[sfAccount];
735745
auto const sle = ctx_.view().peek(keylet::account(account));
736-
auto amount = slep->getFieldAmount(sfAmount);
737-
bool isIssuer = amount.getIssuer() == account;
746+
auto const amount = slep->getFieldAmount(sfAmount);
747+
bool const isIssuer = amount.getIssuer() == account;
738748

739749
std::shared_ptr<SLE> sleLine;
740750

@@ -750,7 +760,7 @@ EscrowCancel::doApply()
750760
account, amount.getIssuer(), amount.getCurrency()));
751761

752762
// dry run before we make any changes to ledger
753-
if (TER result = trustAdjustLockedBalance(
763+
if (TER const result = trustAdjustLockedBalance(
754764
ctx_.view(), sleLine, -amount, -1, ctx_.journal, DryRun);
755765
result != tesSUCCESS)
756766
return result;
@@ -794,7 +804,7 @@ EscrowCancel::doApply()
794804
if (!isIssuer)
795805
{
796806
// unlock previously locked tokens from source line
797-
TER result = trustAdjustLockedBalance(
807+
TER const result = trustAdjustLockedBalance(
798808
ctx_.view(), sleLine, -amount, -1, ctx_.journal, WetRun);
799809

800810
JLOG(ctx_.journal.trace())

src/ripple/app/tx/impl/InvariantCheck.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ NoZeroEscrow::finalize(
361361
if (bad_ && rv.rules().enabled(featurePaychanAndEscrowForTokens) &&
362362
txn.isFieldPresent(sfTransactionType))
363363
{
364-
uint16_t tt = txn.getFieldU16(sfTransactionType);
364+
uint16_t const tt = txn.getFieldU16(sfTransactionType);
365365
if (tt == ttESCROW_CANCEL || tt == ttESCROW_FINISH)
366366
return true;
367367

src/ripple/app/tx/impl/PayChan.cpp

+49-38
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ closeChannel(
133133
keylet::line(src, amount.getIssuer(), amount.getCurrency()));
134134

135135
// dry run
136-
TER result =
136+
TER const result =
137137
trustAdjustLockedBalance(view, sleLine, -amount, -1, j, DryRun);
138138

139139
JLOG(j.trace()) << "closeChannel: trustAdjustLockedBalance(dry) result="
@@ -178,7 +178,7 @@ closeChannel(
178178
(*sle)[sfBalance] = (*sle)[sfBalance] + amount;
179179
else
180180
{
181-
TER result =
181+
TER const result =
182182
trustAdjustLockedBalance(view, sleLine, -amount, -1, j, WetRun);
183183

184184
JLOG(j.trace()) << "closeChannel: trustAdjustLockedBalance(wet) result="
@@ -257,7 +257,7 @@ PayChanCreate::preclaim(PreclaimContext const& ctx)
257257
return tecINSUFFICIENT_RESERVE;
258258

259259
auto const dst = ctx.tx[sfDestination];
260-
bool isIssuer = amount.getIssuer() == account;
260+
bool const isIssuer = amount.getIssuer() == account;
261261

262262
// Check reserve and funds availability
263263
if (isXRP(amount) && balance < reserve + amount)
@@ -272,7 +272,7 @@ PayChanCreate::preclaim(PreclaimContext const& ctx)
272272
// check for any possible bars to a channel existing
273273
// between these accounts for this asset
274274
{
275-
TER result = trustTransferAllowed(
275+
TER const result = trustTransferAllowed(
276276
ctx.view, {account, dst}, amount.issue(), ctx.j);
277277
JLOG(ctx.j.trace())
278278
<< "PayChanCreate::preclaim trustTransferAllowed result="
@@ -287,7 +287,7 @@ PayChanCreate::preclaim(PreclaimContext const& ctx)
287287
{
288288
auto sleLine = ctx.view.read(keylet::line(
289289
account, amount.getIssuer(), amount.getCurrency()));
290-
TER result = trustAdjustLockedBalance(
290+
TER const result = trustAdjustLockedBalance(
291291
ctx.view, sleLine, amount, 1, ctx.j, DryRun);
292292
JLOG(ctx.j.trace()) << "PayChanCreate::preclaim "
293293
"trustAdjustLockedBalance(dry) result="
@@ -334,8 +334,7 @@ PayChanCreate::doApply()
334334
auto const dst = ctx_.tx[sfDestination];
335335

336336
STAmount const amount{ctx_.tx[sfAmount]};
337-
bool isIssuer = amount.getIssuer() == account;
338-
auto xferRate = transferRate(view(), amount.getIssuer());
337+
bool const isIssuer = amount.getIssuer() == account;
339338

340339
// Create PayChan in ledger.
341340
//
@@ -353,12 +352,17 @@ PayChanCreate::doApply()
353352
(*slep)[sfAccount] = account;
354353
(*slep)[sfDestination] = dst;
355354
(*slep)[sfSettleDelay] = ctx_.tx[sfSettleDelay];
356-
(*slep)[sfTransferRate] = xferRate.value;
357355
(*slep)[sfPublicKey] = ctx_.tx[sfPublicKey];
358356
(*slep)[~sfCancelAfter] = ctx_.tx[~sfCancelAfter];
359357
(*slep)[~sfSourceTag] = ctx_.tx[~sfSourceTag];
360358
(*slep)[~sfDestinationTag] = ctx_.tx[~sfDestinationTag];
361359

360+
if (ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
361+
{
362+
auto xferRate = transferRate(view(), amount.getIssuer());
363+
(*slep)[~sfTransferRate] = xferRate.value;
364+
}
365+
362366
ctx_.view().insert(slep);
363367

364368
// Add PayChan to owner directory
@@ -398,7 +402,7 @@ PayChanCreate::doApply()
398402
if (!sleLine)
399403
return tecNO_LINE;
400404

401-
TER result = trustAdjustLockedBalance(
405+
TER const result = trustAdjustLockedBalance(
402406
ctx_.view(), sleLine, amount, 1, ctx_.journal, WetRun);
403407

404408
JLOG(ctx_.journal.trace())
@@ -470,24 +474,28 @@ PayChanFund::doApply()
470474
AccountID const src = (*slep)[sfAccount];
471475
auto const txAccount = ctx_.tx[sfAccount];
472476
auto const expiration = (*slep)[~sfExpiration];
473-
bool isIssuer = amount.getIssuer() == txAccount;
474-
// auto const chanFunds = (*slep)[sfAmount];
475-
476-
// adjust transfer rate
477-
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
478-
auto issuerAccID = amount.getIssuer();
479-
auto const xferRate = transferRate(view(), issuerAccID);
480-
// update if issuer rate less than locked rate
481-
if (xferRate < lockedRate)
482-
(*slep)[sfTransferRate] = xferRate.value;
483-
// throw if issuer rate greater than locked rate
484-
if (xferRate > lockedRate)
485-
return temBAD_TRANSFER_RATE;
477+
bool const isIssuer = amount.getIssuer() == txAccount;
486478

487479
// if this is a Fund operation on an IOU then perform a dry run here
488480
if (!isXRP(amount) &&
489481
ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
490482
{
483+
// adjust transfer rate
484+
if (!slep->isFieldPresent(sfTransferRate))
485+
return tecINTERNAL;
486+
487+
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
488+
auto const issuerAccID = amount.getIssuer();
489+
auto const xferRate = transferRate(view(), issuerAccID);
490+
491+
// update if issuer rate less than locked rate
492+
if (xferRate < lockedRate)
493+
(*slep)[~sfTransferRate] = xferRate.value;
494+
495+
// throw if issuer rate greater than locked rate
496+
if (xferRate > lockedRate)
497+
return temBAD_TRANSFER_RATE;
498+
491499
// issuer does not need to lock anything
492500
if (!isIssuer)
493501
{
@@ -497,7 +505,7 @@ PayChanFund::doApply()
497505
sleLine = ctx_.view().peek(keylet::line(
498506
(*slep)[sfAccount], amount.getIssuer(), amount.getCurrency()));
499507

500-
TER result = trustAdjustLockedBalance(
508+
TER const result = trustAdjustLockedBalance(
501509
ctx_.view(), sleLine, amount, 1, ctx_.journal, DryRun);
502510

503511
JLOG(ctx_.journal.trace())
@@ -571,7 +579,7 @@ PayChanFund::doApply()
571579
// issuer does not need to lock anything
572580
if (!isIssuer)
573581
{
574-
TER result = trustAdjustLockedBalance(
582+
TER const result = trustAdjustLockedBalance(
575583
ctx_.view(), sleLine, amount, 1, ctx_.journal, WetRun);
576584

577585
JLOG(ctx_.journal.trace())
@@ -715,11 +723,11 @@ PayChanClaim::doApply()
715723
}
716724

717725
if (reqBalance > chanFunds)
718-
return tecINSUFFICIENT_FUNDS;
726+
return tecUNFUNDED_PAYMENT;
719727

720728
if (reqBalance <= chanBalance)
721729
// nothing requested
722-
return tecINSUFFICIENT_FUNDS;
730+
return tecUNFUNDED_PAYMENT;
723731

724732
auto sled = ctx_.view().peek(keylet::account(dst));
725733
if (!sled)
@@ -746,17 +754,6 @@ PayChanClaim::doApply()
746754
}
747755
}
748756

749-
// compute transfer fee, if any
750-
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
751-
auto issuerAccID = chanFunds.getIssuer();
752-
auto const xferRate = transferRate(view(), issuerAccID);
753-
// update if issuer rate is less than locked rate
754-
if (xferRate < lockedRate)
755-
{
756-
(*slep)[sfTransferRate] = xferRate.value;
757-
lockedRate = xferRate;
758-
}
759-
760757
(*slep)[sfBalance] = ctx_.tx[sfBalance];
761758
STAmount const reqDelta = reqBalance - chanBalance;
762759
assert(reqDelta >= beast::zero);
@@ -770,8 +767,22 @@ PayChanClaim::doApply()
770767
if (!ctx_.view().rules().enabled(featurePaychanAndEscrowForTokens))
771768
return temDISABLED;
772769

770+
// compute transfer fee, if any
771+
if (!slep->isFieldPresent(sfTransferRate))
772+
return tecINTERNAL;
773+
774+
Rate lockedRate = ripple::Rate(slep->getFieldU32(sfTransferRate));
775+
auto const issuerAccID = chanFunds.getIssuer();
776+
auto const xferRate = transferRate(view(), issuerAccID);
777+
// update if issuer rate is less than locked rate
778+
if (xferRate < lockedRate)
779+
{
780+
(*slep)[~sfTransferRate] = xferRate.value;
781+
lockedRate = xferRate;
782+
}
783+
773784
auto sleSrcAcc = ctx_.view().peek(keylet::account(src));
774-
TER result = trustTransferLockedBalance(
785+
TER const result = trustTransferLockedBalance(
775786
ctx_.view(),
776787
txAccount,
777788
sleSrcAcc,

0 commit comments

Comments
 (0)