Skip to content

fix(chain): validate the total amount when constraining a value balance#10817

Open
arya2 wants to merge 1 commit into
mainfrom
validate-value-balance-total
Open

fix(chain): validate the total amount when constraining a value balance#10817
arya2 wants to merge 1 commit into
mainfrom
validate-value-balance-total

Conversation

@arya2

@arya2 arya2 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Motivation

ValueBalance::constrain converts 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

  • Add a ValueBalance::total() helper that sums all pool balances.
  • After constraining each pool, validate the total and return a new ValueBalanceError::Total variant if it does not satisfy the target constraint.

Testing

  • cargo clippy -p zebra-chain --all-targets -- -D warnings
  • cargo test -p zebra-chain value_balance

AI disclosure

Used Claude Code to complete the compile fix (missing Display arm for the new error variant), commit, and PR description.

`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.
Copilot AI review requested due to automatic review settings June 26, 2026 01:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::constrain to validate the total after per-pool constraining, returning a new ValueBalanceError::Total on failure.
  • Added ValueBalanceError::Total and wired it into Display.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants