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): fix amino json drift from legacy spec #21825

Merged
merged 20 commits into from
Oct 3, 2024
Merged

Conversation

kocubinski
Copy link
Member

@kocubinski kocubinski commented Sep 19, 2024

Description

Related to: #21782

Recent changes had caused all amino JSON legacy equivalence rapid tests to early out before running. As a consequence changes to the x/tx Amino JSON encoder were not properly tested, and several issues were not caught.

They are:

  1. Field ordering was incorrect in some cases (including oneofs)
  2. Decimals were rendering incorrectly

The tests and specs for amino JSON encoding have been tightened up. The tests now closely mimic the same code paths followed in the updated TxDecoder in x/tx.


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 framework for testing transaction signing functionalities, enhancing the testing capabilities for compatibility and correctness.
  • Bug Fixes

    • Improved error handling in encoding functions to manage invalid inputs and ensure consistent output formats.
    • Updated decimal representation in tests for better precision.
  • Refactor

    • Streamlined test setup and logic for message generation and validation, enhancing code readability and maintainability.
    • Modularized custom signer logic for better maintainability.
    • Restructured encoding functions to improve flexibility and error handling.
  • Chores

    • Removed unused imports and redundant error handling to contribute to a cleaner codebase.
    • Added a new entry in the changelog for the x/tx module regarding recent fixes.

Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request includes changes across multiple files, focusing on refining type handling, serialization, and testing frameworks within the Cosmos SDK. Key modifications involve removing certain type declarations, enhancing error handling in encoding functions, and introducing a new testing fixture to streamline test setups. Additionally, the changes aim to improve the organization and maintainability of the codebase by modularizing components and optimizing existing logic.

Changes

Files Change Summary
tests/integration/rapidgen/rapidgen.go Removed GenType calls for &authtypes.ModuleAccount{} and &authapi.ModuleAccount{}; commented out GenType for &stakingtypes.StakeAuthorization{} and &stakingapi.StakeAuthorization{} due to JSON ordering issues.
tests/integration/tx/aminojson/aminojson_test.go Refactored test setup using internal.NewSigningFixture, updated test functions to utilize new methods for improved readability, and removed unused imports and redundant error handling.
tests/integration/tx/context_test.go Removed ProvideCustomGetSigners function and replaced it with a reference to internal.ProvideCustomGetSigner, enhancing modularity.
tests/integration/tx/internal/util.go Introduced a new file containing SigningFixture struct and related methods for testing transaction signing, including methods for marshaling, unmarshaling, and ensuring legacy equivalence.
x/auth/migrations/legacytx/stdtx_test.go Modified string formatting in TestStdSignBytes to represent decField with higher precision in JSON serialization.
x/tx/signing/aminojson/encoder.go Enhanced error handling in cosmosDecEncoder, restructured moduleAccountEncoder for better flexibility, and adjusted thresholdStringEncoder for consistent output format.

Possibly related PRs

  • feat(x/tx): add aminoNameAsTypeURL option in aminojson encoder #21712: The introduction of the AminoNameAsTypeURL option in the amino JSON encoder could relate to the changes in type handling and serialization processes in the main PR, as both involve enhancing how types are managed and represented in the code.
  • chore: add releases to changelog #21831: The updates to the changelog include various improvements and bug fixes, including those related to serialization and type handling, which align with the focus of the main PR on refining type management in the codebase.

Suggested labels

Type: CI, backport/v0.52.x

Suggested reviewers

  • julienrbrt
  • testinginprod
  • sontrinh16
  • aaronc

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.

@kocubinski kocubinski marked this pull request as ready for review September 19, 2024 21:53
@kocubinski kocubinski requested review from testinginprod, aaronc and a team as code owners September 19, 2024 21:53

This comment has been minimized.

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

Outside diff range and nitpick comments (2)
tests/integration/tx/internal/util.go (1)

210-210: Typographical Correction in Comment

In the comment on line 210, there is a missing word. It should read: "It is used here to ensure the x/tx JSON encoding..."

Apply this diff to fix the typo:

 // amino JSON encoding x/auth/migrations/legacytx.StdSignBytes. It is used here ensure the x/tx
+// amino JSON encoding x/auth/migrations/legacytx.StdSignBytes. It is used here to ensure the x/tx
tests/integration/tx/aminojson/aminojson_test.go (1)

145-146: Maintain consistent formatting for module listings in fixture initialization.

In TestAminoJSON_LegacyParity, the modules are listed in a compact format, whereas in TestAminoJSON_Equivalence, each module is listed on a separate line. For consistency and improved readability, consider formatting the module list in the same way in both functions.

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d6364b8 and cd607a9.

