fix(chain)!: validate the total amount when updating a value balance#10817
Open
arya2 wants to merge 9 commits into
Open
fix(chain)!: validate the total amount when updating a value balance#10817arya2 wants to merge 9 commits into
arya2 wants to merge 9 commits 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.
conradoplg
previously approved these changes
Jul 2, 2026
conradoplg
left a comment
Collaborator
There was a problem hiding this comment.
Looks good, tagged as do-not-merge due to the upcoming release
Collaborator
|
@arya2 needs conflict solving |
conradoplg
reviewed
Jul 9, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
zebra-chain/src/value_balance.rs:330
- The PR title and Solution say the target constraint is validated by
ValueBalance::constrain, butconstrainremains pool-by-pool and the total check now applies only toadd_chain_value_pool_change. Consequently, other publicconstraincallers can still receive a value balance whose total violates the target constraint. If this narrower consensus check is intentional, update the title/description and crate changelog to scope the behavior to chain-pool updates; otherwise introduce a separate checked-constrain API without making widening conversions fallible.
/// We implement the consensus rules above by constraining the returned value balance to
/// [`ValueBalance<NonNegative>`].
#[allow(clippy::unwrap_in_result)]
pub fn add_chain_value_pool_change(
self,
…validate-value-balance-total
hilljessica8787-dot
approved these changes
Jul 14, 2026
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.
Closes #10882
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.