Skip to content

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

Open
arya2 wants to merge 9 commits into
mainfrom
validate-value-balance-total
Open

fix(chain)!: validate the total amount when updating a value balance#10817
arya2 wants to merge 9 commits into
mainfrom
validate-value-balance-total

Conversation

@arya2

@arya2 arya2 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Closes #10882

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 thread zebra-chain/src/value_balance.rs Outdated
Comment thread zebra-chain/src/value_balance.rs Outdated
@conradoplg conradoplg self-assigned this Jul 1, 2026
@conradoplg conradoplg added the do-not-merge Tells Mergify not to merge this PR label Jul 2, 2026
conradoplg
conradoplg previously approved these changes Jul 2, 2026

@conradoplg conradoplg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good, tagged as do-not-merge due to the upcoming release

@mpguerra mpguerra removed the do-not-merge Tells Mergify not to merge this PR label Jul 3, 2026
@conradoplg

Copy link
Copy Markdown
Collaborator

@arya2 needs conflict solving

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread zebra-chain/src/value_balance.rs Outdated

@conradoplg conradoplg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Considering Copilot's review I think the proper place to check the bound is when adding pools, rather then when constraining. So I did that in 591d450

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread zebra-chain/src/value_balance.rs
Comment thread zebra-chain/src/value_balance.rs Outdated
Comment thread zebra-chain/src/value_balance.rs

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

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, but constrain remains pool-by-pool and the total check now applies only to add_chain_value_pool_change. Consequently, other public constrain callers 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,

Comment thread zebra-chain/src/value_balance.rs Outdated
Copilot AI review requested due to automatic review settings July 10, 2026 16:16

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 10, 2026 21:58
@conradoplg conradoplg changed the title fix(chain): validate the total amount when constraining a value balance fix(chain)!: validate the total amount when constraining a value balance Jul 10, 2026

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread zebra-chain/CHANGELOG.md Outdated
Comment thread CHANGELOG.md

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread zebra-chain/src/value_balance.rs
@conradoplg conradoplg changed the title fix(chain)!: validate the total amount when constraining a value balance fix(chain)!: validate the total amount when updating a value balance Jul 10, 2026
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.

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

6 participants