Files selected for processing (5)
  • tests/integration/rapidgen/rapidgen.go (1 hunks)
  • tests/integration/tx/aminojson/aminojson_test.go (4 hunks)
  • tests/integration/tx/context_test.go (2 hunks)
  • tests/integration/tx/internal/util.go (1 hunks)
  • x/tx/signing/aminojson/encoder.go (4 hunks)
Additional context used
Path-based instructions (5)
tests/integration/rapidgen/rapidgen.go (2)

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"

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"

tests/integration/tx/context_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"

tests/integration/tx/internal/util.go (2)

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"

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

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

Additional comments not posted (15)
tests/integration/tx/context_test.go (3)

10-10: LGTM!

Moving the custom signer logic to a separate internal package is a good refactoring decision. It improves code organization and maintainability.


14-14: LGTM!

Importing the require package from testify is a standard practice for writing assertions in Go tests. It will help in validating the expected behavior of the code under test.


29-29: LGTM!

Providing the internal.ProvideCustomGetSigner function to the dependency injection container is the correct way to make the custom signer logic available for the tests after the refactoring. It ensures that the tests can access the custom signer functionality.

tests/integration/rapidgen/rapidgen.go (2)

262-264: Skipping review of the comment.

The comment provides useful context about a pending fix in a separate pull request. The TODO comment is a good practice to track pending tasks. No changes are required for this comment in this pull request.


262-264: Skipping review of the commented out code.

The code segment is intentionally commented out due to a pending fix in a separate pull request. Commenting out the code is a good practice to avoid breaking changes until the fix is merged. No changes are required for this code segment in this pull request.

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

56-61: Correctly handle decimal unmarshalling in cosmosDecEncoder.

The code properly unmarshals the decimal value using dec.Unmarshal([]byte(val)) and marshals it back to a string representation. This ensures that decimal values are accurately processed and enhances the robustness of the encoder.


136-140: Fields added to moduleAccountPretty are consistent with JSON output.

The inclusion of Address and PubKey fields with appropriate JSON tags ensures that all necessary account information is included in the serialized JSON output. This aligns with the expected structure for module accounts.


146-152: Proper type assertion in moduleAccountEncoder.

The code correctly asserts that the provided msg is of type authapi.ModuleAccount by comparing the full names of the descriptors. This improves type safety and prevents potential runtime errors due to incorrect message types.


154-156: Initialize PubKey and populate Name in moduleAccountPretty.

The PubKey field is initialized as an empty string, and the Name field is correctly retrieved from the message fields. This ensures that the JSON output includes these fields, even if the public key is not set.


162-168: Extract base account information if available.

The code correctly checks for the presence of base_account and extracts Address, AccountNumber, and Sequence fields. This enhances flexibility and prevents potential nil pointer dereferences when the base account is not set.


189-191: Initialize variables in thresholdStringEncoder.

The necessary variables pk, msgDesc, and fields are properly initialized to process the multisig public key message. This sets the foundation for accurate serialization of multisig keys.


199-201: Write opening of JSON object and handle errors properly.

The code correctly writes the beginning of the JSON object with {"pubkeys": and checks for potential I/O errors. This ensures that any write failures are promptly handled.


203-213: Ensure correct JSON serialization of pubkeys.

The function properly handles the case where pubkeys is empty by writing an empty array []. When pubkeys are present, it serializes the list using enc.marshalList, ensuring accurate JSON representation.


215-216: Serialize threshold field as a string in JSON output.

The code appends the threshold value as a string to the JSON output using fmt.Fprintf. Serializing numeric values as strings maintains compatibility with legacy amino JSON encoding requirements.

tests/integration/tx/aminojson/aminojson_test.go (1)

99-99: Use t.Logf instead of fmt.Printf for test logging.

In test code, it's recommended to use t.Logf for logging instead of fmt.Printf. This ensures that log messages are properly associated with the test and can be filtered or formatted by the testing framework.

Apply this diff to make the change:

-	fmt.Printf("testing %s\n", tt.Pulsar.ProtoReflect().Descriptor().FullName())
+	t.Logf("testing %s", tt.Pulsar.ProtoReflect().Descriptor().FullName())

Likely invalid or redundant comment.

Comment on lines 36 to 39
testMsg := msg.(*testpb.TestRepeatedFields)
// arbitrary logic
signer := testMsg.NullableDontOmitempty[1].Value
return [][]byte{[]byte(signer)}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Prevent Potential Index Out of Range Panic in ProvideCustomGetSigner

In the Fn function (lines 35-40), accessing testMsg.NullableDontOmitempty[1] at line 38 without checking the slice length may cause an index out of range panic if the slice has fewer than two elements. To ensure robustness, add a check to confirm that testMsg.NullableDontOmitempty has at least two elements before accessing.

Apply this diff to add the safety check:

 func (msg proto.Message) ([][]byte, error) {
 	testMsg := msg.(*testpb.TestRepeatedFields)
+	if len(testMsg.NullableDontOmitempty) <= 1 {
+		return nil, fmt.Errorf("NullableDontOmitempty must have at least two elements")
+	}
 	// arbitrary logic
 	signer := testMsg.NullableDontOmitempty[1].Value
 	return [][]byte{[]byte(signer)}, nil
 }
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
testMsg := msg.(*testpb.TestRepeatedFields)
// arbitrary logic
signer := testMsg.NullableDontOmitempty[1].Value
return [][]byte{[]byte(signer)}, nil
testMsg := msg.(*testpb.TestRepeatedFields)
if len(testMsg.NullableDontOmitempty) <= 1 {
return nil, fmt.Errorf("NullableDontOmitempty must have at least two elements")
}
// arbitrary logic
signer := testMsg.NullableDontOmitempty[1].Value
return [][]byte{[]byte(signer)}, nil

tests/integration/tx/internal/util.go Show resolved Hide resolved
x/tx/signing/aminojson/encoder.go Outdated Show resolved Hide resolved
Comment on lines +157 to +160
permissions := msg.Get(fields.ByName("permissions")).List()
for i := 0; i < permissions.Len(); i++ {
pretty.Permissions = append(pretty.Permissions, permissions.Get(i).String())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Preallocate Permissions slice to improve performance.

Since the length of permissions is known, consider preallocating the Permissions slice to avoid multiple allocations during the append operations.

Apply this diff to preallocate the slice:

 permissions := msg.Get(fields.ByName("permissions")).List()
+pretty.Permissions = make([]string, 0, permissions.Len())
 for i := 0; i < permissions.Len(); i++ {
     pretty.Permissions = append(pretty.Permissions, permissions.Get(i).String())
 }
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
permissions := msg.Get(fields.ByName("permissions")).List()
for i := 0; i < permissions.Len(); i++ {
pretty.Permissions = append(pretty.Permissions, permissions.Get(i).String())
}
permissions := msg.Get(fields.ByName("permissions")).List()
pretty.Permissions = make([]string, 0, permissions.Len())
for i := 0; i < permissions.Len(); i++ {
pretty.Permissions = append(pretty.Permissions, permissions.Get(i).String())
}

tests/integration/tx/aminojson/aminojson_test.go Outdated Show resolved Hide resolved
@julienrbrt julienrbrt added this pull request to the merge queue Oct 3, 2024
@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Oct 3, 2024
@julienrbrt
Copy link
Member

adding backport label to 0.50 to release 0.13.x.
On main i'll prepare x/tx v1.0.0-alpha.1

@julienrbrt
Copy link
Member

oh I need to add the backport 0.52 label as well to backport the testing suite changes.

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Oct 3, 2024
Merged via the queue into main with commit 2d40cc1 Oct 3, 2024
71 of 73 checks passed
@julienrbrt julienrbrt deleted the kocu/amino-json-tests branch October 3, 2024 11:26
mergify bot pushed a commit that referenced this pull request Oct 3, 2024
(cherry picked from commit 2d40cc1)

# Conflicts:
#	tests/integration/tx/aminojson/aminojson_test.go
#	x/tx/CHANGELOG.md
#	x/tx/signing/aminojson/encoder.go
@mergify mergify bot mentioned this pull request Oct 3, 2024
12 tasks
mergify bot pushed a commit that referenced this pull request Oct 3, 2024
(cherry picked from commit 2d40cc1)

# Conflicts:
#	tests/integration/tx/aminojson/aminojson_test.go
#	x/auth/migrations/legacytx/stdtx_test.go
#	x/tx/CHANGELOG.md
julienrbrt added a commit that referenced this pull request Oct 3, 2024
Co-authored-by: Matt Kocubinski <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
yihuang added a commit to yihuang/cosmos-sdk that referenced this pull request Oct 14, 2024
(cherry picked from commit 2d40cc1)

fix half conflicts and revert tests

conflicts
yihuang added a commit to crypto-org-chain/cosmos-sdk that referenced this pull request Oct 14, 2024
* reset to upstream

* feat(x/tx): support bytes field as signer (cosmos#21850)

(cherry picked from commit dbf6cdb)

* fix(x/tx): fix amino json drift from legacy spec (cosmos#21825)

(cherry picked from commit 2d40cc1)

fix half conflicts and revert tests

conflicts

* changelog
@coderabbitai coderabbitai bot mentioned this pull request Oct 25, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/auth C:x/tx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants