Skip to content

Virtual Raindex live facade#2181

Open
hardyjosh wants to merge 4 commits into2025-10-03-vr-live-quotefrom
2025-10-03-vr-in-raindexclient
Open

Virtual Raindex live facade#2181
hardyjosh wants to merge 4 commits into2025-10-03-vr-live-quotefrom
2025-10-03-vr-in-raindexclient

Conversation

@hardyjosh
Copy link
Contributor

@hardyjosh hardyjosh commented Oct 7, 2025

Caution

This PR is chained to #2176, review and merge

Motivation

#2176 made the virtual engine snapshot-friendly and wasm-ready, but callers still had to orchestrate their own live syncing, bytecode hydration, and persistence. Every consumer (RaindexClient, solvers etc) would otherwise rebuild the same scaffolding—polling for mutations, retrying while bytecode warms up, storing snapshots/cursors, and surfacing “warming up” telemetry. We needed a reusable façade so hosts can keep a VirtualRaindex instance live without re-implementing that plumbing, and we needed deterministic fixtures to exercise the flow before the local DB pipeline lands.

Solution

  • Add a live module exporting LiveVirtualRaindex, which owns the engine, mutation queue, and bytecode warm-up. The builder takes pluggable adapters (SnapshotStore, CursorStore, MetricsSink) so hosts can supply their own storage and telemetry.
  • Define the SyncEngine trait plus supporting types (MutationEnvelope, SyncPoll, BytecodeArtifact) so hosts can stream mutations, cached bytecode, and pending artifacts into the façade.
  • Ship in-memory snapshot/cursor stores and a no-op metrics sink, along with a scripted StubSyncEngine + JSON fixtures for deterministic tests and examples.
  • Extend the bytecode cache via LiveCodeCache so the façade ingests artifacts asynchronously, retries deferred mutations, and emits LiveStatus advisories (Idle, Syncing, PendingArtifacts, Errored).
  • Document the state machine, adapter contracts, and fixture format in src/live/README.md, and highlight the live sync entry point in the crate README.

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Introduces a Virtual Raindex engine for local simulation: quoting, taking orders, adding/removing orders, vault balance updates, and store writes.
    • Snapshot import/export with a portable bundle format.
    • Event ingestion from logs to rebuild state.
    • Live sync facade with pluggable snapshot/cursor stores, metrics, and bytecode warmup.
    • In-memory bytecode cache and a fast interpreter host.
    • Public request/response types for quote and take flows.
  • Documentation

    • Added comprehensive plans, implementation notes, and READMEs for the engine and live sync.
  • Tests

    • Extensive unit, integration, and live-sync tests with a demo fixture.
  • Chores

    • More robust local EVM startup in test fixtures.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 7, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Adds a new virtual-raindex crate with engine, state, types, error surface, REVM-backed interpreter host, event ingestion, live sync facade with adapters and status model, cache implementation, snapshots, and extensive tests plus docs. Updates test fixtures to robustly spawn Anvil. Introduces crate manifest and READMEs.

Changes

