Closes #48 Implemented: replace silent underflow clamp with typed panic in release_milestone_multi_asset asset_raised decrement#55
Open
Promise278 wants to merge 3 commits into
Conversation
… underflow with .unwrap_or(0).max(0) should panic with typed Error
Contributor
|
Typed panic on underflow is the right move for #48, but two things: (1) the two new files |
Contributor
|
Hey @Promise278 👋 — the typed |
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.
Fixes an inconsistent underflow handling pattern in
release_milestone_multi_asset where a silent .unwrap_or(0).max(0)
clamp on the asset_raised decrement could mask a real ledger
invariant violation — while the surrounding code in the same
function correctly panics with a typed error on underflow.
Problem
In multi_asset_release.rs lines 188-189, the per-asset raised
counter is decremented using a silent clamp:
let new_asset_raised = asset_raised
.checked_sub(clamped_release)
.unwrap_or(0) // silent on underflow
.max(0); // second clamp, same intent
This is inconsistent with the typed-error contract established
by the release_amount path just above it in the same function:
let milestone_release = milestone.target_amount
.checked_sub(milestone.released_amount)
.unwrap_or_else(|| panic_with_error!(env, Error::MilestoneReleasedExceedsTarget));
One underflow path panics loudly with a typed error.
The other collapses silently to 0.
What Changed
campaign/src/multi_asset_release.rs
Replaced .unwrap_or(0).max(0) with a typed panic
that mirrors the rest of the function:
let new_asset_raised = asset_raised
.checked_sub(clamped_release)
.unwrap_or_else(|| panic_with_error!(env, Error::LedgerUnderflow));
Reconciled storage_set_total_raised in the surrounding
loop with the same typed-panic approach for consistency
campaign/src/types.rs
following the existing numbered scheme) so the cause
is unmistakable in logs and distinguishable from
the generic Error::Overflow attribution
campaign/src/test/release_milestone_tests.rs
injecting funds above the recorded asset_raised ledger
instead of silently collapsing to 0
Invariant Being Enforced
asset_raised >= clamped_release must always hold
by construction before the decrement runs.
If it does not hold, the contract has encountered
a state it cannot safely reason about. Surfacing it
loudly as a typed panic is correct — silent correction
via clamp is not.
How to Verify
cargo test