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

perf: cache origin token amount during crosschain #755

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

zakir-code
Copy link
Contributor

@zakir-code zakir-code commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced caching functionality for cross-chain transactions, allowing for more efficient handling of amounts and fees.
  • Bug Fixes

    • Resolved issues related to nonce handling in bridge calls, improving transaction reliability.
  • Documentation

    • Updated method signatures for better clarity and usability in the API.
  • Refactor

    • Improved data types in cache management to support integer values, streamlining operations.

Copy link

coderabbitai bot commented Oct 18, 2024

Walkthrough

The changes primarily involve updates to method signatures and cache handling within the keeper package, specifically in the bridge_call_out.go, expected_keepers_mocks.go, and related files. The BridgeCallBaseCoin and CrosschainBaseCoin methods have been modified to include additional parameters for handling fees and target amounts. The SetCache method across several files has been updated to accept an amount of type sdkmath.Int, reflecting a shift in cache management.

Changes

File Path Change Summary
x/crosschain/keeper/bridge_call_out.go Updated BridgeCallBaseCoin and CrosschainBaseCoin methods to modify nonce handling and cache logic.
x/crosschain/mock/expected_keepers_mocks.go Added import for math, updated SetCache method signature to include amount math.Int.
x/crosschain/types/expected_keepers.go Added import for sdkmath, updated Erc20Keeper interface's SetCache method signature.
x/erc20/keeper/cache.go Updated SetCache method to include amount sdkmath.Int parameter.
x/erc20/keeper/keeper.go Changed Cache field type from collections.Map[string, collections.NoValue] to collections.Map[string, sdkmath.Int].

Possibly related PRs

🐇 In the code, I hop and play,
With fees and caches, I pave the way.
A nonce replaced, a method refined,
In cross-chain magic, new paths we find.
So let’s leap forward, with joy and cheer,
For every change brings us closer, my dear! 🌟


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: 0

🧹 Outside diff range and nitpick comments (4)
x/erc20/keeper/keeper.go (1)

Line range hint 1-124: Overall impact looks good, but additional context would be helpful.

The changes to the Cache field in the Keeper struct and its constructor are implemented correctly and consistently. However, to fully understand the impact of these changes, it would be helpful to have more context:

  1. How is this cached data being used in crosschain operations?
  2. Are there any other files that need to be updated to work with this new cache structure?
  3. Have all the methods that interact with the Cache been updated to handle sdkmath.Int values?

Additionally, it might be beneficial to add a comment explaining the purpose of the Cache field, especially in relation to the performance optimization mentioned in the PR title.

Consider adding a comment above the Cache field in the Keeper struct to explain its purpose and usage, for example:

// Cache stores crosschain origin token amounts for performance optimization
Cache collections.Map[string, sdkmath.Int]

This would improve code readability and make the purpose of the cache clear to other developers.

x/crosschain/types/expected_keepers.go (1)

Line range hint 1-94: Summary of changes and potential impact.

The changes to this file are minimal but potentially impactful:

  1. A new import for sdkmath has been added, which is used in the updated SetCache method signature.
  2. The SetCache method in the Erc20Keeper interface now includes an amount parameter of type sdkmath.Int.

These changes suggest an enhancement to the caching mechanism, allowing it to store amount information along with keys. This could potentially improve performance by reducing the need for separate lookups.

However, it's crucial to ensure that these changes are consistently applied throughout the codebase. All implementations of the Erc20Keeper interface and all calls to the SetCache method will need to be updated to match this new signature.

Consider documenting this change in the module's documentation or changelog, as it represents a breaking change to the Erc20Keeper interface that might affect other parts of the system or external integrations.

x/crosschain/keeper/bridge_call_out.go (2)

Line range hint 250-283: LGTM! Consider improving variable naming for clarity.

The changes to BridgeCallBaseCoin look good and align with the PR objective of caching origin token amounts. The introduction of nonce improves consistency across IBC and non-IBC transfers.

