-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
test(x/tx): add amino JSON pretty bytes demonstration test #21935
Conversation
📝 WalkthroughWalkthroughThe pull request includes changes to several files, primarily focusing on the removal of the Changes
Possibly related PRs
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
x/tx/signing/aminojson/json_marshal_test.go (1)
437-442
: Consider adding error handling for the custom encoding functionThe custom field encoding function for "hex" is concise and clear. However, it might be beneficial to add error handling for the
Fprintf
call, even though it's unlikely to fail for byte slices.Consider updating the custom encoding function as follows:
encoder.DefineFieldEncoding( "hex", func(enc *aminojson.Encoder, v protoreflect.Value, w io.Writer) error { - _, err := fmt.Fprintf(w, "\"%x\"", v.Bytes()) - return err + n, err := fmt.Fprintf(w, "\"%x\"", v.Bytes()) + if err != nil { + return err + } + if n == 0 { + return fmt.Errorf("no bytes written") + } + return nil })
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
📒 Files selected for processing (4)
- buf.work.yaml (0 hunks)
- x/tx/signing/aminojson/internal/testpb/test.proto (1 hunks)
- x/tx/signing/aminojson/internal/testpb/test.pulsar.go (16 hunks)
- x/tx/signing/aminojson/json_marshal_test.go (2 hunks)
💤 Files not reviewed due to no reviewable changes (1)
- buf.work.yaml
🧰 Additional context used
📓 Path-based instructions (2)
x/tx/signing/aminojson/internal/testpb/test.pulsar.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_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"
🔇 Additional comments (18)
x/tx/signing/aminojson/internal/testpb/test.proto (3)
48-48
: LGTM: New fieldpretty_bytes
added correctlyThe addition of the
pretty_bytes
field with theamino.encoding
option set to "hex" is well-implemented. This change aligns with the PR objective of demonstrating a "pretty" bytes encoder for arbitrary bytes. The field is correctly placed at the end of the message (field number 20), which maintains backward compatibility.
35-47
: Formatting improvements enhance readabilityThe formatting changes in these lines improve the overall readability and consistency of the Protocol Buffer definition. The adjustments in spacing make the field definitions more uniform and easier to scan. These changes align with good coding practices for
.proto
files.
35-48
: Changes align well with PR objectivesThe modifications in this file, particularly the addition of the
pretty_bytes
field and the formatting improvements, successfully address the PR's objective of demonstrating how to set up a "pretty" bytes encoder for arbitrary bytes. The use of theamino.encoding
option with "hex" value provides a clear example of how to achieve the desired encoding. These changes contribute to enhancing the functionality and readability of the amino JSON encoding process within the Cosmos SDK.x/tx/signing/aminojson/json_marshal_test.go (2)
419-447
: LGTM: Well-structured test demonstrating custom byte encodingThis test function effectively demonstrates the use of custom byte encoding for the
ABitOfEverything
message. It compares the default encoding with a custom hex encoding, showcasing the flexibility of theaminojson.Encoder
.The test structure follows Go best practices, using
require
assertions for clear error reporting. The use of SHA-256 for generating test bytes is a good choice for reproducibility.
443-446
: LGTM: Effective comparison of legacy and custom encoding resultsThe comparison between legacy JSON and custom amino JSON encoding is well-implemented. The use of
require.NotEqual
ensures that the custom encoding produces different results from the legacy encoding, which is the expected behavior.The logging of hex-encoded bytes is helpful for debugging and understanding the output.
x/tx/signing/aminojson/internal/testpb/test.pulsar.go (13)
1758-1775
: Field Descriptor forPrettyBytes
Added CorrectlyThe declaration and initialization of
fd_ABitOfEverything_pretty_bytes
ensure that thePrettyBytes
field is properly integrated into the protobuf reflection mechanism.Also applies to: 1797-1797
1961-1966
: Inclusion ofPrettyBytes
inRange
MethodThe
Range
method correctly handles thePrettyBytes
field, allowing it to be iterated over alongside other populated fields.
2014-2015
: Accurate Implementation ofHas
forPrettyBytes
The
Has
method correctly checks thePrettyBytes
field for non-zero length, ensuring accurate presence detection.
2064-2065
: Proper Clearing ofPrettyBytes
inClear
MethodThe
Clear
method appropriately setsPrettyBytes
tonil
, effectively clearing its value.
2133-2135
: Correct Retrieval inGet
Method forPrettyBytes
The
Get
method accurately retrieves the value ofPrettyBytes
, returning it as aprotoreflect.ValueOfBytes
.
2190-2191
: Appropriate Assignment inSet
Method forPrettyBytes
The
Set
method correctly assigns the provided bytes value to thePrettyBytes
field.
2251-2252
: Expected Panic inMutable
Method forPrettyBytes
The
Mutable
method correctly panics when called for the non-compositePrettyBytes
field, consistent with the handling of similar scalar fields.
2300-2301
:NewField
Method Returns Default forPrettyBytes
The
NewField
method properly returns a defaultnil
value for thePrettyBytes
field, aligning with expected behavior.
2426-2429
: Inclusion ofPrettyBytes
in Size CalculationThe size computation within
ProtoMethods
accurately accounts for thePrettyBytes
field, ensuring correct message sizing.
2459-2467
: Correct Serialization inMarshal
Method forPrettyBytes
The
Marshal
method properly serializes thePrettyBytes
field, handling length encoding and field numbering correctly.
3018-3051
: Accurate Deserialization inUnmarshal
Method forPrettyBytes
The
Unmarshal
method correctly deserializes thePrettyBytes
field, including handling of length prefixes and data copying.
4265-4265
: Addition ofPrettyBytes
Field toABitOfEverything
StructThe new
PrettyBytes []byte
field is correctly added to theABitOfEverything
struct with appropriate protobuf tags and JSON annotations.
4400-4405
: Correct Implementation ofGetPrettyBytes
MethodThe
GetPrettyBytes
accessor method is properly implemented, providing safe access to thePrettyBytes
field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK
Description
Related: #21850, #15740
This adds a test showing how a developer could set up a "pretty" bytes encoder for arbitrary bytes, which are now supported as a signer field. The default is
base64
encoding, as this is theencoding/json
std implementation spec.Also fixed a problem in the buf work file, as this was causing the error below when regenerating test pbs.
Test output with the hex encoder:
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...
!
in the type prefix if API or client breaking changeCHANGELOG.md
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...
Summary by CodeRabbit
New Features
pretty_bytes
to theABitOfEverything
message for enhanced data handling.pretty_bytes
field, allowing for hexadecimal string formatting.Bug Fixes
test.proto
file.Tests
ABitOfEverything
message with the new field.