Skip to content
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

update Paths snippet test #2765

Merged
merged 5 commits into from
Sep 19, 2024
Merged

update Paths snippet test #2765

merged 5 commits into from
Sep 19, 2024

Conversation

ckeshava
Copy link
Collaborator

@ckeshava ckeshava commented Aug 29, 2024

perform prerequisite test setup within the test, remove dependence on the external state of testnet

High Level Overview of Change

This is the sister PR for XRPLF/xrpl-py#739. Please check that PR for more context.

Context of Change

The paths snippet tests depends on several test net accounts for its functionality. This PR aims to remove such dependencies. The requirements of the test are setup within the test itself. This causes less disruptions to the CI/CD pipelines after testnet resets.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Did you update HISTORY.md?

  • Yes
  • No, this change does not impact library users

Summary by CodeRabbit

  • New Features

    • Enhanced pathfinding test functionality for issued currencies on the XRPL.
    • Introduced a more structured testing setup with multiple wallets and trust lines.
    • Improved pathfinding requests to allow for currency transfers beyond XRP.
  • Bug Fixes

    • Enhanced response handling to validate paths and source amounts accurately.

… test, remove dependence on theexternal state of testnet
Copy link
Collaborator

@mvadari mvadari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this snippet way more complicated?

@ckeshava
Copy link
Collaborator Author

This was the easiest design that I could find, to demonstrate the use of path_find request. The existing code is broken, it warrants manual intervention after every test net reset.

@justinr1234
Copy link
Collaborator

@CodeRabbit full review

@justinr1234
Copy link
Collaborator

@coderabbitai full review

Copy link

coderabbitai bot commented Sep 19, 2024

Actions performed

Full review triggered.

Copy link

coderabbitai bot commented Sep 19, 2024

Warning

Rate limit exceeded

@ckeshava has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 22 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between edeec19 and e2efb1c.

Walkthrough

The changes in the paths.ts file enhance the pathfinding test for the XRP Ledger by renaming the function createTxWithPaths to runTest and expanding its functionality. The implementation now includes three wallets and establishes trust lines for the currency 'USD', allowing for more comprehensive testing of currency transfers. The pathfinding request has been updated to search for paths between accounts using issued currencies, improving response handling and validation of paths and amounts.

Changes

Files Change Summary
packages/xrpl/snippets/src/paths.ts Renamed async function createTxWithPaths() to async function runTest(). Enhanced pathfinding test functionality with three wallets, trust lines for 'USD', and improved response handling.

Suggested reviewers

  • coderabbit
  • justinr1234

🐰 In the ledger's bright light,
Paths twist and turn, oh what a sight!
With wallets three, trust lines in play,
Currency flows in a joyful ballet.
Testing now dances, robust and free,
Hooray for the changes, hop with glee! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f3960c3 and edeec19.

Files selected for processing (1)
  • packages/xrpl/snippets/src/paths.ts (1 hunks)

Comment on lines 21 to 30
await client.submitAndWait(
{
TransactionType: 'AccountSet',
Account: alice.classicAddress,
SetFlag: AccountSetAsfFlags.asfDefaultRipple,
},
{
wallet: alice,
},
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor repeated AccountSet transactions into a helper function

The code for setting the asfDefaultRipple flag is repeated for Alice, Bob, and Carol. Refactoring this into a helper function enhances maintainability and adheres to the DRY (Don't Repeat Yourself) principle.

Here's a suggested helper function:

async function setDefaultRipple(client: Client, wallet: Wallet): Promise<void> {
  await client.submitAndWait(
    {
      TransactionType: 'AccountSet',
      Account: wallet.classicAddress,
      SetFlag: AccountSetAsfFlags.asfDefaultRipple,
    },
    { wallet },
  )
}

You can then use this function for each wallet:

await setDefaultRipple(client, alice)
await setDefaultRipple(client, bob)
await setDefaultRipple(client, carol)

Also applies to: 32-41, 43-52

Comment on lines 55 to 68
await client.submitAndWait(
{
TransactionType: 'TrustSet',
Account: bob.classicAddress,
LimitAmount: {
currency: 'USD',
issuer: alice.classicAddress,
value: '1000',
},
},
{
wallet: bob,
},
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor repeated TrustSet transactions into a helper function

The code for establishing trust lines from Bob to Alice and Carol to Bob is similar. Creating a helper function reduces duplication and improves code readability.

Suggested helper function:

async function setTrustLine(
  client: Client,
  wallet: Wallet,
  currency: string,
  issuer: string,
): Promise<void> {
  await client.submitAndWait(
    {
      TransactionType: 'TrustSet',
      Account: wallet.classicAddress,
      LimitAmount: {
        currency,
        issuer,
        value: '1000',
      },
    },
    { wallet },
  )
}

Usage:

await setTrustLine(client, bob, 'USD', alice.classicAddress)
await setTrustLine(client, carol, 'USD', bob.classicAddress)

Also applies to: 70-83

Comment on lines 118 to 121
const path = paths[0].paths_computed[0][0]
if (path.account !== bob.classicAddress) {
throw new Error('Path does not include bob')
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add checks to prevent possible runtime errors when accessing nested paths

Accessing paths[0].paths_computed[0][0] may throw an error if any of the arrays are empty. Adding checks ensures the arrays contain the expected elements.

Proposed modification:

 const paths = response.result.alternatives
 if (paths.length === 0) {
   throw new Error('No paths found')
 }
+if (
+  !paths[0].paths_computed ||
+  paths[0].paths_computed.length === 0 ||
+  paths[0].paths_computed[0].length === 0
+) {
+  throw new Error('No computed paths available')
+}
 const path = paths[0].paths_computed[0][0]
 if (path.account !== bob.classicAddress) {
   throw new Error('Path does not include Bob')
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const path = paths[0].paths_computed[0][0]
if (path.account !== bob.classicAddress) {
throw new Error('Path does not include bob')
}
const paths = response.result.alternatives
if (paths.length === 0) {
throw new Error('No paths found')
}
if (
!paths[0].paths_computed ||
paths[0].paths_computed.length === 0 ||
paths[0].paths_computed[0].length === 0
) {
throw new Error('No computed paths available')
}
const path = paths[0].paths_computed[0][0]
if (path.account !== bob.classicAddress) {
throw new Error('Path does not include Bob')
}

Comment on lines 126 to 133
const sourceAmount = paths[0].source_amount as IssuedCurrencyAmount
if (
sourceAmount.currency !== 'USD' ||
sourceAmount.issuer !== alice.classicAddress ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- 5 is an arbitrarily chosen amount for this test
parseFloat(sourceAmount.value) !== 5.0
) {
throw new Error('Unexpected source amount')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure source_amount exists before accessing

To prevent runtime errors, verify that source_amount is defined before accessing it.

Suggested change:

+if (!paths[0].source_amount) {
+  throw new Error('Source amount is not defined')
+}
 const sourceAmount = paths[0].source_amount as IssuedCurrencyAmount
 if (
   sourceAmount.currency !== 'USD' ||
   sourceAmount.issuer !== alice.classicAddress ||
   parseFloat(sourceAmount.value) !== 5.0
 ) {
   throw new Error('Unexpected source amount')
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const sourceAmount = paths[0].source_amount as IssuedCurrencyAmount
if (
sourceAmount.currency !== 'USD' ||
sourceAmount.issuer !== alice.classicAddress ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- 5 is an arbitrarily chosen amount for this test
parseFloat(sourceAmount.value) !== 5.0
) {
throw new Error('Unexpected source amount')
if (!paths[0].source_amount) {
throw new Error('Source amount is not defined')
}
const sourceAmount = paths[0].source_amount as IssuedCurrencyAmount
if (
sourceAmount.currency !== 'USD' ||
sourceAmount.issuer !== alice.classicAddress ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- 5 is an arbitrarily chosen amount for this test
parseFloat(sourceAmount.value) !== 5.0
) {
throw new Error('Unexpected source amount')
}

@ckeshava
Copy link
Collaborator Author

I had to merge these changes into this PR as well: #2773 The CI/CD is not passing without the Paths changes.

Once this PR is approved, I can merge the bridge_snippet PR and close this PR.

@justinr1234 justinr1234 merged commit 385a56a into XRPLF:main Sep 19, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants