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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

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

    • Streamlined test setup and logic for message generation and validation, enhancing code readability and maintainability.
    • Modularized custom signer logic for better maintainability.
  • Chores

    • Removed unused imports and redundant error handling to contribute to a cleaner codebase.

Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

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.
  • test(e2e/accounts): fix build #21725: This PR introduces a utility function for converting messages to a specific type, which enhances type safety and relates to the main PR's changes in handling types and their serialization processes.
  • feat(runtime(v2)): add sanity checks on store upgrades #21748: Although primarily focused on store upgrades, the introduction of checks and validation processes in this PR may indirectly relate to the overall handling of types and their integrity within the system, which is a concern in the main PR.
  • tests(math): fix zero value test #21821: This PR improves the serialization of decimal values, which is relevant to the main PR's focus on ensuring correctness in serialization and deserialization processes.

Suggested labels

C:x/genutil


Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 62f47d8 and 3e417a9.

Files selected for processing (2)
  • tests/integration/tx/aminojson/aminojson_test.go (4 hunks)
  • x/tx/signing/aminojson/encoder.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • tests/integration/tx/aminojson/aminojson_test.go
  • x/tx/signing/aminojson/encoder.go

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.

@@ -51,7 +53,12 @@ func cosmosDecEncoder(_ *Encoder, v protoreflect.Value, w io.Writer) error {
if val == "" {
return jsonMarshal(w, "0")
}
return jsonMarshal(w, val)
var dec math.LegacyDec
err := dec.Unmarshal([]byte(val))
Copy link
Member Author

Choose a reason for hiding this comment

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

Dynamic messages represent the packed Decimal value without a radix, so math marshaling (String() implementation) is needed here.

@@ -27,6 +27,8 @@ func cosmosIntEncoder(_ *Encoder, v protoreflect.Value, w io.Writer) error {
if val == "" {
return jsonMarshal(w, "0")
}
// TODO
// I think this needs a similar path as cosmosDecEncoder
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure on this, is the packed encoding equivalent?

@kocubinski kocubinski marked this pull request as ready for review September 19, 2024 21:53
Copy link
Contributor

@kocubinski your pull request is missing a changelog!

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
Comment on lines 30 to 31
// TODO
// I think this needs a similar path as cosmosDecEncoder
Copy link
Contributor

Choose a reason for hiding this comment

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

Address the TODO and implement handling in cosmosIntEncoder.

The cosmosIntEncoder function contains a TODO comment indicating that further implementation is needed. Additionally, there's a note suggesting that it may need a similar approach to cosmosDecEncoder. Please complete the implementation to ensure proper encoding of cosmos.Int types and remove the TODO comment once addressed.

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

1 participant