Self-audit
An honest internal audit of crates/rulebook and programs/var-settlement, written by the person
who built it, for whoever picks this up next (external auditor, judge, or future us). Structured as:
what’s proven, what’s checked-but-not-proven, and what’s a known limitation we’re choosing to ship
with, not hiding.
Invariants proven with Kani
All four run against the pure rulebook crate (crates/rulebook/src/lib.rs,
#[cfg(kani)] mod proofs), via cargo kani -p rulebook — transcript at
docs/KANI_PROOF_TRANSCRIPT.txt: Complete - 4 successfully verified harnesses, 0 failures:
| ID | Name | Harness | What it rules out |
|---|---|---|---|
| INV-1 | Totality / fail-closed | inv1_resolve_outcome_total_and_correct |
Panics on any input in the swept range (home_goals, away_goals in [-3, 20], all 6 MatchStatus variants); wrong 90’ mapping; a non-Completed* status resolving to anything but Refund |
| INV-2 | Conservation | inv2_settlement_conserves |
Minting value at the pool level: asserts pot == home + draw + away, fee + net == pot, net <= pot, fee <= pot, and refunds always carry fee == 0, net == 0 |
| INV-3 | Settlement fail-closed | inv3_settle_fail_closed |
Outcome::Refund or an out-of-range fee_bps producing anything but a zero-fee full refund |
| INV-4 | Determinism | inv4_resolve_outcome_deterministic |
Any hidden state — identical inputs asserted to always produce an identical resolution (settle is pure, so settlement determinism follows) |
Not in Kani, by design: the per-winner payout bound (winner_payout <= net, including any
split among winners) divides by a symbolic divisor at u128 width, which CBMC must bit-blast —
intractable. It is covered instead by the randomized property suite
crates/rulebook/tests/payout_props.rs: 3 solvency/payout properties × 4,000 cases each (12,000
cases at full USDC magnitudes). See the inline comment in inv2_settlement_conserves for the
rationale.
These are exhaustive over the bounded ranges Kani sweeps (kani::assume bounds: goals -3..=20,
pool values <= 1000, fee_bps <= MAX_FEE_BPS), not sampled —
Kani’s bounded model checker explores the full state space within those bounds. Widening the bounds
further (e.g. arbitrary u64 pool values instead of <=1000) is a cheap follow-up; the current
bounds were chosen to keep proof runtime reasonable while still covering every code path (every
branch in resolve_outcome and settle is exercised at the boundary values).
What Kani does not cover: anything inside programs/var-settlement — Solana account
handling, CPI serialization, PDA derivation, the claimed guard. Kani proves properties of pure
functions; it doesn’t model an Anchor program’s account graph. Those properties are covered by
ordinary tests and the devnet integration suite (tests-devnet/smoke.ts, full lifecycle with real
SPL transfers — see DEPLOYMENTS.md) instead.
Checked-arithmetic / overflow discipline
#![forbid(unsafe_code)] at the top of crates/rulebook/src/lib.rs — the entire verified core is
safe Rust, no exceptions.
Every arithmetic operation that could overflow is explicit about it:
Pools::total()returnsOption<u64>viachecked_addchains;None(overflow) is treated as a fail-closed refund-with-pot-zero insettle(), not a wrapped/panicking add.feecomputation widens tou128before multiplying ((pot as u128) * (fee_bps as u128) / BPS_DENOM as u128) specifically sopot * fee_bpscan’t overflowu64even nearu64::MAXpot values, then narrows back with a comment justifying why the narrow is safe (fee <= potsincefee_bps <= MAX_FEE_BPS <= BPS_DENOM).winner_payout()does the sameu128-intermediate trick forwinning_stake * net / winning_pool, with the invariantwinning_stake <= winning_pooljustified in a comment (by construction — a Position’s stake in a bucket can’t exceed the market’s total pool for that bucket, since the pool is the sum of all positions’ stakes in it).- On the program side, every pool/position update in
deposit()useschecked_add(...).ok_or( VarError::MathOverflow)?— no bare+=on escrow-tracking fields anywhere inprograms/var-settlement/src/lib.rs. - The workspace
Cargo.tomlsetsoverflow-checks = trueunder[profile.release], so even if a checked path were missed, a release build would panic (not silently wrap) rather than corrupt state — belt-and-suspenders over the explicitchecked_*calls above.
Double-claim guard
Position.claimed: bool, checked in claim():
require!(!p.claimed, VarError::AlreadyClaimed);
...
p.claimed = true;
if amount == 0 { return Ok(()); }
... token::transfer(...)
claimed is set to true before the transfer executes and regardless of whether amount turns out
to be zero, so a second call to claim() against the same Position always errors out before
touching the vault. This is a straightforward account-state guard, not (yet) formally proven — see
Known Limitations. The happy-path claim is exercised end-to-end on devnet by
tests-devnet/smoke.ts (both winner and loser claims, real SPL transfers); an explicit
second-claim-must-fail devnet test is a cheap follow-up an auditor should ask for.
Fixture / stat-key binding (anti cross-stat-spoofing)
Settlement is two instructions, and each binds its own witness before any CPI happens.
attest_home checks the home side:
require!(home.summary.fixture_id == m.fixture_id, VarError::FixtureMismatch);
require!(home.stat.stat_to_prove.key == m.home_stat_key, VarError::StatKeyMismatch);
require!(home.stat.stat_to_prove.period == m.period, VarError::StatPeriodMismatch);
and resolve checks the away side identically (m.away_stat_key), plus
require!(m.home_attested, VarError::HomeNotAttested) so a resolution can’t skip the home proof.
Because both witnesses are compared against the same m.fixture_id, a cross-fixture home/away
pair is transitively rejected.
Without this, validate_stat would happily authenticate some true stat off the feed (a corner
count, a different fixture’s goal stat, the wrong period) and the program would have no way to tell
it isn’t the home/away regulation-goals stat this specific market was created against. The market’s
home_stat_key/away_stat_key/period are fixed at create_market time and never mutated
afterward, so this binding is stable for the market’s whole lifetime.
Resolve deadline / grace window
require!(now <= m.resolve_deadline.saturating_add(RESOLVE_GRACE_SECS), VarError::ResolveWindowPassed);
RESOLVE_GRACE_SECS = 7 * 24 * 60 * 60 (7 days). This exists because Tx LINE’s Merkle roots publish
per epoch-day (see docs/TXLINE_API_FEEDBACK.md), so the root covering the final-whistle update
may land after resolve_deadline if a match runs late relative to when the market was configured.
The grace window is a deliberate tradeoff: too short and legitimate late settlements get stuck with
no valid path forward (there is no admin override to force-resolve); too long and a stale market
sits resolvable long after the fact. Seven days was picked as generous-but-bounded; an auditor
should sanity-check this against Tx LINE’s actual observed root-publish latency once that’s
measured against the live feed (currently undocumented on Tx LINE’s side — flagged in
docs/TXLINE_API_FEEDBACK.md).
There is currently no minimum time-after-match-end check beyond “the root must actually validate”
— validate_stat itself will fail if the root doesn’t yet cover the claimed stat, which is the real
gate. The deadline/grace window bounds the other side (how late is too late), not this one.
Known limitations
- Daily-root batching means settlement is post-match, not per-tick. Tx LINE commits
daily_scores_rootsper epoch-day, not per stat-update. VAR’sresolvetherefore cannot fire the moment the final whistle blows — it can only fire once the covering root publishes (seeRESOLVE_GRACE_SECSabove). This is a deliberate consequence of building on the feed’s actual commitment model rather than assuming a real-time push VAR doesn’t have. Anyone reviewing latency claims for this project should read “trustless resolution,” not “instant resolution.” - CPI return-data dependency.
validate_stat_equalreads theboolresult viaget_return_data()afterinvoke(). This is standard Solana return-data plumbing, but it means VAR’s correctness depends onTxoracleactually populating return data on every call path (including its error paths) the way the IDL implies. We have not independently fuzzedTxoracle’s return-data behavior — that program is out of our control and out of scope for this audit; we treat it as the trusted data source (seedocs/TRUST_SURFACE.md). - Single market type in V1 — and
market_kindis a nonce, not a validated enum. Every market resolves as 1X2;create_marketdoes not validatemarket_kind— it is deliberately repurposed as a per-fixture nonce in the Market PDA seeds so one fixture can host multiple market accounts (see the comment increate_market;tests-devnet/txline-settle.tsrelies on this). TheUnsupportedMarketKinderror variant is defined but currently unused (reserved for when kinds beyond 1X2 exist). The rulebook’sMatchState/Outcometypes are designed to extend to Over/Under, correct-score, etc. (seespec.md§1), but that extension is unbuilt — an auditor should not assume any other market kind has been reviewed. - No program-level Kani coverage (see table above). The
claimedguard, the deadline check, and the stat-key binding are conventionalrequire!guards, not machine-proven. They’re simple enough to review by hand (each is a single boolean condition gating a single side effect) but an external auditor should treat them with ordinary scrutiny, not assume Kani’s guarantees extend to them. - Devnet only — no mainnet run yet. The program is built for SBF, deployed on devnet
(
AepSNpDzMUdBgjxA9irxxL7NTQHxXtDVq6rnqq17Lxk), exercised end-to-end bytests-devnet/smoke.ts, and has settled a real fixture (18192996) against the realTxoracleCPI over the live on-chain daily Merkle root — seeDEPLOYMENTS.mdfor every tx signature. What remains is a mainnet (real-time L12) run, which changes cluster and service tier, not code. ResolutionReceiptbinds tosource_rootderived from the twoevents_sub_tree_rootvalues hashed together, not the full daily main root. This is enough to detect if either side’s witness changes, but an auditor verifying the receipt against the chain independently should knowsource_rootis a derived binding hash, not literally the on-chaindaily_scores_merkle_rootsvalue — the actual authentication happened at CPI time viavalidate_stat, and the receipt is a record of that, not a second independent proof.
What an external auditor should focus on
In priority order:
- The CPI call site (
txoracle_cpi::validate_stat_equaland the tworequire!(ok_*, ...)calls inresolve()) — this is the single seam where “authenticated by the chain” and “believed by the program” meet. Confirm the account list passed toinvoke()matches whatTxoracleactually expects once its real accounts (beyond the PDA) are known, and confirm there’s no way to pass adaily_scores_merkle_rootsaccount for a different epoch-day than the one implied bywitness.ts. - The fixture/stat-key/period binding — verify the three-way binding holds as implemented:
attest_homeassertshome.summary.fixture_id == m.fixture_idandresolveassertsaway.summary.fixture_id == m.fixture_id(VarError::FixtureMismatch), on top of the stat-key/period checks, so a cross-fixture home/away witness pair is transitively rejected. (This was a real gap found and closed during this audit — see the resolution note below.) - The Kani proof bounds — are
-3..=20goals and<=1000pool values wide enough to call this “exhaustive in practice,” or should they be widened before calling INV-1..4 airtight for mainnet-scale pools? - The devnet integration suite (
tests-devnet/) —smoke.tscovers the full happy path with real account state and SPL transfers; what it does not yet cover is a second-claim-must-fail attempt, the early/late resolve boundary, and the refund path exercised on-chain. Those are the cheap follow-up tests to demand before mainnet.
Resolved during this audit: each witness is now bound on-chain to the market’s own
fixture_id — attest_home asserts home.summary.fixture_id == m.fixture_id and resolve
asserts away.summary.fixture_id == m.fixture_id (both VarError::FixtureMismatch), in addition
to the stat-key and period checks. Because both are compared against m.fixture_id, a cross-fixture
home/away pair is transitively rejected. A valid Merkle proof lifted from a different match that
shares stat keys can no longer be used to settle this market.
Remaining open item: the match-status code is a resolver input, not an oracle-authenticated
field. The proven rulebook fail-closes every non-Completed status to Refund (INV-1), bounding
the downside to a refund, but binding status to an authenticated feed field is the gap to close
before mainnet.