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

feat(x/tx): add enum as string encoder option #19618

Merged
merged 8 commits into from
Mar 5, 2024
Merged

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Mar 1, 2024

Description

Closes: #19456

The backport will only add the option to client/v2 encoder options.
After this PR I'll tag x/tx.

ref: #19530


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

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
    • Improved data readability and consistency by encoding enums as strings in various components.
  • Bug Fixes
    • Resolved denotational issues involving commas in denominations.
  • Refactor
    • Restructured FormatCoins for enhanced efficiency.
  • Tests
    • Added test cases to ensure accurate encoding of enums as strings.

@julienrbrt julienrbrt requested a review from a team as a code owner March 1, 2024 14:51
Copy link
Contributor

coderabbitai bot commented Mar 1, 2024

Warning

Rate Limit Exceeded

@julienrbrt has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 57 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between aae9611 and e568ffd.

Walkthrough

The overarching change involves enhancing the encoding process to allow enums to be represented as strings in JSON outputs, addressing issues related to readability and data representation consistency. This update includes fixing issues with denomination strings containing commas and restructuring coin formatting functionalities. The modifications span across various components, introducing an EnumAsString option in encoder configurations, adjusting function calls to accommodate new parameters, and adding tests to ensure the correct implementation of these enhancements.

Changes

File Path Change Summary
client/v2/autocli/query.go Added EnumAsString: true to EncoderOptions in BuildQueryMethodCommand
client/v2/CHANGELOG.md, x/tx/CHANGELOG.md Enum as string option added, fixed denominations with commas, restructured FormatCoins
x/tx/signing/aminojson/encoder.go, x/tx/signing/aminojson/json_marshal.go, x/tx/signing/aminojson/json_marshal_test.go Introduced EnumAsString in EncoderOptions, updated marshal methods, added TestEnumAsString

Assessment against linked issues

