Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions move/sources/segment_market_v4.move
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ const EArchiveAlreadyRecorded: u64 = 24;
/// the live rugged state with a fresh `option::none()` which is unsafe
/// mid-round. Re-disable would need its own entry.
const ERugAlreadyEnabled: u64 = 25;
/// open_segment_ride_v4 refused: this round already rugged (MARKET HALT). Opening
/// post-rug would settle on the rug-free, player-positive path — a vault drain.
const EMarketRugged: u64 = 26;

// === Settlement kinds === (unchanged from v3)
const SETTLEMENT_OPEN: u8 = 0;
Expand Down Expand Up @@ -881,6 +884,17 @@ public fun open_segment_ride_v4<C>(
assert!(market.vault_id == object::id(vault), EWrongMarket);
// 2. Lazy-roll the round if needed
ensure_round_current(market);
// 2b. Refuse opens into an ALREADY-RUGGED round. The rug is the ENTIRE house
// edge — the touch payout against ±barriers over a full round is player-positive
// WITHOUT it (touch ~79% vs ~57% break-even). A round rugs at most once; after
// RugFiredV4 fires there is ZERO remaining rug risk, and a ride opened at
// entry_segment > rugged_seg settles on the rug-free path (it isn't <= rugged_seg,
// so decide_settlement won't route it to EXPIRED_LOSS) — carrying positive player
// EV: a vault drain an attacker triggers by watching RugFiredV4. The round is
// HALTED after a rug; opens resume next round (ensure_round_current clears
// rugged_at_segment on the roll). The frontend already blocks this; this closes
// the direct-MoveCall / modified-client bypass.
assert!(!is_rugged(market), EMarketRugged);
// 3. (v4: NO open-window assert — every segment in the round is open)
// 4. Stake range
assert!(
Expand Down
52 changes: 45 additions & 7 deletions move/tests/segment_market_v4_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,51 @@ fun rugged_ride_cannot_escape_via_in_round_touch_held_cross_round() {
sc.end();
}

/// Regression (v4.27) — the durable rug read must NOT over-rug: a ride opened
/// AFTER the rug segment (entry > rug_seg) is an honest post-rug bet and keeps
/// its win, even with the durable history present and even closing cross-round.
/// Complement to `rugged_ride_cannot_escape_via_in_round_touch_held_cross_round`
/// (entry ≤ rug_seg → rugged); together they pin the entry boundary under the
/// durable read. Here the rug fired at segment 2 but the ride opened at segment
/// 5 (> 2), touches in-round, and is closed cross-round → TOUCH_WIN.
/// CRITICAL (v4.27 economic audit) — opening into an ALREADY-RUGGED round must
/// abort. The rug is the ENTIRE house edge; once it has fired this round there is
/// ZERO remaining rug risk, so a ride opened at entry > rug_seg settles rug-free
/// and the touch bet is player-positive (payout > escrow) — a vault drain an
/// attacker triggers by watching RugFiredV4 and opening max-stake post-rug.
/// `open_segment_ride_v4` guards this with `EMarketRugged`.
#[test]
#[expected_failure(abort_code = sm4::EMarketRugged, location = wick::segment_market_v4)]
fun open_into_an_already_rugged_round_aborts() {
let mut sc = ts::begin(ALICE);
let (mut vault, vcap, mut market, bots, bcap, upo_obj, pcap, wts, wcap, pool, scap, clk) =
mk_full_world(&mut sc);
sm4::enable_rug<SUI>(&mut market, 150);
let seed = mint_sui(10_000_000_000, &mut sc);
mv::test_deposit_ride_escrow<SUI>(&mut vault, seed);

// Round 0 has ALREADY rugged at segment 2 — the live `rugged_at_segment` flag
// is set, exactly as roll_rug sets it when segment 2 records. The attacker now
// opens at entry 5 (> 2): a post-rug bet carrying the full touch upside with
// NO rug downside. The open MUST abort EMarketRugged.
sm4::test_only_bump_segment_index<SUI>(&mut market, 5);
sm4::test_only_set_rugged_at_segment<SUI>(&mut market, 2);

let stake = 1_000u64;
let r = sm4::open_segment_ride_v4<SUI>(
&mut market, &mut vault, &bots,
stake, mint_sui(stake * ROUND_DURATION, &mut sc), &clk, sc.ctx(),
); // ← aborts EMarketRugged

// Unreached.
sm4::test_only_destroy_ride(r);
teardown_world(vault, vcap, market, bots, bcap, upo_obj, pcap, wts, wcap, pool, scap, clk);
sc.end();
}

/// Defense-in-depth complement to the guard above — `decide_settlement`'s durable
/// read is EXACT at the entry boundary. This constructs a post-rug ride
/// ARTIFICIALLY (the rug is recorded into the durable history AFTER the open, so
/// `is_rugged` is false at open and the EMarketRugged guard doesn't fire) purely
/// to pin that a ride at entry > rug_seg is NOT over-rugged by the settlement
/// math: it settles on its true outcome (here an in-round touch → TOUCH_WIN), not
/// a false EXPIRED_LOSS. The open guard — not this path — is what blocks the drain
/// in production; its mirror
/// `rugged_ride_cannot_escape_via_in_round_touch_held_cross_round`
/// (entry ≤ rug_seg → rugged) pins the other side.
#[test]
fun post_rug_open_keeps_its_win_under_the_durable_read() {
let mut sc = ts::begin(ALICE);
Expand Down
Loading