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(x/tx/amino): special case for string represented decimals #22161

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

kocubinski
Copy link
Member

@kocubinski kocubinski commented Oct 7, 2024

Description

Ref: #21853, #20935

Types which are flagged as cosmos.Dec but ARE NOT represented by a LegacyDec need special casing.


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

  • New Features

    • Introduced new test cases to enhance coverage of JSON encoding for governance parameters.
    • Added a constant for cosmos.Dec to improve code clarity and maintainability.
  • Bug Fixes

    • Improved error handling for unmarshalling issues related to cosmos.Dec types.
  • Documentation

    • Updated comments for clarity regarding legacy encoding compatibility.
  • Refactor

    • Renamed methods for better understanding and consistency in naming conventions.

@kocubinski kocubinski requested review from testinginprod, aaronc and a team as code owners October 7, 2024 22:22
Copy link
Contributor

coderabbitai bot commented Oct 7, 2024

📝 Walkthrough

Walkthrough

The pull request introduces several enhancements to the testing framework and encoding logic within the aminojson package. A new test case is added to verify the JSON output equivalence between legacy and new encoders for governance parameters. Modifications include improved error handling in the encoder and updates to comments for clarity. Additionally, a new constant is defined, and function names are adjusted for consistency. Overall, these changes aim to refine the encoder's functionality and enhance test coverage.

Changes

File Path Change Summary
tests/integration/tx/aminojson/aminojson_test.go Added new test case gov/v1_params in TestAminoJSON_LegacyParity; modified TestSendAuthorization to clarify SpendLimit behavior.
x/tx/signing/aminojson/encoder.go Updated comments and enhanced error handling in cosmosDecEncoder function.
x/tx/signing/aminojson/json_marshal.go Added constant cosmosDecType and renamed getFieldEncoding to getFieldEncoder.
x/tx/signing/aminojson/options.go Added new imports, defined customTypeExtension, and modified getFieldEncoder and getMessageAminoNameAny functions.

Possibly related PRs

Suggested labels

C:x/tx, C:x/auth, backport/v0.50.x, backport/v0.52.x

Suggested reviewers

  • aaronc
  • testinginprod
  • julienrbrt
  • tac0turtle

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.

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

🧹 Outside diff range and nitpick comments (1)
tests/integration/tx/aminojson/aminojson_test.go (1)

261-265: LGTM! Consider adding more test cases for governance parameters.

The addition of the "gov/v1_params" test case is a good improvement to ensure parity between legacy and new amino JSON encoders for governance parameters. It specifically tests the encoding of the Quorum field with a decimal value.

Consider adding more test cases with different gov_v1_types.Params fields to ensure comprehensive coverage of governance parameter encoding. For example, you could include cases for VotingPeriod, MinDepositDenom, and other relevant fields.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9076487 and 253221f.

📒 Files selected for processing (4)
  • tests/integration/tx/aminojson/aminojson_test.go (1 hunks)
  • x/tx/signing/aminojson/encoder.go (2 hunks)
  • x/tx/signing/aminojson/json_marshal.go (3 hunks)
  • x/tx/signing/aminojson/options.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
tests/integration/tx/aminojson/aminojson_test.go (3)

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


Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"


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

x/tx/signing/aminojson/encoder.go (1)

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

x/tx/signing/aminojson/json_marshal.go (1)

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

x/tx/signing/aminojson/options.go (1)

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

🔇 Additional comments (6)
x/tx/signing/aminojson/encoder.go (3)

46-47: Improved function comment accuracy

The updated comment now correctly reflects that this encoder only handles cosmos.Dec types, removing the mention of cosmos.Int. This change improves the accuracy of the documentation.


57-57: Enhanced error handling for better debugging

The error message has been improved to include the problematic string value, which will make debugging easier. This change follows best practices for error handling in Go.


Line range hint 46-57: Summary of changes to cosmosDecEncoder

The modifications to cosmosDecEncoder improve both documentation accuracy and error handling:

  1. The function comment now correctly specifies that it only handles cosmos.Dec types.
  2. Error handling for string unmarshalling has been enhanced, providing more context for debugging.

These changes align with the PR objectives and improve the overall code quality. The implementation adheres to the Uber Go Style Guide and maintains the existing logic of the function.

x/tx/signing/aminojson/json_marshal.go (3)

19-20: LGTM: Good addition of a constant for "cosmos.Dec"

The introduction of the cosmosDecType constant improves code maintainability and readability. It centralizes the string value "cosmos.Dec", making it easier to update if needed in the future.


73-74: LGTM: Proper usage of the new constant

The replacement of the hardcoded string "cosmos.Dec" with the cosmosDecType constant is a good practice. It improves code consistency and makes future updates easier.


371-371: LGTM: Improved method naming, verify usage

The renaming of getFieldEncoding to getFieldEncoder enhances code clarity and maintains consistency with other "encoder" terminology in the file. This change improves the overall readability of the code.

Please ensure that all calls to this method have been updated throughout the codebase. Run the following script to verify:

✅ Verification successful

LGTM: Improved method naming, usage verified

The renaming of getFieldEncoding to getFieldEncoder has been successfully verified. No references to getFieldEncoding remain, and getFieldEncoder is consistently used throughout the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining usage of getFieldEncoding method

# Test: Search for getFieldEncoding. Expect: No results
rg --type go 'getFieldEncoding'

# Test: Confirm getFieldEncoder is now used. Expect: At least one result
rg --type go 'getFieldEncoder'

Length of output: 278

@@ -110,6 +122,18 @@ func (enc Encoder) getFieldEncoding(field protoreflect.FieldDescriptor) FieldEnc
}
if proto.HasExtension(opts, cosmos_proto.E_Scalar) {
scalar := proto.GetExtension(opts, cosmos_proto.E_Scalar).(string)
// do not handle encoding of fields tagged only with scalar which are not backed by a
// LegacyDec custom type. This types are handled by the default encoding, as they are
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix grammatical error in comment

There's a typo in the comment on line 126. The word "This" should be "These" to correct the grammatical error.

Apply this diff to fix the typo:

-		// This types are handled by the default encoding, as they are
+		// These types are handled by the default encoding, as they are
📝 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
// LegacyDec custom type. This types are handled by the default encoding, as they are
// LegacyDec custom type. These types are handled by the default encoding, as they are

julienrbrt added a commit that referenced this pull request Oct 8, 2024
@julienrbrt julienrbrt mentioned this pull request Oct 8, 2024
12 tasks
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.

nice, tACK

@julienrbrt julienrbrt self-assigned this Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants