-
Notifications
You must be signed in to change notification settings - Fork 197
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(ibc-hooks): improved UnmarshalJSON validation & error handling #324
base: main
Are you sure you want to change the base?
feat(ibc-hooks): improved UnmarshalJSON validation & error handling #324
Conversation
Descritpion ----------- Improves AsyncCallback JSON unmarshaling with: - Better validation for ModuleAddress format and required fields - More specific error messages - Memory optimization using single intermediate struct - Comprehensive test coverage for error cases Tetsing the introduced `feat` ----------------------------- Fetch this PR branch and from the root directory run: ``` go test ./x/ibc-hooks/move-hooks/message_test.go -v ```
📝 Walkthrough📝 WalkthroughWalkthroughThe pull request focuses on improving the JSON unmarshaling process for the Changes
Assessment against linked issues
Poem
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🔇 Additional comments (7)x/ibc-hooks/move-hooks/message.go (4)
The
The validation sequence is well-structured:
Each validation step provides specific error messages that will help users identify and fix issues quickly.
The ID handling logic effectively addresses the previous overflow concerns by:
The field assignment is performed only after all validations pass, preventing partial updates in error cases. x/ibc-hooks/move-hooks/message_test.go (3)
The test cases effectively cover both numeric and string ID formats with clear assertions and good use of subtests for organization.
The test suite thoroughly covers validation error cases including:
Each test properly asserts the specific error message expected.
Excellent coverage of numeric edge cases including:
The tests ensure robust handling of all potential numeric input scenarios. 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
CodeRabbit Configuration File (
|
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: 1
🧹 Nitpick comments (2)
x/ibc-hooks/move-hooks/message.go (2)
58-62
: Consider avoiding interface{} for the Id field.
While using interface{} is convenient for handling different JSON number/string representations, it can degrade type safety and clarity. You may want to consider a custom unmarshal approach (e.g., using json.Number) or a well-defined union type to avoid frequent type assertions.
86-98
: String ID parsing approach is valid, but consider using strconv for clarity.
Unmarshaling the string into a float64 (line 93) is clever but slightly indirect. A direct call to strconv.ParseUint or a check with json.Number could offer a clearer path to integer conversion, avoiding potential edge cases with float representations.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
x/ibc-hooks/move-hooks/message.go
(2 hunks)x/ibc-hooks/move-hooks/message_test.go
(1 hunks)
🔇 Additional comments (8)
x/ibc-hooks/move-hooks/message.go (2)
5-6
: Imports look good for format validation.
The addition of "strings" alongside "fmt" solidly supports the new validation checks for the "module_address" prefix requirement.
101-102
: Overall unmarshal logic is solid.
The final assignment of ModuleAddress and ModuleName is straightforward, and the new validations capture common mistakes effectively. The approach aligns well with the sub-tests added in message_test.go. Nice work!
x/ibc-hooks/move-hooks/message_test.go (6)
12-25
: Great coverage for numeric ID.
These test assertions properly verify that numeric IDs are unmarshaled as expected.
27-40
: Well-structured test for string ID.
Testing a string-based ID ensures the newly introduced logic is exercised. The coverage here is thorough.
42-73
: Robust validation for empty or invalid fields.
Covering empty module address/name and invalid module address format thoroughly confirms the new defensive checks.
75-84
: Properly handles unexpected ID types.
Verifying an invalid boolean ID ensures that erroneous JSON inputs are flagged.
86-95
: String ID format errors are correctly caught.
This sub-test ensures that non-numeric strings raise a parsing error, in line with the code’s intended behavior.
97-102
: Malformed JSON test is well implemented.
This confirms the parser gracefully fails on structurally invalid input, further enhancing confidence in the unmarshal logic.
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
🧹 Nitpick comments (2)
x/ibc-hooks/move-hooks/message.go (2)
58-62
: Well-structured intermediate type for JSON unmarshaling.Good use of an intermediate type with
interface{}
for flexible ID handling. This approach effectively separates the concerns of JSON parsing from the final validated type.Consider documenting the supported ID formats in the struct's documentation to help API consumers understand the expected input formats.
73-84
: Consider enhancing module address validation.While the current validation checks for the "0x" prefix, consider adding additional checks for:
- Valid hexadecimal characters after the "0x" prefix
- Specific length requirements for the address
Example enhancement:
if !strings.HasPrefix(ic.ModuleAddress, "0x") { return fmt.Errorf("invalid module_address format: must start with '0x'") } +// Remove "0x" prefix and validate hex string +addr := ic.ModuleAddress[2:] +for _, c := range addr { + if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { + return fmt.Errorf("invalid module_address: contains non-hexadecimal characters") + } +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
x/ibc-hooks/move-hooks/message.go
(2 hunks)x/ibc-hooks/move-hooks/message_test.go
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- x/ibc-hooks/move-hooks/message_test.go
🔇 Additional comments (2)
x/ibc-hooks/move-hooks/message.go (2)
5-6
: LGTM: Appropriate imports added for enhanced error handling.
The addition of "fmt" and "strings" packages aligns well with the improved error handling and string validation requirements.
86-103
: Optimize ID parsing and verify edge cases.
The ID parsing logic is thorough but has some redundancy in the string case. Consider:
- The overflow check is duplicated between the float64 and string cases
- The string case could potentially use
strconv.ParseUint
for direct parsing
Let's verify the handling of edge cases:
Description ----------- - Added overflow checking for uint64 ID conversion - Added validation for decimal and negative values - Added comprehensive test cases for edge cases
31ce042
to
e556df8
Compare
Description
Closes: #317
Improves AsyncCallback JSON unmarshaling with:
Testing the introduced
feat
Fetch this PR branch and from the root directory run:
go test ./x/ibc-hooks/move-hooks/message_test.go -v
Author Checklist
!
in the type prefix if API or client breaking change