Skip to content

feat: implement circuit breaker lock in Soroban contract#568

Open
Jennylila wants to merge 1 commit intoHahfyeex:Defaultfrom
Jennylila:feat/153-circuit-breaker-lock
Open

feat: implement circuit breaker lock in Soroban contract#568
Jennylila wants to merge 1 commit intoHahfyeex:Defaultfrom
Jennylila:feat/153-circuit-breaker-lock

Conversation

@Jennylila
Copy link
Copy Markdown

Overview

This PR implements a per-market circuit breaker for the Soroban prediction market contract to protect pools from anomalous betting activity. It adds persistent circuit-breaker state and rolling pool snapshots, blocks new bets once the breaker is triggered, emits an on-chain event for indexers, and provides admin recovery flows to reopen or force-resolve affected markets.

Related Issue

Closes #143

Changes

⚙️ Circuit Breaker Implementation

  • [MODIFY] contracts/prediction_market/src/lib.rs
    • Added DataKey::CircuitBreaker(u64) and DataKey::PoolSnapshot(u64) to contract storage.
    • Added a persistent circuit-breaker flag per market.
    • Added a persistent rolling pool snapshot per market.
    • On each place_bet, compares the current pool against the snapshot from at least 60 seconds earlier.
    • Triggers the circuit breaker when pool movement is greater than 50% within the 60-second window.
    • Rejects later bets with CIRCUIT_BREAKER_ACTIVE once the breaker is active.
    • Emits a breaker contract event with market ID, previous snapshot, current pool, and trigger timestamp.
    • Refreshes the stored pool snapshot using ledger timestamp checks.

🛠️ Admin Recovery Functions

  • [MODIFY] contracts/prediction_market/src/lib.rs
    • Added reopen_market(market_id) to clear the breaker and refresh the snapshot baseline.
    • Added force_resolve(market_id, outcome) to allow emergency admin resolution.
    • Both recovery functions require admin auth.

🧪 Test Coverage

  • [MODIFY] contracts/prediction_market/src/lib.rs
    • Added unit tests for:
      • breaker default state
      • trigger when movement exceeds 50% after 60 seconds
      • no trigger at exactly 50%
      • no trigger inside the 60-second window
      • bets blocked after trigger
      • admin reopen flow
      • admin force-resolve flow
    • Verified the full contract test suite passes.

📚 Documentation

  • [MODIFY] README.md
    • Documented circuit breaker states.
    • Documented the admin recovery procedure.
    • Described how the 60-second snapshot and trigger threshold work.

📦 Dependency / Audit Maintenance

  • [MODIFY] contracts/prediction_market/Cargo.lock
    • Updated lockfile dependencies so cargo audit passes without high or critical advisories.

Verification Results

Acceptance Criteria Status
Circuit breaker triggers correctly when pool movement exceeds 50% within 60 seconds
New bets are rejected with CIRCUIT_BREAKER_ACTIVE after trigger
Contract event is emitted on trigger
reopen_market and force_resolve require admin auth and work correctly
Unit tests cover trigger and recovery edge cases
cargo audit passes with zero high or critical advisories
README documents breaker states and recovery procedure

How to Test

# 1. Confirm you're on the feature branch
git branch --show-current

# 2. Run the contract tests
cd contracts/prediction_market
cargo test
<img width="808" height="393" alt="image" src="https://github.com/user-attachments/assets/f6fd42a1-2c79-4b11-98ae-eb1dbb921989" />

# 3. Verify dependency audit
cargo audit

# 4. Optional: inspect the main changed files
git diff -- README.md contracts/prediction_market/src/lib.rs contracts/prediction_market/Cargo.lock

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 29, 2026

@Jennylila 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

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.

[BE] #153 - Circuit Breaker Lock

2 participants