Objective Addressed Explanation
Make enums readable by encoding as strings instead of values after v0.50.x upgrade (#19456)
Ensure compatibility with previous versions in terms of enum representation in JSON outputs (#19456) The changes address the readability issue by encoding enums as strings. However, it's unclear if the compatibility with previous versions, specifically the exact string representations used in v0.47.x, is maintained.
Fix issues with denominations containing commas (#19456)
Restructure FormatCoins functionality for better clarity and maintenance (#19456)

The modifications appear to directly address the primary concerns highlighted in the linked issue, especially regarding the readability of enums in JSON outputs. However, there is a slight ambiguity regarding the exact compatibility with previous versions' enum representations, which could benefit from further clarification to ensure complete alignment with the objectives.

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

@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Mar 1, 2024
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.yml

Commits Files that changed from the base of the PR and between 3d59013 and 9bfae4c.
Files selected for processing (5)
  • client/v2/autocli/query.go (1 hunks)
  • x/tx/CHANGELOG.md (1 hunks)
  • x/tx/signing/aminojson/encoder.go (2 hunks)
  • x/tx/signing/aminojson/json_marshal.go (9 hunks)
  • x/tx/signing/aminojson/json_marshal_test.go (1 hunks)
Additional comments: 8
x/tx/signing/aminojson/encoder.go (2)
  • 78-78: Adding a nil argument as a placeholder to the marshalList function call. Ensure that the marshalList function is designed to handle nil appropriately in this context. If nil represents a specific condition or default behavior, this should be clearly documented in the marshalList function's comments.
  • 164-164: Passing pubkeysField as an argument to the marshalList function. This change seems to be intended to provide additional context or customization for the marshaling process. Verify that the marshalList function utilizes this argument effectively and that this change aligns with the intended functionality. Additionally, ensure that this does not introduce any unintended side effects, especially in scenarios where pubkeysField might not be set or could be different from expected.
client/v2/autocli/query.go (1)
  • 114-114: The addition of EnumAsString: true to EncoderOptions in the BuildQueryMethodCommand function is a significant change aimed at improving the readability of query responses by encoding enums as strings. This change aligns with the PR's objectives to enhance user experience. Ensure that all relevant parts of the codebase that rely on enum encoding are tested with this new behavior to prevent any unintended consequences. Additionally, consider documenting this behavior in relevant user-facing documentation to inform users about how enums will be represented in query responses.
x/tx/CHANGELOG.md (1)
  • 44-44: The documentation of the bug fix related to rejecting denoms containing a comma is clear and concise, placed correctly under the "Bug Fixes" section. This entry helps users understand the specific issue addressed by the PR. Ensure that the PR link provided is accurate and leads to the correct issue or PR addressing this bug fix.
x/tx/signing/aminojson/json_marshal.go (4)
  • 31-32: The addition of the EnumAsString field in EncoderOptions is correctly implemented. This field allows users to specify whether enums should be encoded as strings, aligning with the PR's objective to improve the readability of query responses.
  • 50-50: The enumsAsString field added to the Encoder struct correctly reflects the EnumAsString option from EncoderOptions. This field is essential for controlling the encoding behavior of enums throughout the encoding process.
  • 84-84: The assignment of the enumsAsString field from EncoderOptions to the Encoder struct in the NewEncoder function is correctly implemented. This ensures that the encoder respects the EnumAsString option when encoding enums.
  • 222-241: > 📝 NOTE

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

The logic in the marshal method to encode enums as strings based on the EnumAsString option is correctly implemented. It checks if enumsAsString is true and if the field descriptor is not nil before attempting to encode the enum as a string. If the descriptor is found, it writes the enum's name as a string; otherwise, it falls back to encoding the enum as an integer. This approach is consistent with the PR's goal to enhance readability by allowing enums to be encoded as strings.


* [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma.
* * [#](https://github.com/cosmos/cosmos-sdk/pull/) Add enum as string option to encoder.
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition of the feature to add an enum as a string option to the encoder is accurately documented under the "Features" section. This entry clearly communicates the enhancement made to the SDK, aligning with the PR's objectives. Ensure that the placeholder link [#](https://github.com/cosmos/cosmos-sdk/pull/) is replaced with the actual PR link for proper referencing.

- * * [#](https://github.com/cosmos/cosmos-sdk/pull/) Add enum as string option to encoder.
+ * * [#19618](https://github.com/cosmos/cosmos-sdk/pull/19618) Add enum as string option to encoder.

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.

Suggested change
* * [#](https://github.com/cosmos/cosmos-sdk/pull/) Add enum as string option to encoder.
* * [#19618](https://github.com/cosmos/cosmos-sdk/pull/19618) Add enum as string option to encoder.

x/tx/signing/aminojson/json_marshal_test.go Show resolved Hide resolved
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.yml

Commits Files that changed from the base of the PR and between 9bfae4c and a05eebd.
Files selected for processing (4)
  • client/v2/CHANGELOG.md (1 hunks)
  • x/tx/CHANGELOG.md (1 hunks)
  • x/tx/signing/aminojson/encoder.go (2 hunks)
  • x/tx/signing/aminojson/json_marshal.go (9 hunks)
Files skipped from review as they are similar to previous changes (3)
  • x/tx/CHANGELOG.md
  • x/tx/signing/aminojson/encoder.go
  • x/tx/signing/aminojson/json_marshal.go
Additional comments: 2
client/v2/CHANGELOG.md (2)
  • 49-49: The entry for PR #19216 clearly communicates the improvement of using the TxConfig provided in the context directly. This change promotes better consistency and reliability in transaction configuration, aligning well with the PR objectives and changelog best practices.
  • 49-49: The "API Breaking Changes" section effectively communicates the removal of address codecs from autocli.AppOptions and flag.Builder, a significant change that developers need to be aware of. This entry is clear, accurate, and aligns well with the principles of a good changelog by informing developers of changes that may require adjustments in their implementations.

client/v2/CHANGELOG.md Show resolved Hide resolved
@kocubinski
Copy link
Member

kocubinski commented Mar 5, 2024

When constructing an amino JSON encoder for signing should we ensure that this new option is always set to false?
e.g.

if options.Encoder == nil {
h.encoder = NewEncoder(EncoderOptions{
FileResolver: options.FileResolver,
TypeResolver: options.TypeResolver,
})
} else {
h.encoder = *options.Encoder
}

Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

Marshaling mechanics look good, but maybe we should ensure this option is false in aminojson.NewSignModeHandler. toggling on/off will produce different sign bytes.

@julienrbrt
Copy link
Member Author

That makes sense, I'll update to explicitly setting it to false there.
I'll add a warning comment in the option field as well 👍

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

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between a05eebd and 4dbd031.
Files selected for processing (3)
  • x/tx/CHANGELOG.md (2 hunks)
  • x/tx/signing/aminojson/aminojson.go (1 hunks)
  • x/tx/signing/aminojson/json_marshal.go (9 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/tx/signing/aminojson/json_marshal.go
Additional comments: 4
x/tx/signing/aminojson/aminojson.go (1)
  • 47-47: The addition of EnumAsString: false explicitly sets enums to be encoded as numbers by default. This change is clear and aligns with the PR's objectives. However, it would be beneficial to add a comment explaining why this default is chosen, especially since the PR aims to enhance readability by allowing enums to be encoded as strings. This will help future maintainers understand the decision-making process.
x/tx/CHANGELOG.md (3)
  • 38-38: The changelog entry clearly documents the addition of the enum as string encoding option, including a reference to the PR. This entry is well-formatted and adheres to the changelog's guiding principles.
  • 42-42: The changelog entry accurately documents the restructuring of FormatCoins from core/coins to signing/textual. This entry is clear and provides the necessary context for understanding the improvement.
  • 46-46: The changelog entry concisely documents the bug fix related to rejecting denominations that contain a comma. This entry is clear and accurately reflects the nature of the bug fix.

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

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 4dbd031 and aae9611.
Files selected for processing (4)
  • x/tx/go.mod (2 hunks)
  • x/tx/go.sum (1 hunks)
  • x/tx/signing/textual/internal/testdata/e2e.json (3 hunks)
  • x/tx/signing/textual/internal/testdata/tx.json (4 hunks)
Additional comments: 10
x/tx/go.mod (1)
  • 38-40: The comment added provides clear context on why specific versions of dependencies are retained, referencing an unresolved issue. This is a good practice for maintaining clarity in decision-making regarding dependency management.
x/tx/go.sum (2)
  • 1-2: The updates to module versions for cosmossdk.io/api and cosmossdk.io/core are reflected correctly in the checksums, ensuring integrity and consistency with the go.mod file.
  • 1-1: The removal of references to buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go and buf.build/gen/go/tendermint/tendermint/protocolbuffers/go likely indicates a cleanup or update in dependency management, which is a positive change for maintaining a clean and current dependency graph.
x/tx/signing/textual/internal/testdata/tx.json (4)
  • 145-145: The change from a placeholder or previous enum value to "VOTE_OPTION_YES" aligns with the PR's objective to improve readability and clarity in query responses by using string representations of enums. This change is consistent and correctly implemented within the context of the provided JSON data.
  • 196-196: The update to "VOTE_OPTION_YES" in this instance further supports the PR's goal of enhancing the user experience by providing more descriptive and human-readable enum values in query responses. This modification is accurately reflected in the JSON structure.
  • 229-229: Similar to the previous comments, changing the enum value to "VOTE_OPTION_YES" here is in line with the PR's intention to use string representations for enums. This change is correctly applied and contributes to the overall improvement of query response readability.
  • 342-342: This final instance of updating the enum value to "VOTE_OPTION_YES" is consistent with the PR's objectives and the changes made in other parts of the JSON file. It correctly implements the desired enhancement of using string values for enums to improve readability.
x/tx/signing/textual/internal/testdata/e2e.json (3)
  • 216-216: The change from "VOTE_OPTION_ONE" to "VOTE_OPTION_YES" in the MsgVote message aligns with the PR's objective to enhance readability and clarity in query responses. This update is consistent with the intention to use string representations for enum values, making the output more understandable for users.
  • 329-329: The inclusion of detailed metadata within the MsgVote message, ending with a single ampersand "@", is an interesting choice. While it serves as a placeholder or example, ensure that such content is appropriate and intended for demonstration purposes. If this is meant to simulate real-world metadata, it's effectively demonstrating the capability to handle long strings and special characters.
  • 377-377: The cbor field contains a long string representing CBOR-encoded data. While the review focuses on JSON changes, it's important to ensure that any updates to the JSON structure are reflected in the corresponding CBOR representation if necessary. This might require additional verification or testing to ensure the CBOR data remains accurate and consistent with the JSON structure changes.

@julienrbrt julienrbrt added this pull request to the merge queue Mar 5, 2024
@julienrbrt julienrbrt removed the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Mar 5, 2024
Merged via the queue into main with commit 41f9272 Mar 5, 2024
59 of 61 checks passed
@julienrbrt julienrbrt deleted the julien/enum-as-string branch March 5, 2024 21:01
@julienrbrt
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: After 0.50.x, queries now show enum value than name
3 participants