""" Replay verification — determinism, mismatch detection, binary search. """ import pytest from malkhut.state import ( AccountState, MarketWorldState, Mode, OrderBookState, PriceLevel, VenueRules, ) from malkhut.cwm.core import MinimalCryptoLOBCWM from malkhut.cwm.replay_verify import ReplayVerifier, ReplayStep from malkhut.actions import ActionKind, FulfilmentAction def _venue(): return VenueRules( exchange="bingx", symbol="BTCUSDT", tick_size=0.1, lot_size=0.001, min_qty=0.001, min_notional=5.0, maker_fee_bps=-0.2, taker_fee_bps=0.5, post_only_supported=True, reduce_only_supported=True, max_orders_per_second=100, max_cancels_per_minute=120, ) def _state(ts=1_000_000_000): return MarketWorldState( ts_ns=ts, mode=Mode.REPLAY_NO_IMPACT, venue=_venue(), book=OrderBookState(ts_ns=ts, symbol="BTCUSDT", bids=(PriceLevel(50000.0, 1.0),), asks=(PriceLevel(50001.0, 1.0),)), account=AccountState(ts_ns=ts, equity=10000.0, wallet_balance=10000.0, available_balance=10000.0, margin_used=0.0, total_notional=0.0), ) def _noop(): return FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0) class TestReplayVerifyDeterministic: def test_perfect_replay_passes(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) s2 = cwm.transition(s1, (a,)) replay = [ ReplayStep(s0, (a,), s1), ReplayStep(s1, (a,), s2), ] result = verifier.verify(cwm, replay) assert result.passed assert len(result.mismatches) == 0 def test_mismatch_detected(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) # Tamper with ground truth bad_s1 = MarketWorldState( ts_ns=s1.ts_ns + 999, mode=s1.mode, venue=s1.venue, book=s1.book, account=s1.account, ) replay = [ReplayStep(s0, (a,), bad_s1)] result = verifier.verify(cwm, replay) assert not result.passed assert len(result.mismatches) > 0 def test_mismatch_at_correct_index(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) s2 = cwm.transition(s1, (a,)) s3 = cwm.transition(s2, (a,)) # First step correct, second tampered bad_s2 = MarketWorldState( ts_ns=s2.ts_ns + 999, mode=s2.mode, venue=s2.venue, book=s2.book, account=s2.account, ) replay = [ ReplayStep(s0, (a,), s1), ReplayStep(s1, (a,), bad_s2), ReplayStep(s2, (a,), s3), ] result = verifier.verify(cwm, replay) assert not result.passed assert result.mismatches[0].index == 1 def test_empty_replay_passes(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() result = verifier.verify(cwm, []) assert result.passed class TestReplayVerifyDeterminismCheck: def test_same_replay_twice_same_result(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) replay = [ReplayStep(s0, (a,), s1)] r1 = verifier.verify(cwm, replay) r2 = verifier.verify(cwm, replay) assert r1.passed == r2.passed def test_determinism_verification_passes_for_cwm(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) replay = [ReplayStep(s0, (a,), s1)] result = verifier.verify_determinism(cwm, replay) assert result.passed class TestReplayBisect: def test_bisect_returns_first_mismatch(self): cwm = MinimalCryptoLOBCWM() verifier = ReplayVerifier() s0 = _state() a = _noop() s1 = cwm.transition(s0, (a,)) from malkhut.cwm.replay_verify import ReplayMismatch replay = [ReplayStep(s0, (a,), s1)] result = verifier.verify(cwm, replay) assert result.passed # Bisect on clean replay returns None mismatch = verifier.bisect(cwm, replay) assert mismatch is None