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: query balances with --resolve-denom panics on invalid metadata.Display #23149

Merged
merged 3 commits into from
Jan 7, 2025

Conversation

arlai-mk
Copy link
Contributor

@arlai-mk arlai-mk commented Jan 2, 2025

Description

Closes: #23147

This PR checks that the metadata.Display is a valid denom when querying bank balances with --resolve-denom flag set, so that the query does not panic anymore if the Display field is not a valid denom. It should fall back to the original denom.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Bug Fixes
    • Improved denomination validation in balance retrieval to prevent potential errors with coin metadata.
    • Enhanced error handling for balance-related gRPC queries.
  • Tests
    • Updated test cases for querying balances and metadata to improve clarity and accuracy.
    • Added new test cases for handling pagination and incomplete metadata scenarios.

@arlai-mk arlai-mk requested review from julienrbrt, sontrinh16 and a team as code owners January 2, 2025 09:15
Copy link
Contributor

coderabbitai bot commented Jan 2, 2025

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the AllBalances method in the BaseKeeper struct to enhance error handling when querying balances. It adds a validation check for denomination metadata, ensuring that if the metadata's display field is invalid or absent, the method defaults to using the base denomination. This change prevents panics during balance queries when the --resolve-denom flag is used, particularly for tokens with incomplete metadata. Additionally, the test cases have been updated to reflect these changes and improve clarity.

Changes

File Change Summary
x/bank/keeper/grpc_query.go Added validation for denomination in AllBalances method, with fallback to base denomination if display is invalid
x/bank/keeper/grpc_query_test.go Updated test cases for querying balances; added handling for pagination and incomplete metadata
x/bank/keeper/keeper_test.go Introduced constants and functions for managing incomplete metadata in tests

Assessment against linked issues

Objective Addressed Explanation
Prevent panic when metadata lacks display field [#23147]
Fallback to base denom when display is missing

Possibly related PRs

  • fix(x/bank): Better handling of negative spendable balances #21407: This PR modifies the SpendableBalances function in the BaseKeeper struct, which is closely related to the changes made in the AllBalances method of the same struct in the main PR, as both involve balance calculations and validation of denominations.
  • feat(bank/v2): Send function #21606: This PR introduces enhancements to the Keeper struct, including the SendCoins method, which also deals with coin management and balance handling, similar to the changes made in the AllBalances method of the main PR.

Suggested labels

C:x/bank/v2

Suggested reviewers

  • facundomedica
  • sontrinh16
  • julienrbrt
  • akhilkumarpilli

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

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

@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

🧹 Nitpick comments (1)
x/bank/keeper/grpc_query.go (1)

68-70: Consider adding explicit handling or logging for the fallback scenario.

When metadata.Display is invalid, the code defaults to using key.K2(). While this behavior is correct, it would be helpful to add a comment or log message to clarify that the fallback is triggered due to metadata.Display being invalid. This will improve the code's maintainability and debuggability.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d39d0bc and 4fea281.

📒 Files selected for processing (1)
  • x/bank/keeper/grpc_query.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
x/bank/keeper/grpc_query.go (1)

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

@aljo242 aljo242 self-requested a review January 3, 2025 14:42
@aljo242
Copy link
Collaborator

aljo242 commented Jan 3, 2025

@arlai-mk looks good! can you add a test for this case?

@julienrbrt
Copy link
Member

+1 for the test case @arlai-mk

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

🧹 Nitpick comments (1)
x/bank/keeper/keeper_test.go (1)

118-131: Consider adding validation for required fields.

The helper function creates metadata without validating required fields. Consider adding validation to ensure test data integrity.

 func addIncompleteMetadata(ctx context.Context, k keeper.BaseKeeper) {
+    if incompleteBaseDenom == "" || incompletePath == "" {
+        panic("incomplete metadata test requires valid base denom and path")
+    }
     metadata := banktypes.Metadata{
         Description: "Incomplete metadata without display field",
         DenomUnits: []*banktypes.DenomUnit{
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4fea281 and c4294e5.

📒 Files selected for processing (2)
  • x/bank/keeper/grpc_query_test.go (3 hunks)
  • x/bank/keeper/keeper_test.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
x/bank/keeper/grpc_query_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"

x/bank/keeper/keeper_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"

⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: tests (03)
  • GitHub Check: tests (02)
  • GitHub Check: tests (01)
  • GitHub Check: tests (00)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-integration
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: golangci-lint
  • GitHub Check: Summary
🔇 Additional comments (7)
x/bank/keeper/grpc_query_test.go (5)

123-124: LGTM! Good test data setup.

The test properly initializes an incomplete metadata coin for testing the panic fix.


127-128: LGTM! Good ordering of test data.

The coins are correctly ordered alphabetically to ensure consistent test behavior.


138-141: LGTM! Clear test assertions.

The assertions are well-structured and verify both the length and content of the response.


Line range hint 159-196: LGTM! Comprehensive pagination testing.

The pagination tests thoroughly verify the behavior with both regular and IBC denoms.


142-156: Test case properly verifies panic prevention.

The test case effectively verifies that the query doesn't panic with incomplete metadata and correctly falls back to the original denomination.

Let's verify that this is the only test covering the panic prevention:

✅ Verification successful

Test coverage for panic prevention is complete and well-designed.

The test case is the only one needed as it comprehensively verifies both the panic prevention and correct fallback behavior in the context of incomplete metadata resolution. The test is well-integrated into a larger test suite that covers related functionality, ensuring proper coverage of edge cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other test cases that might be testing the same panic scenario
rg -A 2 "NotPanics.*AllBalances.*resolve.*denom.*metadata" 

Length of output: 318


Script:

#!/bin/bash
# Search more broadly for NotPanics tests related to AllBalances
rg -B 2 -A 2 "NotPanics.*AllBalances"

# Also get the full test file content to understand the complete test coverage
cat x/bank/keeper/grpc_query_test.go

Length of output: 27473

x/bank/keeper/keeper_test.go (2)

44-45: LGTM! Clear constant naming.

Constants are well-named and follow the existing naming pattern in the file.


88-90: LGTM! Consistent helper function implementation.

The helper function follows the same pattern as other coin creation helpers in the file.

@arlai-mk
Copy link
Contributor Author

arlai-mk commented Jan 7, 2025

Hi @julienrbrt @aljo242,
Thanks for your feedback, I have modified the TestQueryAllBalances to include a test for this case.

For information:

  • As NewCoins sort coins alphabetically, I have reordered the coins to be less confusing
  • I have updated order of arguments for calls to suite.Equal, as expected is the first argument and actual is second. Again, less confusing when tests don't pass
  • I added some check on the Denom for all tests, just for completeness

Obviously, I checked that the new test passes fine, but I also made sure that it is relevant for this case, as it would not pass with the old code (without the fix included in this PR).

If you have any question, please let me know.

Thank you.

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

ACK, thanks!

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Jan 7, 2025
@julienrbrt julienrbrt added this pull request to the merge queue Jan 7, 2025
Merged via the queue into cosmos:main with commit f77a274 Jan 7, 2025
69 of 73 checks passed
mergify bot pushed a commit that referenced this pull request Jan 7, 2025
…isplay (#23149)

Co-authored-by: Alex | Skip <[email protected]>
(cherry picked from commit f77a274)
julienrbrt pushed a commit that referenced this pull request Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/bank
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: x/bank: query balances with --resolve-denom panics when metadata lacks display field
4 participants