Skip to content

feat(vault): add Kani harnesses for transfer correctness + fix compile errors (#490)#523

Merged
greatest0fallt1me merged 2 commits into
CalloraOrg:mainfrom
ZuLu0890:feature/kani-vault-transfer-harness
Jun 27, 2026
Merged

feat(vault): add Kani harnesses for transfer correctness + fix compile errors (#490)#523
greatest0fallt1me merged 2 commits into
CalloraOrg:mainfrom
ZuLu0890:feature/kani-vault-transfer-harness

Conversation

@ZuLu0890

Copy link
Copy Markdown
Contributor

Summary

Closes #490 — Add Kani harness for vault.transfer correctness under adversarial input.


Compilation fixes

The vault contract had several bugs preventing compilation:

  • StorageKey::MetaKeyStorageKey::Meta — 5 occurrences replaced; MetaKey variant does not exist in the enum.
  • Missing get_max_deduct() method — referenced by deduct and batch_deduct but never defined; added as a public view function.
  • Dead code warning — added #[allow(dead_code)] to the orphaned migrate() helper.

Correctness fixes

  • deduct() did not check pause statedeposit and batch_deduct both called require_not_paused(); deduct did not, allowing deductions through the circuit-breaker. Fixed.
  • Deposit event topic countEVENT_SCHEMA.md specifies topic[0] = Symbol("deposit"), topic[1] = caller Address. The implementation only emitted 1 topic. Fixed to match schema.

Test fixes

  • deduct_while_paused_succeedsdeduct_while_paused_fails (#[should_panic])
  • batch_deduct_while_paused_succeedsbatch_deduct_while_paused_fails (#[should_panic])
  • Deposit event tests updated to assert 2 topics + verify caller address
  • Fuzz tests: removed duplicate else branch that caused parse error
  • settlement/test.rs: fixed _third_partythird_party in setup_contract()
  • Fixed unused variable warnings (settlement, step_cap)

Kani formal verification (issue #490)

Added contracts/vault/src/kani_proofs.rs with 7 adversarial harnesses:

Harness Property verified
kani_deduct_balance_non_negative balance ≥ 0 after any valid deduction
kani_deduct_strictly_reduces_balance deduct always strictly decreases balance
kani_deposit_no_overflow checked_add succeeds for all in-range deposits
kani_deposit_overflow_detected checked_add returns None on overflow (panic path sound)
kani_max_deduct_enforced amount > max_deduct is rejected before any mutation
kani_batch_deduct_total_no_overflow batch total accumulation stays non-negative and correct
kani_withdraw_balance_non_negative withdraw mirrors deduct invariant under adversarial amounts

All harnesses use symbolic inputs via kani::any() + kani::assume() to cover the full input space, including adversarial edge cases (e.g. near i128::MAX, zero boundaries).

The file is guarded with #[cfg(kani)] — normal cargo test / CI runs skip it entirely. Vault Cargo.toml declares cfg(kani) as a known check-cfg to suppress the rustc unexpected_cfgs warning.

Running harnesses locally

cargo install --locked kani-verifier
cargo kani --package callora-vault

CI verification

Check Result
cargo fmt --check
cargo clippy -D warnings
cargo test --workspace ✅ 270 tests (179 vault + 52 settlement + 39 revenue-pool)

ZuLu0890 added 2 commits June 27, 2026 16:16
…errors

Closes CalloraOrg#490

## Compilation fixes
- Fix StorageKey::MetaKey → StorageKey::Meta (5 occurrences in lib.rs)
- Add missing get_max_deduct() method to CalloraVault
- Add #[allow(dead_code)] to orphaned migrate() helper

## Correctness fixes
- deduct(): add require_not_paused() check (deposit/batch_deduct already had it)
- deposit event: add caller Address as topic[1] to match EVENT_SCHEMA.md (2 topics)

## Test fixes
- deduct_while_paused_succeeds → deduct_while_paused_fails (#[should_panic])
- batch_deduct_while_paused_succeeds → batch_deduct_while_paused_fails (#[should_panic])
- deposit_event_schema_alignment: update to assert 2 topics + caller address
- owner_deposit_increases_balance_and_emits_event: same update
- owner_can_deposit: unwrap on topic[1] now resolves correctly
- fuzz tests: remove duplicate else-branch that caused parse error
- settlement/test.rs: fix _third_party → third_party in setup_contract()
- Fix unused variable warnings (settlement, step_cap)

## Kani formal verification (closes CalloraOrg#490)
Add contracts/vault/src/kani_proofs.rs with 7 adversarial harnesses:
  1. kani_deduct_balance_non_negative  – balance ≥ 0 after any valid deduct
  2. kani_deduct_strictly_reduces_balance – deduct always decreases balance
  3. kani_deposit_no_overflow          – deposit checked_add succeeds in-range
  4. kani_deposit_overflow_detected    – checked_add returns None on overflow
  5. kani_max_deduct_enforced          – amount > max_deduct rejected before mutation
  6. kani_batch_deduct_total_no_overflow – batch total accumulation stays valid
  7. kani_withdraw_balance_non_negative – withdraw mirrors deduct invariant

Harnesses compile only under cfg(kani); normal cargo test/CI unaffected.
Vault Cargo.toml declares cfg(kani) as a known check-cfg to silence rustc warning.

## CI status
- cargo fmt --check: ✅
- cargo clippy -D warnings: ✅
- cargo test --workspace: ✅ (270 tests: 179 vault + 52 settlement + 39 revenue-pool)
@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@ZuLu0890 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me greatest0fallt1me merged commit ee30963 into CalloraOrg:main Jun 27, 2026
0 of 5 checks passed
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.

Add Kani harness for vault.transfer correctness under adversarial input

2 participants