Cohort / File(s) Summary
Documentation
VIRTUAL_RAINDEX_CLIENT_PLAN.md, VIRTUAL_RAINDEX_IMPLEMENTATION.md, crates/virtual-raindex/README.md, crates/virtual-raindex/src/live/README.md
Adds planning and implementation notes; README for crate and live system. No code.
Test fixture robustness
crates/test_fixtures/src/lib.rs
Replaces direct Anvil spawn with helper spawn_anvil_instance() attempting multiple strategies; rest of setup unchanged.
Crate scaffold
crates/virtual-raindex/Cargo.toml, crates/virtual-raindex/src/lib.rs
Introduces new crate manifest, features, deps, dev-deps; exports public API from modules.
Core state + serialization
crates/virtual-raindex/src/state.rs, crates/virtual-raindex/src/snapshot.rs, crates/virtual-raindex/src/error.rs
Implements in-memory state, mutations, hashing, FQN derivation; snapshot bundle serde and conversion; defines RaindexError and Result.
Engine core
crates/virtual-raindex/src/engine/mod.rs, crates/virtual-raindex/src/engine/context.rs, crates/virtual-raindex/src/engine/mutations.rs, crates/virtual-raindex/src/engine/post_tasks.rs
Adds VirtualRaindex with constructors, snapshotting, mutation prep/apply, order add with post-tasks, and context builders.
Engine execution paths
crates/virtual-raindex/src/engine/quote.rs, crates/virtual-raindex/src/engine/take.rs
Implements quote and take-orders flows including IO validation, interpreter eval, vault/store updates, and outcomes.
Interpreter host (REVM)
crates/virtual-raindex/src/host/mod.rs, crates/virtual-raindex/src/host/revm.rs
Adds InterpreterHost trait, EvalOutcome, and REVM-backed host with bytecode loading and eval4 execution.
Bytecode cache
crates/virtual-raindex/src/cache.rs
Adds CodeCache trait and StaticCodeCache with ingestion/upsert, collision checks, and artifact ensures.
Types (public API)
crates/virtual-raindex/src/types/mod.rs, .../types/order_ref.rs, .../types/quote.rs, .../types/take.rs
Introduces OrderRef, quote request/override/result types, and batch take types and warnings.
Event ingestion
crates/virtual-raindex/src/events/mod.rs, .../events/orderbook.rs, .../events/store.rs
Converts OrderBook and Store events into RaindexMutation sequences; includes validations and helpers.
Live sync core
crates/virtual-raindex/src/live/mod.rs, .../live/sync_engine.rs, .../live/facade.rs, .../live/cache_ext.rs, .../live/adapter.rs, .../live/status.rs, .../live/bytecode.rs, .../live/stub.rs
Adds live sync model: sync engine trait, facade with builder and sync_once, in-memory snapshot/cursor stores, metrics, live cache extension, status types, warmup queue, and stub sync engine.
Tests (unit/integration/live)
crates/virtual-raindex/src/engine/tests.rs, crates/virtual-raindex/src/integration_tests.rs, crates/virtual-raindex/src/live/tests.rs, test-resources/virtual_raindex/live/demo.json
Extensive tests for engine, REVM eval paths, snapshots, mutations, quotes/takes, live sync status/progress, and demo script fixture.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant LVR as LiveVirtualRaindex
  participant SE as SyncEngine
  participant CC as CodeCache
  participant SS as SnapshotStore
  participant CS as CursorStore

  User->>LVR: sync_once()
  LVR->>CS: load_cursor(orderbook)
  LVR->>SE: poll(orderbook, cursor?)
  SE-->>LVR: SyncPoll{bytecode, mutation_batches, next_cursor, pending, heartbeat}

  loop Ingest artifacts
    LVR->>CC: ingest(BytecodeArtifact)
    CC-->>LVR: Result
  end

  alt Batches ready
    LVR->>LVR: apply ready MutationEnvelope(s)
  else Pending artifacts
    LVR->>LVR: enqueue pending envelopes (warmup queue)
  end

  opt Next cursor
    LVR->>CS: persist_cursor(orderbook, next_cursor)
  end

  opt Snapshot change
    LVR->>SS: persist_snapshot(SnapshotBundle)
  end

  LVR-->>User: LiveStatus (Idle|Syncing|PendingArtifacts|Errored)
Loading
sequenceDiagram
  autonumber
  actor Caller
  participant VR as VirtualRaindex
  participant CC as CodeCache
  participant IH as InterpreterHost

  Caller->>VR: quote(QuoteRequest)
  VR->>CC: ensure_artifacts(order)
  VR->>VR: build context + store snapshot
  VR->>IH: eval4(interpreter, EvalV4, snapshot, env)
  IH-->>VR: EvalOutcome{stack,writes}
  VR-->>Caller: Quote{io_ratio, output_max, stack, writes}
Loading

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120 minutes

Possibly related PRs

  • V5 Upgrade #1911 — Migrates to IOrderBookV5/EvalV4/OrderV4; directly related to the new virtual-raindex engine and bindings used here.

Suggested labels

rust, test

Suggested reviewers

  • 0xgleb
  • findolor

Pre-merge checks and finishing touches and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.64% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “Virtual Raindex live facade” succinctly identifies the main feature introduced by the PR, namely the live synchronization façade for the Virtual Raindex engine. It directly reflects the addition of LiveVirtualRaindex and its surrounding live-sync components without extraneous detail. A teammate reviewing the commit history will immediately grasp that this change concerns the live façade layer.

Comment @coderabbitai help to get the list of available commands and usage tips.

@hardyjosh hardyjosh changed the base branch from main to 2025-10-01-vr-event-stream October 7, 2025 14:16
@hardyjosh hardyjosh changed the title Virtual Raindex Virtual Raindex live facade Oct 7, 2025
@hardyjosh hardyjosh self-assigned this Oct 8, 2025
@hardyjosh hardyjosh changed the base branch from 2025-10-01-vr-event-stream to 2025-10-03-vr-live-quote October 8, 2025 09:43
offline resumes cheap.

## Bytecode Warmup Strategy
- Sync engines may stream `MutationEnvelope`s before matching bytecode is ready. The façade determines the
Copy link
Contributor

@rouzwelt rouzwelt Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo for facade

- `live::SyncEngine` – host-supplied async trait that yields mutation envelopes, hydrated bytecode, and
currently fetching artifacts. The stub implementation lives in `live::stub` and is backed by deterministic
scripts.
- `live::LiveVirtualRaindex` – façade owning the engine, code cache, interpreter host, and persistence
Copy link
Contributor

@rouzwelt rouzwelt Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo for facade

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.

2 participants