This file provides guidance to WARP (warp.dev) when working with code in this repository.
This is a Rust-based traffic light simulation demonstrating distributed state machine replication. The core concept models a four-way intersection where:
- A "Core" thread generates random actions
- Actions are distributed to replica threads (simulating network transfer via cloning)
- Each replica independently processes its actions and maintains intersection state
- The Core also processes all actions sequentially to show the canonical state progression
cargo build # Build the project
cargo run # Run the simulation
cargo run --release # Run optimized versioncargo test # Run all tests
cargo test test_intersection # Run specific test
cargo test -- --nocapture # Run tests with outputcargo check # Fast compilation check without binary
cargo clippy # Lint with Clippy
cargo fmt # Format codeThe FourWayIntersection is a deterministic state machine with valid states:
- North/South: Red → Green → Yellow → Red
- East/West: Red → Green → Yellow → Red
- Only opposing directions (N/S or E/W) can be Green simultaneously
Invalid states are automatically reset to all-Red to maintain safety.
Three action types with probabilistic generation:
Next(70%): Advance intersection to next valid stateSleep(u64)(20%): Thread sleep for 0-3 seconds (simulates network delay)Glitch(FourWayIntersection)(10%): Force arbitrary state (simulates Byzantine faults)
- Core Thread: Generates 30 random actions, distributes to 3 replicas via HashMap
- Network Transfer: Simulated by cloning the actions HashMap
- Replica Threads: Run in parallel using
thread::scope, each processing only its assigned actions - Verification: Core sequentially processes all actions to show expected final states per replica
- Consistency: Replicas may diverge due to Glitch actions (Byzantine fault simulation)
- Parallelism: Uses scoped threads for automatic cleanup, avoiding 'static lifetime constraints
- Determinism: Given same action sequence, a replica produces same state (except for Sleep timing)
src/main.rs: Single-file implementation containing:TrafficLightenum: Individual light states (Red/Yellow/Green)Actionenum: Operations that can be performedFourWayIntersectionstruct: Main state machine with transition logicmain(): Orchestrates Core and Replica simulationtestsmodule: Validates state transitions including invalid state recovery
/scoped/main.rs
graph LR;
A[Core] --> B{Event};
B --Sleep--> C(Replica);
B --Next--> D(Replica);