Consider renaming nonce to transferNonce or bridgeCallNonce to make its purpose more explicit, especially since it's used in different contexts (IBC transfers and bridge calls).


311-313: LGTM! Consider adding a comment for clarity.

The update to include the fee amount in the cached value is a good improvement. It ensures that the total amount (including fees) is accurately tracked, which aligns with the PR objective.

Consider adding a brief comment explaining why the fee is added to the amount before caching. For example:

// Cache the total amount including fees to accurately track the origin token amount
return k.erc20Keeper.SetCache(ctx, cacheKey, amount.Amount.Add(fee.Amount))

This will help future developers understand the reasoning behind this calculation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0a123f8 and 57178b1.

📒 Files selected for processing (5)
  • x/crosschain/keeper/bridge_call_out.go (3 hunks)
  • x/crosschain/mock/expected_keepers_mocks.go (2 hunks)
  • x/crosschain/types/expected_keepers.go (2 hunks)
  • x/erc20/keeper/cache.go (1 hunks)
  • x/erc20/keeper/keeper.go (3 hunks)
🧰 Additional context used
🔇 Additional comments (11)
x/erc20/keeper/cache.go (2)

6-6: LGTM: New import added for sdkmath.

The new import for sdkmath "cosmossdk.io/math" is correctly added to support the use of sdkmath.Int in the SetCache method.


13-14: LGTM: SetCache method updated to store amount.

The SetCache method has been successfully modified to accept and store an amount of type sdkmath.Int. This change aligns with the PR objective of caching origin token amounts during crosschain operations.

To ensure all callers of this method have been updated, please run the following verification script:

Make sure to update any calls that don't match the new signature: SetCache(ctx, key, amount).

✅ Verification successful

✅ Verified: All calls to SetCache have been updated to the new signature. No issues found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all calls to SetCache method have been updated with the new signature.

# Test: Search for SetCache method calls. Expect: All calls should have three arguments.
rg --type go 'SetCache\s*\([^)]*\)' -g '!*_test.go'

Length of output: 776

x/erc20/keeper/keeper.go (2)

10-10: LGTM: Import addition is correct and necessary.

The addition of the sdkmath import is appropriate for the subsequent changes in the file. The alias used is consistent with Go naming conventions.


41-41: LGTM: Cache type change looks good. Please provide performance details.

The modification of the Cache field from collections.Map[string, collections.NoValue] to collections.Map[string, sdkmath.Int] allows storing integer values instead of just keys. This change, along with the corresponding update in the NewKeeper function, is implemented correctly.

Given that the PR title mentions performance improvements, could you please provide more details on:

  1. The expected performance impact of this change?
  2. Any benchmarks or metrics that demonstrate the improvement?

To verify the consistency of this change across the codebase, please run the following script:

Also applies to: 74-74

✅ Verification successful

Verified: Cache type modification confirmed. No remaining usages of collections.NoValue found, and all Cache usages utilize sdkmath.Int.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining usages of collections.NoValue in relation to the Cache

# Test 1: Search for any remaining usages of collections.NoValue
echo "Searching for remaining usages of collections.NoValue:"
rg --type go 'collections\.NoValue'

# Test 2: Verify all Cache usages are updated to use sdkmath.Int
echo "Verifying Cache usages with sdkmath.Int:"
rg --type go 'Cache.*sdkmath\.Int'

Length of output: 629

x/crosschain/types/expected_keepers.go (2)

7-7: LGTM: New import added correctly.

The new import for sdkmath is correctly placed and follows the standard convention for import grouping. This import is necessary for the updated SetCache method signature.


50-50: Verify impact of updated SetCache method signature.

The addition of the amount sdkmath.Int parameter to the SetCache method is a significant change. This modification allows storing an amount along with the key in the cache, which can improve performance by reducing the need for separate lookups.

