fix: limit possible underflows/overflows - #7459
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThe PR adds checked and saturating arithmetic across beacon, chain, RPC, storage, and state paths. It introduces fallible beacon round calculations, explicit negative-height validation, shared hexadecimal parsing, and regression tests for boundary and overflow cases. ChangesBeacon round calculations
Epoch and boundary validation
Shared hexadecimal parsing
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
da43e98 to
dcae0db
Compare
5344a31 to
b8dfa6f
Compare
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (2)
src/beacon/mock_beacon.rs (1)
53-54: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd context to the epoch conversion error.
u64::try_from(fil_epoch)?returns a generic conversion error. Add context that identifies the invalid mock-beacon epoch.As per coding guidelines, add context with
.context()when errors occur.Proposed fix
- Ok(u64::try_from(fil_epoch)?) + u64::try_from(fil_epoch).context("mock beacon epoch must be non-negative")🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/beacon/mock_beacon.rs` around lines 53 - 54, Update the epoch conversion in the mock beacon method returning anyhow::Result<u64> to attach context with .context() before propagating the error, identifying the invalid fil_epoch value while preserving the successful u64 conversion result.Source: Coding guidelines
src/state_manager/chain_rand.rs (1)
129-129: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd context to the beacon-round error.
Line 129 returns the raw error. Add
.context()with the epoch before?. This makes failures actionable across the state-manager and RPC call paths.As per coding guidelines, use
anyhow::Result<T>for most operations and add context with.context()when errors occur.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/state_manager/chain_rand.rs` at line 129, Update the error propagation in the beacon round lookup within the surrounding epoch-processing function by adding anyhow context containing the current epoch before the `?` operator. Preserve the existing `max_beacon_round_for_epoch` call and return behavior while making the failure context available to state-manager and RPC callers.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/beacon/drand.rs`:
- Around line 136-140: Validate that the `entry.round()` returned by
`curr_beacon.entry(cur)` matches `cur` before decrementing it. Reject mismatched
rounds, including `cur + 1`, and retain the existing `checked_sub(1)` handling
for a matching round.
In `@src/chain_sync/validation.rs`:
- Around line 307-309: Replace the saturating arithmetic used to compute
expected in the timestamp validation with checked multiplication and addition,
rejecting validation when either operation overflows before comparing the block
timestamp. Extend the related regression test to use timestamp u64::MAX and
assert that the block is rejected.
In `@src/rpc/methods/eth/filter/mod.rs`:
- Around line 684-688: Update the min_height validation before the range-walk
branches to require min_height >= 0, removing acceptance of the -1 sentinel. Add
a regression test alongside the existing negative-height test covering
from_block_number(-1) and verify it is rejected.
In `@src/rpc/methods/state.rs`:
- Around line 2254-2259: In the beacon-entry wait flow, update the genesis
timestamp conversion before calling beacon_entry_wait to use i64::try_from(...),
adding the requested context message and propagating conversion failure.
Preserve the existing wait calculation while rejecting u64 timestamps that
cannot be represented as i64.
---
Nitpick comments:
In `@src/beacon/mock_beacon.rs`:
- Around line 53-54: Update the epoch conversion in the mock beacon method
returning anyhow::Result<u64> to attach context with .context() before
propagating the error, identifying the invalid fil_epoch value while preserving
the successful u64 conversion result.
In `@src/state_manager/chain_rand.rs`:
- Line 129: Update the error propagation in the beacon round lookup within the
surrounding epoch-processing function by adding anyhow context containing the
current epoch before the `?` operator. Preserve the existing
`max_beacon_round_for_epoch` call and return behavior while making the failure
context available to state-manager and RPC callers.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ac8dd4e2-b749-46df-812c-3b6b1193a065
📒 Files selected for processing (22)
src/beacon/drand.rssrc/beacon/mock_beacon.rssrc/beacon/tests/drand.rssrc/blocks/header.rssrc/chain/mod.rssrc/chain/store/chain_store.rssrc/chain/store/errors.rssrc/chain/store/index.rssrc/chain/tests.rssrc/chain_sync/validation.rssrc/db/car/forest/index/mod.rssrc/fil_cns/weight.rssrc/lotus_json/mod.rssrc/rpc/methods/beacon.rssrc/rpc/methods/chain.rssrc/rpc/methods/eth/filter/mod.rssrc/rpc/methods/f3/types.rssrc/rpc/methods/state.rssrc/state_manager/chain_rand.rssrc/state_manager/message_search.rssrc/utils/encoding/hex.rssrc/wallet/subcommands/wallet_cmd.rs
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
filecoin-project/lotus(manual)
24dcb51 to
804d0cc
Compare
804d0cc to
b2a066b
Compare
Summary of changes
Changes introduced in this pull request:
Reference issue to close (if applicable)
Closes
Other information and links
Change checklist
Outside contributions
Summary by CodeRabbit