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(client): don't marshal zero-ed fields #22723

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Dec 2, 2024

Description

Due to the new additions of two new fields in the tx body: unordered and timeout, v0.52 clients aren't compatible with lower version anymore.

While always checking unknown fields is crucial, the client library of the SDK doesn't need to marshal all fields of the tx.
Meaning, a tx where unordered and/or timeout are set, will fail in 0.50 with a v0.52 client, when you don't make use of that feature, you should still be able to use the v0.52 client.

This PR fixes that by overriding the json encoder.

HOWEVER, there's two way to do it, and we need to pick one:

This PR took option 1, but maybe option 2 is better?
When we migrate to client/v2/tx that won't be required, as we use the other proto marshaller.

It should however be tested by the IBC team first (cc @chatton @damiannolan). I have cherry-picked this for 0.52 in this branch: https://github.com/cosmos/cosmos-sdk/tree/julien/loosen-codec-052


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 a new function for JSON encoding that skips empty fields to optimize data transmission.
    • Added a custom JSON marshaling method for better handling of protobuf messages.
  • Improvements

    • Enhanced transaction encoding to prevent unpopulated fields from being emitted.
    • Improved error handling for auxiliary signer data.
  • Bug Fixes

    • Removed unnecessary interface, simplifying the codec structure without affecting functionality.
  • Documentation

    • Updated comments to clarify changes and their impacts on compatibility across SDK versions.

@julienrbrt julienrbrt requested review from JulianToledano and a team as code owners December 2, 2024 20:47
Copy link
Contributor

coderabbitai bot commented Dec 2, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces several modifications across multiple files to enhance transaction handling and encoding processes. Key changes include the addition of a custom JSON marshaling method, updates to transaction encoding options to prevent unpopulated fields, and the removal of an interface to simplify the codec structure. These adjustments aim to improve compatibility and error handling within the transaction system while maintaining existing functionalities.

Changes

File Change Summary
client/tx/factory.go Added a comment // DITTO next to the encoder assignment in the BuildSimTx method.
client/tx/tx.go Added imports for gogoproto, codec, and codectypes. Updated BroadcastTx to use authtx.DefaultJSONTxEncoder. Modified makeAuxSignerData for error handling. Introduced clientProtoCodec type with custom JSON marshaling.
client/v2/tx/encoder.go Updated jsonMarshalOptions to include EmitUnpopulated: false to prevent unpopulated fields in JSON serialization.
codec/codec.go Commented out the mustEmbedCodec() method in the Codec interface, removing the requirement for implementations to embed an official codec.
codec/json.go Added ProtoMarshalJSONSkipEmpty function for encoding Proto3 messages while omitting empty fields.
codec/proto_codec.go Removed the ProtoCodecMarshaler interface, simplifying the codec structure while retaining the functionality of the ProtoCodec struct.

Possibly related PRs

Suggested labels

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

Suggested reviewers

  • kocubinski
  • testinginprod

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

github-actions bot commented Dec 2, 2024

@julienrbrt your pull request is missing a changelog!

@julienrbrt julienrbrt marked this pull request as draft December 2, 2024 20:47
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 (2)
client/tx/tx.go (1)

137-137: Typo in comment: Change 'accross' to 'across'

There's a typo in the comment on line 137; 'accross' should be 'across'.

Apply this diff to correct the typo:

-// It helps the client to be compatible accross SDK versions.
+// It helps the client to be compatible across SDK versions.
🧰 Tools
🪛 golangci-lint (1.62.2)

137-137: accross is a misspelling of across

(misspell)

client/v2/tx/encoder.go (1)

26-26: Typo in comment: Change 'accross' to 'across'

There's a typo in the comment on line 26; 'accross' should be 'across'.

Apply this diff to correct the typo:

-// It helps the client to be compatible accross SDK versions.
+// It helps the client to be compatible across SDK versions.
🧰 Tools
🪛 golangci-lint (1.62.2)

26-26: accross is a misspelling of across

(misspell)

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9d9c19c and cde6edb.

📒 Files selected for processing (6)
  • client/tx/factory.go (1 hunks)
  • client/tx/tx.go (3 hunks)
  • client/v2/tx/encoder.go (1 hunks)
  • codec/codec.go (1 hunks)
  • codec/json.go (1 hunks)
  • codec/proto_codec.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • client/tx/factory.go
🧰 Additional context used
📓 Path-based instructions (5)
client/v2/tx/encoder.go (1)

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

codec/codec.go (1)

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

codec/proto_codec.go (1)

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

codec/json.go (1)

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

client/tx/tx.go (1)

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

🪛 golangci-lint (1.62.2)
client/v2/tx/encoder.go

26-26: accross is a misspelling of across

(misspell)

client/tx/tx.go

137-137: accross is a misspelling of across

(misspell)

🔇 Additional comments (6)
client/tx/tx.go (3)

12-12: LGTM: Necessary imports added

The added imports are appropriate and necessary for the new functionalities introduced in the code.

Also applies to: 17-18, 25-25


136-149: LGTM: Custom encoder implementation to skip unpopulated fields

The introduction of the custom encoder using clientProtoCodec to avoid emitting unpopulated fields enhances compatibility across SDK versions. The fallback mechanism to the default encoder ensures robustness in case of any issues with the custom encoder.

🧰 Tools
🪛 golangci-lint (1.62.2)

137-137: accross is a misspelling of across

(misspell)


456-468: LGTM: Implementation of clientProtoCodec and MarshalJSON method

The clientProtoCodec struct and its MarshalJSON method are correctly implemented to marshal messages while skipping empty fields. This aligns with the goal of improving compatibility and reducing unnecessary data in transaction encoding.

client/v2/tx/encoder.go (1)

25-29: LGTM: Setting EmitUnpopulated to false

Setting EmitUnpopulated: false in jsonMarshalOptions appropriately configures the JSON marshaling to omit unpopulated fields. This change enhances compatibility across SDK versions by preventing the inclusion of zero-value fields.

🧰 Tools
🪛 golangci-lint (1.62.2)

26-26: accross is a misspelling of across

(misspell)

codec/codec.go (1)

44-44: Verify the impact of commenting out mustEmbedCodec()

By commenting out mustEmbedCodec() in the Codec interface, the requirement for implementations to embed an official codec is removed. Please verify that this modification does not affect backward compatibility or lead to unintended side effects in codec implementations.

codec/proto_codec.go (1)

24-24: Confirm the removal of ProtoCodecMarshaler interface

The removal of the ProtoCodecMarshaler interface simplifies the codec structure. Ensure that this change does not impact any existing components that may rely on this interface and that all necessary functionalities are preserved.

Comment on lines +40 to +41
jm := defaultJM
jm.EmitDefaults = false
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

Avoid modifying the shared defaultJM variable

Modifying the shared defaultJM variable by changing its EmitDefaults field can introduce unintended side effects in other parts of the code that rely on defaultJM. Instead, create a new instance of jsonpb.Marshaler with the desired settings.

Apply this diff to fix the issue:

 func ProtoMarshalJSONSkipEmpty(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error) {
-	jm := defaultJM
-	jm.EmitDefaults = false
+	jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: nil}
 	if resolver != nil {
 		jm = &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: resolver}
 	}
📝 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
jm := defaultJM
jm.EmitDefaults = false
jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: nil}
if resolver != nil {
jm = &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: resolver}
}

@julienrbrt
Copy link
Member Author

Turned out that didn't work. I'll check another way.

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.

3 participants