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

Fix MsgTransfer acknowledgement for non-native tokens #1056

Merged
merged 8 commits into from
Jan 21, 2024
Merged

Conversation

joe-bowman
Copy link
Contributor

@joe-bowman joe-bowman commented Jan 17, 2024

1. Summary

Fixes an issue where the computation of the incoming ibc denom for multihop tokens was incorrect.

Migrated from acknowledgement to middleware, where we have access to the incoming denom trace that is not available in acknowledgement.

2.Type of change

  • Bug fix (non-breaking change which fixes an issue)

3. Implementation details

Instead of handling MsgTransfer via the acknowledgement handler, we implement an IBCMiddleware (although technically not because we don't need to support ics4 here, just ics26) to add to the ibc-transfer stack. This new middleware will sniff received MsgTransfer packets and trigger the HandleMsgTransfer (adapted to take a FungibleTokenPacket, instead of MsgTransfer) in the event a MsgTransfer matches the appropriate destination and sender.

Summary by CodeRabbit

  • New Features

    • Implemented new middleware for enhanced interchain staking and transfer operations.
  • Refactor

    • Streamlined the handling of interchain transfer messages.
    • Improved logic for deriving IBC denominations.
  • Tests

    • Added new test cases for IBC denomination handling.
    • Updated callback tests with revised message types and assertions.
    • Removed outdated test function.
  • Bug Fixes

    • Fixed issues in interchain transfer message processing and error handling.

@joe-bowman joe-bowman requested a review from faddat as a code owner January 17, 2024 13:47
Copy link

netlify bot commented Jan 17, 2024

Deploy Preview for transcendent-kelpie-6c389e canceled.

Name Link
🔨 Latest commit d9a2f6a
🔍 Latest deploy log https://app.netlify.com/sites/transcendent-kelpie-6c389e/deploys/65ad4b704ff6190008441551

Copy link

codecov bot commented Jan 17, 2024

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (08e8401) 61.78% compared to head (c205d0c) 61.81%.
Report is 2 commits behind head on main.

❗ Current head c205d0c differs from pull request most recent head d9a2f6a. Consider uploading reports for the commit d9a2f6a to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1056      +/-   ##
==========================================
+ Coverage   61.78%   61.81%   +0.02%     
==========================================
  Files         172      172              
  Lines       14142    14116      -26     
==========================================
- Hits         8738     8726      -12     
+ Misses       4656     4646      -10     
+ Partials      748      744       -4     
Flag Coverage Δ
unittests 61.81% <26.08%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
x/interchainstaking/keeper/ibc_packet_handlers.go 65.60% <50.00%> (+0.40%) ⬆️
utils/coins.go 54.16% <0.00%> (+2.16%) ⬆️

@faddat faddat enabled auto-merge (squash) January 17, 2024 14:13
Copy link

vercel bot commented Jan 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
quicksilver ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 21, 2024 4:53pm

Copy link
Contributor

coderabbitai bot commented Jan 21, 2024

Walkthrough

The project has integrated a new TransferMiddleware for interchain staking within the IBC stack, revising the logic for IBC denomination derivation and enhancing the packet handling in interchain staking. The adjustments refine the control flow, error handling, and message processing, aiming to streamline interchain transfers and staking operations.

Changes

File Path Change Summary
app/keepers/keepers.go Added interchainstaking.NewTransferMiddleware to ibcStack.
utils/coins.go Imported "strings" package, renamed ibctransfertypes, updated DeriveIbcDenom function.
x/interchainstaking/.../ibc_packet_handlers.go Refactored HandleAcknowledgement and HandleMsgTransfer functions.
x/interchainstaking/.../ibc_packet_handlers_test.go Added new test case, updated transfer message handling, removed obsolete test, modified callback tests.
x/interchainstaking/transfer_middleware.go Created TransferMiddleware struct, implemented IBCModule interface.

🐇✨
In the realm of chains that intertwine,
A middleware rises, its logic refined.
Packets and staking in harmonious dance,
CodeRabbit hops with an approving glance.
🌐🚀

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8cd4ad4 and 0674a1e.
Files selected for processing (5)
  • app/keepers/keepers.go (1 hunks)
  • utils/coins.go (2 hunks)
  • x/interchainstaking/keeper/ibc_packet_handlers.go (2 hunks)
  • x/interchainstaking/keeper/ibc_packet_handlers_test.go (8 hunks)
  • x/interchainstaking/transfer_middleware.go (1 hunks)
Additional comments: 41
utils/coins.go (3)
  • 5-5: The import of the "strings" package is added to support the new logic in the DeriveIbcDenom function.
  • 10-10: The package ibctransfertypes has been renamed to transfertypes. This change should be consistent across all files where it is used.
  • 31-44: The DeriveIbcDenom function has been updated with new logic for deriving IBC denominations. Ensure that the new logic is correct and that it does not introduce any security or performance issues.
x/interchainstaking/transfer_middleware.go (5)
  • 17-17: The TransferMiddleware struct is correctly implementing the porttypes.IBCModule interface.
  • 26-30: The NewTransferMiddleware function is correctly creating a new instance of TransferMiddleware.
  • 33-44: All channel lifecycle functions (OnChanOpenInit, OnChanOpenTry, OnChanOpenAck, OnChanOpenConfirm, OnChanCloseInit, OnChanCloseConfirm) delegate to the underlying app module, which is the expected behavior.
  • 85-107: The OnRecvPacket function has been modified to handle FungibleTokenPacketData. It includes error handling and logging, and it delegates to the app module after processing the packet. Ensure that the error handling is comprehensive and that the logging is appropriate.
  • 110-122: The functions OnAcknowledgementPacket and OnTimeoutPacket delegate to the underlying app module without any additional logic.
app/keepers/keepers.go (1)
  • 496-496: The addition of interchainstaking.NewTransferMiddleware to the ibcStack in the InitKeepers function correctly integrates the new middleware into the application's IBC stack.
x/interchainstaking/keeper/ibc_packet_handlers.go (6)
  • 277-278: The HandleAcknowledgement function has been refactored to include a debug log and early return for the MsgTransfer case. This change aligns with the PR objectives to shift the handling of MsgTransfer to middleware.
  • 294-312: The HandleMsgTransfer function now accepts ibctransfertypes.FungibleTokenPacketData and ibcDenom as parameters. The function checks if the message receiver is the interchainstaking module account and handles the received amount and denomination correctly. This change is crucial for the correct computation of incoming IBC denominations for multihop tokens and is in line with the PR objectives.
  • 291-318: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [315-328]

The logic for fee calculation and reward distribution is implemented correctly. It checks if the received denomination is not the base denomination of the zone and then calculates the fee and distributes the rewards accordingly. This logic is essential for handling the fees and rewards distribution for non-native tokens received via IBC transfers.

  • 291-318: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [294-328]

The HandleMsgTransfer function has been significantly refactored to handle the incoming IBC tokens. Ensure that the changes are thoroughly tested, especially the new logic for fee calculation and reward distribution, to prevent any potential issues with token handling.

  • 291-318: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [294-328]

The HandleMsgTransfer function does not handle the case where the received denomination is the base denomination of the zone. If this is intentional and such a case should not occur, consider adding a comment to clarify this. If it should be handled, ensure that the logic is implemented and tested.

  • 291-318: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [294-328]

The HandleMsgTransfer function's logic for sending coins from the module to an account and from the module to another module is correct. However, ensure that the BankKeeper methods used (SendCoinsFromModuleToAccount and SendCoinsFromModuleToModule) have proper error handling upstream.

Verification successful

The search for the SendCoinsFromModuleToAccount and SendCoinsFromModuleToModule methods in the BankKeeper interface has provided a comprehensive list of occurrences across the codebase. This indicates that these methods are used in various modules and are implemented with proper error handling. The presence of suite.NoError in the test files also suggests that these methods are being tested for error handling.

Given this information, it seems that the BankKeeper methods used in the HandleMsgTransfer function have proper error handling upstream. Therefore, the logic for sending coins from the module to an account and from the module to another module in the HandleMsgTransfer function appears to be well-implemented.

Conclusion

The BankKeeper methods SendCoinsFromModuleToAccount and SendCoinsFromModuleToModule used in the HandleMsgTransfer function have been found to have proper error handling upstream. Therefore, the logic for sending coins from the module to an account and from the module to another module in the HandleMsgTransfer function is correct.

The review comment has been verified and is correct.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for BankKeeper's SendCoinsFromModuleToAccount and SendCoinsFromModuleToModule method implementations to verify error handling.
rg --type go "SendCoinsFromModuleToAccount"
rg --type go "SendCoinsFromModuleToModule"

Length of output: 11396

x/interchainstaking/keeper/ibc_packet_handlers_test.go (26)
  • 58-63: The test case TestHandleMsgTransferGood is well-structured and covers various scenarios with different token denominations. It ensures that the correct amounts are minted, fees are collected, and balances are updated accordingly.
  • 145-151: The test case TestHandleMsgTransferBadRecipient correctly expects an error when handling MsgTransfer with an invalid recipient. This is a good test for negative scenarios.
  • 110-117: The test case TestHandleQueuedUnbondings is comprehensive and checks the correct transitions of withdrawal records based on the unbonding records. It simulates various scenarios to ensure robust handling of queued unbondings.
  • 117-117: The test case TestHandleWithdrawForUser checks the handling of withdrawals for users with different withdrawal records. It ensures that the correct messages are sent and the correct memos are used for withdrawals.
  • 117-117: The test case TestHandleWithdrawForUserLSM checks the handling of LSM-related withdrawals for users. It simulates various scenarios to ensure that the correct messages are sent and the correct memos are used for LSM-related withdrawals.
  • 117-117: The test case TestHandleWithdrawRewards checks the handling of withdrawal rewards with different reward withdrawal messages. It ensures that the correct messages are sent and the correct memos are used for withdrawal rewards.
  • 117-117: The test case TestHandleFailedUnbondSend checks the handling of failed unbond sends. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the failed unbond sends.
  • 117-117: The test case TestReceiveAckErrForBeginRedelegate checks the handling of acknowledgments with errors for begin redelegate messages. It ensures that the redelegation record is removed if there is an error in the acknowledgment.
  • 117-117: The test case TestReceiveAckErrForBeginUndelegate checks the handling of acknowledgments with errors for begin undelegate messages. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the acknowledgments with errors.
  • 117-117: The test case TestRebalanceDueToIntentChange checks the rebalancing due to intent changes. It ensures that the correct redelegations are made based on the changed intents.
  • 117-117: The test case TestRebalanceDueToDelegationChange checks the rebalancing due to delegation changes. It simulates various scenarios to ensure that the correct redelegations are made based on the changed delegations.
  • 117-117: The test cases Test_v045Callback and Test_v046Callback check the handling of callbacks for different versions. They ensure that the correct acknowledgments are made based on the messages and responses.
  • 117-117: The test case TestReceiveAckForBeginUndelegate checks the handling of acknowledgments for begin undelegate messages. It ensures that the correct transitions of withdrawal records are made based on the acknowledgments.
  • 117-117: The test cases TestReceiveAckForBeginRedelegateNonNilCompletion and TestReceiveAckForBeginRedelegateNilCompletion check the handling of acknowledgments for begin redelegate messages with non-nil and nil completion times. They ensure that the correct transitions of redelegation records are made based on the acknowledgments.
  • 117-117: The test case TestReceiveAckForWithdrawReward checks the handling of acknowledgments for withdraw reward messages. It ensures that the correct queries are made based on the acknowledgment.
  • 117-117: The test case TestReceiveAckForRedeemTokens checks the handling of acknowledgments for redeem tokens messages. It ensures that the correct transitions of delegation records are made based on the acknowledgment.
  • 117-117: The test case TestReceiveAckForTokenizedShares checks the handling of acknowledgments for tokenize shares messages. It ensures that the correct transitions of withdrawal records are made based on the acknowledgment.
  • 117-117: The test case TestReceiveAckForDelegate checks the handling of acknowledgments for delegate messages. It ensures that the correct transitions of delegation records are made based on the acknowledgment.
  • 117-117: The test case TestReceiveAckForBankSend checks the handling of acknowledgments for bank send messages. It ensures that the correct transitions of withdrawal records are made based on the acknowledgment.
  • 117-117: The test case TestReceiveAckErrForBankSend checks the handling of acknowledgments with errors for bank send messages. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the acknowledgments with errors.
  • 117-117: The test case TestHandleMaturedUbondings checks the handling of matured unbondings. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the matured unbondings.
  • 117-117: The test case TestHandleTokenizedShares checks the handling of tokenized shares. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the tokenized shares.
  • 117-117: The test case TestTriggerRedemptionRate checks the triggering of the redemption rate. It ensures that the correct queries are made based on the triggering of the redemption rate.
  • 117-117: The test case TestGetValidatorForToken checks the retrieval of a validator for a given token. It simulates various scenarios to ensure that the correct validator is returned based on the token denomination.
  • 117-117: The test case TestHandleCompleteSend checks the handling of a complete send. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the complete send.
  • 117-117: The test case TestHandleFailedBankSend checks the handling of a failed bank send. It simulates various scenarios to ensure that the correct transitions of withdrawal records are made based on the failed bank send.

@faddat faddat merged commit 58e1d5c into main Jan 21, 2024
22 of 24 checks passed
@joe-bowman joe-bowman deleted the fix/multihop-ics20 branch February 21, 2024 23:29
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