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

test: cover more of transfer OnTimeoutPacket #6456

Merged

Conversation

gjermundgaraba
Copy link
Contributor

@gjermundgaraba gjermundgaraba commented Jun 3, 2024

Description

closes: #6447

Some relatively light unit tests for the transfer IBCModule OnTimeoutPacket method. Most of the critical functionality is dealt with in the relay_test file (I also added another test case there after going through the coverage report).


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/).
  • Added relevant godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Include a descriptive changelog entry when appropriate. This may be left to the discretion of the PR reviewers. (e.g. chores should be omitted from changelog)
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

Summary by CodeRabbit

  • Tests
    • Enhanced TestOnChanOpenAck to better handle counterparty version validation.
    • Added TestOnTimeoutPacket to cover various packet timeout scenarios, improving reliability.
    • Updated TestOnTimeoutPacket in relay_test to handle amount as a string, ensuring consistency in data handling.

@gjermundgaraba gjermundgaraba linked an issue Jun 3, 2024 that may be closed by this pull request
3 tasks
Copy link
Contributor

coderabbitai bot commented Jun 3, 2024

Walkthrough

The updates enhance the ibc_module_test.go and keeper/relay_test.go files in the Cosmos SDK. Key changes include importing necessary modules, refining the TestOnChanOpenAck function for better counterparty version validation, and introducing a new TestOnTimeoutPacket function to test packet timeout scenarios. Additionally, the amount variable in keeper/relay_test.go was changed from sdkmath.Int to string, with corresponding adjustments in handling.

Changes

Files Change Summary
modules/.../ibc_module_test.go Imported sdkmath and ibcerrors, modified TestOnChanOpenAck for counterparty version validation, added TestOnTimeoutPacket function for packet timeout scenarios.
modules/.../keeper/relay_test.go Changed amount from sdkmath.Int to string in TestOnTimeoutPacket, adjusted handling of amount accordingly.

Sequence Diagram(s) (Beta)

No sequence diagrams are necessary for these changes as they primarily involve test modifications and minor adjustments.

Assessment against linked issues

Objective Addressed Explanation
Add test for transfer's OnTimeoutPacket (#6447)
Use expected errors in ibc_module_test.go for transfer module (#6214) The changes do not explicitly mention refactoring to use expError instead of expPass.
Add additional tests for multi token transfers in relay tests (#6402) The changes do not include tests for multi-token transfers in relay_test.go.

Possibly related issues


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 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

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.
  • 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
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Thanks, @gjermundgaraba!

Comment on lines 286 to 295
name: "success",
malleate: func() {},
expError: nil,
},
{
name: "non-existent channel",
malleate: func() {
packet.SourceChannel = "channel-100"
},
expError: ibcerrors.ErrNotFound,
Copy link
Contributor

Choose a reason for hiding this comment

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

You could remove the name, malleate and expError keys to be consistent with the two test cases below.

Comment on lines 344 to 347
// check that module account escrow address has locked the tokens
escrowAddress := types.GetEscrowAddress(packet.GetSourcePort(), packet.GetSourceChannel())
escrowBalance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), escrowAddress, sdk.DefaultBondDenom)
suite.Require().Equal(coinToSendToB, escrowBalance)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is already tested elsewhere, maybe we can remove it to narrow the scope of the test?

@@ -885,7 +885,7 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacketSetsTotalEscrowAmountFo
func (suite *KeeperTestSuite) TestOnTimeoutPacket() {
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in this file are unrelated to the main purpose of the PR to increase coverage of OnTtimeoutPacket, right? We would normally do this in a separate PR, so that reviews are easier; or at least would add a comment with the purpose of the changes to help the reviewers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, this was somewhat orthogonal to the original issue. It was just a case I saw that was not covered when going through the coverage report. I can easily pull this out into a separate PR. Would you prefer an issue for it, a comment (I am adding that in any case now) or a PR without an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

A separate PR would be fine by me. Thank you, @gjermundgaraba!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done: #6460

@@ -885,7 +885,7 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacketSetsTotalEscrowAmountFo
func (suite *KeeperTestSuite) TestOnTimeoutPacket() {
var (
path *ibctesting.Path
amount sdkmath.Int
amount string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this test to take string directly so that we can also test the case of a malformed string amount, which was not currently covered. I discovered this when going through the coverage report related to the original issue. I can also pull this out into a separate PR.

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.

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 027e515 and 5424614.

Files selected for processing (2)
  • modules/apps/transfer/ibc_module_test.go (4 hunks)
  • modules/apps/transfer/keeper/relay_test.go (7 hunks)
Additional context used
Path-based instructions (2)
modules/apps/transfer/ibc_module_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

modules/apps/transfer/keeper/relay_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Additional comments not posted (6)
modules/apps/transfer/ibc_module_test.go (4)

7-8: New import sdkmath from "cosmossdk.io/math" added.

This import is necessary for handling mathematical operations with more precision, which is often required in blockchain applications.


19-19: New import ibcerrors from "github.com/cosmos/ibc-go/v8/modules/core/errors" added.

This import is crucial for handling IBC-specific errors, ensuring that error handling is consistent with the IBC module's standards.


236-240: Modified test case to handle an "invalid counterparty version".

This change enhances the robustness of the version handling in channel negotiations by explicitly testing for invalid versions.


276-367: Added new function TestOnTimeoutPacket to test various timeout scenarios.

This addition is crucial for improving test coverage on the OnTimeoutPacket method, addressing the previously identified gap in coverage. The test scenarios are well thought out, covering cases like non-existent channels, invalid packet data, and already timed-out packets.

modules/apps/transfer/keeper/relay_test.go (2)

888-888: Change in handling amount from sdkmath.Int to string to test malformed string amounts.

This change allows the test to cover scenarios where the amount is not properly formatted, increasing the robustness of the test suite.


904-906: Ensure proper parsing and error handling for amount.

This change introduces string parsing for amount, which is crucial for handling different input formats. Ensure that the parsing is robust and handles errors gracefully.

Also applies to: 921-923, 935-937, 949-950

Verification successful

The code surrounding the NewIntFromString function calls includes checks to ensure that the parsing was successful by verifying the ok variable. This indicates that the error handling for the amount parsing is properly implemented.

  • Lines 904-906: suite.Require().True(ok) ensures that the parsing was successful.
  • Lines 921-923: suite.Require().True(ok) ensures that the parsing was successful.
  • Lines 935-937: suite.Require().True(ok) ensures that the parsing was successful.
  • Lines 949-950: suite.Require().True(ok) ensures that the parsing was successful.

These checks confirm that the code handles potential parsing errors appropriately.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the amount parsing is robust against various malformed inputs.

# Test: Search for the function usage. Expect: Proper error handling for malformed inputs.
rg --type go $'NewIntFromString' modules/apps/transfer/keeper/relay_test.go

Length of output: 359



Script:

#!/bin/bash
# Extract surrounding lines of each instance of NewIntFromString to verify error handling
rg --type go -A 3 -B 3 $'NewIntFromString' modules/apps/transfer/keeper/relay_test.go

Length of output: 1642

},
sdkerrors.ErrInsufficientFunds,
},
{
"failure: cannot mint because sender address is invalid",
func() {
denom = types.NewDenom(sdk.DefaultBondDenom)
amount = sdkmath.OneInt()
amount = sdkmath.OneInt().String()
Copy link
Contributor

Choose a reason for hiding this comment

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

Handling of invalid sender address and amount.

- sender = "invalid address"
- amount = "invalid"
+ sender = suite.chainA.SenderAccount.GetAddress().String()  # Use a valid address
+ amount = sdkmath.NewInt(100).String()  # Use a valid amount

The test cases for invalid sender address and amount should ensure that the system behaves as expected under these error conditions. However, make sure that these are intentional and covered by assertions in the test.

Also applies to: 969-970

Committable suggestion was skipped due to low confidence.

Copy link

sonarqubecloud bot commented Jun 4, 2024

Quality Gate Passed Quality Gate passed for 'ibc-go'

Issues
12 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

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

Clean and easy to follow, nice work! Great to increase coverage in transfer

@colin-axner colin-axner added the priority PRs that need prompt reviews label Jun 5, 2024
path = ibctesting.NewTransferPath(suite.chainA, suite.chainB)
path.Setup()

coinToSendToB := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(42))
Copy link
Contributor

Choose a reason for hiding this comment

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

should be able to use the ibctesting.TestCoin here but will open issue for that to be cleaned up in bulk.

Copy link
Contributor

@DimitrisJim DimitrisJim left a comment

Choose a reason for hiding this comment

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

LGTM! thanks!

@DimitrisJim DimitrisJim enabled auto-merge (squash) June 5, 2024 16:21
@DimitrisJim DimitrisJim merged commit 01eb3cb into main Jun 5, 2024
72 of 74 checks passed
@DimitrisJim DimitrisJim deleted the gjermund/6447-add-test-for-transfers-ontimeoutpacket branch June 5, 2024 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority PRs that need prompt reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test for transfer's OnTimeoutPacket
4 participants