Skip to content

test(vault): claim when the balance is smaller than the tip - #14

Merged
0dillon merged 1 commit into
ledgerkeep:mainfrom
enny791:test/underfunded-vault-claim
Aug 7, 2026
Merged

test(vault): claim when the balance is smaller than the tip#14
0dillon merged 1 commit into
ledgerkeep:mainfrom
enny791:test/underfunded-vault-claim

Conversation

@enny791

@enny791 enny791 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Closes #10

Summary

Adds two tests to contracts/rent_vault/src/test.rs covering the claim path when a vault's balance is at or below its tip — the state every underfunded vault ends in.

claim already guards this with VaultError::InsufficientBalance (207) when vault.balance < vault.tip, checked after the keeper/timing verifications and before any mutation or transfer. This PR asserts that behavior explicitly rather than leaving it implied.

What changed

  • test::claim_rejects_underfunded_vault — funds a vault with 50 against a 100 tip, performs real maintenance (extend_all after a full interval), then claims. Asserts:
    • the outcome is InsufficientBalance (207, a 2xx error),
    • the vault balance is unchanged (50),
    • last_claim is unchanged (the failed claim does not consume the interval),
    • the keeper received 0 tokens and the vault still holds 50 — it never pays out more than it holds.
  • test::claim_at_exact_tip_balance — funds exactly the tip (100), the boundary case of the balance check, then claims. Asserts a full payout of 100, a drained balance of 0, the token leaves the vault, and last_claim advances to the payout ledger.

Key design decisions

  • No production code changed. The balance < tip check already runs before balance -= tip, so no underflow path exists (overflow-checks is on); the tests lock in that ordering.
  • Used the existing rejection-test pattern (try_claim(...).err() == Some(Ok(VaultError::InsufficientBalance))) and the same Fixture/ledger-advance conventions as the rest of the module.
  • The underfunded test records last_claim before the attempt and asserts it is unchanged, which is the direct evidence that a failed claim cannot silently consume the interval.

Acceptance criteria checklist

  • A test funds a vault with less than tip, performs real maintenance, and claims — claim_rejects_underfunded_vault.
  • The outcome is asserted explicitly as a specific 2xx error — InsufficientBalance (207).
  • The vault balance after the attempt is asserted — 50, unchanged.
  • last_claim is asserted — unchanged after the failed claim.
  • A test covers a balance of exactly tipclaim_at_exact_tip_balance.

Test output

cargo test --all (rent_vault suite):

running 10 tests
test test::claim_at_exact_tip_balance ... ok
test test::claim_rejects_underfunded_vault ... ok
test test::balance_accounting_across_fund_claim_withdraw ... ok
... (all 10 pass)

test result: ok. 10 passed; 0 failed; 0 ignored

cargo fmt --all -- --check — clean. cargo clippy --all-targets -- -D warnings — clean. stellar contract build — complete (all three contracts build to wasm).

Follow-ups

None in scope. A partial-payout alternative was considered and rejected: paying a pro-rata amount would violate the fixed tip terms without a corresponding production change, so the existing explicit InsufficientBalance rejection is the behavior worth locking in.

Security note

No new trust assumptions or money-movement code paths are introduced; the changes are test-only. The tests confirm a vault can never pay out more than it holds and that a rejected claim does not advance the payout clock.

@0dillon
0dillon merged commit 24b3793 into ledgerkeep:main Aug 7, 2026
1 check 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.

test(vault): claim when the balance is smaller than the tip

2 participants