Skip to content

fix(validator): Validator Balance Tracker Silent Underflow During Penalty Exceeding Balance - #93

Open
Mona-i wants to merge 2 commits into
VeriNode-Labs:mainfrom
Mona-i:fix/validator-balance-underflow-issue-241
Open

fix(validator): Validator Balance Tracker Silent Underflow During Penalty Exceeding Balance#93
Mona-i wants to merge 2 commits into
VeriNode-Labs:mainfrom
Mona-i:fix/validator-balance-underflow-issue-241

Conversation

@Mona-i

@Mona-i Mona-i commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Closes #24

Replaces saturating_sub with checked_sub in apply_penalty(). Returns InsufficientBalance error when penalty exceeds balance. Records debt and forces immediate ejection. Adds penalty_calculator.rs and full test suite including property tests.

Mona-i added 2 commits July 20, 2026 13:40
…lty #241

- Add src/validator/balance_tracker.rs
  * apply_penalty() now uses checked_sub instead of saturating_sub
  * Returns Err(BalanceError::InsufficientBalance) when penalty > balance
  * Records excess as a debt in a separate BTreeMap<ValidatorIndex, u64>
  * ejection_eligible() treats any validator with outstanding debt as
    immediately ejectable, regardless of stored effective balance
  * apply_reward() is capped at MAX_EFFECTIVE_BALANCE (32 ETH in Gwei)
  * clear_debt() allows callers to purge the debt record after ejection

- Add src/slashing/penalty_calculator.rs
  * compute_slashing_penalty(): 1/32 whistleblower + 1/32 correlation = 1/16 of effective balance
  * compute_inactivity_penalty(): epochs_since_finality^2 * balance / INACTIVITY_PENALTY_QUOTIENT
  * cap_effective_balance(): enforces MAX_EFFECTIVE_BALANCE invariant
  * u128 intermediate arithmetic prevents overflow in inactivity formula

- Add tests/validator/balance_underflow_test.rs
  * Acceptance criterion: 1 ETH validator + 1.5 ETH penalty => ejected same epoch
  * Normal penalty path regression: penalty <= balance reduces balance correctly
  * Ejection threshold boundary test
  * Reward cap tests (saturates at MAX_EFFECTIVE_BALANCE)
  * Debt lifecycle test (clear_debt after ejection)
  * Penalty calculator unit tests
  * Property test: effective_balance never exceeds MAX_EFFECTIVE_BALANCE after rewards
  * Property test: penalty > balance always returns InsufficientBalance + records debt

- Update src/validator/mod.rs: expose balance_tracker module
- Update src/slashing/mod.rs: expose penalty_calculator module
- Update Cargo.toml: register balance_underflow_test integration test
Resolve Cargo.toml conflict by keeping both integration test entries:
- proof_of_connectivity_epoch_nonce_test (from main, PR VeriNode-Labs#94)
- balance_underflow_test (from this branch, issue #241)
@JamesEjembi

Copy link
Copy Markdown
Contributor

resolve conflicts @Mona-i

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.

Validator Balance Tracker Silent Underflow During Penalty Exceeding Balance

2 participants