-
Notifications
You must be signed in to change notification settings - Fork 1.7k
refactor: Extract escrow lock helpers and paychan test helpers #7882
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
Open
dangell7
wants to merge
1
commit into
XRPLF:develop
Choose a base branch
from
Transia-RnD:dangell7/escrow-paychan-refactor
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
|
|
||
| #include <xrpl/basics/Log.h> | ||
| #include <xrpl/beast/utility/Journal.h> | ||
| #include <xrpl/beast/utility/Zero.h> | ||
| #include <xrpl/ledger/ApplyView.h> | ||
| #include <xrpl/ledger/ReadView.h> | ||
| #include <xrpl/ledger/helpers/AccountRootHelpers.h> | ||
| #include <xrpl/ledger/helpers/MPTokenHelpers.h> | ||
| #include <xrpl/ledger/helpers/RippleStateHelpers.h> | ||
|
|
@@ -25,6 +27,291 @@ | |
|
|
||
| namespace xrpl { | ||
|
|
||
| /** | ||
| * Validate that @p account may lock @p amount of a token for later delivery | ||
| * to @p dest. | ||
| * | ||
| * The lock-side counterpart of escrowUnlockPreclaimHelper: every issuer | ||
| * control (locking opt-in, authorization, freeze/lock, transferability, | ||
| * spendable balance) that gates locking token value lives here, so any | ||
| * transactor that locks funds applies the same rules. The signature is | ||
| * view-based rather than PreclaimContext-based so it can also run from | ||
| * doApply. | ||
| */ | ||
| template <ValidIssueType T> | ||
| TER | ||
| escrowLockPreclaimHelper( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| AccountID const& dest, | ||
| STAmount const& amount, | ||
| beast::Journal j); | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowLockPreclaimHelper<Issue>( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| AccountID const& dest, | ||
| STAmount const& amount, | ||
| beast::Journal j) | ||
| { | ||
| auto const& issue = amount.get<Issue>(); | ||
| AccountID const& issuer = amount.getIssuer(); | ||
| // If the issuer is the same as the account, return tecNO_PERMISSION | ||
| if (issuer == account) | ||
| return tecNO_PERMISSION; | ||
|
|
||
| // If the lsfAllowTrustLineLocking is not enabled, return tecNO_PERMISSION | ||
| auto const sleIssuer = view.read(keylet::account(issuer)); | ||
| if (!sleIssuer) | ||
| return tecNO_ISSUER; | ||
| if (!sleIssuer->isFlag(lsfAllowTrustLineLocking)) | ||
| return tecNO_PERMISSION; | ||
|
|
||
| // If the account does not have a trustline to the issuer, return tecNO_LINE | ||
| auto const sleRippleState = view.read(keylet::trustLine(account, issuer, issue.currency)); | ||
| if (!sleRippleState) | ||
| return tecNO_LINE; | ||
|
|
||
| STAmount const balance = (*sleRippleState)[sfBalance]; | ||
|
|
||
| // If balance is positive, issuer must have higher address than account | ||
| if (balance > beast::kZero && issuer < account) | ||
| return tecNO_PERMISSION; // LCOV_EXCL_LINE | ||
|
|
||
| // If balance is negative, issuer must have lower address than account | ||
| if (balance < beast::kZero && issuer > account) | ||
| return tecNO_PERMISSION; // LCOV_EXCL_LINE | ||
|
|
||
| // If the issuer has requireAuth set, check if the account is authorized | ||
| if (auto const ter = requireAuth(view, issue, account); !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has requireAuth set, check if the destination is authorized | ||
| if (auto const ter = requireAuth(view, issue, dest); !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has frozen the account, return tecFROZEN | ||
| if (isFrozen(view, account, issue)) | ||
| return tecFROZEN; | ||
|
|
||
| // If the issuer has frozen the destination, return tecFROZEN | ||
| if (isFrozen(view, dest, issue)) | ||
| return tecFROZEN; | ||
|
|
||
| STAmount const spendableAmount = | ||
| accountHolds(view, account, issue.currency, issuer, FreezeHandling::IgnoreFreeze, j); | ||
|
|
||
| // If the balance is less than or equal to 0, return tecINSUFFICIENT_FUNDS | ||
| if (spendableAmount <= beast::kZero) | ||
| return tecINSUFFICIENT_FUNDS; | ||
|
|
||
| // If the spendable amount is less than the amount, return | ||
| // tecINSUFFICIENT_FUNDS | ||
| if (spendableAmount < amount) | ||
| return tecINSUFFICIENT_FUNDS; | ||
|
|
||
| // If the amount is not addable to the balance, return tecPRECISION_LOSS | ||
| if (!canAdd(spendableAmount, amount)) | ||
| return tecPRECISION_LOSS; | ||
|
|
||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowLockPreclaimHelper<MPTIssue>( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| AccountID const& dest, | ||
| STAmount const& amount, | ||
| beast::Journal j) | ||
| { | ||
| AccountID const issuer = amount.getIssuer(); | ||
| // If the issuer is the same as the account, return tecNO_PERMISSION | ||
| if (issuer == account) | ||
| return tecNO_PERMISSION; | ||
|
|
||
| // If the mpt does not exist, return tecOBJECT_NOT_FOUND | ||
| auto const issuanceKey = keylet::mptokenIssuance(amount.get<MPTIssue>().getMptID()); | ||
| auto const sleIssuance = view.read(issuanceKey); | ||
| if (!sleIssuance) | ||
| return tecOBJECT_NOT_FOUND; | ||
|
|
||
| // If the lsfMPTCanEscrow is not enabled, return tecNO_PERMISSION | ||
| if (!sleIssuance->isFlag(lsfMPTCanEscrow)) | ||
| return tecNO_PERMISSION; | ||
|
|
||
| // If the issuer is not the same as the issuer of the mpt, return | ||
| // tecNO_PERMISSION | ||
| if (sleIssuance->getAccountID(sfIssuer) != issuer) | ||
| return tecNO_PERMISSION; // LCOV_EXCL_LINE | ||
|
|
||
| // If the account does not have the mpt, return tecOBJECT_NOT_FOUND | ||
| if (!view.exists(keylet::mptoken(issuanceKey.key, account))) | ||
| return tecOBJECT_NOT_FOUND; | ||
|
|
||
| // If the issuer has requireAuth set, check if the account is | ||
| // authorized | ||
| auto const& mptIssue = amount.get<MPTIssue>(); | ||
| if (auto const ter = requireAuth(view, mptIssue, account, AuthType::WeakAuth); | ||
| !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has requireAuth set, check if the destination is | ||
| // authorized | ||
| if (auto const ter = requireAuth(view, mptIssue, dest, AuthType::WeakAuth); !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has frozen the account, return tecLOCKED | ||
| if (isFrozen(view, account, mptIssue)) | ||
| return tecLOCKED; | ||
|
|
||
| // If the issuer has frozen the destination, return tecLOCKED | ||
| if (isFrozen(view, dest, mptIssue)) | ||
| return tecLOCKED; | ||
|
|
||
| // If the mpt cannot be transferred, return tecNO_AUTH | ||
| if (auto const ter = canTransfer(view, mptIssue, account, dest); !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| STAmount const spendableAmount = accountHolds( | ||
| view, | ||
| account, | ||
| amount.get<MPTIssue>(), | ||
| FreezeHandling::IgnoreFreeze, | ||
| AuthHandling::IgnoreAuth, | ||
| j); | ||
|
|
||
| // If the balance is less than or equal to 0, return tecINSUFFICIENT_FUNDS | ||
| if (spendableAmount <= beast::kZero) | ||
| return tecINSUFFICIENT_FUNDS; | ||
|
|
||
| // If the spendable amount is less than the amount, return | ||
| // tecINSUFFICIENT_FUNDS | ||
| if (spendableAmount < amount) | ||
| return tecINSUFFICIENT_FUNDS; | ||
|
|
||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| template <ValidIssueType T> | ||
| TER | ||
| escrowLockApplyHelper( | ||
| ApplyView& view, | ||
| AccountID const& issuer, | ||
| AccountID const& sender, | ||
| STAmount const& amount, | ||
| beast::Journal journal); | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowLockApplyHelper<Issue>( | ||
| ApplyView& view, | ||
| AccountID const& issuer, | ||
| AccountID const& sender, | ||
| STAmount const& amount, | ||
| beast::Journal journal) | ||
| { | ||
| // Defensive: Issuer cannot create an escrow | ||
| if (issuer == sender) | ||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||
|
|
||
| auto const ter = | ||
| directSendNoFee(view, sender, issuer, amount, !amount.holds<MPTIssue>(), journal); | ||
| if (!isTesSuccess(ter)) | ||
| return ter; // LCOV_EXCL_LINE | ||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowLockApplyHelper<MPTIssue>( | ||
| ApplyView& view, | ||
| AccountID const& issuer, | ||
| AccountID const& sender, | ||
| STAmount const& amount, | ||
| beast::Journal journal) | ||
| { | ||
| // Defensive: Issuer cannot create an escrow | ||
| if (issuer == sender) | ||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||
|
|
||
| auto const ter = lockEscrowMPT(view, sender, amount, journal); | ||
| if (!isTesSuccess(ter)) | ||
| return ter; // LCOV_EXCL_LINE | ||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| template <ValidIssueType T> | ||
| TER | ||
| escrowUnlockPreclaimHelper( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| STAmount const& amount, | ||
| bool checkFreeze = true); | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowUnlockPreclaimHelper<Issue>( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| STAmount const& amount, | ||
| bool checkFreeze) | ||
| { | ||
| AccountID const& issuer = amount.getIssuer(); | ||
| // If the issuer is the same as the account, return tesSUCCESS | ||
| if (issuer == account) | ||
| return tesSUCCESS; | ||
|
|
||
| // If the issuer has requireAuth set, check if the destination is authorized | ||
| if (auto const ter = requireAuth(view, amount.get<Issue>(), account); !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has deep frozen the destination, return tecFROZEN | ||
| if (checkFreeze && | ||
| isDeepFrozen(view, account, amount.get<Issue>().currency, amount.getIssuer())) | ||
| return tecFROZEN; | ||
|
|
||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| template <> | ||
| inline TER | ||
| escrowUnlockPreclaimHelper<MPTIssue>( | ||
| ReadView const& view, | ||
| AccountID const& account, | ||
| STAmount const& amount, | ||
| bool checkFreeze) | ||
| { | ||
| AccountID const& issuer = amount.getIssuer(); | ||
| // If the issuer is the same as the account, return tesSUCCESS | ||
| if (issuer == account) | ||
| return tesSUCCESS; | ||
|
|
||
| // If the mpt does not exist, return tecOBJECT_NOT_FOUND | ||
| auto const issuanceKey = keylet::mptokenIssuance(amount.get<MPTIssue>().getMptID()); | ||
| auto const sleIssuance = view.read(issuanceKey); | ||
| if (!sleIssuance) | ||
| return tecOBJECT_NOT_FOUND; | ||
|
|
||
| // If the issuer has requireAuth set, check if the account is | ||
| // authorized | ||
| auto const& mptIssue = amount.get<MPTIssue>(); | ||
| if (auto const ter = requireAuth(view, mptIssue, account, AuthType::WeakAuth); | ||
| !isTesSuccess(ter)) | ||
| return ter; | ||
|
|
||
| // If the issuer has frozen the account, return tecLOCKED | ||
| if (checkFreeze && isFrozen(view, account, mptIssue)) | ||
| return tecLOCKED; | ||
|
|
||
| return tesSUCCESS; | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
|
|
||
| template <ValidIssueType T> | ||
| TER | ||
| escrowUnlockApplyHelper( | ||
|
|
@@ -58,14 +345,15 @@ escrowUnlockApplyHelper<Issue>( | |
| bool const recvLow = issuer > receiver; | ||
| bool const senderIssuer = issuer == sender; | ||
| bool const receiverIssuer = issuer == receiver; | ||
| bool const lineExisted = ctx.view.exists(trustLineKey); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit and a problem not for this PR, but we don't need to construct most of these variables until after line 355. |
||
|
|
||
| if (senderIssuer) | ||
| return tecINTERNAL; // LCOV_EXCL_LINE | ||
|
|
||
| if (receiverIssuer) | ||
| return tesSUCCESS; | ||
|
|
||
| if (!ctx.view.exists(trustLineKey) && createAsset) | ||
| if (!lineExisted && createAsset) | ||
| { | ||
| // Can the account cover the trust line's reserve? | ||
| auto const sponsorSle = getEffectiveTxReserveSponsor(ctx, sleDest); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a nit, but it might be worthwhile to considering moving these explicit template instantiations into a source file.