-
Notifications
You must be signed in to change notification settings - Fork 107
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
chore(primitives): hardened GweiFromWei
to avoid integer truncation
#2080
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes made in this pull request involve modifications to the Changes
Possibly related PRs
Suggested reviewers
Poem
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 (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2080 +/- ##
==========================================
+ Coverage 22.23% 22.26% +0.02%
==========================================
Files 356 356
Lines 16022 16028 +6
Branches 12 12
==========================================
+ Hits 3563 3569 +6
Misses 12306 12306
Partials 153 153
|
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: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (2)
- mod/primitives/pkg/math/u64.go (2 hunks)
- mod/primitives/pkg/math/u64_test.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (6)
mod/primitives/pkg/math/u64.go (3)
27-27
: Import statement addedThe addition of
"github.com/berachain/beacon-kit/mod/errors"
is appropriate for enhanced error handling.
140-140
: Define custom overflow errorDefining
ErrGweiOverflow
provides a clear and specific error for overflow conditions, improving error clarity.
143-143
: Ensure callers handle the updatedGweiFromWei
function signatureWith the change to
GweiFromWei
returning an additionalerror
, verify that all calls to this function in the codebase have been updated to handle the error appropriately.Run the following script to identify calls to
GweiFromWei
and check for proper error handling:✅ Verification successful
All callers of
GweiFromWei
correctly handle the returned error.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Find all calls to GweiFromWei and display them with context. # Expected: Callers should handle the error returned by GweiFromWei. rg --type go 'GweiFromWei\(' -A 2Length of output: 667
mod/primitives/pkg/math/u64_test.go (3)
331-334
: Enhanced test input flexibility with function-based inputsChanging the
input
field tofunc(t *testing.T) *big.Int
allows for dynamic generation of test inputs, which enhances the flexibility and readability of the test cases. This is a good practice for preparing complex inputs and ensures that test setup is clear and maintainable.
385-390
: Consistent use ofbig.Int
operations for large valuesIn the "max uint64 wei" test case, you're correctly using
new(big.Int).Mul
withbig.NewInt
andSetUint64
to handle large values without overflow. This ensures the test accurately represents the maximum uint64 value in Gwei.
399-405
: Proper error handling and result verification in testsThe test logic correctly checks for expected errors using
require.ErrorIs
when an error is anticipated, and verifies the result withrequire.Equal
when no error is expected. This ensures that both successful and error scenarios are properly validated.
A Gwei amount larger than
(2**64) * (10**9)
or negative would not be representable asuint64
, but currently would be truncated rather than rejected.This should not happen but we still want to guard against a serialization bug or other mishap.
This PR fixes this.
Summary by CodeRabbit
New Features
GweiFromWei
function now includes error handling for overflow conditions.Bug Fixes
GweiFromWei
to cover invalid inputs and error scenarios.Documentation