fix(chain): validate the total amount when constraining a value balance#10817
Open
arya2 wants to merge 1 commit into
Open
fix(chain): validate the total amount when constraining a value balance#10817arya2 wants to merge 1 commit into
arya2 wants to merge 1 commit into
Conversation
`ValueBalance::constrain` now sums all pool balances and returns a `Total` error if the total amount does not satisfy the target constraint. Adds a `total()` helper and a `ValueBalanceError::Total` variant.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates zebra-chain’s ValueBalance constraint conversion to validate that the sum across all value pools satisfies the target Amount constraint, preventing cases where each pool is individually valid but the overall total is not.
Changes:
- Added
ValueBalance::total()to compute the sum across all pools. - Updated
ValueBalance::constrainto validate the total after per-pool constraining, returning a newValueBalanceError::Totalon failure. - Added
ValueBalanceError::Totaland wired it intoDisplay.
Comment on lines
148
to
+152
| pub fn constrain<C2>(self) -> Result<ValueBalance<C2>, ValueBalanceError> | ||
| where | ||
| C2: Constraint, | ||
| C2: Constraint + Copy, | ||
| { | ||
| Ok(ValueBalance::<C2> { | ||
| let value_balance = ValueBalance::<C2> { |
Comment on lines
+160
to
+161
| value_balance.total().map_err(ValueBalanceError::Total)?; | ||
| Ok(value_balance) |
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.
Motivation
ValueBalance::constrainconverts a value balance to a different constraint by constraining each pool independently, but never checked that the total across all pools satisfies the target constraint.Solution
ValueBalance::total()helper that sums all pool balances.ValueBalanceError::Totalvariant if it does not satisfy the target constraint.Testing
cargo clippy -p zebra-chain --all-targets -- -D warningscargo test -p zebra-chain value_balanceAI disclosure
Used Claude Code to complete the compile fix (missing
Displayarm for the new error variant), commit, and PR description.