cold: overflow-safe math sweep (Closes #686) - #1022
Merged
greatest0fallt1me merged 1 commit intoJul 30, 2026
Merged
Conversation
) Route the hot/cold drift computation in `maybe_rebalance` through `checked_sub`/`checked_abs`, mapping overflow to `VaultError::Overflow` instead of relying on `.abs()` (which panics on i128::MIN) and raw subtraction. Completes the overflow-safe sweep of contracts/vault/src/ cold_storage.rs — no raw arithmetic or unwrap() remains in production paths. Add focused tests for every checked_* branch: share/target overflow, public-wrapper parity, total-overflow propagation, drift safety with the conservation invariant, and cold-signer membership. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@Tekh134 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: overflow-safe math sweep in cold (Closes #686)
Overview
Completes an overflow-safe math sweep of the Callora Vault's cold-storage
module (
contracts/vault/src/cold_storage.rs). Every arithmetic operation inthe cold hot/cold-split and rebalance paths now routes through Rust's
checked_*operators, returningVaultError::Overflowon any overflow insteadof relying on debug assertions or silently wrapping in release builds.
Closes: #686 ("Add overflow-safe math sweep in cold")
What changed
The module was already largely checked-math clean (
total,hot_share_bps,target_hot, and the hot→cold move inmaybe_rebalanceall usedchecked_*).The sweep closes the one remaining gap:
maybe_rebalancedrift computation. The drift between the current andtarget hot share was computed with raw
(current_share - target_share).abs().This is now
current_share.checked_sub(target_share)? .checked_abs()?,mapping any overflow to
VaultError::Overflow.checked_absadditionallyguards the
i128::MINedge case, where.abs()panics.No behaviour changes for in-range inputs — identical results, plus a defined
error return instead of a panic/wrap at the extremes.
Safety properties
unwrap()in production paths — overflow is surfaced asResult<_, VaultError>and propagated with?.hot + cold == total) is preserved by theoverflow-safe rebalance path (asserted in tests).
every state-changing action behind configured cold signers and
require_auth; this PR touches only pure arithmetic helpers and adds no newentrypoints.
Tests
Added focused unit tests alongside the existing suite in the module's
#[cfg(test)] mod tests:hot_share_bps_overflow_is_caughtchecked_muloverflow in share calctarget_hot_overflow_is_caughtchecked_muloverflow in target calctarget_hot_pub_matches_target_hotmaybe_rebalance_total_overflow_is_caughttotal()maybe_rebalance_drift_is_overflow_safeis_cold_signer_detects_membershipThese complement the pre-existing rebalance/validate tests, exercising every
checked_*branch introduced or touched by the sweep.Files changed
contracts/vault/src/cold_storage.rs