Please ensure that all implementations of the Erc20Keeper interface have been updated to match this new signature. Run the following script to verify:

Also, verify that all calls to SetCache have been updated to include the new amount parameter.

✅ Verification successful

Verify that all calls to SetCache include the new amount parameter.

While the implementations of SetCache have been updated, it's crucial to ensure that every call site has been modified to pass the additional amount parameter. This will prevent potential issues related to incomplete method calls.

Please run the following script to identify any instances where SetCache is called without the amount parameter:


Verification Successful: SetCache method signature is correctly updated.

All implementations of the SetCache method have been updated to include the amount sdkmath.Int parameter, and there are no existing calls missing this parameter.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for implementations of Erc20Keeper and verify SetCache method signature

# Search for Erc20Keeper implementations
echo "Searching for Erc20Keeper implementations:"
rg --type go "type \w+ struct" -A 10 | rg "Erc20Keeper"

# Search for SetCache method implementations
echo "Searching for SetCache method implementations:"
ast-grep --lang go --pattern $'func ($_) SetCache(ctx context.Context, key string, amount sdkmath.Int) error {
  $$$
}'

Length of output: 1455


Script:

#!/bin/bash
# Description: Check for calls to SetCache that do not include the 'amount' parameter

# Search for SetCache calls without the 'amount' parameter
echo "Searching for SetCache calls missing the 'amount' parameter:"
rg --type go 'SetCache\s*\(\s*ctx\s*,\s*key\s*\)' --context 2

# Optionally, list all SetCache calls and their parameters for verification
echo "Listing all SetCache method calls with their parameters:"
ast-grep --lang go --pattern $'SetCache($_, $_, $_)'

Length of output: 377

x/crosschain/keeper/bridge_call_out.go (1)

Line range hint 1-346: Overall, the changes in this file look good and align with the PR objectives.

The modifications to BridgeCallBaseCoin and CrosschainBaseCoin functions improve the handling of origin token amounts and fee inclusion in caching. These changes contribute to more accurate tracking of token amounts across chains.

Remember to consider the minor suggestions for variable naming and adding comments for clarity. These small improvements will enhance the code's readability and maintainability.

x/crosschain/mock/expected_keepers_mocks.go (4)

17-17: LGTM: New import added correctly

The new import for the math package from cosmossdk.io is correctly added and is used in the updated SetCache method.


482-486: LGTM: SetCache method updated correctly

The SetCache method has been properly updated to include the new amount parameter of type math.Int. The changes are consistent in both the method signature and the mock controller call.


490-492: LGTM: MockErc20KeeperMockRecorder.SetCache updated correctly

The SetCache method of MockErc20KeeperMockRecorder has been properly updated to include the new amount parameter. The changes are consistent with the updates made to the MockErc20Keeper.SetCache method, ensuring that the mock recorder correctly handles the new parameter.


Line range hint 1-692: Verify SetCache usage across the codebase

The changes to the SetCache method signature in both MockErc20Keeper and MockErc20KeeperMockRecorder are consistent and correct. However, it's important to ensure that all calls to SetCache throughout the codebase have been updated to include the new amount parameter.

To verify the correct usage of the updated SetCache method, please run the following script:

This script will help identify any places where SetCache is called without the new amount parameter, allowing you to update them accordingly.

✅ Verification successful

SetCache Usage Successfully Verified Across the Codebase

All instances of SetCache have been updated to include the new amount parameter, and no outdated usages were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining calls to SetCache that don't include the amount parameter

# Search for SetCache calls in Go files, excluding the mock file we just reviewed
rg --type go -g '!**/expected_keepers_mocks.go' 'SetCache\s*\([^)]*\)' -A 3

Length of output: 1160

@zakir-code zakir-code merged commit 6a7784a into main Oct 18, 2024
10 checks passed
@zakir-code zakir-code deleted the nulnut/cache-crosschain-amount branch October 18, 2024 09:53
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.

2 participants