diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99de175..d0e66fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,9 +55,33 @@ jobs: - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 - run: cargo test --workspace --all-features --no-fail-fast + docs: + name: rustdoc + runs-on: ubuntu-latest + env: + RUSTDOCFLAGS: "-D warnings" + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master 2026-03-27 + with: + toolchain: nightly + targets: wasm32-wasip2 + - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - run: cargo doc --workspace --no-deps + build-module: - name: build example module + name: build ${{ matrix.module }} (wasm32-wasip2) runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + module: + - example + - twap-monitor + - ethflow-watcher + - price-alert + - balance-tracker + - stop-loss steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master 2026-03-27 @@ -65,4 +89,12 @@ jobs: toolchain: nightly targets: wasm32-wasip2 - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 - - run: cargo build -p example --target wasm32-wasip2 --release + - run: cargo build -p ${{ matrix.module }} --target wasm32-wasip2 --release + - name: report wasm size + run: | + artifact_name=$(echo "${{ matrix.module }}" | tr '-' '_') + wasm_path="target/wasm32-wasip2/release/${artifact_name}.wasm" + if [ -f "$wasm_path" ]; then + size=$(wc -c < "$wasm_path") + echo "${{ matrix.module }} .wasm size: ${size} bytes" + fi diff --git a/.gitignore b/.gitignore index 357bddc..25b6a11 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,11 @@ Thumbs.db # Environment .env .env.* + +# Agent skills / AI tooling — installed locally, never committed. +.agents/ +.claude/ +skills-lock.json + +# Engine runtime state (default state_dir from engine.toml). +data/ diff --git a/Cargo.toml b/Cargo.toml index d14c23e..955c9b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,14 @@ [workspace] members = [ "crates/nexum-engine", + "crates/shepherd-sdk", + "crates/shepherd-sdk-test", + "modules/ethflow-watcher", "modules/example", + "modules/examples/balance-tracker", + "modules/examples/price-alert", + "modules/examples/stop-loss", + "modules/twap-monitor", ] resolver = "2" @@ -10,6 +17,20 @@ edition = "2024" license = "AGPL-3.0" repository = "https://github.com/nullisLabs/shepherd" +# `cowprotocol` v1.0.0-alpha.3 (the crates.io release the engine +# depends on) was cut from `cowdao-grants/cow-rs` PR #5 at commit +# `1742ffa`. `bleu/cow-rs` main has diverged since with: the +# `composable::Proof` width fix (relevant to the TWAP poll path), +# `OrderCreation` zero-from-address fast-fail, the `order_book` / +# `composable` submodule splits, `OrderPostErrorKind` + `retry_hint()` +# (BLEU-822, the protocol-level retry contract M2 modules dispatch +# on), and `OrderBookApi::with_base_url(chain, base_url)` for barn / +# staging routing (BLEU-823). Patching to that commit picks the lot +# up without waiting for an alpha.4 publish. Drop once +# `cowprotocol >= 1.0.0-alpha.4` ships. +[patch.crates-io] +cowprotocol = { git = "https://github.com/bleu/cow-rs", rev = "57f5f553ab28c9fff54089daf2d39b4282f3e4dd" } + [profile.dev] panic = "abort" diff --git a/README.md b/README.md index d146082..e44e9d4 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,71 @@ just build # Run the runtime against the example module just run + +# Run unit tests +just test ``` Without Nix, you need: Rust (edition 2024, see `rust-toolchain.toml` if present), the `wasm32-wasip2` target, and `wasm-tools`. +## Running + +### Single-module (development) + +```sh +nexum-engine [] +``` + +The `module.toml` is optional; without it the engine prints a deprecation warning and loads the module with empty capabilities and config (0.1 fallback). + +### Multi-module (production) + +```sh +nexum-engine --engine-config engine.toml +``` + +`engine.toml` declares RPC endpoints, the state directory, and a `[[modules]]` list: + +```toml +[engine] +state_dir = "/var/lib/shepherd" +log_level = "info" + +[chains.1] +rpc_url = "wss://mainnet.infura.io/ws/v3/..." + +[[modules]] +path = "modules/twap-monitor/twap-monitor.wasm" +manifest = "modules/twap-monitor/module.toml" + +[[modules]] +path = "modules/ethflow-watcher/ethflow-watcher.wasm" +``` + +### Module manifest (`module.toml`) + +```toml +[module] +name = "twap-monitor" +version = "0.1.0" + +[capabilities] +required = ["chain", "local-store", "cow-api"] +optional = ["http"] + +[capabilities.http] +allow = ["api.cow.fi"] + +[[subscription]] +kind = "log" +chain_id = 1 +address = "0xC92E8bdf79f0507f65a392b0ab4667716BFE0110" # ComposableCoW + +[[subscription]] +kind = "block" +chain_id = 1 +``` + ## Documentation The `docs/` directory contains the design corpus: diff --git a/crates/nexum-engine/Cargo.toml b/crates/nexum-engine/Cargo.toml index 65768c3..047abfb 100644 --- a/crates/nexum-engine/Cargo.toml +++ b/crates/nexum-engine/Cargo.toml @@ -6,10 +6,56 @@ license.workspace = true repository.workspace = true [dependencies] +# WASM Component Model runtime. wasmtime = { version = "45", features = ["component-model"] } wasmtime-wasi = "45" + +# Async + error plumbing. anyhow = "1" +thiserror = "2" tokio = { version = "1", features = ["full"] } -getrandom = "0.4" + +# Manifest parsing. serde = { version = "1", features = ["derive"] } toml = "1" +serde_json = "1" + +# Observability. `tracing` replaces the prior `eprintln!` debug log +# so the engine can drop into a structured log pipeline in production. +tracing = "0.1" +tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "env-filter", "ansi"] } + +# `cow-api` backend. cowprotocol pulls `OrderBookApi`, `OrderCreation`, +# `OrderUid`, the orderbook base URL table per `Chain`, and the typed +# error surface the host re-projects into `HostError`. Pinned to the +# crates.io release Shepherd is shipping against. +cowprotocol = "1.0.0-alpha" +# REST passthrough for `cow_api::request`. cowprotocol pulls reqwest +# transitively for its own client; we depend on it directly so the +# import is explicit and survives any future cowprotocol feature +# rearrangement. +reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] } + +# `chain` backend. Each configured chain owns a `DynProvider` built +# from a `WsConnect`/`Http` transport so the host's `request` / +# `request-batch` impls can hand a raw `(method, params)` pair to +# alloy's JSON-RPC layer without reimplementing the codec. +alloy-provider = { version = "1.5", default-features = false, features = ["ws", "ipc", "pubsub", "reqwest"] } +alloy-rpc-client = { version = "1.5", default-features = false } +alloy-rpc-types-eth = { version = "1.5", default-features = false, features = ["std"] } +alloy-transport = { version = "1.5", default-features = false } +alloy-transport-ws = { version = "1.5", default-features = false } +alloy-primitives = { version = "1.5", default-features = false, features = ["std", "serde"] } +futures = "0.3" + +# `local-store` backend. Per-module namespacing is enforced +# host-side via a `[len:u8][module_name][raw_key]` prefix. +redb = "2" + +# Misc. +getrandom = "0.4" +url = "2" + +[dev-dependencies] +tempfile = "3" +wiremock = "0.6" diff --git a/crates/nexum-engine/src/bindings.rs b/crates/nexum-engine/src/bindings.rs new file mode 100644 index 0000000..9ddd00c --- /dev/null +++ b/crates/nexum-engine/src/bindings.rs @@ -0,0 +1,16 @@ +//! WIT bindings generated by `wasmtime::component::bindgen!`. +//! +//! Both `wit/nexum-host` and `wit/shepherd-cow` packages are listed +//! explicitly so wit-parser can resolve the cross-package reference +//! natively - no vendored `deps/` tree needed. The world name is fully +//! qualified. +//! +//! Every `Host` trait impl in `crate::host::impls` consumes types +//! generated here. + +wasmtime::component::bindgen!({ + path: ["../../wit/nexum-host", "../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + imports: { default: async }, + exports: { default: async }, +}); diff --git a/crates/nexum-engine/src/cli.rs b/crates/nexum-engine/src/cli.rs new file mode 100644 index 0000000..6488909 --- /dev/null +++ b/crates/nexum-engine/src/cli.rs @@ -0,0 +1,46 @@ +//! Manual CLI parser. Kept hand-rolled (instead of pulling clap) because +//! the surface is small and unlikely to grow in 0.2. + +use std::path::PathBuf; + +/// Parsed CLI surface. +/// +/// `nexum-engine [ []] [--engine-config ]` +/// +/// Positional `` is a backwards-compat shortcut that +/// synthesises a one-module engine config. Production deployments pass +/// `--engine-config` and declare modules in TOML. +#[derive(Debug, Default)] +pub struct Cli { + pub wasm: Option, + pub manifest: Option, + pub engine_config: Option, +} + +impl Cli { + pub fn parse() -> Self { + let mut args = std::env::args().skip(1); + let mut cli = Self::default(); + let mut positional = Vec::new(); + while let Some(arg) = args.next() { + match arg.as_str() { + "--engine-config" => cli.engine_config = args.next().map(PathBuf::from), + "-h" | "--help" => { + eprintln!( + "usage: nexum-engine [ []] \ + [--engine-config ]" + ); + std::process::exit(0); + } + _ => positional.push(arg), + } + } + if let Some(p) = positional.first() { + cli.wasm = Some(PathBuf::from(p)); + } + if let Some(p) = positional.get(1) { + cli.manifest = Some(PathBuf::from(p)); + } + cli + } +} diff --git a/crates/nexum-engine/src/engine_config.rs b/crates/nexum-engine/src/engine_config.rs new file mode 100644 index 0000000..9637981 --- /dev/null +++ b/crates/nexum-engine/src/engine_config.rs @@ -0,0 +1,119 @@ +//! Engine-side runtime configuration. +//! +//! Distinct from `module.toml` (module manifest): this file describes +//! the *engine*'s I/O wiring - chain RPC endpoints and the on-disk +//! location of the `local-store` database. Both are required for the +//! 0.2 reference engine to do anything other than print stubs. +//! +//! Lookup order: +//! +//! 1. `--engine-config ` CLI flag (future), or third positional +//! argument today; +//! 2. `engine.toml` in the current working directory; +//! 3. defaults - no chains configured, `state_dir = ./data`. +//! +//! A missing config is OK for the example module (it only logs); for +//! the cow-api / chain backends it surfaces as `HostError { +//! kind: unsupported }` so guests learn early. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use serde::Deserialize; +use tracing::{info, warn}; + +/// Engine-side configuration loaded from `engine.toml`. +#[derive(Debug, Default, Deserialize)] +pub struct EngineConfig { + #[serde(default)] + pub engine: EngineSection, + /// Per-chain RPC URLs keyed by EVM chain id (decimal in TOML). + /// Used by the `chain::request` host call and as the alloy provider + /// pool seed. + #[serde(default)] + pub chains: BTreeMap, + /// Modules the supervisor should boot. Each entry resolves a + /// `(component.wasm, module.toml)` pair on the local filesystem + /// for 0.2 - content-addressed resolution (Swarm / OCI / + /// `[[content.sources]]`) lands in 0.3 per + /// `docs/03-module-discovery.md`. + #[serde(default)] + pub modules: Vec, +} + +/// One `[[modules]]` table from `engine.toml`. +/// +/// Both fields are filesystem paths in 0.2. `manifest` defaults to +/// `module.toml` next to `path` if omitted, matching the bundle layout +/// in `docs/02-modules-events-packaging.md`. +#[derive(Debug, Deserialize)] +pub struct ModuleEntry { + /// Path to the compiled `.wasm` component. + pub path: std::path::PathBuf, + /// Path to the module's `module.toml`. Defaults to `/module.toml`. + #[serde(default)] + pub manifest: Option, +} + +#[derive(Debug, Deserialize)] +pub struct EngineSection { + #[serde(default = "default_state_dir")] + pub state_dir: PathBuf, + /// `tracing_subscriber::EnvFilter`-compatible directive. Defaults to + /// `info` when absent; `RUST_LOG` overrides at process start. + #[serde(default = "default_log_level")] + pub log_level: String, +} + +impl Default for EngineSection { + fn default() -> Self { + Self { + state_dir: default_state_dir(), + log_level: default_log_level(), + } + } +} + +#[derive(Debug, Deserialize)] +pub struct ChainConfig { + /// JSON-RPC endpoint. `ws://` and `wss://` engage alloy's pubsub + /// transport (required for `eth_subscribe`); `http://` and `https://` + /// engage the HTTP transport (request/response only). + pub rpc_url: String, +} + +fn default_state_dir() -> PathBuf { + PathBuf::from("./data") +} + +fn default_log_level() -> String { + "info".to_owned() +} + +/// Read an engine config from disk, returning defaults if the file is +/// missing. Parse errors propagate. +pub fn load_or_default(path: Option<&Path>) -> anyhow::Result { + let path = match path { + Some(p) => p.to_path_buf(), + None => PathBuf::from("engine.toml"), + }; + + if !path.exists() { + warn!( + path = %path.display(), + "engine.toml not found - running with defaults (no chain RPC endpoints; \ + chain::request and cow_api::submit_order will return Unsupported)" + ); + return Ok(EngineConfig::default()); + } + + let raw = std::fs::read_to_string(&path)?; + let cfg: EngineConfig = toml::from_str(&raw)?; + info!( + path = %path.display(), + chains = cfg.chains.len(), + state_dir = %cfg.engine.state_dir.display(), + "engine config loaded", + ); + Ok(cfg) +} diff --git a/crates/nexum-engine/src/host/cow_orderbook.rs b/crates/nexum-engine/src/host/cow_orderbook.rs new file mode 100644 index 0000000..865ab88 --- /dev/null +++ b/crates/nexum-engine/src/host/cow_orderbook.rs @@ -0,0 +1,140 @@ +//! `shepherd:cow/cow-api` backend. +//! +//! Two responsibilities: +//! +//! 1. `request` - generic REST passthrough. Module gives the HTTP +//! method, path (relative to the chain's orderbook base URL), and +//! optional JSON body. We dispatch via `reqwest`, return the +//! response body verbatim. +//! 2. `submit_order` - typed submission. Module gives a JSON-encoded +//! `cowprotocol::OrderCreation`; we parse, dispatch via +//! `cowprotocol::OrderBookApi::post_order`, return the assigned +//! `OrderUid` as a `0x`-prefixed hex string. +//! +//! Per-chain `OrderBookApi` instances are constructed once at engine +//! boot from the discriminated chain set in `cowprotocol::Chain`. +//! Chains the SDK does not know about return `Unsupported` at the +//! host call boundary. + +use std::collections::BTreeMap; + +use cowprotocol::{Chain, OrderBookApi, OrderCreation, OrderUid}; +use thiserror::Error; + +/// Process-wide pool of `OrderBookApi` clients keyed by EVM chain id. +#[derive(Debug, Clone)] +pub struct OrderBookPool { + clients: BTreeMap, + http: reqwest::Client, +} + +impl Default for OrderBookPool { + /// Build a pool covering every `cowprotocol::Chain` variant. Each entry + /// uses the canonical `api.cow.fi/{slug}/api/v1` base URL from the SDK. + /// Override individual entries via `OrderBookApi::new_with_base_url` for + /// barn or staging targets. + fn default() -> Self { + let http = reqwest::Client::new(); + let chains = [ + Chain::Mainnet, + Chain::Gnosis, + Chain::Sepolia, + Chain::ArbitrumOne, + Chain::Base, + ]; + let clients = chains + .iter() + .map(|c| (c.id(), OrderBookApi::new(*c))) + .collect(); + Self { clients, http } + } +} + +impl OrderBookPool { + /// Look up the client for a chain. + pub fn get(&self, chain_id: u64) -> Result<&OrderBookApi, CowApiError> { + self.clients + .get(&chain_id) + .ok_or(CowApiError::UnknownChain(chain_id)) + } + + /// REST passthrough. The base URL is whichever URL the pool's + /// `OrderBookApi` client carries - overrides set via + /// `OrderBookApi::new_with_base_url` (staging, wiremock) flow + /// through here too, which keeps the passthrough and the typed + /// `submit_order_json` path aimed at the same orderbook. + pub async fn request( + &self, + chain_id: u64, + method: &str, + path: &str, + body: Option<&str>, + ) -> Result { + let api = self.get(chain_id)?; + let base = api.base_url().clone(); + // `path` may or may not lead with a slash; `Url::join` handles + // both, but we strip a single leading `/` so consumers can + // write either `/orders/...` or `orders/...` interchangeably. + let trimmed = path.strip_prefix('/').unwrap_or(path); + let url = base + .join(trimmed) + .map_err(|e| CowApiError::BadPath(format!("{path:?}: {e}")))?; + + let request = match method.to_ascii_uppercase().as_str() { + "GET" => self.http.get(url), + "POST" => self.http.post(url), + "PUT" => self.http.put(url), + "DELETE" => self.http.delete(url), + other => return Err(CowApiError::BadMethod(other.to_owned())), + }; + let request = if let Some(body) = body { + request + .header(reqwest::header::CONTENT_TYPE, "application/json") + .body(body.to_owned()) + } else { + request + }; + + let response = request.send().await.map_err(CowApiError::Network)?; + // Surface the orderbook's structured 4xx / 5xx bodies verbatim + // so the guest can decode `{"errorType": "...", "description": + // "..."}` - projecting them into HostError here loses the + // detail the guest needs to recover. + let text = response.text().await.map_err(CowApiError::Network)?; + Ok(text) + } + + /// Typed submission. `body` is the JSON encoding of + /// `cowprotocol::OrderCreation`. The chain's orderbook validates + /// `from`, the EIP-712 hash, and (if `Eip1271`) the contract + /// signature; we return whatever UID it assigns. + pub async fn submit_order_json( + &self, + chain_id: u64, + body: &[u8], + ) -> Result { + let creation: OrderCreation = serde_json::from_slice(body).map_err(CowApiError::Decode)?; + let api = self.get(chain_id)?; + let uid = api.post_order(&creation).await?; + Ok(uid) + } +} + +#[derive(Debug, Error)] +pub enum CowApiError { + #[error("unknown chain {0} (no cowprotocol::Chain variant)")] + UnknownChain(u64), + #[error("bad HTTP method `{0}` (expected GET/POST/PUT/DELETE)")] + BadMethod(String), + #[error("invalid path: {0}")] + BadPath(String), + #[error("network: {0}")] + Network(#[from] reqwest::Error), + #[error("decode OrderCreation JSON: {0}")] + Decode(#[from] serde_json::Error), + #[error("orderbook: {0}")] + Orderbook(#[from] cowprotocol::Error), +} + +#[cfg(test)] +mod tests; diff --git a/crates/nexum-engine/src/host/cow_orderbook/tests.rs b/crates/nexum-engine/src/host/cow_orderbook/tests.rs new file mode 100644 index 0000000..ef318c9 --- /dev/null +++ b/crates/nexum-engine/src/host/cow_orderbook/tests.rs @@ -0,0 +1,208 @@ +use super::*; +use wiremock::matchers::{method, path}; +use wiremock::{Mock, MockServer, ResponseTemplate}; + +#[test] +fn pool_indexes_default_chains() { + let pool = OrderBookPool::default(); + assert!(pool.get(1).is_ok(), "mainnet present"); + assert!(pool.get(100).is_ok(), "gnosis present"); + assert!(pool.get(11_155_111).is_ok(), "sepolia present"); + assert!(pool.get(42_161).is_ok(), "arbitrum present"); + assert!(pool.get(8_453).is_ok(), "base present"); +} + +#[test] +fn unknown_chain_surfaces_typed_error() { + let pool = OrderBookPool::default(); + assert!(matches!( + pool.get(99_999), + Err(CowApiError::UnknownChain(99_999)) + )); +} + +/// Build a pool whose Mainnet entry points at `mock.uri()`. +/// `OrderBookApi::new_with_base_url` ships in cowprotocol; we +/// rely on it so wiremock-driven tests can exercise the full +/// request path without re-implementing the HTTP client. +fn pool_with_mainnet_at(mock: &MockServer) -> OrderBookPool { + let mut clients = std::collections::BTreeMap::new(); + clients.insert( + Chain::Mainnet.id(), + OrderBookApi::new_with_base_url(mock.uri().parse().expect("mock uri parses")), + ); + OrderBookPool { + clients, + http: reqwest::Client::new(), + } +} + +#[tokio::test] +async fn request_passes_get_path_through() { + let mock = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/api/v1/version")) + .respond_with(ResponseTemplate::new(200).set_body_string(r#"{"version":"x.y.z"}"#)) + .expect(1) + .mount(&mock) + .await; + + let pool = pool_with_mainnet_at(&mock); + let body = pool + .request(Chain::Mainnet.id(), "GET", "/api/v1/version", None) + .await + .expect("request succeeds"); + assert_eq!(body, r#"{"version":"x.y.z"}"#); +} + +#[tokio::test] +async fn request_relative_path_works() { + // Module passes a path without a leading slash. The + // passthrough should still resolve against the orderbook + // base URL. + let mock = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/api/v1/native_price/0xabc")) + .respond_with(ResponseTemplate::new(200).set_body_string("1.23")) + .expect(1) + .mount(&mock) + .await; + + let pool = pool_with_mainnet_at(&mock); + let body = pool + .request( + Chain::Mainnet.id(), + "GET", + "api/v1/native_price/0xabc", + None, + ) + .await + .expect("relative path resolves"); + assert_eq!(body, "1.23"); +} + +#[tokio::test] +async fn request_rejects_unknown_method() { + let pool = OrderBookPool::default(); + let err = pool + .request(Chain::Mainnet.id(), "PATCH", "/x", None) + .await + .unwrap_err(); + assert!(matches!(err, CowApiError::BadMethod(_))); +} + +#[tokio::test] +async fn request_post_with_body_is_forwarded() { + let mock = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/api/v1/quote")) + .respond_with(ResponseTemplate::new(200).set_body_string(r#"{"quote":"ok"}"#)) + .expect(1) + .mount(&mock) + .await; + + let pool = pool_with_mainnet_at(&mock); + let body = pool + .request( + Chain::Mainnet.id(), + "POST", + "/api/v1/quote", + Some(r#"{"sellToken":"0x01"}"#), + ) + .await + .expect("post with body succeeds"); + assert_eq!(body, r#"{"quote":"ok"}"#); +} + +#[tokio::test] +async fn request_4xx_response_is_returned_verbatim() { + // The host must NOT surface a 4xx as an error - the module + // needs the structured JSON body to decode `OrderPostError`. + let mock = MockServer::start().await; + let error_body = r#"{"errorType":"InsufficientFee","description":"fee too low"}"#; + Mock::given(method("POST")) + .and(path("/api/v1/orders")) + .respond_with(ResponseTemplate::new(400).set_body_string(error_body)) + .expect(1) + .mount(&mock) + .await; + + let pool = pool_with_mainnet_at(&mock); + let body = pool + .request( + Chain::Mainnet.id(), + "POST", + "/api/v1/orders", + Some(r#"{"test":true}"#), + ) + .await + .expect("4xx body is returned, not an Err"); + assert_eq!(body, error_body); +} + +#[tokio::test] +async fn request_rejects_unknown_chain() { + let pool = OrderBookPool::default(); + let err = pool.request(99_999, "GET", "/x", None).await.unwrap_err(); + assert!(matches!(err, CowApiError::UnknownChain(99_999))); +} + +#[tokio::test] +async fn submit_order_propagates_orderbook_response() { + let mock = MockServer::start().await; + let body_json = sample_order_json(); + // cowprotocol POST /api/v1/orders returns the order UID + // (56-byte hex) as a JSON string body. + let returned_uid = format!("\"0x{}\"", "ab".repeat(56)); + Mock::given(method("POST")) + .and(path("/api/v1/orders")) + .respond_with(ResponseTemplate::new(201).set_body_string(returned_uid.clone())) + .expect(1) + .mount(&mock) + .await; + + let pool = pool_with_mainnet_at(&mock); + let uid = pool + .submit_order_json(Chain::Mainnet.id(), body_json.as_bytes()) + .await + .expect("submit succeeds"); + assert_eq!(uid.as_slice().len(), 56); + assert_eq!(uid.as_slice(), &[0xab; 56]); +} + +/// A minimal but accepted-by-cowprotocol OrderCreation JSON. We +/// generate it inside the test so the JSON shape stays in lockstep +/// with the published `cowprotocol` version. +fn sample_order_json() -> String { + use alloy_primitives::{Address, U256}; + use cowprotocol::OrderCreation; + use cowprotocol::app_data::{EMPTY_APP_DATA_HASH, EMPTY_APP_DATA_JSON}; + use cowprotocol::order::{BuyTokenDestination, OrderData, OrderKind, SellTokenSource}; + use cowprotocol::signature::Signature; + use cowprotocol::signing_scheme::SigningScheme; + + let order_data = OrderData { + sell_token: Address::from([0x01; 20]), + buy_token: Address::from([0x02; 20]), + receiver: None, + sell_amount: U256::from(100u64), + buy_amount: U256::from(99u64), + valid_to: u32::MAX, + app_data: EMPTY_APP_DATA_HASH, + fee_amount: U256::ZERO, + kind: OrderKind::Sell, + partially_fillable: false, + sell_token_balance: SellTokenSource::Erc20, + buy_token_balance: BuyTokenDestination::Erc20, + }; + let signature = Signature::from_bytes(SigningScheme::PreSign, &[]).expect("presign empty"); + let creation = OrderCreation::from_signed_order_data( + &order_data, + signature, + Address::from([0x03; 20]), + EMPTY_APP_DATA_JSON.to_owned(), + None, + ) + .expect("valid OrderCreation"); + serde_json::to_string(&creation).expect("serialise OrderCreation") +} diff --git a/crates/nexum-engine/src/host/error.rs b/crates/nexum-engine/src/host/error.rs new file mode 100644 index 0000000..15b7255 --- /dev/null +++ b/crates/nexum-engine/src/host/error.rs @@ -0,0 +1,42 @@ +//! Small constructors that wrap the WIT `HostError` shape, used by +//! every `Host` trait impl, plus the lowercase hex encoder shared by +//! the `cow-api` submission path. + +use crate::bindings::HostError; +use crate::bindings::nexum::host::types::HostErrorKind; + +/// `Unsupported` (HTTP 501-style) error for capabilities the engine +/// reference build does not implement yet. +pub(crate) fn unimplemented(domain: &str, detail: impl Into) -> HostError { + HostError { + domain: domain.into(), + kind: HostErrorKind::Unsupported, + code: 501, + message: detail.into(), + data: None, + } +} + +/// `Internal` (HTTP 500-style) error for unexpected backend failures. +pub(crate) fn internal_error(domain: &str, detail: impl Into) -> HostError { + HostError { + domain: domain.into(), + kind: HostErrorKind::Internal, + code: 0, + message: detail.into(), + data: None, + } +} + +/// Lowercase hex encoder. Kept in the engine binary rather than +/// pulling a `hex` crate just for one call site. Writes into the +/// pre-allocated buffer to avoid the per-byte `String` allocation +/// `format!("{b:02x}")` would do. +pub(crate) fn hex_encode(bytes: &[u8]) -> String { + use std::fmt::Write as _; + let mut s = String::with_capacity(bytes.len() * 2); + for b in bytes { + write!(s, "{b:02x}").expect("writing to String never fails"); + } + s +} diff --git a/crates/nexum-engine/src/host/impls/chain.rs b/crates/nexum-engine/src/host/impls/chain.rs new file mode 100644 index 0000000..e0e30db --- /dev/null +++ b/crates/nexum-engine/src/host/impls/chain.rs @@ -0,0 +1,67 @@ +//! `nexum:host/chain`: raw JSON-RPC dispatch over alloy. + +use std::time::Instant; + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::bindings::nexum::host::types::HostErrorKind; +use crate::host::error::internal_error; +use crate::host::provider_pool::ProviderError; +use crate::host::state::HostState; + +impl nexum::host::chain::Host for HostState { + async fn request( + &mut self, + chain_id: u64, + method: String, + params: String, + ) -> Result { + let start = Instant::now(); + tracing::debug!(chain_id, %method, "chain::request"); + let result = match self.chain.request(chain_id, method, params).await { + Ok(body) => Ok(body), + Err(ProviderError::UnknownChain(id)) => Err(HostError { + domain: "chain".into(), + kind: HostErrorKind::Unsupported, + code: 0, + message: format!("chain {id} has no engine.toml RPC entry"), + data: None, + }), + Err(ProviderError::InvalidParams { detail, .. }) => Err(HostError { + domain: "chain".into(), + kind: HostErrorKind::InvalidInput, + code: -32602, + message: detail, + data: None, + }), + Err(ProviderError::Rpc { detail, .. }) => Err(HostError { + domain: "chain".into(), + kind: HostErrorKind::Internal, + code: -32603, + message: detail, + data: None, + }), + Err(err) => Err(internal_error("chain", err.to_string())), + }; + tracing::trace!(elapsed_ms = ?start.elapsed(), "chain::request done"); + result + } + + async fn request_batch( + &mut self, + chain_id: u64, + requests: Vec, + ) -> Result, HostError> { + let start = Instant::now(); + tracing::debug!(chain_id, count = requests.len(), "chain::request-batch"); + let mut out = Vec::with_capacity(requests.len()); + for req in requests { + match nexum::host::chain::Host::request(self, chain_id, req.method, req.params).await { + Ok(s) => out.push(nexum::host::chain::RpcResult::Ok(s)), + Err(e) => out.push(nexum::host::chain::RpcResult::Err(e)), + } + } + tracing::trace!(elapsed_ms = ?start.elapsed(), "chain::request-batch done"); + Ok(out) + } +} diff --git a/crates/nexum-engine/src/host/impls/clock.rs b/crates/nexum-engine/src/host/impls/clock.rs new file mode 100644 index 0000000..f65b674 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/clock.rs @@ -0,0 +1,19 @@ +//! `nexum:host/clock`: wall-clock + monotonic time. + +use std::time::{SystemTime, UNIX_EPOCH}; + +use crate::bindings::nexum; +use crate::host::state::HostState; + +impl nexum::host::clock::Host for HostState { + async fn now_ms(&mut self) -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_millis() as u64) + .unwrap_or(0) + } + + async fn monotonic_ns(&mut self) -> u64 { + self.monotonic_baseline.elapsed().as_nanos() as u64 + } +} diff --git a/crates/nexum-engine/src/host/impls/cow_api.rs b/crates/nexum-engine/src/host/impls/cow_api.rs new file mode 100644 index 0000000..b3b51d5 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/cow_api.rs @@ -0,0 +1,85 @@ +//! `shepherd:cow/cow-api`: REST passthrough + typed `submit_order`. +//! Backend logic lives in [`crate::host::cow_orderbook`]; this is the +//! WIT-side error mapping. + +use std::time::Instant; + +use crate::bindings::nexum::host::types::HostErrorKind; +use crate::bindings::{HostError, shepherd}; +use crate::host::cow_orderbook::CowApiError; +use crate::host::error::{hex_encode, internal_error, unimplemented}; +use crate::host::state::HostState; + +impl shepherd::cow::cow_api::Host for HostState { + async fn request( + &mut self, + chain_id: u64, + method: String, + path: String, + body: Option, + ) -> Result { + let start = Instant::now(); + tracing::debug!(chain_id, %method, %path, "cow-api::request"); + let result = match self + .cow + .request(chain_id, &method, &path, body.as_deref()) + .await + { + Ok(body) => Ok(body), + Err(CowApiError::UnknownChain(id)) => Err(unimplemented( + "cow-api", + format!("chain {id} not in cowprotocol"), + )), + Err(CowApiError::BadMethod(m)) => Err(HostError { + domain: "cow-api".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("unsupported HTTP method: {m}"), + data: None, + }), + Err(CowApiError::BadPath(msg)) => Err(HostError { + domain: "cow-api".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: msg, + data: None, + }), + Err(err) => Err(internal_error("cow-api", err.to_string())), + }; + tracing::trace!(elapsed_ms = ?start.elapsed(), "cow-api::request done"); + result + } + + async fn submit_order( + &mut self, + chain_id: u64, + order_data: Vec, + ) -> Result { + let start = Instant::now(); + tracing::debug!(chain_id, bytes = order_data.len(), "cow-api::submit-order"); + let result = match self.cow.submit_order_json(chain_id, &order_data).await { + Ok(uid) => Ok(format!("0x{}", hex_encode(uid.as_slice()))), + Err(CowApiError::UnknownChain(id)) => Err(unimplemented( + "cow-api", + format!("chain {id} not in cowprotocol"), + )), + Err(CowApiError::Decode(err)) => Err(HostError { + domain: "cow-api".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("invalid OrderCreation JSON: {err}"), + data: None, + }), + Err(CowApiError::Orderbook(err)) => Err(HostError { + domain: "cow-api".into(), + kind: HostErrorKind::Denied, + code: 0, + message: err.to_string(), + data: None, + }), + Err(err) => Err(internal_error("cow-api", err.to_string())), + }; + tracing::trace!(elapsed_ms = ?start.elapsed(), "cow-api::submit-order done"); + result + } +} diff --git a/crates/nexum-engine/src/host/impls/http.rs b/crates/nexum-engine/src/host/impls/http.rs new file mode 100644 index 0000000..2900d4d --- /dev/null +++ b/crates/nexum-engine/src/host/impls/http.rs @@ -0,0 +1,51 @@ +//! `nexum:host/http`: manifest allowlist check, then `Unsupported`. +//! +//! Real `fetch` lands in 0.3. The allowlist is enforced now so a +//! module that ships with an empty (or no) `[capabilities.http].allow` +//! gets denied loudly, matching the "no implicit network" stance. + +use tracing::warn; + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::bindings::nexum::host::types::HostErrorKind; +use crate::host::error::unimplemented; +use crate::host::state::HostState; +use crate::manifest::{extract_host, host_allowed}; + +impl nexum::host::http::Host for HostState { + async fn fetch( + &mut self, + req: nexum::host::http::Request, + ) -> Result { + let host = match extract_host(&req.url) { + Some(h) => h, + None => { + return Err(HostError { + domain: "http".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("not an http(s) URL: {}", req.url), + data: None, + }); + } + }; + if !host_allowed(host, &self.http_allowlist) { + warn!(host, "[http] denied by allowlist"); + return Err(HostError { + domain: "http".into(), + kind: HostErrorKind::Denied, + code: 0, + message: format!( + "host {host} not in [capabilities.http].allow; \ + add it to module.toml to permit" + ), + data: None, + }); + } + Err(unimplemented( + "http", + "fetch not implemented in 0.2 reference runtime (allowlist passed)", + )) + } +} diff --git a/crates/nexum-engine/src/host/impls/identity.rs b/crates/nexum-engine/src/host/impls/identity.rs new file mode 100644 index 0000000..6a4a050 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/identity.rs @@ -0,0 +1,29 @@ +//! `nexum:host/identity`: deferred to 0.3 (keystore / KMS backend). +//! `accounts()` returns an empty roster so guests can probe-then-skip; +//! signing returns `Unsupported`. + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::host::error::unimplemented; +use crate::host::state::HostState; + +impl nexum::host::identity::Host for HostState { + async fn accounts(&mut self) -> Result>, HostError> { + Ok(vec![]) + } + + async fn sign(&mut self, _account: Vec, _message: Vec) -> Result, HostError> { + Err(unimplemented("identity", "sign requires a keystore (0.3)")) + } + + async fn sign_typed_data( + &mut self, + _account: Vec, + _typed_data: String, + ) -> Result, HostError> { + Err(unimplemented( + "identity", + "sign-typed-data requires a keystore (0.3)", + )) + } +} diff --git a/crates/nexum-engine/src/host/impls/local_store.rs b/crates/nexum-engine/src/host/impls/local_store.rs new file mode 100644 index 0000000..66bcc52 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/local_store.rs @@ -0,0 +1,32 @@ +//! `nexum:host/local-store`: redb backend with host-side namespacing. + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::host::error::internal_error; +use crate::host::state::HostState; + +impl nexum::host::local_store::Host for HostState { + async fn get(&mut self, key: String) -> Result>, HostError> { + self.store + .get(&self.module_namespace, &key) + .map_err(|err| internal_error("local-store", err.to_string())) + } + + async fn set(&mut self, key: String, value: Vec) -> Result<(), HostError> { + self.store + .set(&self.module_namespace, &key, &value) + .map_err(|err| internal_error("local-store", err.to_string())) + } + + async fn delete(&mut self, key: String) -> Result<(), HostError> { + self.store + .delete(&self.module_namespace, &key) + .map_err(|err| internal_error("local-store", err.to_string())) + } + + async fn list_keys(&mut self, prefix: String) -> Result, HostError> { + self.store + .list_keys(&self.module_namespace, &prefix) + .map_err(|err| internal_error("local-store", err.to_string())) + } +} diff --git a/crates/nexum-engine/src/host/impls/logging.rs b/crates/nexum-engine/src/host/impls/logging.rs new file mode 100644 index 0000000..b3a2a02 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/logging.rs @@ -0,0 +1,18 @@ +//! `nexum:host/logging`: routes guest log lines through the host's +//! `tracing` subscriber, tagged with the module namespace. + +use crate::bindings::nexum; +use crate::host::state::HostState; + +impl nexum::host::logging::Host for HostState { + async fn log(&mut self, level: nexum::host::logging::Level, message: String) { + let module = self.module_namespace.as_str(); + match level { + nexum::host::logging::Level::Trace => tracing::trace!(module, "{}", message), + nexum::host::logging::Level::Debug => tracing::debug!(module, "{}", message), + nexum::host::logging::Level::Info => tracing::info!(module, "{}", message), + nexum::host::logging::Level::Warn => tracing::warn!(module, "{}", message), + nexum::host::logging::Level::Error => tracing::error!(module, "{}", message), + } + } +} diff --git a/crates/nexum-engine/src/host/impls/messaging.rs b/crates/nexum-engine/src/host/impls/messaging.rs new file mode 100644 index 0000000..5582b00 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/messaging.rs @@ -0,0 +1,27 @@ +//! `nexum:host/messaging`: deferred to 0.3 (Waku backend). `query` +//! returns an empty result, same posture as `identity::accounts`. + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::host::error::unimplemented; +use crate::host::state::HostState; + +impl nexum::host::messaging::Host for HostState { + async fn publish( + &mut self, + _content_topic: String, + _payload: Vec, + ) -> Result<(), HostError> { + Err(unimplemented("messaging", "Waku backend deferred to 0.3")) + } + + async fn query( + &mut self, + _content_topic: String, + _start_time: Option, + _end_time: Option, + _limit: Option, + ) -> Result, HostError> { + Ok(vec![]) + } +} diff --git a/crates/nexum-engine/src/host/impls/mod.rs b/crates/nexum-engine/src/host/impls/mod.rs new file mode 100644 index 0000000..8256af9 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/mod.rs @@ -0,0 +1,19 @@ +//! `Host` trait impls for [`crate::host::state::HostState`], one +//! file per WIT interface. +//! +//! The interfaces themselves (and their generated trait shapes) live +//! in [`crate::bindings`]; this module only contains the dispatch +//! glue between the WIT signature and the corresponding backend in +//! [`crate::host`]. + +mod chain; +mod clock; +mod cow_api; +mod http; +mod identity; +mod local_store; +mod logging; +mod messaging; +mod random; +mod remote_store; +mod types; diff --git a/crates/nexum-engine/src/host/impls/random.rs b/crates/nexum-engine/src/host/impls/random.rs new file mode 100644 index 0000000..88e2f8c --- /dev/null +++ b/crates/nexum-engine/src/host/impls/random.rs @@ -0,0 +1,16 @@ +//! `nexum:host/random`: fills `len` bytes from the OS CSPRNG. +//! Getrandom 0.4 failures are exceptionally rare on supported +//! platforms; on failure we return zero-filled bytes - guests that +//! need a strong-failure signal should use identity or chain primitives +//! instead. + +use crate::bindings::nexum; +use crate::host::state::HostState; + +impl nexum::host::random::Host for HostState { + async fn fill(&mut self, len: u32) -> Vec { + let mut buf = vec![0u8; len as usize]; + let _ = getrandom::fill(&mut buf); + buf + } +} diff --git a/crates/nexum-engine/src/host/impls/remote_store.rs b/crates/nexum-engine/src/host/impls/remote_store.rs new file mode 100644 index 0000000..9001d1f --- /dev/null +++ b/crates/nexum-engine/src/host/impls/remote_store.rs @@ -0,0 +1,40 @@ +//! `nexum:host/remote-store`: deferred to 0.3 (Swarm backend). + +use crate::bindings::HostError; +use crate::bindings::nexum; +use crate::host::error::unimplemented; +use crate::host::state::HostState; + +impl nexum::host::remote_store::Host for HostState { + async fn upload(&mut self, _data: Vec) -> Result, HostError> { + Err(unimplemented( + "remote-store", + "Swarm backend deferred to 0.3", + )) + } + + async fn download(&mut self, _reference: Vec) -> Result, HostError> { + Err(unimplemented( + "remote-store", + "Swarm backend deferred to 0.3", + )) + } + + async fn read_feed( + &mut self, + _owner: Vec, + _topic: Vec, + ) -> Result>, HostError> { + Err(unimplemented( + "remote-store", + "Swarm backend deferred to 0.3", + )) + } + + async fn write_feed(&mut self, _topic: Vec, _data: Vec) -> Result, HostError> { + Err(unimplemented( + "remote-store", + "Swarm backend deferred to 0.3", + )) + } +} diff --git a/crates/nexum-engine/src/host/impls/types.rs b/crates/nexum-engine/src/host/impls/types.rs new file mode 100644 index 0000000..c4a93e1 --- /dev/null +++ b/crates/nexum-engine/src/host/impls/types.rs @@ -0,0 +1,7 @@ +//! `nexum:host/types` is a type-only interface (no functions). The +//! generated trait is empty; we just provide the marker impl. + +use crate::bindings::nexum; +use crate::host::state::HostState; + +impl nexum::host::types::Host for HostState {} diff --git a/crates/nexum-engine/src/host/local_store_redb.rs b/crates/nexum-engine/src/host/local_store_redb.rs new file mode 100644 index 0000000..c832137 --- /dev/null +++ b/crates/nexum-engine/src/host/local_store_redb.rs @@ -0,0 +1,158 @@ +//! `nexum:host/local-store` backend. +//! +//! Single redb file under `EngineConfig.engine.state_dir`. Per-module +//! namespacing is enforced host-side via a fixed-length 32-byte prefix: +//! `keccak256(module_name) ++ raw_key`. Two modules using the same key +//! string see disjoint data regardless of how similar their names are. +//! +//! The 32-byte hash prefix has two properties that the old +//! `[len:u8][name][key]` scheme lacked: +//! +//! - **Fixed width** - no length field to forge; a module cannot craft a +//! key that bleeds into another module's prefix range. +//! - **ENS-compatible** - keccak256 is the same hash used by ENS node +//! derivation, so module identities can be derived from ENS names +//! without extra hashing in the future (ADR-0003). + +#![allow(clippy::result_large_err)] + +use std::path::Path; +use std::sync::Arc; + +use alloy_primitives::keccak256; +use redb::{Database, TableDefinition}; +use thiserror::Error; + +const TABLE: TableDefinition<'static, &[u8], &[u8]> = TableDefinition::new("nexum:local-store"); +#[cfg(test)] +const PREFIX_LEN: usize = 32; + +/// Process-wide handle to the local-store redb database. Cheap to +/// clone; the per-module view is constructed by setting the namespace +/// prefix at call time. +#[derive(Debug, Clone)] +pub struct LocalStore { + db: Arc, +} + +impl LocalStore { + /// Open (or create) the redb file at `path`. Materialises the shared + /// table so subsequent read transactions never hit `TableDoesNotExist`. + pub fn open(path: impl AsRef) -> Result { + let db = Database::create(path).map_err(StorageError::Open)?; + { + let txn = db.begin_write().map_err(StorageError::Txn)?; + txn.open_table(TABLE).map_err(StorageError::Table)?; + txn.commit().map_err(StorageError::Commit)?; + } + Ok(Self { db: Arc::new(db) }) + } + + /// Fetch a value for `(namespace, key)`. Returns `Ok(None)` when + /// no entry exists; the module never observes the prefix. + pub fn get(&self, namespace: &str, key: &str) -> Result>, StorageError> { + let full = build_key(namespace, key)?; + let txn = self.db.begin_read().map_err(StorageError::Txn)?; + let table = txn.open_table(TABLE).map_err(StorageError::Table)?; + let value = table + .get(full.as_slice()) + .map_err(StorageError::Storage)? + .map(|v| v.value().to_vec()); + Ok(value) + } + + /// Insert or overwrite. + pub fn set(&self, namespace: &str, key: &str, value: &[u8]) -> Result<(), StorageError> { + let full = build_key(namespace, key)?; + let txn = self.db.begin_write().map_err(StorageError::Txn)?; + { + let mut table = txn.open_table(TABLE).map_err(StorageError::Table)?; + table + .insert(full.as_slice(), value) + .map_err(StorageError::Storage)?; + } + txn.commit().map_err(StorageError::Commit)?; + Ok(()) + } + + /// Delete. Idempotent - deleting a missing key is a no-op. + pub fn delete(&self, namespace: &str, key: &str) -> Result<(), StorageError> { + let full = build_key(namespace, key)?; + let txn = self.db.begin_write().map_err(StorageError::Txn)?; + { + let mut table = txn.open_table(TABLE).map_err(StorageError::Table)?; + table + .remove(full.as_slice()) + .map_err(StorageError::Storage)?; + } + txn.commit().map_err(StorageError::Commit)?; + Ok(()) + } + + /// Enumerate keys in `namespace` whose raw key (post-prefix) starts + /// with `prefix`. Returns only the module-visible key strings - the + /// host strips the namespace prefix. + pub fn list_keys(&self, namespace: &str, prefix: &str) -> Result, StorageError> { + let ns_prefix = namespace_prefix(namespace)?; + let full_prefix = build_key(namespace, prefix)?; + let txn = self.db.begin_read().map_err(StorageError::Txn)?; + let table = txn.open_table(TABLE).map_err(StorageError::Table)?; + let mut out = Vec::new(); + // redb's B-tree iterates keys in sorted order, so a range + // starting at `full_prefix` only touches matching entries (and + // the first key past the prefix range). Breaking on the first + // non-matching key keeps this O(matching entries) instead of + // the O(total DB entries) `table.iter()` would do. + for entry in table + .range(full_prefix.as_slice()..) + .map_err(StorageError::Storage)? + { + let (k, _v) = entry.map_err(StorageError::Storage)?; + let key_bytes = k.value(); + if !key_bytes.starts_with(&full_prefix) { + break; + } + if let Ok(s) = std::str::from_utf8(&key_bytes[ns_prefix.len()..]) { + out.push(s.to_owned()); + } + } + Ok(out) + } +} + +/// Returns the 32-byte keccak256 hash of `namespace` as a `Vec`. +/// Rejects the empty string so callers can rely on a non-trivial prefix. +fn namespace_prefix(namespace: &str) -> Result, StorageError> { + if namespace.is_empty() { + return Err(StorageError::InvalidNamespace( + "module namespace must not be empty".into(), + )); + } + Ok(keccak256(namespace.as_bytes()).to_vec()) +} + +fn build_key(namespace: &str, key: &str) -> Result, StorageError> { + let mut out = namespace_prefix(namespace)?; + out.extend_from_slice(key.as_bytes()); + Ok(out) +} + +/// Errors surfaced by [`LocalStore`]. +#[derive(Debug, Error)] +pub enum StorageError { + #[error("open redb: {0}")] + Open(#[source] redb::DatabaseError), + #[error("redb txn: {0}")] + Txn(#[source] redb::TransactionError), + #[error("redb table: {0}")] + Table(#[source] redb::TableError), + #[error("redb storage: {0}")] + Storage(#[source] redb::StorageError), + #[error("redb commit: {0}")] + Commit(#[source] redb::CommitError), + #[error("invalid namespace: {0}")] + InvalidNamespace(String), +} + +#[cfg(test)] +mod tests; diff --git a/crates/nexum-engine/src/host/local_store_redb/tests.rs b/crates/nexum-engine/src/host/local_store_redb/tests.rs new file mode 100644 index 0000000..5c4feba --- /dev/null +++ b/crates/nexum-engine/src/host/local_store_redb/tests.rs @@ -0,0 +1,80 @@ +use super::*; + +fn fresh() -> (tempfile::TempDir, LocalStore) { + let dir = tempfile::tempdir().expect("tempdir"); + let store = LocalStore::open(dir.path().join("ls.redb")).expect("open"); + (dir, store) +} + +#[test] +fn set_get_roundtrip() { + let (_dir, store) = fresh(); + store.set("twap", "k", b"v").unwrap(); + assert_eq!(store.get("twap", "k").unwrap().as_deref(), Some(&b"v"[..])); +} + +#[test] +fn namespaces_isolate_modules() { + let (_dir, store) = fresh(); + store.set("a", "k", b"from-a").unwrap(); + store.set("b", "k", b"from-b").unwrap(); + assert_eq!( + store.get("a", "k").unwrap().as_deref(), + Some(&b"from-a"[..]) + ); + assert_eq!( + store.get("b", "k").unwrap().as_deref(), + Some(&b"from-b"[..]) + ); +} + +#[test] +fn delete_then_get_is_none() { + let (_dir, store) = fresh(); + store.set("twap", "k", b"v").unwrap(); + store.delete("twap", "k").unwrap(); + assert!(store.get("twap", "k").unwrap().is_none()); +} + +#[test] +fn list_keys_strips_namespace_prefix() { + let (_dir, store) = fresh(); + store.set("twap", "posted:1", b"x").unwrap(); + store.set("twap", "posted:2", b"y").unwrap(); + store.set("twap", "other", b"z").unwrap(); + let keys = store.list_keys("twap", "posted:").unwrap(); + assert_eq!(keys.len(), 2); + assert!(keys.iter().all(|k| k.starts_with("posted:"))); +} + +#[test] +fn rejects_empty_namespace() { + let (_dir, store) = fresh(); + let err = store.set("", "k", b"v").unwrap_err(); + assert!(matches!(err, StorageError::InvalidNamespace(_))); +} + +#[test] +fn prefix_is_fixed_32_bytes() { + let short = namespace_prefix("a").unwrap(); + let long = namespace_prefix(&"a".repeat(300)).unwrap(); + assert_eq!(short.len(), PREFIX_LEN); + assert_eq!(long.len(), PREFIX_LEN); + // Different inputs produce different prefixes. + assert_ne!(short, long); +} + +#[test] +fn prefix_is_deterministic() { + let p1 = namespace_prefix("twap-monitor").unwrap(); + let p2 = namespace_prefix("twap-monitor").unwrap(); + assert_eq!(p1, p2); +} + +#[test] +fn similar_names_differ() { + // Verify that names that share a common prefix don't collide. + let pa = namespace_prefix("module-a").unwrap(); + let pb = namespace_prefix("module-b").unwrap(); + assert_ne!(pa, pb); +} diff --git a/crates/nexum-engine/src/host/mod.rs b/crates/nexum-engine/src/host/mod.rs new file mode 100644 index 0000000..20f2ec2 --- /dev/null +++ b/crates/nexum-engine/src/host/mod.rs @@ -0,0 +1,21 @@ +//! Host-side backends for the `nexum:host` / `shepherd:cow` interfaces, +//! plus the per-module `HostState` and the WIT `Host` trait impls. +//! +//! Layout: +//! - [`state`]: `HostState` struct + `WasiView` impl, the receiver +//! every WIT `Host` trait is implemented for. +//! - [`error`]: small constructors that build the WIT `HostError` +//! shape (`unimplemented`, `internal_error`) plus the lowercase +//! `hex_encode` shared by the `cow-api` submission path. +//! - [`cow_orderbook`], [`provider_pool`], [`local_store_redb`]: +//! capability backends. Pure code with no bindgen types, so each +//! can be unit-tested without spinning up a wasmtime store. +//! - [`impls`] (private): the bindgen-side trait impls, one file per +//! WIT interface, that dispatch to the backends above. + +pub mod cow_orderbook; +pub mod error; +mod impls; +pub mod local_store_redb; +pub mod provider_pool; +pub mod state; diff --git a/crates/nexum-engine/src/host/provider_pool.rs b/crates/nexum-engine/src/host/provider_pool.rs new file mode 100644 index 0000000..d65e391 --- /dev/null +++ b/crates/nexum-engine/src/host/provider_pool.rs @@ -0,0 +1,234 @@ +//! `nexum:host/chain` backend. +//! +//! Per-chain alloy provider, opened from the engine config at boot. +//! `request` is a raw JSON-RPC dispatch: the host hands `(method, +//! params)` straight to alloy's transport and returns the result body +//! verbatim. No method allowlist, no re-encoding of params - the +//! contract is "give us a JSON-RPC pair, we'll return what the node +//! returns". +//! +//! Transports: +//! - `ws://` / `wss://` - `WsConnect`; required for `eth_subscribe`. +//! - `http://` / `https://` - alloy's HTTP transport; request/response only. + +use std::collections::BTreeMap; +use std::pin::Pin; +use std::sync::Arc; + +use alloy_provider::{DynProvider, Provider, ProviderBuilder, WsConnect}; +use alloy_rpc_types_eth::{Filter, Header, Log}; +use futures::stream::Stream; +use futures::stream::StreamExt as _; +use serde_json::value::RawValue; +use thiserror::Error; +use tracing::info; + +use crate::engine_config::EngineConfig; + +/// Pool of alloy providers keyed by chain id. +#[derive(Debug, Clone)] +pub struct ProviderPool { + providers: Arc>, +} + +impl ProviderPool { + /// Open one provider per chain in `cfg.chains`. WebSocket URLs + /// engage alloy's pubsub transport; HTTP URLs use the HTTP + /// transport. Connection failures propagate to the caller; the + /// engine treats them as fatal at boot. + pub async fn from_config(cfg: &EngineConfig) -> Result { + let mut providers: BTreeMap = BTreeMap::new(); + for (chain_id, chain_cfg) in &cfg.chains { + let url = chain_cfg.rpc_url.as_str(); + info!(chain_id, url, "opening chain RPC provider"); + let provider = if url.starts_with("ws://") || url.starts_with("wss://") { + ProviderBuilder::new() + .connect_ws(WsConnect::new(url)) + .await + .map_err(|e| ProviderError::Connect { + chain_id: *chain_id, + detail: e.to_string(), + })? + .erased() + } else { + let parsed: url::Url = + url.parse() + .map_err(|e: url::ParseError| ProviderError::Connect { + chain_id: *chain_id, + detail: e.to_string(), + })?; + ProviderBuilder::new().connect_http(parsed).erased() + }; + providers.insert(*chain_id, provider); + } + Ok(Self { + providers: Arc::new(providers), + }) + } + + /// Empty pool - used by tests and as a default when no + /// `engine.toml` is found. Every `request` call returns + /// `UnknownChain`. + #[cfg_attr(not(test), allow(dead_code))] + pub fn empty() -> Self { + Self { + providers: Arc::new(BTreeMap::new()), + } + } + + /// Open a new-blocks (`eth_subscribe newHeads`) stream on + /// `chain_id`. Requires a WS / IPC transport at construction + /// time; HTTP-only providers surface `UnknownChain` here. + pub async fn subscribe_blocks(&self, chain_id: u64) -> Result { + let provider = self + .providers + .get(&chain_id) + .ok_or(ProviderError::UnknownChain(chain_id))?; + let sub = provider + .subscribe_blocks() + .await + .map_err(|e| ProviderError::Rpc { + method: "eth_subscribe(newHeads)".into(), + detail: e.to_string(), + })?; + let stream = sub.into_stream().map(Ok::<_, ProviderError>); + Ok(Box::pin(stream)) + } + + /// Open an `eth_subscribe(logs, filter)` stream on `chain_id`. + pub async fn subscribe_logs( + &self, + chain_id: u64, + filter: Filter, + ) -> Result { + let provider = self + .providers + .get(&chain_id) + .ok_or(ProviderError::UnknownChain(chain_id))?; + let sub = provider + .subscribe_logs(&filter) + .await + .map_err(|e| ProviderError::Rpc { + method: "eth_subscribe(logs)".into(), + detail: e.to_string(), + })?; + let stream = sub.into_stream().map(Ok::<_, ProviderError>); + Ok(Box::pin(stream)) + } + + /// Raw JSON-RPC dispatch. `params_json` must be the JSON encoding + /// of the params array (e.g. `"[\"0x...\",\"latest\"]"`), as + /// produced by the SDK's `chain::request` glue. + pub async fn request( + &self, + chain_id: u64, + method: String, + params_json: String, + ) -> Result { + let provider = self + .providers + .get(&chain_id) + .ok_or(ProviderError::UnknownChain(chain_id))?; + // Pass the params through as a raw JSON value so alloy does + // not re-encode them on the way to the node. + let params: Box = + RawValue::from_string(params_json).map_err(|e| ProviderError::InvalidParams { + method: method.clone(), + detail: e.to_string(), + })?; + // `raw_request` consumes the method name; clone once for the + // error branch so the success path moves the original string + // straight into alloy without an extra allocation. + let method_for_err = method.clone(); + let result: Box = + provider + .raw_request(method.into(), params) + .await + .map_err(|e| ProviderError::Rpc { + method: method_for_err, + detail: e.to_string(), + })?; + Ok(result.get().to_owned()) + } +} + +/// Boxed stream of `newHeads`-style block headers. +pub type BlockStream = Pin> + Send>>; +/// Boxed stream of `logs`-filtered log events. +pub type LogStream = Pin> + Send>>; + +/// Errors surfaced by [`ProviderPool`]. +#[derive(Debug, Error)] +pub enum ProviderError { + /// Chain id absent from the engine config. + #[error("unknown chain {0} (no engine.toml entry)")] + UnknownChain(u64), + /// Could not open the underlying transport. + #[error("connect chain {chain_id}: {detail}")] + Connect { + /// Chain id we failed to dial. + chain_id: u64, + /// Transport-side error string. + detail: String, + }, + /// The guest-supplied JSON params did not parse. + #[error("invalid params JSON for `{method}`: {detail}")] + InvalidParams { + /// RPC method name. + method: String, + /// JSON-parser detail. + detail: String, + }, + /// The node returned an error for the dispatched call. + #[error("rpc `{method}` failed: {detail}")] + Rpc { + /// RPC method name. + method: String, + /// Transport-side error string. + detail: String, + }, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn empty_pool_rejects_lookups() { + let pool = ProviderPool::empty(); + let err = pool + .request(1, "eth_blockNumber".into(), "[]".into()) + .await + .unwrap_err(); + assert!(matches!(err, ProviderError::UnknownChain(1))); + } + + #[tokio::test] + async fn empty_pool_rejects_block_subscribe() { + let pool = ProviderPool::empty(); + // Can't use .unwrap_err() because BlockStream doesn't impl Debug. + assert!(matches!( + pool.subscribe_blocks(1).await, + Err(ProviderError::UnknownChain(1)) + )); + } + + #[tokio::test] + async fn empty_pool_rejects_log_subscribe() { + let pool = ProviderPool::empty(); + let filter = alloy_rpc_types_eth::Filter::new(); + assert!(matches!( + pool.subscribe_logs(1, filter).await, + Err(ProviderError::UnknownChain(1)) + )); + } + + #[tokio::test] + async fn invalid_params_json_is_rejected_before_network() { + // RawValue::from_string rejects non-JSON; verify the parse layer + // we rely on before forwarding to alloy. + let bad = "not json at all {{{"; + let result = RawValue::from_string(bad.to_owned()); + assert!(result.is_err(), "invalid JSON should fail RawValue parse"); + } +} diff --git a/crates/nexum-engine/src/host/state.rs b/crates/nexum-engine/src/host/state.rs new file mode 100644 index 0000000..ec50219 --- /dev/null +++ b/crates/nexum-engine/src/host/state.rs @@ -0,0 +1,45 @@ +//! Per-instance host state and its WASI view. +//! +//! One [`HostState`] is created per module, lives inside the wasmtime +//! `Store`, and is the receiver every `Host` trait impl in +//! [`super::impls`] is implemented for. + +use std::time::Instant; + +use wasmtime::component::ResourceTable; +use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView}; + +use super::cow_orderbook::OrderBookPool; +use super::local_store_redb::LocalStore; +use super::provider_pool::ProviderPool; + +pub(crate) struct HostState { + pub wasi: WasiCtx, + pub table: ResourceTable, + /// Wasmtime memory/table/instance resource limits for this store. + pub limits: wasmtime::StoreLimits, + /// Origin for `clock::monotonic-ns`. Differences between successive + /// readings are the only meaningful values. + pub monotonic_baseline: Instant, + /// Per-module `[capabilities.http].allow` allowlist (from module.toml). + /// Consulted by `http::fetch` before any outbound call. + pub http_allowlist: Vec, + /// Namespace for the running module's `local-store` rows. Set from + /// `manifest.module.name` at instantiation. + pub module_namespace: String, + /// `cow-api` backend - per-chain `OrderBookApi` clients + reqwest. + pub cow: OrderBookPool, + /// `chain` backend - per-chain alloy `DynProvider` pool. + pub chain: ProviderPool, + /// `local-store` backend - redb file with host-side namespacing. + pub store: LocalStore, +} + +impl WasiView for HostState { + fn ctx(&mut self) -> WasiCtxView<'_> { + WasiCtxView { + ctx: &mut self.wasi, + table: &mut self.table, + } + } +} diff --git a/crates/nexum-engine/src/main.rs b/crates/nexum-engine/src/main.rs index f013f22..ab24d15 100644 --- a/crates/nexum-engine/src/main.rs +++ b/crates/nexum-engine/src/main.rs @@ -1,377 +1,65 @@ +#![cfg_attr(not(test), warn(unused_crate_dependencies))] + +// alloy split its API across multiple crates; we depend on the +// transports directly so cargo resolves the right feature set, but +// the runtime code only names them through the `alloy_provider` +// re-exports. Silence `unused_crate_dependencies` with `as _`. +use alloy_rpc_client as _; +use alloy_transport as _; +use alloy_transport_ws as _; + +mod bindings; +mod cli; +mod engine_config; +mod host; mod manifest; +mod runtime; +mod supervisor; -use std::path::PathBuf; -use std::time::{Instant, SystemTime, UNIX_EPOCH}; -use wasmtime::component::{Component, Linker, ResourceTable}; -use wasmtime::error::Context as _; -use wasmtime::{Engine, Store}; -use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView}; +use tracing::{info, warn}; +use tracing_subscriber::EnvFilter; +use wasmtime::Engine; +use wasmtime::component::Linker; -// Both packages are listed explicitly so wit-parser can resolve the -// cross-package reference natively — no vendored deps/ tree needed. -// World name is fully qualified. -wasmtime::component::bindgen!({ - path: ["../../wit/nexum-host", "../../wit/shepherd-cow"], - world: "shepherd:cow/shepherd", - imports: { default: async }, - exports: { default: async }, -}); - -use nexum::host::types::HostErrorKind; - -struct HostState { - wasi: WasiCtx, - table: ResourceTable, - /// Origin for `clock::monotonic-ns`. Differences between successive - /// readings are the only meaningful values. - monotonic_baseline: Instant, - /// Per-module `[capabilities.http].allow` allowlist (from nexum.toml). - /// Consulted by `http::fetch` before any outbound call. - http_allowlist: Vec, -} - -impl WasiView for HostState { - fn ctx(&mut self) -> WasiCtxView<'_> { - WasiCtxView { - ctx: &mut self.wasi, - table: &mut self.table, - } - } -} - -fn unimplemented(domain: &str, detail: impl Into) -> HostError { - HostError { - domain: domain.into(), - kind: HostErrorKind::Unsupported, - code: 501, - message: detail.into(), - data: None, - } -} - -// -- Stub implementations for host interfaces -- - -impl nexum::host::types::Host for HostState {} - -impl shepherd::cow::cow_api::Host for HostState { - async fn request( - &mut self, - _chain_id: u64, - method: String, - path: String, - _body: Option, - ) -> Result { - let start = Instant::now(); - eprintln!("[cow-api] {method} {path}"); - let result = Err(unimplemented( - "cow-api", - format!("not implemented: {method} {path}"), - )); - eprintln!("[timing] cow-api::request: {:?}", start.elapsed()); - result - } - - async fn submit_order( - &mut self, - _chain_id: u64, - _order_data: Vec, - ) -> Result { - let start = Instant::now(); - eprintln!("[cow-api] submit-order"); - let result = Err(unimplemented("cow-api", "submit-order not implemented")); - eprintln!("[timing] cow-api::submit-order: {:?}", start.elapsed()); - result - } -} - -impl nexum::host::chain::Host for HostState { - async fn request( - &mut self, - _chain_id: u64, - method: String, - _params: String, - ) -> Result { - let start = Instant::now(); - eprintln!("[chain] request: {method}"); - let result = Err(HostError { - domain: "chain".into(), - kind: HostErrorKind::Unsupported, - code: -32601, - message: format!("method not implemented: {method}"), - data: None, - }); - eprintln!("[timing] chain::request: {:?}", start.elapsed()); - result - } - - async fn request_batch( - &mut self, - chain_id: u64, - requests: Vec, - ) -> Result, HostError> { - let start = Instant::now(); - eprintln!("[chain] request-batch: {} calls", requests.len()); - let mut out = Vec::with_capacity(requests.len()); - for req in requests { - match self.request(chain_id, req.method, req.params).await { - Ok(s) => out.push(nexum::host::chain::RpcResult::Ok(s)), - Err(e) => out.push(nexum::host::chain::RpcResult::Err(e)), - } - } - eprintln!("[timing] chain::request-batch: {:?}", start.elapsed()); - Ok(out) - } -} - -impl nexum::host::identity::Host for HostState { - async fn accounts(&mut self) -> Result>, HostError> { - let start = Instant::now(); - eprintln!("[identity] accounts"); - let result = Ok(vec![]); - eprintln!("[timing] identity::accounts: {:?}", start.elapsed()); - result - } - - async fn sign(&mut self, _account: Vec, _message: Vec) -> Result, HostError> { - let start = Instant::now(); - eprintln!("[identity] sign"); - let result = Err(unimplemented("identity", "sign not implemented")); - eprintln!("[timing] identity::sign: {:?}", start.elapsed()); - result - } - - async fn sign_typed_data( - &mut self, - _account: Vec, - _typed_data: String, - ) -> Result, HostError> { - let start = Instant::now(); - eprintln!("[identity] sign-typed-data"); - let result = Err(unimplemented("identity", "sign-typed-data not implemented")); - eprintln!("[timing] identity::sign-typed-data: {:?}", start.elapsed()); - result - } -} - -impl nexum::host::local_store::Host for HostState { - async fn get(&mut self, key: String) -> Result>, HostError> { - let start = Instant::now(); - eprintln!("[local-store] get: {key}"); - let result = Ok(None); - eprintln!("[timing] local-store::get: {:?}", start.elapsed()); - result - } - - async fn set(&mut self, key: String, _value: Vec) -> Result<(), HostError> { - let start = Instant::now(); - eprintln!("[local-store] set: {key}"); - let result = Ok(()); - eprintln!("[timing] local-store::set: {:?}", start.elapsed()); - result - } - - async fn delete(&mut self, key: String) -> Result<(), HostError> { - let start = Instant::now(); - eprintln!("[local-store] delete: {key}"); - let result = Ok(()); - eprintln!("[timing] local-store::delete: {:?}", start.elapsed()); - result - } - - async fn list_keys(&mut self, prefix: String) -> Result, HostError> { - let start = Instant::now(); - eprintln!("[local-store] list-keys: {prefix}"); - let result = Ok(vec![]); - eprintln!("[timing] local-store::list-keys: {:?}", start.elapsed()); - result - } -} - -impl nexum::host::remote_store::Host for HostState { - async fn upload(&mut self, _data: Vec) -> Result, HostError> { - let start = Instant::now(); - let result = Err(unimplemented("remote-store", "upload not implemented")); - eprintln!("[timing] remote-store::upload: {:?}", start.elapsed()); - result - } - - async fn download(&mut self, _reference: Vec) -> Result, HostError> { - let start = Instant::now(); - let result = Err(unimplemented("remote-store", "download not implemented")); - eprintln!("[timing] remote-store::download: {:?}", start.elapsed()); - result - } - - async fn read_feed( - &mut self, - _owner: Vec, - _topic: Vec, - ) -> Result>, HostError> { - let start = Instant::now(); - let result = Err(unimplemented("remote-store", "read-feed not implemented")); - eprintln!("[timing] remote-store::read-feed: {:?}", start.elapsed()); - result - } - - async fn write_feed(&mut self, _topic: Vec, _data: Vec) -> Result, HostError> { - let start = Instant::now(); - let result = Err(unimplemented("remote-store", "write-feed not implemented")); - eprintln!("[timing] remote-store::write-feed: {:?}", start.elapsed()); - result - } -} - -impl nexum::host::messaging::Host for HostState { - async fn publish(&mut self, content_topic: String, _payload: Vec) -> Result<(), HostError> { - let start = Instant::now(); - eprintln!("[messaging] publish: {content_topic}"); - let result = Err(unimplemented("messaging", "publish not implemented")); - eprintln!("[timing] messaging::publish: {:?}", start.elapsed()); - result - } - - async fn query( - &mut self, - content_topic: String, - _start_time: Option, - _end_time: Option, - _limit: Option, - ) -> Result, HostError> { - let start = Instant::now(); - eprintln!("[messaging] query: {content_topic}"); - let result = Ok(vec![]); - eprintln!("[timing] messaging::query: {:?}", start.elapsed()); - result - } -} - -impl nexum::host::logging::Host for HostState { - async fn log(&mut self, level: nexum::host::logging::Level, message: String) { - let start = Instant::now(); - let level_str = match level { - nexum::host::logging::Level::Trace => "TRACE", - nexum::host::logging::Level::Debug => "DEBUG", - nexum::host::logging::Level::Info => "INFO", - nexum::host::logging::Level::Warn => "WARN", - nexum::host::logging::Level::Error => "ERROR", - }; - eprintln!("[{level_str}] {message}"); - eprintln!("[timing] logging::log: {:?}", start.elapsed()); - } -} - -// -- Additive 0.2 capabilities -- - -impl nexum::host::clock::Host for HostState { - async fn now_ms(&mut self) -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|d| d.as_millis() as u64) - .unwrap_or(0) - } - - async fn monotonic_ns(&mut self) -> u64 { - self.monotonic_baseline.elapsed().as_nanos() as u64 - } -} - -impl nexum::host::random::Host for HostState { - async fn fill(&mut self, len: u32) -> Vec { - let mut buf = vec![0u8; len as usize]; - // getrandom 0.4: fill() returns Result<(), Error>. CSPRNG failures - // are exceptionally rare on supported platforms; on failure we - // return zero-filled bytes — guests that need a strong-failure - // signal should use identity or chain primitives instead. - let _ = getrandom::fill(&mut buf); - buf - } -} - -impl nexum::host::http::Host for HostState { - async fn fetch( - &mut self, - req: nexum::host::http::Request, - ) -> Result { - let start = Instant::now(); - eprintln!("[http] {} {}", req.method, req.url); - - // Manifest allowlist enforcement runs before any I/O. Hosts that - // never link a manifest leave `http_allowlist` empty, which denies - // every request — matching the "no implicit network" stance. - let host = match manifest::extract_host(&req.url) { - Some(h) => h, - None => { - eprintln!("[timing] http::fetch: {:?}", start.elapsed()); - return Err(HostError { - domain: "http".into(), - kind: HostErrorKind::InvalidInput, - code: 0, - message: format!("not an http(s) URL: {}", req.url), - data: None, - }); - } - }; - if !manifest::host_allowed(host, &self.http_allowlist) { - eprintln!("[http] denied by allowlist: {host}"); - eprintln!("[timing] http::fetch: {:?}", start.elapsed()); - return Err(HostError { - domain: "http".into(), - kind: HostErrorKind::Denied, - code: 0, - message: format!( - "host {host} not in [capabilities.http].allow; \ - add it to nexum.toml to permit" - ), - data: None, - }); - } - - // 0.2: allowlist passed, but the reference runtime does not perform - // real HTTP yet. Real fetch lands in 0.3. - let result = Err(unimplemented( - "http", - "fetch not implemented in 0.2 reference runtime (allowlist passed)", - )); - eprintln!("[timing] http::fetch: {:?}", start.elapsed()); - result - } -} +use crate::bindings::Shepherd; +use crate::cli::Cli; +use crate::host::state::HostState; #[tokio::main] async fn main() -> anyhow::Result<()> { - let mut args = std::env::args().skip(1); - let wasm_path = args.next().ok_or_else(|| { - anyhow::anyhow!("usage: nexum-engine []") + let cli = Cli::parse(); + + let engine_cfg = engine_config::load_or_default(cli.engine_config.as_deref())?; + + let env_filter = EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new(&engine_cfg.engine.log_level)) + .unwrap_or_else(|_| EnvFilter::new("info")); + tracing_subscriber::fmt() + .with_env_filter(env_filter) + .with_target(true) + .init(); + + info!("nexum-engine starting"); + + // Bring up shared host backends. + std::fs::create_dir_all(&engine_cfg.engine.state_dir).map_err(|e| { + anyhow::anyhow!( + "create state directory {}: {e}", + engine_cfg.engine.state_dir.display() + ) })?; - let explicit_manifest = args.next().map(PathBuf::from); - - println!("nexum-engine: loading component from {wasm_path}"); - - // Load the manifest from the explicit path if given, otherwise from - // `nexum.toml` next to the component file. Missing → fallback (with - // deprecation warning). - let manifest_path = explicit_manifest.or_else(|| { - PathBuf::from(&wasm_path) - .parent() - .map(|p| p.join("nexum.toml")) - }); - let loaded = match manifest_path.as_deref() { - Some(p) if p.exists() => { - println!("nexum-engine: loading manifest from {}", p.display()); - manifest::load(p)? - } - _ => manifest::fallback_manifest(), - }; + let store_path = engine_cfg.engine.state_dir.join("local-store.redb"); + let local_store = host::local_store_redb::LocalStore::open(&store_path) + .map_err(|e| anyhow::anyhow!("open local-store at {}: {e}", store_path.display()))?; + let cow_pool = host::cow_orderbook::OrderBookPool::default(); + let provider_pool = host::provider_pool::ProviderPool::from_config(&engine_cfg).await?; + // wasmtime engine + linker - one of each, shared across modules. let mut config = wasmtime::Config::new(); config.wasm_component_model(true); + config.consume_fuel(true); let engine = Engine::new(&config)?; - let start = Instant::now(); - let component = - Component::from_file(&engine, &wasm_path).context("failed to load component")?; - eprintln!("[timing] component load: {:?}", start.elapsed()); - let mut linker = Linker::::new(&engine); Shepherd::add_to_linker::>( &mut linker, @@ -379,62 +67,66 @@ async fn main() -> anyhow::Result<()> { )?; wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; - let wasi = WasiCtxBuilder::new().inherit_stdio().build(); + // Boot supervisor - `engine.toml.[[modules]]` first, CLI positional second. + let mut supervisor = if let Some(wasm) = cli.wasm.as_deref() { + if !engine_cfg.modules.is_empty() { + warn!("ignoring engine.toml [[modules]] because a positional was given"); + } + supervisor::Supervisor::boot_single( + &engine, + &linker, + wasm, + cli.manifest.as_deref(), + &cow_pool, + &provider_pool, + &local_store, + ) + .await? + } else if !engine_cfg.modules.is_empty() { + supervisor::Supervisor::boot( + &engine, + &linker, + &engine_cfg, + &cow_pool, + &provider_pool, + &local_store, + ) + .await? + } else { + anyhow::bail!( + "no modules to run - either pass a positional or declare \ + [[modules]] entries in engine.toml" + ); + }; - let mut store = Store::new( - &engine, - HostState { - wasi, - table: ResourceTable::new(), - monotonic_baseline: Instant::now(), - http_allowlist: loaded.http_allowlist, - }, + info!( + modules = supervisor.module_count(), + chains = supervisor.block_chains().len(), + "supervisor ready" ); - let start = Instant::now(); - let bindings = Shepherd::instantiate_async(&mut store, &component, &linker) - .await - .context("failed to instantiate component")?; - eprintln!("[timing] component instantiate: {:?}", start.elapsed()); + // Open per-chain block subscriptions + per-module log + // subscriptions, merge, dispatch until shutdown. + let block_chains = supervisor.block_chains(); + let log_subs = supervisor.log_subscriptions(); - println!("nexum-engine: calling init..."); - // 0.2: [config] is stringly-typed (typed variant deferred to 0.3). - // Fall back to a single ("name", "") pair if the manifest has - // no [config] section so the example module still has something to log. - let config_entries: Config = if loaded.config.is_empty() { - vec![("name".into(), loaded.manifest.module.name.clone())] - } else { - loaded.config - }; - let start = Instant::now(); - match bindings.call_init(&mut store, &config_entries).await? { - Ok(()) => println!("nexum-engine: init succeeded"), - Err(e) => println!( - "nexum-engine: init failed: {}::{:?} {} ({})", - e.domain, e.kind, e.message, e.code - ), + if block_chains.is_empty() && log_subs.is_empty() { + info!("no [[subscription]] entries - engine has nothing to run; exiting"); + return Ok(()); } - eprintln!("[timing] call_init: {:?}", start.elapsed()); - // Dispatch a test block event (timestamps are ms since Unix epoch, UTC). - println!("nexum-engine: dispatching test block event..."); - let block = nexum::host::types::Block { - chain_id: 1, - number: 19_000_000, - hash: vec![0xab; 32], - timestamp: 1_700_000_000_000, + let block_streams = + runtime::event_loop::open_block_streams(&provider_pool, &block_chains).await; + let log_streams = runtime::event_loop::open_log_streams(&provider_pool, log_subs).await; + + let shutdown = async { + match runtime::event_loop::wait_for_shutdown_signal().await { + Ok(name) => info!(signal = %name, "shutdown signal received"), + Err(err) => warn!(error = %err, "signal handler failed - using ctrl-c"), + } }; - let event = nexum::host::types::Event::Block(block); - let start = Instant::now(); - match bindings.call_on_event(&mut store, &event).await? { - Ok(()) => println!("nexum-engine: on-event succeeded"), - Err(e) => println!( - "nexum-engine: on-event failed: {}::{:?} {} ({})", - e.domain, e.kind, e.message, e.code - ), - } - eprintln!("[timing] call_on_event: {:?}", start.elapsed()); - println!("nexum-engine: done"); + runtime::event_loop::run(&mut supervisor, block_streams, log_streams, shutdown).await; + info!("done"); Ok(()) } diff --git a/crates/nexum-engine/src/manifest/capabilities.rs b/crates/nexum-engine/src/manifest/capabilities.rs new file mode 100644 index 0000000..fddb788 --- /dev/null +++ b/crates/nexum-engine/src/manifest/capabilities.rs @@ -0,0 +1,162 @@ +//! Capability enforcement: cross-checks the component's WIT imports +//! against the `[capabilities]` block declared in `module.toml`. + +use std::collections::HashSet; + +use super::error::CapabilityViolation; +use super::types::{KNOWN_CAPABILITIES, LoadedManifest}; + +/// Check that every capability-bearing WIT import of the component is covered +/// by the module's manifest declarations. Call this after loading the +/// component but before instantiation. +/// +/// When `[capabilities]` is absent the manifest is in 0.1-fallback mode and +/// all imports are allowed; the caller is expected to have already emitted +/// a deprecation warning. +/// +/// `component_imports` should be the iterator returned by +/// `component.component_type().imports(&engine)` - pass the **name** part +/// (`&str`) of each `(&str, ComponentItem)` tuple. +pub fn enforce_capabilities<'a>( + loaded: &LoadedManifest, + component_imports: impl Iterator, +) -> Result<(), CapabilityViolation> { + let caps = match loaded.manifest.capabilities.as_ref() { + None => return Ok(()), // 0.1-fallback: no enforcement + Some(c) => c, + }; + + let declared: HashSet<&str> = caps + .required + .iter() + .chain(caps.optional.iter()) + .map(String::as_str) + .collect(); + + for import_name in component_imports { + if let Some(cap) = wit_import_to_cap(import_name) + && !declared.contains(cap) + { + return Err(CapabilityViolation { + capability: cap.to_owned(), + wit_import: import_name.to_owned(), + }); + } + } + Ok(()) +} + +/// Map a WIT import name to a capability name, or `None` for non-capability +/// imports. +/// +/// Returns `Some(iface)` only for interfaces in [`KNOWN_CAPABILITIES`]; +/// type-only packages like `nexum:host/types` and unrelated namespaces +/// (`wasi:*`) fall through to `None` so they do not need a manifest +/// declaration. +/// +/// Examples: +/// - `"nexum:host/chain@0.2.0"` -> `Some("chain")` +/// - `"shepherd:cow/cow-api@0.2.0"` -> `Some("cow-api")` +/// - `"nexum:host/types@0.2.0"` -> `None` (type-only, not a capability) +/// - `"wasi:io/streams@0.2.0"` -> `None` +pub(super) fn wit_import_to_cap(import_name: &str) -> Option<&str> { + let without_version = import_name.split('@').next().unwrap_or(import_name); + let iface = without_version + .strip_prefix("nexum:host/") + .or_else(|| without_version.strip_prefix("shepherd:cow/"))?; + if KNOWN_CAPABILITIES.contains(&iface) { + Some(iface) + } else { + None + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::manifest::types::{CapabilitiesSection, Manifest}; + + #[test] + fn wit_import_to_cap_nexum_host() { + assert_eq!(wit_import_to_cap("nexum:host/chain@0.2.0"), Some("chain")); + assert_eq!( + wit_import_to_cap("nexum:host/local-store@0.2.0"), + Some("local-store") + ); + assert_eq!(wit_import_to_cap("nexum:host/http@0.2.0"), Some("http")); + } + + #[test] + fn wit_import_to_cap_shepherd_cow() { + assert_eq!( + wit_import_to_cap("shepherd:cow/cow-api@0.2.0"), + Some("cow-api") + ); + } + + #[test] + fn wit_import_to_cap_wasi_is_none() { + assert_eq!(wit_import_to_cap("wasi:io/streams@0.2.0"), None); + assert_eq!(wit_import_to_cap("wasi:cli/stdin@0.2.0"), None); + assert_eq!(wit_import_to_cap("wasi:sockets/tcp@0.2.0"), None); + } + + fn manifest_with_caps(required: &[&str], optional: &[&str]) -> LoadedManifest { + LoadedManifest { + manifest: Manifest { + capabilities: Some(CapabilitiesSection { + required: required.iter().map(|s| s.to_string()).collect(), + optional: optional.iter().map(|s| s.to_string()).collect(), + http: None, + }), + ..Default::default() + }, + http_allowlist: vec![], + config: vec![], + } + } + + fn manifest_no_caps() -> LoadedManifest { + LoadedManifest { + manifest: Manifest::default(), + http_allowlist: vec![], + config: vec![], + } + } + + #[test] + fn enforce_passes_when_caps_absent() { + // 0.1-fallback: no capabilities section -> all imports allowed + let loaded = manifest_no_caps(); + let imports = ["nexum:host/chain@0.2.0", "nexum:host/remote-store@0.2.0"]; + assert!(enforce_capabilities(&loaded, imports.into_iter()).is_ok()); + } + + #[test] + fn enforce_passes_when_all_imports_declared() { + let loaded = manifest_with_caps(&["chain", "cow-api"], &["http"]); + let imports = [ + "nexum:host/chain@0.2.0", + "shepherd:cow/cow-api@0.2.0", + "nexum:host/http@0.2.0", + "wasi:io/streams@0.2.0", // wasi is always skipped + ]; + assert!(enforce_capabilities(&loaded, imports.into_iter()).is_ok()); + } + + #[test] + fn enforce_rejects_undeclared_import() { + let loaded = manifest_with_caps(&["chain"], &[]); + // module imports remote-store but didn't declare it + let imports = ["nexum:host/chain@0.2.0", "nexum:host/remote-store@0.2.0"]; + let err = enforce_capabilities(&loaded, imports.into_iter()).unwrap_err(); + assert_eq!(err.capability, "remote-store"); + } + + #[test] + fn enforce_optional_caps_are_also_allowed() { + let loaded = manifest_with_caps(&["chain"], &["remote-store"]); + let imports = ["nexum:host/chain@0.2.0", "nexum:host/remote-store@0.2.0"]; + assert!(enforce_capabilities(&loaded, imports.into_iter()).is_ok()); + } +} diff --git a/crates/nexum-engine/src/manifest/error.rs b/crates/nexum-engine/src/manifest/error.rs new file mode 100644 index 0000000..98bf7d7 --- /dev/null +++ b/crates/nexum-engine/src/manifest/error.rs @@ -0,0 +1,51 @@ +//! Error types for manifest parsing and capability enforcement. + +use super::types::KNOWN_CAPABILITIES; + +/// Errors returned while loading or validating a manifest. +#[derive(Debug)] +pub enum ParseError { + Io(std::io::Error), + Toml(toml::de::Error), + UnknownCapability(String), +} + +impl std::fmt::Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "manifest: i/o: {e}"), + Self::Toml(e) => write!(f, "manifest: parse: {e}"), + Self::UnknownCapability(name) => write!( + f, + "manifest: unknown capability {:?} in [capabilities].required (known: {})", + name, + KNOWN_CAPABILITIES.join(", ") + ), + } + } +} + +impl std::error::Error for ParseError {} + +/// Error returned when a component's WIT imports exceed its declared capabilities. +#[derive(Debug)] +pub struct CapabilityViolation { + /// Capability name (e.g. `"remote-store"`). + pub capability: String, + /// Full WIT import name as it appeared in the component (e.g. + /// `"nexum:host/remote-store@0.2.0"`). + pub wit_import: String, +} + +impl std::fmt::Display for CapabilityViolation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "component imports `{}` ({}) but it is not listed in \ + [capabilities].required or [capabilities].optional", + self.capability, self.wit_import + ) + } +} + +impl std::error::Error for CapabilityViolation {} diff --git a/crates/nexum-engine/src/manifest.rs b/crates/nexum-engine/src/manifest/load.rs similarity index 54% rename from crates/nexum-engine/src/manifest.rs rename to crates/nexum-engine/src/manifest/load.rs index 522e168..b857a76 100644 --- a/crates/nexum-engine/src/manifest.rs +++ b/crates/nexum-engine/src/manifest/load.rs @@ -1,120 +1,17 @@ -//! Minimal `nexum.toml` parser and capability-enforcement helpers (0.2 scope). +//! Parse `module.toml` from disk, validate, and emit operator-visible +//! warnings. //! -//! 0.2 intentionally ships a slim subset of the manifest spec described in -//! the migration guide §3: -//! -//! - `[capabilities].required` is parsed and validated (names must be in -//! the known capability set; the 0.2 reference engine always provides -//! all of them, so this is a sanity check + future-proofing). -//! - `[capabilities].optional` is parsed and logged; trap-stub fallback -//! for absent optionals is deferred to 0.3. -//! - `[capabilities.http].allow` is parsed and consulted by the `http` -//! host impl before any outbound call. -//! - `[config]` is flattened to `Vec<(String, String)>` and passed to the -//! module's `init`. Typed `config-value` variant is deferred to 0.3. -//! -//! When the manifest file is missing or has no `[capabilities]` section, -//! a deprecation warning is emitted on stderr and the engine falls back -//! to 0.1 behaviour (treat every linked capability as required). This -//! fallback will be removed in 0.3. +//! Also exposes the small URL/host helpers the `http` host backend +//! uses to enforce the manifest's `[capabilities.http].allow` list at +//! request time. use std::collections::HashSet; use std::path::Path; -use serde::Deserialize; - -/// Capability names recognised by the 0.2 reference engine. Matches the -/// interfaces the `shepherd` world links into the linker. -pub const KNOWN_CAPABILITIES: &[&str] = &[ - "chain", - "identity", - "local-store", - "remote-store", - "messaging", - "logging", - "clock", - "random", - "http", - // Domain-extension caps (provided by the shepherd world only): - "cow-api", -]; - -#[derive(Debug, Deserialize, Default)] -pub struct Manifest { - #[serde(default)] - pub module: ModuleSection, - #[serde(default)] - pub capabilities: Option, - #[serde(default)] - pub config: toml::Table, -} - -#[derive(Debug, Deserialize, Default)] -#[allow(dead_code)] // version + component parsed for future 0.3 hash-verification. -pub struct ModuleSection { - #[serde(default)] - pub name: String, - #[serde(default)] - pub version: String, - #[serde(default)] - pub component: String, -} - -#[derive(Debug, Deserialize, Default)] -pub struct CapabilitiesSection { - #[serde(default)] - pub required: Vec, - #[serde(default)] - pub optional: Vec, - #[serde(default)] - pub http: Option, -} - -#[derive(Debug, Deserialize, Default)] -pub struct HttpSection { - #[serde(default)] - pub allow: Vec, -} - -/// Errors returned while loading or validating a manifest. -#[derive(Debug)] -pub enum ParseError { - Io(std::io::Error), - Toml(toml::de::Error), - UnknownCapability(String), -} - -impl std::fmt::Display for ParseError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Io(e) => write!(f, "manifest: i/o: {e}"), - Self::Toml(e) => write!(f, "manifest: parse: {e}"), - Self::UnknownCapability(name) => write!( - f, - "manifest: unknown capability {:?} in [capabilities].required (known: {})", - name, - KNOWN_CAPABILITIES.join(", ") - ), - } - } -} - -impl std::error::Error for ParseError {} - -/// Loaded + validated manifest, plus its source path for diagnostics. -pub struct LoadedManifest { - pub manifest: Manifest, - /// Hosts to allow for `http::fetch`. Each entry is either an exact - /// hostname or a `*.suffix` wildcard. - pub http_allowlist: Vec, - /// `[config]` flattened to `(key, stringified-value)` pairs ready to - /// hand to a module's `init`. TOML scalars (string, integer, float, - /// boolean) become their text form. Arrays and tables are rendered as - /// their TOML representation. - pub config: Vec<(String, String)>, -} +use super::error::ParseError; +use super::types::{KNOWN_CAPABILITIES, LoadedManifest, Manifest}; -/// Read `nexum.toml` from `path`, parse, validate, and emit a deprecation +/// Read `module.toml` from `path`, parse, validate, and emit a deprecation /// warning if `[capabilities]` is absent (0.1-compat fallback). pub fn load(path: &Path) -> Result { let raw = std::fs::read_to_string(path).map_err(ParseError::Io)?; @@ -123,10 +20,9 @@ pub fn load(path: &Path) -> Result { let caps = manifest.capabilities.as_ref(); if caps.is_none() { eprintln!( - "[deprecation] no [capabilities] section in nexum.toml — \ + "[deprecation] no [capabilities] section in module.toml - \ defaulting to all-required (0.1 behaviour). This default \ - will be removed in 0.3; add an explicit [capabilities] block \ - now." + will be removed in 0.3; add an explicit [capabilities] block." ); } @@ -173,13 +69,13 @@ pub fn load(path: &Path) -> Result { }) } -/// Synthesise a "0.1 fallback" manifest for when no `nexum.toml` is found. +/// Synthesise a "0.1 fallback" manifest for when no `module.toml` is found. /// Emits the same deprecation warning as a missing-section manifest. pub fn fallback_manifest() -> LoadedManifest { eprintln!( - "[deprecation] no nexum.toml found — defaulting to all-required \ + "[deprecation] no module.toml found - defaulting to all-required \ (0.1 behaviour). This default will be removed in 0.3; ship a \ - nexum.toml alongside your component." + module.toml alongside your component." ); LoadedManifest { manifest: Manifest::default(), @@ -204,7 +100,7 @@ pub fn host_allowed(host: &str, allowlist: &[String]) -> bool { } /// Extract the host component from a URL. Returns `None` for non-http(s) -/// schemes or malformed input. Intentionally simple — adds no `url` +/// schemes or malformed input. Intentionally simple - adds no `url` /// crate dependency. pub fn extract_host(url: &str) -> Option<&str> { let after_scheme = url @@ -235,6 +131,98 @@ fn stringify_toml_value(v: &toml::Value) -> String { #[cfg(test)] mod tests { use super::*; + use crate::manifest::types::Subscription; + + #[test] + fn load_parses_block_and_log_subscriptions() { + let toml = r#" +[module] +name = "twap-monitor" + +[capabilities] +required = ["chain", "local-store"] + +[[subscription]] +kind = "block" +chain_id = 1 + +[[subscription]] +kind = "log" +chain_id = 1 +address = "0xC92E8bdf79f0507f65a392b0ab4667716BFE0110" +event_signature = "0x00000000000000000000000000000000000000000000000000000000deadbeef" +"#; + let manifest: Manifest = toml::from_str(toml).expect("parse"); + assert_eq!(manifest.module.name, "twap-monitor"); + assert_eq!(manifest.subscriptions.len(), 2); + assert!(matches!( + &manifest.subscriptions[0], + Subscription::Block { chain_id: 1 } + )); + if let Subscription::Log { + chain_id, address, .. + } = &manifest.subscriptions[1] + { + assert_eq!(*chain_id, 1); + assert!(address.is_some()); + } else { + panic!("expected Log subscription"); + } + } + + #[test] + fn load_parses_cron_subscription() { + let toml = r#" +[module] +name = "scheduler" + +[[subscription]] +kind = "cron" +schedule = "*/5 * * * *" +"#; + let manifest: Manifest = toml::from_str(toml).expect("parse"); + assert!(matches!( + &manifest.subscriptions[0], + Subscription::Cron { .. } + )); + } + + #[test] + fn load_rejects_unknown_capability() { + let toml = r#" +[module] +name = "bad" + +[capabilities] +required = ["chain", "not-a-real-cap"] +"#; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("module.toml"); + std::fs::write(&path, toml).unwrap(); + let err = load(&path).unwrap_err(); + assert!(matches!(err, ParseError::UnknownCapability(ref name) if name == "not-a-real-cap")); + } + + #[test] + fn load_parses_config_table() { + let toml = r#" +[module] +name = "example" + +[config] +chain_id = 1 +label = "mainnet" +enabled = true +"#; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("module.toml"); + std::fs::write(&path, toml).unwrap(); + let loaded = load(&path).unwrap(); + let config: std::collections::HashMap<_, _> = loaded.config.into_iter().collect(); + assert_eq!(config.get("chain_id").map(String::as_str), Some("1")); + assert_eq!(config.get("label").map(String::as_str), Some("mainnet")); + assert_eq!(config.get("enabled").map(String::as_str), Some("true")); + } #[test] fn extract_host_handles_common_shapes() { diff --git a/crates/nexum-engine/src/manifest/mod.rs b/crates/nexum-engine/src/manifest/mod.rs new file mode 100644 index 0000000..f58d25b --- /dev/null +++ b/crates/nexum-engine/src/manifest/mod.rs @@ -0,0 +1,40 @@ +//! `module.toml` parser and capability-enforcement helpers (0.2 scope). +//! +//! 0.2 intentionally ships a slim subset of the manifest spec: +//! +//! - `[capabilities].required` is parsed and validated (names must be in +//! the known capability set; the 0.2 reference engine always provides +//! all of them, so this is a sanity check + future-proofing). +//! - `[capabilities].optional` is parsed and logged; trap-stub fallback +//! for absent optionals is deferred to 0.3. +//! - `[capabilities.http].allow` is parsed and consulted by the `http` +//! host impl before any outbound call. +//! - `[config]` is flattened to `Vec<(String, String)>` and passed to the +//! module's `init`. Typed `config-value` variant is deferred to 0.3. +//! +//! When the manifest file is missing or has no `[capabilities]` section, +//! a deprecation warning is emitted and the engine falls back to 0.1 +//! behaviour (treat every linked capability as required). This fallback +//! will be removed in 0.3. +//! +//! ## Layout +//! +//! - [`types`]: the serde `Manifest` shape + `LoadedManifest` the engine +//! actually consumes, plus the `KNOWN_CAPABILITIES` registry. +//! - [`mod@load`]: `module.toml` -> `LoadedManifest`, plus the host/URL +//! helpers the `http` backend uses at request time. +//! - [`capabilities`]: WIT-import vs declared-capabilities cross-check. +//! - [`error`]: `ParseError`, `CapabilityViolation`. + +mod capabilities; +mod error; +mod load; +mod types; + +pub use capabilities::enforce_capabilities; +pub use load::{extract_host, fallback_manifest, host_allowed, load}; +pub use types::{LoadedManifest, Subscription}; +// CapabilityViolation, ParseError, and the *Section structs are +// reachable through these functions' return / argument types; +// consumers that need to name them directly do so via +// `crate::manifest::error::*` or `::types::*`. diff --git a/crates/nexum-engine/src/manifest/types.rs b/crates/nexum-engine/src/manifest/types.rs new file mode 100644 index 0000000..e91bff1 --- /dev/null +++ b/crates/nexum-engine/src/manifest/types.rs @@ -0,0 +1,123 @@ +//! Data structures: `Manifest`, sections, and `LoadedManifest`. +//! +//! Plain serde shapes plus the `KNOWN_CAPABILITIES` registry. The parsing +//! and validation logic lives in [`mod@super::load`]; capability enforcement +//! in [`super::capabilities`]. + +use serde::Deserialize; + +/// Capability names recognised by the 0.2 reference engine. Matches the +/// interfaces the `shepherd` world links into the linker. +pub const KNOWN_CAPABILITIES: &[&str] = &[ + "chain", + "identity", + "local-store", + "remote-store", + "messaging", + "logging", + "clock", + "random", + "http", + // Domain-extension caps (provided by the shepherd world only): + "cow-api", +]; + +#[derive(Debug, Deserialize, Default)] +pub struct Manifest { + #[serde(default)] + pub module: ModuleSection, + #[serde(default)] + pub capabilities: Option, + #[serde(default)] + pub config: toml::Table, + /// Event subscriptions the runtime wires before calling + /// `_init`. See `docs/02-modules-events-packaging.md` for the + /// schema; 0.2 implements `block` and `log` kinds, `cron` is + /// parsed and ignored (deferred to 0.3). + #[serde(default, rename = "subscription")] + pub subscriptions: Vec, +} + +/// One `[[subscription]]` table in `module.toml`. +/// +/// The discriminator is the `kind` field; remaining fields are +/// validated per-kind by the supervisor. Unknown kinds are surfaced +/// at load time so a typo does not silently disable an event source. +#[derive(Debug, Deserialize, Clone)] +#[serde(tag = "kind", rename_all = "lowercase")] +pub enum Subscription { + /// New-block events. Fan-out is shared per chain - the + /// supervisor opens one subscription per chain id and routes to + /// every module that asked for blocks on that chain. + Block { + /// EVM chain id. + chain_id: u64, + }, + /// Log events matching `address` + topic-0. Fan-out is + /// per-module - the supervisor opens one subscription per + /// `[[subscription]]` entry and tags emitted events with the + /// owning module. + Log { + /// EVM chain id. + chain_id: u64, + /// Contract address as `0x`-prefixed 20-byte hex. Optional. + #[serde(default)] + address: Option, + /// Topic-0 of the event the module wants to consume. `0x`- + /// prefixed 32-byte hex. Optional - when absent the + /// subscription matches every event from the address(es). + #[serde(default)] + event_signature: Option, + }, + /// Cron-scheduled tick. 0.2 parses but does not dispatch; the + /// supervisor emits a warning so the operator knows the + /// declaration is currently inert. `schedule` is preserved so a + /// 0.3 dispatcher can pick it up without re-parsing the manifest. + Cron { + /// Standard 5-field cron expression. + #[allow(dead_code)] + schedule: String, + }, +} + +#[derive(Debug, Deserialize, Default)] +#[allow(dead_code)] // version + component parsed for future 0.3 hash-verification. +pub struct ModuleSection { + #[serde(default)] + pub name: String, + #[serde(default)] + pub version: String, + #[serde(default)] + pub component: String, +} + +#[derive(Debug, Deserialize, Default)] +pub struct CapabilitiesSection { + #[serde(default)] + pub required: Vec, + #[serde(default)] + pub optional: Vec, + #[serde(default)] + pub http: Option, +} + +#[derive(Debug, Deserialize, Default)] +pub struct HttpSection { + #[serde(default)] + pub allow: Vec, +} + +/// Loaded + validated manifest, plus the data the engine needs to +/// instantiate a module. +#[derive(Debug)] +pub struct LoadedManifest { + pub manifest: Manifest, + /// Hosts to allow for `http::fetch`. Each entry is either an exact + /// hostname or a `*.suffix` wildcard. + pub http_allowlist: Vec, + /// `[config]` flattened to `(key, stringified-value)` pairs ready to + /// hand to a module's `init`. TOML scalars (string, integer, float, + /// boolean) become their text form. Arrays and tables are rendered as + /// their TOML representation. + pub config: Vec<(String, String)>, +} diff --git a/crates/nexum-engine/src/runtime/event_loop.rs b/crates/nexum-engine/src/runtime/event_loop.rs new file mode 100644 index 0000000..ff399fa --- /dev/null +++ b/crates/nexum-engine/src/runtime/event_loop.rs @@ -0,0 +1,173 @@ +//! Open live `eth_subscribe` streams and dispatch their events to the +//! supervisor until a shutdown signal arrives. + +use futures::StreamExt; +use futures::stream::{BoxStream, FuturesUnordered, select_all}; +use tracing::{info, warn}; + +use crate::bindings::nexum; +use crate::host::provider_pool::ProviderPool; +use crate::supervisor::Supervisor; + +/// Per-chain block subscriptions, one shared stream per chain id. +pub async fn open_block_streams( + pool: &ProviderPool, + chains: &std::collections::BTreeSet, +) -> Vec { + let mut openings: FuturesUnordered<_> = chains + .iter() + .copied() + .map(|chain_id| async move { (chain_id, pool.subscribe_blocks(chain_id).await) }) + .collect(); + + let mut streams = Vec::new(); + while let Some((chain_id, result)) = openings.next().await { + match result { + Ok(stream) => { + info!(chain_id, "block subscription open"); + let tagged: TaggedBlockStream = Box::pin(stream.map(move |item| { + item.map(|header| (chain_id, header)) + .map_err(anyhow::Error::from) + })); + streams.push(tagged); + } + Err(err) => { + warn!(chain_id, error = %err, "block subscription failed"); + } + } + } + streams +} + +/// Per-module log subscriptions. Each entry is a stream tagged with +/// the owning module name + chain id. +pub async fn open_log_streams( + pool: &ProviderPool, + subs: Vec<(String, u64, alloy_rpc_types_eth::Filter)>, +) -> Vec { + let mut openings: FuturesUnordered<_> = subs + .into_iter() + .map(|(module, chain_id, filter)| async move { + let stream = pool.subscribe_logs(chain_id, filter).await; + (module, chain_id, stream) + }) + .collect(); + + let mut streams = Vec::new(); + while let Some((module, chain_id, result)) = openings.next().await { + match result { + Ok(stream) => { + info!(module = %module, chain_id, "log subscription open"); + let module_name = module.clone(); + let tagged: TaggedLogStream = Box::pin(stream.map(move |item| { + item.map(|log| (module_name.clone(), chain_id, log)) + .map_err(anyhow::Error::from) + })); + streams.push(tagged); + } + Err(err) => { + warn!(module = %module, chain_id, error = %err, "log subscription failed"); + } + } + } + streams +} + +pub type TaggedBlockStream = std::pin::Pin< + Box< + dyn futures::Stream> + + Send, + >, +>; +pub type TaggedLogStream = std::pin::Pin< + Box< + dyn futures::Stream> + + Send, + >, +>; + +/// Drive the supervisor with events until `shutdown` resolves. +pub async fn run( + supervisor: &mut Supervisor, + block_streams: Vec, + log_streams: Vec, + shutdown: impl std::future::Future + Send, +) { + // `select_all` over an empty Vec yields `None` immediately, which + // would trip the "stream ended -> shut down" arm below before the + // first block / log ever flows. Engine configs that subscribe to + // only one event kind (e.g. all modules use `[[subscription]] kind + // = "block"`) are valid and must not be punished. Replace each + // empty side with `stream::pending()` so the corresponding select + // arm is never selected; the bail-on-None semantic still fires + // when a *non-empty* stream actually closes. + let mut blocks: BoxStream<'_, _> = if block_streams.is_empty() { + futures::stream::pending().boxed() + } else { + select_all(block_streams).boxed() + }; + let mut logs: BoxStream<'_, _> = if log_streams.is_empty() { + futures::stream::pending().boxed() + } else { + select_all(log_streams).boxed() + }; + let mut shutdown = Box::pin(shutdown); + loop { + tokio::select! { + biased; + () = &mut shutdown => return, + next = blocks.next() => match next { + Some(Ok((chain_id, header))) => { + let block = nexum::host::types::Block { + chain_id, + number: header.number, + hash: header.hash.as_slice().to_vec(), + timestamp: header.timestamp.saturating_mul(1000), + }; + supervisor.dispatch_block(block).await; + } + Some(Err(err)) => warn!(error = %err, "block stream error - continuing"), + None => { + // alloy ends the stream with None when the + // WebSocket drops. Without this branch the loop + // keeps polling a dead stream and the operator + // sees no events with no indication anything is + // wrong. Bail out so the supervisor (or whatever + // wraps the engine) restarts us; a reconnect- + // with-backoff is the 0.3 fix. + warn!("block stream ended (WebSocket dropped?) - shutting down for restart"); + return; + } + }, + next = logs.next() => match next { + Some(Ok((module, chain_id, log))) => { + supervisor.dispatch_log(&module, chain_id, log).await; + } + Some(Err(err)) => warn!(error = %err, "log stream error - continuing"), + None => { + warn!("log stream ended (WebSocket dropped?) - shutting down for restart"); + return; + } + }, + } + } +} + +/// Wait for SIGINT or (on Unix) SIGTERM, whichever arrives first. +pub async fn wait_for_shutdown_signal() -> anyhow::Result<&'static str> { + #[cfg(unix)] + { + use tokio::signal::unix::{SignalKind, signal}; + let mut sigterm = signal(SignalKind::terminate())?; + let mut sigint = signal(SignalKind::interrupt())?; + tokio::select! { + _ = sigterm.recv() => Ok("SIGTERM"), + _ = sigint.recv() => Ok("SIGINT"), + } + } + #[cfg(not(unix))] + { + tokio::signal::ctrl_c().await?; + Ok("ctrl-c") + } +} diff --git a/crates/nexum-engine/src/runtime/limits.rs b/crates/nexum-engine/src/runtime/limits.rs new file mode 100644 index 0000000..1f4b1d9 --- /dev/null +++ b/crates/nexum-engine/src/runtime/limits.rs @@ -0,0 +1,14 @@ +//! Per-module wasmtime fuel + memory limits. The supervisor refuels +//! the store before every `on_event` so each invocation gets a fresh +//! budget; a module that exhausts fuel traps with `OutOfFuel` and is +//! marked dead. + +/// Default fuel budget granted per `on_event` invocation +/// (~ 1 billion WASM instructions). Configurable per-module via +/// `engine.toml` in 0.3. +pub const DEFAULT_FUEL_PER_EVENT: u64 = 1_000_000_000; + +/// Default linear-memory cap per module store (64 MiB). Prevents a +/// single runaway module from exhausting process memory. Configurable +/// in 0.3. +pub const DEFAULT_MEMORY_LIMIT: usize = 64 * 1024 * 1024; diff --git a/crates/nexum-engine/src/runtime/mod.rs b/crates/nexum-engine/src/runtime/mod.rs new file mode 100644 index 0000000..72ea95f --- /dev/null +++ b/crates/nexum-engine/src/runtime/mod.rs @@ -0,0 +1,5 @@ +//! Engine-side runtime: per-module resource limits and the event loop +//! that drives the supervisor from live chain subscriptions. + +pub mod event_loop; +pub mod limits; diff --git a/crates/nexum-engine/src/supervisor.rs b/crates/nexum-engine/src/supervisor.rs new file mode 100644 index 0000000..796a37d --- /dev/null +++ b/crates/nexum-engine/src/supervisor.rs @@ -0,0 +1,449 @@ +//! Multi-module supervisor. +//! +//! Loads every `[[modules]]` entry from `engine.toml`, instantiates +//! each as a `Shepherd` bindings against a dedicated wasmtime +//! `Store`, and routes the event types declared in each manifest's +//! `[[subscription]]` table. +//! +//! Trap handling (BLEU-817): a wasmtime trap in `on_event` marks the +//! module as `alive = false` and removes it from all future dispatch. +//! The module's subscriptions remain registered (the event-loop +//! streams are not closed) but the dispatcher skips dead modules. +//! Full restart-with-backoff lands in 0.3. + +use std::collections::BTreeSet; +use std::path::Path; + +use anyhow::{Context, Error, Result, anyhow}; +use tracing::{error, info, warn}; +use wasmtime::component::{Component, Linker, ResourceTable}; +use wasmtime::{Engine, Store}; +use wasmtime_wasi::WasiCtxBuilder; + +use crate::bindings::{Config, Shepherd, nexum}; +use crate::engine_config::{EngineConfig, ModuleEntry}; +use crate::host::cow_orderbook::OrderBookPool; +use crate::host::local_store_redb::LocalStore; +use crate::host::provider_pool::ProviderPool; +use crate::host::state::HostState; +use crate::manifest::{self, LoadedManifest, Subscription}; +use crate::runtime::limits::{DEFAULT_FUEL_PER_EVENT, DEFAULT_MEMORY_LIMIT}; + +/// Owns every loaded module and exposes the dispatch surface the +/// event loop needs. +pub struct Supervisor { + modules: Vec, +} + +struct LoadedModule { + name: String, + bindings: Shepherd, + store: Store, + /// Subscriptions copied from `module.toml`. The supervisor reads + /// these on every event to decide whether to dispatch. + subscriptions: Vec, + /// Set to `false` when `on_event` traps. Dead modules are silently + /// skipped on every subsequent dispatch. Full restart-with-backoff + /// lands in 0.3. + alive: bool, +} + +impl Supervisor { + /// Compile + instantiate every module declared in + /// `engine_cfg.modules`. The wasmtime `Engine` + `Linker` are + /// passed in so `main.rs` can build them once (the bindgen + /// `Shepherd::add_to_linker` call binds them to `HostState`, + /// which the supervisor does not re-derive). + pub async fn boot( + engine: &Engine, + linker: &Linker, + engine_cfg: &EngineConfig, + cow_pool: &OrderBookPool, + provider_pool: &ProviderPool, + local_store: &LocalStore, + ) -> Result { + let mut modules = Vec::with_capacity(engine_cfg.modules.len()); + for entry in &engine_cfg.modules { + let loaded = + Self::load_one(engine, linker, entry, cow_pool, provider_pool, local_store) + .await + .with_context(|| format!("load module {}", entry.path.display()))?; + modules.push(loaded); + } + let alive = modules.iter().filter(|m| m.alive).count(); + info!(loaded = modules.len(), alive, "supervisor up"); + Ok(Self { modules }) + } + + /// One-shot construction from a single ad-hoc `(component, manifest)` + /// pair. Used by the CLI-positional invocation so `just run` + /// against the example module keeps working without an + /// `engine.toml`. + pub async fn boot_single( + engine: &Engine, + linker: &Linker, + wasm: &Path, + manifest: Option<&Path>, + cow_pool: &OrderBookPool, + provider_pool: &ProviderPool, + local_store: &LocalStore, + ) -> Result { + let entry = ModuleEntry { + path: wasm.to_path_buf(), + manifest: manifest.map(Path::to_path_buf), + }; + let loaded = + Self::load_one(engine, linker, &entry, cow_pool, provider_pool, local_store).await?; + Ok(Self { + modules: vec![loaded], + }) + } + + async fn load_one( + engine: &Engine, + linker: &Linker, + entry: &ModuleEntry, + cow_pool: &OrderBookPool, + provider_pool: &ProviderPool, + local_store: &LocalStore, + ) -> Result { + // Canonical name is module.toml (ADR-0001). nexum.toml is accepted + // with a deprecation warning during the 0.1→0.2 transition. + let manifest_path = entry.manifest.clone().or_else(|| { + let dir = entry.path.parent()?.to_owned(); + let canonical = dir.join("module.toml"); + if canonical.exists() { + return Some(canonical); + } + let legacy = dir.join("nexum.toml"); + if legacy.exists() { + eprintln!( + "[deprecation] nexum.toml is deprecated; rename to module.toml \ + (ADR-0001). Support will be removed in 0.3." + ); + return Some(legacy); + } + None + }); + let loaded_manifest: LoadedManifest = match manifest_path.as_deref() { + Some(p) if p.exists() => { + info!(manifest = %p.display(), "loading module manifest"); + manifest::load(p)? + } + _ => { + warn!( + component = %entry.path.display(), + "no module.toml - falling back to anonymous module" + ); + manifest::fallback_manifest() + } + }; + + // Compile + instantiate. + info!(component = %entry.path.display(), "compiling component"); + let component = Component::from_file(engine, &entry.path) + .map_err(Error::from) + .with_context(|| format!("compile {}", entry.path.display()))?; + + // Enforce capability declarations before spending time on instantiation. + manifest::enforce_capabilities( + &loaded_manifest, + component.component_type().imports(engine).map(|(n, _)| n), + ) + .with_context(|| format!("capability violation in {}", entry.path.display()))?; + let wasi = WasiCtxBuilder::new().inherit_stdio().build(); + let module_namespace = if loaded_manifest.manifest.module.name.is_empty() { + "module".to_owned() + } else { + loaded_manifest.manifest.module.name.clone() + }; + let limits = wasmtime::StoreLimitsBuilder::new() + .memory_size(DEFAULT_MEMORY_LIMIT) + .build(); + let mut store = Store::new( + engine, + HostState { + wasi, + table: ResourceTable::new(), + limits, + monotonic_baseline: std::time::Instant::now(), + http_allowlist: loaded_manifest.http_allowlist.clone(), + module_namespace: module_namespace.clone(), + cow: cow_pool.clone(), + chain: provider_pool.clone(), + store: local_store.clone(), + }, + ); + store.limiter(|state| &mut state.limits); + store.set_fuel(DEFAULT_FUEL_PER_EVENT)?; + let bindings = Shepherd::instantiate_async(&mut store, &component, linker) + .await + .map_err(Error::from) + .with_context(|| format!("instantiate {}", entry.path.display()))?; + + // Call `init` with the manifest's `[config]`. + let config: Config = if loaded_manifest.config.is_empty() { + vec![("name".into(), module_namespace.clone())] + } else { + loaded_manifest.config.clone() + }; + // Whether `init` returned `Ok(())`. When `init` returns + // `Err(HostError)` the module's strategy state (e.g. an + // `OnceLock`) is left uninitialised. Existing M3 + // example modules short-circuit on the missing state via + // `SETTINGS.get().is_none() -> return Ok(())`, but future + // modules without that guard could panic, and even with the + // guard each dispatch wastes fuel + an RPC subscription tick + // on a no-op. The `LoadedModule.alive` flag below is set from + // this result so the dispatcher skips the failed module + // without surfacing it to the dispatch fast-path. See + // COW-1070. + let init_succeeded = match bindings + .call_init(&mut store, &config) + .await + .map_err(Error::from)? + { + Ok(()) => { + info!(module = %module_namespace, "init succeeded"); + true + } + Err(e) => { + warn!( + module = %module_namespace, + domain = %e.domain, + kind = ?e.kind, + code = e.code, + message = %e.message, + "init failed - module loaded but marked dead; dispatcher will skip it", + ); + false + } + }; + // Refuel after init so the first on_event starts with a full budget. + store.set_fuel(DEFAULT_FUEL_PER_EVENT)?; + + // Surface any `[[subscription]]` entries the host cannot + // service yet, so an operator running 0.2 against a 0.3 + // manifest does not silently drop events. + for sub in &loaded_manifest.manifest.subscriptions { + if matches!(sub, Subscription::Cron { .. }) { + warn!( + module = %module_namespace, + "cron subscriptions are declared but inert in 0.2 (lands in 0.3)", + ); + } + } + + Ok(LoadedModule { + name: module_namespace, + bindings, + store, + subscriptions: loaded_manifest.manifest.subscriptions.clone(), + alive: init_succeeded, + }) + } + + /// Number of modules currently loaded. + pub fn module_count(&self) -> usize { + self.modules.len() + } + + /// Set of chain ids any module asked for block events on. The + /// caller opens one shared block subscription per chain id and + /// routes through `dispatch_block`. + pub fn block_chains(&self) -> BTreeSet { + let mut out = BTreeSet::new(); + for module in &self.modules { + for sub in &module.subscriptions { + if let Subscription::Block { chain_id } = sub { + out.insert(*chain_id); + } + } + } + out + } + + /// Per-module log subscriptions. Each entry is a `(module_name, + /// chain_id, filter)` triple the event loop opens against the + /// matching alloy provider; the resulting stream tags every log + /// with `module_name` so `dispatch_log` routes correctly. + pub fn log_subscriptions(&self) -> Vec<(String, u64, alloy_rpc_types_eth::Filter)> { + let mut out = Vec::new(); + for module in &self.modules { + for sub in &module.subscriptions { + if let Subscription::Log { + chain_id, + address, + event_signature, + } = sub + { + match build_alloy_filter(address.as_deref(), event_signature.as_deref()) { + Ok(filter) => out.push((module.name.clone(), *chain_id, filter)), + Err(err) => warn!( + module = %module.name, + chain_id, + error = %err, + "invalid log subscription - skipping", + ), + } + } + } + } + out + } + + /// Dispatch a block event to every module subscribed to + /// `block.chain_id`. Returns the number of modules invoked. + /// Modules that trap are marked dead and excluded from future dispatch. + pub async fn dispatch_block(&mut self, block: nexum::host::types::Block) -> usize { + let chain_id = block.chain_id; + let event = nexum::host::types::Event::Block(block); + let mut dispatched = 0; + for module in &mut self.modules { + if !module.alive { + continue; + } + let subscribed = module + .subscriptions + .iter() + .any(|s| matches!(s, Subscription::Block { chain_id: cid } if *cid == chain_id)); + if !subscribed { + continue; + } + // Refuel before each invocation so each event gets a fresh budget. + if let Err(e) = module.store.set_fuel(DEFAULT_FUEL_PER_EVENT) { + error!(module = %module.name, error = %e, "set_fuel failed - skipping"); + continue; + } + match module + .bindings + .call_on_event(&mut module.store, &event) + .await + { + Ok(Ok(())) => dispatched += 1, + Ok(Err(host_err)) => warn!( + module = %module.name, + chain_id, + domain = %host_err.domain, + kind = ?host_err.kind, + message = %host_err.message, + "on-event returned host-error", + ), + Err(trap) => { + error!( + module = %module.name, + chain_id, + error = %trap, + "on-event trapped - module marked dead, removed from dispatch", + ); + module.alive = false; + } + } + } + dispatched + } + + /// Dispatch a log event to the specific module that opened the + /// subscription. Returns `true` when the module accepted the dispatch; + /// `false` when the module is dead, not found, or its callback failed. + /// A trapping module is marked dead and excluded from future dispatch. + pub async fn dispatch_log( + &mut self, + module_name: &str, + chain_id: u64, + log: alloy_rpc_types_eth::Log, + ) -> bool { + let target = match self.modules.iter_mut().find(|m| m.name == module_name) { + Some(m) => m, + None => { + warn!(module = %module_name, "no such module - dropping log"); + return false; + } + }; + if !target.alive { + return false; + } + if let Err(e) = target.store.set_fuel(DEFAULT_FUEL_PER_EVENT) { + error!(module = %module_name, error = %e, "set_fuel failed - skipping"); + return false; + } + let event = nexum::host::types::Event::Logs(vec![project_log(chain_id, &log)]); + match target + .bindings + .call_on_event(&mut target.store, &event) + .await + { + Ok(Ok(())) => true, + Ok(Err(host_err)) => { + warn!( + module = %module_name, + chain_id, + domain = %host_err.domain, + kind = ?host_err.kind, + message = %host_err.message, + "on-event returned host-error", + ); + false + } + Err(trap) => { + error!( + module = %module_name, + chain_id, + error = %trap, + "on-event trapped - module marked dead, removed from dispatch", + ); + target.alive = false; + false + } + } + } + + /// Count of modules currently alive (not dead due to traps). + #[cfg_attr(not(test), allow(dead_code))] + pub fn alive_count(&self) -> usize { + self.modules.iter().filter(|m| m.alive).count() + } +} + +/// Project an alloy `Log` onto the WIT `log` record. The chain id +/// is not on the alloy log (the subscription context carries it), +/// so we receive it alongside. +fn project_log(chain_id: u64, log: &alloy_rpc_types_eth::Log) -> nexum::host::types::Log { + nexum::host::types::Log { + chain_id, + address: log.address().as_slice().to_vec(), + topics: log.topics().iter().map(|t| t.as_slice().to_vec()).collect(), + data: log.inner.data.data.to_vec(), + block_number: log.block_number.unwrap_or(0), + transaction_hash: log + .transaction_hash + .map(|h| h.as_slice().to_vec()) + .unwrap_or_default(), + log_index: log.log_index.unwrap_or(0) as u32, + } +} + +/// Translate a `[[subscription]]` log entry into an alloy `Filter`. +fn build_alloy_filter( + address: Option<&str>, + event_signature: Option<&str>, +) -> Result { + use alloy_primitives::{Address, B256}; + let mut filter = alloy_rpc_types_eth::Filter::new(); + if let Some(addr_hex) = address { + let addr: Address = addr_hex + .parse() + .map_err(|e| anyhow!("invalid log address {addr_hex:?}: {e}"))?; + filter = filter.address(addr); + } + if let Some(topic_hex) = event_signature { + let topic: B256 = topic_hex + .parse() + .map_err(|e| anyhow!("invalid topic {topic_hex:?}: {e}"))?; + filter = filter.event_signature(topic); + } + Ok(filter) +} + +#[cfg(test)] +mod tests; diff --git a/crates/nexum-engine/src/supervisor/tests.rs b/crates/nexum-engine/src/supervisor/tests.rs new file mode 100644 index 0000000..700a7c2 --- /dev/null +++ b/crates/nexum-engine/src/supervisor/tests.rs @@ -0,0 +1,493 @@ +use std::path::{Path, PathBuf}; + +use super::*; + +#[test] +fn empty_supervisor_returns_no_subscriptions() { + let sup = Supervisor { + modules: Vec::new(), + }; + assert!(sup.block_chains().is_empty()); + assert!(sup.log_subscriptions().is_empty()); + assert_eq!(sup.module_count(), 0); +} + +/// Regression guard: engines whose modules only declare +/// `[[subscription]] kind = "block"` (or only `kind = "log"`) must not +/// bail at boot. Previously `select_all` on an empty `Vec` yielded +/// `None` immediately and the "stream ended -> shut down" arm fired +/// before any event flowed. The fix in `runtime/event_loop.rs` +/// substitutes `stream::pending()` when the Vec is empty so the +/// corresponding select arm is never selected. +/// +/// Surfaced when wiring up `engine.m3.toml` for the M3 testnet runbook: +/// the 3 M3 example modules (price-alert, balance-tracker, stop-loss) +/// all subscribe to blocks only, no logs. The engine bailed within +/// ~50 ms of `supervisor ready` until this fix landed. +#[tokio::test] +async fn run_does_not_bail_when_both_stream_kinds_are_empty() { + use std::time::{Duration, Instant}; + + let mut supervisor = Supervisor { + modules: Vec::new(), + }; + let started = Instant::now(); + let shutdown = tokio::time::sleep(Duration::from_millis(50)); + + crate::runtime::event_loop::run(&mut supervisor, Vec::new(), Vec::new(), shutdown).await; + + // If the bug were present, `run` returns ~0 ms (the empty `logs` + // stream's first `.next()` yields `None` and the loop bails on + // the bail-on-None arm). With the fix, `run` blocks on `shutdown` + // for the full 50 ms. + let elapsed = started.elapsed(); + assert!( + elapsed >= Duration::from_millis(40), + "run returned in {elapsed:?}, expected >= ~50ms (shutdown timer)", + ); +} + +// ── E2E helpers ─────────────────────────────────────────────────────── + +/// Path to the pre-built example WASM component. Tests that need it +/// call `example_wasm_or_skip()` which skips gracefully if absent. +fn example_wasm() -> PathBuf { + // CARGO_MANIFEST_DIR → crates/nexum-engine + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .join("target/wasm32-wasip2/release/example.wasm") +} + +fn example_module_toml() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .join("modules/example/module.toml") +} + +/// Returns `None` and prints a skip message if the fixture isn't built. +fn example_wasm_or_skip() -> Option { + let p = example_wasm(); + if p.exists() { + Some(p) + } else { + eprintln!( + "SKIP: {} not found - run `just build-module` to enable E2E tests", + p.display() + ); + None + } +} + +fn make_wasmtime_engine() -> wasmtime::Engine { + let mut config = wasmtime::Config::new(); + config.wasm_component_model(true); + config.consume_fuel(true); + wasmtime::Engine::new(&config).expect("wasmtime engine") +} + +fn make_linker(engine: &wasmtime::Engine) -> Linker { + let mut linker = Linker::::new(engine); + crate::Shepherd::add_to_linker::< + crate::HostState, + wasmtime::component::HasSelf, + >(&mut linker, |s| s) + .expect("add_to_linker"); + wasmtime_wasi::p2::add_to_linker_async(&mut linker).expect("add_wasi"); + linker +} + +/// Return `(dir, store)` so the test holds the `TempDir` for the +/// duration of the test scope and cleans it up on drop. Forgetting +/// the dir (the old `ManuallyDrop` approach) leaks it for the +/// entire process lifetime. +fn temp_local_store() -> (tempfile::TempDir, crate::host::local_store_redb::LocalStore) { + let dir = tempfile::tempdir().expect("tempdir"); + let path = dir.path().join("ls.redb"); + let store = crate::host::local_store_redb::LocalStore::open(path).expect("local store"); + (dir, store) +} + +// ── E2E tests ───────────────────────────────────────────────────────── + +/// Boot supervisor with the example module; verify it starts alive. +#[tokio::test] +async fn e2e_supervisor_boots_example_module() { + let Some(wasm) = example_wasm_or_skip() else { + return; + }; + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let cow_pool = crate::host::cow_orderbook::OrderBookPool::default(); + let provider_pool = crate::host::provider_pool::ProviderPool::empty(); + let (_dir, local_store) = temp_local_store(); + + let supervisor = Supervisor::boot_single( + &engine, + &linker, + &wasm, + Some(example_module_toml()).as_deref(), + &cow_pool, + &provider_pool, + &local_store, + ) + .await + .expect("boot_single"); + + assert_eq!(supervisor.module_count(), 1); + assert_eq!(supervisor.alive_count(), 1); +} + +/// Boot with a manifest that subscribes to block events; dispatch one +/// block event and verify the module was invoked and stayed alive. +#[tokio::test] +async fn e2e_block_subscription_dispatched() { + let Some(wasm) = example_wasm_or_skip() else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + let manifest = dir.path().join("module.toml"); + std::fs::write( + &manifest, + r#" +[module] +name = "example" + +[capabilities] +required = ["logging"] + +[[subscription]] +kind = "block" +chain_id = 1 +"#, + ) + .unwrap(); + + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let cow_pool = crate::host::cow_orderbook::OrderBookPool::default(); + let provider_pool = crate::host::provider_pool::ProviderPool::empty(); + let (_dir, local_store) = temp_local_store(); + + let mut supervisor = Supervisor::boot_single( + &engine, + &linker, + &wasm, + Some(&manifest), + &cow_pool, + &provider_pool, + &local_store, + ) + .await + .expect("boot_single"); + + let block = nexum::host::types::Block { + chain_id: 1, + number: 19_000_000, + hash: vec![0xab; 32], + timestamp: 1_700_000_000_000, + }; + let dispatched = supervisor.dispatch_block(block).await; + assert_eq!(dispatched, 1, "one module subscribed to chain 1 blocks"); + assert_eq!(supervisor.alive_count(), 1, "module must remain alive"); +} + +// ── COW-1068: production module integration tests ──────────────────── +// +// One test per module that goes through the real wit-bindgen + +// WitBindgenHost adapter + supervisor dispatch path, not just the +// strategy-level MockHost coverage. Mirrors the example-module e2e +// shape above; each test is guarded by `module_wasm_or_skip()` so +// local runs without a fresh `--target wasm32-wasip2 --release` +// build are skipped rather than failing. + +const SEPOLIA: u64 = 11_155_111; + +/// Path to a production module's .wasm artefact under the workspace +/// target dir. `Cargo` writes the artefact as `.wasm` with +/// hyphens replaced by underscores, so the helper mirrors that. +fn module_wasm(module_name: &str) -> PathBuf { + let artifact = module_name.replace('-', "_"); + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .join(format!("target/wasm32-wasip2/release/{artifact}.wasm")) +} + +fn module_wasm_or_skip(module_name: &str) -> Option { + let p = module_wasm(module_name); + if p.exists() { + Some(p) + } else { + eprintln!( + "SKIP: {} not found - build with `cargo build -p {module_name} --target wasm32-wasip2 --release`", + p.display() + ); + None + } +} + +/// Resolve a real `module.toml` for one of the production modules. +/// Looking up the real manifest (rather than synthesising one) keeps +/// the integration test honest about the capability set + subscription +/// shape each module actually ships. +fn production_module_toml(relative_path: &str) -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .join(relative_path) +} + +fn synthetic_sepolia_block() -> nexum::host::types::Block { + nexum::host::types::Block { + chain_id: SEPOLIA, + number: 19_000_000, + hash: vec![0xab; 32], + timestamp: 1_700_000_000_000, + } +} + +/// Boot a single module from `(wasm, manifest)` and return the live +/// supervisor. Shared body across the 5 integration tests. +async fn boot_production_module( + engine: &wasmtime::Engine, + linker: &Linker, + local_store: &crate::host::local_store_redb::LocalStore, + wasm: &Path, + manifest: &Path, +) -> Supervisor { + let cow_pool = crate::host::cow_orderbook::OrderBookPool::default(); + let provider_pool = crate::host::provider_pool::ProviderPool::empty(); + Supervisor::boot_single( + engine, + linker, + wasm, + Some(manifest), + &cow_pool, + &provider_pool, + local_store, + ) + .await + .expect("boot_single") +} + +#[tokio::test] +async fn e2e_twap_monitor_block_dispatch() { + let Some(wasm) = module_wasm_or_skip("twap-monitor") else { + return; + }; + let manifest = production_module_toml("modules/twap-monitor/module.toml"); + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + assert_eq!(supervisor.module_count(), 1); + assert_eq!(supervisor.alive_count(), 1); + + // twap-monitor subscribes to Sepolia blocks (poll path). A real + // poll would call chain::request, which ProviderPool::empty() does + // not satisfy - the module surfaces a host-error and warns; the + // supervisor must keep the module alive because the strategy + // catches the error and returns Ok(()). + let dispatched = supervisor.dispatch_block(synthetic_sepolia_block()).await; + assert_eq!(dispatched, 1); + assert_eq!(supervisor.alive_count(), 1); +} + +#[tokio::test] +async fn e2e_ethflow_watcher_log_dispatch() { + let Some(wasm) = module_wasm_or_skip("ethflow-watcher") else { + return; + }; + let manifest = production_module_toml("modules/ethflow-watcher/module.toml"); + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + assert_eq!(supervisor.alive_count(), 1); + + // A log with an unrecognised topic is silently skipped by the + // module's decoder (returns `None` from `decode_order_placement`), + // so the test only proves: supervisor delivered, module did not + // trap, module stayed alive. Stronger asserts (submitted:{uid} + // markers etc.) require a hand-crafted ABI-encoded OrderPlacement + // payload and the real ETH_FLOW_PRODUCTION address, deferred to + // COW-1064 testnet integration. + let synthetic_log = alloy_rpc_types_eth::Log::default(); + let dispatched = supervisor + .dispatch_log("ethflow-watcher", SEPOLIA, synthetic_log) + .await; + assert!(dispatched); + assert_eq!(supervisor.alive_count(), 1); +} + +#[tokio::test] +async fn e2e_price_alert_block_dispatch() { + let Some(wasm) = module_wasm_or_skip("price-alert") else { + return; + }; + let manifest = production_module_toml("modules/examples/price-alert/module.toml"); + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + let dispatched = supervisor.dispatch_block(synthetic_sepolia_block()).await; + assert_eq!(dispatched, 1); + assert_eq!(supervisor.alive_count(), 1); +} + +#[tokio::test] +async fn e2e_balance_tracker_block_dispatch() { + let Some(wasm) = module_wasm_or_skip("balance-tracker") else { + return; + }; + let manifest = production_module_toml("modules/examples/balance-tracker/module.toml"); + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + let dispatched = supervisor.dispatch_block(synthetic_sepolia_block()).await; + assert_eq!(dispatched, 1); + assert_eq!(supervisor.alive_count(), 1); +} + +#[tokio::test] +async fn e2e_stop_loss_block_dispatch() { + let Some(wasm) = module_wasm_or_skip("stop-loss") else { + return; + }; + let manifest = production_module_toml("modules/examples/stop-loss/module.toml"); + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + let dispatched = supervisor.dispatch_block(synthetic_sepolia_block()).await; + assert_eq!(dispatched, 1); + assert_eq!(supervisor.alive_count(), 1); +} + +// ── COW-1070: init-failed modules must be marked dead ──────────────── + +/// Drive `Supervisor::boot_single` with a module whose `[config]` +/// carries a malformed `threshold` value (`"not-a-number"`). The +/// module's `init` returns `Err(HostError { kind: InvalidInput })`. +/// Pre-COW-1070 the supervisor still marked the module +/// `alive = true`, so it received block dispatches forever. The fix +/// flips `alive = false` when `init` fails. +/// +/// Surfaced live on Sepolia in +/// `docs/operations/m3-edge-case-validation.md` scenario 1.4. +#[tokio::test] +async fn init_failure_marks_module_dead_and_excludes_from_dispatch() { + let Some(wasm) = module_wasm_or_skip("price-alert") else { + return; + }; + + // Synthesise a manifest with the same shape as the real + // price-alert module but with a `threshold` that the strategy + // rejects in `parse_config`. + let dir = tempfile::tempdir().unwrap(); + let manifest = dir.path().join("module.toml"); + std::fs::write( + &manifest, + r#" +[module] +name = "price-alert" + +[capabilities] +required = ["logging", "chain"] + +[[subscription]] +kind = "block" +chain_id = 11155111 + +[config] +oracle_address = "0x694AA1769357215DE4FAC081bf1f309aDC325306" +decimals = "8" +threshold = "not-a-number" +direction = "below" +every_n_blocks = "1" +"#, + ) + .unwrap(); + + let engine = make_wasmtime_engine(); + let linker = make_linker(&engine); + let (_dir, store) = temp_local_store(); + + let mut supervisor = boot_production_module(&engine, &linker, &store, &wasm, &manifest).await; + + // The module loaded successfully (wasm compiled, capabilities + // matched, manifest parsed) but `init` returned InvalidInput. + assert_eq!(supervisor.module_count(), 1, "module is loaded"); + assert_eq!( + supervisor.alive_count(), + 0, + "init-failed module must be marked dead", + ); + + // Dispatch the synthetic block. The init-failed module must + // not be reached by the dispatcher. + let dispatched = supervisor.dispatch_block(synthetic_sepolia_block()).await; + assert_eq!( + dispatched, 0, + "no live module is subscribed to chain 11155111 blocks", + ); +} + +// ── build_alloy_filter ──────────────────────────────────────────────── + +#[test] +fn alloy_filter_with_address_and_topic() { + let addr = "0xC92E8bdf79f0507f65a392b0ab4667716BFE0110"; + let topic = "0x237e158222e3e6968b72b9db0d8043aacf074ad9f650f0d1606b4d82ee432c00"; + let filter = build_alloy_filter(Some(addr), Some(topic)).unwrap(); + // Check address is set (alloy Filter doesn't expose a simple getter, + // but we can verify the filter serialises the address field). + let serialised = serde_json::to_value(&filter).unwrap(); + let addr_field = serialised + .get("address") + .unwrap() + .to_string() + .to_lowercase(); + assert!(addr_field.contains(&addr.to_lowercase()[2..])); // strip 0x +} + +#[test] +fn alloy_filter_no_address_no_topic() { + let filter = build_alloy_filter(None, None).unwrap(); + let serialised = serde_json::to_value(&filter).unwrap(); + // Address and topics should be absent or null. + assert!( + serialised.get("address").is_none() + || serialised["address"].is_null() + || serialised["address"] == serde_json::json!([]) + ); +} + +#[test] +fn alloy_filter_rejects_bad_address() { + let err = build_alloy_filter(Some("not-an-address"), None); + assert!(err.is_err()); +} + +#[test] +fn alloy_filter_rejects_bad_topic() { + let addr = "0xC92E8bdf79f0507f65a392b0ab4667716BFE0110"; + let err = build_alloy_filter(Some(addr), Some("not-a-topic")); + assert!(err.is_err()); +} diff --git a/crates/shepherd-sdk-test/Cargo.toml b/crates/shepherd-sdk-test/Cargo.toml new file mode 100644 index 0000000..2735cc8 --- /dev/null +++ b/crates/shepherd-sdk-test/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "shepherd-sdk-test" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "In-memory host mocks for Shepherd module unit tests. Implements shepherd_sdk::host::{ChainHost, LocalStoreHost, CowApiHost, LoggingHost}." + +[lib] +# Plain library, host-only - module Cargo.toml lists this under +# [dev-dependencies] so it never ships in the wasm bundle. + +[dependencies] +shepherd-sdk = { path = "../shepherd-sdk" } +serde_json = "1" diff --git a/crates/shepherd-sdk-test/src/lib.rs b/crates/shepherd-sdk-test/src/lib.rs new file mode 100644 index 0000000..efe93fa --- /dev/null +++ b/crates/shepherd-sdk-test/src/lib.rs @@ -0,0 +1,463 @@ +//! # shepherd-sdk-test +//! +//! In-memory implementations of the [`shepherd_sdk::host`] traits +//! plus assertion helpers, so a Shepherd module can write integration +//! tests for its strategy logic without `wit-bindgen`, `wasmtime`, or +//! a network round-trip. +//! +//! ## Usage +//! +//! Add as a dev-dep on the module crate: +//! +//! ```toml +//! [dev-dependencies] +//! shepherd-sdk-test = { path = "../../crates/shepherd-sdk-test" } +//! ``` +//! +//! Structure the module's strategy function around the host traits: +//! +//! ```rust,ignore +//! pub fn handle_block( +//! host: &H, +//! chain_id: u64, +//! block_number: u64, +//! ) -> Result<(), shepherd_sdk::host::HostError> { +//! // ... +//! let res = host.request(chain_id, "eth_call", "[]")?; +//! host.set("last_block", &block_number.to_le_bytes())?; +//! host.log(shepherd_sdk::host::LogLevel::Info, "saw block"); +//! Ok(()) +//! } +//! ``` +//! +//! Test against [`MockHost`]: +//! +//! ```rust +//! // Glob-import the host traits so the method shortcuts resolve. +//! use shepherd_sdk::host::*; +//! use shepherd_sdk_test::MockHost; +//! +//! let host = MockHost::new(); +//! host.chain.respond_to("eth_blockNumber", "[]", Ok("\"0x1\"".into())); +//! +//! // Call the strategy directly: +//! assert_eq!(host.request(1, "eth_blockNumber", "[]").unwrap(), "\"0x1\""); +//! +//! // Inspect: +//! assert_eq!(host.chain.calls().len(), 1); +//! ``` +//! +//! ## Adapting from wit-bindgen +//! +//! The traits use [`shepherd_sdk::host::HostError`] rather than the +//! `HostError` `wit_bindgen::generate!` emits per-module. A module +//! bridges with two trivial `From` impls (one each direction) on its +//! own crate boundary - see the M3 tutorial (BLEU-848) for the exact +//! shape. + +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![warn(missing_docs)] + +use std::cell::RefCell; +use std::collections::HashMap; + +use shepherd_sdk::host::{ + ChainHost, CowApiHost, HostError, HostErrorKind, LocalStoreHost, LogLevel, LoggingHost, +}; + +/// Composed in-memory host. Each field exposes the per-trait mock so +/// tests can program responses and assert on calls. +#[derive(Default)] +pub struct MockHost { + /// `nexum:host/chain` mock. + pub chain: MockChain, + /// `nexum:host/local-store` mock. + pub store: MockLocalStore, + /// `shepherd:cow/cow-api` mock. + pub cow_api: MockCowApi, + /// `nexum:host/logging` mock. + pub logging: MockLogging, +} + +impl MockHost { + /// Fresh empty host. Equivalent to `Default::default`. + pub fn new() -> Self { + Self::default() + } +} + +impl ChainHost for MockHost { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + self.chain.request(chain_id, method, params) + } +} + +impl LocalStoreHost for MockHost { + fn get(&self, key: &str) -> Result>, HostError> { + self.store.get(key) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), HostError> { + self.store.set(key, value) + } + fn delete(&self, key: &str) -> Result<(), HostError> { + self.store.delete(key) + } + fn list_keys(&self, prefix: &str) -> Result, HostError> { + self.store.list_keys(prefix) + } +} + +impl CowApiHost for MockHost { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + self.cow_api.submit_order(chain_id, body) + } +} + +impl LoggingHost for MockHost { + fn log(&self, level: LogLevel, message: &str) { + self.logging.log(level, message); + } +} + +// ---------------------------------------------------------------- chain + +/// In-memory [`ChainHost`] backed by a `(method, params)` -> response +/// map. Records every call so tests can assert dispatch shape. +#[derive(Default)] +pub struct MockChain { + responses: RefCell>>, + calls: RefCell>, +} + +/// One recorded [`MockChain::request`] invocation. +#[derive(Clone, Debug)] +pub struct ChainCall { + /// EVM chain id the guest passed. + pub chain_id: u64, + /// JSON-RPC method name. + pub method: String, + /// JSON-encoded params array (verbatim). + pub params: String, +} + +impl MockChain { + /// Program a response for the `(method, params)` pair. Overwrites + /// any prior entry. + pub fn respond_to( + &self, + method: impl Into, + params: impl Into, + result: Result, + ) { + self.responses + .borrow_mut() + .insert((method.into(), params.into()), result); + } + + /// All calls received, in arrival order. + pub fn calls(&self) -> Vec { + self.calls.borrow().clone() + } + + /// Last call received, if any. + pub fn last_call(&self) -> Option { + self.calls.borrow().last().cloned() + } + + /// Total call count. + pub fn call_count(&self) -> usize { + self.calls.borrow().len() + } +} + +impl ChainHost for MockChain { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + self.calls.borrow_mut().push(ChainCall { + chain_id, + method: method.to_string(), + params: params.to_string(), + }); + self.responses + .borrow() + .get(&(method.to_string(), params.to_string())) + .cloned() + .unwrap_or_else(|| { + Err(HostError { + domain: "chain".into(), + kind: HostErrorKind::Unsupported, + code: 0, + message: format!("MockChain: no response configured for {method} {params}"), + data: None, + }) + }) + } +} + +// ---------------------------------------------------------------- local-store + +/// In-memory [`LocalStoreHost`] backed by a `HashMap`. Each operation +/// runs in O(1) except `list_keys`, which scans (small N expected for +/// tests). +#[derive(Default)] +pub struct MockLocalStore { + rows: RefCell>>, +} + +impl MockLocalStore { + /// Number of rows currently held. + pub fn len(&self) -> usize { + self.rows.borrow().len() + } + + /// Whether the store is empty. + pub fn is_empty(&self) -> bool { + self.rows.borrow().is_empty() + } + + /// Direct read for assertions - bypasses the trait. + pub fn snapshot(&self) -> HashMap> { + self.rows.borrow().clone() + } +} + +impl LocalStoreHost for MockLocalStore { + fn get(&self, key: &str) -> Result>, HostError> { + Ok(self.rows.borrow().get(key).cloned()) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), HostError> { + self.rows + .borrow_mut() + .insert(key.to_string(), value.to_vec()); + Ok(()) + } + fn delete(&self, key: &str) -> Result<(), HostError> { + self.rows.borrow_mut().remove(key); + Ok(()) + } + fn list_keys(&self, prefix: &str) -> Result, HostError> { + let mut keys: Vec = self + .rows + .borrow() + .keys() + .filter(|k| k.starts_with(prefix)) + .cloned() + .collect(); + keys.sort(); + Ok(keys) + } +} + +// ---------------------------------------------------------------- cow-api + +/// In-memory [`CowApiHost`] that captures every submission and returns +/// a programmable response. +#[derive(Default)] +pub struct MockCowApi { + response: RefCell>>, + calls: RefCell>, +} + +/// One recorded [`MockCowApi::submit_order`] invocation. +#[derive(Clone, Debug)] +pub struct SubmitCall { + /// Chain the guest targeted. + pub chain_id: u64, + /// Raw `OrderCreation` JSON body. + pub body: Vec, +} + +impl MockCowApi { + /// Program the response the mock returns on every subsequent + /// `submit_order` call. Defaults to a host-side `Unsupported` + /// error if unset. + pub fn respond(&self, result: Result) { + *self.response.borrow_mut() = Some(result); + } + + /// All submissions, in arrival order. + pub fn calls(&self) -> Vec { + self.calls.borrow().clone() + } + + /// Last submission, if any. + pub fn last_call(&self) -> Option { + self.calls.borrow().last().cloned() + } + + /// Convenience: parse the most recent body as JSON. + pub fn last_body_as_json(&self) -> Option { + self.last_call() + .and_then(|c| serde_json::from_slice(&c.body).ok()) + } + + /// Count of submissions. + pub fn call_count(&self) -> usize { + self.calls.borrow().len() + } +} + +impl CowApiHost for MockCowApi { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + self.calls.borrow_mut().push(SubmitCall { + chain_id, + body: body.to_vec(), + }); + self.response.borrow().clone().unwrap_or_else(|| { + Err(HostError::unsupported( + "cow-api", + "MockCowApi: no response configured", + )) + }) + } +} + +// ---------------------------------------------------------------- logging + +/// One recorded log line. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct LogLine { + /// Severity the module passed. + pub level: LogLevel, + /// Message body. + pub message: String, +} + +/// In-memory [`LoggingHost`] that buffers every emitted line. +#[derive(Default)] +pub struct MockLogging { + lines: RefCell>, +} + +impl MockLogging { + /// All buffered log lines, in emission order. + pub fn lines(&self) -> Vec { + self.lines.borrow().clone() + } + + /// `true` if any buffered line contains `needle` (substring match). + pub fn contains(&self, needle: &str) -> bool { + self.lines + .borrow() + .iter() + .any(|l| l.message.contains(needle)) + } + + /// Count of lines at `level`. + pub fn count_at(&self, level: LogLevel) -> usize { + self.lines + .borrow() + .iter() + .filter(|l| l.level == level) + .count() + } +} + +impl LoggingHost for MockLogging { + fn log(&self, level: LogLevel, message: &str) { + self.lines.borrow_mut().push(LogLine { + level, + message: message.to_string(), + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn chain_records_calls_and_returns_programmed_response() { + let chain = MockChain::default(); + chain.respond_to("eth_blockNumber", "[]", Ok("\"0x1234\"".into())); + + assert_eq!( + chain.request(1, "eth_blockNumber", "[]").unwrap(), + "\"0x1234\"" + ); + assert_eq!(chain.call_count(), 1); + let last = chain.last_call().unwrap(); + assert_eq!(last.chain_id, 1); + assert_eq!(last.method, "eth_blockNumber"); + } + + #[test] + fn chain_unconfigured_method_returns_unsupported() { + let chain = MockChain::default(); + let err = chain.request(1, "eth_call", "[]").unwrap_err(); + assert_eq!(err.kind, HostErrorKind::Unsupported); + assert!(err.message.contains("MockChain")); + assert_eq!(chain.call_count(), 1); + } + + #[test] + fn local_store_round_trips() { + let store = MockLocalStore::default(); + store.set("k", b"v").unwrap(); + assert_eq!(store.get("k").unwrap().as_deref(), Some(&b"v"[..])); + store.delete("k").unwrap(); + assert!(store.get("k").unwrap().is_none()); + } + + #[test] + fn local_store_list_keys_prefix_scan() { + let store = MockLocalStore::default(); + store.set("watch:a:1", b"").unwrap(); + store.set("watch:a:2", b"").unwrap(); + store.set("submitted:1", b"").unwrap(); + let keys = store.list_keys("watch:").unwrap(); + assert_eq!(keys, vec!["watch:a:1", "watch:a:2"]); + } + + #[test] + fn cow_api_captures_body_and_returns_uid() { + let api = MockCowApi::default(); + api.respond(Ok("0xdeadbeef".into())); + let uid = api.submit_order(1, b"{\"x\":1}").unwrap(); + assert_eq!(uid, "0xdeadbeef"); + let last = api.last_call().unwrap(); + assert_eq!(last.chain_id, 1); + assert_eq!(last.body, b"{\"x\":1}"); + assert_eq!(api.last_body_as_json().unwrap()["x"], 1); + } + + #[test] + fn cow_api_default_response_is_unsupported() { + let api = MockCowApi::default(); + let err = api.submit_order(1, b"{}").unwrap_err(); + assert_eq!(err.kind, HostErrorKind::Unsupported); + } + + #[test] + fn logging_captures_lines_and_filters_by_level() { + let log = MockLogging::default(); + log.log(LogLevel::Info, "hello"); + log.log(LogLevel::Warn, "uh oh"); + log.log(LogLevel::Info, "still here"); + + assert_eq!(log.lines().len(), 3); + assert_eq!(log.count_at(LogLevel::Info), 2); + assert_eq!(log.count_at(LogLevel::Warn), 1); + assert!(log.contains("uh oh")); + } + + #[test] + fn mock_host_dispatches_through_supertrait() { + let host = MockHost::new(); + host.chain + .respond_to("eth_blockNumber", "[]", Ok("\"0x1\"".into())); + host.cow_api.respond(Ok("0xuid".into())); + + // Through the `Host` supertrait. + let _: &dyn shepherd_sdk::host::Host = &host; + host.set("key", b"val").unwrap(); + assert_eq!(host.get("key").unwrap().as_deref(), Some(&b"val"[..])); + assert_eq!(host.request(1, "eth_blockNumber", "[]").unwrap(), "\"0x1\""); + assert_eq!(host.submit_order(1, b"{}").unwrap(), "0xuid"); + host.log(LogLevel::Info, "happy path"); + + assert_eq!(host.chain.call_count(), 1); + assert_eq!(host.cow_api.call_count(), 1); + assert_eq!(host.logging.lines().len(), 1); + assert_eq!(host.store.len(), 1); + } +} diff --git a/crates/shepherd-sdk/Cargo.toml b/crates/shepherd-sdk/Cargo.toml new file mode 100644 index 0000000..96c0bd3 --- /dev/null +++ b/crates/shepherd-sdk/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "shepherd-sdk" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "Guest-side SDK for Shepherd modules: re-exports, helpers, and prelude on top of cowprotocol + alloy types." + +[lib] +# Plain library - modules link this and emit their own cdylib for the +# WASM Component. Building shepherd-sdk on the host target is also +# supported so the helpers are unit-testable without a wasm toolchain. + +[dependencies] +cowprotocol = { version = "1.0.0-alpha.3", default-features = false } +alloy-primitives = { version = "1.6", default-features = false, features = ["std", "serde"] } +alloy-sol-types = { version = "1.5", default-features = false, features = ["std"] } +serde_json = { version = "1", default-features = false, features = ["alloc"] } +thiserror = "2" diff --git a/crates/shepherd-sdk/README.md b/crates/shepherd-sdk/README.md new file mode 100644 index 0000000..c3c0f51 --- /dev/null +++ b/crates/shepherd-sdk/README.md @@ -0,0 +1,90 @@ +# shepherd-sdk + +Guest-side SDK for [Shepherd](https://github.com/nullislabs/shepherd) modules. + +`shepherd-sdk` is the shared companion to each module's +`wit_bindgen::generate!` invocation: the module keeps its own +wit-bindgen call (which emits the world-specific `Guest` trait and +host-import shims into the module's own crate) and pulls helpers, +typed primitives, and the host trait seam from here. + +## Quick tour + +```rust +use shepherd_sdk::prelude::*; +use shepherd_sdk::cow::{gpv2_to_order_data, classify_api_error, RetryAction}; +use shepherd_sdk::chain::{eth_call_params, parse_eth_call_result}; +``` + +| Module | What it provides | +|---|---| +| `prelude` | One-liner `use ::*` for alloy primitives + cowprotocol order / signing / orderbook surface. | +| `cow::order` | `gpv2_to_order_data` - `GPv2OrderData` -> typed `OrderData`. | +| `cow::composable` | `sol! IConditionalOrder` errors + `PollOutcome` + `decode_revert`. | +| `cow::error` | `RetryAction` enum + `classify_api_error` + `try_decode_api_error`. | +| `chain::eth_call` | `eth_call_params`, `parse_eth_call_result`, `decode_revert_hex`. | +| `host` | `Host` trait seam (`ChainHost` / `LocalStoreHost` / `CowApiHost` / `LoggingHost`) + host-neutral `HostError`. | + +## Testing modules host-free + +Add the companion `shepherd-sdk-test` crate as a dev-dep and write +your strategy function against `&impl shepherd_sdk::host::Host`: + +```rust,ignore +use shepherd_sdk::host::*; + +pub fn handle_block(host: &H, chain_id: u64) -> Result<(), HostError> { + let result = host.request(chain_id, "eth_blockNumber", "[]")?; + host.log(LogLevel::Info, &format!("got {result}")); + Ok(()) +} +``` + +Tests against `MockHost` then run without `wit-bindgen` or +`wasmtime`: + +```rust,ignore +let host = MockHost::new(); +host.chain.respond_to("eth_blockNumber", "[]", Ok("\"0x1\"".into())); +handle_block(&host, 1).unwrap(); +assert_eq!(host.chain.call_count(), 1); +``` + +## Why no `wit_bindgen::generate!` in the SDK + +The macro emits types into the calling crate (the module's cdylib). +Re-exporting wit-bindgen output from a library would duplicate +symbols and break the component-export contract. Helpers in this +SDK take primitive arguments (`&[u8]`, `&str`, `Option<&str>`) so +the SDK stays world-neutral; modules unpack their wit-bindgen +`HostError` / `Log` into primitives at the call site. Trade-off +documented in ADR-0006 and ADR-0007 in `docs/adr/`. + +## Layout + +``` +crates/shepherd-sdk/ +├── src/ +│ ├── lib.rs crate root + intra-doc links +│ ├── prelude.rs bulk re-exports +│ ├── cow/ +│ │ ├── mod.rs +│ │ ├── order.rs gpv2_to_order_data +│ │ ├── composable.rs IConditionalOrder + PollOutcome + decode_revert +│ │ └── error.rs RetryAction + classify_api_error +│ ├── chain/ +│ │ ├── mod.rs +│ │ └── eth_call.rs eth_call_params + parse_eth_call_result +│ └── host.rs trait seam + SDK HostError +└── README.md you are here +``` + +## Generating docs locally + +```sh +RUSTDOCFLAGS="-D warnings -D missing-docs" cargo doc -p shepherd-sdk --no-deps --open +``` + +The CI gate `cargo doc -p shepherd-sdk --no-deps` runs under those +flags, so all public items carry doc comments and intra-doc links +resolve. diff --git a/crates/shepherd-sdk/src/chain/eth_call.rs b/crates/shepherd-sdk/src/chain/eth_call.rs new file mode 100644 index 0000000..b915f76 --- /dev/null +++ b/crates/shepherd-sdk/src/chain/eth_call.rs @@ -0,0 +1,161 @@ +//! `eth_call` JSON helpers. + +use alloy_primitives::Address; + +use crate::cow::composable::{PollOutcome, decode_revert}; + +/// Build the JSON params array for `eth_call`: `[{to, data}, "latest"]`. +/// +/// Returned as a `String` rather than `serde_json::Value` so the caller +/// can hand it straight to `chain::request(chain_id, "eth_call", &p)` +/// without re-serialising. +/// +/// # Example +/// +/// ``` +/// use shepherd_sdk::chain::eth_call_params; +/// use shepherd_sdk::prelude::Address; +/// +/// let to: Address = "0xfdaFc9d1902f4e0b84f65F49f244b32b31013b74" +/// .parse() +/// .unwrap(); +/// let selector = [0xaa, 0xbb, 0xcc, 0xdd]; // 4-byte function selector +/// let params = eth_call_params(&to, &selector); +/// +/// assert!(params.contains("\"to\":\"0xfdafc9d1902f4e0b84f65f49f244b32b31013b74\"")); +/// assert!(params.contains("\"data\":\"0xaabbccdd\"")); +/// assert!(params.contains("\"latest\"")); +/// ``` +pub fn eth_call_params(to: &Address, data: &[u8]) -> String { + let to_hex = format!("{to:#x}"); + let data_hex = alloy_primitives::hex::encode_prefixed(data); + serde_json::json!([{ "to": to_hex, "data": data_hex }, "latest"]).to_string() +} + +/// Parse the raw JSON-RPC `result` field a host's `chain::request` +/// returns for an `eth_call`. The value is a JSON string holding hex +/// like `"0x1234..."`; strip the JSON quotes, strip the `0x` prefix, +/// and hex-decode. Returns `None` on shape mismatch. +/// +/// # Example +/// +/// ``` +/// use shepherd_sdk::chain::parse_eth_call_result; +/// +/// // What the host typically returns for an eth_call result: a JSON +/// // string holding 0x-prefixed hex. +/// let raw = r#""0xdeadbeef""#; +/// assert_eq!( +/// parse_eth_call_result(raw), +/// Some(vec![0xde, 0xad, 0xbe, 0xef]), +/// ); +/// +/// // Shape mismatch (not JSON-quoted) -> None. +/// assert_eq!(parse_eth_call_result("not json"), None); +/// ``` +pub fn parse_eth_call_result(result_json: &str) -> Option> { + let s = serde_json::from_str::(result_json).ok()?; + let hex = s.strip_prefix("0x").unwrap_or(&s); + alloy_primitives::hex::decode(hex).ok() +} + +/// Decode a hex string carrying revert bytes (optionally `0x`-prefixed, +/// optionally JSON-quoted) into a [`PollOutcome`] via +/// [`crate::cow::composable::decode_revert`]. +/// +/// This is the bridge between the host's structured error data (a hex +/// string in `host-error.data`) and the typed +/// [`crate::cow::composable::PollOutcome`] dispatch. +/// +/// # Example +/// +/// ``` +/// use alloy_sol_types::SolError; +/// use shepherd_sdk::chain::decode_revert_hex; +/// use shepherd_sdk::cow::{IConditionalOrder, PollOutcome}; +/// +/// // Simulate the host forwarding an OrderNotValid revert payload. +/// let revert = IConditionalOrder::OrderNotValid { +/// reason: "expired".into(), +/// } +/// .abi_encode(); +/// let host_data = format!("\"0x{}\"", alloy_primitives::hex::encode(&revert)); +/// +/// assert!(matches!( +/// decode_revert_hex(&host_data), +/// Some(PollOutcome::DontTryAgain), +/// )); +/// ``` +pub fn decode_revert_hex(s: &str) -> Option { + let stripped = s.trim_matches('"'); + let stripped = stripped.strip_prefix("0x").unwrap_or(stripped); + let bytes = alloy_primitives::hex::decode(stripped).ok()?; + decode_revert(&bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::{U256, address, hex}; + use alloy_sol_types::SolError; + + use crate::cow::composable::IConditionalOrder; + + #[test] + fn eth_call_params_shape() { + let to = address!("fdaFc9d1902f4e0b84f65F49f244b32b31013b74"); + let data = hex!("aabbcc").to_vec(); + let p = eth_call_params(&to, &data); + let parsed: serde_json::Value = serde_json::from_str(&p).unwrap(); + assert_eq!( + parsed[0]["to"], + "0xfdafc9d1902f4e0b84f65f49f244b32b31013b74" + ); + assert_eq!(parsed[0]["data"], "0xaabbcc"); + assert_eq!(parsed[1], "latest"); + } + + #[test] + fn parse_eth_call_result_decodes_hex_string() { + assert_eq!( + parse_eth_call_result(r#""0xdeadbeef""#), + Some(vec![0xde, 0xad, 0xbe, 0xef]), + ); + } + + #[test] + fn parse_eth_call_result_handles_empty_hex() { + assert_eq!(parse_eth_call_result(r#""0x""#), Some(vec![])); + } + + #[test] + fn parse_eth_call_result_rejects_non_json() { + assert_eq!(parse_eth_call_result("garbage"), None); + } + + #[test] + fn decode_revert_hex_strips_prefix_and_quotes() { + let err = IConditionalOrder::PollTryAtBlock { + blockNumber: U256::from(42_u64), + reason: "x".to_string(), + }; + let payload = alloy_primitives::hex::encode_prefixed(err.abi_encode()); + let quoted = format!("\"{payload}\""); + assert!(matches!( + decode_revert_hex("ed), + Some(PollOutcome::TryOnBlock(42)) + )); + } + + #[test] + fn decode_revert_hex_handles_unprefixed_naked_hex() { + let err = IConditionalOrder::PollTryNextBlock { + reason: "noop".to_string(), + }; + let payload = alloy_primitives::hex::encode(err.abi_encode()); + assert!(matches!( + decode_revert_hex(&payload), + Some(PollOutcome::TryNextBlock) + )); + } +} diff --git a/crates/shepherd-sdk/src/chain/mod.rs b/crates/shepherd-sdk/src/chain/mod.rs new file mode 100644 index 0000000..edd9bd2 --- /dev/null +++ b/crates/shepherd-sdk/src/chain/mod.rs @@ -0,0 +1,10 @@ +//! `chain::request` JSON plumbing. +//! +//! Build the `[{to, data}, "latest"]` params array for `eth_call`, +//! parse the `"0x..."` hex result string, decode revert payloads from +//! the host's structured error data. Pure-logic helpers so a module +//! can plumb its own `chain::request` shim around them. + +pub mod eth_call; + +pub use eth_call::{decode_revert_hex, eth_call_params, parse_eth_call_result}; diff --git a/crates/shepherd-sdk/src/cow/composable.rs b/crates/shepherd-sdk/src/cow/composable.rs new file mode 100644 index 0000000..686a1fb --- /dev/null +++ b/crates/shepherd-sdk/src/cow/composable.rs @@ -0,0 +1,189 @@ +//! ComposableCoW poll-revert decoding. +//! +//! `ComposableCoW.getTradeableOrderWithSignature` reverts with one of +//! five custom errors when the conditional order is not ready, expired, +//! or otherwise non-tradeable. This module mirrors that error surface +//! and maps each revert to the typed [`PollOutcome`] every TWAP / +//! strategy module dispatches on. +//! +//! Source for the Solidity errors: +//! `cowprotocol/composable-cow/src/interfaces/IConditionalOrder.sol`. + +use alloy_primitives::{Bytes, U256}; +use alloy_sol_types::{SolError, sol}; +use cowprotocol::GPv2OrderData; + +sol! { + /// Five custom errors `IConditionalOrder.verify` reverts with. + /// Selector source for [`decode_revert`]. The wire shape mirrors + /// the Solidity definitions verbatim so the four-byte selectors + /// computed here match what the contract emits. + #[derive(Debug)] + interface IConditionalOrder { + /// `OrderNotValid(string)` - the order condition is permanently + /// not met. Watch towers drop. + error OrderNotValid(string reason); + /// `PollTryNextBlock(string)` - try again on the next block. + error PollTryNextBlock(string reason); + /// `PollTryAtBlock(uint256, string)` - try at or after the + /// given block number. + error PollTryAtBlock(uint256 blockNumber, string reason); + /// `PollTryAtEpoch(uint256, string)` - try at or after the + /// given Unix timestamp (seconds). + error PollTryAtEpoch(uint256 timestamp, string reason); + /// `PollNever(string)` - the conditional order is dead. + error PollNever(string reason); + } +} + +/// Outcome of a single watch poll. Mirrors the BLEU-827 enum shape: +/// `Ready` carries the materials the submit path needs; the other +/// variants drive the lifecycle handler (BLEU-830). +/// +/// `Ready` is intentionally never produced by [`decode_revert`] - it +/// only comes from the successful return path the poll module +/// constructs at the call site. +#[derive(Debug)] +pub enum PollOutcome { + /// Conditional order is tradeable now; submit `order` with the + /// embedded EIP-1271 `signature` blob. `GPv2OrderData` is boxed + /// to keep the enum cache-friendly (~300 bytes vs. ~8 for the + /// other variants). + Ready { + /// The 12-field order ready to submit. + order: Box, + /// EIP-1271 wire-form signature (raw verifier bytes; the + /// orderbook prepends `from` before settlement). + signature: Bytes, + }, + /// Retry on the very next block - typical for time-sliced TWAP + /// schedules and other handlers that re-check on every tick. + TryNextBlock, + /// Retry once block number reaches the embedded value. + TryOnBlock(u64), + /// Retry once the wall clock (Unix seconds, UTC) reaches the + /// embedded value. + TryAtEpoch(u64), + /// Order is dead - drop the watch. Aggregates `OrderNotValid` and + /// `PollNever` reverts; the original reason string is dropped + /// because the lifecycle handler does not key off it today. + DontTryAgain, +} + +/// Decode a `getTradeableOrderWithSignature` revert payload into a +/// [`PollOutcome`]. +/// +/// Returns `None` when the selector is not one of the five +/// [`IConditionalOrder`] errors - including a bare `Error(string)` +/// require-revert. Callers should treat that as `TryNextBlock` (the +/// safe default) so a transient RPC blip does not drop a still-valid +/// watch. +pub fn decode_revert(data: &[u8]) -> Option { + if data.len() < 4 { + return None; + } + let selector: [u8; 4] = data[..4].try_into().ok()?; + let body = &data[4..]; + match selector { + s if s == IConditionalOrder::OrderNotValid::SELECTOR => Some(PollOutcome::DontTryAgain), + s if s == IConditionalOrder::PollTryNextBlock::SELECTOR => Some(PollOutcome::TryNextBlock), + s if s == IConditionalOrder::PollTryAtBlock::SELECTOR => { + let decoded = IConditionalOrder::PollTryAtBlock::abi_decode_raw(body).ok()?; + Some(PollOutcome::TryOnBlock(u256_to_u64_saturating( + decoded.blockNumber, + ))) + } + s if s == IConditionalOrder::PollTryAtEpoch::SELECTOR => { + let decoded = IConditionalOrder::PollTryAtEpoch::abi_decode_raw(body).ok()?; + Some(PollOutcome::TryAtEpoch(u256_to_u64_saturating( + decoded.timestamp, + ))) + } + s if s == IConditionalOrder::PollNever::SELECTOR => Some(PollOutcome::DontTryAgain), + _ => None, + } +} + +fn u256_to_u64_saturating(v: U256) -> u64 { + u64::try_from(v).unwrap_or(u64::MAX) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn order_not_valid_maps_to_drop() { + let err = IConditionalOrder::OrderNotValid { + reason: "expired".to_string(), + }; + assert!(matches!( + decode_revert(&err.abi_encode()), + Some(PollOutcome::DontTryAgain) + )); + } + + #[test] + fn poll_never_maps_to_drop() { + let err = IConditionalOrder::PollNever { + reason: "cancelled".to_string(), + }; + assert!(matches!( + decode_revert(&err.abi_encode()), + Some(PollOutcome::DontTryAgain) + )); + } + + #[test] + fn try_next_block() { + let err = IConditionalOrder::PollTryNextBlock { + reason: "noop".to_string(), + }; + assert!(matches!( + decode_revert(&err.abi_encode()), + Some(PollOutcome::TryNextBlock) + )); + } + + #[test] + fn try_at_block_carries_number() { + let err = IConditionalOrder::PollTryAtBlock { + blockNumber: U256::from(12_345_678_u64), + reason: "wait".to_string(), + }; + assert!(matches!( + decode_revert(&err.abi_encode()), + Some(PollOutcome::TryOnBlock(12_345_678)) + )); + } + + #[test] + fn try_at_epoch_carries_timestamp() { + let err = IConditionalOrder::PollTryAtEpoch { + timestamp: U256::from(1_700_000_000_u64), + reason: "soon".to_string(), + }; + assert!(matches!( + decode_revert(&err.abi_encode()), + Some(PollOutcome::TryAtEpoch(1_700_000_000)) + )); + } + + #[test] + fn unknown_selector_returns_none() { + let mut data = vec![0xde, 0xad, 0xbe, 0xef]; + data.extend_from_slice(&[0u8; 32]); + assert!(decode_revert(&data).is_none()); + } + + #[test] + fn truncated_returns_none() { + assert!(decode_revert(&[0x01, 0x02]).is_none()); + } + + #[test] + fn u256_saturates_at_max() { + assert_eq!(u256_to_u64_saturating(U256::MAX), u64::MAX); + assert_eq!(u256_to_u64_saturating(U256::from(42_u64)), 42); + } +} diff --git a/crates/shepherd-sdk/src/cow/error.rs b/crates/shepherd-sdk/src/cow/error.rs new file mode 100644 index 0000000..12c45e9 --- /dev/null +++ b/crates/shepherd-sdk/src/cow/error.rs @@ -0,0 +1,163 @@ +//! Orderbook submission error classification. +//! +//! Maps `cow_api::submit_order` failures into a typed [`RetryAction`] +//! the lifecycle layer dispatches on. The orderbook returns a typed +//! [`ApiError`] JSON body on permanent / transient failures; the host +//! forwards that JSON in `host-error.data` (once the chain backend +//! supports it - see the ADR follow-up). Until then, +//! [`classify_api_error`] falls back to `TryNextBlock` so a flaky +//! orderbook does not poison still-valid orders. +//! +//! [`ApiError`]: cowprotocol::error::ApiError + +use cowprotocol::error::ApiError; + +/// What the lifecycle layer should do after a failed submission. +/// +/// Mirrors the BLEU-829 retry contract: `TryNextBlock` / +/// `BackoffSeconds(s)` / `Drop`. The `Backoff` arm has no producer +/// today because cowprotocol's `retry_hint()` is bool-only; the +/// variant is kept so dispatch can grow into it once a server +/// `Retry-After` hint shows up. +#[derive(Debug, Eq, PartialEq)] +pub enum RetryAction { + /// Leave the watch / placement in place; the next event will + /// re-attempt. + TryNextBlock, + /// Persist `next_attempt = now + seconds`. Reserved - no producer + /// today (kept so the dispatch contract is stable). + #[allow(dead_code)] + Backoff { + /// Seconds to wait before retrying. + seconds: u64, + }, + /// Remove the watch / mark as terminally rejected. The orderbook + /// will not accept this body on a retry. + Drop, +} + +/// Best-effort decode of the orderbook's typed [`ApiError`] body from +/// the `host-error.data` field a guest receives on a failed +/// `cow_api::submit_order` call. Returns `None` when the host did not +/// forward a payload, or when the payload does not parse as +/// `ApiError`. +pub fn try_decode_api_error(host_error_data: Option<&str>) -> Option { + serde_json::from_str::(host_error_data?).ok() +} + +/// Classify the host's failure-side payload (the JSON the orderbook +/// returned) into a [`RetryAction`]. +/// +/// - Retriable kinds per `OrderPostErrorKind::is_retriable` (today: +/// `InsufficientFee`, `TooManyLimitOrders`, `PriceExceedsMarketPrice`) +/// → `TryNextBlock`. +/// - Recognised non-retriable kinds → `Drop`. +/// - Payload absent or unparseable → `TryNextBlock` (safe default; a +/// flaky orderbook should not be treated as a permanent rejection). +/// +/// # Example +/// +/// ``` +/// use shepherd_sdk::cow::{classify_api_error, RetryAction}; +/// +/// // Transient: orderbook rejects with InsufficientFee -> retry next block. +/// let transient = serde_json::json!({ +/// "errorType": "InsufficientFee", +/// "description": "fee too low", +/// }) +/// .to_string(); +/// assert_eq!(classify_api_error(Some(&transient)), RetryAction::TryNextBlock); +/// +/// // Permanent: InvalidSignature -> drop the watch / placement. +/// let permanent = serde_json::json!({ +/// "errorType": "InvalidSignature", +/// "description": "bad sig", +/// }) +/// .to_string(); +/// assert_eq!(classify_api_error(Some(&permanent)), RetryAction::Drop); +/// +/// // No payload (e.g. host-error.data is None) -> safe default. +/// assert_eq!(classify_api_error(None), RetryAction::TryNextBlock); +/// ``` +pub fn classify_api_error(host_error_data: Option<&str>) -> RetryAction { + match try_decode_api_error(host_error_data) { + Some(api) if api.retry_hint() => RetryAction::TryNextBlock, + Some(_) => RetryAction::Drop, + None => RetryAction::TryNextBlock, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn body_for(error_type: &str) -> String { + serde_json::json!({ + "errorType": error_type, + "description": "test", + }) + .to_string() + } + + #[test] + fn retriable_kinds_yield_try_next_block() { + for kind in [ + "InsufficientFee", + "TooManyLimitOrders", + "PriceExceedsMarketPrice", + ] { + assert_eq!( + classify_api_error(Some(&body_for(kind))), + RetryAction::TryNextBlock, + "{kind}", + ); + } + } + + #[test] + fn permanent_kinds_yield_drop() { + for kind in [ + "InvalidSignature", + "WrongOwner", + "DuplicateOrder", + "UnsupportedToken", + "InvalidAppData", + "InvalidErc1271Signature", + ] { + assert_eq!( + classify_api_error(Some(&body_for(kind))), + RetryAction::Drop, + "{kind}", + ); + } + } + + #[test] + fn unknown_kind_yields_drop() { + // `Unknown(_)` is non-retriable per cowprotocol's classifier. + assert_eq!( + classify_api_error(Some(&body_for("NewlyMintedErrorType"))), + RetryAction::Drop, + ); + } + + #[test] + fn missing_data_yields_try_next_block() { + assert_eq!(classify_api_error(None), RetryAction::TryNextBlock); + } + + #[test] + fn malformed_data_yields_try_next_block() { + assert_eq!( + classify_api_error(Some("upstream")), + RetryAction::TryNextBlock, + ); + } + + #[test] + fn try_decode_round_trips() { + let body = body_for("InsufficientFee"); + let api = try_decode_api_error(Some(&body)).expect("decode"); + assert_eq!(api.error_type, "InsufficientFee"); + } +} diff --git a/crates/shepherd-sdk/src/cow/mod.rs b/crates/shepherd-sdk/src/cow/mod.rs new file mode 100644 index 0000000..dd80f96 --- /dev/null +++ b/crates/shepherd-sdk/src/cow/mod.rs @@ -0,0 +1,19 @@ +//! CoW Protocol bridging. +//! +//! Type conversions and ABI decoding helpers that translate between +//! the on-chain shape (`GPv2OrderData`, `IConditionalOrder` reverts, +//! orderbook JSON) and the typed Rust surface (`OrderData`, +//! `PollOutcome`, `RetryAction`). +//! +//! Each submodule stays purely host-neutral: helpers take primitive +//! arguments (`&[u8]`, `Option<&str>`, slices) so they can be unit- +//! tested without wit-bindgen scaffolding and re-used unchanged by +//! TWAP, EthFlow, and future strategy modules. + +pub mod composable; +pub mod error; +pub mod order; + +pub use composable::{IConditionalOrder, PollOutcome, decode_revert}; +pub use error::{RetryAction, classify_api_error, try_decode_api_error}; +pub use order::gpv2_to_order_data; diff --git a/crates/shepherd-sdk/src/cow/order.rs b/crates/shepherd-sdk/src/cow/order.rs new file mode 100644 index 0000000..177f7fb --- /dev/null +++ b/crates/shepherd-sdk/src/cow/order.rs @@ -0,0 +1,139 @@ +//! `GPv2OrderData` -> `OrderData` bridging. +//! +//! ComposableCoW and CoWSwapEthFlow both emit / return the 12-field +//! `GPv2OrderData` Solidity tuple, with `kind` / `sellTokenBalance` / +//! `buyTokenBalance` as 32-byte keccak markers. The orderbook signs +//! against the typed `OrderData` shape, with those markers projected +//! into Rust enums. [`gpv2_to_order_data`] is the bridge. + +use alloy_primitives::Address; +use cowprotocol::{BuyTokenDestination, GPv2OrderData, OrderData, OrderKind, SellTokenSource}; + +/// Convert a freshly-polled / freshly-placed [`GPv2OrderData`] into the +/// typed [`OrderData`] shape `OrderCreation::from_signed_order_data` +/// expects. +/// +/// The `kind`, `sellTokenBalance`, and `buyTokenBalance` fields ride +/// the wire as `bytes32` markers (the `keccak256` of the lowercase +/// variant name). This helper hands them off to cowprotocol's +/// `from_contract_bytes` classifiers and returns `None` when the on- +/// chain payload carries a marker the SDK doesn't recognise - the +/// caller skips the order rather than ship a malformed body. +/// +/// `receiver = Address::ZERO` is normalised to `None`; `OrderCreation:: +/// from_signed_order_data` does the same downstream, but doing it here +/// keeps the EIP-712 hash inputs verbatim if a caller bypasses that +/// helper later. +/// +/// # Example +/// +/// ``` +/// use cowprotocol::{ +/// BuyTokenDestination, GPv2OrderData, OrderKind, SellTokenSource, +/// }; +/// use shepherd_sdk::cow::gpv2_to_order_data; +/// use shepherd_sdk::prelude::{Address, U256}; +/// +/// let gpv2 = GPv2OrderData { +/// sellToken: Address::repeat_byte(1), +/// buyToken: Address::repeat_byte(2), +/// receiver: Address::ZERO, // normalised to None +/// sellAmount: U256::from(1_000u64), +/// buyAmount: U256::from(999u64), +/// validTo: u32::MAX, +/// appData: cowprotocol::EMPTY_APP_DATA_HASH, +/// feeAmount: U256::ZERO, +/// kind: OrderKind::SELL, +/// partiallyFillable: false, +/// sellTokenBalance: SellTokenSource::ERC20, +/// buyTokenBalance: BuyTokenDestination::ERC20, +/// }; +/// +/// let order = gpv2_to_order_data(&gpv2).expect("known markers"); +/// assert_eq!(order.sell_amount, U256::from(1_000u64)); +/// assert_eq!(order.receiver, None); +/// ``` +pub fn gpv2_to_order_data(gpv2: &GPv2OrderData) -> Option { + Some(OrderData { + sell_token: gpv2.sellToken, + buy_token: gpv2.buyToken, + receiver: (gpv2.receiver != Address::ZERO).then_some(gpv2.receiver), + sell_amount: gpv2.sellAmount, + buy_amount: gpv2.buyAmount, + valid_to: gpv2.validTo, + app_data: gpv2.appData, + fee_amount: gpv2.feeAmount, + kind: OrderKind::from_contract_bytes(gpv2.kind)?, + partially_fillable: gpv2.partiallyFillable, + sell_token_balance: SellTokenSource::from_contract_bytes(gpv2.sellTokenBalance)?, + buy_token_balance: BuyTokenDestination::from_contract_bytes(gpv2.buyTokenBalance)?, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::{B256, U256, address}; + + fn submittable_gpv2() -> GPv2OrderData { + GPv2OrderData { + sellToken: address!("6810e776880C02933D47DB1b9fc05908e5386b96"), + buyToken: address!("DAE5F1590db13E3B40423B5b5c5fbf175515910b"), + receiver: address!("DeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"), + sellAmount: U256::from(1_000_000_u64), + buyAmount: U256::from(999_u64), + validTo: 0xffff_ffff, + appData: cowprotocol::EMPTY_APP_DATA_HASH, + feeAmount: U256::ZERO, + kind: OrderKind::SELL, + partiallyFillable: false, + sellTokenBalance: SellTokenSource::ERC20, + buyTokenBalance: BuyTokenDestination::ERC20, + } + } + + #[test] + fn happy_path_round_trips_markers() { + let g = submittable_gpv2(); + let od = gpv2_to_order_data(&g).expect("known markers"); + assert_eq!(od.sell_token, g.sellToken); + assert_eq!(od.buy_token, g.buyToken); + assert_eq!(od.kind, OrderKind::Sell); + assert_eq!(od.sell_token_balance, SellTokenSource::Erc20); + assert_eq!(od.buy_token_balance, BuyTokenDestination::Erc20); + } + + #[test] + fn zero_receiver_normalises_to_none() { + let mut g = submittable_gpv2(); + g.receiver = Address::ZERO; + assert_eq!(gpv2_to_order_data(&g).unwrap().receiver, None); + } + + #[test] + fn non_zero_receiver_preserved() { + let g = submittable_gpv2(); + assert_eq!(gpv2_to_order_data(&g).unwrap().receiver, Some(g.receiver)); + } + + #[test] + fn unknown_kind_marker_returns_none() { + let mut g = submittable_gpv2(); + g.kind = B256::repeat_byte(0x42); + assert!(gpv2_to_order_data(&g).is_none()); + } + + #[test] + fn unknown_sell_token_balance_returns_none() { + let mut g = submittable_gpv2(); + g.sellTokenBalance = B256::repeat_byte(0x99); + assert!(gpv2_to_order_data(&g).is_none()); + } + + #[test] + fn unknown_buy_token_balance_returns_none() { + let mut g = submittable_gpv2(); + g.buyTokenBalance = B256::repeat_byte(0x55); + assert!(gpv2_to_order_data(&g).is_none()); + } +} diff --git a/crates/shepherd-sdk/src/host.rs b/crates/shepherd-sdk/src/host.rs new file mode 100644 index 0000000..7eacbfb --- /dev/null +++ b/crates/shepherd-sdk/src/host.rs @@ -0,0 +1,177 @@ +//! Host traits - the seam between strategy logic and the wit-bindgen +//! shims a module generates per-cdylib. +//! +//! Each trait mirrors one nexum / shepherd host interface +//! ([`ChainHost`] for `nexum:host/chain`, [`LocalStoreHost`] for +//! `nexum:host/local-store`, [`CowApiHost`] for `shepherd:cow/cow-api`, +//! [`LoggingHost`] for `nexum:host/logging`). A module that wants +//! host-free unit tests writes its strategy logic against the +//! [`Host`] supertrait and lets `shepherd-sdk-test` slot in the +//! in-memory mocks. +//! +//! ## Why a separate `HostError` +//! +//! `wit_bindgen::generate!` emits a `HostError` struct into each +//! module's own crate, so its identity is per-module. The SDK +//! exposes [`HostError`] (this module) with the same field shape - +//! modules wire a one-liner `From` impl between the two so the +//! traits stay world-neutral and the mocks compile without a wasm +//! toolchain. See `shepherd-sdk-test`'s README for the adapter +//! pattern. + +/// Severity for log messages routed through [`LoggingHost::log`]. +/// Mirrors `nexum:host/logging.level`. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub enum LogLevel { + /// Verbose tracing for development. + Trace, + /// Detail useful to operators when investigating. + Debug, + /// Steady-state events. + Info, + /// Recoverable errors - operator notice but no immediate action. + Warn, + /// Unrecoverable errors - operator should investigate. + Error, +} + +/// Coarse categorisation of host failures, mirrored verbatim from +/// `nexum:host/types.host-error-kind` so a module's wit-bindgen +/// `HostErrorKind` can convert one-to-one. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub enum HostErrorKind { + /// Capability declared but not provisioned by the operator. + Unsupported, + /// Capability temporarily unavailable (RPC down, etc). + Unavailable, + /// Capability declined the request (auth, allowlist, …). + Denied, + /// Rate-limited by an upstream service. + RateLimited, + /// Operation took too long. + Timeout, + /// Caller-supplied input did not parse / validate. + InvalidInput, + /// Catch-all for host-side bugs. + Internal, +} + +/// SDK-side counterpart to wit-bindgen's `HostError`. Same field shape +/// so a module bridges between the two with a trivial `From` impl on +/// each side. +#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)] +#[error("{domain}: {message} (code={code}, kind={kind:?})")] +pub struct HostError { + /// Short subsystem identifier (`"chain"`, `"local-store"`, + /// `"cow-api"`, `"logging"`). + pub domain: String, + /// See [`HostErrorKind`]. + pub kind: HostErrorKind, + /// Domain-specific numeric (HTTP status, JSON-RPC code, etc). + pub code: i32, + /// Human-readable detail. + pub message: String, + /// Optional opaque payload (often JSON-encoded). + pub data: Option, +} + +impl HostError { + /// Convenience constructor for unsupported / not-yet-implemented + /// host endpoints. Useful in tests and mock setups. + pub fn unsupported(domain: impl Into, message: impl Into) -> Self { + Self { + domain: domain.into(), + kind: HostErrorKind::Unsupported, + code: 501, + message: message.into(), + data: None, + } + } +} + +/// `nexum:host/chain` - raw JSON-RPC dispatch. +pub trait ChainHost { + /// Execute a JSON-RPC request against the given chain. The host + /// routes to its configured provider; the SDK does not care which + /// transport (HTTP / WebSocket / mock) implements the call. + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result; +} + +/// `nexum:host/local-store` - per-module key-value persistence. +pub trait LocalStoreHost { + /// Fetch a value. `Ok(None)` when the key is absent. + fn get(&self, key: &str) -> Result>, HostError>; + /// Insert or overwrite. + fn set(&self, key: &str, value: &[u8]) -> Result<(), HostError>; + /// Delete. No-op if the key is absent. + fn delete(&self, key: &str) -> Result<(), HostError>; + /// Enumerate keys whose raw form starts with `prefix`. + fn list_keys(&self, prefix: &str) -> Result, HostError>; +} + +/// `shepherd:cow/cow-api` - orderbook submission path. +pub trait CowApiHost { + /// Submit an `OrderCreation` JSON body. The host returns the + /// canonical order UID on success. + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result; +} + +/// `nexum:host/logging` - structured runtime logs. +pub trait LoggingHost { + /// Emit a log line at the given level. + fn log(&self, level: LogLevel, message: &str); +} + +/// Supertrait that bundles the four host interfaces a typical strategy +/// module exercises. Modules that want full host-free integration +/// tests take `&impl Host` (or a generic ``) in their +/// strategy function; `shepherd-sdk-test::MockHost` is the in-memory +/// implementation. +/// +/// A blanket impl is provided for any type that implements all four +/// component traits, so callers do not have to add a redundant +/// `impl Host for MyHost {}`. +/// +/// # Example +/// +/// Strategy functions are generic over [`Host`]. Production code plugs +/// the per-module `WitBindgenHost` adapter (see `modules/examples/`); +/// unit tests plug `shepherd_sdk_test::MockHost`. +/// +/// ``` +/// use shepherd_sdk::host::{ +/// ChainHost, CowApiHost, Host, HostError, LocalStoreHost, LogLevel, LoggingHost, +/// }; +/// +/// /// Pure strategy logic - no wit-bindgen calls in here. +/// fn record_block(host: &H, chain_id: u64, key: &str) -> Result<(), HostError> { +/// host.log(LogLevel::Info, "recording block"); +/// host.set(key, b"")?; +/// let _block_number = host.request(chain_id, "eth_blockNumber", "[]")?; +/// Ok(()) +/// } +/// +/// // Minimal hand-rolled host so the doctest is self-contained. +/// // Real modules wire `shepherd_sdk_test::MockHost` here. +/// # struct StubHost; +/// # impl ChainHost for StubHost { +/// # fn request(&self, _: u64, _: &str, _: &str) -> Result { +/// # Ok("\"0x0\"".into()) +/// # } +/// # } +/// # impl LocalStoreHost for StubHost { +/// # fn get(&self, _: &str) -> Result>, HostError> { Ok(None) } +/// # fn set(&self, _: &str, _: &[u8]) -> Result<(), HostError> { Ok(()) } +/// # fn delete(&self, _: &str) -> Result<(), HostError> { Ok(()) } +/// # fn list_keys(&self, _: &str) -> Result, HostError> { Ok(vec![]) } +/// # } +/// # impl CowApiHost for StubHost { +/// # fn submit_order(&self, _: u64, _: &[u8]) -> Result { Ok("".into()) } +/// # } +/// # impl LoggingHost for StubHost { +/// # fn log(&self, _: LogLevel, _: &str) {} +/// # } +/// record_block(&StubHost, 1, "block:42").unwrap(); +/// ``` +pub trait Host: ChainHost + LocalStoreHost + CowApiHost + LoggingHost {} +impl Host for T {} diff --git a/crates/shepherd-sdk/src/lib.rs b/crates/shepherd-sdk/src/lib.rs new file mode 100644 index 0000000..b09a407 --- /dev/null +++ b/crates/shepherd-sdk/src/lib.rs @@ -0,0 +1,108 @@ +//! # shepherd-sdk +//! +//! Guest-side SDK for Shepherd modules. The crate is the shared +//! companion to the per-module `wit_bindgen::generate!` invocation: +//! modules keep their own wit-bindgen call (which emits the world- +//! specific `Guest` trait, `HostError` shape, and host import shims +//! into the module's own crate) and pull helpers + canonical +//! primitive types from here. +//! +//! ## What lives here +//! +//! - [`prelude`] - `use shepherd_sdk::prelude::*` imports alloy +//! primitives ([`Address`], [`B256`], [`Bytes`], [`U256`], +//! [`keccak256`]) and cowprotocol's order / signing / orderbook +//! surface ([`OrderCreation`], [`OrderData`], [`OrderUid`], +//! [`OrderKind`], [`Signature`], [`Chain`], [`GPv2OrderData`], +//! [`EMPTY_APP_DATA_JSON`], [`ApiError`], [`OrderPostErrorKind`]). +//! +//! - [`cow`] - `GPv2OrderData` -> `OrderData` bridging +//! ([`gpv2_to_order_data`]), `IConditionalOrder` revert decoding +//! ([`PollOutcome`] + [`decode_revert`]), and the +//! [`RetryAction`] classifier driving submit-failure dispatch. +//! +//! - [`chain`] - `eth_call` JSON plumbing +//! ([`eth_call_params`], [`parse_eth_call_result`], +//! [`decode_revert_hex`]). +//! +//! - [`host`] - host trait seam ([`Host`] / [`ChainHost`] / +//! [`LocalStoreHost`] / [`CowApiHost`] / [`LoggingHost`]) plus a +//! host-neutral [`HostError`]. Modules that want host-free tests +//! structure their strategy logic against these traits and slot +//! in the `shepherd-sdk-test` mocks. See the host module docs for +//! the wit-bindgen adapter pattern. +//! +//! - `store` - placeholder for `WatchSet` / `BackoffLedger` +//! per ADR-0006. Populated when a second strategy module needs +//! the same key conventions. +//! +//! ## Why no `wit_bindgen::generate!` here +//! +//! The macro emits types into the calling crate (the module's +//! cdylib). Re-exporting wit-bindgen output from a library crate +//! would duplicate symbols and break the component-export contract. +//! Helpers in this SDK therefore take primitive types (`&[u8]`, +//! `Option<&str>`, slices) rather than the per-module `HostError` +//! struct; modules unpack their `HostError` on the way in. Trade-off +//! documented in ADR-0006 / ADR-0007 - the SDK stays on the guest +//! side, neutral to which world the module exports. +//! +//! [`Address`]: alloy_primitives::Address +//! [`B256`]: alloy_primitives::B256 +//! [`Bytes`]: alloy_primitives::Bytes +//! [`U256`]: alloy_primitives::U256 +//! [`keccak256`]: alloy_primitives::keccak256 +//! [`OrderCreation`]: cowprotocol::OrderCreation +//! [`OrderData`]: cowprotocol::OrderData +//! [`OrderUid`]: cowprotocol::OrderUid +//! [`OrderKind`]: cowprotocol::OrderKind +//! [`Signature`]: cowprotocol::Signature +//! [`Chain`]: cowprotocol::Chain +//! [`GPv2OrderData`]: cowprotocol::GPv2OrderData +//! [`EMPTY_APP_DATA_JSON`]: cowprotocol::EMPTY_APP_DATA_JSON +//! [`ApiError`]: cowprotocol::ApiError +//! [`OrderPostErrorKind`]: cowprotocol::error::OrderPostErrorKind +//! [`gpv2_to_order_data`]: cow::gpv2_to_order_data +//! [`PollOutcome`]: cow::PollOutcome +//! [`decode_revert`]: cow::decode_revert +//! [`RetryAction`]: cow::RetryAction +//! [`eth_call_params`]: chain::eth_call_params +//! [`parse_eth_call_result`]: chain::parse_eth_call_result +//! [`decode_revert_hex`]: chain::decode_revert_hex +//! [`Host`]: host::Host +//! [`ChainHost`]: host::ChainHost +//! [`LocalStoreHost`]: host::LocalStoreHost +//! [`CowApiHost`]: host::CowApiHost +//! [`LoggingHost`]: host::LoggingHost +//! [`HostError`]: host::HostError + +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![warn(missing_docs)] +#![cfg_attr(docsrs, feature(doc_cfg))] + +pub mod chain; +pub mod cow; +pub mod host; +pub mod prelude; + +#[cfg(test)] +mod tests { + //! The skeleton has no behaviour to exercise; this test just + //! locks the prelude's surface - the build itself proves the + //! re-exports compile against both `wasm32-wasip2` and the + //! host target. + + use crate::prelude::*; + + #[test] + fn prelude_re_exports_resolve() { + let _addr: Address = Address::ZERO; + let _hash: B256 = B256::ZERO; + let _amt: U256 = U256::ZERO; + let _empty: Bytes = Bytes::new(); + // cowprotocol re-exports + let _kind: OrderKind = OrderKind::Sell; + let _chain: Chain = Chain::Sepolia; + assert_eq!(EMPTY_APP_DATA_JSON, "{}"); + } +} diff --git a/crates/shepherd-sdk/src/prelude.rs b/crates/shepherd-sdk/src/prelude.rs new file mode 100644 index 0000000..004d66e --- /dev/null +++ b/crates/shepherd-sdk/src/prelude.rs @@ -0,0 +1,38 @@ +//! Bulk-imports the protocol primitives every Shepherd module uses on +//! every other line. `use shepherd_sdk::prelude::*` is a one-liner that +//! covers alloy address / hash / numeric types plus cowprotocol's +//! order, signing, and orderbook-error surface. +//! +//! The wit-bindgen-generated types (`Guest`, `HostError`, `Event`, …) +//! are **not** re-exported here because they live in each module's own +//! crate (one `wit_bindgen::generate!` call per cdylib). The prelude +//! covers only the host-neutral protocol layer that the SDK helpers +//! consume by value. + +pub use alloy_primitives::{Address, B256, Bytes, U256, address, b256, hex, keccak256}; + +pub use cowprotocol::{ + BuyTokenDestination, + // App-data + chain + domain identity. + Chain, + DomainSeparator, + EMPTY_APP_DATA_HASH, + EMPTY_APP_DATA_JSON, + // Settlement primitives carried in event payloads and order bodies. + GPv2OrderData, + // Orderbook submission body + the parts every assembly path touches. + OrderCreation, + OrderData, + OrderKind, + // Order identity. + OrderUid, + SellTokenSource, + // Signing. + Signature, + SigningScheme, +}; + +/// Re-exported `ApiError` typed error surface from the orderbook - +/// guest-side helpers (BLEU-840) read this back out of host-error JSON +/// to drive the `RetryAction` dispatch. +pub use cowprotocol::error::{ApiError, OrderPostErrorKind}; diff --git a/docs/00-overview.md b/docs/00-overview.md index a09de9b..2d6cc24 100755 --- a/docs/00-overview.md +++ b/docs/00-overview.md @@ -1,19 +1,19 @@ # Nexum: Universal WASM Component Model Runtime -Nexum is a WASM Component Model runtime that provides secure, sandboxed execution for WebAssembly modules. Modules react to blockchain events, read chain state, persist data locally and to decentralised storage, communicate via decentralised messaging — all within a capability-based sandbox with zero implicit permissions. +Nexum is a WASM Component Model runtime that provides secure, sandboxed execution for WebAssembly modules. Modules react to blockchain events, read chain state, persist data locally and to decentralised storage, communicate via decentralised messaging - all within a capability-based sandbox with zero implicit permissions. -**Shepherd** is the Nexum distribution that includes CoW Protocol extensions (`shepherd:cow` WIT package). A module compiled against the universal `nexum:host/event-module` world runs on any Nexum-compatible host. A module compiled against `shepherd:cow/shepherd` additionally gains access to CoW Protocol APIs and order submission — and requires a Shepherd host. +**Shepherd** is the Nexum distribution that includes CoW Protocol extensions (`shepherd:cow` WIT package). A module compiled against the universal `nexum:host/event-module` world runs on any Nexum-compatible host. A module compiled against `shepherd:cow/shepherd` additionally gains access to CoW Protocol APIs and order submission - and requires a Shepherd host. ### Vocabulary: engine vs. host (`nexum-engine` vs. `nexum:host`) -Two project names look similar but mean different things — keeping them straight is load-bearing for everything that follows: +Two project names look similar but mean different things - keeping them straight is load-bearing for everything that follows: | Term | What it is | Where you find it | |---|---|---| -| **engine** (`nexum-engine`) | A concrete *implementation* that loads and runs WASM components. The 0.2 reference engine is a wasmtime-based server daemon. Mobile / browser / embedded engines could exist later — each is a separate engine. | `crates/nexum-engine/`, the binary, `cargo run -p nexum-engine` | -| **host** (`nexum:host`) | The WIT *contract* — the set of host-imported interfaces (chain, identity, local-store, etc.), types, and worlds that every engine must implement and every module imports. The contract is one; engines are many. | `wit/nexum-host/`, `package nexum:host@0.2.0`, Rust path `nexum::host::*` | +| **engine** (`nexum-engine`) | A concrete *implementation* that loads and runs WASM components. The 0.2 reference engine is a wasmtime-based server daemon. Mobile / browser / embedded engines could exist later - each is a separate engine. | `crates/nexum-engine/`, the binary, `cargo run -p nexum-engine` | +| **host** (`nexum:host`) | The WIT *contract* - the set of host-imported interfaces (chain, identity, local-store, etc.), types, and worlds that every engine must implement and every module imports. The contract is one; engines are many. | `wit/nexum-host/`, `package nexum:host@0.2.0`, Rust path `nexum::host::*` | -The relationship: an engine *implements* `nexum:host` so that modules *built against* `nexum:host` can run on it. The `nexum:host` package itself does not run anything — it's a specification. When this doc says "the host", it means whichever engine the module currently runs on, as seen through the `nexum:host` contract. +The relationship: an engine *implements* `nexum:host` so that modules *built against* `nexum:host` can run on it. The `nexum:host` package itself does not run anything - it's a specification. When this doc says "the host", it means whichever engine the module currently runs on, as seen through the `nexum:host` contract. > **Upgrading from 0.1?** See the [Migration Guide](migration/0.1-to-0.2.md) for the full rename table (`web3:runtime` → `nexum:host`, `csn` → `chain`, `msg` → `messaging`, `headless-module` → `event-module`, etc.), the unified `host-error` model, and the manifest-driven capability negotiation introduced in 0.2. @@ -32,7 +32,7 @@ flowchart TB mc["Module C"] end - subgraph host["Host API — WIT Interfaces"] + subgraph host["Host API - WIT Interfaces"] uni["nexum:host\nchain · identity · local-store · remote-store · messaging · logging"] ext["shepherd:cow\ncow-api"] end @@ -58,11 +58,11 @@ flowchart TB ## Design Principles -- **Component Model from day 1** — WIT-defined API contract; structural sandboxing (no WASI, no FS, no network); multi-language guests. -- **Declarative subscriptions** — modules declare events in their manifest; the runtime wires sources. -- **Transactional state** — per-event all-or-nothing semantics; commit on success, rollback on trap. -- **Content-addressed distribution** — modules are fetched by hash (Swarm, IPFS, OCI, HTTPS); integrity always verified. -- **Self-hosted** — no centralised dependency; operator runs their own node. +- **Component Model from day 1** - WIT-defined API contract; structural sandboxing (no WASI, no FS, no network); multi-language guests. +- **Declarative subscriptions** - modules declare events in their manifest; the runtime wires sources. +- **Transactional state** - per-event all-or-nothing semantics; commit on success, rollback on trap. +- **Content-addressed distribution** - modules are fetched by hash (Swarm, IPFS, OCI, HTTPS); integrity always verified. +- **Self-hosted** - no centralised dependency; operator runs their own node. ## The Six Primitives @@ -79,20 +79,20 @@ Every module has access to six orthogonal capabilities through the `nexum:host` These primitives are orthogonal: -- **Chain** is the source of truth — the blockchain consensus state. Modules read chain state and (indirectly) write to it via order submission or transactions. -- **Identity** is cryptographic identity — key management and signing. The `chain` host implementation depends on `identity` internally: signing RPC methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) delegate to the identity backend. Modules can also import `identity` directly for raw signing operations. -- **Local Store** is the module's private scratchpad — fast, local, scoped to one module on one device. Does not replicate. -- **Remote Store** is shared persistent content — content-addressed, decentralised, survives independent of any device. Any module on any device can read what another module wrote. -- **Messaging** is real-time communication — ephemeral pub/sub messages between modules, devices, or users. Transient and topic-based. -- **Logging** is diagnostics — one-way output for debugging and monitoring. Not a data channel. +- **Chain** is the source of truth - the blockchain consensus state. Modules read chain state and (indirectly) write to it via order submission or transactions. +- **Identity** is cryptographic identity - key management and signing. The `chain` host implementation depends on `identity` internally: signing RPC methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) delegate to the identity backend. Modules can also import `identity` directly for raw signing operations. +- **Local Store** is the module's private scratchpad - fast, local, scoped to one module on one device. Does not replicate. +- **Remote Store** is shared persistent content - content-addressed, decentralised, survives independent of any device. Any module on any device can read what another module wrote. +- **Messaging** is real-time communication - ephemeral pub/sub messages between modules, devices, or users. Transient and topic-based. +- **Logging** is diagnostics - one-way output for debugging and monitoring. Not a data channel. ## Additive 0.2 Capabilities In addition to the six core primitives, the 0.2 WIT introduces three optional capabilities that modules can declare in their manifest: -- **`clock`** — wall-clock (`now-ms`, UTC milliseconds since Unix epoch) and monotonic (`monotonic-ns`) time, replacing the 0.1 workaround of reading `block.timestamp` inside `on_block`. -- **`random`** — a CSPRNG (`fill(len)`), since 0.1 modules had no source of secure randomness at all. -- **`http`** — an allowlisted outbound HTTP client (`fetch(request)`), gated by a `[capabilities.http].allow` domain list. The host MUST enforce the allowlist. This replaces the 0.1 anti-pattern of tunnelling notifications through Waku. +- **`clock`** - wall-clock (`now-ms`, UTC milliseconds since Unix epoch) and monotonic (`monotonic-ns`) time, replacing the 0.1 workaround of reading `block.timestamp` inside `on_block`. +- **`random`** - a CSPRNG (`fill(len)`), since 0.1 modules had no source of secure randomness at all. +- **`http`** - an allowlisted outbound HTTP client (`fetch(request)`), gated by a `[capabilities.http].allow` domain list. The host MUST enforce the allowlist. This replaces the 0.1 anti-pattern of tunnelling notifications through Waku. 0.2 also publishes (but does not yet host) the experimental **`query-module`** world for request/response modules (wallet rule evaluators, signature validators, pricing oracles). The WIT is stable enough to target with `MockHost` tests; production host support lands in 0.3. See the migration guide for the full WIT. @@ -102,12 +102,12 @@ The WIT is split into layered packages. The universal layer (`nexum:host`) provi ```mermaid graph TB - subgraph l3["Layer 3 — Domain Extensions"] + subgraph l3["Layer 3 - Domain Extensions"] cow["shepherd:cow\ncow-api"] other["future:domain\nvault · strategy · …"] end - subgraph l1["Layer 1 — Universal Runtime"] + subgraph l1["Layer 1 - Universal Runtime"] pkg["nexum:host"] ifaces["chain · identity · local-store · remote-store · messaging · logging"] exports["Exports: init · on-event"] @@ -118,19 +118,19 @@ graph TB ``` ``` -// Universal layer — any platform, any blockchain app +// Universal layer - any platform, any blockchain app package nexum:host@0.2.0 world event-module { - import chain — consensus access (JSON-RPC passthrough) - import identity — key management and message signing - import local-store — local key-value persistence - import remote-store — decentralised storage (Swarm) - import messaging — decentralised messaging (Waku) - import logging — log (trace/debug/info/warn/error) - - export init(config) — called once on load - export on_event(event)— called per subscribed event (block, logs, tick, message) + import chain - consensus access (JSON-RPC passthrough) + import identity - key management and message signing + import local-store - local key-value persistence + import remote-store - decentralised storage (Swarm) + import messaging - decentralised messaging (Waku) + import logging - log (trace/debug/info/warn/error) + + export init(config) - called once on load + export on_event(event) - called per subscribed event (block, logs, tick, message) } // CoW Protocol extension @@ -138,13 +138,13 @@ package shepherd:cow@0.2.0 world shepherd { include event-module - import cow-api — CoW Protocol REST API + order submission + import cow-api - CoW Protocol REST API + order submission } ``` -The `event-module` world imports **six** interfaces — chain, identity, local-store, remote-store, messaging, logging. The 0.1 WIT framing claimed six primitives but only actually imported five; 0.2 brings `identity` into the world definition so the contract matches the documentation. +The `event-module` world imports **six** interfaces - chain, identity, local-store, remote-store, messaging, logging. The 0.1 WIT framing claimed six primitives but only actually imported five; 0.2 brings `identity` into the world definition so the contract matches the documentation. -No WASI interfaces are imported. All I/O is mediated through host interfaces. The `chain` interface exposes a single generic `request` function (plus an additive `request-batch` in 0.2) — the SDK implements alloy's `Transport` trait on top of it, giving modules the full alloy `Provider` API (80+ methods) with zero WIT churn. +No WASI interfaces are imported. All I/O is mediated through host interfaces. The `chain` interface exposes a single generic `request` function (plus an additive `request-batch` in 0.2) - the SDK implements alloy's `Transport` trait on top of it, giving modules the full alloy `Provider` API (80+ methods) with zero WIT churn. > Design rationale: [07-rpc-namespace-design.md](07-rpc-namespace-design.md) | Platform generalisation: [08-platform-generalisation.md](08-platform-generalisation.md) @@ -156,15 +156,15 @@ No WASI interfaces are imported. All I/O is mediated through host interfaces. Th |---------|--------|---------| | Language | Rust | 1.90+ | | WASM runtime | wasmtime (Component Model) | 45.x | -| API contract | WIT (`nexum:host@0.2.0`, `shepherd:cow@0.2.0`) | — | +| API contract | WIT (`nexum:host@0.2.0`, `shepherd:cow@0.2.0`) | - | | Guest bindings | wit-bindgen | 0.57.x | -| Async | Tokio | — | +| Async | Tokio | - | | Ethereum RPC | alloy | 1.5.x | | Local store | redb | 3.1.x | -| Logging | tracing + tracing-subscriber | — | -| Metrics | metrics + metrics-exporter-prometheus | — | -| Deployment | Docker | — | -| License | AGPL-3.0 | — | +| Logging | tracing + tracing-subscriber | - | +| Metrics | metrics + metrics-exporter-prometheus | - | +| Deployment | Docker | - | +| License | AGPL-3.0 | - | ## Module Package @@ -198,7 +198,7 @@ cow_api_url = "https://api.cow.fi/arbitrum" slippage_bps = 50 # integers stay integers in 0.2 ``` -The manifest declares identity, resource caps, chain requirements, event subscriptions, capability grants, and typed module config — everything the runtime needs to load and run the module. In 0.2, `[capabilities]` is the canonical place to declare what host primitives a module needs; imports listed as `optional` install trap stubs that return `host-error { kind: unsupported }` on call rather than failing instantiation. Omitting `[capabilities]` falls back to "all imports required" with a deprecation warning. +The manifest declares identity, resource caps, chain requirements, event subscriptions, capability grants, and typed module config - everything the runtime needs to load and run the module. In 0.2, `[capabilities]` is the canonical place to declare what host primitives a module needs; imports listed as `optional` install trap stubs that return `host-error { kind: unsupported }` on call rather than failing instantiation. Omitting `[capabilities]` falls back to "all imports required" with a deprecation warning. -> Full spec: [02-modules-events-packaging.md](02-modules-events-packaging.md) @@ -238,8 +238,8 @@ stateDiagram-v2 - **Load**: compile `Component`, validate WIT world, create `InstancePre`. - **Init**: create `Store`, instantiate, call `init(config)`. - **Run**: dispatch subscribed events to `on_event`. Each call gets a fuel budget. -- **Restart**: on crash — exponential backoff (1s -> 5min cap), fresh `Store`, state persists. -- **Dead**: after N consecutive failures (poison pill) — requires manual intervention. +- **Restart**: on crash - exponential backoff (1s -> 5min cap), fresh `Store`, state persists. +- **Dead**: after N consecutive failures (poison pill) - requires manual intervention. -> Full lifecycle: [02-modules-events-packaging.md](02-modules-events-packaging.md) @@ -248,7 +248,7 @@ stateDiagram-v2 - **Sources**: `block` (new heads via `eth_subscribe`), `log` (filtered contract events), `cron` (schedule-based), `message` (Waku content topics). - **Shared subscriptions**: one block subscription per chain, fanned out to all subscribed modules. - **Dispatch**: concurrent across modules, sequential within a module (ordered delivery). -- **Declared in manifest**: `[[subscription]]` blocks — the runtime wires sources, not the module. +- **Declared in manifest**: `[[subscription]]` blocks - the runtime wires sources, not the module. -> Full design: [02-modules-events-packaging.md](02-modules-events-packaging.md) @@ -256,7 +256,7 @@ stateDiagram-v2 - **Backend**: redb (pure Rust, ACID, MVCC, crash-safe). - **Isolation**: one database file per module; modules cannot access each other's state. -- **Transactions**: each `on_event` runs in an implicit write transaction — commit on success, rollback on failure. +- **Transactions**: each `on_event` runs in an implicit write transaction - commit on success, rollback on failure. - **Survives restarts**: state is external to WASM instance. - **Size enforcement**: `max_state_bytes` from manifest, enforced host-side. - **Prefix scanning**: `list-keys(prefix)` for namespaced key organisation. @@ -269,23 +269,23 @@ The SDK mirrors the WIT layering: `nexum-sdk` (universal) and `shepherd-sdk` (Co | Crate | Provides | |-------|----------| -| `nexum-sdk` | `provider(chain_id)` — full alloy `Provider` backed by host RPC via `HostTransport` | -| | `Signer` — signing client (get accounts, sign messages, sign EIP-712 typed data) | -| | `TypedState` — serde-based typed local state (postcard serialisation) | -| | `RemoteStore` — typed decentralised storage client (upload, download, feeds) | -| | `Messaging` — typed messaging client (publish, query) | -| | `abi::sol!` — compile-time Ethereum ABI codec (alloy-sol-types) | -| | `log::{info!, …}` — formatted logging macros | -| | `HostError` / `HostErrorKind` — unified host error type with `?` support | -| | `#[nexum::module]` — proc macro for universal modules | -| `shepherd-sdk` | `Cow` — typed CoW Protocol API client backed by host `cow-api` interface | -| | `#[shepherd::module]` — proc macro for CoW modules (extends `#[nexum::module]`) | -| | `prelude::*` — all types, interfaces, helpers in one import | -| Both | `testing::MockHost` — native-Rust unit tests with mock host | -| | `testing::WasmTestHarness` — integration tests in real wasmtime | -| | `cargo nexum` — CLI: new / build / package / publish / check / migrate | - -Multi-language support: module authors can use Rust, C/C++, Go, JavaScript, or Python — all compile to valid components against the same WIT world. +| `nexum-sdk` | `provider(chain_id)` - full alloy `Provider` backed by host RPC via `HostTransport` | +| | `Signer` - signing client (get accounts, sign messages, sign EIP-712 typed data) | +| | `TypedState` - serde-based typed local state (postcard serialisation) | +| | `RemoteStore` - typed decentralised storage client (upload, download, feeds) | +| | `Messaging` - typed messaging client (publish, query) | +| | `abi::sol!` - compile-time Ethereum ABI codec (alloy-sol-types) | +| | `log::{info!, …}` - formatted logging macros | +| | `HostError` / `HostErrorKind` - unified host error type with `?` support | +| | `#[nexum::module]` - proc macro for universal modules | +| `shepherd-sdk` | `Cow` - typed CoW Protocol API client backed by host `cow-api` interface | +| | `#[shepherd::module]` - proc macro for CoW modules (extends `#[nexum::module]`) | +| | `prelude::*` - all types, interfaces, helpers in one import | +| Both | `testing::MockHost` - native-Rust unit tests with mock host | +| | `testing::WasmTestHarness` - integration tests in real wasmtime | +| | `cargo nexum` - CLI: new / build / package / publish / check / migrate | + +Multi-language support: module authors can use Rust, C/C++, Go, JavaScript, or Python - all compile to valid components against the same WIT world. -> Full design: [05-sdk-design.md](05-sdk-design.md) @@ -322,16 +322,16 @@ Metrics cover three groups: runtime-level (modules loaded/dead), per-module (eve ## Platform Generalisation -Nexum is **designed** to be portable to mobile and browser hosts: the WIT contract is the universal interface and any host that implements it can run modules unchanged. The **0.2 reference runtime ships server-only** — a Rust/Tokio/wasmtime binary. The mobile, WebView, and super-app targets remain on the roadmap and live in the docs as architectural direction, not shipping artifacts. +Nexum is **designed** to be portable to mobile and browser hosts: the WIT contract is the universal interface and any host that implements it can run modules unchanged. The **0.2 reference runtime ships server-only** - a Rust/Tokio/wasmtime binary. The mobile, WebView, and super-app targets remain on the roadmap and live in the docs as architectural direction, not shipping artifacts. | Platform | WASM Engine | Local Store | RPC Backend | Status | |----------|-------------|-------------|-------------|--------| | **Server** (reference) | wasmtime | redb | alloy provider | **Shipping in 0.2** | -| **Mobile** (Flutter/Dart) | wasmtime C API / wasm3 | SQLite | HTTP client | Planned — see roadmap | -| **WebView** | Browser engine + `jco` | IndexedDB | JS bridge / wallet | Planned — see roadmap | -| **Super app** | All of the above | SQLite | HTTP + wallet | Planned — see roadmap | +| **Mobile** (Flutter/Dart) | wasmtime C API / wasm3 | SQLite | HTTP client | Planned - see roadmap | +| **WebView** | Browser engine + `jco` | IndexedDB | JS bridge / wallet | Planned - see roadmap | +| **Super app** | All of the above | SQLite | HTTP + wallet | Planned - see roadmap | -The mobile/wallet host story — including the experimental `query-module` world's production support, the C ABI for non-Rust embedders, and the `nexum-host` embedder facade — is on the 0.3 roadmap, conditional on a named design partner. +The mobile/wallet host story - including the experimental `query-module` world's production support, the C ABI for non-Rust embedders, and the `nexum-host` embedder facade - is on the 0.3 roadmap, conditional on a named design partner. -> Full design (and the design rationale for each target): [08-platform-generalisation.md](08-platform-generalisation.md) diff --git a/docs/01-runtime-environment.md b/docs/01-runtime-environment.md index 3f72ab0..261c58d 100755 --- a/docs/01-runtime-environment.md +++ b/docs/01-runtime-environment.md @@ -26,13 +26,13 @@ The Component Model is **production-viable in wasmtime 45** and gives us critical advantages over raw core modules: -1. **Structural sandboxing.** A component compiled against a WIT world with no filesystem import literally *cannot* access the filesystem — enforced at the type level, not just by omission of host functions. This is stronger than core module sandboxing where imports are stringly-typed. +1. **Structural sandboxing.** A component compiled against a WIT world with no filesystem import literally *cannot* access the filesystem - enforced at the type level, not just by omission of host functions. This is stronger than core module sandboxing where imports are stringly-typed. 2. **Type-safe API contract.** The WIT definition *is* the API spec. Both host and guest get generated bindings (`wasmtime::component::bindgen!` on the host, `wit_bindgen::generate!` on the guest). No manual ABI wrangling, no serialisation disagreements. 3. **Resource types.** Opaque handles with lifecycle management (constructors, methods, destructors via `ResourceTable`). Ideal for subscription handles, RPC connections, etc. -4. **Multi-language guests from day 1.** Module authors can use Rust, C/C++, Go, JavaScript (ComponentizeJS), or Python (componentize-py) — all producing valid components against the same WIT world. This dramatically lowers the barrier for community modules. +4. **Multi-language guests from day 1.** Module authors can use Rust, C/C++, Go, JavaScript (ComponentizeJS), or Python (componentize-py) - all producing valid components against the same WIT world. This dramatically lowers the barrier for community modules. 5. **No WASI required.** The Component Model and WASI are architecturally separate. We define a pure `nexum:host` world with exactly our host APIs. Zero WASI imports means zero implicit capabilities. @@ -47,9 +47,9 @@ The Component Model is **production-viable in wasmtime 45** and gives us critica | Aspect | Risk | |--------|------| -| `bindgen!` macro, custom worlds, resource types | Low — stable, well-documented | -| `wit-bindgen` guest bindings | Medium — API churn between versions | -| Component Model native async (streams/futures) | High — not needed yet, avoid for now | +| `bindgen!` macro, custom worlds, resource types | Low - stable, well-documented | +| `wit-bindgen` guest bindings | Medium - API churn between versions | +| Component Model native async (streams/futures) | High - not needed yet, avoid for now | ## Core Concepts @@ -210,7 +210,7 @@ interface chain { } /// Additive 0.2 method: batched JSON-RPC. The alloy-backed HostTransport - /// routes RequestPacket::Batch through this — `provider.multicall(...)` + /// routes RequestPacket::Batch through this - `provider.multicall(...)` /// actually batches on the wire in 0.2. Hosts that cannot batch natively /// MUST fall back to sequential `request` calls; the returned list is /// the same length as `requests` and in the same order. @@ -227,7 +227,7 @@ interface identity { /// Sign a message with `personal_sign` semantics. The host MUST prepend /// the EIP-191 prefix (`\x19Ethereum Signed Message:\n`) before /// hashing and signing. Hosts MUST NOT expose a raw-bytes signing path - /// through this function — a raw signer can be tricked into signing + /// through this function - a raw signer can be tricked into signing /// EIP-155 transactions or EIP-712 payloads disguised as plain bytes. /// /// Returns a 65-byte ECDSA secp256k1 signature (r || s || v). @@ -257,7 +257,7 @@ interface logging { /// The universal event-driven module world. Platform-agnostic: no CoW, /// no domain-specific imports. Suitable for any web3 automation. /// -/// In 0.2 this imports all six primitives — the identity import was +/// In 0.2 this imports all six primitives - the identity import was /// missing from the 0.1 WIT despite being part of the documented primitive /// taxonomy, and is now present. world event-module { @@ -276,7 +276,7 @@ world event-module { } ``` -In addition to the six core imports, 0.2 publishes three additive optional capabilities — `clock` (`now-ms` / `monotonic-ns`), `random` (CSPRNG `fill`), and `http` (allowlisted outbound HTTP) — which modules can declare in their `nexum.toml` `[capabilities]` section. The migration guide carries the full WIT for each. 0.2 also publishes the experimental **`query-module`** world for request/response modules; the WIT is stable but no host implementation ships in 0.2, so it's a target for `MockHost` testing only. +In addition to the six core imports, 0.2 publishes three additive optional capabilities - `clock` (`now-ms` / `monotonic-ns`), `random` (CSPRNG `fill`), and `http` (allowlisted outbound HTTP) - which modules can declare in their `nexum.toml` `[capabilities]` section. The migration guide carries the full WIT for each. 0.2 also publishes the experimental **`query-module`** world for request/response modules; the WIT is stable but no host implementation ships in 0.2, so it's a target for `MockHost` testing only. ### CoW-Specific Package: `shepherd:cow@0.2.0` @@ -318,14 +318,14 @@ world shepherd { ### Key properties -- **No WASI** — by default, modules cannot access FS, network, clocks, or random. The additive 0.2 capabilities (`clock`, `random`, `http`) provide controlled access to time, entropy, and allowlisted HTTP — but only when declared in the manifest's `[capabilities]` section. -- **All I/O through our interfaces** — RPC reads, identity/signing, CoW API, local-store, order submission, logging. -- **Generic JSON-RPC passthrough** — the `chain` interface exposes a single `request` function (plus an additive `request-batch`). The SDK implements alloy's `Transport` trait on top of it, giving modules the full alloy `Provider` API. See doc 07 for details. -- **Identity as a first-class primitive** — the `identity` interface provides key management and signing. The `chain` host implementation depends on `identity` internally: signing RPC methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) are intercepted and delegated to the identity backend. Modules can also import `identity` directly for `personal_sign`-style message signing, EIP-712 typed data signing, and listing accounts. (Raw-bytes signing, gated by an explicit capability, is on the 0.3 roadmap; the current `sign` MUST prepend the EIP-191 prefix.) -- **Unified `host-error` taxonomy** — every host function returns `result`. The 0.1 per-protocol error types (`json-rpc-error`, `identity-error`, `msg-error`, `store-error`, `api-error`) are gone. Modules match on `host-error-kind` (`unsupported`, `unavailable`, `denied`, `rate-limited`, `timeout`, `invalid-input`, `internal`) for retry/backoff decisions. -- **`list` for raw bytes** — local-store values, order payloads, signatures, accounts, etc. The SDK provides typed wrappers. +- **No WASI** - by default, modules cannot access FS, network, clocks, or random. The additive 0.2 capabilities (`clock`, `random`, `http`) provide controlled access to time, entropy, and allowlisted HTTP - but only when declared in the manifest's `[capabilities]` section. +- **All I/O through our interfaces** - RPC reads, identity/signing, CoW API, local-store, order submission, logging. +- **Generic JSON-RPC passthrough** - the `chain` interface exposes a single `request` function (plus an additive `request-batch`). The SDK implements alloy's `Transport` trait on top of it, giving modules the full alloy `Provider` API. See doc 07 for details. +- **Identity as a first-class primitive** - the `identity` interface provides key management and signing. The `chain` host implementation depends on `identity` internally: signing RPC methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) are intercepted and delegated to the identity backend. Modules can also import `identity` directly for `personal_sign`-style message signing, EIP-712 typed data signing, and listing accounts. (Raw-bytes signing, gated by an explicit capability, is on the 0.3 roadmap; the current `sign` MUST prepend the EIP-191 prefix.) +- **Unified `host-error` taxonomy** - every host function returns `result`. The 0.1 per-protocol error types (`json-rpc-error`, `identity-error`, `msg-error`, `store-error`, `api-error`) are gone. Modules match on `host-error-kind` (`unsupported`, `unavailable`, `denied`, `rate-limited`, `timeout`, `invalid-input`, `internal`) for retry/backoff decisions. +- **`list` for raw bytes** - local-store values, order payloads, signatures, accounts, etc. The SDK provides typed wrappers. - **Resource types** can be added later (e.g. subscription handles, cursor-based log iteration). -- **Two worlds in 0.2's reference runtime** — `nexum:host/event-module` for platform-agnostic modules; `shepherd:cow/shepherd` for CoW Protocol modules that need the `cow-api` import. The experimental `nexum:host/query-module` world is published but not yet hosted. +- **Two worlds in 0.2's reference runtime** - `nexum:host/event-module` for platform-agnostic modules; `shepherd:cow/shepherd` for CoW Protocol modules that need the `cow-api` import. The experimental `nexum:host/query-module` world is published but not yet hosted. ## Host-Side Embedding @@ -409,7 +409,7 @@ impl nexum::host::chain::Host for NexumHostState { let provider = self.provider_for(chain_id)?; let raw_params: Box = RawValue::from_string(params)?; - // One function handles the entire eth_ namespace — alloy's provider + // One function handles the entire eth_ namespace - alloy's provider // stack (timeout, retry, rate-limit, fallback) applies transparently. match provider.raw_request_dyn(method.into(), &raw_params).await { Ok(result) => Ok(Ok(result.get().to_string())), @@ -492,7 +492,7 @@ See doc 07 for the full `chain` and `cow-api` host implementations, method allow ### Universal modules (`nexum-sdk`) -Module authors targeting the universal `event-module` world add the `nexum-sdk` crate and use the `#[nexum::module]` proc macro. Modules can access identity for signing operations — either indirectly through `chain` (signing RPC methods are handled transparently) or directly via the `identity` interface for raw signing: +Module authors targeting the universal `event-module` world add the `nexum-sdk` crate and use the `#[nexum::module]` proc macro. Modules can access identity for signing operations - either indirectly through `chain` (signing RPC methods are handled transparently) or directly via the `identity` interface for raw signing: ```rust use nexum_sdk::prelude::*; @@ -518,7 +518,7 @@ impl BlockLogger { ### CoW Protocol modules (`shepherd-sdk`) -Module authors targeting the CoW-specific `shepherd` world add the `shepherd-sdk` crate and use the `#[shepherd::module]` proc macro. The macro provides **named event handlers** (`on_block`, `on_logs`, `on_tick`, `on_message`) — it generates the `on_event` match dispatch, WIT export wrapper, and optional provider injection. Handlers can be `async fn` for natural `.await`: +Module authors targeting the CoW-specific `shepherd` world add the `shepherd-sdk` crate and use the `#[shepherd::module]` proc macro. The macro provides **named event handlers** (`on_block`, `on_logs`, `on_tick`, `on_message`) - it generates the `on_event` match dispatch, WIT export wrapper, and optional provider injection. Handlers can be `async fn` for natural `.await`: ```rust use shepherd_sdk::prelude::*; @@ -538,11 +538,11 @@ impl TwapMonitor { Ok(()) } - // Named handler — macro generates on_event match dispatch. + // Named handler - macro generates on_event match dispatch. // provider is injected from block.chain_id. - // async fn — macro wraps in block_on (single-poll, zero overhead). + // async fn - macro wraps in block_on (single-poll, zero overhead). async fn on_block(block: Block, provider: &RootProvider) -> Result<()> { - // Full alloy Provider API — natural .await + // Full alloy Provider API - natural .await let block_num = provider.get_block_number().await?; let balance = provider.get_balance(owner).latest().await?; @@ -590,12 +590,12 @@ All produce valid components against the same WIT worlds (`nexum:host/event-modu ### Fuel (deterministic cost accounting) -- `Config::consume_fuel(true)` — each WASM op consumes fuel; exhaustion traps. +- `Config::consume_fuel(true)` - each WASM op consumes fuel; exhaustion traps. - Use for **per-invocation budgets**: cap a single `on_event` callback. ### Epoch Interruption (cooperative time-slicing) -- `Config::epoch_interruption(true)` — background Tokio task calls `engine.increment_epoch()` on a fixed interval. +- `Config::epoch_interruption(true)` - background Tokio task calls `engine.increment_epoch()` on a fixed interval. - Stores yield at epoch boundaries via `epoch_deadline_async_yield_and_update`. - Use for **wall-clock fairness**: prevent one module from starving others. @@ -605,9 +605,9 @@ Both are needed: fuel for correctness, epochs for liveness. Implement `ResourceLimiter` to cap per-module: -- **Memory growth** — target <10 MB default. -- **Table growth** — max entries. -- **Instance count** — max concurrent. +- **Memory growth** - target <10 MB default. +- **Table growth** - max entries. +- **Instance count** - max concurrent. Enforced synchronously on every `memory.grow` / `table.grow`. @@ -628,7 +628,7 @@ All RPC and CoW API I/O is async (alloy / reqwest on the host). wasmtime bridges - WASI 0.2.1 is stable in wasmtime. WASI 0.3 (native async) is in preview. - The `event-module` world imports **zero WASI interfaces**. - This is a security feature: components structurally cannot access FS/network/clocks via WASI. -- The 0.2 additive capabilities (`clock`, `random`, `http`) cover the common needs that would otherwise drive a WASI import, but as first-class Nexum interfaces — capability-negotiated via the manifest, allowlisted (in the HTTP case), and consistent with the rest of the host surface (`host-error` returns, no panics on capability absence). +- The 0.2 additive capabilities (`clock`, `random`, `http`) cover the common needs that would otherwise drive a WASI import, but as first-class Nexum interfaces - capability-negotiated via the manifest, allowlisted (in the HTTP case), and consistent with the rest of the host surface (`host-error` returns, no panics on capability absence). ## Summary: Nexum <-> wasmtime Mapping diff --git a/docs/02-modules-events-packaging.md b/docs/02-modules-events-packaging.md index bebdb96..8cc7cd0 100755 --- a/docs/02-modules-events-packaging.md +++ b/docs/02-modules-events-packaging.md @@ -2,7 +2,7 @@ ## Module Package: the Nexum Module Bundle -A module is distributed as a **bundle** — a WASM component plus a manifest that declares its identity, event subscriptions, chain requirements, and resource limits. The manifest is the bridge between packaging, the event system, and the runtime lifecycle. +A module is distributed as a **bundle** - a WASM component plus a manifest that declares its identity, event subscriptions, chain requirements, and resource limits. The manifest is the bridge between packaging, the event system, and the runtime lifecycle. ### Manifest (`nexum.toml`) @@ -26,12 +26,12 @@ max_state_bytes = 52_428_800 # 50 MB [module.restart] max_consecutive_failures = 10 # Dead after this many consecutive failures -# Chain requirements — the runtime provides RPC for these +# Chain requirements - the runtime provides RPC for these [chains] required = [42161] # Arbitrum (must have) optional = [1, 100] # Mainnet, Gnosis (used if available) -# Capability negotiation (new in 0.2) — which host primitives the module needs. +# Capability negotiation (new in 0.2) - which host primitives the module needs. # Optional imports trap with host-error { kind: unsupported } on call rather # than failing instantiation. Omitting this section falls back to # "all imports required" with a deprecation warning. @@ -43,7 +43,7 @@ denied = [] [capabilities.http] allow = ["api.cow.fi"] # outbound HTTP domain allowlist -# Event subscriptions — declares what the runtime should feed this module +# Event subscriptions - declares what the runtime should feed this module [[subscription]] kind = "block" chain_id = 42161 @@ -58,7 +58,7 @@ topics = ["0x…"] # ComposableCoW ConditionalOrderCreated kind = "cron" schedule = "*/5 * * * *" # every 5 minutes -# Typed config — TOML values preserve their type at the guest (0.2) +# Typed config - TOML values preserve their type at the guest (0.2) [config] cow_api_url = "https://api.cow.fi/arbitrum" min_twap_interval_secs = 120 # integer stays integer @@ -67,11 +67,11 @@ enable_alerts = true # boolean stays boolean Key design points: -- **`component` is a content hash**, not a filename. The runtime resolves it via the content store (see below). (Was `wasm = ...` in 0.1 — see the migration guide.) -- **`[[subscription]]` blocks are declarative.** The module doesn't set up its own subscriptions imperatively — the runtime reads the manifest and wires up event sources before calling `init`. The 0.1 spelling was `[[subscribe]]` with `type = ...`; 0.2 uses `[[subscription]]` with `kind = ...` because `type` is a reserved word in several binding languages. +- **`component` is a content hash**, not a filename. The runtime resolves it via the content store (see below). (Was `wasm = ...` in 0.1 - see the migration guide.) +- **`[[subscription]]` blocks are declarative.** The module doesn't set up its own subscriptions imperatively - the runtime reads the manifest and wires up event sources before calling `init`. The 0.1 spelling was `[[subscribe]]` with `type = ...`; 0.2 uses `[[subscription]]` with `kind = ...` because `type` is a reserved word in several binding languages. - **`[capabilities]`** is new in 0.2 and now drives what the runtime links into the module's import space. See the migration guide for the full schema (including `[capabilities.http]` allowlists and `[capabilities.identity].methods` subsets). - **`resources` are caps**, not requests. The runtime enforces them via wasmtime's `ResourceLimiter` and fuel system. -- **`chains.required`** — if the runtime doesn't have an RPC endpoint for a required chain, the module fails to load (fast, clear error). +- **`chains.required`** - if the runtime doesn't have an RPC endpoint for a required chain, the module fails to load (fast, clear error). - **`config`** is opaque to the runtime. 0.2 keeps 0.1's stringly-typed shape (`list>`); the host flattens TOML scalars (numbers, booleans) to their string form on the way through. A typed `config-value` variant is on the 0.3 roadmap, bundled with the manifest-parser work. ### Bundle Format @@ -98,7 +98,7 @@ How the directory is represented depends on the content backend: ## Content-Addressed Distribution -Distribution is **agnostic** — the runtime resolves content by hash through pluggable backends. The manifest's `wasm` field is a content address; the `source` in the runtime config tells the runtime *where* to look. +Distribution is **agnostic** - the runtime resolves content by hash through pluggable backends. The manifest's `wasm` field is a content address; the `source` in the runtime config tells the runtime *where* to look. ### Content Reference Scheme @@ -152,7 +152,7 @@ registry = "ghcr.io" This means: - A **local dev** just drops `.wasm` files in a directory. - A **production deployment** fetches from Swarm or OCI on first load, then caches locally. -- **Integrity is always verified** — the content hash in the manifest is the trust anchor, not the transport. +- **Integrity is always verified** - the content hash in the manifest is the trust anchor, not the transport. ## Module Lifecycle @@ -178,7 +178,7 @@ stateDiagram-v2 |-------|-------------| | **Resolve** | Content store resolves `component` hash to local path. Fail -> `Dead`. | | **Load** | `Component::from_file`, create `InstancePre`. Validates that the component satisfies the target WIT world (`nexum:host/event-module` or `shepherd:cow/shepherd`). Installs trap stubs for capabilities the manifest declares `optional` but the host does not provide. Fail -> `Dead`. | -| **Init** | Create `Store`, instantiate, call `init(config)` inside an implicit write transaction (same semantics as `on_event` — commit on success, rollback on failure). Module sets up internal state. Fail -> `Restart` (might be transient). | +| **Init** | Create `Store`, instantiate, call `init(config)` inside an implicit write transaction (same semantics as `on_event` - commit on success, rollback on failure). Module sets up internal state. Fail -> `Restart` (might be transient). | | **Run** | Runtime dispatches events to `on_event`. Each call gets a fuel budget. Module processes events and may call host imports (chain, local-store, identity, cow-api, etc.). | | **Restart** | After a trap or error. Backoff: 1s -> 2s -> 4s -> ... -> 5min cap. A fresh `Store` is created (clean memory), but **local-store data persists** (it's in redb, external to the WASM instance). | | **Dead** | After N consecutive failures (poison pill detection) or explicit operator shutdown. No further event dispatch. Requires manual intervention. | @@ -186,10 +186,10 @@ stateDiagram-v2 ### Key Lifecycle Properties - **State survives restarts.** The redb key-value store is external to the WASM instance. A restarted module picks up where it left off. -- **Memory does not survive restarts.** Each restart creates a fresh `Store` — clean linear memory, no stale pointers. +- **Memory does not survive restarts.** Each restart creates a fresh `Store` - clean linear memory, no stale pointers. - **`InstancePre` is reused.** Compilation and linking are done once at Load. Restarts only create a new `Store` and call `init` again. - **Config is immutable for a loaded module.** Changing config requires a reload (new Load cycle). -- **Hot-reload sequence.** When a module update is detected (e.g. ENS contenthash changed): (1) let the current in-flight `on_event` complete, (2) stop event dispatch for this module, (3) fetch and compile the new `Component`, (4) create new `InstancePre`, (5) create fresh `Store`, (6) call `init` with new config — state table is inherited (module handles migration), (7) resume event dispatch. The old `InstancePre` is dropped. +- **Hot-reload sequence.** When a module update is detected (e.g. ENS contenthash changed): (1) let the current in-flight `on_event` complete, (2) stop event dispatch for this module, (3) fetch and compile the new `Component`, (4) create new `InstancePre`, (5) create fresh `Store`, (6) call `init` with new config - state table is inherited (module handles migration), (7) resume event dispatch. The old `InstancePre` is dropped. ## Event System @@ -257,7 +257,7 @@ When an event fires: - **Sequential within a module.** Events for the same module are dispatched in order. A module sees block N before block N+1. This is enforced by a per-module dispatch queue (Tokio `mpsc` channel). - **Best-effort delivery.** If a module is in Restart state when an event arrives, the event is queued (bounded buffer). If the buffer fills, oldest events are dropped and a warning is logged. - **No acknowledgement.** A successful return from `on_event` is not an ack. The module is responsible for using the local-store to track its own progress (e.g. "last processed block"). -- **Catch-up after gaps.** Events can be dropped during restart (bounded buffer overflow). Modules should query for missed data on startup — e.g. in `init`, read `last_block` from local-store, use the alloy `Provider` (backed by `chain::request`) to call `get_block_number()` and `get_logs()` to backfill any gap. This is a best practice, not enforced by the runtime. +- **Catch-up after gaps.** Events can be dropped during restart (bounded buffer overflow). Modules should query for missed data on startup - e.g. in `init`, read `last_block` from local-store, use the alloy `Provider` (backed by `chain::request`) to call `get_block_number()` and `get_logs()` to backfill any gap. This is a best practice, not enforced by the runtime. ### Event Type Encoding @@ -283,7 +283,7 @@ record tick { } ``` -The runtime serialises event data via the canonical ABI (handled automatically by `bindgen!`). Note the 0.2 semantic change: all `u64` timestamps in 0.2 are **milliseconds since Unix epoch, UTC**. The 0.1 WIT did not specify a unit and several sources used seconds — audit any timestamp arithmetic. The `tick` variant (formerly `timer(u64)`) is now a record so bindings read `event.tick.firedAt` instead of comparing a bare integer. +The runtime serialises event data via the canonical ABI (handled automatically by `bindgen!`). Note the 0.2 semantic change: all `u64` timestamps in 0.2 are **milliseconds since Unix epoch, UTC**. The 0.1 WIT did not specify a unit and several sources used seconds - audit any timestamp arithmetic. The `tick` variant (formerly `timer(u64)`) is now a record so bindings read `event.tick.firedAt` instead of comparing a bare integer. ## Updated WIT Worlds @@ -384,7 +384,7 @@ interface identity { sign-typed-data: func(account: list, typed-data: string) -> result, host-error>; } -/// Universal event-driven module world — platform-agnostic. Imports the six +/// Universal event-driven module world - platform-agnostic. Imports the six /// primitives in 0.2 (identity was missing from the 0.1 WIT despite being /// part of the primitive taxonomy). world event-module { @@ -424,7 +424,7 @@ interface cow-api { -> result; } -/// CoW Protocol module world — extends event-module with cow-api. +/// CoW Protocol module world - extends event-module with cow-api. world shepherd { include nexum:host/event-module; @@ -466,11 +466,11 @@ Operator deploys a module: → Router → twap-monitor's dispatch queue → Tokio task calls on_event(Event::Block(…)) → Module calls chain::request (via alloy Provider), local-store get, cow-api submit-order - → Returns Ok(()) — runtime logs success + → Returns Ok(()) - runtime logs success 7. On crash: → Module trapped (fuel exhaustion / panic) → Runtime logs error, enters Restart state → Backoff 1s, creates fresh Store, calls init again - → Local-store data still intact — module resumes + → Local-store data still intact - module resumes ``` diff --git a/docs/03-module-discovery.md b/docs/03-module-discovery.md index 56067cb..4459239 100755 --- a/docs/03-module-discovery.md +++ b/docs/03-module-discovery.md @@ -1,6 +1,6 @@ # Module Discovery -Doc 02 defines how modules are packaged (bundle = `nexum.toml` + `module.wasm`) and how content is fetched by hash (pluggable content store). This document defines how the runtime **discovers which modules to load** — the layer above content resolution. +Doc 02 defines how modules are packaged (bundle = `nexum.toml` + `module.wasm`) and how content is fetched by hash (pluggable content store). This document defines how the runtime **discovers which modules to load** - the layer above content resolution. Three discovery sources, from simplest to most decentralised: @@ -54,7 +54,7 @@ twap-monitor.shepherd.eth └── text: shepherd.name → "twap-monitor" ``` -The `contenthash` points to the full bundle on Swarm (a directory containing `nexum.toml` + `module.wasm`). Text records provide lightweight metadata the runtime can read without fetching the bundle — useful for filtering or display. +The `contenthash` points to the full bundle on Swarm (a directory containing `nexum.toml` + `module.wasm`). Text records provide lightweight metadata the runtime can read without fetching the bundle - useful for filtering or display. ### Runtime resolution flow @@ -98,11 +98,11 @@ When the module author publishes a new version, they: 1. Upload the new bundle to Swarm → get new content hash 2. Update the ENS `contenthash` record -The runtime detects the change on its next poll (or via event — see below), fetches the new bundle, and hot-reloads the module. +The runtime detects the change on its next poll (or via event - see below), fetches the new bundle, and hot-reloads the module. ## 3. On-Chain Registry (Contract Events) -For fully autonomous discovery — the runtime watches a contract for registration events and auto-loads modules without operator intervention. +For fully autonomous discovery - the runtime watches a contract for registration events and auto-loads modules without operator intervention. ### Option A: Dedicated registry contract @@ -130,7 +130,7 @@ interface INexumRegistry { The runtime subscribes to `ModuleRegistered` events, resolves the ENS name from the event, and enters the ENS resolution flow above. -### Option B: No ad-hoc registry — contracts self-declare via ENS +### Option B: No ad-hoc registry - contracts self-declare via ENS This is the more decentralised approach. Instead of a central registry: @@ -169,7 +169,7 @@ ethflow.modules.shepherd.eth → contenthash of Ethflow bundle *.modules.shepherd.eth → resolved by registry contract ``` -The wildcard resolver is itself the registry — anyone can register a subdomain. The runtime subscribes to events from the resolver contract to discover new modules. +The wildcard resolver is itself the registry - anyone can register a subdomain. The runtime subscribes to events from the resolver contract to discover new modules. This gives us human-readable, permissionless module discovery under a shared namespace. @@ -196,8 +196,8 @@ Discovery is permissionless, but **execution requires operator consent**. The ru ```toml [discovery] -# "allowlist" — only load modules from these sources -# "auto" — load anything discovered (use with caution) +# "allowlist" - only load modules from these sources +# "auto" - load anything discovered (use with caution) mode = "allowlist" # If mode = "allowlist", only these ENS names / registries are trusted @@ -222,8 +222,8 @@ In `auto` mode, the runtime loads any module it discovers (useful for a public " Suggested naming under a shared parent (e.g. `shepherd.eth` or a subdomain of the protocol): ``` -.shepherd.eth — community / independent modules -..eth — protocol-owned modules +.shepherd.eth - community / independent modules +..eth - protocol-owned modules Examples: twap-monitor.shepherd.eth diff --git a/docs/04-state-store.md b/docs/04-state-store.md index 1eff78c..98fa172 100755 --- a/docs/04-state-store.md +++ b/docs/04-state-store.md @@ -4,26 +4,26 @@ Every Nexum module has access to a persistent key-value store that survives restarts, crashes, and module updates. The store is backed by **redb** (v3.1, pure Rust, embedded, ACID, MVCC) and exposed to modules through the `local-store` WIT interface. -The local store is the only durable memory a module has — WASM linear memory is wiped on every restart. Modules must be written to reconstruct their working state from the store on `init`. +The local store is the only durable memory a module has - WASM linear memory is wiped on every restart. Modules must be written to reconstruct their working state from the store on `init`. ## redb Fundamentals | Property | Detail | |----------|--------| | Engine | Copy-on-write B-tree | -| Concurrency | MVCC — concurrent readers, single writer, no blocking | +| Concurrency | MVCC - concurrent readers, single writer, no blocking | | Durability | Crash-safe by default (fsync on commit) | -| Transactions | Full ACID — read txns and write txns | +| Transactions | Full ACID - read txns and write txns | | Key types | `&str`, `&[u8]`, integers, tuples, `Option`, fixed arrays | | Value types | All key types + `Vec`, `f32`/`f64`, `()` | | Size | No hard limit; v3 file format starts at ~50 KiB | ## Isolation Model -Each module gets its own **redb database file**. Modules cannot read or write each other's state — enforced by filesystem-level separation. +Each module gets its own **redb database file**. Modules cannot read or write each other's state - enforced by filesystem-level separation. ```rust -// Runtime side — one database per module +// Runtime side - one database per module fn open_module_db(module_id: &str) -> Result { let path = format!("/var/nexum/state/{module_id}.redb"); Database::create(&path) @@ -33,7 +33,7 @@ fn open_module_db(module_id: &str) -> Result { const LOCAL_STORE_TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("state"); ``` -Module identity = `name` from `nexum.toml`. If two module instances share a name, they share state (intentional — enables hot-reload with state continuity). Different modules have different names and fully isolated database files. +Module identity = `name` from `nexum.toml`. If two module instances share a name, they share state (intentional - enables hot-reload with state continuity). Different modules have different names and fully isolated database files. ``` /var/nexum/state/ @@ -56,7 +56,7 @@ interface local-store { /// Set a key-value pair. Overwrites existing value. /// Returns host-error { domain: "store", kind: invalid-input | internal | ... } on failure. /// Quota exhaustion surfaces as host-error { domain: "store", kind: invalid-input } - /// (or a future dedicated `quota-exceeded` kind) — see the migration guide. + /// (or a future dedicated `quota-exceeded` kind) - see the migration guide. set: func(key: string, value: list) -> result<_, host-error>; /// Delete a key. No-op if key doesn't exist. @@ -69,7 +69,7 @@ interface local-store { In 0.1 `local-store` errors were bare `string` values. 0.2 replaces them with the unified `host-error` type (see [migration guide §2](migration/0.1-to-0.2.md#2-error-model-unification-both)) so modules can match on `host-error-kind` rather than parsing error strings. -Keys are UTF-8 strings. Values are opaque bytes — the SDK provides typed wrappers (see doc 05). +Keys are UTF-8 strings. Values are opaque bytes - the SDK provides typed wrappers (see doc 05). `list-keys` enables prefix-based namespacing within a module's state: @@ -107,7 +107,7 @@ flowchart TD B --> C["No state changes persisted -- atomically rolled back"] ``` -This gives us **all-or-nothing semantics per call**: either all state mutations from a single `init` or `on_event` callback are applied, or none are. This is critical for correctness — a module that crashes halfway through processing a block doesn't leave behind partial state. Equally, a failed `init` during restart doesn't corrupt state from the previous version. +This gives us **all-or-nothing semantics per call**: either all state mutations from a single `init` or `on_event` callback are applied, or none are. This is critical for correctness - a module that crashes halfway through processing a block doesn't leave behind partial state. Equally, a failed `init` during restart doesn't corrupt state from the previous version. ### Read-your-own-writes @@ -123,7 +123,7 @@ This works because all operations within one event go through the same `WriteTra ### Concurrency: One Database Per Module -redb allows only **one `WriteTransaction` at a time** per `Database` — a second `begin_write()` blocks until the first commits or aborts. Since modules dispatch events concurrently (doc 02), a single shared redb file would serialise all write transactions across modules, negating concurrency. +redb allows only **one `WriteTransaction` at a time** per `Database` - a second `begin_write()` blocks until the first commits or aborts. Since modules dispatch events concurrently (doc 02), a single shared redb file would serialise all write transactions across modules, negating concurrency. **Design decision:** each module gets its own redb `Database` file: @@ -134,7 +134,7 @@ redb allows only **one `WriteTransaction` at a time** per `Database` — a secon └── price-alert.redb ``` -This gives true write isolation — module A's transaction never blocks module B. The cost is more file handles (one per module), which is negligible for the expected module count. +This gives true write isolation - module A's transaction never blocks module B. The cost is more file handles (one per module), which is negligible for the expected module count. Within a single module, events are already sequential (doc 02 dispatch semantics), so there is never contention on a module's own database. @@ -178,7 +178,7 @@ On first load, the module's table is empty. The module's `init` function should ```rust fn init(config: Config) -> Result<(), HostError> { if local_store::get("initialized")?.is_none() { - // First run — set up initial state + // First run - set up initial state local_store::set("initialized", &[1])?; local_store::set("last_block", &0u64.to_le_bytes())?; } @@ -224,8 +224,8 @@ fn init(config: Config) -> Result<(), HostError> { ### Module Removal When an operator removes a module, its state table can optionally be: -- **Retained** (default) — in case the module is re-added later. -- **Purged** — operator explicitly requests deletion via CLI. +- **Retained** (default) - in case the module is re-added later. +- **Purged** - operator explicitly requests deletion via CLI. ```bash nexum state purge --module twap-monitor @@ -294,9 +294,9 @@ impl ModuleStateCtx { | Key type | UTF-8 string | | Value type | Opaque bytes (`list` in WIT) | | Namespacing within module | Convention: slash-separated prefixes + `list-keys` | -| Transaction scope | Per `init` / `on_event` call — commit on success, rollback on failure | +| Transaction scope | Per `init` / `on_event` call - commit on success, rollback on failure | | Read-your-own-writes | Yes (same `WriteTransaction`) | | Size limit | Enforced per-module via manifest `max_state_bytes` | -| Survives restart | Yes — state is external to WASM instance | +| Survives restart | Yes - state is external to WASM instance | | Module update | New version inherits state; `init` handles migration | | Backup | Online copy under read transaction | diff --git a/docs/05-sdk-design.md b/docs/05-sdk-design.md index 758a5d2..13f14b1 100755 --- a/docs/05-sdk-design.md +++ b/docs/05-sdk-design.md @@ -1,5 +1,33 @@ # SDK Design: Layered SDK (`nexum-sdk` + `shepherd-sdk`) +> **Current implementation status (M3, 2026-06-17)** +> +> This document is the **0.2 / M5+ north-star** vision. M3 shipped a focused subset; everything else below is deferred to M4/M5. The split: +> +> | Feature | M3 status | Where | +> |---|---|---| +> | `shepherd-sdk` crate | ✅ shipped | `crates/shepherd-sdk/` | +> | `shepherd-sdk-test` crate (mock host) | ✅ shipped | `crates/shepherd-sdk-test/` | +> | Host traits (`ChainHost`, `LocalStoreHost`, `CowApiHost`, `LoggingHost`) + supertrait `Host` | ✅ shipped | `crates/shepherd-sdk/src/host.rs` (see ADR-0009) | +> | `strategy.rs` (pure logic) + `lib.rs` (wit-bindgen adapter) recipe | ✅ shipped | every M2/M3 module | +> | `HostError` / `HostErrorKind` (SDK-side mirror of wit) | ✅ shipped | `crates/shepherd-sdk/src/host.rs` | +> | `chain` helpers (`eth_call_params`, `parse_eth_call_result`, `decode_revert_hex`) | ✅ shipped | `crates/shepherd-sdk/src/chain/` | +> | `cow` helpers (`PollOutcome`, `RetryAction`, `classify_api_error`, `gpv2_to_order_data`, `decode_revert`, `IConditionalOrder`) | ✅ shipped | `crates/shepherd-sdk/src/cow/` | +> | `MockHost` with per-trait mocks (`MockChain`, `MockLocalStore`, `MockCowApi`, `MockLogging`) | ✅ shipped | `crates/shepherd-sdk-test/src/lib.rs` | +> | Separate `nexum-sdk` crate | ❌ deferred (M5) | only `shepherd-sdk` exists today | +> | `#[nexum::module]` / `#[shepherd::module]` proc macros | ❌ deferred (M5) | modules write `wit_bindgen::generate!` + `WitBindgenHost` adapter by hand | +> | Named event handlers (`on_block` / `on_logs` / `on_tick` / `on_message` injection) | ❌ deferred (M5) | modules pattern-match on `types::Event` in `Guest::on_event` | +> | `async fn` handler support via `block_on` | ❌ deferred (M5) | strategy functions are synchronous | +> | Full alloy `Provider` via `HostTransport` | ❌ deferred (M5) | modules call `host.request(chain_id, method, params)` with JSON strings | +> | `TypedState` (postcard-backed typed local-store) | ❌ deferred (M5) | modules call `host.set(&key, &raw_bytes)` directly | +> | `Signer` (ECDSA + EIP-712 via `identity` host interface) | ❌ deferred (M5) | modules use `Signature::PreSign` / `Signature::Eip1271`; no key custody on the module side | +> | `Cow` typed CoW Protocol API client (quote / get_order / raw_request) | ❌ deferred (M5) | `cow-api` exposes only `submit-order` today | +> | `MockIdentity`, `MockProvider`, `WasmTestHarness` | ❌ deferred (M5) | tests against `&impl Host` + per-trait mocks | +> | `cargo nexum` CLI (new / build / package / publish) | ❌ deferred (M5) | modules use `cargo build --target wasm32-wasip2` directly | +> | `block.timestamp` in ms | ✅ shipped | confirmed in `nexum:host/types` | +> +> **Reader's guide**: treat the sections below as design intent the next two milestones move toward, not API documentation for the code that exists today. For M3 API reference, see [sdk.md](sdk.md) and the rustdoc on `crates/shepherd-sdk/`. The M3 architectural decision is captured in [ADR-0009](adr/0009-host-trait-surface.md). + ## Purpose The SDK is split into two layers: diff --git a/docs/07-rpc-namespace-design.md b/docs/07-rpc-namespace-design.md index 8f443a3..ba31db8 100755 --- a/docs/07-rpc-namespace-design.md +++ b/docs/07-rpc-namespace-design.md @@ -22,19 +22,19 @@ This creates several problems: 1. **Boilerplate multiplication.** Every new `eth_` method requires changes in three places: WIT definition, host trait implementation, and SDK wrapper. The Ethereum JSON-RPC namespace has 30+ methods; most modules will need more than the three currently exposed. -2. **Alloy incompatibility.** Module authors using Rust cannot use alloy's `Provider` API — which provides 80+ typed convenience methods — because the transport layer is locked behind per-method WIT functions. They're forced to manually ABI-encode calldata, call `blockchain::eth_call`, and ABI-decode the result for every interaction. +2. **Alloy incompatibility.** Module authors using Rust cannot use alloy's `Provider` API - which provides 80+ typed convenience methods - because the transport layer is locked behind per-method WIT functions. They're forced to manually ABI-encode calldata, call `blockchain::eth_call`, and ABI-decode the result for every interaction. 3. **Namespace rigidity.** Adding a `cow_` namespace for CoW Protocol API calls would duplicate the same per-method pattern. Future namespaces (debug_, trace_, etc.) compound this further. -The goal: **one WIT function to rule the entire `eth_` namespace**, with a guest-side SDK that gives module authors the full alloy `Provider` API — no manual ABI wrangling, no WIT changes when new methods are needed. +The goal: **one WIT function to rule the entire `eth_` namespace**, with a guest-side SDK that gives module authors the full alloy `Provider` API - no manual ABI wrangling, no WIT changes when new methods are needed. ## Design: Generic JSON-RPC Passthrough ### Core Insight -alloy's `Transport` trait is a Tower `Service`. If we expose a single JSON-RPC dispatch function in WIT, the SDK can implement `Transport` on top of it. This gives guest modules the entire alloy `Provider` API for free — every current and future `eth_` method works automatically. +alloy's `Transport` trait is a Tower `Service`. If we expose a single JSON-RPC dispatch function in WIT, the SDK can implement `Transport` on top of it. This gives guest modules the entire alloy `Provider` API for free - every current and future `eth_` method works automatically. -From the guest's perspective, host function calls are synchronous (they block until the host returns). The returned future resolves in a single poll. This means alloy's async `Provider` methods work with a trivial executor — no real async machinery needed. +From the guest's perspective, host function calls are synchronous (they block until the host returns). The returned future resolves in a single poll. This means alloy's async `Provider` methods work with a trivial executor - no real async machinery needed. ### Architecture @@ -89,11 +89,11 @@ interface chain { } ``` -Errors are reported via the unified `host-error` (see doc 00 and the [migration guide §2](migration/0.1-to-0.2.md#2-error-model-unification-both)) — the 0.1 `json-rpc-error` shape is gone. Modules match on `host-error-kind` (`unavailable`, `rate-limited`, `timeout`, `denied`, `invalid-input`, ...) for retry/backoff decisions rather than parsing numeric JSON-RPC codes. +Errors are reported via the unified `host-error` (see doc 00 and the [migration guide §2](migration/0.1-to-0.2.md#2-error-model-unification-both)) - the 0.1 `json-rpc-error` shape is gone. Modules match on `host-error-kind` (`unavailable`, `rate-limited`, `timeout`, `denied`, `invalid-input`, ...) for retry/backoff decisions rather than parsing numeric JSON-RPC codes. The `types` interface is unchanged in shape (it now exposes `host-error` / `host-error-kind`). The `local-store`, `remote-store`, `messaging`, and `logging` interfaces are unchanged. -The `identity` interface provides cryptographic identity — key management and signing: +The `identity` interface provides cryptographic identity - key management and signing: ```wit interface identity { @@ -111,7 +111,7 @@ interface identity { } ``` -The universal `event-module` world (in `nexum:host`) contains the platform-agnostic interfaces — six imports in 0.2: +The universal `event-module` world (in `nexum:host`) contains the platform-agnostic interfaces - six imports in 0.2: ```wit world event-module { @@ -143,21 +143,21 @@ world shepherd { | `blockchain::eth-call(chain-id, to, data)` | `chain::request(chain-id, "eth_call", params_json)` | | `blockchain::eth-get-logs(filter)` | `chain::request(chain-id, "eth_getLogs", params_json)` | | `blockchain::eth-block-number(chain-id)` | `chain::request(chain-id, "eth_blockNumber", "[]")` | -| *n/a — not exposed* | `chain::request(chain-id, "eth_getBalance", params_json)` | -| *n/a — not exposed* | `chain::request(chain-id, "eth_getCode", params_json)` | -| *n/a — not exposed* | `chain::request(chain-id, "eth_getStorageAt", params_json)` | -| *n/a — not exposed* | Any `eth_*` method — no WIT change needed | +| *n/a - not exposed* | `chain::request(chain-id, "eth_getBalance", params_json)` | +| *n/a - not exposed* | `chain::request(chain-id, "eth_getCode", params_json)` | +| *n/a - not exposed* | `chain::request(chain-id, "eth_getStorageAt", params_json)` | +| *n/a - not exposed* | Any `eth_*` method - no WIT change needed | ### Why JSON Strings (Not `list`) -- The Ethereum JSON-RPC spec is JSON. alloy serialises params to JSON internally. Using `string` means zero intermediate format — the guest produces JSON, the host forwards JSON to alloy's `raw_request_dyn` which accepts `&RawValue` (a JSON string). +- The Ethereum JSON-RPC spec is JSON. alloy serialises params to JSON internally. Using `string` means zero intermediate format - the guest produces JSON, the host forwards JSON to alloy's `raw_request_dyn` which accepts `&RawValue` (a JSON string). - Debuggability: JSON is human-readable in logs and traces. - The canonical ABI cost of copying a JSON string across the component boundary is negligible relative to the network RTT of an actual RPC call. - Binary encoding (CBOR, postcard) would require custom (de)serialisation on both sides, defeating the purpose of minimising boilerplate. ## Host Implementation -The host implementation is minimal — one function handles the entire `eth_` namespace: +The host implementation is minimal - one function handles the entire `eth_` namespace: ```rust use serde_json::value::RawValue; @@ -251,7 +251,7 @@ This could be made configurable per-module via `nexum.toml`: ```toml [module.chain] # Additional methods beyond the default read-only set. -# Use with caution — write methods can have side-effects. +# Use with caution - write methods can have side-effects. extra_allowed_methods = ["eth_createAccessList"] ``` @@ -526,7 +526,7 @@ use tower::Service; use std::task::{Context, Poll}; /// An alloy-compatible transport that routes JSON-RPC requests through the -/// Nexum host engine. Synchronous from the guest's perspective — the host +/// Nexum host engine. Synchronous from the guest's perspective - the host /// function blocks until the RPC response is available. #[derive(Debug, Clone)] pub struct HostTransport { @@ -545,7 +545,7 @@ impl Service for HostTransport { type Future = TransportFut<'static>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { - // Always ready — host function calls are synchronous from the guest. + // Always ready - host function calls are synchronous from the guest. Poll::Ready(Ok(())) } @@ -594,7 +594,7 @@ fn dispatch_single( let params_json = req.params().map(|p| p.get()).unwrap_or("[]"); // This calls the WIT-imported host function. Synchronous from the guest's - // perspective — the host executes the RPC call asynchronously and returns + // perspective - the host executes the RPC call asynchronously and returns // the result when ready. match chain::request(chain_id, method, params_json) { Ok(result_json) => { @@ -626,13 +626,13 @@ fn dispatch_single( ### Why This Works Without Real Async -The `call()` method returns a `Box::pin(async move { ... })` — but the body is entirely synchronous. The `chain::request` host function blocks from the guest's perspective (the host runs the actual RPC call asynchronously via wasmtime's `func_wrap_async`, but the guest sees a normal function call that returns a value). The future resolves in a single poll. +The `call()` method returns a `Box::pin(async move { ... })` - but the body is entirely synchronous. The `chain::request` host function blocks from the guest's perspective (the host runs the actual RPC call asynchronously via wasmtime's `func_wrap_async`, but the guest sees a normal function call that returns a value). The future resolves in a single poll. -This means alloy's `Provider` methods — which `await` the transport internally — complete immediately when driven by any executor. The SDK provides a minimal single-threaded executor: +This means alloy's `Provider` methods - which `await` the transport internally - complete immediately when driven by any executor. The SDK provides a minimal single-threaded executor: ```rust /// Drive a future to completion. Since the HostTransport resolves -/// synchronously, this is a single-poll operation — no actual async +/// synchronously, this is a single-poll operation - no actual async /// scheduling occurs. pub fn block_on(future: F) -> F::Output { futures_executor::block_on(future) @@ -649,8 +649,8 @@ use alloy_rpc_client::RpcClient; /// Create an alloy `Provider` backed by the Nexum host engine. /// -/// The returned provider supports the full alloy `Provider` API — all `eth_*` -/// methods, builder patterns, typed responses — routing every request through +/// The returned provider supports the full alloy `Provider` API - all `eth_*` +/// methods, builder patterns, typed responses - routing every request through /// the host's RPC stack (timeout, retry, rate-limit, failover). /// /// ```rust @@ -675,15 +675,15 @@ let block_num = block_on(provider.get_block_number())?; // noisy let balance = block_on(provider.get_balance(addr).latest())?; // everywhere ``` -This is verbose and obscures the actual logic. But we can't reimplement every `Provider` method as a synchronous wrapper — that defeats the purpose of the generic passthrough. +This is verbose and obscures the actual logic. But we can't reimplement every `Provider` method as a synchronous wrapper - that defeats the purpose of the generic passthrough. ### The Solution: Named Event Handlers + `async fn` The proc macro (see doc 05) already generates the WIT export boilerplate. We extend it in two ways. For universal modules, the `#[nexum::module]` macro is used; for CoW modules, the `#[shepherd::module]` macro (which extends the universal one with CoW-specific imports): -1. **Named event handlers** — instead of writing the `match event { ... }` dispatch manually, module authors implement `on_block`, `on_logs`, `on_tick`, and/or `on_message`. The macro generates the `on_event` match. -2. **`async fn` support** — handlers can be async. The macro wraps the generated `on_event` in `block_on()`, so `.await` works naturally. -3. **Provider injection** — if a handler accepts `&RootProvider` as a second parameter, the macro creates the provider from the event's chain_id and passes it in. +1. **Named event handlers** - instead of writing the `match event { ... }` dispatch manually, module authors implement `on_block`, `on_logs`, `on_tick`, and/or `on_message`. The macro generates the `on_event` match. +2. **`async fn` support** - handlers can be async. The macro wraps the generated `on_event` in `block_on()`, so `.await` works naturally. +3. **Provider injection** - if a handler accepts `&RootProvider` as a second parameter, the macro creates the provider from the event's chain_id and passes it in. **What the module author writes (universal module):** @@ -748,7 +748,7 @@ impl Guest for MyModule { } ``` -The generated code calls `block_on` exactly once — at the top-level export boundary. Inside the async block, all `.await` calls resolve immediately (the `HostTransport` is synchronous under the hood). No real async scheduler runs. No tokio. No waker machinery. It's syntactic sugar that costs nothing at runtime. +The generated code calls `block_on` exactly once - at the top-level export boundary. Inside the async block, all `.await` calls resolve immediately (the `HostTransport` is synchronous under the hood). No real async scheduler runs. No tokio. No waker machinery. It's syntactic sugar that costs nothing at runtime. ### Named Handler Conventions @@ -765,11 +765,11 @@ The macro inspects each handler's signature: - **Async handlers** -> wrapped in `block_on`; sync handlers called directly - **Missing handlers** -> `Ok(())` for that variant (no-op) -**Escape hatch:** defining `on_event` directly takes precedence — the macro uses it as-is (wrapping in `block_on` if async) and ignores named handlers. +**Escape hatch:** defining `on_event` directly takes precedence - the macro uses it as-is (wrapping in `block_on` if async) and ignores named handlers. ### Why This Works -1. **WIT exports are synchronous.** The Component Model export signature is `func(event) -> result<_, string>` — no async. The macro bridges this by wrapping the generated dispatch in `block_on`. +1. **WIT exports are synchronous.** The Component Model export signature is `func(event) -> result<_, string>` - no async. The macro bridges this by wrapping the generated dispatch in `block_on`. 2. **The transport resolves in one poll.** `HostTransport::call()` returns a future whose body is entirely synchronous (it calls the WIT host function, which blocks). When alloy's `Provider` awaits the transport, the future completes immediately. @@ -779,10 +779,10 @@ The macro inspects each handler's signature: ```rust async fn on_block(block: Block, provider: &RootProvider) -> Result<()> { - // EthCall builder — .latest() and .await both work + // EthCall builder - .latest() and .await both work let result = provider.call(tx).latest().await?; - // Filter builder — standard alloy ergonomics + // Filter builder - standard alloy ergonomics let logs = provider.get_logs(&filter).await?; // Raw request for unlisted methods @@ -832,7 +832,7 @@ impl MyModule { // Manual ABI encode let calldata = balanceOfCall { owner: addr }.abi_encode(); - // Raw host call — returns opaque bytes + // Raw host call - returns opaque bytes let result_bytes = blockchain::eth_call( block.chain_id, &token_addr.to_vec(), @@ -863,9 +863,9 @@ sol! { struct MyModule; impl MyModule { - // Named handler — macro generates the match dispatch + provider injection + // Named handler - macro generates the match dispatch + provider injection async fn on_block(block: Block, provider: &RootProvider) -> Result<()> { - // Full alloy Provider API — natural .await, provider injected + // Full alloy Provider API - natural .await, provider injected let block_num = provider.get_block_number().await?; let eth_balance = provider.get_balance(addr).latest().await?; let code = provider.get_code_at(contract).latest().await?; @@ -904,7 +904,7 @@ Every alloy `Provider` method works. No WIT changes. No host-side per-method cod CoW Protocol's API is REST-based, not JSON-RPC. Two options: -### Option A: Separate REST Interface (Recommended — chosen for 0.2) +### Option A: Separate REST Interface (Recommended - chosen for 0.2) In 0.1 this was two interfaces, `cow` (REST passthrough) and `order` (typed `submit`). 0.2 merges them into a single `cow-api` interface, dropping the `cow::cow::request` triple-stutter: @@ -1023,7 +1023,7 @@ async fn request(&mut self, chain_id: u64, method: String, params: String) } ``` -**Option A is recommended and is what 0.2 ships.** The CoW API is REST, not JSON-RPC — forcing it into JSON-RPC semantics adds a translation layer on both sides. A separate `cow-api` interface keeps the contract explicit and makes it clear in the WIT world what capabilities a module has. It also allows independent evolution — the `chain` interface doesn't need to know about CoW, and vice versa. +**Option A is recommended and is what 0.2 ships.** The CoW API is REST, not JSON-RPC - forcing it into JSON-RPC semantics adds a translation layer on both sides. A separate `cow-api` interface keeps the contract explicit and makes it clear in the WIT world what capabilities a module has. It also allows independent evolution - the `chain` interface doesn't need to know about CoW, and vice versa. ### SDK: `Cow` @@ -1077,7 +1077,7 @@ Usage in a module: async fn on_block(block: Block, provider: &RootProvider) -> Result<()> { let cow = Cow::new(block.chain_id); - // Read chain state via alloy — provider injected by macro + // Read chain state via alloy - provider injected by macro let block_num = provider.get_block_number().await?; // Submit order via CoW API @@ -1086,7 +1086,7 @@ async fn on_block(block: Block, provider: &RootProvider) -> Result<()> { buy_token: weth, sell_amount: U256::from(1_000_000_000), kind: OrderKind::Sell, - // block.timestamp is ms-since-epoch in 0.2 — divide for seconds + // block.timestamp is ms-since-epoch in 0.2 - divide for seconds valid_to: (provider.get_block(block_num.into(), false).await? .unwrap().header.timestamp / 1000) + 300, ..Default::default() @@ -1187,8 +1187,8 @@ use nexum_sdk::testing::MockProvider; #[test] fn test_reads_balance() { - // block_on is still useful in tests — tests are sync by default. - // (Or use #[tokio::test] — MockProvider works with any executor.) + // block_on is still useful in tests - tests are sync by default. + // (Or use #[tokio::test] - MockProvider works with any executor.) let mut mock = MockProvider::new(42161); // Queue mock responses (FIFO) @@ -1232,7 +1232,7 @@ fn test_submits_order() { | **WIT changes for new methods** | None | New function + types per method | | **Host implementation** | ~20 lines total | Per-method impl + dispatch | | **Guest API** | Full alloy Provider (80+ methods) | Only what WIT exposes | -| **alloy compatibility** | Native — IS an alloy transport | Manual ABI encode/decode | +| **alloy compatibility** | Native - IS an alloy transport | Manual ABI encode/decode | | **Type safety at WIT boundary** | Runtime (JSON strings) | Compile-time (WIT types) | | **Method allowlisting** | Runtime string match | Implicit (only exposed methods exist) | | **Debugging** | JSON in/out visible in traces | Structured WIT types in traces | @@ -1240,7 +1240,7 @@ fn test_submits_order() { The primary trade-off is **type safety at the WIT boundary**: JSON strings vs. structured WIT types. This is mitigated by: -1. **Rust guests** use alloy's type system — serialisation errors surface as alloy `TransportError` with clear messages. +1. **Rust guests** use alloy's type system - serialisation errors surface as alloy `TransportError` with clear messages. 2. **Non-Rust guests** (JS, Python, Go) typically work with JSON natively, so JSON strings are actually *more* natural than WIT record types. 3. **Tracing**: the host can log method + params as structured JSON before forwarding, providing equal or better debuggability. @@ -1255,8 +1255,8 @@ For modules and embedders moving from 0.1 to 0.2, follow the [Migration Guide](m | Component | What 0.2 ships | |---|---| | **WIT** | `chain` interface with `request` + additive `request-batch`. `identity` (accounts, sign, sign-typed-data). Merged `cow-api` in `shepherd:cow`. `event-module` imports 6 interfaces: chain, identity, local-store, remote-store, messaging, logging. Plus additive `clock` / `random` / `http` capabilities and the experimental `query-module` world. | -| **Host** | `ChainHost` — one `chain::request` impl that forwards read-only methods to `provider.raw_request_dyn` and delegates signing methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) to the `Identity` backend. Plus `chain::request-batch` that actually pipelines. One `identity::Host` impl delegating to the same backend. One `cow-api::request` + `submit-order` impl forwarding to HTTP client. All host functions return `host-error`. | +| **Host** | `ChainHost` - one `chain::request` impl that forwards read-only methods to `provider.raw_request_dyn` and delegates signing methods (`eth_sendTransaction`, `eth_accounts`, `eth_signTypedData_v4`, `personal_sign`) to the `Identity` backend. Plus `chain::request-batch` that actually pipelines. One `identity::Host` impl delegating to the same backend. One `cow-api::request` + `submit-order` impl forwarding to HTTP client. All host functions return `host-error`. | | **SDK** | `nexum-sdk`: `HostTransport` (alloy `Transport` impl, batches via `chain::request-batch`), `provider()` constructor, `Signer` (typed identity wrapper), `HostError` / `HostErrorKind`. `shepherd-sdk`: `Cow` (extends `nexum-sdk`). `block_on` is internal. | | **`#[nexum::module]` / `#[shepherd::module]` macros** | Named event handlers (`on_block`, `on_logs`, `on_tick`, `on_message`) with generated match dispatch. `async fn` support. Optional `&RootProvider` injection. `#[nexum::module]` for universal modules; `#[shepherd::module]` for CoW modules. | | **Module author experience** | Full alloy `Provider` API via injected provider. Signing via `Signer` or transparently through `chain::request` signing methods. Full CoW API via `Cow`. No match boilerplate. No `block_on`. No manual ABI wrangling for RPC calls. Match on `HostErrorKind` for retry/backoff. | -| **Existing ABI helpers** | Unchanged — `sol!` macro and `alloy-sol-types` still used for contract calldata encoding/decoding. | +| **Existing ABI helpers** | Unchanged - `sol!` macro and `alloy-sol-types` still used for contract calldata encoding/decoding. | diff --git a/docs/08-platform-generalisation.md b/docs/08-platform-generalisation.md index afc50ab..7326873 100755 --- a/docs/08-platform-generalisation.md +++ b/docs/08-platform-generalisation.md @@ -1,17 +1,17 @@ # Platform Generalisation -> **Status (0.2):** Nexum is **designed** to be portable to mobile and browser hosts; the 0.2 **reference runtime is server-only**. The mobile, WebView, and super-app targets in this document describe architectural direction, not shipping artifacts. They remain in the docs because they're load-bearing design — the WIT contract is shaped by the requirement that all four can implement it — but they are **planned** work, conditional on a named design partner for 0.3. See the per-target rows below for current status. +> **Status (0.2):** Nexum is **designed** to be portable to mobile and browser hosts; the 0.2 **reference runtime is server-only**. The mobile, WebView, and super-app targets in this document describe architectural direction, not shipping artifacts. They remain in the docs because they're load-bearing design - the WIT contract is shaped by the requirement that all four can implement it - but they are **planned** work, conditional on a named design partner for 0.3. See the per-target rows below for current status. ## Motivation -The Nexum runtime (docs 01-07) is designed as a server-side Rust binary embedding wasmtime. But the core abstractions — WIT-defined host interfaces, content-addressed module distribution, declarative manifests — are not inherently server-specific. The same module binary, the same packaging, and the same distribution mechanism are intended to serve multiple platform targets: +The Nexum runtime (docs 01-07) is designed as a server-side Rust binary embedding wasmtime. But the core abstractions - WIT-defined host interfaces, content-addressed module distribution, declarative manifests - are not inherently server-specific. The same module binary, the same packaging, and the same distribution mechanism are intended to serve multiple platform targets: -1. **Server runtime** *(shipping in 0.2)* — the current design (Rust/Tokio/wasmtime). Headless automation: blockchain event monitoring, order submission, background computation. -2. **Mobile app (Flutter/Dart)** *(planned — see roadmap)* — a WASM runtime embedded in a native mobile application via FFI. Modules run on-device, backed by local state (SQLite) and RPC over HTTP. -3. **WebView** *(planned — see roadmap)* — a browser engine (V8/JSC/SpiderMonkey) executing WASM natively, with host functions injected from the native layer via a JavaScript bridge. Enables rich web-based UIs with blockchain-native capabilities. -4. **Decentralised super app** *(planned — see roadmap)* — a shell application (mobile or desktop) that dynamically loads modules discovered via ENS and fetched from Swarm. Some modules are headless (automation); others are interactive (UI). All are sandboxed, all are distributed without a central app store. +1. **Server runtime** *(shipping in 0.2)* - the current design (Rust/Tokio/wasmtime). Headless automation: blockchain event monitoring, order submission, background computation. +2. **Mobile app (Flutter/Dart)** *(planned - see roadmap)* - a WASM runtime embedded in a native mobile application via FFI. Modules run on-device, backed by local state (SQLite) and RPC over HTTP. +3. **WebView** *(planned - see roadmap)* - a browser engine (V8/JSC/SpiderMonkey) executing WASM natively, with host functions injected from the native layer via a JavaScript bridge. Enables rich web-based UIs with blockchain-native capabilities. +4. **Decentralised super app** *(planned - see roadmap)* - a shell application (mobile or desktop) that dynamically loads modules discovered via ENS and fetched from Swarm. Some modules are headless (automation); others are interactive (UI). All are sandboxed, all are distributed without a central app store. -The key insight: **the WIT contract is the universal interface**. Any host that implements the required interfaces can run the same module binary. The differences between platforms are in *how* the host implements those interfaces — not in what the module sees. +The key insight: **the WIT contract is the universal interface**. Any host that implements the required interfaces can run the same module binary. The differences between platforms are in *how* the host implements those interfaces - not in what the module sees. This document defines the layered architecture that enables this generalisation and specifies the universal interface set. The 0.2 server runtime is the first host implementation; the experimental `nexum:host/query-module` WIT world (published but unhosted in 0.2) exists to give mobile/wallet embedders a stable target to implement against before 0.3. @@ -22,7 +22,7 @@ Before diving into WIT definitions, the universal runtime is built on six primit | Primitive | Interface | Backed by | Purpose | |-----------|-----------|-----------|---------| | **Chain** | `chain` | JSON-RPC (eth_*) | Read/write blockchain consensus state | -| **Identity** | `identity` | Keystore / KMS / device keychain / wallet extension | Cryptographic identity — key management and signing | +| **Identity** | `identity` | Keystore / KMS / device keychain / wallet extension | Cryptographic identity - key management and signing | | **Local Store** | `local-store` | redb / SQLite / IndexedDB | Per-module private persistence on the device | | **Remote Store** | `remote-store` | Ethereum Swarm | Decentralised content-addressed storage | | **Messaging** | `messaging` | Waku | Decentralised pub/sub messaging | @@ -30,16 +30,16 @@ Before diving into WIT definitions, the universal runtime is built on six primit These six primitives are orthogonal: -- **Chain** is the source of truth — the blockchain consensus state. Modules read chain state and (indirectly) write to it via order submission or transactions. -- **Identity** is cryptographic agency — key management and signing. Modules can enumerate available accounts and request signatures (ECDSA secp256k1 by default, extensible). The `chain` host implementation depends on `identity` internally — signing RPC methods (e.g. `eth_sendTransaction`) delegate to `identity` for the actual signature. -- **Local Store** is the module's private scratchpad — fast, local, scoped to one module on one device. Does not replicate. -- **Remote Store** is shared persistent content — content-addressed, decentralised, survives independent of any device. Any module on any device can read what another module wrote. -- **Messaging** is real-time communication — ephemeral pub/sub messages between modules, devices, or users. Unlike remote store (persistent, content-addressed), messaging is transient and topic-based. -- **Logging** is diagnostics — one-way output for debugging and monitoring. Not a data channel. +- **Chain** is the source of truth - the blockchain consensus state. Modules read chain state and (indirectly) write to it via order submission or transactions. +- **Identity** is cryptographic agency - key management and signing. Modules can enumerate available accounts and request signatures (ECDSA secp256k1 by default, extensible). The `chain` host implementation depends on `identity` internally - signing RPC methods (e.g. `eth_sendTransaction`) delegate to `identity` for the actual signature. +- **Local Store** is the module's private scratchpad - fast, local, scoped to one module on one device. Does not replicate. +- **Remote Store** is shared persistent content - content-addressed, decentralised, survives independent of any device. Any module on any device can read what another module wrote. +- **Messaging** is real-time communication - ephemeral pub/sub messages between modules, devices, or users. Unlike remote store (persistent, content-addressed), messaging is transient and topic-based. +- **Logging** is diagnostics - one-way output for debugging and monitoring. Not a data channel. Together they cover the full spectrum: persistent truth (chain), cryptographic agency (identity), local scratch (local-store), shared content (remote-store), real-time coordination (messaging), and diagnostics (logging). -The 0.2 `event-module` world imports all six. (In 0.1 the WIT inadvertently omitted `identity` from the world definition despite the docs claiming six primitives; 0.2 makes the contract match the taxonomy.) Three additional **additive** capabilities — `clock`, `random`, and `http` (allowlisted) — are available via the manifest's `[capabilities]` section but are not part of the six-primitive core. +The 0.2 `event-module` world imports all six. (In 0.1 the WIT inadvertently omitted `identity` from the world definition despite the docs claiming six primitives; 0.2 makes the contract match the taxonomy.) Three additional **additive** capabilities - `clock`, `random`, and `http` (allowlisted) - are available via the manifest's `[capabilities]` section but are not part of the six-primitive core. ## Architectural Principle: Layered WIT Worlds @@ -48,22 +48,22 @@ The current `shepherd` world conflates universal blockchain runtime capabilities ```mermaid graph TD subgraph L3["Layer 3: Application-Specific Worlds"] - COW["shepherd:cow — cow + order (CoW Protocol automation)"] - DEFI["myapp:defi — vault + strategy (DeFi yield app)"] - GAME["game:engine — physics + assets (on-chain game)"] + COW["shepherd:cow - cow + order (CoW Protocol automation)"] + DEFI["myapp:defi - vault + strategy (DeFi yield app)"] + GAME["game:engine - physics + assets (on-chain game)"] end subgraph L2["Layer 2: Capability Extensions (optional, composable)"] - UI["ui — user interface bridge (interactive modules)"] + UI["ui - user interface bridge (interactive modules)"] end subgraph L1["Layer 1: Universal Runtime Interfaces"] - CSN["chain — consensus access (JSON-RPC passthrough)"] - ID["identity — cryptographic identity (key management, signing)"] - LS["local-store — local key-value persistence"] - RS["remote-store — decentralised content-addressed storage"] - MSG["messaging — decentralised pub/sub messaging"] - LOG["logging — structured logging"] + CSN["chain - consensus access (JSON-RPC passthrough)"] + ID["identity - cryptographic identity (key management, signing)"] + LS["local-store - local key-value persistence"] + RS["remote-store - decentralised content-addressed storage"] + MSG["messaging - decentralised pub/sub messaging"] + LOG["logging - structured logging"] EXP["Exports: init(config) + on-event(event)"] end @@ -75,11 +75,11 @@ Each layer builds on the one below via WIT `include`. A module compiled against ## Layer 1: Universal Interfaces -These six interfaces form the universal runtime contract. Any platform — server, mobile, WebView, desktop — can implement them. +These six interfaces form the universal runtime contract. Any platform - server, mobile, WebView, desktop - can implement them. -### `chain` — Consensus Access +### `chain` - Consensus Access -The module's window into blockchain consensus. A single generic function that forwards JSON-RPC requests to the host's provider infrastructure, plus an additive batched variant. The host decides *how* to reach the chain — the module only specifies *what* to ask. +The module's window into blockchain consensus. A single generic function that forwards JSON-RPC requests to the host's provider infrastructure, plus an additive batched variant. The host decides *how* to reach the chain - the module only specifies *what* to ask. ```wit interface chain { @@ -112,13 +112,13 @@ interface chain { | WebView | JavaScript bridge -> `window.ethereum` (injected wallet) or native HTTP via message channel | | Super app | Same as mobile, with per-module chain permissions | -The Rust SDK's `HostTransport` (doc 07) works identically on all platforms — it implements alloy's `Transport` trait over `chain::request`, so module authors get the full alloy `Provider` API regardless of where the module runs. +The Rust SDK's `HostTransport` (doc 07) works identically on all platforms - it implements alloy's `Transport` trait over `chain::request`, so module authors get the full alloy `Provider` API regardless of where the module runs. -### `identity` — Cryptographic Identity +### `identity` - Cryptographic Identity Provides key management and signing capabilities to modules. ECDSA secp256k1 by default (the Ethereum standard), extensible to other schemes. Modules can enumerate available accounts and request signatures over arbitrary data. -The `chain` host implementation depends on `identity` internally — signing RPC methods such as `eth_sendTransaction` or `eth_signTypedData_v4` delegate to `identity` for the actual cryptographic signature. Modules can also import `identity` directly for raw signing operations outside of JSON-RPC (e.g. signing EIP-712 typed data for off-chain order submission). +The `chain` host implementation depends on `identity` internally - signing RPC methods such as `eth_sendTransaction` or `eth_signTypedData_v4` delegate to `identity` for the actual cryptographic signature. Modules can also import `identity` directly for raw signing operations outside of JSON-RPC (e.g. signing EIP-712 typed data for off-chain order submission). ```wit interface identity { @@ -156,11 +156,11 @@ The `chain` host implementation uses `identity` internally when it encounters si 2. Calls `identity::sign` to produce the signature. 3. Sends the signed transaction via the provider. -This means modules that only need to sign transactions via standard JSON-RPC methods do not need to import `identity` directly — `chain` handles it transparently. Modules that need raw signing (e.g. off-chain message signing for order submission, attestations, or custom protocols) import `identity` explicitly. +This means modules that only need to sign transactions via standard JSON-RPC methods do not need to import `identity` directly - `chain` handles it transparently. Modules that need raw signing (e.g. off-chain message signing for order submission, attestations, or custom protocols) import `identity` explicitly. -### `local-store` — Local Key-Value Persistence +### `local-store` - Local Key-Value Persistence -The module's private scratchpad. **Local to the device/process** — does not replicate, sync, or share across instances. Scoped to one module: module A cannot read module B's local state. +The module's private scratchpad. **Local to the device/process** - does not replicate, sync, or share across instances. Scoped to one module: module A cannot read module B's local state. ```wit interface local-store { @@ -190,15 +190,15 @@ interface local-store { | WebView | IndexedDB (per-module object store) or `localStorage` | | Super app | SQLite (shared database, per-module namespace isolation) | -The semantics are deliberately minimal — get, set, delete, prefix scan. This is the LCD (lowest common denominator) that every platform can implement efficiently. Advanced features (transactions, MVCC, crash-safety) are host-specific and not exposed in the WIT. +The semantics are deliberately minimal - get, set, delete, prefix scan. This is the LCD (lowest common denominator) that every platform can implement efficiently. Advanced features (transactions, MVCC, crash-safety) are host-specific and not exposed in the WIT. The server runtime's all-or-nothing transactional semantics (doc 04) remain an implementation detail of the Nexum host, not a guarantee modules can rely on across platforms. Modules that need stronger guarantees should design for idempotency. -### `remote-store` — Decentralised Content-Addressed Storage +### `remote-store` - Decentralised Content-Addressed Storage -Backed by Ethereum Swarm. Provides decentralised persistence beyond the local device — content-addressed, censorship-resistant, and accessible from any host on any device. +Backed by Ethereum Swarm. Provides decentralised persistence beyond the local device - content-addressed, censorship-resistant, and accessible from any host on any device. -Swarm is both the distribution mechanism (modules are fetched from Swarm) and a runtime capability. This interface closes the loop — modules can publish to the same network they were distributed through. +Swarm is both the distribution mechanism (modules are fetched from Swarm) and a runtime capability. This interface closes the loop - modules can publish to the same network they were distributed through. ```wit interface remote-store { @@ -208,7 +208,7 @@ interface remote-store { /// Returns the 32-byte content reference (Swarm address). /// /// The host routes to its configured Bee node. Postage batch - /// management is the host's responsibility — the module only + /// management is the host's responsibility - the module only /// provides data and gets back a reference. upload: func(data: list) -> result, host-error>; @@ -235,7 +235,7 @@ interface remote-store { /// /// The host signs the feed update with its configured identity /// (Bee node's Ethereum key). Only the host's own feeds can be - /// updated — the owner is implicit (the host's address). + /// updated - the owner is implicit (the host's address). /// /// `topic`: 32-byte topic hash. /// `data`: the payload to publish. @@ -260,13 +260,13 @@ interface remote-store { **Why remote-store as a universal interface:** - **Decentralised persistence.** `local-store` is device-local. `remote-store` gives modules access to content-addressed storage that persists independent of any single device. -- **Content distribution.** Modules can publish data (feeds, references) that other modules or users can consume — without a central server. -- **Cross-device coordination.** Two instances of the same module on different devices can share data via feed topics — one writes via `write-feed`, the other reads via `read-feed`. +- **Content distribution.** Modules can publish data (feeds, references) that other modules or users can consume - without a central server. +- **Cross-device coordination.** Two instances of the same module on different devices can share data via feed topics - one writes via `write-feed`, the other reads via `read-feed`. - **Consistency with distribution model.** Modules are already fetched from Swarm (doc 02, 03). Exposing `remote-store` at runtime means modules participate in the same content-addressed network they were distributed through. -### `messaging` — Decentralised Messaging +### `messaging` - Decentralised Messaging -Backed by Waku. Provides real-time, privacy-preserving pub/sub messaging between modules, devices, and users. Unlike `remote-store` (persistent, content-addressed), `messaging` is transient and topic-based — fire-and-forget messages on content topics. +Backed by Waku. Provides real-time, privacy-preserving pub/sub messaging between modules, devices, and users. Unlike `remote-store` (persistent, content-addressed), `messaging` is transient and topic-based - fire-and-forget messages on content topics. ```wit interface messaging { @@ -345,11 +345,11 @@ This follows the same pattern as all other event sources: sending uses the impor - **Module-to-module communication.** Two modules on different devices can exchange real-time messages via shared content topics. The TWAP monitor on a server can notify a mobile dashboard module that a new part was posted. - **User notifications.** A headless server module can publish an alert to a content topic; the user's mobile app module subscribes and displays a notification. -- **Decentralised coordination.** Multiple instances of the same module (e.g. running on different operator nodes) can coordinate via messaging — leader election, work distribution, heartbeats. +- **Decentralised coordination.** Multiple instances of the same module (e.g. running on different operator nodes) can coordinate via messaging - leader election, work distribution, heartbeats. - **Privacy.** Waku supports encrypted messaging and ephemeral relay. Modules can communicate without exposing data to the public chain. - **Complementary to remote-store.** `remote-store` is for persistent content (data that should survive). `messaging` is for ephemeral signals (notifications, coordination, real-time feeds). Together they cover the full persistence spectrum. -### `logging` — Structured Logging +### `logging` - Structured Logging Unchanged from the current design: @@ -427,7 +427,7 @@ interface types { // ... chain, identity, local-store, remote-store, messaging, logging interfaces as above ... -/// Event-driven module — automation, background processing. +/// Event-driven module - automation, background processing. /// No UI capabilities. Runs on any conforming host. Six imports in 0.2. world event-module { import chain; @@ -446,7 +446,7 @@ A module compiled against `nexum:host/event-module` is the **maximally portable* ## Layer 2: UI Interface -Interactive modules — those with a user-facing presence in a super app or WebView container — import the `ui` interface in addition to the Layer 1 universals. +Interactive modules - those with a user-facing presence in a super app or WebView container - import the `ui` interface in addition to the Layer 1 universals. ### Design Approach @@ -507,7 +507,7 @@ interface ui { Interactive modules export additional lifecycle hooks beyond `init` and `on-event`: ```wit -/// Interactive module — has a UI presence. +/// Interactive module - has a UI presence. world app-module { include event-module; import ui; @@ -605,7 +605,7 @@ world yield-module { } ``` -The `include` mechanism ensures that any domain-specific module inherits the full universal interface set. A `shepherd` module can call `chain::request`, `identity::sign`, `local-store::get`, `remote-store::upload`, `messaging::publish`, and `logging::log` — plus the CoW-specific `cow-api::request` and `cow-api::submit-order`. +The `include` mechanism ensures that any domain-specific module inherits the full universal interface set. A `shepherd` module can call `chain::request`, `identity::sign`, `local-store::get`, `remote-store::upload`, `messaging::publish`, and `logging::log` - plus the CoW-specific `cow-api::request` and `cow-api::submit-order`. ## Complete WIT Package Layout @@ -625,7 +625,7 @@ wit/ │ ├── ui.wit # ui interface + host-capabilities (planned hosts only) │ ├── event-module.wit # event-module world (6 imports) │ ├── query-module.wit # experimental: query-module world (no host impl in 0.2) -│ └── app-module.wit # app-module world (includes ui) — design only +│ └── app-module.wit # app-module world (includes ui) - design only │ └── shepherd-cow/ ├── cow-api.wit # merged cow-api interface (request + submit-order) @@ -636,23 +636,23 @@ The `nexum-host` package is domain-agnostic and reusable. The `shepherd-cow` pac ## Platform Targets -### Server Runtime (Reference Implementation — Nexum) +### Server Runtime (Reference Implementation - Nexum) This is the current design (docs 01-07), adapted for the layered WIT. Shepherd is the Nexum distribution with CoW Protocol support. | Interface | Implementation | |-----------|---------------| | `chain` | alloy provider with tower middleware (timeout, retry, rate-limit, fallback) | -| `identity` | Keystore file, AWS KMS, or HSM — operator-configured signing backend | +| `identity` | Keystore file, AWS KMS, or HSM - operator-configured signing backend | | `local-store` | redb (per-module database file, ACID, MVCC, crash-safe) | -| `remote-store` | Bee API (`http://localhost:1633`) — operator runs a Bee node | +| `remote-store` | Bee API (`http://localhost:1633`) - operator runs a Bee node | | `messaging` | Waku node (nwaku) via JSON-RPC or REST API | | `logging` | `tracing` crate -> JSON structured logs | | `cow-api` | reqwest HTTP client -> CoW Protocol API (REST passthrough + typed `submit-order`) | | Event sources | `eth_subscribe` (blocks, logs), cron (Tokio interval), Waku relay (messages) | | WASM engine | wasmtime 45.x (Component Model, fuel, epoch metering) | -### Mobile App (Flutter/Dart) — Planned +### Mobile App (Flutter/Dart) - Planned > **Status:** No mobile host ships in 0.2. The design below is the target architecture for a future release (0.3+, conditional on a named design partner). It's retained because the WIT contract was shaped to make this implementation possible, and the `query-module` world in 0.2 is the experimental contract a mobile/wallet embedder would target. @@ -692,7 +692,7 @@ flowchart TD |--------|----------------|----------------|-------| | wasmtime (C API) | Full | aarch64 (iOS/Android ARM64) | Best compatibility, largest binary size (~15 MB) | | wasmer | Partial | Good (wasmer_dart exists) | Component Model support is partial | -| wasm3 | None | Excellent (tiny C library, ~100 KB) | Interpreter only, no Component Model — requires core module + shim | +| wasm3 | None | Excellent (tiny C library, ~100 KB) | Interpreter only, no Component Model - requires core module + shim | For full Component Model support (identical module binaries across server and mobile), **wasmtime via C API** is the recommended path. Dart's FFI (`dart:ffi`) can call the wasmtime C API directly. The binary size cost (~15 MB) is acceptable for a mobile app. @@ -703,7 +703,7 @@ For full Component Model support (identical module binaries across server and mo - **Connectivity.** Mobile networks are intermittent. Host functions should handle offline gracefully (queue requests, retry on reconnect). - **Waku light client.** Mobile devices should use Waku's light push and filter protocols rather than full relay to minimise bandwidth and battery consumption. -### WebView (Browser Engine + Injected Host Functions) — Planned +### WebView (Browser Engine + Injected Host Functions) - Planned > **Status:** No WebView host ships in 0.2. The architecture below describes a future target. The `jco`-based transpilation path is the strongest candidate, but it depends on Component Model browser support stabilising and on a concrete embedder design partner. @@ -743,7 +743,7 @@ Browsers don't natively support the WASM Component Model (as of early 2026). Two 2. **Core module variant.** Compile the module as a core WASM module (not a component) with a JS shim layer that maps the WIT interface to JavaScript imports. This requires a separate build target but avoids the `jco` dependency. -Approach 1 is preferred — it preserves the single-artifact property (one `.wasm` component, multiple platforms). +Approach 1 is preferred - it preserves the single-artifact property (one `.wasm` component, multiple platforms). **WebView-specific capability: `window.ethereum`** @@ -764,22 +764,22 @@ chain: { } ``` -This is powerful: the same module that runs headless on a server (reading chain state via a configured RPC endpoint) can run in a WebView and read chain state via the user's wallet — gaining access to the user's connected accounts and signing capabilities. +This is powerful: the same module that runs headless on a server (reading chain state via a configured RPC endpoint) can run in a WebView and read chain state via the user's wallet - gaining access to the user's connected accounts and signing capabilities. Similarly, the `identity` interface in a WebView context can delegate to `window.ethereum` for account enumeration and signing, providing a seamless bridge between the module's signing needs and the user's wallet extension. **WebView-specific capability: `js-waku`** -For messaging in the browser, `js-waku` provides a pure JavaScript Waku client. The `messaging` host function can route through `js-waku` directly in the WebView without needing the native bridge — peer-to-peer messaging from the browser. +For messaging in the browser, `js-waku` provides a pure JavaScript Waku client. The `messaging` host function can route through `js-waku` directly in the WebView without needing the native bridge - peer-to-peer messaging from the browser. -### Decentralised Super App — Planned +### Decentralised Super App - Planned > **Status:** The super app is the convergence of the mobile and WebView targets. No super-app host ships in 0.2. The content below describes the target architecture for a future release once mobile and WebView are live. The super app is the convergence of all targets. A native shell (Flutter) that would: -1. **Discover modules** via ENS (doc 03) — the same discovery mechanism as the server runtime. -2. **Fetch modules** from Swarm/IPFS — the same content-addressed distribution. +1. **Discover modules** via ENS (doc 03) - the same discovery mechanism as the server runtime. +2. **Fetch modules** from Swarm/IPFS - the same content-addressed distribution. 3. **Run event-driven modules** in an embedded WASM runtime (automation, background tasks). 4. **Run interactive modules** in WebViews (UI, dashboards, transaction builders). 5. **Provide the universal interfaces** to all modules (chain, identity, local-store, remote-store, messaging, logging). @@ -846,40 +846,40 @@ The super app adds a capability-grant layer on top of the WIT world. When a modu ``` "TWAP Monitor" requests: - ✓ chain — read blockchain state (chains: 42161) - ✓ identity — sign with your accounts - ✓ local-store — store data on your device - ✓ remote-store — read/write to Swarm network - ✓ messaging — send/receive messages (topics: /nexum/1/twap-*) - ✗ ui — (not requested — event-driven module) - ✓ cow-api — interact with CoW Protocol API and submit orders + ✓ chain - read blockchain state (chains: 42161) + ✓ identity - sign with your accounts + ✓ local-store - store data on your device + ✓ remote-store - read/write to Swarm network + ✓ messaging - send/receive messages (topics: /nexum/1/twap-*) + ✗ ui - (not requested - event-driven module) + ✓ cow-api - interact with CoW Protocol API and submit orders [Allow] [Deny] ``` -The host only links interfaces the user has approved. A module that doesn't import `messaging` structurally cannot publish messages — the same structural sandboxing property that the server runtime uses (doc 01). +The host only links interfaces the user has approved. A module that doesn't import `messaging` structurally cannot publish messages - the same structural sandboxing property that the server runtime uses (doc 01). ## Host Adapter Specification -Any platform that wants to run modules must implement the **Host Adapter** — the set of host functions backing the WIT interfaces. The specification defines the contract: +Any platform that wants to run modules must implement the **Host Adapter** - the set of host functions backing the WIT interfaces. The specification defines the contract: ### Required Behaviours -In 0.2 every host function returns `result`. The `host-error.kind` discriminant (`unsupported`, `unavailable`, `denied`, `rate-limited`, `timeout`, `invalid-input`, `internal`) is normative — embedders MUST pick the most specific kind for each backend failure. See the [migration guide §2](migration/0.1-to-0.2.md#2-error-model-unification-both) for the embedder-side mapping table. +In 0.2 every host function returns `result`. The `host-error.kind` discriminant (`unsupported`, `unavailable`, `denied`, `rate-limited`, `timeout`, `invalid-input`, `internal`) is normative - embedders MUST pick the most specific kind for each backend failure. See the [migration guide §2](migration/0.1-to-0.2.md#2-error-model-unification-both) for the embedder-side mapping table. **`chain::request` / `chain::request-batch`** (Chain) - MUST forward the JSON-RPC request to a provider for the given chain. - MUST return the JSON-encoded result (the `result` field from the JSON-RPC response). - MUST return `host-error` with `domain = "chain"` for provider errors, method-not-found, and transport failures. Use `kind: invalid-input` for method-not-found, `unavailable`/`timeout` for transport, `rate-limited` for 429s, `denied` for 401/403. - SHOULD enforce a method allowlist (configurable by the operator/user). -- MAY apply middleware (timeout, retry, rate-limit, fallback) — this is platform-specific. +- MAY apply middleware (timeout, retry, rate-limit, fallback) - this is platform-specific. **`identity::accounts/sign/sign-typed-data`** (Identity) - `accounts` MUST return the list of available account identifiers (addresses) for the current host configuration. - `sign` MUST produce a valid cryptographic signature over the provided data using the specified account's private key. - `sign-typed-data` MUST produce a valid EIP-712 signature over the provided typed data structure. - MUST return `host-error` with `domain = "identity"`. User rejection is `kind: denied`; unknown account is `kind: invalid-input`; backend offline is `kind: unavailable`. -- MAY prompt the user for approval before signing (platform-dependent — e.g. wallet extension popup in WebView, biometric prompt on mobile). +- MAY prompt the user for approval before signing (platform-dependent - e.g. wallet extension popup in WebView, biometric prompt on mobile). - SHOULD NOT expose private key material to the module. The module sends data in, gets a signature out. **`local-store::get/set/delete/list-keys`** @@ -970,13 +970,13 @@ graph TD ShepherdSDK -->|"extends"| NexumSDK ``` -- **`nexum-sdk`** — the universal Rust SDK for any module targeting `nexum:host/event-module`. Provides `HostTransport` (alloy `Transport` trait over `chain::request` / `chain::request-batch`), `provider(chain_id)`, `TypedState` (serde over `local-store`), `RemoteStore` (typed wrapper over `remote-store`), `Messaging` (typed wrapper over `messaging`), `Signer` (typed wrapper over `identity`), logging macros, `HostError`/`HostErrorKind`. Any module author — CoW, DeFi, gaming, whatever — uses this. +- **`nexum-sdk`** - the universal Rust SDK for any module targeting `nexum:host/event-module`. Provides `HostTransport` (alloy `Transport` trait over `chain::request` / `chain::request-batch`), `provider(chain_id)`, `TypedState` (serde over `local-store`), `RemoteStore` (typed wrapper over `remote-store`), `Messaging` (typed wrapper over `messaging`), `Signer` (typed wrapper over `identity`), logging macros, `HostError`/`HostErrorKind`. Any module author - CoW, DeFi, gaming, whatever - uses this. -- **`shepherd-sdk`** — extends `nexum-sdk` with the typed `Cow` client and the `#[shepherd::module]` proc macro (which generates the `cow-api` import in addition to the universals). +- **`shepherd-sdk`** - extends `nexum-sdk` with the typed `Cow` client and the `#[shepherd::module]` proc macro (which generates the `cow-api` import in addition to the universals). A module author building a generic blockchain automation module depends only on `nexum-sdk`. A module author building a CoW Protocol module depends on `shepherd-sdk` (which re-exports `nexum-sdk`). -For **non-Rust** module authors (JavaScript, Python, Go, C++), the SDK is unnecessary — they use `wit-bindgen` directly against the WIT package for their target world. The WIT is the universal contract; the SDK is a Rust ergonomics layer on top. +For **non-Rust** module authors (JavaScript, Python, Go, C++), the SDK is unnecessary - they use `wit-bindgen` directly against the WIT package for their target world. The WIT is the universal contract; the SDK is a Rust ergonomics layer on top. ## Migration from 0.1 @@ -1006,10 +1006,10 @@ For the full 0.1 → 0.2 rename and behaviour change list, see the [Migration Gu | Concept | Scope | |---------|-------| -| `nexum:host` WIT package | Universal — any blockchain app, any platform | -| `event-module` world (0.2, shipping) | Event-driven modules — server today, mobile/background planned | -| `query-module` world (0.2 experimental) | Request/response modules — WIT published, no host impl in 0.2 | -| `app-module` world | Interactive modules — design only; planned hosts | +| `nexum:host` WIT package | Universal - any blockchain app, any platform | +| `event-module` world (0.2, shipping) | Event-driven modules - server today, mobile/background planned | +| `query-module` world (0.2 experimental) | Request/response modules - WIT published, no host impl in 0.2 | +| `app-module` world | Interactive modules - design only; planned hosts | | `shepherd:cow` WIT package | CoW Protocol domain extension | | `shepherd` world | CoW automation modules (includes event-module + cow-api) | | `nexum-sdk` crate | Universal Rust SDK (HostTransport, TypedState, RemoteStore, Messaging, Signer, HostError) | @@ -1017,4 +1017,4 @@ For the full 0.1 → 0.2 rename and behaviour change list, see the [Migration Gu | Content-addressed distribution | Platform-agnostic (Swarm/IPFS, ENS discovery, hash verification) | | Host Adapter | Platform-specific implementation of universal interfaces | -The module binary is the portable artifact. The WIT contract is the universal interface. The host adapter is the platform-specific implementation. Everything else — packaging, distribution, discovery, SDK — layers cleanly on top. +The module binary is the portable artifact. The WIT contract is the universal interface. The host adapter is the platform-specific implementation. Everything else - packaging, distribution, discovery, SDK - layers cleanly on top. diff --git a/docs/adr/0001-engine-toml-separate-from-nexum-toml.md b/docs/adr/0001-engine-toml-separate-from-nexum-toml.md new file mode 100644 index 0000000..3e54548 --- /dev/null +++ b/docs/adr/0001-engine-toml-separate-from-nexum-toml.md @@ -0,0 +1,36 @@ +--- +status: proposed +implemented-in: nullislabs/shepherd#8, nullislabs/shepherd#9 +--- + +# Operator config (`engine.toml`) is separate from module manifest (`module.toml`) + +## Context + +The engine needs two distinct kinds of configuration: what the **operator** decides at deployment time (which chains to connect to, where the local-store database lives, which modules to boot) and what the **module developer** declares at build time (required and optional capabilities, HTTP allowlist, module-specific config keys). These have different reviewers, different threat models, and change on different cadences. + +The filenames need to signal who owns each file directly. An operator opening a config file should know without prior context whether the file is their concern or the module developer's. A name like `nexum.toml` requires the reader to know that "nexum" refers to the runtime that hosts the module, which is one indirection too many; `module.toml` reads as "the module's manifest" with no prior context. + +## Decision + +Two distinct files, distinct schemas, distinct loaders: + +- **`engine.toml`** - operator-owned, lives next to the engine binary or pointed to by `--engine-config`. Defines `[engine]` (state_dir, log_level), `[chains.]` (rpc_url), and `[[modules]]` (path, manifest). Loaded by `engine_config::EngineConfig::load`. +- **`module.toml`** - module-developer-owned, ships in the module's bundle alongside its `.wasm` component. Defines `[module]`, `[capabilities]` (required, optional, http allowlist), `[config]`. Loaded by `manifest::load`. + +The engine config carries the path to each module's manifest; the two never collapse into one file. The names `engine.toml` and `module.toml` map directly onto the two distinct roles, so a reader reaching either file knows whose concerns it covers. + +## Considered options + +- **Single `shepherd.toml` with `[engine]`, `[chains]`, `[[modules]]` *and* nested `[modules..capabilities]` per module.** Rejected: conflates operator and developer concerns. A module's capability declaration is a property of the build, not the deployment - it belongs in the artifact, not in the operator's local file. Auditing a module's capabilities also becomes a per-deployment exercise instead of a property visible in the published bundle. +- **Keep the `nexum.toml` filename for the module manifest.** Rejected: the name does not signal who owns the file (engine vs module). `module.toml` reads as "the module's manifest" without prior context. +- **`module.toml` inside the engine config (module entries embed it inline).** Rejected for the same reason as the single-file proposal; also bloats `engine.toml`. +- **Drop `engine.toml` entirely; pass everything as CLI flags or env vars.** Rejected: per-chain RPC URLs and module lists are awkward as flags, and `RUST_LOG` already covers the only thing that env vars naturally express. + +## Consequences + +- A deployment needs both files. A missing `engine.toml` falls back to "no chains, default state_dir" - the example logging module still runs; cow-api / chain backends report `unsupported`. +- A missing `module.toml` triggers the 0.1-compat deprecation warning in `manifest::fallback_manifest()` (defined in `crates/nexum-engine/src/manifest.rs`) and treats every linked capability as required. This fallback is scheduled for removal in 0.3 per `docs/migration/0.1-to-0.2.md`. +- Module-bundle redistribution carries `module.toml` with the artifact; engines do not need to ship templates. +- Future content-addressed module distribution (0.3) embeds `module.toml` in the bundle hash; `engine.toml` references modules by content address rather than filesystem path. The split survives that migration unchanged. +- Implementation impact: `crates/nexum-engine/src/manifest.rs` and `engine_config.rs` need to update the filename lookup from `nexum.toml` to `module.toml`. The 0.1-compat fallback in `manifest::fallback_manifest()` should accept both names during the transition; after 0.3 only `module.toml` is recognised. diff --git a/docs/adr/0002-provider-pool-transport-by-scheme.md b/docs/adr/0002-provider-pool-transport-by-scheme.md new file mode 100644 index 0000000..5506879 --- /dev/null +++ b/docs/adr/0002-provider-pool-transport-by-scheme.md @@ -0,0 +1,39 @@ +--- +status: proposed +implemented-in: nullislabs/shepherd#8, nullislabs/shepherd#9 +--- + +# Per-chain alloy provider transport selected by URL scheme + +## Context + +`nexum:host/chain` covers both generic JSON-RPC dispatch (`request`) and event subscriptions (`subscribe-blocks`, `subscribe-logs`). Subscriptions require a duplex transport (`eth_subscribe` is push-only over a long-lived connection); request/response works on either HTTP or WebSocket. The operator configures one RPC endpoint per chain in `engine.toml`; the engine has to decide which alloy transport to use. + +## Decision + +The `ProviderPool::from_config` constructor reads each chain's `rpc_url` and switches by URL scheme prefix: + +- `ws://` or `wss://` → `ProviderBuilder::new().connect_ws(WsConnect::new(url))`. Pubsub transport. Subscriptions and request/response both work. **This is the recommended configuration for any chain a module subscribes to.** +- `http://` or `https://` → `ProviderBuilder::new().connect_http(parsed)`. HTTP transport. Request/response only; `subscribe-blocks` and `subscribe-logs` surface as `host-error.unsupported` to the guest. + +Both transports erase to `DynProvider` so the rest of the engine is transport-agnostic. + +Alloy is capable of emulating `eth_subscribe` on HTTP via polling, but this is intentionally **not** enabled. The engine takes an opinionated stance favouring WebSockets for subscriptions; operators who want push-based events configure WSS endpoints. HTTP-only chains are supported for `request` traffic but not for subscriptions. + +## Non-goals + +- **RPC failover, load balancing, and retry policies are explicitly out of scope for the engine.** This logic lives in upstream crates (alloy ships tower-style middleware for timeout / retry / rate-limit / fallback endpoint). The engine does not roll its own. Operators wanting failover configure it via alloy provider builders before passing them through, or rely on the provider's own fallback (Alchemy, Infura, etc. handle it server-side). +- Re-routing requests across chains, rebalancing across pools within a chain, and similar provider-management concerns are likewise alloy's responsibility. + +## Considered options + +- **Force WSS everywhere.** Rejected: many providers (Alchemy, Infura, self-hosted RPC) expose HTTP-only on free tiers, and modules that only need `request` (no subscriptions) shouldn't be blocked by a WSS requirement. +- **Explicit `transport = "ws" | "http"` field per chain in `engine.toml`.** Rejected for 0.2: redundant with the URL scheme, and operators already distinguish `wss://` from `https://` endpoints when copying them from their RPC provider's dashboard. Revisit if we add IPC (`/path/to/geth.ipc`) - scheme alone won't carry that. +- **Open both an HTTP and a WSS connection per chain.** Rejected: doubles connection count for the common case where one endpoint serves both, and forces operators to provide two URLs even when their provider returns identical data on both. + +## Consequences + +- Operators that need subscriptions must supply WSS URLs; HTTP-only chains downgrade to request-only mode at the host call boundary. +- Connection failures at boot are fatal (the engine refuses to start with a broken chain). This is intentional - silent fall-back to a half-functioning state masks misconfiguration that a module then rediscovers at first event. +- Adding IPC support is additive: extend the scheme match with `/` / `file://` and call `connect_ipc`. +- The `DynProvider` erasure costs a virtual dispatch per call - a measurable concern at scale, deferred to M4 if profiling shows it. diff --git a/docs/adr/0003-local-store-namespacing.md b/docs/adr/0003-local-store-namespacing.md new file mode 100644 index 0000000..a7a5ac1 --- /dev/null +++ b/docs/adr/0003-local-store-namespacing.md @@ -0,0 +1,49 @@ +--- +status: proposed +implemented-in: nullislabs/shepherd#8 +--- + +# Per-module namespacing in `local-store` via 32-byte deterministic hash prefix + +## Context + +`nexum:host/local-store` is a key-value store shared across all modules the engine runs. Two modules using the same key string (e.g. `"last-block"`) must see disjoint values; one module must never read or overwrite another's data. The engine knows each module's identity at instantiation time, so namespacing is a host-side concern. + +Two properties matter for the namespace prefix: + +1. **Deterministic and unspoofable.** An arbitrary `module_name` string read out of `module.toml` lets a malicious or careless operator give two modules the same name and have one read the other's state. A fixed-size hash derived from the module's canonical identity is harder to collide and removes the operator-supplied-text attack surface. +2. **Composes with ENS-based module discovery** (per `docs/03-module-discovery.md`): when a module is identified by an ENS name (e.g. `twap-monitor.shepherd.eth`), the ENS namehash is a natural prefix. ENS TXT records pinning the `.wasm` content hash provide a separate verification path against the loaded bundle. + +## Decision + +Single redb database file at `EngineConfig.engine.state_dir`, single shared table `nexum:local-store`. Every key handed to redb is composed host-side as: + +``` +[32-byte namespace prefix][raw key bytes] +``` + +The 32-byte prefix is computed deterministically from the module's canonical identity: + +- **ENS-identified modules** (M3+, per `docs/03`): prefix is `ens_namehash(name)` (EIP-137), e.g. `namehash("twap-monitor.shepherd.eth")`. +- **Locally-loaded modules** (current 0.2 scope, no ENS): prefix is `keccak256(module_name)` where `module_name` comes from `module.toml`'s `[module].name` field. + +Both produce a 32-byte digest with the same domain, so a module loaded locally during development and later published under an ENS name can keep its existing state by registering an alias (`alias = keccak256(name)`) the engine recognises during the migration window. The exact alias mechanism is out of scope for this ADR. + +Modules see plain key strings on both the read and write paths; the prefix is invisible to the WIT-facing API. + +## Considered options + +- **Separator string** (`{module}:{key}`). Rejected: any module name containing `:` collides with another module's `:`-bearing key. A fixed-size hash is unambiguous regardless of payload bytes. +- **`[len:u8][module_name][key]` length-prefixed string.** Rejected: spoofable (the name is operator-supplied text), and does not align with the ENS-based discovery path that 0.3 will introduce. The 32-byte hash is deterministic and namespace-uniform. +- **One redb database file per module.** Rejected: multiplies open file handles linearly in modules, blocks any future cross-module atomic operations (not currently planned but cheap to keep on the table), and complicates backup tooling (N files vs 1). +- **One redb *table* per module within a single file.** Rejected: redb `TableDefinition` lifetimes are `'static`, so table names must be known at compile time. Dynamic table opening per module would force string-leak workarounds and exposes the same name-collision question as separator-based keys. +- **Engine-allocated incrementing module id.** Rejected: stable across reboots only if the engine persists the allocation table, which adds a chicken-and-egg dependency on the local-store itself. Determinism from the name avoids the dependency entirely. + +## Consequences + +- The prefix is fixed-size (32 bytes) and independent of module name length. Range scans over a single module's keys are O(log n + module-key-count) - fine for our workload. +- Migrations changing the prefix derivation (e.g., switching the local-mode hash function or the ENS resolver) would orphan every existing module's persisted state. The derivation must stay stable through 0.x; ENS-mode introduction in 0.3 happens additively via the alias mechanism, not by changing existing prefixes. +- A module's `list-keys` iterates over the namespace range (32-byte prefix scan); the host strips the prefix before returning to the guest. +- Module data versioning (schema migrations across module versions) is the module's responsibility. The local-store does not version values; modules MAY embed a `schema_version` byte in their stored payloads and migrate on `init` when the read value's version differs from the current code's expectation. +- ENS-based discovery (per docs/03) integrates without a prefix-format change: when a module is loaded by ENS name, the prefix is `namehash(name)`. The corresponding `.wasm` content hash is verified via ENS TXT records before loading, separately from the local-store prefix derivation. +- Spoofing protection: an operator cannot make module A read module B's state by renaming, because the prefix is the hash of the canonical name. Renaming a module to match another's name produces a name conflict the engine refuses at boot, rather than silent state takeover. diff --git a/docs/adr/0004-patch-cowprotocol-to-bleu-cow-rs.md b/docs/adr/0004-patch-cowprotocol-to-bleu-cow-rs.md new file mode 100644 index 0000000..c8ba48b --- /dev/null +++ b/docs/adr/0004-patch-cowprotocol-to-bleu-cow-rs.md @@ -0,0 +1,38 @@ +--- +status: proposed +implemented-in: nullislabs/shepherd#10 +--- + +# Patch `cowprotocol` crate to the head of upstream PR #5 + +## Context + +`cowprotocol` v1.0.0-alpha.3 (the version on crates.io) was cut from an early snapshot of `cowdao-grants/cow-rs` PR #5 at commit `1742ffa`. That PR is still open and is the canonical upstream channel for landing additions to the Rust SDK. Its head branch is `bleu/cow-rs:main`, currently at commit `c012404`, carrying 18 follow-up commits the engine materially depends on: + +- `composable::Proof` byte-width fix (consumed by the TWAP poll path). +- `OrderCreation` zero-`from` fast-fail (closes a MEDIUM severity finding in PR #5). +- `order_book` / `composable` submodule splits (cleaner imports on the engine side). + +ADR-0007 commits us to landing three protocol-level primitives into PR #5 directly (`OrderPostError` rich variants + `retry_hint`, `OrderBookApi::with_base_url`, and `wasm32` feature-gating) by pushing additional commits to its head branch. Each commit advances both PR #5 and the patch rev consumed here. + +There is no published `alpha.4` and no scheduled date for one; the engine cannot wait. + +## Decision + +Add a workspace-level `[patch.crates-io]` redirecting `cowprotocol` to `https://github.com/bleu/cow-rs` at commit `c012404`. Every crate that declares `cowprotocol = "1.0.0-alpha.3"` (engine, modules, future SDK) silently picks up the patched build with no `Cargo.toml` change at the dependent site. + +This is not a parallel fork. `bleu/cow-rs:main` IS the head branch of upstream PR #5. Pushing to it updates PR #5; the patch rev advances by bumping a single workspace line. + +## Considered options + +- **Vendor the missing types locally.** Rejected: re-implementing `composable::Proof`, `OrderCreation`, etc. in the engine repo is the AI-duplication anti-pattern that the cow-rs SDK already solves. Reuse over reimplement applies. +- **Pin every dependent to `cow-rs` git directly.** Works but every new workspace member has to remember the git source. `[patch.crates-io]` centralises the override. +- **Open a separate PR per primitive against `cowdao-grants/cow-rs`.** Rejected: fragments the change across multiple PRs when one already exists at the appropriate granularity. Stacking commits on PR #5 keeps the change coherent and lets the cumulative diff be tracked in one place. +- **Wait for `alpha.4` to publish.** No ETA; the TWAP/EthFlow milestone cannot land without `composable::Proof` correct. + +## Consequences + +- `cargo update` will re-resolve to the same `rev`; the lock pins it. +- Bumping the rev is a single-line workspace edit; reviewers see one diff per primitive added to PR #5. +- Drop the patch entirely once a published `cowprotocol` release contains both the alpha.3 follow-ups and the ADR-0007 protocol-primitive additions (`OrderPostError` rich variants + `retry_hint`, `OrderBookApi::with_base_url`, `wasm32` feature-gate). Until then, expect the patch rev to advance with every push to PR #5. +- Modules built against this workspace inherit the patch transitively; modules built standalone against crates.io will see `alpha.3` and may hit the very bugs the patch closes. Flag this in the SDK README when M3 lands. diff --git a/docs/adr/0005-cow-api-via-cached-orderbookapi.md b/docs/adr/0005-cow-api-via-cached-orderbookapi.md new file mode 100644 index 0000000..0862d54 --- /dev/null +++ b/docs/adr/0005-cow-api-via-cached-orderbookapi.md @@ -0,0 +1,34 @@ +--- +status: proposed +implemented-in: nullislabs/shepherd#8 +--- + +# `cow-api` host backend routes both `request` and `submit-order` through `cowprotocol::OrderBookApi` + +## Context + +`shepherd:cow/cow-api` exposes two operations: a generic REST passthrough (`request`) and a typed order submission (`submit-order`). Either could be implemented with raw `reqwest` against `api.cow.fi/{slug}/api/v1`, but the published `cowprotocol` crate already ships an `OrderBookApi` client that knows the chain-specific base URL, the canonical paths, and the `post_order` codec. + +## Decision + +At engine boot, construct one `cowprotocol::OrderBookApi` per `cowprotocol::Chain` variant (currently Mainnet, Gnosis, Sepolia, ArbitrumOne, Base) into a `BTreeMap` keyed by EVM chain id. "Cached" here means built once during boot and reused for the engine's lifetime; clients are not lazy-constructed on each call nor LRU-evicted. The pool implements `Default` so callers instantiate it as `OrderBookPool::default()`; the trait impl populates the map with one entry per `cowprotocol::Chain` variant. + +Both `cow-api` operations consult this pool: + +- `request` resolves the chain's `OrderBookApi`, reads `api.base_url()` for the prefix, joins the module-supplied path, and dispatches via a shared `reqwest::Client`. +- `submit-order` deserialises the JSON `OrderCreation` and calls `OrderBookApi::post_order` directly. The crate handles signing-scheme encoding, error mapping, and `OrderUid` extraction. + +Chains not in `cowprotocol::Chain` return `HostError { kind: unsupported }` at the host call boundary. + +## Considered options + +- **Raw `reqwest` for both.** Rejected: forces us to maintain the chain → base-URL table (drifts whenever cowprotocol adds a chain) and reimplement `post_order`'s body codec and error mapping, the exact duplication the cow-rs SDK already eliminates. +- **`OrderBookApi` for `submit-order`, raw `reqwest` for `request`.** Tempting (request is opaque to the crate) but means two separate chain-resolution paths, two HTTP clients, and a second place to keep the chain set in sync. +- **Build `OrderBookApi` lazily on first call per chain.** Rejected: hides config errors at runtime. Up-front boot construction surfaces unknown chains immediately and amortises away the per-call cost. + +## Consequences + +- Operator-supplied custom orderbook URLs (barn, staging, forked deployments) are out of scope for the default constructor and require a follow-on `OrderBookApi::with_base_url(chain_id, base_url)` constructor in the cow-rs crate (ADR-0007 item 2, not vendored locally). +- Adding a chain means a `cowprotocol::Chain` variant lands in cow-rs first; the engine inherits it on the next patched rev bump. +- The shared `reqwest::Client` enables connection pooling across both `request` and `submit-order` paths. +- Guest-side TWAP and EthFlow modules (ADR-0006) submit orders through this `cow-api` interface; no specialised host helpers wrap it. diff --git a/docs/adr/0006-cow-twap-ethflow-host-helpers.md b/docs/adr/0006-cow-twap-ethflow-host-helpers.md new file mode 100644 index 0000000..1a139d2 --- /dev/null +++ b/docs/adr/0006-cow-twap-ethflow-host-helpers.md @@ -0,0 +1,49 @@ +--- +status: proposed +--- + +# TWAP and EthFlow run as guest modules using low-level host primitives (no specialised `shepherd:cow` interfaces) + +## Context + +TWAP (over ComposableCoW) and EthFlow are the two CoW workflows the M2 grant ships modules for. The natural-seeming approach is to add `shepherd:cow/twap` and `shepherd:cow/ethflow` WIT interfaces that the host implements on top of `cowprotocol` crate primitives, so modules would call `twap.poll-and-submit(...)` and `ethflow.submit-from-log(...)` as host functions. This ADR rejects that direction. + +The dividing line is protocol vs implementation. CoW Protocol primitives - order types, signing schemes, the orderbook REST surface - are protocol concerns and belong in shared layers (`cowprotocol` crate, `shepherd:cow/cow-api` interface). TWAP is one of many strategies built _on top of_ those primitives; ComposableCoW is the contract surface a TWAP module observes, but the act of polling, deciding when to submit, and reacting to orderbook errors is application logic. Putting that application logic in the host or in `cowprotocol` couples every consumer to one implementation and one error-handling policy. + +Embedding a concrete TWAP implementation in an SDK is an architectural smell the grant explicitly seeks to alleviate. The grant seeks to enable Shepherd as the runtime where many independent strategy implementations coexist, each compiled to its own WASM module. A specialised `twap` interface in the host would defeat that goal: every Shepherd deployment would have to use the same polling implementation, the same error-mapping, the same retry hints, with no room for different strategies to differ on those choices. + +## Decision + +The `shepherd:cow` WIT package contains only the existing `cow-api` interface (REST passthrough + `submit-order`), which is protocol-level. No `twap` interface, no `ethflow` interface, no host-side helpers specific to either workflow. + +TWAP and EthFlow modules implement their logic in Rust guest code using: + +- **`nexum:host/chain`** - `request` (for `eth_call`, `eth_getLogs`, etc.), `subscribe-blocks`, `subscribe-logs`. +- **`nexum:host/local-store`** - for watch lists, cursors, and backoff state. +- **`nexum:host/logging`** - for structured logs. +- **`shepherd:cow/cow-api`** - `submit-order` for orderbook submission. +- **`cowprotocol` crate** (consumed directly by the module, gated on the wasm32 feature work in ADR-0007) - for protocol types: `Order`, `OrderCreation`, `OrderUid`, signing schemes, `OrderPostError`, etc. +- **`alloy_sol_types`** (or equivalent) - for ABI-aware decoding of `ConditionalOrderCreated`, `OrderPlacement`, `getTradeableOrderWithSignature` return values, and similar Solidity-typed payloads. + +Concretely, a TWAP module's `on_event(block)` handler iterates the local-store watch set, makes an `eth_call` to `ComposableCoW.getTradeableOrderWithSignature(owner, params, "", [])` via `chain.request`, decodes the return (or revert reason) with `alloy_sol_types`, constructs an `OrderCreation` with `cowprotocol` types, and submits via `cow-api/submit-order`. Orderbook errors are interpreted via `OrderPostError::retry_hint()` (ADR-0007). Backoff state is persisted to `local-store`. All of this lives in module Rust source, not in the engine. + +An EthFlow module's `on_event(log)` handler decodes the `OrderPlacement` event with `alloy_sol_types`, constructs the `OrderCreation` (with the EIP-1271 signing scheme pointing at the `CoWSwapEthFlow` contract), and submits the same way. Module-side, no host helper required. + +## Considered options + +- **Specialised `shepherd:cow/twap` and `shepherd:cow/ethflow` interfaces** with rich `PollOutcome` variants and per-event host helpers, backed by `composable::poll_and_build_order` and `eth_flow::decode_placement` primitives in the `cowprotocol` crate. Rejected: this puts a single concrete TWAP / EthFlow implementation behind a WIT boundary, forcing every Shepherd deployment to use the same polling policy, the same error-mapping, the same retry hints. It also blurs the protocol-vs-implementation boundary the grant is meant to clarify. Multiple TWAP implementations (different polling cadences, different error tolerances, different cancel-on-loss thresholds) must be able to coexist as separate modules without changing the host or the SDK. +- **Move TWAP / EthFlow primitives into `cowprotocol` crate but skip the WIT interfaces**, leaving modules to call `composable::poll_and_build_order` from guest code. Rejected for the same reason: `cowprotocol` is the protocol SDK, not the strategy SDK. Putting TWAP logic there embeds an implementation in the shared layer, which is the smell the grant seeks to fix. +- **Ship a thin `shepherd-sdk` helper crate** that wraps the low-level primitive calls (eth_call, decode, submit) into a convenient `Twap::poll(...)` interface for guest modules. **Acceptable for M3** because the helper would live in guest-callable code, not behind a WIT boundary - a module that wants different polling policy just doesn't use the SDK helper. The host stays neutral. +- **EthFlow as pure passive observer (no submission)**. Rejected on closer read of `cowprotocol/services/crates/autopilot/src/database/onchain_order_events/ethflow_events.rs`: the canonical CoW flow expects the event to be relayed into the orderbook, which is what autopilot currently does internally. Shepherd's `ethflow-watcher` externalises that role, so the module does submit; just from guest code, not via a specialised host interface. +- **TWAP merkle-proof / `setRoot` support in v1.** Deferred. The 0.2 module only handles `ComposableCoW.create()` (empty proof, single conditional order). `setRoot` polling requires off-chain proof derivation; when a real module needs it, it will be implemented in guest code using the same low-level primitives, possibly with an SDK helper to encapsulate the proof bookkeeping. + +## Consequences + +- `shepherd:cow@0.2.0` keeps `cow-api` as its only interface. No new WIT files in this ADR. +- `KNOWN_CAPABILITIES` in `crates/nexum-engine/src/manifest.rs` does **not** gain `"twap"` or `"ethflow"` entries. Modules declare the universal capabilities they actually use: `chain`, `local-store`, `logging`, `cow-api`. +- Modules ship larger (~150 LOC each estimated, up from the ~30 LOC the host-helper design implied), because event decoding, eth_call orchestration, OrderCreation construction, and error-hint interpretation now live in guest code. This is the explicit trade-off: more code per module, less coupling, more freedom for different strategies to coexist. +- Different TWAP polling strategies can coexist as different modules. Operators choose which to load via `engine.toml`'s `[[modules]]` array. +- The watch-tower TypeScript implementation remains the closest reference for what a TWAP module's logic looks like, but it is reference material, not a template the Rust module mirrors verbatim. A newer ComposableCoW iteration in development may simplify the polling surface significantly; the relevant decisions live in the module, not the host. +- `OrderPostError` rich variants + `retry_hint()` (ADR-0007 item 1, formerly item 3) become the primary protocol-level contract between the orderbook and any module submitting orders. Modules `match` on the typed error and apply the `RetryHint` (try-next-block / backoff-seconds / drop). This logic is generic across TWAP, EthFlow, stop-loss, and any future strategy. +- The M3 SDK (`shepherd-sdk` crate) is the natural home for ergonomic guest-side helpers: `WatchSet`, `PollLoop`, `BackoffLedger`, decode-and-submit utilities. The SDK is opt-in for module authors and lives entirely on the guest side; the host remains protocol-neutral. +- The architecture and sequence diagrams in `docs/diagrams/` that depict `twap.poll-and-submit` and `ethflow.submit-from-log` host calls reflect the rejected design and must be updated to show modules calling low-level primitives directly. diff --git a/docs/adr/0007-upstream-protocol-logic-to-cow-rs.md b/docs/adr/0007-upstream-protocol-logic-to-cow-rs.md new file mode 100644 index 0000000..3a828f5 --- /dev/null +++ b/docs/adr/0007-upstream-protocol-logic-to-cow-rs.md @@ -0,0 +1,46 @@ +--- +status: proposed +--- + +# Push CoW Protocol primitives to `cow-rs` first, adopt in `nexum-engine` second + +## Context + +Implementing ADR-0005 (cow-api backend) and supporting guest-side TWAP / EthFlow modules per ADR-0006 surfaces a recurring question: when the engine or its modules need a piece of CoW Protocol logic that the `cowprotocol` Rust SDK does not yet expose (rich orderbook error variants, custom orderbook URLs, wasm32 compatibility), do we write that logic locally and tidy it up upstream later, or do we add it to the open upstream PR first and only land the engine wiring afterwards? + +The failure mode is well-known: duplicating work that an existing crate could do is the AI-coding anti-pattern most likely to land in a contribution. The same risk applies to any engine-side reimplementation of protocol logic. + +The line between **protocol primitives** (which belong in `cowprotocol`) and **strategy implementations** (which belong in guest modules, per ADR-0006) is the operating principle. This ADR covers only the protocol-primitive additions; TWAP polling and EthFlow event decoding stay in guest modules and are explicitly **not** primitives we push to `cowprotocol`. + +## Decision + +Protocol-level CoW logic - anything that an indexer, a bot, or a non-`nexum` Rust consumer of CoW Protocol would also need to interact with the protocol - lands as additional commits on `cowdao-grants/cow-rs` PR #5 first (head branch `bleu/cow-rs:main`), and is consumed by `nexum-engine` and by guest modules via the `[patch.crates-io]` rev bump (ADR-0004). The engine and the modules never write throwaway local copies of the same logic with the intent to "port later". + +The concrete set of primitives this ADR commits to upstream, in priority order: + +1. **`cowprotocol::OrderPostError` rich variants + `retry_hint(&self) -> RetryHint`** - typed orderbook submission errors (`QuoteNotFound`, `InvalidQuote`, `InsufficientAllowance`, `InsufficientBalance`, `TooManyLimitOrders`, `InvalidAppData`, `AppDataFromMismatch`, `SellAmountOverflow`, `ZeroAmount`, `TransferSimulationFailed`, `ExcessiveValidTo`, …) with a `retry_hint()` helper classifying each into `TryNextBlock`, `BackoffSeconds(u64)`, or `Drop`. Mirrors watch-tower's `API_ERRORS_TRY_NEXT_BLOCK` / `API_ERRORS_BACKOFF` / `API_ERRORS_DROP` tables. Without this, every Rust consumer of CoW reinvents the same mapping, and modules spam the orderbook with permanently-broken orders. **Critical-path, not optional.** + +2. **`cowprotocol::OrderBookApi::with_base_url(chain_id, base_url)`** - custom-URL constructor for barn / staging / forked deployments. Unblocks per-chain orderbook URL overrides in `engine.toml` (ADR-0005). + +3. **`cowprotocol` `wasm32` compatibility** - feature-gate the `reqwest` dependency so guest modules can use the pure types (`Order`, `OrderCreation`, `OrderUid`, signing schemes, error variants) without dragging in an HTTP client. **Critical for ADR-0006**: modules implement TWAP and EthFlow logic in guest code and need `cowprotocol` types compiled to wasm32. Without this, guest modules fall back to duplicating type definitions. + +Lower-priority follow-ons (`OrderUid::from_slice`, retry middleware on `OrderBookApi`, `OrderCreation::from_gpv2`) are good-to-have but are not blocking for the M2 host or module scope. + +## Considered options + +- **Implement locally, refactor upstream later.** Faster short term but predictably leaves an indeterminate amount of duplicated logic in the engine, contradicts the conventions established on cow-rs PR #5, and grows technical debt every time cow-rs evolves the underlying types. Rejected. +- **Push TWAP / ComposableCoW primitives** (`composable::poll_and_build_order`) into `cowprotocol`. Rejected: TWAP is a concrete strategy on top of the protocol, not part of the protocol. Putting it in the SDK forces every consumer to use one polling implementation and one error-mapping policy. Per ADR-0006, TWAP polling lives in guest module code, not in shared layers. +- **Push EthFlow log-decoding primitives** (`eth_flow::decode_placement`) into `cowprotocol`. **Rejected for the same reason**: EthFlow event decoding is an implementation detail of how a particular module relays orders into the orderbook. The protocol layer defines the order types and the orderbook submission endpoint; the act of decoding an on-chain event into an `OrderCreation` is module-side logic. Modules decode `OrderPlacement` directly with `alloy_sol_types` and construct the `OrderCreation` with the EIP-1271 signing scheme. +- **Wait for cow-rs upstream maintainers to add these on their own.** No evidence anyone else is doing this work; the grant timeline does not permit waiting. +- **Vendor a fork of cow-rs inside `nullislabs/shepherd`.** Worst of all worlds: blocks neither the engine nor cow-rs from drifting, and forces every other CoW consumer to re-derive the same primitives. +- **Host-side `AppDataResolver` (LRU cache + GET against `/api/v1/app_data/{hash}`).** Rejected after verifying watch-tower's behavior: it never fetches app-data. The trader uploads the JSON to the orderbook via `PUT /api/v1/app_data/{hash}` separately; the relayer module just submits and reacts to `INVALID_APP_DATA` (backoff 1 min) / `APPDATA_FROM_MISMATCH` (drop) via the error map in item 1 above. + +## Consequences + +- Every M2 engine or module issue that consumes one of the three primitives above is blocked on the corresponding commit landing in PR #5's head branch. Items 1, 2, 3 can be authored as independent commits and pushed in parallel rather than serially. +- `[patch.crates-io]` rev in the workspace `Cargo.toml` (ADR-0004) is bumped after each push to PR #5; the bump is the engine's signal that a new primitive is consumable. +- Commits added to PR #5 follow its established conventions: alloy reuse over local reimplementation, GPL-3.0, edition 2024, terse rustdoc. +- The engine repo stays small: `nexum-engine` contains WIT, host wiring, supervisor, redb store, alloy provider pool, and `engine.toml` schema, with nothing about CoW Protocol semantics. +- Guest modules consume `cowprotocol` types directly (gated on the wasm32 feature in item 3). The `shepherd-sdk` crate in M3 may add ergonomic wrappers on top, but those live on the guest side, not behind a WIT boundary. +- A follow-on Bleu module - the Rust-side equivalent of `cowprotocol/refunder` (permissionless `invalidateOrder` triggering for expired EthFlow orders) - becomes natural to ship once an ethflow-watcher module lands. Out of scope for M2 but explicitly enabled by the same primitives. +- TWAP polling logic (decode `ConditionalOrderCreated`, eth_call `getTradeableOrderWithSignature`, decode return, build `OrderCreation`) and EthFlow event decoding stay entirely in guest module code. The `cowprotocol` crate provides only the types and the orderbook client; the strategy is the module's. diff --git a/docs/adr/0008-factory-subscriptions-in-manifest.md b/docs/adr/0008-factory-subscriptions-in-manifest.md new file mode 100644 index 0000000..f7fff7d --- /dev/null +++ b/docs/adr/0008-factory-subscriptions-in-manifest.md @@ -0,0 +1,56 @@ +--- +status: deferred +deferred-to: 0.3 +--- + +# Dynamic address registration for log subscriptions (deferred to 0.3) + +## Status + +**Deferred to 0.3.** Neither TWAP nor EthFlow (the M2 grant deliverables) needs this capability, and the design's complexity is not justified by current need. + +This ADR is preserved as a reference for the design space; the final shape will be revisited when the first module actually requiring dynamic address registration emerges. + +## Context + +Some module archetypes need to track contracts deployed dynamically by a factory, for example Uniswap V3 pools (deployed by `UniswapV3Factory`). Static `[[subscription]]` declarations in `module.toml` cannot express this: the child addresses are not known when the module's manifest is authored. + +Neither TWAP nor EthFlow needs this; both subscribe to a single well-known contract per chain. This ADR was originally framed as forward-looking work to land in 0.2's breaking-change window. + +## Why deferred + +Two considerations motivate the deferral: + +1. **`eth_getLogs` already supports topic-only filtering.** The JSON-RPC method accepts a filter without an `address` field, so a module subscribing to a topic across all addresses can be served by the existing primitives if the operator's RPC endpoint cooperates. If topic-only filters at the JSON-RPC layer are good enough for the common case, the engine does not need a manifest-and-host-function mechanism on top. +2. **The schema and host-function surface add engine complexity that no M2 deliverable consumes.** The historical-backfill story is the largest contributor to that complexity and was already trimmed once; deferring the rest in the same spirit avoids paying for a mechanism nothing exercises yet. + +Combined: the dynamic-subscription design is not load-bearing for M2 deliverables, and the simplest path (topic-only `eth_subscribe` filters with module-side address filtering) may suffice for a wide range of indexer use cases. The dynamic-registration mechanism originally proposed (Envio-style `register-address`) addresses scaling concerns at high address counts but should land when a real consumer is on the table to validate the trade-off. + +## Reference design (not adopted in 0.2) + +The original proposal - kept here so future discussions have a starting point - was a hybrid of static topics and dynamic addresses: + +- `[[subscription.template]]` block in `module.toml` declaring `chain_id`, `name`, `event_topics` (no address). +- `chain.register-address(chain_id, template_name, address)` host function for the module to add addresses at runtime. +- `chain.unregister-address(chain_id, template_name, address)` mirror function. +- `log-source.template(string)` variant on the event dispatch so modules route by template name. +- Engine maintains a single aggregated `eth_subscribe logs` per chain per template, with filter `(topic ∈ event_topics) ∧ (address ∈ current_set)`. The address set is mutated as the module discovers new contracts. +- Historical backfill (`from-block` argument on register, paginated `eth_getLogs` orchestration) was contentious and was already trimmed before deferral. + +Envio HyperIndex's `context..register()` API is the closest existing pattern, validated in production for indexers tracking thousands of dynamically-discovered contracts. + +## Alternatives left open for 0.3 + +- **Topic-only `[[subscription]]`** (no address field; engine forwards `eth_subscribe logs` with topic-only filter; module client-side filters logs by address it cares about). Simplest, no new host functions. Trade-off: firehose volume for common topics like `Transfer`. +- **Dynamic register-address** (the original reference design above). +- **Engine-extracted factory child addresses** (Ponder-style declarative schema with ABI-aware extraction rules). Schema complexity grows with exotic factory shapes. +- **No factory pattern; modules wanting dynamic discovery use raw `chain.subscribe-logs` with topic-only filter and persist the discovered address set themselves**. + +The choice depends on what the first consumer actually needs. + +## Consequences of deferring + +- The `shepherd:cow` and `nexum:host` WIT surfaces remain unchanged in 0.2. +- `module.toml` schema does not gain `[[subscription.template]]` in 0.2. +- 0.2 is the breaking-change window; adding any of the above options in 0.3 may require a major version bump if the chosen shape extends `module.toml` or `nexum:host/chain` non-additively. This risk is accepted on the basis that the M2 grant deliverables do not require this surface. +- TWAP and EthFlow modules ship in 0.2 against the existing static `[[subscription]]` declarations (one address per subscription, known at manifest authorship time). This is consistent with how the autopilot ethflow indexer and watch-tower configure their subscriptions today. diff --git a/docs/adr/0009-host-trait-surface.md b/docs/adr/0009-host-trait-surface.md new file mode 100644 index 0000000..2b27b0c --- /dev/null +++ b/docs/adr/0009-host-trait-surface.md @@ -0,0 +1,73 @@ +--- +status: proposed +implemented-in: bleu/nullis-shepherd#12, #13, #15, #22, #23, #24, #25 +--- + +# M3 Host trait surface: four per-capability traits + supertrait `Host`, with per-module `strategy.rs` / `lib.rs` split + +## Context + +`docs/05-sdk-design.md` describes a much richer M5+ SDK (`#[nexum::module]` proc macro, alloy `Provider`, `TypedState`, `Signer`, named event handlers with async dispatch). M3's scope was narrower: deliver a testable host abstraction that lets module logic compile against an in-memory mock without a `wasm32-wasip2` toolchain, and that the M2 modules (twap-monitor, ethflow-watcher) can adopt without breaking their existing dispatch. + +The constraint is unusual: `wit_bindgen::generate!` emits per-cdylib types - every module gets its own `HostError`, `Event`, `Log`, etc. - so a single shared SDK type cannot be re-used across the wit boundary. Mocks live in their own crate (`shepherd-sdk-test`) and need to compile for the host target (not wasm). + +## Decision + +Three coupled choices: + +### 1. Four per-capability traits with a supertrait `Host` + +`shepherd-sdk` exposes four traits, one per host import: + +```rust +pub trait ChainHost { fn request(&self, chain_id: u64, method: &str, params: &str) -> Result; } +pub trait LocalStoreHost { fn get / set / delete / list_keys ... } +pub trait CowApiHost { fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result; } +pub trait LoggingHost { fn log(&self, level: LogLevel, message: &str); } + +pub trait Host: ChainHost + LocalStoreHost + CowApiHost + LoggingHost {} +impl Host for T {} +``` + +Module strategy code takes `&impl Host` (or ``), so it can call any of the four interfaces uniformly. Tests inject `shepherd_sdk_test::MockHost`; production inject `WitBindgenHost`. The blanket `impl Host for T` means callers never write `impl Host for MyHost {}` by hand. + +### 2. SDK-side `HostError` mirroring the wit struct field-for-field + +`shepherd_sdk::host::HostError` has the same fields as the wit-bindgen-generated `HostError` in each module crate, but is its own type: + +```rust +pub struct HostError { + pub domain: String, + pub kind: HostErrorKind, + pub code: i32, + pub message: String, + pub data: Option, +} +``` + +Each module's `lib.rs` writes a one-liner `convert_err` and `sdk_err_into_wit` to bridge the two. The traits stay world-neutral: `shepherd-sdk-test` compiles for the host target without needing a wasm toolchain, and the mocks are usable from any module's tests. + +### 3. Per-module `strategy.rs` + `lib.rs` split + +Every module is shaped as: + +- `strategy.rs` - pure logic. Imports `shepherd_sdk::host::{Host, HostError, LogLevel}`. Defines small carrier types (`LogView<'a>`, `BlockInfo`, `Settings`) so the strategy is wit-independent. Tests live here under `#[cfg(test)]` against `MockHost`. +- `lib.rs` - per-cdylib glue. `wit_bindgen::generate!`, the `WitBindgenHost` struct implementing all four traits with `chain::request` / `local_store::*` / `cow_api::submit_order` / `logging::log` calls, the `convert_err` + `sdk_err_into_wit` + `convert_level` helpers, and the `Guest` impl that destructures `types::Event` and delegates to `strategy`. + +Reference implementations: `modules/examples/price-alert/`, `modules/examples/stop-loss/`, `modules/twap-monitor/`, `modules/ethflow-watcher/`. The wit-bindgen adapter is intentionally mechanical and is a candidate for a future declarative macro in `shepherd-sdk` (the `#[nexum::module]` design in doc 05). + +## Considered options + +- **Single fat `Host` trait.** Rejected: pulls every module's tests into mocking the full surface even when the strategy only touches one or two capabilities. The four-trait split lets tests `respond_to` exactly the calls the strategy makes. +- **`#[nexum::module]` proc macro now.** Rejected for M3 scope. The proc macro is the right shape long-term (see doc 05) but adds a macro crate, parsing logic, and a debugging surface we did not need to ship M2 modules with MockHost coverage. The manual adapter is verbose but understandable in one read; we land the macro as M5 work. +- **Re-export wit-bindgen `HostError` from the SDK.** Rejected: the wit-bindgen types are per-cdylib. Re-exporting one module's `HostError` would break all others. A shared SDK struct with field-equivalent shape and module-local `From` impls is the only way the SDK stays world-neutral. +- **Strategy lives in `lib.rs` next to the wit-bindgen adapter.** Rejected after BLEU-851 (price-alert) showed the dispatch matrix was not unit-testable without MockHost, and BLEU-854 / BLEU-855 ported twap-monitor / ethflow-watcher to the split. The wit-bindgen adapter is ~150 lines of mechanical glue; the strategy is hundreds of lines of logic - colocating them obscures both. + +## Consequences + +- **Strategy code is testable in native Rust** without `wasm32-wasip2`. The 145 host tests across the workspace (twap 20, ethflow 12, balance-tracker 13, price-alert 16, stop-loss 7, shepherd-sdk 27, shepherd-sdk-test 8, nexum-engine 41, plus 1 doctest) all exercise this seam. +- **The `WitBindgenHost` adapter is duplicated across modules.** ~150 lines of identical glue (the four trait impls plus the two converters and `convert_level`). Acceptable today; the M5 `#[nexum::module]` macro is the path to eliminate it. +- **`shepherd-sdk-test` does not need wit-bindgen.** It depends only on `shepherd-sdk` and `std`; no wasm toolchain involved. Tests compile and run as plain Rust. +- **`HostError` round-trips lossily at the WIT boundary.** The wit-bindgen and SDK types have identical fields today; if either evolves (new variant on `HostErrorKind`, new field), modules need a one-line `From` update. ADR-0009 follow-up COW-1029 / BLEU-853 will `#[non_exhaustive]` both enums before any field-add or variant-add lands. +- **The four-trait split is not an interface contract with mfw78's WIT.** WIT defines the wire shape; the SDK traits are a Rust-side ergonomics layer. The two evolve together but are not the same artifact. +- **Future capabilities (e.g. `messaging`, `remote-store`, `http`) add new traits.** Each new host interface becomes a new trait + new `MockX` in `shepherd-sdk-test`, and the supertrait `Host` is bumped to bound on the new trait. Modules that do not use the new capability are unaffected (they only need `` etc. on the subset they actually touch - the supertrait is a convenience for full-surface modules, not a hard requirement). diff --git a/docs/diagrams/README.md b/docs/diagrams/README.md new file mode 100644 index 0000000..c9c509e --- /dev/null +++ b/docs/diagrams/README.md @@ -0,0 +1,31 @@ +# Diagrams + +Mermaid sources and rendered PNGs covering the engine architecture, the CoW workflows that the M2 modules implement (TWAP and EthFlow, both as guest modules using low-level host primitives), and the engine internals that new contributors most often need to reason about. + +## Architecture and CoW flows + +| File | Type | Shows | +|---|---|---| +| `architecture.png` / `.mmd` | Component | Static view: external infra, nexum-engine internals, WASM modules (twap-monitor, ethflow-watcher) consuming low-level host primitives, and the `cowprotocol` crate (consumed via `[patch.crates-io]` and the wasm32 feature). The `shepherd:cow` package contains only `cow-api`; no specialised TWAP or EthFlow interfaces. | +| `sequence-ethflow.png` / `.mmd` | Sequence | `OrderPlacement` on-chain event handled entirely in the `ethflow-watcher` guest module: `alloy_sol_types` decodes the event, the module builds an `OrderCreation` with the EIP-1271 signing scheme using `cowprotocol` types, and submits via `cow-api/submit-order`. The orderbook error path runs through `OrderPostError::try_from(host-error).retry_hint()`. | +| `sequence-twap.png` / `.mmd` | Sequence | `ConditionalOrderCreated` registration plus the per-block polling loop driven by the `twap-monitor` guest module: `alloy_sol_types` decodes registrations and `eth_call` returns, the module makes the `getTradeableOrderWithSignature` call via `chain.request`, builds `OrderCreation` via `cowprotocol` types, and submits via `cow-api/submit-order`. Orderbook errors flow through `OrderPostError::retry_hint`. | + +## Engine internals (for contributors) + +| File | Type | Shows | +|---|---|---| +| `module-lifecycle.png` / `.mmd` | State machine | Resolve → Load → Init → Run → Restart → Dead transitions and what triggers each. Documents the exponential-backoff restart policy and the implicit write transaction around `init`. | +| `engine-boot.png` / `.mmd` | Sequence | Boot order: engine.toml → tracing → ProviderPool → LocalStore → OrderBookPool → Supervisor (load each module) → open subscriptions → run event loop. | +| `wit-call-path.png` / `.mmd` | Sequence | One host call traced end-to-end: module Rust source → wit-bindgen stubs → WASM Component → wasmtime Linker → HostState trait impl → ProviderPool → alloy → Chain RPC, and back. Demystifies the WASM/Rust boundary. | +| `subscription-dispatch.png` / `.mmd` | Flow chart | How the supervisor aggregates `[[subscription]]` declarations across modules, opens shared block subscriptions (broadcast) and per-filter log subscriptions (routed), and dispatches events to the right `on_event` handlers. | + +## Regenerate + +```sh +cd docs/diagrams +for f in *.mmd; do + npx -y @mermaid-js/mermaid-cli@latest -i "$f" -o "${f%.mmd}.png" -b white --width 1800 +done +``` + +Mermaid sources are the source of truth; PNGs are committed for offline viewing and PR previews. diff --git a/docs/diagrams/architecture.mmd b/docs/diagrams/architecture.mmd new file mode 100644 index 0000000..f5b97f2 --- /dev/null +++ b/docs/diagrams/architecture.mmd @@ -0,0 +1,69 @@ +graph TB + subgraph external["External Infrastructure"] + OB["CoW Orderbook
api.cow.fi"] + RPC["Chain RPC
ws:// or https://"] + CC["ComposableCoW
(Solidity contract)"] + EF["CoWSwapEthFlow
(Solidity contract)"] + end + + subgraph engine["nexum-engine binary"] + ETOML["engine.toml
(operator config)"] + SUP["Supervisor
(boot + dispatch)"] + EL["Event Loop
futures::select_all"] + + subgraph host["HostState (per module)"] + HW["Host Backends
cow-api, chain,
local-store, logging, ..."] + CP["OrderBookPool
(BTreeMap of OrderBookApi)"] + PP["ProviderPool
(alloy DynProvider per chain)"] + LS["LocalStore
(redb, 32-byte hash prefix per module)"] + end + end + + subgraph modules["WASM Modules (Component Model)"] + MTOML["module.toml
(module manifest)"] + TM["twap-monitor
(decodes events, polls,
builds OrderCreation,
submits via cow-api)"] + EM["ethflow-watcher
(decodes OrderPlacement,
builds OrderCreation,
submits via cow-api)"] + end + + subgraph cowrs["cowprotocol crate (via [patch.crates-io] to PR #5)"] + OBA["OrderBookApi
(orderbook submission)"] + TYPES["Protocol types
(Order, OrderCreation,
OrderUid, signing schemes)"] + OPE["OrderPostError
+ retry_hint -> RetryHint"] + end + + ETOML --> SUP + MTOML -.declares.-> SUP + SUP --> EL + SUP -.loads via wasmtime.-> TM + SUP -.loads via wasmtime.-> EM + + EL -- "on_event(block | log)" --> TM + EL -- "on_event(block | log)" --> EM + + TM -- "WIT host call" --> HW + EM -- "WIT host call" --> HW + + TM -- "consumes types
(wasm32 feature)" --> TYPES + EM -- "consumes types
(wasm32 feature)" --> TYPES + TM -- "matches on" --> OPE + EM -- "matches on" --> OPE + + HW --> CP + HW --> PP + HW --> LS + + CP --> OBA + PP --> RPC + OBA --> OB + CC -. "ConditionalOrderCreated" .-> RPC + EF -. "OrderPlacement / OrderInvalidation" .-> RPC + + classDef external fill:#f0e6ff,stroke:#7e3ff2,color:#000 + classDef engineNode fill:#e6f3ff,stroke:#1e88e5,color:#000 + classDef moduleNode fill:#fff4e6,stroke:#ff9800,color:#000 + classDef cowrsNode fill:#e6ffe6,stroke:#2e7d32,color:#000 + + class OB,RPC,CC,EF external + class ETOML,SUP,EL,HW,CP,PP,LS engineNode + class MTOML,TM,EM moduleNode + class OBA,TYPES,OPE cowrsNode diff --git a/docs/diagrams/diagrams.md b/docs/diagrams/diagrams.md new file mode 100644 index 0000000..710717b --- /dev/null +++ b/docs/diagrams/diagrams.md @@ -0,0 +1,472 @@ +# Shepherd - Architecture Diagrams + +Visual reference for the Shepherd engine, its interactions with Nexum, CoW Protocol, and the WASM module layer. Derived from ADRs 0001–0008 and the internal architecture document. + +> **Scope note** - diagrams 1–4 and 7–8 reflect the **M1 implemented state** plus the **M2 target design** as described by the ADRs. Diagrams 5–6 (TWAP, EthFlow) describe **guest-module-driven flows**: the modules do all the protocol work themselves using low-level host primitives, with no specialised `twap` or `ethflow` host interfaces. Where the current code differs from the target design, a note is included in the relevant block reference. + +--- + +## 1. System Architecture Overview + +High-level component map: what lives where, how repositories depend on each other. + +```mermaid +graph TD + ET["engine.toml · module.toml\n(operator config + module manifest)"] + SUP["Supervisor::boot"] + POOLS["ProviderPool · OrderBookPool · LocalStore"] + HS["HostState (per module)\nnexum:host@0.2.0 + shepherd:cow@0.2.0"] + EL["EventLoop - futures::stream::select_all\nfan-out block/log streams to subscribers"] + MODS["WASM Modules\ntwap.wasm · eth-flow.wasm\n(self-contained protocol logic in guest)"] + BC["Blockchain (Sepolia / Mainnet / …)\nComposableCoW · CowEthFlow · RPC Node"] + CR["bleu/cow-rs ← [patch.crates-io]\nOrder · OrderCreation · OrderUid · signing schemes\nOrderBookApi · OrderPostError + retry_hint\nOrderBookApi::with_base_url · wasm32 feature"] + OB["api.cow.fi (Orderbook REST)"] + + ET --> SUP + SUP --> POOLS + POOLS --> HS + HS --> EL + BC -->|"block/log events (eth_subscribe)"| EL + EL -->|"on_event(block/log)"| MODS + MODS -->|"WIT calls (chain · local-store · cow-api · …)"| HS + MODS -.->|"consumes types (wasm32 feature)"| CR + HS -->|"eth_call / subscribe"| BC + HS -->|"OrderBookApi.post_order"| OB + HS -->|"cow-api passthrough"| OB +``` + +### Block reference + +| Block | What it is | +|---|---| +| **engine.toml** | Written by the operator. Declares which chains to connect to (RPC URLs), where to store state on disk, and which WASM modules to load at boot. | +| **module.toml** | Written by the module developer and shipped inside the module bundle. Declares which capabilities the module needs (`required`), which on-chain events to subscribe to, and any module-specific config keys. Renamed from `nexum.toml` per ADR-0001 so the operator/module split is directly apparent. | +| **Supervisor::boot** | The boot orchestrator. Reads both config files, creates the shared resource pools, loads each `.wasm` component via wasmtime, and wires their subscriptions into the event streams. | +| **ProviderPool · OrderBookPool · LocalStore** | The three shared backends. `ProviderPool` holds one alloy RPC client per chain. `OrderBookPool` holds one CoW orderbook HTTP client per chain. `LocalStore` is a single redb key-value database shared by all modules (with per-module 32-byte hash namespacing - ADR-0003). | +| **HostState (per module)** | The per-module bridge between WASM guest code and Rust host code. When a module calls a WIT function (`local-store/set`, `cow-api/submit-order`, etc.), wasmtime routes that call to the corresponding method on that module's `HostState`. Checks capability permissions before dispatching. | +| **EventLoop** | The main async loop. Runs all block-header and log-event streams concurrently via `futures::stream::select_all`. When a stream fires, it routes the event to every module that subscribed to it in their `module.toml`. | +| **WASM Modules** | The guest programs. Each module exports `init(config)` (called once at boot) and `on_event(event)` (called on every relevant block or log). They contain the protocol logic themselves: TWAP polling, EthFlow event decoding, OrderCreation construction. They call back into the host through universal WIT interfaces only - no CoW-specific helper interfaces (ADR-0006). | +| **Blockchain** | The EVM chain being watched. Delivers new block headers and contract log events over a persistent WebSocket (`eth_subscribe`). Also handles `eth_call` for on-chain reads (e.g. checking whether a TWAP order is ready). | +| **bleu/cow-rs [patch.crates-io]** | The Rust crate containing CoW Protocol **primitives**: order types, signing schemes, the orderbook HTTP client, and the typed orderbook error model (`OrderPostError` + `retry_hint`). Pulled via `[patch.crates-io]` pointing at the head of upstream PR #5. Modules consume the types directly via the `wasm32` feature; the engine consumes the orderbook client via its `cow-api` host backend. No TWAP or EthFlow strategy logic lives here - that stays in module code (ADR-0007). | +| **api.cow.fi (Orderbook REST)** | The CoW Protocol orderbook service. Accepts `POST /orders` to register new orders. Trader-uploaded app-data documents are PUT to `/app_data/{hash}` separately by whoever signed the order (not by the relayer module). | + +--- + +## 2. Domain / Class Diagram + +Key types, their fields, and relationships across the engine codebase. + +```mermaid +classDiagram + class EngineConfig { + +state_dir: PathBuf + +chains: BTreeMap~u64, ChainConfig~ + +modules: Vec~ModuleEntry~ + } + class Manifest { + +name: String + +capabilities_required: Vec~String~ + +subscriptions: Vec~Subscription~ + } + class Subscription { + +kind: Block | Log + +chain_id: u64 + +address: Option~Address~ + +topics: Vec~B256~ + } + class Supervisor { + +dispatch_block(chain_id, block) + +dispatch_log(owner, log) + } + class ProviderPool { + +providers: BTreeMap~u64, DynProvider~ + } + class OrderBookPool { + +clients: BTreeMap~u64, OrderBookApi~ + } + class LocalStore { + +db: redb~Database~ + +get(key: String) Option~Vec~u8~~ + +set(key: String, value: Vec~u8~) + +delete(key: String) + +list_keys(prefix: String) Vec~String~ + } + class HostState { + +wasi: WasiCtx + +table: ResourceTable + +http_allowlist: Vec~String~ + +monotonic_baseline: Instant + %% M2 additions (ADR-0005): + %% +module_namespace: [u8; 32] (ENS namehash or keccak256) + %% +provider_pool: Arc~ProviderPool~ + %% +ob_pool: Arc~OrderBookPool~ + %% +local_store: Arc~LocalStore~ + } + class EventLoop { + +streams: SelectAll~Pin~Box~dyn Stream~~~ + } + class TwapModule { + <> + +on_event(log) persist watch via local-store + +on_event(block) poll each watch via chain.request(eth_call) + +decode return via alloy_sol_types + +build OrderCreation via cowprotocol types + +submit via cow-api.submit-order + +interpret errors via OrderPostError.retry_hint + } + class EthFlowModule { + <> + +on_event(log) decode OrderPlacement via alloy_sol_types + +build OrderCreation (EIP-1271 sig) via cowprotocol types + +submit via cow-api.submit-order + +interpret errors via OrderPostError.retry_hint + } + + EngineConfig --> Supervisor + Manifest --> Supervisor + Manifest "1" --> "*" Subscription + Supervisor --> ProviderPool + Supervisor --> OrderBookPool + Supervisor --> LocalStore + Supervisor "1" --> "*" HostState : per module + HostState --> ProviderPool + HostState --> OrderBookPool + HostState --> LocalStore + EventLoop --> Supervisor + TwapModule ..> HostState : WIT calls (universal) + EthFlowModule ..> HostState : WIT calls (universal) +``` + +### Class reference + +| Class | What it is | +|---|---| +| **EngineConfig** | Deserialized from `engine.toml`. Holds the database path (`state_dir`), one `ChainConfig` per chain (just an RPC URL), and the list of module paths to load. | +| **Manifest** | Deserialized from `module.toml`, which ships inside the module bundle. Declares what capabilities the module needs, which on-chain events to watch, and any module-level config values. | +| **Subscription** | One event declaration inside `module.toml`. `kind=Block` fires on every new block for a given chain. `kind=Log` fires when a specific contract emits an event matching the given address and topics. Factory-style dynamic subscriptions (`[[subscription.template]]` + `register-address`) are deferred to 0.3 - see ADR-0008. | +| **Supervisor** | Orchestrates boot and event dispatch. Creates one `HostState` per module. On each incoming block or log, calls `dispatch_block` / `dispatch_log` to fan the event out to subscribed modules. | +| **ProviderPool** | Holds one alloy `DynProvider` per chain. `wss://` chains get a pubsub provider that supports both subscriptions and requests. `https://` chains get HTTP-only (subscriptions unavailable, by design - ADR-0002). | +| **OrderBookPool** | Holds one `OrderBookApi` client per known CoW chain (Mainnet, Gnosis, Sepolia, ArbitrumOne, Base). Instantiated via `OrderBookPool::default()` at boot (ADR-0005). | +| **LocalStore** | A single redb embedded database at `state_dir`. All modules write into the same file. Keys are prefixed host-side as `[32-byte module namespace][raw_key]` so two modules never collide, and the namespace is unspoofable (ADR-0003). The namespace is `keccak256(module_name)` for locally-loaded modules and `ens_namehash(name)` for ENS-discovered modules. | +| **HostState** | The per-module runtime context. `wasmtime::component::bindgen!` generates one trait per WIT interface (e.g. `shepherd::cow::cow_api::Host`); `HostState` implements each trait. `Shepherd::add_to_linker` registers all trait implementations with the `Linker` once at boot. **Current fields** (M1): `wasi: WasiCtx`, `table: ResourceTable`, `http_allowlist: Vec`, `monotonic_baseline: Instant`. **M2 additions** will add `module_namespace: [u8; 32]`, `provider_pool: Arc`, `ob_pool: Arc`, `local_store: Arc`. | +| **EventLoop** | Runs `futures::stream::select_all` over a `Vec + Send>>>`. The loop never exits until SIGINT/SIGTERM. Each fired event is forwarded to `Supervisor` for fan-out. | +| **TwapModule** | The TWAP watcher WASM component. On a `Log` event (ConditionalOrderCreated): persists the registration in `local-store`. On a `Block` event: iterates all watches and, for each, makes an `eth_call` via `chain.request`, decodes the result via `alloy_sol_types` (in-module), builds an `OrderCreation` via `cowprotocol` types (consumed via wasm32 feature), and submits via `cow-api.submit-order`. Orderbook errors flow through `OrderPostError::retry_hint`. All polling logic lives in the module, not the host (ADR-0006). | +| **EthFlowModule** | The EthFlow watcher WASM component. On a `Log` event (OrderPlacement): decodes the event via `alloy_sol_types` in-module, builds the `OrderCreation` with the EIP-1271 signing scheme via `cowprotocol` types, and submits via `cow-api.submit-order`. No polling loop - one log equals one submission attempt. | + +--- + +## 3. WIT Interface Hierarchy + +Two WIT packages: the universal `nexum:host` and the CoW-specific `shepherd:cow`. + +```mermaid +graph TD + NH["nexum:host@0.2.0\n(universal - no CoW knowledge)"] + SC["shepherd:cow@0.2.0\n(CoW Protocol extensions)"] + + NH --> n1["chain ✅ implemented\nrequest(chain-id, method, params)\nrequest-batch(chain-id, requests)\n - \nsubscribe-blocks · subscribe-logs →\n engine-managed via module.toml subscriptions\nregister-address · unregister-address →\n 🕓 deferred to 0.3 (ADR-0008)"] + NH --> n2["local-store ✅ implemented\nget(key) · set(key, value)\ndelete(key) · list-keys(prefix)\nnamespacing: 32-byte hash prefix (ADR-0003)"] + NH --> n3["identity · messaging · http · remote-store\n✅ stubs (Unsupported) - full impl in 0.3"] + NH --> n4["logging · clock · random ✅ implemented"] + + SC -->|"use nexum:host/types"| NH + SC --> s1["cow-api ✅ implemented\nrequest(chain-id, method, path, body)\nsubmit-order(chain-id, order-data)\n→ result\n(only protocol-level interface in shepherd:cow)"] + + note1["No twap / ethflow host interfaces.\nTWAP and EthFlow logic lives in guest\nmodule code, using universal primitives\n(chain · local-store · cow-api).\nSee ADR-0006."] + SC -.-> note1 + + style note1 fill:#fff4e6,stroke:#ff9800,color:#000 +``` + +### Interface reference + +| Interface | What it does | +|---|---| +| **nexum:host@0.2.0** | The base WIT package. Any module running in the engine - CoW-aware or not - imports from here. Defines shared types (`chain-id`, `log`, `host-error`) used by both packages. | +| **chain** | Reads from the blockchain via JSON-RPC. `request` sends a single call; `request-batch` sends several in one round-trip. **Subscriptions are not callable WIT functions** - they are declared in `module.toml` and opened by the engine at boot. Dynamic `register-address` for factory patterns is deferred to 0.3 (ADR-0008). | +| **local-store** | Persistent key-value storage that survives restarts. Operations: `get(key)`, `set(key, value)`, `delete(key)`, `list-keys(prefix)`. The host prefixes every key with a 32-byte deterministic namespace (`keccak256(module_name)` locally, or `ens_namehash(name)` when ENS-loaded) so modules are fully isolated and the namespace cannot be spoofed (ADR-0003). | +| **identity · messaging · http · remote-store** | Capabilities stubbed at 0.2 - they return `Unsupported`. `identity` will provide keystore-backed signing. `messaging` will send Waku messages. `http` will allow direct outbound HTTP calls (subject to the manifest's allowlist). `remote-store` will read/write Swarm/IPFS. | +| **logging · clock · random** | Lightweight utilities. `logging` emits to the engine's `tracing` subscriber (inherits `RUST_LOG` filters). `clock` returns wall-clock time. `random` returns cryptographically-secure random bytes. | +| **shepherd:cow@0.2.0** | The CoW Protocol extension package. Imports `nexum:host/types` for shared types so modules don't re-define `chain-id` or `log`. Only CoW-aware modules need to import this package. Contains exactly **one** interface in 0.2: `cow-api`. | +| **cow-api** | Generic orderbook access. `request` is a raw REST passthrough (returns JSON string). `submit-order` takes raw order bytes and returns a `result` where the string is the order UID. Routes through the engine's `OrderBookPool`. This is the only protocol-level CoW interface in 0.2 - the boundary between "what CoW Protocol *is*" (orderbook submission, order types) and "what's implemented *on top* of CoW" (TWAP polling, EthFlow event handling). | +| **(no twap interface)** | Per ADR-0006, no specialised TWAP host interface exists. The TWAP module implements polling, decoding, and submission entirely in guest code, using `chain.request` for `eth_call`, `local-store` for state, `alloy_sol_types` (in-module) for ABI decoding, `cowprotocol` types for `OrderCreation`, and `cow-api.submit-order` for orderbook submission. Multiple TWAP strategies can coexist as separate modules with different polling policies and error tolerances. | +| **(no ethflow interface)** | Per ADR-0006, no specialised EthFlow host interface exists. The EthFlow module decodes `OrderPlacement` directly in guest code via `alloy_sol_types`, constructs the `OrderCreation` with the EIP-1271 signing scheme via `cowprotocol` types, and submits via `cow-api`. | + +--- + +## 4. Engine Boot Sequence + +```mermaid +flowchart TD + Start([nexum-engine starts]) --> ReadConfig + ReadConfig["1. Read engine.toml\n(EngineConfig::load)"] + ReadConfig --> InitTracing + InitTracing["2. Init tracing\n(RUST_LOG / log_level)"] + InitTracing --> ProvPool + ProvPool["3. ProviderPool::from_config\nFor each chain:\n wss:// → pubsub DynProvider\n https:// → http DynProvider\n(fatal on connection error - ADR-0002)"] + ProvPool --> OpenStore + OpenStore["4. LocalStore::open(state_dir)\nOpen/create redb DB\nnexum:local-store table\n(ADR-0003)"] + OpenStore --> OBPoolInit + OBPoolInit["5. OrderBookPool::default()\nBuild OrderBookApi for:\nMainnet, Gnosis, Sepolia, ArbitrumOne, Base\n(ADR-0005)"] + OBPoolInit --> SupervisorBoot + SupervisorBoot["6. Supervisor::boot\nFor each [[modules]] in engine.toml:"] + SupervisorBoot --> LoadManifest[" a. Load module.toml (Manifest)"] + LoadManifest --> LoadWasm[" b. Load .wasm Component (wasmtime)"] + LoadWasm --> Instantiate[" c. Instantiate with dedicated HostState\n (links nexum:host + shepherd:cow impls)"] + Instantiate --> CallInit[" d. Call module.init(config)"] + CallInit --> AnnotateSubs[" e. Annotate subscriptions from manifest"] + AnnotateSubs --> MoreModules{More modules?} + MoreModules -->|yes| LoadManifest + MoreModules -->|no| OpenStreams + OpenStreams["7. open_block_streams + open_log_streams\neth_subscribe newHeads per chain\neth_subscribe logs per (chain, address, topics)"] + OpenStreams --> RunLoop + RunLoop["8. run_event_loop\nfutures::stream::select_all over all streams\nfan-out: block → all block subscribers\nlog → owner module only"] + RunLoop --> Wait([Await SIGINT/SIGTERM]) +``` + +### Step reference + +| Step | What happens | +|---|---| +| **1. Read engine.toml** | Deserializes the operator config. If the file is missing, the engine falls back to defaults (no chains, default `state_dir`). Modules that need chains will receive `Unsupported` errors at runtime. | +| **2. Init tracing** | Sets up the `tracing` subscriber using `RUST_LOG` or the `log_level` field from `engine.toml`. All host log output flows through here, including per-capability trace events. | +| **3. ProviderPool** | Opens one alloy connection per chain declared in `[chains]`. WebSocket URLs get a full pubsub provider (the recommended setup for any chain a module subscribes to). HTTP URLs get a request-only provider. Any connection failure at this step is fatal - the engine refuses to start with a broken chain rather than silently degrading. Failover and retry are out of scope; they live in alloy middleware (ADR-0002). | +| **4. LocalStore** | Opens (or creates) the redb database at `state_dir`. Creates the `nexum:local-store` table if it doesn't exist. Per-module namespacing uses a 32-byte deterministic hash prefix. Module state from previous runs is immediately available. | +| **5. OrderBookPool** | Constructs one `OrderBookApi` HTTP client for each supported CoW chain via the `Default` implementation. Built upfront so config errors (unknown chain IDs) surface at boot, not on the first order submission. | +| **6. Supervisor::boot (per module)** | For each module listed in `engine.toml`: reads its `module.toml`, loads the `.wasm` component into wasmtime, creates a dedicated `HostState`, calls the module's `init(config)` export, and records which subscriptions the module declared. | +| **7. Open streams** | Aggregates all subscriptions declared across all modules. Opens one `eth_subscribe newHeads` per chain and one `eth_subscribe logs` per (chain, contract-address, topics) filter. | +| **8. Event loop** | The engine enters its steady-state loop. `futures::stream::select_all` waits for the next event on any stream. Block events are broadcast to all modules subscribed to that chain. Log events are delivered only to the module that owns that subscription. | + +--- + +## 5. TWAP Complete Flow (Registration → Submit) + +The TWAP module runs the entire flow in guest Rust code, using only universal host primitives. + +```mermaid +sequenceDiagram + actor User + participant CC as ComposableCoW
Contract + participant RPC as RPC Node
(wss://) + participant EL as EventLoop + participant TM as twap module
(WASM guest) + participant SD as alloy_sol_types
(in module) + participant CR as cowprotocol types
(in module, wasm32) + participant HS as HostState
(Rust) + participant OB as api.cow.fi
(Orderbook) + participant LS as LocalStore
(redb) + + Note over User,CC: Step 0 - On-chain registration (off-engine) + User->>CC: ComposableCoW.create(twapParams) + CC-->>RPC: emit ConditionalOrderCreated(owner, params, proof) + + Note over RPC,LS: Step 1 - Indexing (once per TWAP) + RPC->>EL: log batch (eth_subscribe logs) + EL->>TM: on_event(Event::Logs([registration_log])) + TM->>SD: decode ConditionalOrderCreated + SD-->>TM: (owner, params, salt) + TM->>HS: local-store.set("watch:{owner}:{hash}", params) + HS->>LS: write [module_namespace][watch:...] = params_bytes + + Note over RPC,LS: Step 2 - Poll loop (every block) + loop Every block on chain_id + RPC->>EL: block header (eth_subscribe newHeads) + EL->>TM: on_event(Event::Block(Block { number: N, ... })) + TM->>HS: local-store.list-keys("watch:") + HS-->>TM: registration entries + + loop For each watch where next_attempt <= N + TM->>HS: chain.request(chain_id, "eth_call", [...]) + HS->>RPC: eth_call ComposableCoW.getTradeableOrderWithSignature(owner, params, "", []) + RPC-->>HS: return value or revert + HS-->>TM: result (JSON string) + TM->>SD: decode return value (or interpret revert reason) + SD-->>TM: ready(GPv2OrderData, signature) OR not-ready(hint) + + alt Order ready + TM->>CR: build OrderCreation + CR-->>TM: OrderCreation + TM->>HS: cow-api.submit-order(chain_id, order_json) + HS->>OB: POST /api/v1/orders + alt Submit OK + OB-->>HS: 200 OK, OrderUid + HS-->>TM: Ok(OrderUid) + TM->>LS: set("submitted:{uid}", order_uid) + else Orderbook error + OB-->>HS: 4xx with error code + HS-->>TM: Err(host-error) + TM->>CR: OrderPostError::try_from(host-error).retry_hint() + CR-->>TM: TryNextBlock / BackoffSeconds(s) / Drop + TM->>LS: update next_attempt or remove watch + end + else Not yet ready (TryAtEpoch / TryOnBlock / TryNextBlock / Terminal) + TM->>LS: persist hint (next_attempt) or delete watch + end + end + end +``` + +### Participant reference + +| Participant | Role in this flow | +|---|---| +| **User** | The trader. Interacts with the blockchain directly - the engine never touches private keys. | +| **ComposableCoW Contract** | The on-chain conditional order registry. Accepts TWAP parameters via `create()` and emits `ConditionalOrderCreated`. Also exposes `getTradeableOrderWithSignature()`, which the engine polls to check whether the current TWAP part is ready to trade. | +| **RPC Node** | The WebSocket connection to the chain. Delivers log events (subscriptions) and handles `eth_call` (synchronous reads). Must be `wss://` for this flow since it uses subscriptions. | +| **EventLoop** | Receives raw events from the RPC node and routes them to the module that subscribed to them. Opaque to the flow - it just calls `on_event`. | +| **twap module (WASM guest)** | Contains the entire TWAP strategy: decoding registrations, deciding when to poll (using stored hints), reacting to revert reasons, building orders, interpreting orderbook errors. Calls into the host only through universal WIT primitives. | +| **alloy_sol_types (in module)** | The ABI-aware decoder. Compiled into the module's WASM. Decodes `ConditionalOrderCreated` from raw log bytes; decodes the `getTradeableOrderWithSignature` return; interprets revert reasons. No host involvement for decoding. | +| **cowprotocol types (in module)** | The protocol-level types from `bleu/cow-rs`, consumed by the module via the wasm32 feature (ADR-0007 item 3). Used to build `OrderCreation`, manipulate `OrderUid`, and pattern-match `OrderPostError`. The crate's HTTP client (`OrderBookApi`) is **not** used directly by the module - orderbook submission goes through the host's `cow-api`. | +| **HostState (Rust)** | Provides only the universal primitives (`chain.request`, `local-store.*`, `cow-api.submit-order`). Knows nothing about TWAP semantics. | +| **api.cow.fi (Orderbook)** | Receives the signed `OrderCreation`, validates it, and returns a 56-byte `OrderUid`. The order is now visible to CoW solvers. | +| **LocalStore (redb)** | Persistent state for the TWAP module. `watch:{owner}:{hash}` entries hold registrations. `submitted:{uid}` entries record completed submissions. `next_attempt` hints (epoch or block) let the module skip polling during the gap between TWAP parts. All entries survive engine restarts. | + +--- + +## 6. EthFlow Complete Flow (Event-Driven) + +```mermaid +sequenceDiagram + actor User + participant EFC as CoWSwapEthFlow
Contract + participant RPC as RPC Node
(wss://) + participant EL as EventLoop + participant EM as eth-flow module
(WASM guest) + participant SD as alloy_sol_types
(in module) + participant CR as cowprotocol types
(in module, wasm32) + participant HS as HostState
(Rust) + participant OB as api.cow.fi
(Orderbook) + participant LS as LocalStore
(redb) + + Note over User,EFC: Step 0 - User creates ETH order on-chain + User->>EFC: createOrder(order, msg.value=ETH) + EFC->>EFC: store orders[hash] = onchainData,
emit OrderPlacement(sender, order, EIP1271-sig, data) + EFC-->>RPC: log emitted on block N + + Note over RPC,LS: Step 1 - Log arrives via subscription + RPC->>EL: log batch matching CoWSwapEthFlow address + OrderPlacement topic + EL->>EM: on_event(Event::Logs([placement_log])) + + Note over EM,LS: Step 2 - Decode and submit (1 log = 1 submission) + EM->>SD: decode OrderPlacement(sender, order, sig, data) + SD-->>EM: (sender, GPv2OrderData, EIP-1271 sig, data) + + EM->>CR: build OrderCreation with EIP-1271 scheme
pointing at CoWSwapEthFlow contract + CR-->>EM: OrderCreation + OrderUid + + EM->>HS: cow-api.submit-order(chain_id, order_json) + HS->>OB: POST /api/v1/orders + OB-->>HS: result + + alt 200 OK with OrderUid + HS-->>EM: Ok(OrderUid) + EM->>LS: set("submitted:{uid}", order_uid) + else 4xx with error code + HS-->>EM: Err(host-error with code) + EM->>CR: OrderPostError::try_from(host-error).retry_hint() + CR-->>EM: TryNextBlock / BackoffSeconds(s) / Drop + + alt TryNextBlock + Note over EM: log and skip; next block retries + else BackoffSeconds(s) + EM->>LS: set("backoff:{uid}", now + s) + else Drop + EM->>LS: set("dropped:{uid}", reason) + end + end +``` + +### Participant reference + +| Participant | Role in this flow | +|---|---| +| **User** | The trader. Deposits native ETH into the `CoWSwapEthFlow` contract and specifies swap parameters. The contract is the EIP-1271 signer on behalf of the user. | +| **CoWSwapEthFlow Contract** | Custodies the ETH, stores the order metadata on-chain, and emits `OrderPlacement` so off-chain relayers (this module, plus CoW's own internal autopilot indexer) can pick up the order. | +| **RPC Node** | Delivers the `OrderPlacement` log via the persistent WebSocket subscription. No `eth_call` is needed in this flow - the log contains everything required to reconstruct the order. | +| **EventLoop** | Routes the log to the eth-flow module based on the `[[subscription]]` entry in its `module.toml` (matching the `CoWSwapEthFlow` contract address and the `OrderPlacement` topic). | +| **eth-flow module (WASM guest)** | Contains the entire EthFlow relay logic: decoding, OrderCreation construction, submission, error handling. No polling loop; one log equals one submission attempt. | +| **alloy_sol_types (in module)** | Decodes the `OrderPlacement` event in module-side Rust. The event payload carries the typed `GPv2OrderData`, the EIP-1271 signature blob, and the extra data field. | +| **cowprotocol types (in module)** | Used to construct the `OrderCreation` with the EIP-1271 signing scheme (the signature points at the `CoWSwapEthFlow` contract address, not at the user's key) and to compute the 56-byte `OrderUid`. `OrderPostError` from the same crate is used to interpret orderbook errors. | +| **HostState (Rust)** | Provides only the `cow-api.submit-order` primitive for this flow. Maps orderbook errors to `host-error` with the original error code preserved so the module can recover the typed `OrderPostError`. | +| **api.cow.fi (Orderbook)** | Receives the order. Returns `OrderUid` on success. Returns a typed error code on failure, which the module recovers and passes through `OrderPostError::retry_hint()` to decide what to do next. App-data documents are **not** fetched here; the trader uploads them via `PUT /api/v1/app_data/{hash}` separately. | +| **LocalStore (redb)** | `submitted:{uid}` records successful submissions. `backoff:{uid}` records pending retries with a deadline. `dropped:{uid}` records permanently-failed orders. All entries survive restarts so the module does not re-submit known orders. | + +--- + +## 7. Capability Dispatch (Generic Host Call Path) + +How any WIT function call from a WASM module reaches the host backend it targets. + +```mermaid +flowchart TD + Module["WASM Module\n(twap or eth-flow)"] + Module -->|"e.g. cow-api.submit-order(chain_id, order_json)"| Linker + Linker["wasmtime Linker\n(resolves import → host function)"] + Linker --> CapCheck["HostState\nmanifest.required check\n(returns denied if not declared)"] + CapCheck --> Trace["tracing::info!\n[capability] op chain=…"] + Trace --> HostFn["host backend Rust function"] + HostFn -->|"cow-api"| OBPool["OrderBookPool\n.get(chain_id)?.post_order(order)"] + HostFn -->|"chain (request)"| ProviderP["ProviderPool\n.get(chain_id).request(method, params)"] + HostFn -->|"chain (subscribe-*)"| EngineSubs["engine-managed streams\nopened at boot from module.toml"] + HostFn -->|"local-store"| StoreP["LocalStore\n.get/set/delete/list_keys\n(namespace-prefixed host-side)"] + HostFn -->|"logging · clock · random"| Misc["direct host impl"] + OBPool --> CowAPI["api.cow.fi\nPOST /api/v1/orders"] + ProviderP --> RPCNode["RPC Node\nJSON-RPC"] + CowAPI -->|"OrderUid or error"| Module + RPCNode -->|"result"| Module +``` + +### Node reference + +| Node | What it does | +|---|---| +| **WASM Module** | The guest program. It calls imported WIT functions exactly like regular function calls - it has no visibility into the host machinery behind them. | +| **wasmtime Linker** | `Linker` built once at startup. `wasmtime::component::bindgen!` generates a `Shepherd` world struct and one trait per WIT interface (e.g. `shepherd::cow::cow_api::Host`, `nexum::host::local_store::Host`). `Shepherd::add_to_linker(&mut linker, \|state\| state)` registers every trait method as a host function. After that, calls from WASM resolve with zero dynamic dispatch overhead - the vtable is built at link time, not per-call. | +| **HostState - manifest.required check** | Before dispatching, `HostState` checks that the called capability is listed under `[capabilities].required` in the module's `module.toml`. If not, it returns `host-error { kind: denied }` immediately. The 0.2 engine validates known capability names at boot via `KNOWN_CAPABILITIES`; per-call gating is the M2 target. | +| **tracing::info!** | Every host call emits a structured trace event (capability name, chain id, etc.). Operators use `RUST_LOG=shepherd=debug` to see every call a module makes. | +| **host backend Rust function** | `HostState` implements one generated trait per WIT interface. Each `async fn` in the trait receives `&mut self` (giving access to all host resources) and returns the WIT-mapped Rust type. There are no CoW-strategy-specific backends - only the universal ones plus `cow-api` (ADR-0006). | +| **OrderBookPool** | Looks up the `OrderBookApi` client for the requested chain and calls `post_order`. Returns a 56-byte `OrderUid` on success or an `OrderPostError`-bearing host error on failure. | +| **ProviderPool (chain.request)** | Looks up the alloy provider for the requested chain and dispatches the JSON-RPC call (`eth_call`, `eth_getLogs`, etc.). | +| **engine-managed streams (chain.subscribe-*)** | Subscriptions are not exposed as runtime-callable host functions in 0.2. They are opened by the engine at boot from each module's declared `[[subscription]]` entries; events flow into the module via `on_event`. Dynamic `register-address` for factory patterns is deferred (ADR-0008). | +| **LocalStore** | Reads or writes a key in the module's namespace. The module sees plain keys; the host silently prepends a 32-byte namespace prefix. | +| **logging · clock · random** | Lightweight stateless helpers; implemented directly on `HostState` without a separate pool. | + +--- + +## 8. Repository Dependency Map + +```mermaid +graph TD + upstream["cowdao-grants/cow-rs\n(alpha.3 on crates.io - PR #5 base)"] + bleu_cr["bleu/cow-rs\n(PR #5 head branch)"] + prims["Protocol primitives added to PR #5:\n• OrderPostError + retry_hint\n• OrderBookApi::with_base_url\n• wasm32 feature-gate"] + existing["Already in PR #5:\nOrder · OrderCreation · OrderUid\nsigning schemes · OrderBookApi"] + patch["[patch.crates-io]\ncowprotocol → bleu/cow-rs @ rev"] + engine["nexum-engine\n(WIT host, supervisor, event loop)"] + witCowApi["shepherd:cow/cow-api WIT"] + modules["WASM modules\n(twap · eth-flow)"] + + upstream -->|"is PR base"| bleu_cr + bleu_cr --> prims + bleu_cr --> existing + patch -->|"redirects cowprotocol to rev of"| bleu_cr + engine -->|"workspace Cargo.toml declares"| patch + engine --> witCowApi + witCowApi -->|"backed by OrderBookApi"| existing + modules -->|"imports WIT"| witCowApi + modules -.->|"consumes types (wasm32 feature)"| existing + modules -.->|"matches on errors"| prims +``` + +### Node reference + +| Node | What it is | +|---|---| +| **cowdao-grants/cow-rs** | The upstream CoW Protocol Rust SDK, maintained by the DAO. Version `alpha.3` is published to crates.io but predates 18 follow-up commits Bleu has been pushing through PR #5. This is the PR base - changes land here eventually. | +| **bleu/cow-rs** | Bleu's repository, which is simultaneously the head branch of the DAO's open PR #5. Every commit Bleu pushes here also advances PR #5 for upstream review. This is not a long-lived parallel fork - it is the active PR branch (ADR-0004). | +| **Protocol primitives added to PR #5** | The three additions Bleu is pushing into PR #5: `OrderPostError` rich variants + `retry_hint()` (critical for module error handling), `OrderBookApi::with_base_url` (barn / staging / forked deployments), and `wasm32` feature-gating (critical so guest modules can consume `cowprotocol` types). All three are protocol primitives - they describe what CoW Protocol *is*, not how a particular strategy uses it. TWAP polling and EthFlow event decoding are explicitly *not* added here; they stay in module code (ADR-0007). | +| **Already in PR #5** | The types and orderbook client Bleu's modules consume but did not add: `Order`, `OrderCreation`, `OrderUid`, signing-scheme enums, and `OrderBookApi`. These existed in PR #5 before the M2 work. | +| **[patch.crates-io]** | A single line in the workspace `Cargo.toml` that tells Cargo to use `bleu/cow-rs` at a specific git rev instead of the `alpha.3` release on crates.io. Bumping the rev is the only change needed to pick up a new primitive after it is pushed to `bleu/cow-rs` (ADR-0004). | +| **nexum-engine** | The engine binary. Contains the WIT host implementations, Supervisor, EventLoop, config loaders, and alloy/redb integration. Contains no CoW Protocol logic - protocol primitives live in `bleu/cow-rs`; strategy logic lives in guest modules. | +| **shepherd:cow/cow-api WIT** | The only CoW-specific WIT interface in 0.2. The engine implements it (host side); WASM modules import it (guest side). Backed by `OrderBookPool` (and through that, `OrderBookApi` from `cow-rs`). | +| **WASM modules (twap · eth-flow)** | The grant deliverables. Compiled to `.wasm` Component Model binaries. Import only universal WIT interfaces (`chain`, `local-store`, `logging`) plus `shepherd:cow/cow-api`. Consume `cowprotocol` types directly through the wasm32 feature for building `OrderCreation` and pattern-matching on `OrderPostError`. Contain all TWAP and EthFlow strategy logic themselves (ADR-0006). | diff --git a/docs/diagrams/engine-boot.mmd b/docs/diagrams/engine-boot.mmd new file mode 100644 index 0000000..85ff094 --- /dev/null +++ b/docs/diagrams/engine-boot.mmd @@ -0,0 +1,59 @@ +sequenceDiagram + autonumber + actor Op as Operator + participant Bin as nexum-engine binary + participant FS as Filesystem + participant Trc as tracing subscriber + participant PP as ProviderPool + participant LS as LocalStore (redb) + participant OBP as OrderBookPool + participant Sup as Supervisor + participant Mod as Module instance(s) + participant EL as Event Loop + + Op->>Bin: nexum-engine --engine-config engine.toml + Bin->>FS: read engine.toml + FS-->>Bin: EngineConfig { engine, chains, modules } + + Bin->>Trc: init_tracing(log_level) + Note over Trc: RUST_LOG overrides
engine.toml.log_level + + Bin->>PP: ProviderPool::from_config(&cfg) + loop For each chain in cfg.chains + alt URL is ws:// or wss:// + PP->>PP: ProviderBuilder.connect_ws(WsConnect) + else URL is http:// or https:// + PP->>PP: ProviderBuilder.connect_http(Url) + end + Note over PP: Connection failure = fatal,
engine refuses to start + end + PP-->>Bin: pool ready + + Bin->>LS: LocalStore::open(state_dir) + Note over LS: Creates redb file if missing,
materialises shared table. + LS-->>Bin: store ready + + Bin->>OBP: OrderBookPool::default() + Note over OBP: One OrderBookApi per
cowprotocol::Chain variant. + OBP-->>Bin: pool ready + + Bin->>Sup: Supervisor::boot(cfg, host_resources) + loop For each [[modules]] entry in cfg.modules + Sup->>FS: read module.toml + FS-->>Sup: Manifest + Sup->>FS: load .wasm + FS-->>Sup: wasm bytes + Sup->>Mod: wasmtime Component compile + instantiate
with dedicated HostState + Sup->>Mod: call init(config) inside write txn + Mod-->>Sup: Ok or Err + Sup->>Sup: record subscriptions declared in manifest + end + Sup-->>Bin: modules loaded + + Bin->>EL: open block + log subscriptions + EL->>PP: subscribe_blocks(chain_id) per unique chain + EL->>PP: subscribe_logs(chain_id, filter) per unique filter + PP-->>EL: streams ready + + Bin->>EL: run_event_loop(futures::select_all) + Note over EL: Loop until SIGINT or SIGTERM diff --git a/docs/diagrams/module-lifecycle.mmd b/docs/diagrams/module-lifecycle.mmd new file mode 100644 index 0000000..7a73bd1 --- /dev/null +++ b/docs/diagrams/module-lifecycle.mmd @@ -0,0 +1,39 @@ +stateDiagram-v2 + direction LR + + [*] --> Resolve + + Resolve --> Load: bundle fetched OK + Resolve --> Dead: resolution failed permanently + + Load --> Init: wasmtime compile and instantiate OK + Load --> Restart: compile or instantiate failed + + Init --> Run: init(config) returned Ok + Init --> Restart: init returned Err or trapped + + Run --> Run: on_event handled OK + Run --> Restart: on_event trapped or returned Err or fuel exhausted + + Restart --> Init: backoff expired (1s, 2s, 4s, up to 5min cap) + Restart --> Dead: max_consecutive_failures reached (default 10) + + Dead --> Init: operator action (nexum module restart or reload) + Dead --> [*]: operator removes module + + note right of Restart + Exponential backoff with jitter. + Memory zeroed, local-store survives. + InstancePre reused, no recompile. + end note + + note right of Dead + Module excluded from dispatch. + Engine continues running other modules. + Manual intervention required to resume. + end note + + note right of Init + Runs inside an implicit write txn. + Ok commits, Err rolls back. + end note diff --git a/docs/diagrams/sequence-ethflow.mmd b/docs/diagrams/sequence-ethflow.mmd new file mode 100644 index 0000000..7427779 --- /dev/null +++ b/docs/diagrams/sequence-ethflow.mmd @@ -0,0 +1,39 @@ +sequenceDiagram + autonumber + actor User as User
(wallet) + participant Contract as CoWSwapEthFlow
(on-chain) + participant RPC as Chain RPC
(WSS) + participant EL as nexum-engine
Event Loop + participant Mod as ethflow-watcher
(WASM module) + participant SolDec as alloy_sol_types
(in module) + participant Cow as cowprotocol crate
(in module, wasm32) + participant Host as HostState + participant OB as CoW Orderbook + + User->>Contract: createOrder(order, msg.value=ETH) + Contract->>Contract: store orders[hash] = onchainData,
emit OrderPlacement + Contract-->>RPC: log emitted on block N + + RPC-->>EL: eth_subscribe logs delivery + EL->>Mod: on_event(LogEvent) + + Mod->>SolDec: decode OrderPlacement(sender, order, sig, data) + SolDec-->>Mod: (sender, GPv2OrderData, EIP1271-sig, data) + + Mod->>Cow: build OrderCreation with EIP-1271 scheme
pointing at CoWSwapEthFlow contract + Cow-->>Mod: OrderCreation + OrderUid + + Mod->>Host: cow-api.submit-order(chain_id, order_json) + Host->>OB: POST /api/v1/orders + OB-->>Host: result + + alt 200 OK with OrderUid + Host-->>Mod: Ok(OrderUid) + Mod->>Host: local-store.set("submitted:{uid}", ...) + else 4xx with error code + Host-->>Mod: Err(host-error with code) + Mod->>Cow: OrderPostError::try_from(host-error).retry_hint() + Cow-->>Mod: RetryHint variant + end + + Note over Mod: Module applies RetryHint:
TryNextBlock - log + skip
BackoffSeconds(s) - persist next_attempt = now + s
Drop - permanent failure, do not retry diff --git a/docs/diagrams/sequence-twap.mmd b/docs/diagrams/sequence-twap.mmd new file mode 100644 index 0000000..5b5e75e --- /dev/null +++ b/docs/diagrams/sequence-twap.mmd @@ -0,0 +1,56 @@ +sequenceDiagram + autonumber + actor User as User
(wallet) + participant Contract as ComposableCoW
(on-chain) + participant RPC as Chain RPC
(WSS) + participant EL as nexum-engine
Event Loop + participant Mod as twap-monitor
(WASM module) + participant SolDec as alloy_sol_types
(in module) + participant Cow as cowprotocol crate
(in module, wasm32) + participant Host as HostState
(host backends) + participant OB as CoW Orderbook + + Note over User,Contract: Registration (one-time per TWAP) + User->>Contract: create(twapParams) + Contract-->>RPC: emit ConditionalOrderCreated(owner, params) + RPC-->>EL: log delivered + EL->>Mod: on_event(LogEvent) + Mod->>SolDec: decode ConditionalOrderCreated + SolDec-->>Mod: (owner, params, salt) + Mod->>Host: local-store.set("watch:{owner}:{hash}", params) + + Note over EL,OB: Per-block polling loop + loop For each block N + RPC-->>EL: eth_subscribe newHeads + EL->>Mod: on_event(BlockEvent { N }) + Mod->>Host: local-store.list_keys("watch:") + + loop For each watch where next_attempt <= N + Mod->>Host: chain.request(chain_id, "eth_call", [...]) + Host->>RPC: eth_call ComposableCoW.
getTradeableOrderWithSignature(owner, params) + RPC-->>Host: return value or revert + Host-->>Mod: result (JSON encoded) + Mod->>SolDec: decode return or interpret revert reason + SolDec-->>Mod: PollOutcome (module-defined enum) + + alt Ready (order, signature) + Mod->>Cow: build OrderCreation + Cow-->>Mod: OrderCreation + Mod->>Host: cow-api.submit-order(chain_id, order_json) + Host->>OB: POST /api/v1/orders + OB-->>Host: Ok(OrderUid) or Err(error_code) + Host-->>Mod: Result + alt Ok(uid) + Mod->>Host: local-store.set("submitted:{uid}", ...) + else Err + Mod->>Cow: OrderPostError::try_from(host-error)
.retry_hint() + Cow-->>Mod: TryNextBlock / BackoffSeconds(s) / Drop + Mod->>Host: local-store update
(next_attempt or remove watch) + end + else NotReady (try at epoch t) + Mod->>Host: local-store.set("watch:...next_attempt", t) + else Terminal (TWAP completed or cancelled) + Mod->>Host: local-store.delete("watch:...") + end + end + end diff --git a/docs/diagrams/subscription-dispatch.mmd b/docs/diagrams/subscription-dispatch.mmd new file mode 100644 index 0000000..6f11359 --- /dev/null +++ b/docs/diagrams/subscription-dispatch.mmd @@ -0,0 +1,61 @@ +graph TB + subgraph manifests["Module Manifests (module.toml per module)"] + M1S["module twap-monitor:
[[subscription]] kind=log
chain=1, address=ComposableCoW,
topics=[ConditionalOrderCreated]
[[subscription]] kind=block, chain=1"] + M2S["module ethflow-watcher:
[[subscription]] kind=log
chain=1, address=CowEthFlow,
topics=[OrderPlacement]"] + M3S["module other-module:
[[subscription]] kind=block, chain=1"] + end + + subgraph supervisor["Supervisor: aggregate at boot"] + BlockAgg["Block subscriptions
grouped by chain_id"] + LogAgg["Log subscriptions
grouped by (chain_id, filter_key)
where filter_key = address+topics hash"] + end + + subgraph subs["Aggregated WSS subscriptions"] + BSub["eth_subscribe newHeads
chain=1 (shared by all subscribers)"] + LSub1["eth_subscribe logs
chain=1, filter=ComposableCoW+ConditionalOrderCreated
(owner: twap-monitor)"] + LSub2["eth_subscribe logs
chain=1, filter=CowEthFlow+OrderPlacement
(owner: ethflow-watcher)"] + end + + subgraph dispatch["Event Loop dispatch"] + Decide{Event kind?} + Broadcast["Fan-out: dispatch to ALL
subscribers of that chain"] + Route["Route: dispatch to OWNER
of matching filter_key only"] + end + + subgraph delivery["Module on_event invocations"] + D1["twap-monitor.on_event(BlockEvent)"] + D2["ethflow-watcher.on_event(LogEvent)
or twap-monitor (LogEvent)"] + D3["other-module.on_event(BlockEvent)"] + end + + M1S --> BlockAgg + M1S --> LogAgg + M2S --> LogAgg + M3S --> BlockAgg + + BlockAgg --> BSub + LogAgg --> LSub1 + LogAgg --> LSub2 + + BSub --> Decide + LSub1 --> Decide + LSub2 --> Decide + + Decide -- "block" --> Broadcast + Decide -- "log" --> Route + + Broadcast --> D1 + Broadcast --> D3 + Route --> D2 + + classDef manifest fill:#fff4e6,stroke:#ff9800,color:#000 + classDef agg fill:#e6f3ff,stroke:#1e88e5,color:#000 + classDef wss fill:#f0e6ff,stroke:#7e3ff2,color:#000 + classDef decide fill:#fffde7,stroke:#fbc02d,color:#000 + classDef dispatch fill:#e6ffe6,stroke:#2e7d32,color:#000 + + class M1S,M2S,M3S manifest + class BlockAgg,LogAgg agg + class BSub,LSub1,LSub2 wss + class Decide,Broadcast,Route decide + class D1,D2,D3 dispatch diff --git a/docs/diagrams/wit-call-path.mmd b/docs/diagrams/wit-call-path.mmd new file mode 100644 index 0000000..5fe4159 --- /dev/null +++ b/docs/diagrams/wit-call-path.mmd @@ -0,0 +1,34 @@ +sequenceDiagram + autonumber + participant Src as Module Rust source
(twap-monitor/src/lib.rs) + participant Bindgen as wit-bindgen
(generated stubs) + participant Wasm as WASM Component
(twap.wasm) + participant Wt as wasmtime instance + participant Lk as wasmtime Linker + participant HS as HostState (Rust) + participant PP as ProviderPool + participant Alloy as alloy provider + participant RPC as Chain RPC + + Note over Src,Wasm: Build phase (cargo build) + Src->>Bindgen: chain::request(chain_id, method, params)
(plain Rust call) + Bindgen->>Wasm: compile into WASM Component
with "nexum:host/chain" import + + Note over Wasm,RPC: Runtime: module calls chain::request + Wasm->>Wt: WASM import call:
"nexum:host/chain"."request" + Wt->>Lk: lookup binding for import + Lk->>HS: invoke trait impl:
HostState::Chain::request(...) + Note over HS: tracing::info!("[chain] request
chain=... method=...") + HS->>PP: pool.get(chain_id) + PP-->>HS: &DynProvider + HS->>Alloy: provider.raw_request(method, params) + Alloy->>RPC: JSON-RPC over WSS/HTTP + RPC-->>Alloy: response bytes + Alloy-->>HS: serde_json::Value + HS-->>Lk: Ok(stringified JSON) + Lk-->>Wt: return value (marshaled) + Wt-->>Wasm: import call returns + Wasm-->>Bindgen: unmarshal canonical ABI
back to Rust types + Bindgen-->>Src: returns Result + + Note over Src,RPC: All host calls follow this path.
Only the trait impl on the right side
(HS → PP/OBP/LS/...) changes per capability. diff --git a/docs/migration/0.1-to-0.2.md b/docs/migration/0.1-to-0.2.md index 8a036ab..ece1be7 100644 --- a/docs/migration/0.1-to-0.2.md +++ b/docs/migration/0.1-to-0.2.md @@ -4,14 +4,14 @@ Nexum 0.2 is a single coordinated breaking-change release. It does the renames, This guide is written for two audiences: -- **Module authors** — you write WASM components that import the Nexum WIT. -- **Host embedders** — you build the runtime that loads modules (the server daemon, a mobile wallet, a browser host). +- **Module authors** - you write WASM components that import the Nexum WIT. +- **Host embedders** - you build the runtime that loads modules (the server daemon, a mobile wallet, a browser host). Each section is tagged `[author]`, `[embedder]`, or `[both]`. --- -## TL;DR — what changed [both] +## TL;DR - what changed [both] | Area | 0.1 | 0.2 | |---|---|---| @@ -30,9 +30,9 @@ Each section is tagged `[author]`, `[embedder]`, or `[both]`. | Manifest field | `wasm = "sha256:..."` | `component = "sha256:..."` | | Manifest section | `[[subscribe]]` | `[[subscription]]` | | Config type | `list>` (stringified) | unchanged in 0.2; typed variant on the 0.3 roadmap | -| New capabilities | — | `clock`, `random`, `http` (allowlisted) | -| New RPC method | — | `chain::request-batch` (additive) | -| New world | — | `query-module` (experimental, no host impl shipped) | +| New capabilities | - | `clock`, `random`, `http` (allowlisted) | +| New RPC method | - | `chain::request-batch` (additive) | +| New world | - | `query-module` (experimental, no host impl shipped) | If you only do four things: update your `nexum.toml`, run the sed cheat-sheet at the bottom, replace your error handling with the new `host-error` taxonomy, and declare your capabilities explicitly. Everything else is mechanical. @@ -138,7 +138,7 @@ The five 0.1 error shapes (`json-rpc-error`, `identity-error`, `msg-error`, `sto interface types { record host-error { domain: string, // "chain" | "store" | "messaging" | "identity" | "cow" | ... - kind: host-error-kind, // normative discriminant — see below + kind: host-error-kind, // normative discriminant - see below code: s32, // domain-specific message: string, data: option, // JSON for richer context @@ -174,7 +174,7 @@ interface types { + } ``` -`local-store` errors are no longer bare `string`s. The same `host-error` shape applies — `domain: "store"`, `kind` indicates whether you hit a quota, the key doesn't exist (for write-conditional ops), etc. +`local-store` errors are no longer bare `string`s. The same `host-error` shape applies - `domain: "store"`, `kind` indicates whether you hit a quota, the key doesn't exist (for write-conditional ops), etc. Module export signatures also change: @@ -239,7 +239,7 @@ If any code, docs, or scripts reference `shepherd.toml`, change to `nexum.toml`. ### Capability declaration (new, required) -In 0.1 the world declared which interfaces a module imported, and instantiation failed if any were unsatisfied. In 0.2, imports declared `optional` in the manifest install a trap stub on the host side — calling them returns `host-error { kind: unsupported }` rather than failing instantiation. +In 0.1 the world declared which interfaces a module imported, and instantiation failed if any were unsatisfied. In 0.2, imports declared `optional` in the manifest install a trap stub on the host side - calling them returns `host-error { kind: unsupported }` rather than failing instantiation. ```toml [capabilities] @@ -254,11 +254,11 @@ allow = ["api.coingecko.com", "discord.com"] methods = ["sign-typed-data"] # subset of identity surface used ``` -If you omit `[capabilities]` entirely, 0.2 falls back to "all imports required" — same as 0.1 behaviour — and prints a deprecation warning at load. Add the section in your next module update; the implicit-all fallback will be removed in 0.3. +If you omit `[capabilities]` entirely, 0.2 falls back to "all imports required" - same as 0.1 behaviour - and prints a deprecation warning at load. Add the section in your next module update; the implicit-all fallback will be removed in 0.3. ### Config: unchanged in 0.2 -`[config]` values continue to flow through to the guest as `list>` — the host flattens TOML scalars (numbers, booleans) to their string form on the way through, same as 0.1. If you currently parse `"50"` into `u64`, that code continues to work unchanged: +`[config]` values continue to flow through to the guest as `list>` - the host flattens TOML scalars (numbers, booleans) to their string form on the way through, same as 0.1. If you currently parse `"50"` into `u64`, that code continues to work unchanged: ```rust let bps: u64 = config.iter() @@ -373,7 +373,7 @@ interface chain { } ``` -Additive. The alloy-backed `HostTransport` now routes `RequestPacket::Batch` through `request-batch` — your existing `provider.multicall(...).await` actually batches on the wire in 0.2 (it didn't in 0.1, despite the docs). +Additive. The alloy-backed `HostTransport` now routes `RequestPacket::Batch` through `request-batch` - your existing `provider.multicall(...).await` actually batches on the wire in 0.2 (it didn't in 0.1, despite the docs). --- @@ -441,7 +441,7 @@ The Rust API surface is otherwise unchanged in 0.2. The C ABI and `nexum-host` e ### Non-Rust SDKs -The WIT renames propagate mechanically through `wit-bindgen`. Regenerate your bindings against the 0.2 WIT and your existing call sites — adjusted for the renames in §1 — will type-check. +The WIT renames propagate mechanically through `wit-bindgen`. Regenerate your bindings against the 0.2 WIT and your existing call sites - adjusted for the renames in §1 - will type-check. --- @@ -453,7 +453,7 @@ For mechanical search/replace in your codebase. Apply in order; some replacement # WIT package rg -l 'web3:runtime' | xargs sed -i 's/web3:runtime/nexum:host/g' -# Interface names (do these before function names — some functions reference the old interface in paths) +# Interface names (do these before function names - some functions reference the old interface in paths) rg -l '\bcsn\b' | xargs sed -i 's/\bcsn\b/chain/g' rg -l '\bmsg\b' | xargs sed -i 's/\bmsg\b/messaging/g' @@ -489,7 +489,7 @@ rg -l '\[\[subscribe\]\]' | xargs sed -i 's/\[\[subscribe\]\]/[[subscription]]/g rg -l '^wasm = ' | xargs sed -i 's/^wasm = /component = /' ``` -Things that **cannot** be sedded — do these by hand: +Things that **cannot** be sedded - do these by hand: - `timer(u64)` → `tick(tick)` with the new `tick { fired-at: u64 }` record. Call sites that pattern-match `Event::Timer(ts)` become `Event::Tick(tick) => tick.fired_at`. - Error handling. The five old error types are gone; you can't mechanically rewrite a `match` against `JsonRpcError { code, .. }` into the new `HostError { kind, .. }` discriminant. Do these per-call-site. @@ -505,7 +505,7 @@ After running the renames: - [ ] `cargo check --workspace --all-targets` is clean (Rust + bindings). - [ ] `cargo check --target wasm32-wasip2 -p ` is clean. - [ ] `cargo test --workspace --no-fail-fast` passes. -- [ ] Your bindgen invocations point at the package's own WIT dir (`wit/nexum-host/`) — or, when consuming both `nexum:host` and a domain-extension package, list both paths explicitly. The 0.1 vendored `deps/` pattern is no longer used in the reference repo. +- [ ] Your bindgen invocations point at the package's own WIT dir (`wit/nexum-host/`) - or, when consuming both `nexum:host` and a domain-extension package, list both paths explicitly. The 0.1 vendored `deps/` pattern is no longer used in the reference repo. - [ ] `nexum.toml` has a `[capabilities]` section listing what the module uses. - [ ] `nexum.toml` references `component = "sha256:..."` not `wasm = ...`. - [ ] All `[[subscribe]]` sections renamed to `[[subscription]]` with `kind` (not `type`). diff --git a/docs/operations/m2-testnet-runbook.md b/docs/operations/m2-testnet-runbook.md new file mode 100644 index 0000000..01f6bfb --- /dev/null +++ b/docs/operations/m2-testnet-runbook.md @@ -0,0 +1,241 @@ +# M2 testnet runbook (Sepolia) + +How to actually run the M2 modules - twap-monitor and ethflow-watcher - +on Sepolia and exercise the full path the unit tests cannot: real +`eth_subscribe` streams, real `eth_call` reverts, real orderbook +submissions. + +Two flavours: + +1. **Smoke run**: boot the engine, watch the supervisor pick up every + `ConditionalOrderCreated` / `OrderPlacement` log that lands on + Sepolia. Passive; you do not produce traffic. 15-30 min wall clock. +2. **Round-trip run**: smoke run plus you author a TWAP order via a + Sepolia Safe and an EthFlow swap via the public CoW Swap UI. The + engine indexes / decodes / submits. 1-2 h. + +Both share the same boot. The round-trip is the smoke run with a hand +on the wheel. + +--- + +## 0. Prerequisites + +- Rust toolchain matching `rust-toolchain.toml` (nightly with + `wasm32-wasip2` target). `rustup target add wasm32-wasip2` once. +- `just` (`cargo install just` or `brew install just`). +- Sepolia RPC. Public endpoint in `engine.m2.toml` works for short + runs; switch to Alchemy/Infura with a key for anything past ~20 min. +- For the round-trip: + - A Sepolia EOA with some test ETH ([Alchemy faucet](https://sepoliafaucet.com)). + - A [Sepolia Safe](https://app.safe.global/?chain=sep) (only for the + TWAP half). + +--- + +## 1. Smoke run + +```bash +just run-m2 +``` + +Equivalent long form: + +```bash +cargo build -p twap-monitor --target wasm32-wasip2 --release +cargo build -p ethflow-watcher --target wasm32-wasip2 --release +cargo run -p nexum-engine -- --engine-config engine.m2.toml +``` + +### What you should see in the first ~5 seconds (observed) + +``` +INFO nexum_engine nexum-engine starting +INFO nexum_engine::host::provider_pool opening chain RPC provider chain_id=11155111 url="wss://..." +INFO nexum_engine::supervisor loading module manifest manifest=modules/twap-monitor/module.toml +[manifest] required capabilities: logging, local-store, chain, cow-api +INFO nexum_engine::supervisor compiling component component=target/wasm32-wasip2/release/twap_monitor.wasm +INFO nexum_engine::host::impls::logging twap-monitor init module="twap-monitor" +INFO nexum_engine::supervisor init succeeded module=twap-monitor +INFO nexum_engine::supervisor loading module manifest manifest=modules/ethflow-watcher/module.toml +[manifest] required capabilities: logging, local-store, chain, cow-api +INFO nexum_engine::supervisor compiling component component=target/wasm32-wasip2/release/ethflow_watcher.wasm +INFO nexum_engine::host::impls::logging ethflow-watcher init module="ethflow-watcher" +INFO nexum_engine::supervisor init succeeded module=ethflow-watcher +INFO nexum_engine::supervisor supervisor up count=2 +INFO nexum_engine supervisor ready modules=2 chains=1 +INFO nexum_engine::runtime::event_loop block subscription open chain_id=11155111 +INFO nexum_engine::runtime::event_loop log subscription open module=twap-monitor chain_id=11155111 +INFO nexum_engine::runtime::event_loop log subscription open module=ethflow-watcher chain_id=11155111 +``` + +Then every ~12s (Sepolia block time): + +``` +INFO nexum_engine::runtime::event_loop dispatch block chain_id=11155111 number=N +``` + +### What to verify + +| Check | How | +|---|---| +| Both modules booted | `module_count: 2` + 2 `loaded module` lines | +| Subscriptions wired | 2 log subs + 1 block sub | +| No traps in the first 10 blocks | `alive: 2` stays at 2; no `module ... trapped` lines | +| State persistence works | `ls data/m2/` shows `ls.redb` growing | + +### Stopping cleanly + +Ctrl-C. Tear down `./data/m2/` between runs if you want a fresh slate. + +### Common surprises + +- **Public RPC throttles after a few minutes.** Symptom: `eth_subscribe` + reconnects in a loop. Fix: switch to Alchemy/Infura. Edit the + `[chains.11155111]` block in `engine.m2.toml` (env-substitution is + not wired yet). +- **You see `eth_call failed (...); defaulting to TryNextBlock`.** This + is twap-monitor polling watches that are still empty (no + `ConditionalOrderCreated` indexed yet). Expected on a fresh `./data/m2`. +- **You see NO log dispatches for hours.** Sepolia has low ComposableCoW + / EthFlow traffic. The smoke run is mostly a "stay alive" test until + you produce events yourself (see round-trip below). + +--- + +## 2. Round-trip run + +Same boot as #1; you produce the events. + +### 2a. TWAP half (via Safe + Compose) + +The TWAP flow lives behind a Safe, not an EOA, because ComposableCoW +expects the conditional-order owner to be an EIP-1271 verifier. + +1. **Create a Sepolia Safe** at . + Single signer with your EOA is fine. Fund it with ~0.05 Sepolia + ETH (gas) and ~10 of a Sepolia ERC-20 you want to sell. +2. **Install the Compose app** in the Safe. CoW Protocol publishes the + ComposableCoW Watch Tower as a Safe app on Sepolia. + - In Safe -> Apps -> Add custom app: use the URL from + README ("Add to + Safe"). +3. **Author a TWAP order**. Compose UI -> "TWAP". Recommended for the + first run: + - Sell: 1 of your test ERC-20. + - Buy: any Sepolia stable. + - Split into 2 parts, 5-minute interval, validity 30 min. + - Confirm + sign the Safe tx. +4. **Watch the engine logs.** Within ~12s of the Safe tx confirming, + you should see: + ``` + INFO twap-monitor indexed watch:0x:0x + ``` + Then on the next blocks where the tranche is ready: + ``` + INFO twap-monitor poll watch:... -> Ready + INFO twap-monitor submitted submitted:0x + ``` + Sometimes you see `TryAtEpoch(t)` instead of `Ready` - that means + the tranche is gated until time `t`. Wait the configured interval. +5. **Confirm on the orderbook.** Get the UID from the log, then: + ```bash + curl https://api.cow.fi/sepolia/api/v1/orders/0x + ``` + You should see the order JSON back. Trade settlement on Sepolia is + spotty (solvers do not always pick up); the goal of this test is + that the order reached the orderbook, not that it filled. + +### 2b. EthFlow half (via swap.cow.fi) + +EthFlow does not need a Safe - any EOA works. + +1. Go to (Sepolia native + ETH selector). +2. Connect your EOA, select a small swap (e.g. 0.001 SETH -> any + token), confirm. +3. The CoWSwapEthFlow contract on Sepolia + (`0xbA3cB4...EadeC`) emits `OrderPlacement`. +4. **Watch the engine logs:** + ``` + INFO ethflow-watcher ethflow submitted 0x + ``` + If you see `ethflow backoff 0x ...` instead: orderbook + classified the submit as retriable. Wait one block, the watcher + does not retry on its own today (planned for M4 supervisor + restart wiring). + + If you see `ethflow dropped 0x ...`: orderbook rejected + permanently (most likely `DuplicateOrder` - CoW Swap submits the + order itself first, ethflow-watcher races and loses). Expected; the + `dropped:{uid}` row is the regression guard for COW-1064 not the + failure signal here. + +### What "passing M2 round-trip" looks like + +- At least one `submitted:{uid}` row in `data/m2/ls.redb` written by + each module. +- Both modules still alive (`alive: 2`) at the end of the run. +- Zero `module ... trapped` lines in the engine log. +- `curl api.cow.fi/sepolia/api/v1/orders/` returns the order JSON + for at least one submitted UID (`null` means the orderbook never + accepted; non-null means we round-tripped). + +--- + +## 3. Inspecting state after a run + +The local-store is a redb file. Quick inspection without writing a +tool: + +```bash +# Build the example mini-CLI the engine ships +cargo run -p nexum-engine --bin ls-dump -- data/m2/ls.redb 2>/dev/null \ + || echo "no ls-dump bin in 0.2 - read via the engine on next boot" +``` + +Today the canonical way to read the store is to boot the engine again +on the same `state_dir`: the supervisor logs every `watch:` / +`submitted:` / `dropped:` row it loads. A proper inspector is +production-hardening scope (M4, see COW-1030). + +--- + +## 4. What this run does NOT prove + +- **Throughput / soak stability**. That is COW-1031 (7-day soak). +- **Cross-module isolation under load**. That is COW-1064 (4-6h + multi-module e2e). The local-store namespace test guarantees the + invariant in unit; the runbook above is a single-Safe / single-EOA + setup. +- **Resource-limit enforcement under adversarial guests**. COW-1036 + (fuel + memory tests in M4). +- **Security review**. COW-1065. + +The M2 runbook covers: "does the engine actually boot the two M2 +modules end-to-end against Sepolia, route real subscription events +through the wit-bindgen + WitBindgenHost path, and round-trip orders +to the CoW orderbook". That is the deliverable M2 is responsible for. + +--- + +## 5. Troubleshooting + +| Symptom | Likely cause | Fix | +|---|---|---| +| `connection refused` / WS retries | Public node throttled | Switch RPC to Alchemy / Infura | +| `module twap-monitor trapped: OutOfFuel` | Dispatch path exceeded fuel budget | Almost certainly an upstream issue, file under COW-1036; raise `[engine.limits]` fuel temporarily | +| `eth_call failed (rate limited)` repeatedly | Public node | Same as above | +| `ParseManifestError: missing capability cow-api` | Engine version mismatch with module.toml | `cargo build -p nexum-engine --release` and use the fresh binary | +| `data/m2/ls.redb` not created | `state_dir` not writable | Check permissions, or change `state_dir` in `engine.m2.toml` | + +--- + +## 6. References + +- Engine config schema: `crates/nexum-engine/src/engine_config.rs` +- M2 modules: `modules/twap-monitor/`, `modules/ethflow-watcher/` +- ADR-0005 (cow-api routing): `docs/adr/0005-cow-api-via-cached-orderbookapi.md` +- ADR-0006 (twap + ethflow helpers): `docs/adr/0006-cow-twap-ethflow-host-helpers.md` +- ADR-0009 (host trait surface): `docs/adr/0009-host-trait-surface.md` +- M2 PRs in `bleu/nullis-shepherd`: #2-#11 diff --git a/docs/operations/m3-edge-case-validation.md b/docs/operations/m3-edge-case-validation.md new file mode 100644 index 0000000..2649429 --- /dev/null +++ b/docs/operations/m3-edge-case-validation.md @@ -0,0 +1,231 @@ +# M3 testnet edge-case validation (2026-06-18) + +Five edge cases run against the live `engine.m3.toml` boot on Sepolia. +Each takes ~10-15 s of wall clock; together they exercise the error +paths the runbook section 1 cannot cover passively. **All five +passed with one minor observation** (init-failed module stays +`alive=true`; safe in practice, worth a follow-up issue). + +Run on commit `feat/m3-edge-case-validation` tip; engine debug log +level. + +--- + +## 1.1 Bad RPC URL -> structured connect error, clean exit + +**Mutation**: `engine.m3.toml` `rpc_url = "wss://nonexistent.example.com"`. + +**Observed**: + +``` +INFO nexum-engine starting +INFO opening chain RPC provider chain_id=11155111 url="wss://nonexistent.example.com" +Error: connect chain 11155111: IO error: failed to lookup address information: + nodename nor servname provided, or not known +``` + +**Verdict**: ✅ engine exits with structured `connect chain N: ...` +error chain. No panic, no retry loop, no silent hang. Operator +gets a clear cue to fix the URL. + +**Implication**: an operator misconfiguring an RPC URL fails fast and +loud. Combined with the supervisor restart loop (M4 BLEU-1033), +this gives "kill engine, fix config, restart, no orphaned state". + +--- + +## 1.2 Bad oracle address -> module Warn + stays alive + +**Mutation**: `modules/examples/price-alert/module.toml::[config]` +`oracle_address = "0x0000000000000000000000000000000000000001"` (an +EOA with no code; `eth_call` returns empty bytes). + +**Observed**: boot clean; on the first block: + +``` +WARN price-alert: latestRoundData decode failed: + ABI decoding failed: buffer overrun while deserializing +``` + +Engine stays at `supervisor up count=3`; balance-tracker and +stop-loss continue to operate normally. + +**Verdict**: ✅ module gracefully handles upstream giving the wrong +shape. The decode error names the failing call (`latestRoundData`) +and the failure mode (buffer overrun), so an operator can correlate +to a misconfigured `oracle_address` without reading source. + +**Implication**: validates the SDK error model end-to-end: +`chain::request` returns Ok with empty bytes, `parse_eth_call_result` +returns `Some(vec![])`, `latestRoundDataCall::abi_decode_returns` +fails with `alloy_sol_types::Error::Buffer overrun`, the strategy's +`map_err` surfaces it as a `Warn` log via `LoggingHost::log`. All +four host traits + the `cow` helper path exercised. + +--- + +## 1.3 Capability mismatch -> boot rejects module + +**Mutation**: `modules/examples/stop-loss/module.toml::[capabilities]` +`required = ["logging"]` (dropped `chain`, `local-store`, `cow-api`). + +**Observed**: + +``` +INFO loading module manifest manifest=modules/examples/stop-loss/module.toml +[manifest] required capabilities: logging +INFO compiling component component=...stop_loss.wasm +Error: load module target/wasm32-wasip2/release/stop_loss.wasm + +Caused by: + 0: capability violation in target/wasm32-wasip2/release/stop_loss.wasm + 1: component imports `cow-api` (shepherd:cow/cow-api@0.2.0) but it + is not listed in [capabilities].required or [capabilities].optional +``` + +Engine exits with non-zero. The whole boot fails because the +supervisor cannot honour the (intentionally under-declared) manifest. + +**Verdict**: ✅ the capability security boundary is enforced at module +load, not deferred to first host call. Error chain identifies the +specific `cow-api` import that the manifest does not authorise. This +is the BLEU-816 (`enforce capability declarations at module +instantiation`, COW-1025) Done invariant working in production. + +**Implication**: a malicious or buggy module cannot import a host +capability without explicitly declaring it. This is the M3 SDK +contract's core security guarantee. + +--- + +## 1.4 Malformed `[config]` -> init returns typed `InvalidInput` + +**Mutation**: `modules/examples/price-alert/module.toml::[config]` +`threshold = "not-a-number"`. + +**Observed**: + +``` +INFO loading module manifest manifest=modules/examples/price-alert/module.toml +WARN init failed + module=price-alert + domain=price-alert + kind=HostErrorKind::InvalidInput + code=0 + "price-alert: invalid [config]: threshold: non-digit character in + \"not-a-number\"" +INFO balance-tracker init: 2 addresses, ... +INFO init succeeded module=balance-tracker +INFO stop-loss init: owner=..., trigger=..., ... +INFO init succeeded module=stop-loss +INFO supervisor up count=3 +``` + +**Verdict**: ✅ init failure isolated to the offending module. +Balance-tracker and stop-loss boot normally. The typed `HostError` +carries `domain="price-alert"`, `kind=InvalidInput`, and a clear +message identifying the field + the invalid character. + +**Update (COW-1070, landed in this PR series)**: the supervisor now +flips `alive = false` when `init` returns `Err`, and the boot log +shows `supervisor up loaded=3 alive=2` so the discrepancy is +visible. Re-running scenario 1.4 against live Sepolia after the fix: + +``` +WARN init failed - module loaded but marked dead; dispatcher will skip it + module=price-alert kind=HostErrorKind::InvalidInput + "price-alert: invalid [config]: threshold: non-digit character in 'not-a-number'" +INFO supervisor up loaded=3 alive=2 +``` + +Subsequent block dispatches reach only the 2 alive modules; the +init-failed price-alert is now skipped by the dispatch fast-path +without surfacing the no-op fuel cost. Regression test: +`supervisor::tests::init_failure_marks_module_dead_and_excludes_from_dispatch`. + +--- + +## 1.5 Persistence cross-restart -> redb file preserved + +**Mutation**: boot 1 with `rm -rf data/m3` (fresh state), then boot 2 +without rm. + +**Observed**: + +``` +=== Boot 1 (fresh) === +INFO balance-tracker init: 2 addresses, ... +INFO init succeeded module=balance-tracker +(stopped after 14s) + +=== State after boot 1 === +total 7200 +-rw-r--r-- brunotavaresdosanjos 3686400 data/m3/local-store.redb + +=== Boot 2 (state preserved) === +INFO balance-tracker init: 2 addresses, ... +INFO init succeeded module=balance-tracker +(stopped after 14s) +``` + +Both boots clean; `local-store.redb` file size stable (3.6 MB - redb +pre-allocates pages; actual key/value content is bytes, not MB). + +**Verdict**: ✅ the redb file survives `kill -TERM` cleanly, can be +re-opened on the next boot, and the supervisor reads from it +without corruption. This validates the 32-byte hash prefix +namespace (BLEU-814 / COW-1027 Done) in production: modules wrote +keys, the engine shut down, modules re-attached on restart, no +panic. + +**Implication**: the local-store invariant that BLEU-814 introduced +(`namespaces_isolate_modules` unit test + cross-restart durability) +is now confirmed against a real Sepolia run. Combined with the +supervisor integration tests (COW-1068), this is sufficient +evidence that local-store persistence works at the production +boundary, not only in mocks. + +**Caveat**: there is no built-in CLI to dump the redb contents, so +visual confirmation of specific keys (`last:0x...`, etc.) requires +either re-booting the engine on the same state_dir or writing an +ad-hoc inspector. Filed as a future M4-territory nice-to-have. + +--- + +## Summary + +| # | Scenario | Verdict | New issue? | +|---|---|---|---| +| 1.1 | Bad RPC URL | ✅ structured error + clean exit | no | +| 1.2 | Bad oracle address | ✅ Warn + module alive + clear decode error | no | +| 1.3 | Capability mismatch | ✅ boot rejects with structured error chain | no | +| 1.4 | Malformed `[config]` | ✅ typed `InvalidInput`; init-failed module marked dead + excluded from dispatch (COW-1070 fix) | resolved in this PR series | +| 1.5 | Cross-restart persistence | ✅ redb file preserved + re-attaches cleanly | no (a state-dump CLI would help; M4 nice-to-have) | + +**One follow-up issue**: in `Supervisor::load`, when `init` returns +`Err(HostError)`, set `alive=false` (or skip pushing the module into +`self.modules`). Subsequent dispatch wastes fuel on a no-op +short-circuit otherwise. Safe today; cleanup before M4. + +**Not in scope here** (M4 territory, already filed): +- Fuel exhaustion → COW-1036 +- Memory exhaustion → COW-1036 +- Module trap during `on_event` + restart with backoff → COW-1033 / COW-1032 +- WS reconnect logic instead of bail → not filed (current behaviour + is documented in `runtime/event_loop.rs` as "0.3 fix") + +--- + +## How to reproduce + +Each scenario is a one-line config mutation + `just run-m3` (or the +equivalent `cargo run`). Mutations are listed inline above. Restore +config between runs: + +```bash +git checkout modules/examples/price-alert/module.toml \ + modules/examples/stop-loss/module.toml \ + engine.m3.toml +``` + +Tested on commit `` at 2026-06-18, Sepolia public WS. diff --git a/docs/operations/m3-testnet-runbook.md b/docs/operations/m3-testnet-runbook.md new file mode 100644 index 0000000..3f98727 --- /dev/null +++ b/docs/operations/m3-testnet-runbook.md @@ -0,0 +1,208 @@ +# M3 testnet runbook (Sepolia) + +How to exercise the M3 example modules - price-alert, balance-tracker, +stop-loss - on Sepolia. Same shape as the M2 runbook but the modules +are different: + +- **price-alert** validates SDK `chain` helpers + Chainlink ABI decode. + Read-only; no on-chain or orderbook action. +- **balance-tracker** validates SDK `chain::request` (raw RPC) + + `local-store` per-key diff persistence. Read-only. +- **stop-loss** validates the full M3 surface: `chain::request` + + `local-store` dedup + `cow-api::submit-order` with + `Signature::PreSign`. Will attempt to submit a real CoW order to the + Sepolia orderbook when the oracle price crosses the trigger. + +In other words: M3 exercises the *strategy*-side SDK surface that M2 +modules eventually consume. The runbook below validates everything in +~8 seconds of wall clock against the real Sepolia ETH/USD Chainlink +feed. + +--- + +## 0. Prerequisites + +- Same as the M2 runbook (Rust nightly + `wasm32-wasip2`, `just` + optional, Sepolia RPC). +- For stop-loss to actually settle an order (not just submit and get + rejected) you also need: + - An EOA matching `[config] owner = ...` in + `modules/examples/stop-loss/module.toml` that has called + `setPreSignature(orderUid, true)` on the GPv2Settlement Sepolia + contract for the computed UID. + - That EOA holds + has approved enough of `sell_token` to settle. + + Without those, stop-loss will hit `TransferSimulationFailed` (or + `InvalidSignature` / `InsufficientAllowance`) and log it as a + retriable error or drop. **That outcome alone validates the + orderbook round-trip** - same shape as the M2 EthFlow validation. + +--- + +## 1. Smoke + active run + +The M3 modules all subscribe to blocks only and start working +immediately - there is no `[[subscription]] kind = "log"` to wait for. +A single Sepolia block (~12 s) drives all three through their full +strategy. + +```bash +just run-m3 +``` + +Equivalent long form: + +```bash +cargo build -p price-alert --target wasm32-wasip2 --release +cargo build -p balance-tracker --target wasm32-wasip2 --release +cargo build -p stop-loss --target wasm32-wasip2 --release +cargo run -p nexum-engine -- --engine-config engine.m3.toml +``` + +### What you should see in the first ~10 seconds (observed) + +``` +INFO nexum-engine starting +INFO opening chain RPC provider chain_id=11155111 url="wss://..." +INFO loading module manifest manifest=modules/examples/price-alert/module.toml +[manifest] required capabilities: logging, chain +INFO compiling component component=...price_alert.wasm +INFO price-alert init: oracle=0x694aa1769357215de4fac081bf1f309adc325306 + threshold=250000000000 direction=Below every_n_blocks=1 +INFO init succeeded module=price-alert +INFO loading module manifest manifest=modules/examples/balance-tracker/module.toml +[manifest] required capabilities: logging, chain, local-store +INFO compiling component component=...balance_tracker.wasm +INFO balance-tracker init: 2 addresses, threshold=100000000000000000 wei +INFO init succeeded module=balance-tracker +INFO loading module manifest manifest=modules/examples/stop-loss/module.toml +[manifest] required capabilities: logging, chain, local-store, cow-api +INFO compiling component component=...stop_loss.wasm +INFO stop-loss init: owner=0x70997970c51812dc3a010c7d01b50e0d17dc79c8 + trigger=250000000000 sell=0x6810e776880c02933d47db1b9fc05908e5386b96 + buy=0xfff9976782d46cc05630d1f6ebab18b2324d6b14 +INFO init succeeded module=stop-loss +INFO supervisor up count=3 +INFO supervisor ready modules=3 chains=1 +INFO block subscription open chain_id=11155111 +``` + +Then on the FIRST Sepolia block dispatch (~5-15s after boot): + +``` +DEBUG chain::request chain_id=11155111 method=eth_call # price-alert reads oracle +WARN price-alert: TRIGGERED answer=174553978080 threshold=250000000000 (Below) +DEBUG chain::request chain_id=11155111 method=eth_getBalance # balance-tracker addr 1 +DEBUG chain::request chain_id=11155111 method=eth_getBalance # balance-tracker addr 2 +DEBUG chain::request chain_id=11155111 method=eth_call # stop-loss reads oracle +DEBUG cow-api::submit-order chain_id=11155111 bytes=561 +WARN stop-loss retry on next block (0): orderbook error (TransferSimulationFailed): + sell token cannot be transferred +``` + +That single block proves the entire M3 strategy surface end-to-end: +oracle read + ABI decode + multi-key local-store + cow-api submit + +typed retry classification, all routed through real wit-bindgen + +WitBindgenHost + supervisor dispatch on a live testnet. + +### Why TRIGGERED fires immediately + +The default `threshold = "2500.00"` in `module.toml::[config]` is +above the Sepolia Chainlink ETH/USD feed (which tracks a stale or +mocked value, often around $1745). Direction is `below`, so the very +first poll trips the alert. Tune `threshold` if you want to test the +"silent" path. + +### Why stop-loss logs TransferSimulationFailed + +The default `owner = 0x70997970...` in stop-loss's config is the +canonical hardhat test EOA (`anvil` account index 1). It does not own +or approve the `sell_token` on Sepolia, so the orderbook simulates +the would-be settle and rejects with +`TransferSimulationFailed`. **This is the orderbook returning a typed +error - the full submit path worked.** The module's +`classify_api_error` SDK helper correctly tagged it as retriable +(`TryNextBlock`), so the watch is left in place for the next block. + +For the silent ("idle until trigger") run path, set `owner` to a real +EOA with the right allowances + pre-signature - see section 2 below. + +--- + +## 2. Active validation (optional) + +To see stop-loss actually submit + persist `submitted:{uid}` you need +to set up a real signed order: + +1. Pick a Sepolia EOA you control. +2. In `modules/examples/stop-loss/module.toml`, set `owner = "0x..."` + to that EOA. +3. Choose a `sell_token` / `buy_token` pair the EOA holds. +4. Compute the OrderUid the module will submit (the `build_creation` + helper in `strategy.rs` shows the construction; you can also boot + the engine once with a high trigger so it stays idle, then + simulate-decode the would-be submit by reading the supervisor's + debug log). +5. Call `GPv2Settlement.setPreSignature(uid, true)` from that EOA on + Sepolia. +6. Approve `sell_token` to the GPv2VaultRelayer for the sell amount. +7. Lower the `trigger_price` in `module.toml` so the next poll fires. + +On the next block: + +``` +INFO stop-loss TRIGGERED price=... trigger=... +DEBUG cow-api::submit-order ... +INFO stop-loss submitted submitted:0x +``` + +This is the M3 equivalent of the M2 EthFlow validation: same +end-to-end surface, different module. + +--- + +## 3. State inspection + +`./data/m3/ls.redb` accumulates the `last:{addr}` keys +(balance-tracker), `submitted:{uid}` / `dropped:{uid}` (stop-loss). +Same caveat as M2 - no `ls-dump` CLI today; reboot the engine on the +same `state_dir` and the supervisor logs every key it loads. + +`rm -rf ./data/m3` between runs for a fresh slate. + +--- + +## 4. What this does NOT prove + +Same boundary as M2's section 4: + +- Throughput / 7-day soak -> COW-1031. +- Cross-module isolation under load -> COW-1064 (4-6 h e2e). +- Adversarial resource exhaustion -> COW-1036. +- Security review -> COW-1065. +- `app_data` resolution for stop-loss orders with non-empty metadata + -> M5 (typed `Cow` client with `raw_request`). + +--- + +## 5. Troubleshooting + +Most of the M2 runbook's section 5 applies verbatim. M3-specific: + +| Symptom | Likely cause | Fix | +|---|---|---| +| `module stop-loss trapped: TransferSimulationFailed` | Trap vs warn confusion | The "sell token cannot be transferred" line is a Warn, not a trap. Module stays alive. Read again carefully. | +| Engine bails immediately with `log stream ended (WebSocket dropped?)` | Pre-fix M1 bug | Should not happen on this commit. The fix lands in `runtime/event_loop.rs`: `select_all` over empty `Vec` is replaced with `stream::pending()`. Regression test at `supervisor::tests::run_does_not_bail_when_both_stream_kinds_are_empty`. | +| `price-alert: TRIGGERED` does not fire | Oracle returned shape we cannot decode, or Sepolia public node throttled the `eth_call` | Check for `eth_call failed` warnings; switch to Alchemy. | +| `balance-tracker` only logs 1 of 2 addresses | RPC dropped a request mid-block | Same RPC throttle path; switch RPC. | + +--- + +## 6. References + +- M3 modules: `modules/examples/{price-alert,balance-tracker,stop-loss}/` +- SDK helpers exercised: `crates/shepherd-sdk/src/{chain,cow}/` +- ADR-0009 (host trait surface): `docs/adr/0009-host-trait-surface.md` +- M3 PRs in `bleu/nullis-shepherd`: #12-#26 (SDK + examples + tutorial + QA cleanup) +- M3 fix tail PRs: #27-#31 (CI matrix, rustdoc gate, doctests, supervisor integration, M2 runbook) +- M2 runbook (sister doc, same shape): `docs/operations/m2-testnet-runbook.md` diff --git a/docs/qa-signoff-cow-1063.md b/docs/qa-signoff-cow-1063.md new file mode 100644 index 0000000..929f081 --- /dev/null +++ b/docs/qa-signoff-cow-1063.md @@ -0,0 +1,81 @@ +# Internal QA sign-off - pre-upstream review pass + +**Tracking issue**: [COW-1063](https://linear.app/bleu-builders/issue/COW-1063) +**Branch**: `qa/cleanup-cow-1063` (tip of M2 + M3 stack) +**Generated**: 2026-06-17 + +## Mechanical checks (workspace-wide) + +| Check | Status | Notes | +|---|---|---| +| `cargo fmt --all --check` | ✅ | One pre-existing drift in `supervisor/tests.rs` (M1) plus M2/M3 leaf modules; bulk applied as single cleanup commit. | +| `cargo clippy --all-targets --workspace -- -D warnings` | ✅ | Clean. | +| `cargo test --workspace` | ✅ | 145 host tests + 1 doctest passing. | +| Em-dashes in `crates/`, `modules/`, `docs/` | ✅ | 0. One was in `price-alert/strategy.rs:4` (mine), fixed. | +| Em-dashes in `wit/**.wit` | ⚠ | 3 in mfw78's M1 prose. Intentionally left alone; flag for him in upstream review. | +| `warn(unused_crate_dependencies)` on every crate root | ✅ | sdk, sdk-test, nexum-engine, twap, ethflow, price-alert, balance-tracker, stop-loss. | +| WASM build (`wasm32-wasip2 --release`) | ✅ | All 5 modules build. Sizes: twap 314 KB, ethflow 282 KB, stop-loss 311 KB, price-alert 215 KB, balance-tracker 102 KB. | +| String-wrapped errors outside WIT boundary | ✅ | All hits in `crates/nexum-engine/src/host/impls/*` (FFI boundary - exception per rust-idiomatic skill). No leaks in SDK or modules. | + +## Per-PR shape + +| PR | Linear | Module | Tests | Strategy/lib split | Notes | +|---|---|---|---|---|---| +| #2-#7 | BLEU-825..830 (COW-1019..1024) | twap-monitor M2 | 13 | ❌ no split until BLEU-854 | Stacked TWAP. Strategy ↔ lib.rs split landed at #24. | +| #8-#10 | BLEU-831..833 | ethflow-watcher M2 | 7 | ❌ no split until BLEU-855 | Split landed at #25. | +| #11 | BLEU-834 | module.toml manifests | - | - | Both M2 modules have manifests with capability + subscription comments. ✅ | +| #12 | BLEU-835 | shepherd-sdk skeleton | - | - | Public surface present. | +| #13 | BLEU-840 | sdk helpers extraction | - | - | OK. | +| #14 | BLEU-843 | M2 on SDK | - | - | M2 modules now consume `shepherd_sdk::cow` / `chain` helpers. | +| #15 | BLEU-841 | shepherd-sdk-test (MockHost) | 8 | - | Full mock surface; matches Host trait. | +| #16 | BLEU-844 | SDK docs | - | - | README + rustdoc on public items. **See architectural finding below.** | +| #17 | BLEU-836 | deployment guide | - | - | `docs/06-production-hardening.md` exists. | +| #18 | BLEU-846 | price-alert | 11 | ❌ no split until BLEU-851 | Refactor at #22. | +| #19 | BLEU-847 | balance-tracker | 13 | ❌ never refactored | Acceptable: balance-tracker has no submit path, dispatch matrix simpler. **Optional follow-up: bring to same shape for consistency.** | +| #20 | BLEU-848 | tutorial | - | - | Rewritten as guided tour at #23; reads top-to-bottom against real stop-loss source. | +| #21 | - | rust-idiomatic compliance | - | - | em-dash purge, thiserror, warn(unused_crate_dependencies). ✅ | +| #22 | BLEU-851 | price-alert host-trait | 16 | ✅ | Reference shape. | +| #23 | BLEU-852 | stop-loss | 7 | ✅ | First module with the full M3 surface (chain + local-store + cow-api + logging). | +| #24 | BLEU-854 | twap-monitor host-trait | 20 | ✅ | Strategy split; 7 new MockHost dispatch tests. | +| #25 | BLEU-855 | ethflow-watcher host-trait | 12 | ✅ | Strategy split; 5 new MockHost tests including PR #10 c5e4d7d regression guard. | + +## Architectural finding - DOC ↔ CODE divergence in M3 SDK + +**`docs/05-sdk-design.md` describes a 2-layer SDK that does not exist**: + +- `nexum-sdk` (universal) + `shepherd-sdk` (CoW extension) - we shipped only `shepherd-sdk`. No `nexum-sdk` crate. +- `#[nexum::module]` / `#[shepherd::module]` proc macros - not implemented. We use raw `wit_bindgen::generate!` + `WitBindgenHost` adapter pattern. +- Full alloy `Provider` backed by `HostTransport` - not implemented. We pass JSON-RPC method + params strings via `ChainHost::request`. +- Typed local-store helpers (serde over raw bytes) - not implemented. Modules call `host.set(&key, &value)` with raw bytes. +- Typed `Signer` for key management - not implemented. Modules use `Signature::PreSign` / `Signature::Eip1271`; no key custody on the module side. + +**Two paths**, mfw78's call: + +1. Update `docs/05-sdk-design.md` to describe what M3 actually shipped (Host traits + helpers + MockHost; defer proc macros, Provider, Signer, `nexum-sdk` split to M5+). +2. Or treat the doc as M5 north-star and implement the missing layers as part of M4 / M5 scope. + +Doc is currently aspirational; code is M3-scoped. They need to agree before upstream review. + +## Outstanding / deferred + +| Item | Issue | Status | +|---|---|---| +| `#[non_exhaustive]` batch on SDK public enums (`HostErrorKind`, `LogLevel`, `PollOutcome`, `RetryAction`) | COW-1029 (BLEU-853) | Held until just before upstream cut. | +| WIT-file em-dashes in upstream prose (3 occurrences) | - | Ask mfw78. | +| balance-tracker host-trait refactor (consistency with other 4 modules) | - | Optional follow-up. | +| PR description template (mfw78's "What does this PR do? / Why / Changes / Breaking changes / Testing / AI disclosure") | - | Cosmetic; could template-bump existing PR bodies before upstream push. | +| ADR for the M3 Host trait surface | - | None today. Worth one short ADR (0009 candidate) capturing the strategy/lib split decision before upstream review. | + +## Sign-off + +| Area | Ready for upstream? | +|---|---| +| M2 modules (twap + ethflow + manifests) | ✅ once PRs #24 + #25 land | +| M3 SDK + examples | ✅ pending doc 05 reconciliation | +| Tutorial | ✅ | +| Rust-idiomatic compliance | ✅ | +| Tests + builds | ✅ | +| Docs | ⚠ doc 05 vs code mismatch must resolve | +| ADRs | ⚠ M3 host trait surface lacks an ADR | + +**Recommendation**: address the two ⚠ items (doc 05 + ADR-0009) before opening the consolidated upstream PR. Everything else is green. diff --git a/docs/sdk.md b/docs/sdk.md new file mode 100644 index 0000000..11bed4b --- /dev/null +++ b/docs/sdk.md @@ -0,0 +1,86 @@ +# shepherd-sdk + +`shepherd-sdk` is the guest-side library every Shepherd module +consumes. It provides typed primitives, ABI helpers, an effect-trait +seam for testing, and a `prelude` that keeps boilerplate out of +module crates. + +This page is the entry point. The full API reference is the rustdoc +site under `target/doc/shepherd_sdk/`, generated by: + +```sh +RUSTDOCFLAGS="-D warnings -D missing-docs" cargo doc -p shepherd-sdk --no-deps --open +``` + +## Supported host capabilities + +`shepherd-sdk` is host-neutral - it does not call wit-bindgen- +generated functions directly. Instead, it exposes traits that mirror +the on-the-wire host interfaces, and modules adapt their wit-bindgen +imports to the traits at the cdylib boundary. The traits in +[`shepherd_sdk::host`][host-doc] are: + +| Trait | Mirrors | What it does | +|---|---|---| +| `ChainHost` | `nexum:host/chain@0.2.0` | JSON-RPC dispatch (`eth_call`, `eth_getLogs`, …) | +| `LocalStoreHost` | `nexum:host/local-store@0.2.0` | Per-module key-value store | +| `CowApiHost` | `shepherd:cow/cow-api@0.2.0` | Orderbook submission (`POST /api/v1/orders`) | +| `LoggingHost` | `nexum:host/logging@0.2.0` | Structured log lines tagged by module | +| `Host` | supertrait | Bundles the four; blanket impl | + +A module declaring `[capabilities].required = ["chain", "local-store", +"cow-api", "logging"]` in its `module.toml` matches the host trait +seam one-for-one. + +[host-doc]: ../target/doc/shepherd_sdk/host/index.html + +## Modules + +- [`prelude`](../target/doc/shepherd_sdk/prelude/index.html) - bulk + re-exports. `use shepherd_sdk::prelude::*;` and every module path + resolves: alloy primitives (`Address`, `B256`, `Bytes`, `U256`, + `keccak256`) plus cowprotocol order / signing / orderbook surface. + +- [`cow`](../target/doc/shepherd_sdk/cow/index.html) - CoW Protocol + bridging: + - `cow::order::gpv2_to_order_data` - convert the on-chain + `GPv2OrderData` (12-field Solidity tuple with bytes32 markers) + into the typed `OrderData` shape the orderbook signs against. + - `cow::composable::PollOutcome` + `cow::composable::decode_revert` + - typed dispatch over the five `IConditionalOrder` custom errors + (`OrderNotValid`, `PollTryNextBlock`, `PollTryAtBlock`, + `PollTryAtEpoch`, `PollNever`). + - `cow::error::RetryAction` + `cow::error::classify_api_error` - + map `cow_api::submit_order` failures into `TryNextBlock` / + `Backoff(s)` / `Drop`. + +- [`chain`](../target/doc/shepherd_sdk/chain/index.html) - `eth_call` + JSON plumbing: + - `chain::eth_call_params(to, data)` - build the `[{to, data}, + "latest"]` params array. + - `chain::parse_eth_call_result(json)` - parse the `"0x..."` hex + response into bytes. + - `chain::decode_revert_hex(s)` - `host-error.data` hex blob -> + typed `PollOutcome`. + +- [`host`](../target/doc/shepherd_sdk/host/index.html) - host trait + seam plus the SDK's host-neutral `HostError` (same field shape + as wit-bindgen's, bridged via one-liner `From` impls per module). + +## Companion: shepherd-sdk-test + +Add `shepherd-sdk-test` as a dev-dep on the module crate to write +strategy tests against in-memory mocks. See its +[README](../crates/shepherd-sdk-test/src/lib.rs) for the usage +pattern. + +## Versioning + +The SDK is currently `0.1.0` and lives at `crates/shepherd-sdk/` in +the shepherd monorepo. It is not yet published to crates.io; modules +depend on it via a workspace path. + +The `[patch.crates-io]` at the workspace root pins `cowprotocol` to a +specific commit on `bleu/cow-rs` (per ADR-0004); the SDK rides that +patch transitively, so module Cargo.toml files declare +`cowprotocol = "1.0.0-alpha.3"` and pick up the fork automatically. diff --git a/docs/tutorial-first-module.md b/docs/tutorial-first-module.md new file mode 100644 index 0000000..4525696 --- /dev/null +++ b/docs/tutorial-first-module.md @@ -0,0 +1,444 @@ +# Build your first Shepherd module + +This is the cold-start guide for an external developer. Target +completion time: **under four hours** from "I cloned the repo" to +"I see my module's first event in the engine log". + +The walked-through example is **stop-loss**: a module that watches a +Chainlink price oracle on every block and submits a pre-signed CoW +order when the price drops below a configured trigger. The fully +working source lives at [`modules/examples/stop-loss/`]( +../modules/examples/stop-loss). The rest of this guide reads that +source top-to-bottom and explains *why* each piece is shaped the +way it is. Open the files alongside the guide as you read. + +## 0. Prerequisites (15 minutes) + +You need a recent Rust toolchain (`rustc 1.91+`, ships with `cargo`) +and the WASM Component Model target. From the repo root: + +```sh +rustup target add wasm32-wasip2 +``` + +Verify the engine builds and runs against the example module that +ships in the workspace: + +```sh +cargo build --target wasm32-wasip2 --release -p example +cargo run -p nexum-engine -- \ + target/wasm32-wasip2/release/example.wasm \ + modules/example/nexum.toml +``` + +You should see two log lines from the example module - one in +`init`, one on the synthetic block event. Stop here and triage if +the build fails or those log lines do not appear; the rest of the +tutorial assumes a working local engine. + +Now build the stop-loss module: + +```sh +cargo build --target wasm32-wasip2 --release -p stop-loss +ls -lh target/wasm32-wasip2/release/stop_loss.wasm +``` + +Expected size: ~300 KB. + +## 1. Anatomy of a module (10 minutes) + +A Shepherd module is a Cargo crate with `crate-type = ["cdylib"]` +compiled to `wasm32-wasip2`. The minimum layout: + +``` +modules/examples/stop-loss/ +├── Cargo.toml declares deps (shepherd-sdk, cowprotocol, alloy, ...) +├── module.toml declares capabilities + subscriptions + config +└── src/ + ├── lib.rs wit-bindgen glue + Guest impl + adapter + └── strategy.rs pure logic against `shepherd_sdk::host::Host` +``` + +The split into `lib.rs` (impure / wit-bindgen) and `strategy.rs` +(pure / `&impl Host`) is the recipe that lets you test the strategy +end-to-end against `shepherd-sdk-test::MockHost` without ever +running the wasm toolchain. + +Open [`Cargo.toml`](../modules/examples/stop-loss/Cargo.toml) and +note the four key features: + +- **`crate-type = ["cdylib"]`** - produces a WASM Component when + built for `wasm32-wasip2`. +- **`shepherd-sdk` path dep** - the helpers (`cow::`, `chain::`, + `host::`, `prelude`) live here. +- **`shepherd-sdk-test` as a dev-dep** - `MockHost` is only linked + under `cargo test`, never in the wasm bundle. +- **No `nexum-engine` dep** - modules never link the engine; they + communicate exclusively through wit-bindgen-generated shims. + +The workspace `Cargo.toml` at the repo root has the crate listed +under `[workspace] members`. + +## 2. The manifest: capabilities and config (10 minutes) + +Open [`module.toml`](../modules/examples/stop-loss/module.toml). +Two things matter: + +```toml +[capabilities] +required = ["logging", "chain", "local-store", "cow-api"] +``` + +The engine enforces this list against the WIT imports the +compiled component declares. Declaring a capability you do not +use is fine; *missing* one you do use is a hard error at +instantiation. Stop-loss touches all four: + +| Capability | Used for | +|---|---| +| `logging` | every Info / Warn line | +| `chain` | the `eth_call` to read the oracle | +| `local-store` | the `submitted:{uid}` and `dropped:{uid}` dedup flags | +| `cow-api` | submitting the `OrderCreation` body | + +```toml +[[subscription]] +kind = "block" +chain_id = 11155111 # Sepolia +``` + +Stop-loss reacts to every new block on Sepolia. WebSocket RPC is +required because `block` rides `eth_subscribe`; see +[`docs/deployment.md`](./deployment.md) for the operator-side +chain config. + +```toml +[config] +oracle_address = "0x694AA1..." +decimals = "8" +trigger_price = "2500.00" +owner = "0x70997970..." +sell_token = "..." +buy_token = "..." +sell_amount_wei = "..." +buy_amount_wei = "..." +valid_to_seconds = "..." +``` + +`[config]` is operator-supplied. The values are strings; the +module parses them once at `init`. We will look at the parsing +code in §4. + +## 3. The wit-bindgen adapter in `lib.rs` (15 minutes) + +Open [`src/lib.rs`](../modules/examples/stop-loss/src/lib.rs). Top +of the file: + +```rust +wit_bindgen::generate!({ + path: ["../../../wit/nexum-host", "../../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +mod strategy; +``` + +The `generate!` macro emits the per-cdylib `Guest` trait, +`HostError` struct, and host import shims (`nexum::host::chain:: +request`, `local_store::set`, etc.) into this crate's scope. +`generate_all` is required because the `shepherd:cow/shepherd` +world cross-references types from `nexum:host/types` - see +[`docs/sdk.md`](./sdk.md) for the gotcha. + +Below the macro, three blocks deserve attention: + +### 3a. `WitBindgenHost` (~80 lines) + +```rust +struct WitBindgenHost; + +impl ChainHost for WitBindgenHost { + fn request(&self, chain_id: u64, method: &str, params: &str) + -> Result + { + chain::request(chain_id, method, params).map_err(convert_err) + } +} +// ... LocalStoreHost / CowApiHost / LoggingHost ... +``` + +This is the bridge between wit-bindgen's free functions and the +`shepherd_sdk::host::Host` trait the strategy works against. The +shape is mechanical and identical across modules - copy it as-is +into your own module, and a future declarative macro in +`shepherd-sdk` will eventually elide it. + +### 3b. `convert_err` / `sdk_err_into_wit` / `convert_level` + +`wit_bindgen::generate!` emits a `HostError` struct into the +module's own crate. `shepherd_sdk::host::HostError` is a *separate* +type with the same fields. The three converters are 7-arm enum +maps - mechanical, but necessary so the trait surface can stay +world-neutral. + +### 3c. `Guest for StopLoss` + +```rust +impl Guest for StopLoss { + fn init(config: Vec<(String, String)>) -> Result<(), HostError> { + let cfg = strategy::parse_config(&config).map_err(sdk_err_into_wit)?; + // ... log + cache in OnceLock ... + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + let Some(cfg) = SETTINGS.get() else { return Ok(()); }; + if let types::Event::Block(block) = event { + strategy::on_block(&WitBindgenHost, block.chain_id, cfg) + .map_err(sdk_err_into_wit)?; + } + Ok(()) + } +} +``` + +`init` parses + caches; `on_event` hands a `WitBindgenHost` to the +strategy and translates the resulting `SdkHostError` back into the +wit-bindgen one for the supervisor. + +`SETTINGS: OnceLock` is the recommended +single-init pattern. wasm32 modules are single-threaded so +`OnceLock` is overkill on synchronisation but cheap and explicit +about lifetime. + +## 4. The strategy in `strategy.rs` (45 minutes) + +Open [`src/strategy.rs`](../modules/examples/stop-loss/src/strategy.rs). +This file is the heart of the module - the only one you would +diff against if you rebased on a newer SDK. + +### 4a. `Settings` + `parse_config` + +The parser walks `Vec<(String, String)>` and produces a typed +`Settings`. It returns `Result` so the upstream `Guest::init` can lift the failure +straight into the wit-bindgen `HostError` envelope with no extra +plumbing. `scale_signed` is a hand-rolled decimal-to-I256 scaler +because alloy ships no `Decimal::parse_units` equivalent (yet). + +### 4b. `read_oracle` + +```rust +fn read_oracle(host: &H, chain_id: u64, oracle: Address) + -> Option +{ + let call_data = AggregatorV3::latestRoundDataCall {}.abi_encode(); + let params = eth_call_params(&oracle, &call_data); + let result_json = host.request(chain_id, "eth_call", ¶ms).ok()?; + let bytes = parse_eth_call_result(&result_json)?; + AggregatorV3::latestRoundDataCall::abi_decode_returns(&bytes) + .ok() + .map(|r| r.answer) +} +``` + +Three SDK helpers in three lines: `chain::eth_call_params` builds +the `[{to, data}, "latest"]` JSON, `chain::parse_eth_call_result` +unpacks the `"0x..."` hex response. The `sol! interface AggregatorV3` +declared at the top of the file gives us a typed call + return +decoder; the same pattern works for any read-only EVM contract. + +Returning `Option` (with a Warn log on the error path inside +the function) is intentional: the next block re-polls, and a +single flaky RPC reply should not propagate into the supervisor. + +### 4c. `build_creation` + +The most interesting piece. Constructs a `cowprotocol:: +OrderCreation` body the orderbook accepts: + +```rust +let chain = Chain::try_from(chain_id)?; +let domain = chain.settlement_domain(); +let gpv2 = GPv2OrderData { ... }; +let order_data = gpv2_to_order_data(&gpv2)?; // shepherd-sdk helper +let uid = order_data.uid(&domain, settings.owner); +let creation = OrderCreation::from_signed_order_data( + &order_data, + Signature::PreSign, // owner has called setPreSignature on-chain + settings.owner, + EMPTY_APP_DATA_JSON.to_string(), + None, +)?; +``` + +Three load-bearing decisions: + +- **`Signature::PreSign`**: the module ships no ECDSA. The order + owner is expected to have called `GPv2Signing.setPreSignature` + on-chain ahead of the trigger. The body shipped to the orderbook + carries the owner address and an empty signature; the orderbook + validates by checking the on-chain pre-signature record at + settlement. +- **`gpv2_to_order_data`**: the `shepherd-sdk` helper that maps the + on-chain `bytes32` markers (`kind`, balance sources) onto + cowprotocol's typed enums. Same code-path twap-monitor and + ethflow-watcher take after the BLEU-843 refactor. +- **`order_data.uid(&domain, settings.owner)`**: computes the + canonical 56-byte UID locally. The orderbook's `POST /api/v1/ + orders` returns the same UID; the module uses the local version + to dedup *before* paying for the network round-trip. + +### 4d. `on_block` + +The dispatch loop: + +```rust +pub fn on_block(host: &H, chain_id: u64, settings: &Settings) + -> Result<(), HostError> +{ + let price = read_oracle(host, chain_id, settings.oracle_address) else { return Ok(()) }; + + if price > settings.trigger_price_scaled { + // idle - log and wait for the next block + return Ok(()); + } + + let (creation, uid) = build_creation(chain_id, settings)?; + let uid_hex = format!("{uid}"); + + // Dedup: skip if already submitted OR previously dropped. + if host.get(&format!("submitted:{uid_hex}"))?.is_some() { return Ok(()); } + if host.get(&format!("dropped:{uid_hex}"))?.is_some() { return Ok(()); } + + let body = serde_json::to_vec(&creation)?; + match host.submit_order(chain_id, &body) { + Ok(server_uid) => { + host.set(&format!("submitted:{server_uid}"), b"")?; + host.log(LogLevel::Warn, &format!("TRIGGERED, uid={server_uid}")); + } + Err(err) => match classify_api_error(err.data.as_deref()) { + RetryAction::TryNextBlock | RetryAction::Backoff { .. } => { + // log and let the next block re-attempt + } + RetryAction::Drop => { + host.set(&format!("dropped:{uid_hex}"), b"")?; + // log + give up - the orderbook will not accept the + // same body on a retry + } + }, + } + Ok(()) +} +``` + +The `shepherd_sdk::cow::classify_api_error` helper is the BLEU-829 +retry contract - it maps the orderbook's typed `ApiError` into +`TryNextBlock` / `Backoff` / `Drop`. The module's only role here is +to act on the verdict: log and idle, or persist a `dropped:` flag +so the next block does not re-attempt. + +### 4e. Tests at the bottom + +Seven tests cover the dispatch matrix: + +- `idle_when_price_above_trigger` +- `triggers_and_submits_once_then_dedups` +- `permanent_submit_error_marks_dropped` (+ confirms dedup on the + next block) +- `transient_submit_error_leaves_state_unchanged` +- `oracle_rpc_error_is_warn_and_continue` +- `parse_config_round_trips_settings` + `parse_config_rejects_ + missing_owner` + +All seven run against `shepherd_sdk_test::MockHost`. `host.chain. +respond_to(...)` programs the oracle return; `host.cow_api.respond +(...)` programs the orderbook response; assertions read +`host.store.snapshot()` and `host.logging.contains(...)`. No +`wasmtime`, no network, no fixture wasm bundle. + +## 5. Build the `.wasm` (5 minutes) + +You already did this in §0. Re-build to confirm the strategy edits +compile: + +```sh +cargo build --target wasm32-wasip2 --release -p stop-loss +ls -lh target/wasm32-wasip2/release/stop_loss.wasm +``` + +If the file ballooned past ~500 KB, look at +`cargo tree -p stop-loss --target wasm32-wasip2` - usually a fresh +dependency pulled `reqwest` or `tokio` into the wasm graph. + +## 6. Wire `engine.toml` and run it (10 minutes) + +Add a Sepolia RPC entry: + +```toml +[chains.11155111] +rpc_url = "wss://ethereum-sepolia-rpc.publicnode.com" +``` + +WebSocket is required because the `[[subscription]]` is `kind = +"block"`. Run: + +```sh +cargo run -p nexum-engine -- \ + target/wasm32-wasip2/release/stop_loss.wasm \ + modules/examples/stop-loss/module.toml +``` + +Expected output: + +- `init`: `stop-loss init: owner=0x... trigger=...` +- on each new block: `stop-loss idle: price=... > trigger=...` + while the oracle stays above the threshold, then `stop-loss + TRIGGERED: ...` if the price ever drops at or below. + +If the engine reports `unsupported` for any capability, double- +check `[capabilities].required` matches the imports the strategy +exercises. + +For multi-module operation (running stop-loss alongside other +strategies), see the BLEU-818 supervisor PR. + +## 7. Where to go from here (10 minutes) + +- **Production hardening**: tune `[engine.limits].fuel_per_event` + and `memory_bytes` for your hardware - see [`docs/deployment.md`]( + ./deployment.md) for the operator runbook. +- **A different strategy**: copy `modules/examples/stop-loss/`, + rename, and change `on_block`. The wit-bindgen adapter in + `lib.rs` is identical for every module; only `strategy.rs` and + `module.toml::[config]` move. +- **Custom signing**: swap `Signature::PreSign` for + `Signature::Eip1271(bytes)` when the owner is a Safe with an + isValidSignature handler - same pattern ethflow-watcher uses. +- **Multi-chain operation**: change `[[subscription]].chain_id` + and add the `engine.toml::[chains.]` entry. The strategy + stays unchanged because every host call passes `chain_id` + through. + +## Time-budget check + +If a section ran much longer than the rough estimate above, please +file an issue tagged `docs/tutorial` with the section that dragged. +The target is **<4h cold from a fresh checkout to a successful run +in §6**, and we tighten the prose against feedback. + +## Reference index + +- SDK overview: [`docs/sdk.md`](./sdk.md) +- Deployment runbook: [`docs/deployment.md`](./deployment.md) +- The example: [`modules/examples/stop-loss/`]( + ../modules/examples/stop-loss/) +- ADR-0001 (`engine.toml` vs `module.toml` split) +- ADR-0006 (TWAP / EthFlow as guest modules, no specialised + WIT interfaces) +- ADR-0007 (push protocol primitives to `cow-rs` first) +- Worked examples that share the same recipe: + [`price-alert`](../modules/examples/price-alert/), + [`balance-tracker`](../modules/examples/balance-tracker/), + [`twap-monitor`](../modules/twap-monitor/), + [`ethflow-watcher`](../modules/ethflow-watcher/) diff --git a/engine.example.toml b/engine.example.toml new file mode 100644 index 0000000..d6513b0 --- /dev/null +++ b/engine.example.toml @@ -0,0 +1,34 @@ +# Engine-side runtime configuration for `nexum-engine`. +# +# Distinct from `nexum.toml` (per-module manifest): this file +# describes the *engine*'s I/O wiring. Copy to `engine.toml` next to +# the binary, or pass the path as the third positional argument. + +[engine] +# Directory the local-store redb file (and future engine artefacts) +# will be created under. Created automatically at boot. +state_dir = "./data" + +# `tracing_subscriber::EnvFilter`-compatible directive. `RUST_LOG` +# overrides at process start. +log_level = "info" + +# One [chains.] table per chain the engine should be able to talk +# to. Chain ids are EVM decimal. `ws://` and `wss://` URLs engage +# alloy's pubsub transport (needed for `eth_subscribe`); `http://` and +# `https://` use the HTTP transport. + +[chains.1] +rpc_url = "https://ethereum-rpc.publicnode.com" + +[chains.100] +rpc_url = "https://rpc.gnosischain.com" + +[chains.11155111] +rpc_url = "wss://ethereum-sepolia-rpc.publicnode.com" + +[chains.42161] +rpc_url = "https://arb1.arbitrum.io/rpc" + +[chains.8453] +rpc_url = "https://mainnet.base.org" diff --git a/engine.m2.toml b/engine.m2.toml new file mode 100644 index 0000000..cb179dc --- /dev/null +++ b/engine.m2.toml @@ -0,0 +1,35 @@ +# M2 smoke / round-trip config for nexum-engine. +# +# Boots the two M2 modules (twap-monitor + ethflow-watcher) against +# Sepolia public RPC and the CoW Protocol Sepolia orderbook. +# +# Usage: +# just run-m2 +# # or: +# cargo build -p twap-monitor --target wasm32-wasip2 --release +# cargo build -p ethflow-watcher --target wasm32-wasip2 --release +# cargo run -p nexum-engine -- --engine-config engine.m2.toml +# +# Override RPC if rate-limited: +# SEPOLIA_RPC=wss://eth-sepolia.g.alchemy.com/v2/ ... +# Today the engine does not env-substitute - edit the URL inline. + +[engine] +# Separate from `./data` so it does not collide with the M1 example +# runbook state. Wiped freely between smoke runs. +state_dir = "./data/m2" +log_level = "info,nexum_engine=debug" + +# Sepolia. Public node; switch to Alchemy/Infura with a key for any +# sustained run (public nodes throttle aggressively under +# eth_subscribe load). +[chains.11155111] +rpc_url = "wss://ethereum-sepolia-rpc.publicnode.com" + +[[modules]] +path = "target/wasm32-wasip2/release/twap_monitor.wasm" +manifest = "modules/twap-monitor/module.toml" + +[[modules]] +path = "target/wasm32-wasip2/release/ethflow_watcher.wasm" +manifest = "modules/ethflow-watcher/module.toml" diff --git a/engine.m3.toml b/engine.m3.toml new file mode 100644 index 0000000..979f792 --- /dev/null +++ b/engine.m3.toml @@ -0,0 +1,36 @@ +# M3 smoke / validation config for nexum-engine. +# +# Boots the 3 M3 example modules (price-alert + balance-tracker + +# stop-loss) against Sepolia. The 3 modules exercise the full SDK +# helper surface (chain::request via Chainlink read, local-store +# diffing, cow-api submit with PreSign). +# +# Usage: +# just run-m3 +# # or: +# cargo build -p price-alert --target wasm32-wasip2 --release +# cargo build -p balance-tracker --target wasm32-wasip2 --release +# cargo build -p stop-loss --target wasm32-wasip2 --release +# cargo run -p nexum-engine -- --engine-config engine.m3.toml + +[engine] +# Separate from data/m2 and the M1 example state. +state_dir = "./data/m3" +log_level = "info,nexum_engine=debug" + +# Sepolia. Override with an Alchemy / Infura WS for sustained runs; +# the public node throttles eth_subscribe under load. +[chains.11155111] +rpc_url = "wss://ethereum-sepolia-rpc.publicnode.com" + +[[modules]] +path = "target/wasm32-wasip2/release/price_alert.wasm" +manifest = "modules/examples/price-alert/module.toml" + +[[modules]] +path = "target/wasm32-wasip2/release/balance_tracker.wasm" +manifest = "modules/examples/balance-tracker/module.toml" + +[[modules]] +path = "target/wasm32-wasip2/release/stop_loss.wasm" +manifest = "modules/examples/stop-loss/module.toml" diff --git a/justfile b/justfile index 3e01212..f8a59d6 100644 --- a/justfile +++ b/justfile @@ -10,10 +10,40 @@ build-module: build: build-engine build-module # Build the module then run the engine with it. The second argument is the -# module's nexum.toml — without it the engine prints the 0.1-compat +# module's module.toml — without it the engine prints the 0.1-compat # deprecation warning and proceeds with empty capabilities/config. run: build-module build-engine - cargo run -p nexum-engine -- target/wasm32-wasip2/release/example.wasm modules/example/nexum.toml + cargo run -p nexum-engine -- target/wasm32-wasip2/release/example.wasm modules/example/module.toml + +# Run host engine unit tests +test: + cargo test -p nexum-engine + +# Build module + engine, then run E2E integration tests +test-e2e: build-module build-engine + cargo test -p nexum-engine supervisor::tests::e2e + +# Build the M2 modules (twap-monitor + ethflow-watcher) for wasm32-wasip2. +build-m2: + cargo build -p twap-monitor --target wasm32-wasip2 --release + cargo build -p ethflow-watcher --target wasm32-wasip2 --release + +# Run nexum-engine wired for the M2 smoke / round-trip scenario +# (Sepolia, both M2 modules). See `docs/operations/m2-testnet-runbook.md`. +run-m2: build-m2 build-engine + cargo run -p nexum-engine -- --engine-config engine.m2.toml + +# Build the M3 example modules (price-alert + balance-tracker + stop-loss) +# for wasm32-wasip2. +build-m3: + cargo build -p price-alert --target wasm32-wasip2 --release + cargo build -p balance-tracker --target wasm32-wasip2 --release + cargo build -p stop-loss --target wasm32-wasip2 --release + +# Run nexum-engine wired for the M3 smoke / validation scenario +# (Sepolia, 3 example modules). See `docs/operations/m3-testnet-runbook.md`. +run-m3: build-m3 build-engine + cargo run -p nexum-engine -- --engine-config engine.m3.toml # Check the entire workspace check: diff --git a/modules/ethflow-watcher/Cargo.toml b/modules/ethflow-watcher/Cargo.toml new file mode 100644 index 0000000..aaad196 --- /dev/null +++ b/modules/ethflow-watcher/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "ethflow-watcher" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true + +[lib] +crate-type = ["cdylib"] + +[dependencies] +shepherd-sdk = { path = "../../crates/shepherd-sdk" } +cowprotocol = { version = "1.0.0-alpha.3", default-features = false } +alloy-primitives = { version = "1.5", default-features = false, features = ["std"] } +alloy-sol-types = { version = "1.5", default-features = false, features = ["std"] } +serde_json = { version = "1", default-features = false, features = ["alloc"] } +thiserror = "2" +wit-bindgen = { version = "0.57", default-features = false, features = ["macros", "realloc"] } + +[dev-dependencies] +shepherd-sdk-test = { path = "../../crates/shepherd-sdk-test" } diff --git a/modules/ethflow-watcher/module.toml b/modules/ethflow-watcher/module.toml new file mode 100644 index 0000000..c169f9c --- /dev/null +++ b/modules/ethflow-watcher/module.toml @@ -0,0 +1,35 @@ +# ethflow-watcher: see `CoWSwapEthFlow.OrderPlacement`, lift the embedded +# `GPv2OrderData` into an `OrderCreation`, and submit it via the CoW +# Protocol orderbook with the EIP-1271 signing scheme. + +[module] +name = "ethflow-watcher" +version = "0.1.0" +# Placeholder content hash. 0.2 parses but does not verify this; 0.3 will +# compare it against the sha256 of the loaded component bytes. +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +# Same set as twap-monitor for symmetry and future-proofing - the module +# imports logging, local-store and cow-api today; `chain` is declared +# because a follow-up may add an eth_call (e.g. to read the EthFlow +# refund pointer) without churning the manifest. +required = ["logging", "local-store", "chain", "cow-api"] +optional = [] + +[capabilities.http] +# All outbound HTTP goes through `cow-api`; no direct `http` calls. +allow = [] + +# --- subscriptions ------------------------------------------------------ + +# CoWSwapEthFlow.OrderPlacement on Sepolia. topic-0 = keccak256( +# "OrderPlacement(address,(address,address,address,uint256,uint256,uint32, +# bytes32,uint256,bytes32,bool,bytes32,bytes32),(uint8,bytes),bytes)"). +# `address` is the production deployment, identical on every chain CoW +# Protocol supports (cowprotocol::ETH_FLOW_PRODUCTION). +[[subscription]] +kind = "log" +chain_id = 11155111 +address = "0xbA3cB449bD2B4ADddBc894D8697F5170800EAdeC" +event_signature = "0xcf5f9de2984132265203b5c335b25727702ca77262ff622e136baa7362bf1da9" diff --git a/modules/ethflow-watcher/src/lib.rs b/modules/ethflow-watcher/src/lib.rs new file mode 100644 index 0000000..ccd44c9 --- /dev/null +++ b/modules/ethflow-watcher/src/lib.rs @@ -0,0 +1,149 @@ +//! # ethflow-watcher (Shepherd module) +//! +//! Subscribes to `CoWSwapOnchainOrders.OrderPlacement` logs from the +//! CoWSwap EthFlow contracts and resubmits each placed order through +//! the orderbook API with `Signature::Eip1271`. The EthFlow contract +//! is the EIP-1271 verifier, so the `from` field on the resubmission +//! is the contract address (not the original native-token seller). +//! +//! ## Module layout (BLEU-855) +//! +//! - `strategy.rs` holds the pure logic and unit tests against +//! `shepherd_sdk::host::Host`. It does not know `wit-bindgen` +//! exists. +//! - `lib.rs` (this file) is the per-cdylib glue: wit-bindgen import +//! shims, the `WitBindgenHost` adapter that bridges the generated +//! free functions to the SDK traits, and the `Guest` impl that +//! delegates the `Logs` event variant to `strategy::on_logs`. + +// wit_bindgen::generate! expands to host-import shims whose arity +// matches the WIT signatures, which can exceed clippy's +// too-many-arguments threshold. +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![allow(clippy::too_many_arguments)] + +wit_bindgen::generate!({ + path: ["../../wit/nexum-host", "../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +mod strategy; + +use shepherd_sdk::host::{ + ChainHost, CowApiHost, HostError as SdkHostError, HostErrorKind as SdkHostErrorKind, + LocalStoreHost, LogLevel as SdkLogLevel, LoggingHost, +}; + +use nexum::host::types::HostErrorKind; +use nexum::host::{chain, local_store, logging, types}; +use shepherd::cow::cow_api; + +struct WitBindgenHost; + +impl ChainHost for WitBindgenHost { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + chain::request(chain_id, method, params).map_err(convert_err) + } +} + +impl LocalStoreHost for WitBindgenHost { + fn get(&self, key: &str) -> Result>, SdkHostError> { + local_store::get(key).map_err(convert_err) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), SdkHostError> { + local_store::set(key, value).map_err(convert_err) + } + fn delete(&self, key: &str) -> Result<(), SdkHostError> { + local_store::delete(key).map_err(convert_err) + } + fn list_keys(&self, prefix: &str) -> Result, SdkHostError> { + local_store::list_keys(prefix).map_err(convert_err) + } +} + +impl CowApiHost for WitBindgenHost { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + cow_api::submit_order(chain_id, body).map_err(convert_err) + } +} + +impl LoggingHost for WitBindgenHost { + fn log(&self, level: SdkLogLevel, message: &str) { + logging::log(convert_level(level), message); + } +} + +fn convert_err(e: HostError) -> SdkHostError { + SdkHostError { + domain: e.domain, + kind: match e.kind { + HostErrorKind::Unsupported => SdkHostErrorKind::Unsupported, + HostErrorKind::Unavailable => SdkHostErrorKind::Unavailable, + HostErrorKind::Denied => SdkHostErrorKind::Denied, + HostErrorKind::RateLimited => SdkHostErrorKind::RateLimited, + HostErrorKind::Timeout => SdkHostErrorKind::Timeout, + HostErrorKind::InvalidInput => SdkHostErrorKind::InvalidInput, + HostErrorKind::Internal => SdkHostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn sdk_err_into_wit(e: SdkHostError) -> HostError { + HostError { + domain: e.domain, + kind: match e.kind { + SdkHostErrorKind::Unsupported => HostErrorKind::Unsupported, + SdkHostErrorKind::Unavailable => HostErrorKind::Unavailable, + SdkHostErrorKind::Denied => HostErrorKind::Denied, + SdkHostErrorKind::RateLimited => HostErrorKind::RateLimited, + SdkHostErrorKind::Timeout => HostErrorKind::Timeout, + SdkHostErrorKind::InvalidInput => HostErrorKind::InvalidInput, + SdkHostErrorKind::Internal => HostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn convert_level(l: SdkLogLevel) -> logging::Level { + match l { + SdkLogLevel::Trace => logging::Level::Trace, + SdkLogLevel::Debug => logging::Level::Debug, + SdkLogLevel::Info => logging::Level::Info, + SdkLogLevel::Warn => logging::Level::Warn, + SdkLogLevel::Error => logging::Level::Error, + } +} + +struct EthFlowWatcher; + +impl Guest for EthFlowWatcher { + fn init(_config: Vec<(String, String)>) -> Result<(), HostError> { + logging::log(logging::Level::Info, "ethflow-watcher init"); + Ok(()) + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + if let types::Event::Logs(logs) = event { + let views: Vec> = logs + .iter() + .map(|log| strategy::LogView { + chain_id: log.chain_id, + address: &log.address, + topics: &log.topics, + data: &log.data, + }) + .collect(); + strategy::on_logs(&WitBindgenHost, &views).map_err(sdk_err_into_wit)?; + } + // Block / Tick / Message are not used by this module. + Ok(()) + } +} + +export!(EthFlowWatcher); diff --git a/modules/ethflow-watcher/src/strategy.rs b/modules/ethflow-watcher/src/strategy.rs new file mode 100644 index 0000000..e9811c5 --- /dev/null +++ b/modules/ethflow-watcher/src/strategy.rs @@ -0,0 +1,628 @@ +//! Pure strategy logic for the ethflow-watcher module. +//! +//! Every interaction with the world flows through the +//! `shepherd_sdk::host::Host` trait seam - no direct calls to wit- +//! bindgen-generated free functions live here. The `lib.rs` glue +//! wraps a `WitBindgenHost` adapter around the per-cdylib wit-bindgen +//! imports and hands it to [`on_logs`]; tests under `#[cfg(test)]` +//! hand the same function a `shepherd_sdk_test::MockHost`. + +use alloy_primitives::{Address, B256, Bytes}; +use alloy_sol_types::SolEvent; +use cowprotocol::{ + Chain, CoWSwapOnchainOrders::OrderPlacement, EMPTY_APP_DATA_JSON, ETH_FLOW_PRODUCTION, + ETH_FLOW_STAGING, GPv2OrderData, OnchainSignature, OnchainSigningScheme, OrderCreation, + OrderUid, Signature, +}; +use shepherd_sdk::cow::{RetryAction, classify_api_error, gpv2_to_order_data}; +use shepherd_sdk::host::{Host, HostError, LogLevel}; + +/// Fields the strategy needs from a wit-bindgen `log`. Borrowed slices +/// keep the strategy independent from the per-cdylib wit types. +pub struct LogView<'a> { + pub chain_id: u64, + pub address: &'a [u8], + pub topics: &'a [Vec], + pub data: &'a [u8], +} + +/// Fully decoded payload of a `CoWSwapOnchainOrders.OrderPlacement` +/// log. `GPv2OrderData` is ~300 bytes; box it so the struct stays +/// cache-friendly through the submit path. +#[derive(Debug)] +pub(crate) struct DecodedPlacement { + /// EthFlow contract that emitted the event - also the EIP-1271 + /// verifier `from` for the submitted `OrderCreation`. + pub(crate) contract: Address, + /// Original native-token seller - logged for diagnostics; the + /// orderbook's `from` is the contract (EIP-1271 owner), not this. + pub(crate) sender: Address, + pub(crate) order: Box, + pub(crate) signature: OnchainSignature, + /// Refund pointer / opaque placer metadata. Not consumed by the + /// submit path today, but the field is part of the BLEU-832 + /// decoder contract. + #[allow(dead_code)] + pub(crate) data: Bytes, +} + +/// Entry point: decode every `OrderPlacement` log in a dispatch batch +/// and feed the decoded placement to the submit path. +pub fn on_logs(host: &H, logs: &[LogView<'_>]) -> Result<(), HostError> { + for log in logs { + if let Some(placement) = decode_order_placement(log.address, log.topics, log.data) { + submit_placement(host, log.chain_id, &placement)?; + } + } + Ok(()) +} + +// ---- BLEU-832: decode ---- + +/// Decode a raw event log against `CoWSwapOnchainOrders.OrderPlacement`. +/// +/// Returns `None` when: +/// - the log's contract address is neither `ETH_FLOW_PRODUCTION` nor +/// `ETH_FLOW_STAGING` (defensive - the host's `[[subscription]]` +/// filter already pins the address, but a misconfigured engine +/// could still leak through); +/// - topic0 does not match the event signature; or +/// - the ABI body fails to decode. +pub(crate) fn decode_order_placement( + address: &[u8], + topics: &[Vec], + data: &[u8], +) -> Option { + if address.len() != 20 { + return None; + } + let contract = Address::from_slice(address); + if contract != ETH_FLOW_PRODUCTION && contract != ETH_FLOW_STAGING { + return None; + } + let topic0 = topics.first()?; + if topic0.len() != 32 || B256::from_slice(topic0) != OrderPlacement::SIGNATURE_HASH { + return None; + } + let words: Vec = topics + .iter() + .filter(|t| t.len() == 32) + .map(|t| B256::from_slice(t)) + .collect(); + let decoded = OrderPlacement::decode_raw_log(words, data).ok()?; + Some(DecodedPlacement { + contract, + sender: decoded.sender, + order: Box::new(decoded.order), + signature: decoded.signature, + data: decoded.data, + }) +} + +// ---- BLEU-833: submit + retry ---- + +#[derive(Debug, thiserror::Error)] +pub(crate) enum BuildError { + #[error("GPv2OrderData carried an unknown enum marker")] + UnknownMarker, + #[error("OnchainSignature carried an unknown scheme variant")] + UnknownSignatureScheme, + #[error("chain {0} is not supported by cowprotocol")] + UnsupportedChain(u64), + #[error(transparent)] + Cowprotocol(#[from] cowprotocol::Error), +} + +/// Lift `OnchainSignature` into the orderbook-typed `Signature`. The +/// EthFlow contract is the EIP-1271 verifier, so the `data` blob is +/// the raw verifier bytes; for `PreSign` the orderbook accepts an +/// empty payload. +fn to_signature(sig: &OnchainSignature) -> Option { + // sol! adds a hidden `__Invalid` variant on every Solidity enum, + // so exhaustive patterns require a wildcard; we surface it as + // `None` (caller falls back to skipping the placement) rather + // than panic. + match sig.scheme { + OnchainSigningScheme::Eip1271 => Some(Signature::Eip1271(sig.data.to_vec())), + OnchainSigningScheme::PreSign => Some(Signature::PreSign), + _ => None, + } +} + +/// Assemble `(OrderCreation, OrderUid)` from a placement. `from` is +/// the EthFlow contract (EIP-1271 owner). `app_data` is fixed to +/// `EMPTY_APP_DATA_JSON` - placements pinning a real IPFS document +/// get rejected by `from_signed_order_data` (digest mismatch) and +/// skipped. +pub(crate) fn build_eth_flow_creation( + chain_id: u64, + placement: &DecodedPlacement, +) -> Result<(OrderCreation, OrderUid), BuildError> { + let chain = Chain::try_from(chain_id).map_err(|_| BuildError::UnsupportedChain(chain_id))?; + let domain = chain.settlement_domain(); + let order_data = gpv2_to_order_data(&placement.order).ok_or(BuildError::UnknownMarker)?; + let uid = order_data.uid(&domain, placement.contract); + let signature = to_signature(&placement.signature).ok_or(BuildError::UnknownSignatureScheme)?; + let creation = OrderCreation::from_signed_order_data( + &order_data, + signature, + placement.contract, + EMPTY_APP_DATA_JSON.to_string(), + None, + )?; + Ok((creation, uid)) +} + +fn submit_placement( + host: &H, + chain_id: u64, + placement: &DecodedPlacement, +) -> Result<(), HostError> { + let (creation, uid) = match build_eth_flow_creation(chain_id, placement) { + Ok(x) => x, + Err(e) => { + host.log( + LogLevel::Warn, + &format!( + "ethflow submit skipped (sender={:#x}): {e}", + placement.sender + ), + ); + return Ok(()); + } + }; + let uid_hex = format!("{uid}"); + + // Idempotency. A host reconnect or engine restart may replay the + // same OrderPlacement log; without the guard we would attempt a + // second submit, the orderbook would reject `DuplicateOrder` + // (permanent), and we would end up with both `submitted:` AND + // `dropped:` written for the same UID. `backoff:` is *not* a + // short-circuit - a previous transient error deserves a fresh + // attempt on re-delivery. + match prior_outcome(host, &uid_hex)? { + PriorOutcome::Submitted => { + host.log( + LogLevel::Info, + &format!("ethflow {uid_hex} already submitted; skipping"), + ); + return Ok(()); + } + PriorOutcome::Dropped => { + host.log( + LogLevel::Info, + &format!("ethflow {uid_hex} previously dropped; skipping"), + ); + return Ok(()); + } + PriorOutcome::None | PriorOutcome::Backoff => {} + } + + let body = match serde_json::to_vec(&creation) { + Ok(b) => b, + Err(e) => { + host.log( + LogLevel::Error, + &format!("OrderCreation JSON encode failed: {e}"), + ); + return Ok(()); + } + }; + match host.submit_order(chain_id, &body) { + Ok(server_uid) => { + // Persist under the server-supplied UID so downstream + // observers (cow-tooling, dune) join on the same key. The + // client UID we just computed should equal it; a Warn is + // worth a closer look if not (domain/owner divergence). + if server_uid != uid_hex { + host.log( + LogLevel::Warn, + &format!("ethflow uid drift: local={uid_hex} server={server_uid}"), + ); + } + host.set(&format!("submitted:{server_uid}"), b"")?; + // Clear any backoff: marker a prior transient error left + // behind; the terminal `submitted:` flag supersedes it. + let _ = host.delete(&format!("backoff:{server_uid}")); + host.log(LogLevel::Info, &format!("ethflow submitted {server_uid}")); + } + Err(err) => apply_submit_retry(host, &err, &uid_hex)?, + } + Ok(()) +} + +/// Which terminal / transient marker (if any) the local store carries +/// for `uid_hex`. The submit path short-circuits on `Submitted` / +/// `Dropped`; `Backoff` still proceeds with a fresh attempt; `None` +/// means a clean first try. +#[derive(Debug, Eq, PartialEq)] +enum PriorOutcome { + None, + Submitted, + Backoff, + Dropped, +} + +fn prior_outcome(host: &H, uid_hex: &str) -> Result { + if host.get(&format!("submitted:{uid_hex}"))?.is_some() { + return Ok(PriorOutcome::Submitted); + } + if host.get(&format!("dropped:{uid_hex}"))?.is_some() { + return Ok(PriorOutcome::Dropped); + } + if host.get(&format!("backoff:{uid_hex}"))?.is_some() { + return Ok(PriorOutcome::Backoff); + } + Ok(PriorOutcome::None) +} + +fn apply_submit_retry(host: &H, err: &HostError, uid_hex: &str) -> Result<(), HostError> { + match classify_api_error(err.data.as_deref()) { + RetryAction::TryNextBlock | RetryAction::Backoff { .. } => { + host.set(&format!("backoff:{uid_hex}"), b"")?; + host.log( + LogLevel::Warn, + &format!("ethflow backoff {uid_hex} ({}): {}", err.code, err.message), + ); + } + RetryAction::Drop => { + host.set(&format!("dropped:{uid_hex}"), b"")?; + // Clear `backoff:` if a prior transient attempt left it + // behind - the terminal `dropped:` flag now supersedes + // it, and we want at most one outcome marker per UID at + // rest. + let _ = host.delete(&format!("backoff:{uid_hex}")); + host.log( + LogLevel::Warn, + &format!("ethflow dropped {uid_hex} ({}): {}", err.code, err.message), + ); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::{U256, address, hex}; + use alloy_sol_types::SolValue; + use cowprotocol::{BuyTokenDestination, OrderKind, SellTokenSource}; + use shepherd_sdk::host::{HostErrorKind as Kind, LocalStoreHost as _}; + use shepherd_sdk_test::MockHost; + + const SEPOLIA: u64 = 11_155_111; + + fn submittable_order() -> GPv2OrderData { + GPv2OrderData { + sellToken: address!("6810e776880C02933D47DB1b9fc05908e5386b96"), + buyToken: address!("DAE5F1590db13E3B40423B5b5c5fbf175515910b"), + receiver: address!("DeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"), + sellAmount: U256::from(1_000_000_u64), + buyAmount: U256::from(999_u64), + validTo: 0xffff_ffff, + appData: cowprotocol::EMPTY_APP_DATA_HASH, + feeAmount: U256::ZERO, + kind: OrderKind::SELL, + partiallyFillable: false, + sellTokenBalance: SellTokenSource::ERC20, + buyTokenBalance: BuyTokenDestination::ERC20, + } + } + + fn well_formed_placement() -> DecodedPlacement { + DecodedPlacement { + contract: ETH_FLOW_PRODUCTION, + sender: address!("00112233445566778899aabbccddeeff00112233"), + order: Box::new(submittable_order()), + signature: OnchainSignature { + scheme: OnchainSigningScheme::Eip1271, + data: hex!("c0ffeec0ffeec0ffee").to_vec().into(), + }, + data: Bytes::new(), + } + } + + fn sample_event_for_decode() -> OrderPlacement { + OrderPlacement { + sender: address!("00112233445566778899aabbccddeeff00112233"), + order: submittable_order(), + signature: OnchainSignature { + scheme: OnchainSigningScheme::Eip1271, + data: hex!("c0ffeec0ffeec0ffee").to_vec().into(), + }, + data: hex!("deadbeef").to_vec().into(), + } + } + + fn encode_log(event: &OrderPlacement) -> (Vec>, Vec) { + let mut sender_topic = vec![0u8; 12]; + sender_topic.extend_from_slice(event.sender.as_slice()); + let topics = vec![OrderPlacement::SIGNATURE_HASH.to_vec(), sender_topic]; + let data = ( + event.order.clone(), + event.signature.clone(), + event.data.clone(), + ) + .abi_encode_params(); + (topics, data) + } + + fn placement_log_view<'a>( + address_bytes: &'a [u8], + topics: &'a [Vec], + data: &'a [u8], + ) -> LogView<'a> { + LogView { + chain_id: SEPOLIA, + address: address_bytes, + topics, + data, + } + } + + // ---- existing pure tests preserved from BLEU-832/833 ---- + + #[test] + fn decodes_well_formed_placement() { + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let decoded = decode_order_placement(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data) + .expect("decode succeeds"); + assert_eq!(decoded.contract, ETH_FLOW_PRODUCTION); + assert_eq!(decoded.sender, event.sender); + assert_eq!(decoded.signature.scheme, OnchainSigningScheme::Eip1271); + } + + #[test] + fn rejects_unrelated_contract_address() { + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let stranger = address!("dead00000000000000000000000000000000dead"); + assert!(decode_order_placement(stranger.as_slice(), &topics, &data).is_none()); + } + + #[test] + fn build_eip1271_creation_has_contract_as_from() { + let placement = well_formed_placement(); + let (creation, uid) = + build_eth_flow_creation(11_155_111, &placement).expect("build succeeds"); + assert_eq!(creation.from, placement.contract); + assert_eq!(creation.signing_scheme, cowprotocol::SigningScheme::Eip1271); + assert_eq!( + creation.signature.to_bytes(), + placement.signature.data.to_vec(), + ); + assert_eq!(&uid.as_slice()[32..52], placement.contract.as_slice()); + assert_eq!( + &uid.as_slice()[52..56], + &placement.order.validTo.to_be_bytes(), + ); + } + + #[test] + fn build_presign_emits_presign_scheme() { + let mut placement = well_formed_placement(); + placement.signature = OnchainSignature { + scheme: OnchainSigningScheme::PreSign, + data: Bytes::new(), + }; + let (creation, _) = build_eth_flow_creation(1, &placement).expect("build succeeds"); + assert_eq!(creation.signing_scheme, cowprotocol::SigningScheme::PreSign); + assert!(creation.signature.to_bytes().is_empty()); + } + + #[test] + fn build_rejects_unsupported_chain() { + let placement = well_formed_placement(); + let err = build_eth_flow_creation(0xdead_beef, &placement).unwrap_err(); + assert!(matches!(err, BuildError::UnsupportedChain(0xdead_beef))); + } + + #[test] + fn build_rejects_unknown_kind_marker() { + let mut placement = well_formed_placement(); + placement.order.kind = B256::repeat_byte(0x42); + let err = build_eth_flow_creation(1, &placement).unwrap_err(); + assert!(matches!(err, BuildError::UnknownMarker)); + } + + #[test] + fn build_rejects_non_empty_app_data() { + let mut placement = well_formed_placement(); + placement.order.appData = B256::repeat_byte(0xee); + let err = build_eth_flow_creation(1, &placement).unwrap_err(); + assert!(matches!(err, BuildError::Cowprotocol(_))); + } + + // ---- BLEU-855: MockHost dispatch tests ---- + + fn programmed_uid(placement: &DecodedPlacement) -> String { + let (_creation, uid) = build_eth_flow_creation(SEPOLIA, placement).unwrap(); + format!("{uid}") + } + + #[test] + fn placement_log_submits_order_and_persists_submitted_uid() { + let host = MockHost::new(); + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let view = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + let placement = + decode_order_placement(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data).unwrap(); + let uid = programmed_uid(&placement); + host.cow_api.respond(Ok(uid.clone())); + + on_logs(&host, &[view]).unwrap(); + + assert_eq!(host.cow_api.call_count(), 1); + assert!( + host.store + .snapshot() + .contains_key(&format!("submitted:{uid}")) + ); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("backoff:{uid}")) + ); + assert!(host.logging.contains(&format!("ethflow submitted {uid}"))); + } + + #[test] + fn redelivered_placement_is_skipped_via_submitted_uid_dedup() { + // BLEU-833 / commit c5e4d7d regression guard: a host + // reconnect or engine restart that replays the same + // OrderPlacement log must not double-submit. + let host = MockHost::new(); + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let view1 = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + let placement = + decode_order_placement(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data).unwrap(); + let uid = programmed_uid(&placement); + host.cow_api.respond(Ok(uid.clone())); + + on_logs(&host, &[view1]).unwrap(); + assert_eq!(host.cow_api.call_count(), 1); + + let view2 = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + on_logs(&host, &[view2]).unwrap(); + + assert_eq!( + host.cow_api.call_count(), + 1, + "redelivered placement must not resubmit" + ); + assert!(host.logging.contains("already submitted")); + } + + #[test] + fn submit_transient_error_writes_backoff_marker_and_returns() { + let host = MockHost::new(); + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let view = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + let placement = + decode_order_placement(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data).unwrap(); + let uid = programmed_uid(&placement); + + // InsufficientFee classifies as TryNextBlock per cowprotocol's + // retry_hint; ethflow-watcher treats every retriable + // classification as a backoff: marker (next event will retry, + // not next block). + let api_body = serde_json::json!({ + "errorType": "InsufficientFee", + "description": "fee too low", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InsufficientFee".into(), + data: Some(api_body), + })); + + on_logs(&host, &[view]).unwrap(); + + assert!( + host.store + .snapshot() + .contains_key(&format!("backoff:{uid}")) + ); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("submitted:{uid}")) + ); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("dropped:{uid}")) + ); + assert!(host.logging.contains("ethflow backoff")); + } + + #[test] + fn submit_permanent_error_persists_dropped_uid_and_clears_backoff() { + let host = MockHost::new(); + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let placement = + decode_order_placement(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data).unwrap(); + let uid = programmed_uid(&placement); + + // Pre-seed a backoff: marker (prior transient attempt). A + // permanent failure on the retry must drop the order AND + // clear the stale backoff: row so we never have both at rest. + host.store.set(&format!("backoff:{uid}"), b"").unwrap(); + + let api_body = serde_json::json!({ + "errorType": "InvalidSignature", + "description": "bad sig", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InvalidSignature".into(), + data: Some(api_body), + })); + + let view = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + on_logs(&host, &[view]).unwrap(); + + assert!( + host.store + .snapshot() + .contains_key(&format!("dropped:{uid}")) + ); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("backoff:{uid}")), + "terminal `dropped:` must clear stale `backoff:` marker" + ); + assert!(host.logging.contains("ethflow dropped")); + } + + #[test] + fn eip1271_signature_shape_round_trips_through_submit_body() { + // Snapshot the JSON the host receives so reviewers can confirm + // the signing scheme / signature wire shape stays stable. The + // orderbook is strict about both fields. + let host = MockHost::new(); + let event = sample_event_for_decode(); + let (topics, data) = encode_log(&event); + let view = placement_log_view(ETH_FLOW_PRODUCTION.as_slice(), &topics, &data); + host.cow_api.respond(Ok("0xfeedface".to_string())); + + on_logs(&host, &[view]).unwrap(); + + let body_json = host + .cow_api + .last_body_as_json() + .expect("body was submitted"); + // OrderCreation serialises signingScheme as a lowercase string + // and signature as a hex-prefixed bytes blob. + assert_eq!(body_json["signingScheme"].as_str(), Some("eip1271")); + let sig_hex = body_json["signature"] + .as_str() + .expect("signature is a string"); + assert!(sig_hex.starts_with("0x")); + assert_eq!( + sig_hex, "0xc0ffeec0ffeec0ffee", + "EIP-1271 signature blob must be passed through verbatim" + ); + // EthFlow contract is the orderbook `from`, not the original sender. + assert_eq!( + body_json["from"].as_str(), + Some(&*format!("{:#x}", ETH_FLOW_PRODUCTION)) + ); + } +} diff --git a/modules/example/module.toml b/modules/example/module.toml new file mode 100644 index 0000000..528c84b --- /dev/null +++ b/modules/example/module.toml @@ -0,0 +1,27 @@ +# Example module manifest - exercises the 0.2 manifest schema end-to-end. + +[module] +name = "example" +version = "0.1.0" +# Placeholder content hash. 0.2 parses but does not verify this; 0.3 will +# compare it against the sha256 of the loaded component bytes. +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +# 0.2 reference engine provides all listed capabilities; this list is a +# sanity check + future-proofing. +required = ["logging"] + +# Capabilities the module would use opportunistically. In 0.2 these are +# parsed and logged; trap-stub fallback for absent optionals ships in 0.3. +optional = [] + +[capabilities.http] +# Per-module HTTP allowlist. Empty list = no outbound HTTP permitted. +# Entries are exact hostnames or *.domain wildcards. +allow = [] + +[config] +# Stringly-typed in 0.2 (typed variant on 0.3 roadmap). Numbers and +# booleans are flattened to their text form by the host on the way through. +name = "example" diff --git a/modules/example/nexum.toml b/modules/example/nexum.toml index e17a547..528c84b 100644 --- a/modules/example/nexum.toml +++ b/modules/example/nexum.toml @@ -1,4 +1,4 @@ -# Example module manifest — exercises the 0.2 manifest schema end-to-end. +# Example module manifest - exercises the 0.2 manifest schema end-to-end. [module] name = "example" diff --git a/modules/example/src/lib.rs b/modules/example/src/lib.rs index a008a3d..832f596 100644 --- a/modules/example/src/lib.rs +++ b/modules/example/src/lib.rs @@ -1,5 +1,6 @@ // wit_bindgen::generate! expands to host-import shims whose arity matches // the WIT signatures, which can exceed clippy's too-many-arguments threshold. +#![cfg_attr(not(test), warn(unused_crate_dependencies))] #![allow(clippy::too_many_arguments)] wit_bindgen::generate!({ diff --git a/modules/examples/balance-tracker/Cargo.toml b/modules/examples/balance-tracker/Cargo.toml new file mode 100644 index 0000000..5fe8607 --- /dev/null +++ b/modules/examples/balance-tracker/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "balance-tracker" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "Shepherd example module: tracks native-token balances of a list of addresses and emits a log when one changes by more than a threshold. Demonstrates chain::request + local-store + multi-key persistence." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +shepherd-sdk = { path = "../../../crates/shepherd-sdk" } +wit-bindgen = { version = "0.57", default-features = false, features = ["macros", "realloc"] } diff --git a/modules/examples/balance-tracker/module.toml b/modules/examples/balance-tracker/module.toml new file mode 100644 index 0000000..2f4bd17 --- /dev/null +++ b/modules/examples/balance-tracker/module.toml @@ -0,0 +1,32 @@ +# balance-tracker example module: tracks native-token balances of a +# fixed address list and emits a Warn log when one moves by more than +# `change_threshold` wei between blocks. Demonstrates `chain::request` +# (non-eth_call), per-key `local-store` state, and "diff-against-last- +# seen" patterns reusable across indexer modules. + +[module] +name = "balance-tracker" +version = "0.1.0" +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +required = ["logging", "chain", "local-store"] +optional = [] + +[capabilities.http] +allow = [] + +# --- subscriptions ---------------------------------------------------- + +[[subscription]] +kind = "block" +chain_id = 11155111 + +# --- config ----------------------------------------------------------- + +[config] +# Comma-separated list of 0x-prefixed 20-byte addresses. Whitespace +# around entries is tolerated. +addresses = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8,0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" +# Change threshold in wei. Default is 0.1 ETH = 10**17. +change_threshold = "100000000000000000" diff --git a/modules/examples/balance-tracker/src/lib.rs b/modules/examples/balance-tracker/src/lib.rs new file mode 100644 index 0000000..041671b --- /dev/null +++ b/modules/examples/balance-tracker/src/lib.rs @@ -0,0 +1,335 @@ +//! # balance-tracker (example Shepherd module) +//! +//! Subscribes to blocks, reads `eth_getBalance(addr)` for every +//! address in `[config].addresses` (comma-separated), persists the +//! last seen value under `balance:{addr}` in local-store, and emits +//! a Warn-level log line when the balance changes by more than +//! `[config].change_threshold` wei since the previous block. +//! +//! Demonstrates: +//! +//! - `chain::request` with a non-`eth_call` method (raw JSON-RPC), +//! - `local-store` for persistent per-key state across events, +//! - a "diff against last seen" pattern that is generic across many +//! indexer modules (transfer monitor, allowance tracker, …). +//! +//! ## Config +//! +//! ```toml +//! [config] +//! # Comma-separated list of 0x-prefixed 20-byte addresses. +//! addresses = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8,0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" +//! # Change threshold in wei; an alert fires when the delta exceeds it. +//! change_threshold = "100000000000000000" # 0.1 ETH +//! ``` + +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![allow(clippy::too_many_arguments)] + +wit_bindgen::generate!({ + path: ["../../../wit/nexum-host", "../../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +use std::sync::OnceLock; + +use shepherd_sdk::prelude::{Address, U256}; + +use nexum::host::types::HostErrorKind; +use nexum::host::{chain, local_store, logging, types}; + +/// Resolved settings parsed from `[config]` at `init` and read on +/// every event. +#[derive(Debug)] +struct Settings { + addresses: Vec
, + change_threshold: U256, +} + +static SETTINGS: OnceLock = OnceLock::new(); + +struct BalanceTracker; + +impl Guest for BalanceTracker { + fn init(config: Vec<(String, String)>) -> Result<(), HostError> { + match parse_settings(&config) { + Ok(s) => { + logging::log( + logging::Level::Info, + &format!( + "balance-tracker init: {} addresses, threshold={} wei", + s.addresses.len(), + s.change_threshold, + ), + ); + let _ = SETTINGS.set(s); + Ok(()) + } + Err(e) => Err(HostError { + domain: "balance-tracker".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("balance-tracker: invalid [config]: {e}"), + data: None, + }), + } + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + let Some(s) = SETTINGS.get() else { + return Ok(()); // init failed; no-op. + }; + if let types::Event::Block(block) = event { + for addr in &s.addresses { + if let Err(err) = check_one(block.chain_id, *addr, s.change_threshold) { + // Surface but do not propagate - a single flaky + // eth_getBalance shouldn't stop the loop. + logging::log( + logging::Level::Warn, + &format!("balance-tracker {addr:#x} ({}): {}", err.code, err.message), + ); + } + } + } + Ok(()) + } +} + +/// Poll one address: fetch latest balance, diff against the last +/// stored value, emit a log if the delta crosses `threshold`, then +/// persist the new value under `balance:{addr}`. +fn check_one(chain_id: u64, addr: Address, threshold: U256) -> Result<(), HostError> { + let current = fetch_balance(chain_id, addr)?; + let key = balance_key(&addr); + let prior = local_store::get(&key)? + .and_then(|b| parse_u256_le(&b)) + .unwrap_or(U256::ZERO); + + if abs_diff(current, prior) >= threshold { + // Distinguish first-seen (prior == ZERO and we have no + // record) from a real change - the Warn line carries the + // delta direction so an operator can grep. + let direction = if current > prior { "+" } else { "-" }; + logging::log( + logging::Level::Warn, + &format!( + "balance-tracker {addr:#x} changed {direction}{} wei (prior={prior}, current={current})", + abs_diff(current, prior), + ), + ); + } + // Always persist the latest reading so the next event's diff is + // accurate even when the change was below threshold. + local_store::set(&key, &u256_to_le_bytes(current))?; + Ok(()) +} + +/// `chain::request("eth_getBalance", [addr, "latest"])` -> `U256`. +/// Returns a typed HostError on any failure; the caller decides +/// whether to keep going or surface upward. +fn fetch_balance(chain_id: u64, addr: Address) -> Result { + let params = format!("[\"{addr:#x}\",\"latest\"]"); + let result_json = chain::request(chain_id, "eth_getBalance", ¶ms)?; + parse_balance_hex(&result_json).ok_or_else(|| HostError { + domain: "balance-tracker".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("eth_getBalance result not a hex string: {result_json}"), + data: None, + }) +} + +// ---- pure helpers (tested) ----------------------------------------- + +/// Parse the `"0x..."` JSON string `eth_getBalance` returns into a +/// `U256`. `None` on shape mismatch. +fn parse_balance_hex(result_json: &str) -> Option { + let trimmed = result_json.trim(); + let body = trimmed + .strip_prefix('"') + .and_then(|s| s.strip_suffix('"'))?; + let hex = body.strip_prefix("0x").unwrap_or(body); + // Empty hex (`"0x"`) is a legitimate zero balance. + if hex.is_empty() { + return Some(U256::ZERO); + } + U256::from_str_radix(hex, 16).ok() +} + +fn balance_key(addr: &Address) -> String { + format!("balance:{addr:#x}") +} + +fn abs_diff(a: U256, b: U256) -> U256 { + if a >= b { a - b } else { b - a } +} + +fn u256_to_le_bytes(v: U256) -> [u8; 32] { + v.to_le_bytes() +} + +fn parse_u256_le(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + let mut buf = [0u8; 32]; + buf.copy_from_slice(bytes); + Some(U256::from_le_bytes(buf)) +} + +/// Parse a comma-separated address list, stripping whitespace. +fn parse_addresses(raw: &str) -> Result, String> { + let mut out = Vec::new(); + for (i, part) in raw.split(',').enumerate() { + let trimmed = part.trim(); + if trimmed.is_empty() { + continue; + } + let addr = trimmed + .parse::
() + .map_err(|e| format!("address #{i} ({trimmed:?}): {e}"))?; + out.push(addr); + } + if out.is_empty() { + return Err("expected at least one address".into()); + } + Ok(out) +} + +fn parse_settings(entries: &[(String, String)]) -> Result { + let addresses_raw = entries + .iter() + .find(|(k, _)| k == "addresses") + .map(|(_, v)| v.as_str()) + .ok_or_else(|| "missing key \"addresses\"".to_string())?; + let change_threshold_raw = entries + .iter() + .find(|(k, _)| k == "change_threshold") + .map(|(_, v)| v.as_str()) + .ok_or_else(|| "missing key \"change_threshold\"".to_string())?; + let addresses = parse_addresses(addresses_raw)?; + let change_threshold = change_threshold_raw + .parse::() + .map_err(|e| format!("change_threshold: {e}"))?; + Ok(Settings { + addresses, + change_threshold, + }) +} + +export!(BalanceTracker); + +#[cfg(test)] +mod tests { + use super::*; + use shepherd_sdk::prelude::address; + + #[test] + fn parse_balance_hex_decodes_canonical_response() { + // 0x16345785d8a0000 = 100_000_000_000_000_000 = 0.1 ETH. + assert_eq!( + parse_balance_hex("\"0x16345785d8a0000\""), + Some(U256::from(100_000_000_000_000_000_u128)), + ); + } + + #[test] + fn parse_balance_hex_handles_zero() { + assert_eq!(parse_balance_hex("\"0x0\""), Some(U256::ZERO)); + assert_eq!(parse_balance_hex("\"0x\""), Some(U256::ZERO)); + } + + #[test] + fn parse_balance_hex_rejects_unquoted() { + // Real responses are always quoted; reject as a safety net. + assert!(parse_balance_hex("0x1234").is_none()); + } + + #[test] + fn parse_balance_hex_rejects_garbage() { + assert!(parse_balance_hex("\"hello\"").is_none()); + } + + #[test] + fn u256_le_round_trip() { + let v = U256::from(42_u64); + let bytes = u256_to_le_bytes(v); + assert_eq!(parse_u256_le(&bytes), Some(v)); + } + + #[test] + fn parse_u256_le_rejects_wrong_length() { + assert!(parse_u256_le(&[0u8; 16]).is_none()); + assert!(parse_u256_le(&[0u8; 64]).is_none()); + } + + #[test] + fn abs_diff_is_symmetric() { + let a = U256::from(100_u64); + let b = U256::from(30_u64); + assert_eq!(abs_diff(a, b), U256::from(70_u64)); + assert_eq!(abs_diff(b, a), U256::from(70_u64)); + assert_eq!(abs_diff(a, a), U256::ZERO); + } + + #[test] + fn parse_addresses_handles_whitespace_and_multiple() { + let raw = " 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 ,\ + 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"; + let parsed = parse_addresses(raw).unwrap(); + assert_eq!(parsed.len(), 2); + assert_eq!( + parsed[0], + address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"), + ); + } + + #[test] + fn parse_addresses_skips_empty_segments() { + let parsed = parse_addresses("0x70997970C51812dc3A010C7d01b50e0d17dc79C8,,").unwrap(); + assert_eq!(parsed.len(), 1); + } + + #[test] + fn parse_addresses_rejects_empty_list() { + assert!(parse_addresses("").is_err()); + assert!(parse_addresses(", ,").is_err()); + } + + #[test] + fn parse_addresses_rejects_malformed() { + assert!(parse_addresses("not-an-address").is_err()); + } + + #[test] + fn parse_settings_happy_path() { + let entries = vec![ + ( + "addresses".into(), + "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".into(), + ), + ("change_threshold".into(), "100000000000000000".into()), + ]; + let s = parse_settings(&entries).unwrap(); + assert_eq!(s.addresses.len(), 1); + assert_eq!(s.change_threshold, U256::from(100_000_000_000_000_000_u128)); + } + + #[test] + fn parse_settings_rejects_missing_keys() { + assert!( + parse_settings(&[("change_threshold".into(), "1".into())]) + .unwrap_err() + .contains("addresses") + ); + assert!( + parse_settings(&[( + "addresses".into(), + "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".into() + )]) + .unwrap_err() + .contains("change_threshold") + ); + } +} diff --git a/modules/examples/price-alert/Cargo.toml b/modules/examples/price-alert/Cargo.toml new file mode 100644 index 0000000..a4173d7 --- /dev/null +++ b/modules/examples/price-alert/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "price-alert" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "Shepherd example module: polls a Chainlink price oracle every block and emits a Warn log when the price crosses a config-supplied threshold." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +shepherd-sdk = { path = "../../../crates/shepherd-sdk" } +alloy-primitives = { version = "1.5", default-features = false, features = ["std"] } +alloy-sol-types = { version = "1.5", default-features = false, features = ["std"] } +wit-bindgen = { version = "0.57", default-features = false, features = ["macros", "realloc"] } + +[dev-dependencies] +shepherd-sdk-test = { path = "../../../crates/shepherd-sdk-test" } diff --git a/modules/examples/price-alert/module.toml b/modules/examples/price-alert/module.toml new file mode 100644 index 0000000..e5f95fc --- /dev/null +++ b/modules/examples/price-alert/module.toml @@ -0,0 +1,43 @@ +# price-alert example module: polls a Chainlink price oracle on every +# block and emits a Warn log when the price crosses a config-supplied +# threshold. Demonstrates `chain::request` + ABI decode via +# `alloy_sol_types` + config-driven module behaviour. + +[module] +name = "price-alert" +version = "0.1.0" +# Placeholder content hash. 0.2 parses but does not verify this; 0.3 +# will compare against the sha256 of the loaded component bytes. +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +required = ["logging", "chain"] +optional = [] + +[capabilities.http] +# All chain traffic flows through the `chain` capability (host's +# pinned alloy provider). No direct `http` calls. +allow = [] + +# --- subscriptions ---------------------------------------------------- + +# New blocks on Sepolia drive the polling cadence. +[[subscription]] +kind = "block" +chain_id = 11155111 + +# --- config ----------------------------------------------------------- + +[config] +# Chainlink AggregatorV3Interface address. Default points at the +# canonical ETH/USD feed on Sepolia. +oracle_address = "0x694AA1769357215DE4FAC081bf1f309aDC325306" +# Decimals the oracle reports (Chainlink USD pairs are 8). +decimals = "8" +# Threshold in the oracle's native decimal units. +threshold = "2500.00" +# "above" -> fires when answer >= threshold +# "below" -> fires when answer <= threshold +direction = "below" +# Throttle: only poll every Nth block. Default 1. +every_n_blocks = "1" diff --git a/modules/examples/price-alert/src/lib.rs b/modules/examples/price-alert/src/lib.rs new file mode 100644 index 0000000..0130bcb --- /dev/null +++ b/modules/examples/price-alert/src/lib.rs @@ -0,0 +1,161 @@ +//! # price-alert (example Shepherd module) +//! +//! Polls a Chainlink price oracle on every new block (throttled by +//! `every_n_blocks`) and emits a Warn-level log when the price +//! crosses a config-supplied threshold. +//! +//! ## Module layout +//! +//! - `strategy.rs` holds the pure logic and tests against +//! `shepherd_sdk::host::Host`. It does not know `wit-bindgen` +//! exists. +//! - `lib.rs` (this file) bridges the per-cdylib wit-bindgen imports +//! into the trait surface and delegates `init` / `on_event` to +//! `strategy`. +//! +//! This split is the M3 "host trait + adapter" recipe documented in +//! `docs/tutorial-first-module.md`. + +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![allow(clippy::too_many_arguments)] + +wit_bindgen::generate!({ + path: ["../../../wit/nexum-host", "../../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +mod strategy; + +use std::sync::OnceLock; + +use shepherd_sdk::host::{ + ChainHost, CowApiHost, HostError as SdkHostError, HostErrorKind as SdkHostErrorKind, + LocalStoreHost, LogLevel as SdkLogLevel, LoggingHost, +}; + +use nexum::host::types::HostErrorKind; +use nexum::host::{chain, local_store, logging, types}; +use shepherd::cow::cow_api; + +static SETTINGS: OnceLock = OnceLock::new(); + +/// Wraps the module's per-cdylib wit-bindgen imports so the strategy +/// can hold a `&impl Host` instead of dispatching on the free +/// functions directly. The implementation is mechanical and identical +/// across modules; a future declarative macro in `shepherd-sdk` will +/// elide the boilerplate. +struct WitBindgenHost; + +impl ChainHost for WitBindgenHost { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + chain::request(chain_id, method, params).map_err(convert_err) + } +} + +impl LocalStoreHost for WitBindgenHost { + fn get(&self, key: &str) -> Result>, SdkHostError> { + local_store::get(key).map_err(convert_err) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), SdkHostError> { + local_store::set(key, value).map_err(convert_err) + } + fn delete(&self, key: &str) -> Result<(), SdkHostError> { + local_store::delete(key).map_err(convert_err) + } + fn list_keys(&self, prefix: &str) -> Result, SdkHostError> { + local_store::list_keys(prefix).map_err(convert_err) + } +} + +impl CowApiHost for WitBindgenHost { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + cow_api::submit_order(chain_id, body).map_err(convert_err) + } +} + +impl LoggingHost for WitBindgenHost { + fn log(&self, level: SdkLogLevel, message: &str) { + logging::log(convert_level(level), message); + } +} + +fn convert_err(e: HostError) -> SdkHostError { + SdkHostError { + domain: e.domain, + kind: match e.kind { + HostErrorKind::Unsupported => SdkHostErrorKind::Unsupported, + HostErrorKind::Unavailable => SdkHostErrorKind::Unavailable, + HostErrorKind::Denied => SdkHostErrorKind::Denied, + HostErrorKind::RateLimited => SdkHostErrorKind::RateLimited, + HostErrorKind::Timeout => SdkHostErrorKind::Timeout, + HostErrorKind::InvalidInput => SdkHostErrorKind::InvalidInput, + HostErrorKind::Internal => SdkHostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn sdk_err_into_wit(e: SdkHostError) -> HostError { + HostError { + domain: e.domain, + kind: match e.kind { + SdkHostErrorKind::Unsupported => HostErrorKind::Unsupported, + SdkHostErrorKind::Unavailable => HostErrorKind::Unavailable, + SdkHostErrorKind::Denied => HostErrorKind::Denied, + SdkHostErrorKind::RateLimited => HostErrorKind::RateLimited, + SdkHostErrorKind::Timeout => HostErrorKind::Timeout, + SdkHostErrorKind::InvalidInput => HostErrorKind::InvalidInput, + SdkHostErrorKind::Internal => HostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn convert_level(l: SdkLogLevel) -> logging::Level { + match l { + SdkLogLevel::Trace => logging::Level::Trace, + SdkLogLevel::Debug => logging::Level::Debug, + SdkLogLevel::Info => logging::Level::Info, + SdkLogLevel::Warn => logging::Level::Warn, + SdkLogLevel::Error => logging::Level::Error, + } +} + +struct PriceAlert; + +impl Guest for PriceAlert { + fn init(config: Vec<(String, String)>) -> Result<(), HostError> { + let cfg = strategy::parse_config(&config).map_err(sdk_err_into_wit)?; + logging::log( + logging::Level::Info, + &format!( + "price-alert init: oracle={:#x} threshold={} direction={:?} every_n_blocks={}", + cfg.oracle_address, cfg.threshold_scaled, cfg.direction, cfg.every_n_blocks, + ), + ); + // OnceLock::set fails only if already set - in a single-init + // module that means a re-entry from the supervisor, which is + // not a hard error; we keep the first parse. + let _ = SETTINGS.set(cfg); + Ok(()) + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + let Some(cfg) = SETTINGS.get() else { + return Ok(()); // init failed; no-op. + }; + if let types::Event::Block(block) = event { + strategy::on_block(&WitBindgenHost, block.chain_id, cfg, block.number) + .map_err(sdk_err_into_wit)?; + } + // Logs / Tick / Message are not used by this example. + Ok(()) + } +} + +export!(PriceAlert); diff --git a/modules/examples/price-alert/src/strategy.rs b/modules/examples/price-alert/src/strategy.rs new file mode 100644 index 0000000..71f17c7 --- /dev/null +++ b/modules/examples/price-alert/src/strategy.rs @@ -0,0 +1,523 @@ +//! Pure strategy logic for the price-alert module. +//! +//! Every interaction with the world flows through the [`Host`] trait +//! seam exposed by `shepherd-sdk` - no direct calls to wit-bindgen- +//! generated free functions live here. The `lib.rs` glue wraps a +//! `WitBindgenHost` adapter around the module's per-cdylib wit-bindgen +//! imports and hands it to [`on_block`]; tests under `#[cfg(test)]` +//! hand the same function a `shepherd_sdk_test::MockHost`. + +use alloy_primitives::I256; +use alloy_sol_types::{SolCall, sol}; +use shepherd_sdk::chain::{eth_call_params, parse_eth_call_result}; +use shepherd_sdk::host::{Host, HostError, HostErrorKind, LogLevel}; +use shepherd_sdk::prelude::{Address, U256}; + +sol! { + /// Chainlink AggregatorV3Interface - only the function this module + /// needs. + interface AggregatorV3 { + function latestRoundData() external view returns ( + uint80 roundId, + int256 answer, + uint256 startedAt, + uint256 updatedAt, + uint80 answeredInRound + ); + } +} + +/// Resolved configuration, parsed from `module.toml::[config]` at +/// `init` and read on every `on_event`. +#[derive(Debug)] +pub struct Settings { + /// Chainlink AggregatorV3Interface address. + pub oracle_address: Address, + /// Threshold scaled to the oracle's native units + /// (`threshold_decimal * 10**decimals`). + pub threshold_scaled: I256, + /// Which side of the threshold fires. + pub direction: Direction, + /// Throttle: only poll every Nth block. + pub every_n_blocks: u64, +} + +/// Which side of the threshold the alert fires on. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Direction { + /// Fire when `answer >= threshold`. + Above, + /// Fire when `answer <= threshold`. + Below, +} + +/// React to a new block. +/// +/// Returns `Ok(())` on success and on recoverable upstream failures +/// (oracle RPC error, decode failure) - the strategy logs a Warn and +/// lets the next block re-poll rather than propagating into the +/// supervisor. Only host-level I/O on the persistence side would +/// bubble up via `?`, and this module does not touch the store. +pub fn on_block( + host: &H, + chain_id: u64, + settings: &Settings, + block_number: u64, +) -> Result<(), HostError> { + if !block_number.is_multiple_of(settings.every_n_blocks) { + return Ok(()); + } + let call_data = AggregatorV3::latestRoundDataCall {}.abi_encode(); + let params = eth_call_params(&settings.oracle_address, &call_data); + let result_json = match host.request(chain_id, "eth_call", ¶ms) { + Ok(s) => s, + Err(err) => { + host.log( + LogLevel::Warn, + &format!( + "price-alert eth_call failed ({}): {}", + err.code, err.message + ), + ); + return Ok(()); + } + }; + let Some(bytes) = parse_eth_call_result(&result_json) else { + host.log( + LogLevel::Warn, + &format!("price-alert: cannot decode result hex {result_json}"), + ); + return Ok(()); + }; + let decoded = match AggregatorV3::latestRoundDataCall::abi_decode_returns(&bytes) { + Ok(d) => d, + Err(e) => { + host.log( + LogLevel::Warn, + &format!("price-alert: latestRoundData decode failed: {e}"), + ); + return Ok(()); + } + }; + let answer = decoded.answer; + if classify(answer, settings.threshold_scaled, settings.direction) { + host.log( + LogLevel::Warn, + &format!( + "price-alert: TRIGGERED answer={answer} threshold={} ({:?})", + settings.threshold_scaled, settings.direction, + ), + ); + } else { + host.log( + LogLevel::Info, + &format!( + "price-alert: ok answer={answer} threshold={} ({:?})", + settings.threshold_scaled, settings.direction, + ), + ); + } + Ok(()) +} + +/// `true` when `answer` is on the firing side of `threshold` per +/// `direction`. Pure - exercised by the unit tests. +pub fn classify(answer: I256, threshold: I256, direction: Direction) -> bool { + match direction { + Direction::Above => answer >= threshold, + Direction::Below => answer <= threshold, + } +} + +/// Parse `module.toml::[config]` into a typed [`Settings`]. +/// +/// One-shot config-parser style: returns `Result` so the +/// `Guest::init` adapter can lift the failure into the wit-bindgen +/// `HostError` with no extra plumbing. +pub fn parse_config(entries: &[(String, String)]) -> Result { + let oracle_address = config_get(entries, "oracle_address")? + .parse::
() + .map_err(|e| config_err(format!("oracle_address: {e}")))?; + let decimals = config_get(entries, "decimals")? + .parse::() + .map_err(|e| config_err(format!("decimals: {e}")))?; + if decimals > 38 { + return Err(config_err(format!( + "decimals={decimals} exceeds the I256 power-of-ten budget" + ))); + } + let threshold_decimal = config_get(entries, "threshold")?; + let threshold_scaled = scale_threshold(threshold_decimal, decimals)?; + let direction = match config_get(entries, "direction")? + .to_ascii_lowercase() + .as_str() + { + "above" => Direction::Above, + "below" => Direction::Below, + other => { + return Err(config_err(format!( + "direction: expected 'above'|'below', got {other:?}" + ))); + } + }; + let every_n_blocks = config_get_optional(entries, "every_n_blocks") + .map(|s| { + s.parse::() + .map_err(|e| config_err(format!("every_n_blocks: {e}"))) + }) + .transpose()? + .unwrap_or(1) + .max(1); + Ok(Settings { + oracle_address, + threshold_scaled, + direction, + every_n_blocks, + }) +} + +fn config_get<'a>(entries: &'a [(String, String)], key: &str) -> Result<&'a str, HostError> { + entries + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v.as_str()) + .ok_or_else(|| config_err(format!("missing key {key:?}"))) +} + +fn config_get_optional<'a>(entries: &'a [(String, String)], key: &str) -> Option<&'a str> { + entries + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v.as_str()) +} + +fn config_err(message: impl Into) -> HostError { + HostError { + domain: "price-alert".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("price-alert: invalid [config]: {}", message.into()), + data: None, + } +} + +/// Multiply `threshold_decimal` (e.g. `"2500.00"`) by `10**decimals` +/// into an `I256` for direct comparison with the oracle's answer. +fn scale_threshold(threshold_decimal: &str, decimals: u32) -> Result { + let (sign, body) = if let Some(rest) = threshold_decimal.strip_prefix('-') { + (-1i32, rest) + } else { + (1, threshold_decimal) + }; + let (whole, frac) = match body.split_once('.') { + Some((w, f)) => (w, f), + None => (body, ""), + }; + if whole.is_empty() && frac.is_empty() { + return Err(config_err("threshold: empty")); + } + if !whole.chars().all(|c| c.is_ascii_digit()) || !frac.chars().all(|c| c.is_ascii_digit()) { + return Err(config_err(format!( + "threshold: non-digit character in {threshold_decimal:?}" + ))); + } + let frac_len = frac.len() as u32; + let composed: String = if frac_len <= decimals { + let mut s = String::with_capacity(whole.len() + decimals as usize); + s.push_str(whole); + s.push_str(frac); + for _ in 0..(decimals - frac_len) { + s.push('0'); + } + s + } else { + let mut s = String::with_capacity(whole.len() + decimals as usize); + s.push_str(whole); + s.push_str(&frac[..decimals as usize]); + s + }; + let raw = if composed.is_empty() { "0" } else { &composed }; + let unsigned: U256 = raw + .parse() + .map_err(|e| config_err(format!("threshold parse: {e}")))?; + let signed = + I256::try_from(unsigned).map_err(|e| config_err(format!("threshold range: {e}")))?; + Ok(if sign < 0 { -signed } else { signed }) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::hex; + use shepherd_sdk::host::HostErrorKind as Kind; + use shepherd_sdk_test::MockHost; + + fn sample_settings(trigger_scaled_dec: i128, direction: Direction) -> Settings { + Settings { + oracle_address: "0x694AA1769357215DE4FAC081bf1f309aDC325306" + .parse() + .unwrap(), + threshold_scaled: I256::try_from(trigger_scaled_dec).unwrap(), + direction, + every_n_blocks: 1, + } + } + + /// Encode a `latestRoundData` return into the `"0x..."` JSON string + /// the host's `chain::request` would yield. + fn oracle_response_json(answer_scaled: i128) -> String { + use alloy_primitives::aliases::U80; + let returns = AggregatorV3::latestRoundDataReturn { + roundId: U80::ZERO, + answer: I256::try_from(answer_scaled).unwrap(), + startedAt: U256::ZERO, + updatedAt: U256::ZERO, + answeredInRound: U80::ZERO, + }; + let encoded = AggregatorV3::latestRoundDataCall::abi_encode_returns(&returns); + let hex = hex::encode_prefixed(encoded); + format!("\"{hex}\"") + } + + fn programmed_eth_call(host: &MockHost, oracle: Address, response: Result) { + let call_data = AggregatorV3::latestRoundDataCall {}.abi_encode(); + let params = eth_call_params(&oracle, &call_data); + host.chain.respond_to("eth_call", ¶ms, response); + } + + // ---- pure helpers ---- + + #[test] + fn classify_below_fires_at_or_under_threshold() { + let t = I256::try_from(100_i32).unwrap(); + assert!(classify( + I256::try_from(99_i32).unwrap(), + t, + Direction::Below + )); + assert!(classify( + I256::try_from(100_i32).unwrap(), + t, + Direction::Below + )); + assert!(!classify( + I256::try_from(101_i32).unwrap(), + t, + Direction::Below + )); + } + + #[test] + fn classify_above_fires_at_or_over_threshold() { + let t = I256::try_from(100_i32).unwrap(); + assert!(classify( + I256::try_from(101_i32).unwrap(), + t, + Direction::Above + )); + assert!(classify( + I256::try_from(100_i32).unwrap(), + t, + Direction::Above + )); + assert!(!classify( + I256::try_from(99_i32).unwrap(), + t, + Direction::Above + )); + } + + #[test] + fn scale_threshold_pads_short_fractional() { + assert_eq!( + scale_threshold("1.5", 8).unwrap(), + I256::try_from(150_000_000_i64).unwrap(), + ); + } + + #[test] + fn scale_threshold_truncates_long_fractional() { + assert_eq!( + scale_threshold("1.123456789", 8).unwrap(), + I256::try_from(112_345_678_i64).unwrap(), + ); + } + + #[test] + fn scale_threshold_handles_no_decimal_point() { + assert_eq!( + scale_threshold("42", 8).unwrap(), + I256::try_from(4_200_000_000_i64).unwrap(), + ); + } + + #[test] + fn scale_threshold_handles_negative_values() { + assert_eq!( + scale_threshold("-1.5", 8).unwrap(), + -I256::try_from(150_000_000_i64).unwrap(), + ); + } + + #[test] + fn scale_threshold_rejects_garbage() { + assert!(matches!( + scale_threshold("abc", 8).unwrap_err().kind, + Kind::InvalidInput + )); + assert!(matches!( + scale_threshold("1.2.3", 8).unwrap_err().kind, + Kind::InvalidInput + )); + } + + #[test] + fn parse_config_happy_path() { + let entries = vec![ + ( + "oracle_address".into(), + "0x694AA1769357215DE4FAC081bf1f309aDC325306".into(), + ), + ("decimals".into(), "8".into()), + ("threshold".into(), "2500.50".into()), + ("direction".into(), "below".into()), + ("every_n_blocks".into(), "5".into()), + ]; + let cfg = parse_config(&entries).unwrap(); + assert_eq!(cfg.direction, Direction::Below); + assert_eq!(cfg.every_n_blocks, 5); + assert_eq!( + cfg.threshold_scaled, + I256::try_from(250_050_000_000_i64).unwrap() + ); + } + + #[test] + fn parse_config_defaults_every_n_blocks_to_one() { + let entries = vec![ + ( + "oracle_address".into(), + "0x694AA1769357215DE4FAC081bf1f309aDC325306".into(), + ), + ("decimals".into(), "8".into()), + ("threshold".into(), "1".into()), + ("direction".into(), "above".into()), + ]; + let cfg = parse_config(&entries).unwrap(); + assert_eq!(cfg.every_n_blocks, 1); + assert_eq!(cfg.direction, Direction::Above); + } + + #[test] + fn parse_config_rejects_missing_key() { + let entries = vec![ + ("decimals".into(), "8".into()), + ("threshold".into(), "1".into()), + ("direction".into(), "above".into()), + ]; + let err = parse_config(&entries).unwrap_err(); + assert!(matches!(err.kind, Kind::InvalidInput)); + assert!(err.message.contains("oracle_address")); + } + + // ---- strategy behaviour against MockHost ---- + + #[test] + fn on_block_idle_when_price_above_below_trigger() { + let host = MockHost::new(); + let settings = sample_settings(/*trigger*/ 250_050_000_000, Direction::Below); + programmed_eth_call( + &host, + settings.oracle_address, + Ok(oracle_response_json(300_000_000_000)), + ); + + on_block(&host, 11_155_111, &settings, 100).unwrap(); + + assert_eq!(host.chain.call_count(), 1); + assert!(host.logging.contains("ok answer=")); + assert_eq!(host.logging.count_at(LogLevel::Warn), 0); + } + + #[test] + fn on_block_triggers_below_threshold() { + let host = MockHost::new(); + let settings = sample_settings(250_050_000_000, Direction::Below); + programmed_eth_call( + &host, + settings.oracle_address, + Ok(oracle_response_json(200_000_000_000)), + ); + + on_block(&host, 11_155_111, &settings, 100).unwrap(); + + assert!(host.logging.contains("TRIGGERED")); + assert_eq!(host.logging.count_at(LogLevel::Warn), 1); + } + + #[test] + fn on_block_triggers_above_threshold() { + let host = MockHost::new(); + let settings = sample_settings(100, Direction::Above); + programmed_eth_call( + &host, + settings.oracle_address, + Ok(oracle_response_json(200)), + ); + + on_block(&host, 11_155_111, &settings, 100).unwrap(); + + assert!(host.logging.contains("TRIGGERED")); + } + + #[test] + fn on_block_warns_and_continues_on_rpc_error() { + let host = MockHost::new(); + let settings = sample_settings(100, Direction::Below); + programmed_eth_call( + &host, + settings.oracle_address, + Err(HostError { + domain: "chain".into(), + kind: Kind::Timeout, + code: 504, + message: "upstream timed out".into(), + data: None, + }), + ); + + // Strategy returns Ok so the supervisor moves on. + on_block(&host, 11_155_111, &settings, 100).unwrap(); + assert!(host.logging.contains("eth_call failed")); + // No "TRIGGERED" / "ok answer=" log because we never got an + // oracle response. + assert!(!host.logging.contains("TRIGGERED")); + } + + #[test] + fn on_block_warns_on_undecodable_result() { + let host = MockHost::new(); + let settings = sample_settings(100, Direction::Below); + programmed_eth_call(&host, settings.oracle_address, Ok("not-json".into())); + + on_block(&host, 11_155_111, &settings, 100).unwrap(); + assert!(host.logging.contains("cannot decode result hex")); + } + + #[test] + fn on_block_respects_every_n_blocks_throttle() { + let host = MockHost::new(); + let mut settings = sample_settings(100, Direction::Below); + settings.every_n_blocks = 5; + programmed_eth_call(&host, settings.oracle_address, Ok(oracle_response_json(50))); + + // Blocks 1..5 do not poll; only block 5 (which divides evenly). + for n in 1..5 { + on_block(&host, 11_155_111, &settings, n).unwrap(); + } + assert_eq!(host.chain.call_count(), 0); + + on_block(&host, 11_155_111, &settings, 5).unwrap(); + assert_eq!(host.chain.call_count(), 1); + } +} diff --git a/modules/examples/stop-loss/Cargo.toml b/modules/examples/stop-loss/Cargo.toml new file mode 100644 index 0000000..0184851 --- /dev/null +++ b/modules/examples/stop-loss/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "stop-loss" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +description = "Shepherd example module: stop-loss order submitter. Watches a Chainlink oracle, submits a pre-signed CoW order when price drops below a configured trigger, dedups via submitted:{uid}." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +shepherd-sdk = { path = "../../../crates/shepherd-sdk" } +cowprotocol = { version = "1.0.0-alpha.3", default-features = false } +alloy-primitives = { version = "1.5", default-features = false, features = ["std"] } +alloy-sol-types = { version = "1.5", default-features = false, features = ["std"] } +serde_json = { version = "1", default-features = false, features = ["alloc"] } +wit-bindgen = { version = "0.57", default-features = false, features = ["macros", "realloc"] } + +[dev-dependencies] +shepherd-sdk-test = { path = "../../../crates/shepherd-sdk-test" } diff --git a/modules/examples/stop-loss/module.toml b/modules/examples/stop-loss/module.toml new file mode 100644 index 0000000..17cebad --- /dev/null +++ b/modules/examples/stop-loss/module.toml @@ -0,0 +1,41 @@ +# stop-loss example module: watches a Chainlink oracle and submits a +# CoW order when the price drops below the configured trigger. +# Demonstrates eth_call + OrderCreation + cow-api submit + local-store +# dedup, the full M3 SDK surface. + +[module] +name = "stop-loss" +version = "0.1.0" +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +required = ["logging", "chain", "local-store", "cow-api"] +optional = [] + +[capabilities.http] +allow = [] + +# --- subscriptions ---------------------------------------------------- + +[[subscription]] +kind = "block" +chain_id = 11155111 # Sepolia + +# --- config ----------------------------------------------------------- + +[config] +# Chainlink AggregatorV3Interface address (ETH/USD on Sepolia). +oracle_address = "0x694AA1769357215DE4FAC081bf1f309aDC325306" +# Oracle's decimals (Chainlink USD pairs are 8). +decimals = "8" +# Trigger price in the oracle's native decimal units. Below this, sell. +trigger_price = "2500.00" +# Order parameters. The owner pre-signs via GPv2Signing.setPreSignature +# (on-chain, outside this module); the module submits the body with +# Signature::PreSign on trigger. +owner = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" +sell_token = "0x6810e776880C02933D47DB1b9fc05908e5386b96" +buy_token = "0xfff9976782d46cc05630d1f6ebab18b2324d6b14" +sell_amount_wei = "1000000000000000000" +buy_amount_wei = "300000000000000000" +valid_to_seconds = "4294967295" diff --git a/modules/examples/stop-loss/src/lib.rs b/modules/examples/stop-loss/src/lib.rs new file mode 100644 index 0000000..de7aaf0 --- /dev/null +++ b/modules/examples/stop-loss/src/lib.rs @@ -0,0 +1,154 @@ +//! # stop-loss (example Shepherd module) +//! +//! Watches a Chainlink price oracle on every block. When the price +//! drops at or below `trigger_price`, the module submits a pre-signed +//! CoW order using the parameters from `module.toml::[config]` and +//! persists `submitted:{uid}` to dedup re-poll attempts. The owner is +//! expected to have called `GPv2Signing.setPreSignature` on-chain +//! ahead of the trigger so the orderbook accepts the submission. +//! +//! ## Module layout +//! +//! - `strategy.rs` holds the pure logic and tests against +//! `shepherd_sdk::host::Host`. It does not know `wit-bindgen` +//! exists. +//! - `lib.rs` (this file) is the per-cdylib glue: wit-bindgen import +//! shims, the `WitBindgenHost` adapter, the `Guest` impl. +//! +//! Same recipe as `price-alert` (BLEU-851) - the wit-bindgen adapter +//! is intentionally mechanical and is a candidate for a future +//! declarative macro in `shepherd-sdk`. + +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![allow(clippy::too_many_arguments)] + +wit_bindgen::generate!({ + path: ["../../../wit/nexum-host", "../../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +mod strategy; + +use std::sync::OnceLock; + +use shepherd_sdk::host::{ + ChainHost, CowApiHost, HostError as SdkHostError, HostErrorKind as SdkHostErrorKind, + LocalStoreHost, LogLevel as SdkLogLevel, LoggingHost, +}; + +use nexum::host::types::HostErrorKind; +use nexum::host::{chain, local_store, logging, types}; +use shepherd::cow::cow_api; + +static SETTINGS: OnceLock = OnceLock::new(); + +struct WitBindgenHost; + +impl ChainHost for WitBindgenHost { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + chain::request(chain_id, method, params).map_err(convert_err) + } +} + +impl LocalStoreHost for WitBindgenHost { + fn get(&self, key: &str) -> Result>, SdkHostError> { + local_store::get(key).map_err(convert_err) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), SdkHostError> { + local_store::set(key, value).map_err(convert_err) + } + fn delete(&self, key: &str) -> Result<(), SdkHostError> { + local_store::delete(key).map_err(convert_err) + } + fn list_keys(&self, prefix: &str) -> Result, SdkHostError> { + local_store::list_keys(prefix).map_err(convert_err) + } +} + +impl CowApiHost for WitBindgenHost { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + cow_api::submit_order(chain_id, body).map_err(convert_err) + } +} + +impl LoggingHost for WitBindgenHost { + fn log(&self, level: SdkLogLevel, message: &str) { + logging::log(convert_level(level), message); + } +} + +fn convert_err(e: HostError) -> SdkHostError { + SdkHostError { + domain: e.domain, + kind: match e.kind { + HostErrorKind::Unsupported => SdkHostErrorKind::Unsupported, + HostErrorKind::Unavailable => SdkHostErrorKind::Unavailable, + HostErrorKind::Denied => SdkHostErrorKind::Denied, + HostErrorKind::RateLimited => SdkHostErrorKind::RateLimited, + HostErrorKind::Timeout => SdkHostErrorKind::Timeout, + HostErrorKind::InvalidInput => SdkHostErrorKind::InvalidInput, + HostErrorKind::Internal => SdkHostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn sdk_err_into_wit(e: SdkHostError) -> HostError { + HostError { + domain: e.domain, + kind: match e.kind { + SdkHostErrorKind::Unsupported => HostErrorKind::Unsupported, + SdkHostErrorKind::Unavailable => HostErrorKind::Unavailable, + SdkHostErrorKind::Denied => HostErrorKind::Denied, + SdkHostErrorKind::RateLimited => HostErrorKind::RateLimited, + SdkHostErrorKind::Timeout => HostErrorKind::Timeout, + SdkHostErrorKind::InvalidInput => HostErrorKind::InvalidInput, + SdkHostErrorKind::Internal => HostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn convert_level(l: SdkLogLevel) -> logging::Level { + match l { + SdkLogLevel::Trace => logging::Level::Trace, + SdkLogLevel::Debug => logging::Level::Debug, + SdkLogLevel::Info => logging::Level::Info, + SdkLogLevel::Warn => logging::Level::Warn, + SdkLogLevel::Error => logging::Level::Error, + } +} + +struct StopLoss; + +impl Guest for StopLoss { + fn init(config: Vec<(String, String)>) -> Result<(), HostError> { + let cfg = strategy::parse_config(&config).map_err(sdk_err_into_wit)?; + logging::log( + logging::Level::Info, + &format!( + "stop-loss init: owner={:#x} trigger={} sell={:#x} buy={:#x}", + cfg.owner, cfg.trigger_price_scaled, cfg.sell_token, cfg.buy_token, + ), + ); + let _ = SETTINGS.set(cfg); + Ok(()) + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + let Some(cfg) = SETTINGS.get() else { + return Ok(()); + }; + if let types::Event::Block(block) = event { + strategy::on_block(&WitBindgenHost, block.chain_id, cfg).map_err(sdk_err_into_wit)?; + } + Ok(()) + } +} + +export!(StopLoss); diff --git a/modules/examples/stop-loss/src/strategy.rs b/modules/examples/stop-loss/src/strategy.rs new file mode 100644 index 0000000..3ba7944 --- /dev/null +++ b/modules/examples/stop-loss/src/strategy.rs @@ -0,0 +1,595 @@ +//! Pure stop-loss strategy logic. Reads an oracle, optionally submits +//! a pre-signed CoW order, dedups via local-store. Every interaction +//! with the world flows through the [`Host`] trait so the tests can +//! drive it against `shepherd_sdk_test::MockHost`. + +use alloy_primitives::I256; +use alloy_sol_types::{SolCall, sol}; +use shepherd_sdk::chain::{eth_call_params, parse_eth_call_result}; +use shepherd_sdk::cow::{RetryAction, classify_api_error, gpv2_to_order_data}; +use shepherd_sdk::host::{Host, HostError, HostErrorKind, LogLevel}; +use shepherd_sdk::prelude::{ + Address, BuyTokenDestination, Bytes, Chain, EMPTY_APP_DATA_JSON, GPv2OrderData, OrderCreation, + OrderKind, OrderUid, SellTokenSource, Signature, U256, +}; + +sol! { + interface AggregatorV3 { + function latestRoundData() external view returns ( + uint80 roundId, + int256 answer, + uint256 startedAt, + uint256 updatedAt, + uint80 answeredInRound + ); + } +} + +/// Resolved configuration parsed from `module.toml::[config]`. +#[derive(Clone, Debug)] +pub struct Settings { + /// Chainlink AggregatorV3Interface address. + pub oracle_address: Address, + /// Trigger price scaled to the oracle's native units. + pub trigger_price_scaled: I256, + /// Order owner (= EIP-712 signer / PreSign caller). + pub owner: Address, + /// Sell side of the order. + pub sell_token: Address, + /// Buy side of the order. + pub buy_token: Address, + /// Sell amount in atomic units of `sell_token`. + pub sell_amount: U256, + /// Buy amount in atomic units of `buy_token`. + pub buy_amount: U256, + /// Order expiry (Unix seconds). + pub valid_to: u32, +} + +/// React to a new block. +/// +/// Returns `Ok(())` on success and on recoverable upstream failures +/// (oracle RPC error, decode failure). Only host-store errors bubble +/// up via `?` so the supervisor can surface persistence issues - all +/// other faults log and let the next block re-poll. +pub fn on_block(host: &H, chain_id: u64, settings: &Settings) -> Result<(), HostError> { + let price = match read_oracle(host, chain_id, settings.oracle_address) { + Some(p) => p, + None => return Ok(()), // logged inside read_oracle + }; + + if price > settings.trigger_price_scaled { + host.log( + LogLevel::Info, + &format!( + "stop-loss idle: price={price} > trigger={}", + settings.trigger_price_scaled, + ), + ); + return Ok(()); + } + + // Compute UID up-front so we can dedup before paying for the + // serialise + submit round trip. + let (creation, uid) = match build_creation(chain_id, settings) { + Ok(x) => x, + Err(e) => { + host.log(LogLevel::Warn, &format!("stop-loss skipped (build): {e}")); + return Ok(()); + } + }; + let uid_hex = format!("{uid}"); + let dedup_key = format!("submitted:{uid_hex}"); + if host.get(&dedup_key)?.is_some() { + host.log( + LogLevel::Info, + &format!("stop-loss: {uid_hex} already submitted, idle"), + ); + return Ok(()); + } + let dropped_key = format!("dropped:{uid_hex}"); + if host.get(&dropped_key)?.is_some() { + host.log( + LogLevel::Info, + &format!("stop-loss: {uid_hex} previously dropped, idle"), + ); + return Ok(()); + } + + let body = match serde_json::to_vec(&creation) { + Ok(b) => b, + Err(e) => { + host.log( + LogLevel::Error, + &format!("OrderCreation JSON encode failed: {e}"), + ); + return Ok(()); + } + }; + match host.submit_order(chain_id, &body) { + Ok(server_uid) => { + if server_uid != uid_hex { + host.log( + LogLevel::Warn, + &format!("stop-loss uid drift: local={uid_hex} server={server_uid}"), + ); + } + host.set(&format!("submitted:{server_uid}"), b"")?; + host.log( + LogLevel::Warn, + &format!( + "stop-loss TRIGGERED: price={price} <= trigger={}, uid={server_uid}", + settings.trigger_price_scaled, + ), + ); + } + Err(err) => match classify_api_error(err.data.as_deref()) { + RetryAction::TryNextBlock | RetryAction::Backoff { .. } => { + host.log( + LogLevel::Warn, + &format!( + "stop-loss retry on next block ({}): {}", + err.code, err.message + ), + ); + } + RetryAction::Drop => { + host.set(&dropped_key, b"")?; + host.log( + LogLevel::Warn, + &format!( + "stop-loss dropped {uid_hex} ({}): {}", + err.code, err.message + ), + ); + } + }, + } + Ok(()) +} + +/// Fetch the oracle's latest answer, returning `None` (and logging a +/// Warn) on any host or decode failure. The caller treats `None` as +/// "skip this block". +fn read_oracle(host: &H, chain_id: u64, oracle: Address) -> Option { + let call_data = AggregatorV3::latestRoundDataCall {}.abi_encode(); + let params = eth_call_params(&oracle, &call_data); + let result_json = match host.request(chain_id, "eth_call", ¶ms) { + Ok(s) => s, + Err(err) => { + host.log( + LogLevel::Warn, + &format!( + "stop-loss oracle eth_call failed ({}): {}", + err.code, err.message + ), + ); + return None; + } + }; + let bytes = parse_eth_call_result(&result_json)?; + AggregatorV3::latestRoundDataCall::abi_decode_returns(&bytes) + .ok() + .map(|r| r.answer) +} + +/// Assemble the `OrderCreation` body + canonical UID from settings. +/// Uses `Signature::PreSign` so the module ships zero ECDSA - the +/// owner is expected to have called `GPv2Signing.setPreSignature` +/// on-chain ahead of the trigger. +fn build_creation( + chain_id: u64, + settings: &Settings, +) -> Result<(OrderCreation, OrderUid), HostError> { + let chain = Chain::try_from(chain_id).map_err(|_| HostError { + domain: "stop-loss".into(), + kind: HostErrorKind::Unsupported, + code: 0, + message: format!("chain {chain_id} not supported by cowprotocol"), + data: None, + })?; + let domain = chain.settlement_domain(); + let gpv2 = GPv2OrderData { + sellToken: settings.sell_token, + buyToken: settings.buy_token, + receiver: settings.owner, + sellAmount: settings.sell_amount, + buyAmount: settings.buy_amount, + validTo: settings.valid_to, + appData: cowprotocol::EMPTY_APP_DATA_HASH, + feeAmount: U256::ZERO, + kind: OrderKind::SELL, + partiallyFillable: false, + sellTokenBalance: SellTokenSource::ERC20, + buyTokenBalance: BuyTokenDestination::ERC20, + }; + let order_data = gpv2_to_order_data(&gpv2).ok_or_else(|| HostError { + domain: "stop-loss".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: "GPv2OrderData carried an unknown enum marker".into(), + data: None, + })?; + let uid = order_data.uid(&domain, settings.owner); + let creation = OrderCreation::from_signed_order_data( + &order_data, + Signature::PreSign, + settings.owner, + EMPTY_APP_DATA_JSON.to_string(), + None, + ) + .map_err(|e| HostError { + domain: "stop-loss".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("cowprotocol rejected the body: {e}"), + data: None, + })?; + // Silence the unused `Bytes` import on builds where `Signature:: + // PreSign` is the only signature variant we construct. + let _: Option = None; + Ok((creation, uid)) +} + +/// Parse `module.toml::[config]` into a typed [`Settings`]. +pub fn parse_config(entries: &[(String, String)]) -> Result { + let oracle_address = require(entries, "oracle_address")? + .parse::
() + .map_err(|e| invalid(format!("oracle_address: {e}")))?; + let decimals = require(entries, "decimals")? + .parse::() + .map_err(|e| invalid(format!("decimals: {e}")))?; + if decimals > 38 { + return Err(invalid(format!( + "decimals={decimals} exceeds the I256 power-of-ten budget" + ))); + } + let trigger_price_scaled = scale_signed(require(entries, "trigger_price")?, decimals)?; + let owner = require(entries, "owner")? + .parse::
() + .map_err(|e| invalid(format!("owner: {e}")))?; + let sell_token = require(entries, "sell_token")? + .parse::
() + .map_err(|e| invalid(format!("sell_token: {e}")))?; + let buy_token = require(entries, "buy_token")? + .parse::
() + .map_err(|e| invalid(format!("buy_token: {e}")))?; + let sell_amount = require(entries, "sell_amount_wei")? + .parse::() + .map_err(|e| invalid(format!("sell_amount_wei: {e}")))?; + let buy_amount = require(entries, "buy_amount_wei")? + .parse::() + .map_err(|e| invalid(format!("buy_amount_wei: {e}")))?; + let valid_to = require(entries, "valid_to_seconds")? + .parse::() + .map_err(|e| invalid(format!("valid_to_seconds: {e}")))?; + Ok(Settings { + oracle_address, + trigger_price_scaled, + owner, + sell_token, + buy_token, + sell_amount, + buy_amount, + valid_to, + }) +} + +fn require<'a>(entries: &'a [(String, String)], key: &str) -> Result<&'a str, HostError> { + entries + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v.as_str()) + .ok_or_else(|| invalid(format!("missing key {key:?}"))) +} + +fn invalid(message: impl Into) -> HostError { + HostError { + domain: "stop-loss".into(), + kind: HostErrorKind::InvalidInput, + code: 0, + message: format!("stop-loss: invalid [config]: {}", message.into()), + data: None, + } +} + +fn scale_signed(threshold_decimal: &str, decimals: u32) -> Result { + let (sign, body) = if let Some(rest) = threshold_decimal.strip_prefix('-') { + (-1i32, rest) + } else { + (1, threshold_decimal) + }; + let (whole, frac) = match body.split_once('.') { + Some((w, f)) => (w, f), + None => (body, ""), + }; + if whole.is_empty() && frac.is_empty() { + return Err(invalid("trigger_price: empty")); + } + if !whole.chars().all(|c| c.is_ascii_digit()) || !frac.chars().all(|c| c.is_ascii_digit()) { + return Err(invalid(format!( + "trigger_price: non-digit character in {threshold_decimal:?}" + ))); + } + let frac_len = frac.len() as u32; + let composed: String = if frac_len <= decimals { + let mut s = String::with_capacity(whole.len() + decimals as usize); + s.push_str(whole); + s.push_str(frac); + for _ in 0..(decimals - frac_len) { + s.push('0'); + } + s + } else { + let mut s = String::with_capacity(whole.len() + decimals as usize); + s.push_str(whole); + s.push_str(&frac[..decimals as usize]); + s + }; + let raw = if composed.is_empty() { "0" } else { &composed }; + let unsigned: U256 = raw + .parse() + .map_err(|e| invalid(format!("trigger_price parse: {e}")))?; + let signed = + I256::try_from(unsigned).map_err(|e| invalid(format!("trigger_price range: {e}")))?; + Ok(if sign < 0 { -signed } else { signed }) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::hex; + use shepherd_sdk::host::HostErrorKind as Kind; + use shepherd_sdk_test::MockHost; + + const SEPOLIA: u64 = 11_155_111; + + fn settings_below(trigger_scaled: i128) -> Settings { + Settings { + oracle_address: "0x694AA1769357215DE4FAC081bf1f309aDC325306" + .parse() + .unwrap(), + trigger_price_scaled: I256::try_from(trigger_scaled).unwrap(), + owner: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" + .parse() + .unwrap(), + sell_token: "0x6810e776880C02933D47DB1b9fc05908e5386b96" + .parse() + .unwrap(), + buy_token: "0xfff9976782d46cc05630d1f6ebab18b2324d6b14" + .parse() + .unwrap(), + sell_amount: U256::from(1_000_000_000_000_000_000_u128), + buy_amount: U256::from(300_000_000_000_000_000_u128), + valid_to: u32::MAX, + } + } + + fn oracle_response_json(answer_scaled: i128) -> String { + use alloy_primitives::aliases::U80; + let returns = AggregatorV3::latestRoundDataReturn { + roundId: U80::ZERO, + answer: I256::try_from(answer_scaled).unwrap(), + startedAt: U256::ZERO, + updatedAt: U256::ZERO, + answeredInRound: U80::ZERO, + }; + let encoded = AggregatorV3::latestRoundDataCall::abi_encode_returns(&returns); + let hex_body = hex::encode_prefixed(encoded); + format!("\"{hex_body}\"") + } + + fn program_oracle(host: &MockHost, oracle: Address, response: Result) { + let call_data = AggregatorV3::latestRoundDataCall {}.abi_encode(); + let params = eth_call_params(&oracle, &call_data); + host.chain.respond_to("eth_call", ¶ms, response); + } + + fn programmed_uid(settings: &Settings) -> String { + let (_creation, uid) = build_creation(SEPOLIA, settings).unwrap(); + format!("{uid}") + } + + #[test] + fn idle_when_price_above_trigger() { + let host = MockHost::new(); + let s = settings_below(/*trigger*/ 250_000_000_000); + program_oracle( + &host, + s.oracle_address, + Ok(oracle_response_json(300_000_000_000)), + ); + + on_block(&host, SEPOLIA, &s).unwrap(); + + assert_eq!(host.cow_api.call_count(), 0); + assert_eq!(host.store.len(), 0); + assert!(host.logging.contains("stop-loss idle")); + } + + #[test] + fn triggers_and_submits_once_then_dedups() { + let host = MockHost::new(); + let s = settings_below(250_000_000_000); + program_oracle( + &host, + s.oracle_address, + Ok(oracle_response_json(200_000_000_000)), + ); + let uid = programmed_uid(&s); + host.cow_api.respond(Ok(uid.clone())); + + // First block: submits. + on_block(&host, SEPOLIA, &s).unwrap(); + assert_eq!(host.cow_api.call_count(), 1); + assert!(host.logging.contains("TRIGGERED")); + assert!( + host.store + .snapshot() + .contains_key(&format!("submitted:{uid}")) + ); + + // Second block at the same price: dedup'd, no new submit. + on_block(&host, SEPOLIA, &s).unwrap(); + assert_eq!(host.cow_api.call_count(), 1); + assert!(host.logging.contains("already submitted")); + } + + #[test] + fn permanent_submit_error_marks_dropped() { + let host = MockHost::new(); + let s = settings_below(250_000_000_000); + program_oracle( + &host, + s.oracle_address, + Ok(oracle_response_json(200_000_000_000)), + ); + + // Orderbook returns InvalidSignature - permanent per + // `OrderPostErrorKind::is_retriable`. + let api_body = serde_json::json!({ + "errorType": "InvalidSignature", + "description": "bad sig", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InvalidSignature".into(), + data: Some(api_body), + })); + + on_block(&host, SEPOLIA, &s).unwrap(); + let uid = programmed_uid(&s); + assert!( + host.store + .snapshot() + .contains_key(&format!("dropped:{uid}")) + ); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("submitted:{uid}")) + ); + assert!(host.logging.contains("dropped")); + + // Second block: dropped marker idles the loop. + on_block(&host, SEPOLIA, &s).unwrap(); + assert_eq!(host.cow_api.call_count(), 1); // no resubmit + assert!(host.logging.contains("previously dropped")); + } + + #[test] + fn transient_submit_error_leaves_state_unchanged() { + let host = MockHost::new(); + let s = settings_below(250_000_000_000); + program_oracle( + &host, + s.oracle_address, + Ok(oracle_response_json(200_000_000_000)), + ); + + let api_body = serde_json::json!({ + "errorType": "InsufficientFee", + "description": "fee too low", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InsufficientFee".into(), + data: Some(api_body), + })); + + on_block(&host, SEPOLIA, &s).unwrap(); + + // No persistence flag - next block will retry. + assert_eq!(host.store.len(), 0); + assert!(host.logging.contains("retry on next block")); + } + + #[test] + fn oracle_rpc_error_is_warn_and_continue() { + let host = MockHost::new(); + let s = settings_below(250_000_000_000); + program_oracle( + &host, + s.oracle_address, + Err(HostError { + domain: "chain".into(), + kind: Kind::Timeout, + code: 504, + message: "upstream timed out".into(), + data: None, + }), + ); + + on_block(&host, SEPOLIA, &s).unwrap(); + + assert_eq!(host.cow_api.call_count(), 0); + assert_eq!(host.store.len(), 0); + assert!(host.logging.contains("oracle eth_call failed")); + } + + #[test] + fn parse_config_round_trips_settings() { + let entries = vec![ + ( + "oracle_address".into(), + "0x694AA1769357215DE4FAC081bf1f309aDC325306".into(), + ), + ("decimals".into(), "8".into()), + ("trigger_price".into(), "2500.00".into()), + ( + "owner".into(), + "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".into(), + ), + ( + "sell_token".into(), + "0x6810e776880C02933D47DB1b9fc05908e5386b96".into(), + ), + ( + "buy_token".into(), + "0xfff9976782d46cc05630d1f6ebab18b2324d6b14".into(), + ), + ("sell_amount_wei".into(), "1000000000000000000".into()), + ("buy_amount_wei".into(), "300000000000000000".into()), + ("valid_to_seconds".into(), "4294967295".into()), + ]; + let s = parse_config(&entries).unwrap(); + assert_eq!(s.valid_to, u32::MAX); + assert_eq!( + s.trigger_price_scaled, + I256::try_from(250_000_000_000_i64).unwrap() + ); + } + + #[test] + fn parse_config_rejects_missing_owner() { + let entries = vec![ + ( + "oracle_address".into(), + "0x694AA1769357215DE4FAC081bf1f309aDC325306".into(), + ), + ("decimals".into(), "8".into()), + ("trigger_price".into(), "1.0".into()), + ( + "sell_token".into(), + "0x6810e776880C02933D47DB1b9fc05908e5386b96".into(), + ), + ( + "buy_token".into(), + "0xfff9976782d46cc05630d1f6ebab18b2324d6b14".into(), + ), + ("sell_amount_wei".into(), "1".into()), + ("buy_amount_wei".into(), "1".into()), + ("valid_to_seconds".into(), "1".into()), + ]; + let err = parse_config(&entries).unwrap_err(); + assert!(matches!(err.kind, Kind::InvalidInput)); + assert!(err.message.contains("owner")); + } +} diff --git a/modules/twap-monitor/Cargo.toml b/modules/twap-monitor/Cargo.toml new file mode 100644 index 0000000..205de15 --- /dev/null +++ b/modules/twap-monitor/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "twap-monitor" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true + +[lib] +crate-type = ["cdylib"] + +[dependencies] +shepherd-sdk = { path = "../../crates/shepherd-sdk" } +cowprotocol = { version = "1.0.0-alpha.3", default-features = false } +alloy-primitives = { version = "1.5", default-features = false, features = ["std"] } +alloy-sol-types = { version = "1.5", default-features = false, features = ["std"] } +serde_json = { version = "1", default-features = false, features = ["alloc"] } +thiserror = "2" +wit-bindgen = { version = "0.57", default-features = false, features = ["macros", "realloc"] } + +[dev-dependencies] +shepherd-sdk-test = { path = "../../crates/shepherd-sdk-test" } diff --git a/modules/twap-monitor/module.toml b/modules/twap-monitor/module.toml new file mode 100644 index 0000000..fb3f361 --- /dev/null +++ b/modules/twap-monitor/module.toml @@ -0,0 +1,41 @@ +# twap-monitor: poll registered ComposableCoW conditional orders and +# submit ready ones via the CoW Protocol orderbook. + +[module] +name = "twap-monitor" +version = "0.1.0" +# Placeholder content hash. 0.2 parses but does not verify this; 0.3 will +# compare it against the sha256 of the loaded component bytes. +component = "sha256:0000000000000000000000000000000000000000000000000000000000000000" + +[capabilities] +# Host interfaces the module imports and exercises: +# - logging -> structured runtime logs +# - local-store -> watch: / next_block: / next_epoch: / submitted: / +# backoff: / dropped: persistence +# - chain -> eth_call into ComposableCoW.getTradeableOrderWithSignature +# - cow-api -> POST /api/v1/orders submission path +required = ["logging", "local-store", "chain", "cow-api"] +optional = [] + +[capabilities.http] +# All outbound HTTP goes through `cow-api` (which routes through the +# host's pinned orderbook URL); no direct `http` calls. +allow = [] + +# --- subscriptions ------------------------------------------------------ + +# ComposableCoW.ConditionalOrderCreated emissions on Sepolia. topic-0 = +# keccak256("ConditionalOrderCreated(address,(address,bytes32,bytes))"). +# Both `address` and `event_signature` are pinned so the supervisor +# does not deliver unrelated logs to the module. +[[subscription]] +kind = "log" +chain_id = 11155111 +address = "0xfdaFc9d1902f4e0b84f65F49f244b32b31013b74" +event_signature = "0x2cceac5555b0ca45a3744ced542f54b56ad2eb45e521962372eef212a2cbf361" + +# New-block ticks drive the TWAP poll loop (`getTradeableOrderWithSignature`). +[[subscription]] +kind = "block" +chain_id = 11155111 diff --git a/modules/twap-monitor/src/lib.rs b/modules/twap-monitor/src/lib.rs new file mode 100644 index 0000000..4e3ef8e --- /dev/null +++ b/modules/twap-monitor/src/lib.rs @@ -0,0 +1,159 @@ +//! # twap-monitor (Shepherd module) +//! +//! Indexes `ComposableCoW.ConditionalOrderCreated` logs and polls each +//! watched conditional order on every block, submitting tranches to +//! the CoW orderbook as they go live. +//! +//! ## Module layout (BLEU-854) +//! +//! - `strategy.rs` holds the pure logic and unit tests against +//! `shepherd_sdk::host::Host`. It does not know `wit-bindgen` +//! exists. +//! - `lib.rs` (this file) is the per-cdylib glue: wit-bindgen import +//! shims, the `WitBindgenHost` adapter that bridges the generated +//! free functions to the SDK traits, and the `Guest` impl that +//! delegates each event variant to `strategy`. +//! +//! Same recipe as `modules/examples/price-alert` (BLEU-851) and +//! `modules/examples/stop-loss` (BLEU-852). + +// wit_bindgen::generate! expands to host-import shims whose arity +// matches the WIT signatures, which can exceed clippy's +// too-many-arguments threshold. +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![allow(clippy::too_many_arguments)] + +wit_bindgen::generate!({ + path: ["../../wit/nexum-host", "../../wit/shepherd-cow"], + world: "shepherd:cow/shepherd", + generate_all, +}); + +mod strategy; + +use shepherd_sdk::host::{ + ChainHost, CowApiHost, HostError as SdkHostError, HostErrorKind as SdkHostErrorKind, + LocalStoreHost, LogLevel as SdkLogLevel, LoggingHost, +}; + +use nexum::host::types::HostErrorKind; +use nexum::host::{chain, local_store, logging, types}; +use shepherd::cow::cow_api; + +struct WitBindgenHost; + +impl ChainHost for WitBindgenHost { + fn request(&self, chain_id: u64, method: &str, params: &str) -> Result { + chain::request(chain_id, method, params).map_err(convert_err) + } +} + +impl LocalStoreHost for WitBindgenHost { + fn get(&self, key: &str) -> Result>, SdkHostError> { + local_store::get(key).map_err(convert_err) + } + fn set(&self, key: &str, value: &[u8]) -> Result<(), SdkHostError> { + local_store::set(key, value).map_err(convert_err) + } + fn delete(&self, key: &str) -> Result<(), SdkHostError> { + local_store::delete(key).map_err(convert_err) + } + fn list_keys(&self, prefix: &str) -> Result, SdkHostError> { + local_store::list_keys(prefix).map_err(convert_err) + } +} + +impl CowApiHost for WitBindgenHost { + fn submit_order(&self, chain_id: u64, body: &[u8]) -> Result { + cow_api::submit_order(chain_id, body).map_err(convert_err) + } +} + +impl LoggingHost for WitBindgenHost { + fn log(&self, level: SdkLogLevel, message: &str) { + logging::log(convert_level(level), message); + } +} + +fn convert_err(e: HostError) -> SdkHostError { + SdkHostError { + domain: e.domain, + kind: match e.kind { + HostErrorKind::Unsupported => SdkHostErrorKind::Unsupported, + HostErrorKind::Unavailable => SdkHostErrorKind::Unavailable, + HostErrorKind::Denied => SdkHostErrorKind::Denied, + HostErrorKind::RateLimited => SdkHostErrorKind::RateLimited, + HostErrorKind::Timeout => SdkHostErrorKind::Timeout, + HostErrorKind::InvalidInput => SdkHostErrorKind::InvalidInput, + HostErrorKind::Internal => SdkHostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn sdk_err_into_wit(e: SdkHostError) -> HostError { + HostError { + domain: e.domain, + kind: match e.kind { + SdkHostErrorKind::Unsupported => HostErrorKind::Unsupported, + SdkHostErrorKind::Unavailable => HostErrorKind::Unavailable, + SdkHostErrorKind::Denied => HostErrorKind::Denied, + SdkHostErrorKind::RateLimited => HostErrorKind::RateLimited, + SdkHostErrorKind::Timeout => HostErrorKind::Timeout, + SdkHostErrorKind::InvalidInput => HostErrorKind::InvalidInput, + SdkHostErrorKind::Internal => HostErrorKind::Internal, + }, + code: e.code, + message: e.message, + data: e.data, + } +} + +fn convert_level(l: SdkLogLevel) -> logging::Level { + match l { + SdkLogLevel::Trace => logging::Level::Trace, + SdkLogLevel::Debug => logging::Level::Debug, + SdkLogLevel::Info => logging::Level::Info, + SdkLogLevel::Warn => logging::Level::Warn, + SdkLogLevel::Error => logging::Level::Error, + } +} + +struct TwapMonitor; + +impl Guest for TwapMonitor { + fn init(_config: Vec<(String, String)>) -> Result<(), HostError> { + logging::log(logging::Level::Info, "twap-monitor init"); + Ok(()) + } + + fn on_event(event: types::Event) -> Result<(), HostError> { + match event { + types::Event::Logs(logs) => { + let views: Vec> = logs + .iter() + .map(|log| strategy::LogView { + topics: &log.topics, + data: &log.data, + }) + .collect(); + strategy::on_logs(&WitBindgenHost, &views).map_err(sdk_err_into_wit)?; + } + types::Event::Block(block) => { + let info = strategy::BlockInfo { + chain_id: block.chain_id, + number: block.number, + timestamp: block.timestamp, + }; + strategy::on_block(&WitBindgenHost, info).map_err(sdk_err_into_wit)?; + } + // Tick / Message are not used by this module. + _ => {} + } + Ok(()) + } +} + +export!(TwapMonitor); diff --git a/modules/twap-monitor/src/strategy.rs b/modules/twap-monitor/src/strategy.rs new file mode 100644 index 0000000..26e6fa4 --- /dev/null +++ b/modules/twap-monitor/src/strategy.rs @@ -0,0 +1,956 @@ +//! Pure strategy logic for the twap-monitor module. +//! +//! Every interaction with the world flows through the +//! `shepherd_sdk::host::Host` trait seam - no direct calls to wit- +//! bindgen-generated free functions live here. The `lib.rs` glue +//! wraps a `WitBindgenHost` adapter around the per-cdylib wit-bindgen +//! imports and hands it to [`on_logs`] / [`on_block`]; tests under +//! `#[cfg(test)]` hand the same functions a +//! `shepherd_sdk_test::MockHost`. + +use alloy_primitives::{Address, B256, Bytes, keccak256}; +use alloy_sol_types::{SolCall, SolEvent, SolValue}; +use cowprotocol::{ + COMPOSABLE_COW, ComposableCoW::ConditionalOrderCreated, ConditionalOrderParams, + EMPTY_APP_DATA_JSON, GPv2OrderData, OrderCreation, Signature, +}; +use shepherd_sdk::chain::{eth_call_params, parse_eth_call_result}; +use shepherd_sdk::cow::{PollOutcome, RetryAction, classify_api_error, gpv2_to_order_data}; +use shepherd_sdk::host::{Host, HostError, LogLevel}; + +/// Topics + data slice the indexer path consumes from a wit-bindgen +/// `log`. Carrying borrowed slices keeps `strategy.rs` independent +/// from the wit types generated per-cdylib. +pub struct LogView<'a> { + pub topics: &'a [Vec], + pub data: &'a [u8], +} + +/// Block fields the poll path reads on every dispatch. +pub struct BlockInfo { + pub chain_id: u64, + pub number: u64, + pub timestamp: u64, +} + +mod abi { + use alloy_sol_types::sol; + + sol! { + /// Wire-format mirror of `cowprotocol::ConditionalOrderParams`. sol! + /// cannot reference Rust types declared in another sol! block, but + /// the ABI is identical (same field types in the same order) so + /// the generated call selector matches the real contract. + struct Params { + address handler; + bytes32 salt; + bytes staticInput; + } + + /// Selector source for `eth_call`. The successful return path + /// decodes into the canonical `cowprotocol::GPv2OrderData` + /// instead of duplicating the 12-field struct here. + function getTradeableOrderWithSignature( + address owner, + Params params, + bytes offchainInput, + bytes32[] proof + ) external view; + } +} + +/// Indexer entry: decode every `ComposableCoW.ConditionalOrderCreated` +/// log in a dispatch batch and persist its watch. +pub fn on_logs(host: &H, logs: &[LogView<'_>]) -> Result<(), HostError> { + for log in logs { + if let Some((owner, params)) = decode_conditional_order_created(log.topics, log.data) { + persist_watch(host, owner, ¶ms)?; + } + } + Ok(()) +} + +/// Poll entry: scan every persisted watch and dispatch ready tranches. +pub fn on_block(host: &H, block: BlockInfo) -> Result<(), HostError> { + poll_all_watches(host, &block) +} + +// ---- BLEU-826: indexing path ---- + +fn decode_conditional_order_created( + topics: &[Vec], + data: &[u8], +) -> Option<(Address, ConditionalOrderParams)> { + let topic0 = topics.first()?; + if topic0.len() != 32 || B256::from_slice(topic0) != ConditionalOrderCreated::SIGNATURE_HASH { + return None; + } + let words: Vec = topics + .iter() + .filter(|t| t.len() == 32) + .map(|t| B256::from_slice(t)) + .collect(); + let decoded = ConditionalOrderCreated::decode_raw_log(words, data).ok()?; + Some((decoded.owner, decoded.params)) +} + +/// `set` overwrites in place, so re-indexing the same log (re-org +/// replay, overlapping subscription windows) produces no observable +/// side effect. +fn persist_watch( + host: &H, + owner: Address, + params: &ConditionalOrderParams, +) -> Result<(), HostError> { + let encoded = params.abi_encode(); + let params_hash = keccak256(&encoded); + let key = watch_key(&owner, ¶ms_hash); + host.set(&key, &encoded)?; + host.log(LogLevel::Info, &format!("indexed {key}")); + Ok(()) +} + +// ---- BLEU-827: poll path ---- + +fn poll_all_watches(host: &H, block: &BlockInfo) -> Result<(), HostError> { + let now_epoch_s = block.timestamp / 1000; + let keys = host.list_keys("watch:")?; + for key in keys { + let Some((owner_hex, hash_hex)) = parse_watch_key(&key) else { + continue; + }; + if !is_ready(host, owner_hex, hash_hex, block.number, now_epoch_s)? { + continue; + } + let Some(value) = host.get(&key)? else { + continue; + }; + let Ok(params) = ConditionalOrderParams::abi_decode(&value) else { + host.log( + LogLevel::Warn, + &format!("watch {key} carried unparseable params; skipping"), + ); + continue; + }; + let Ok(owner) = owner_hex.parse::
() else { + continue; + }; + let outcome = poll_one(host, block.chain_id, &owner, ¶ms); + host.log( + LogLevel::Info, + &format!("poll {key} -> {}", outcome_label(&outcome)), + ); + match outcome { + PollOutcome::Ready { order, signature } => { + submit_ready( + host, + block.chain_id, + owner, + &order, + signature, + &key, + now_epoch_s, + )?; + } + non_ready => { + apply_watch_update(host, outcome_to_update(&non_ready), &key)?; + } + } + } + Ok(()) +} + +fn poll_one( + host: &H, + chain_id: u64, + owner: &Address, + params: &ConditionalOrderParams, +) -> PollOutcome { + let call = abi::getTradeableOrderWithSignatureCall { + owner: *owner, + params: abi::Params { + handler: params.handler, + salt: params.salt, + staticInput: params.staticInput.clone(), + }, + offchainInput: Bytes::new(), + proof: Vec::new(), + }; + let params_json = eth_call_params(&COMPOSABLE_COW, &call.abi_encode()); + match host.request(chain_id, "eth_call", ¶ms_json) { + Ok(result_json) => parse_eth_call_result(&result_json) + .and_then(|bytes| decode_return(&bytes)) + .unwrap_or(PollOutcome::TryNextBlock), + Err(err) => { + // The host's chain backend currently stuffs the formatted + // RPC error into `message` with `data: None`; once it + // forwards the structured `error.data` from alloy's + // `RpcError::ErrorResp`, those bytes feed into + // `shepherd_sdk::chain::decode_revert_hex` here. Until then + // the `data` branch is unreachable on real traffic and the + // safe default is to retry on the next block. + if let Some(data) = err.data.as_deref() + && let Some(outcome) = shepherd_sdk::chain::decode_revert_hex(data) + { + return outcome; + } + host.log( + LogLevel::Warn, + &format!( + "eth_call failed ({}); defaulting to TryNextBlock", + err.message + ), + ); + PollOutcome::TryNextBlock + } + } +} + +/// Decode a successful `getTradeableOrderWithSignature` return into +/// `Ready { order, signature }`. The wire format is `abi.encode(order, +/// signature)` - the canonical Solidity return tuple - so the two-tuple +/// parameter decode lines up. +fn decode_return(data: &[u8]) -> Option { + let (order, signature) = <(GPv2OrderData, Bytes)>::abi_decode_params(data).ok()?; + Some(PollOutcome::Ready { + order: Box::new(order), + signature, + }) +} + +fn outcome_label(o: &PollOutcome) -> &'static str { + match o { + PollOutcome::Ready { .. } => "Ready", + PollOutcome::TryAtEpoch(_) => "TryAtEpoch", + PollOutcome::TryOnBlock(_) => "TryOnBlock", + PollOutcome::TryNextBlock => "TryNextBlock", + PollOutcome::DontTryAgain => "DontTryAgain", + } +} + +// ---- key conventions shared with BLEU-830 ---- + +fn watch_key(owner: &Address, params_hash: &B256) -> String { + format!("watch:{owner:#x}:{params_hash:#x}") +} + +fn parse_watch_key(key: &str) -> Option<(&str, &str)> { + let rest = key.strip_prefix("watch:")?; + let (owner, hash) = rest.split_once(':')?; + Some((owner, hash)) +} + +fn is_ready( + host: &H, + owner_hex: &str, + hash_hex: &str, + block_number: u64, + epoch_s: u64, +) -> Result { + if let Some(next) = read_u64(host, &format!("next_block:{owner_hex}:{hash_hex}"))? + && block_number < next + { + return Ok(false); + } + if let Some(next) = read_u64(host, &format!("next_epoch:{owner_hex}:{hash_hex}"))? + && epoch_s < next + { + return Ok(false); + } + Ok(true) +} + +fn read_u64(host: &H, key: &str) -> Result, HostError> { + let bytes = host.get(key)?; + Ok(bytes + .and_then(|b| <[u8; 8]>::try_from(b.as_slice()).ok()) + .map(u64::from_le_bytes)) +} + +// ---- BLEU-828: submission path ---- + +/// `cowprotocol`-side rejection envelope for an `OrderCreation` we +/// failed to assemble. Surfaces in a Warn log; the watch is left in +/// place so the next poll can either re-construct or transition on +/// its own. +#[derive(Debug, thiserror::Error)] +enum BuildError { + /// `GPv2OrderData` carried a marker (`kind`, balance enum) we don't + /// know how to map. + #[error("GPv2OrderData carried an unknown enum marker")] + UnknownMarker, + /// `cowprotocol` rejected the body - typically + /// `keccak256(app_data) != order.app_data` or `from == + /// Address::ZERO`. + #[error(transparent)] + Cowprotocol(#[from] cowprotocol::Error), +} + +/// Assemble the `OrderCreation` body the orderbook expects from a +/// freshly-polled TWAP tranche. `app_data` is left at +/// `EMPTY_APP_DATA_JSON` - conditional orders that pin a non-empty +/// IPFS document get rejected here and the watch is left in place. +fn build_order_creation( + order: &GPv2OrderData, + signature: Bytes, + from: Address, +) -> Result { + let order_data = gpv2_to_order_data(order).ok_or(BuildError::UnknownMarker)?; + let signature = Signature::Eip1271(signature.to_vec()); + let creation = OrderCreation::from_signed_order_data( + &order_data, + signature, + from, + EMPTY_APP_DATA_JSON.to_string(), + None, + )?; + Ok(creation) +} + +fn submit_ready( + host: &H, + chain_id: u64, + owner: Address, + order: &GPv2OrderData, + signature: Bytes, + watch_key: &str, + now_epoch_s: u64, +) -> Result<(), HostError> { + let creation = match build_order_creation(order, signature, owner) { + Ok(c) => c, + Err(e) => { + host.log( + LogLevel::Warn, + &format!("twap submit skipped for {owner:#x}: {e}"), + ); + return Ok(()); + } + }; + let body = match serde_json::to_vec(&creation) { + Ok(b) => b, + Err(e) => { + host.log( + LogLevel::Error, + &format!("OrderCreation JSON encode failed: {e}"), + ); + return Ok(()); + } + }; + match host.submit_order(chain_id, &body) { + Ok(uid) => { + let key = format!("submitted:{uid}"); + // Empty marker - presence of the key is the receipt. + host.set(&key, b"")?; + host.log(LogLevel::Info, &format!("submitted {key}")); + } + Err(err) => { + apply_submit_retry(host, &err, watch_key, now_epoch_s)?; + } + } + Ok(()) +} + +// ---- BLEU-829: OrderPostError -> retry action ---- + +fn apply_submit_retry( + host: &H, + err: &HostError, + watch_key: &str, + now_epoch_s: u64, +) -> Result<(), HostError> { + let action = classify_api_error(err.data.as_deref()); + match action { + RetryAction::TryNextBlock => { + host.log( + LogLevel::Warn, + &format!("submit retry-next-block ({}): {}", err.code, err.message), + ); + } + RetryAction::Backoff { seconds } => { + let until = now_epoch_s.saturating_add(seconds); + if let Some((owner_hex, hash_hex)) = parse_watch_key(watch_key) { + host.set( + &format!("next_epoch:{owner_hex}:{hash_hex}"), + &until.to_le_bytes(), + )?; + } + host.log( + LogLevel::Warn, + &format!( + "submit backoff {seconds}s -> next_epoch={until} ({}): {}", + err.code, err.message + ), + ); + } + RetryAction::Drop => { + host.delete(watch_key)?; + if let Some((owner_hex, hash_hex)) = parse_watch_key(watch_key) { + let _ = host.delete(&format!("next_block:{owner_hex}:{hash_hex}")); + let _ = host.delete(&format!("next_epoch:{owner_hex}:{hash_hex}")); + } + host.log( + LogLevel::Warn, + &format!("submit dropped watch ({}): {}", err.code, err.message), + ); + } + } + Ok(()) +} + +// ---- BLEU-830: PollOutcome lifecycle dispatch ---- + +/// What `apply_watch_update` should do for a given outcome. Kept as a +/// data type (rather than running the effects directly) so the +/// decision is host-free testable. +#[derive(Debug, Eq, PartialEq)] +enum WatchUpdate { + /// Leave the store untouched. Next block re-polls the watch. + NoOp, + /// Write `next_block:` so subsequent polls skip until the given + /// block number is reached. + SetNextBlock(u64), + /// Write `next_epoch:` so subsequent polls skip until the given + /// Unix-seconds timestamp is reached. + SetNextEpoch(u64), + /// Delete the watch and any stale gate keys - TWAP completed, + /// cancelled, or otherwise irrecoverable. + DropWatch, +} + +/// Pure mapping from a non-Ready `PollOutcome` to the lifecycle effect +/// the BLEU-830 contract specifies. `Ready` is handled by the submit +/// path (BLEU-828) and is rejected here so a caller cannot +/// accidentally erase the watch when an order was actually produced. +fn outcome_to_update(outcome: &PollOutcome) -> WatchUpdate { + match outcome { + PollOutcome::Ready { .. } => WatchUpdate::NoOp, + PollOutcome::TryNextBlock => WatchUpdate::NoOp, + PollOutcome::TryOnBlock(n) => WatchUpdate::SetNextBlock(*n), + PollOutcome::TryAtEpoch(t) => WatchUpdate::SetNextEpoch(*t), + PollOutcome::DontTryAgain => WatchUpdate::DropWatch, + } +} + +fn apply_watch_update( + host: &H, + update: WatchUpdate, + watch_key: &str, +) -> Result<(), HostError> { + match update { + WatchUpdate::NoOp => Ok(()), + WatchUpdate::SetNextBlock(n) => { + if let Some((owner_hex, hash_hex)) = parse_watch_key(watch_key) { + host.set( + &format!("next_block:{owner_hex}:{hash_hex}"), + &n.to_le_bytes(), + )?; + } + Ok(()) + } + WatchUpdate::SetNextEpoch(t) => { + if let Some((owner_hex, hash_hex)) = parse_watch_key(watch_key) { + host.set( + &format!("next_epoch:{owner_hex}:{hash_hex}"), + &t.to_le_bytes(), + )?; + } + Ok(()) + } + WatchUpdate::DropWatch => { + host.delete(watch_key)?; + if let Some((owner_hex, hash_hex)) = parse_watch_key(watch_key) { + let _ = host.delete(&format!("next_block:{owner_hex}:{hash_hex}")); + let _ = host.delete(&format!("next_epoch:{owner_hex}:{hash_hex}")); + } + host.log(LogLevel::Info, &format!("dropped watch {watch_key}")); + Ok(()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::{U256, address, b256, hex}; + use cowprotocol::{BuyTokenDestination, OrderKind, SellTokenSource}; + use shepherd_sdk::host::{HostErrorKind as Kind, LocalStoreHost as _}; + use shepherd_sdk_test::MockHost; + + const SEPOLIA: u64 = 11_155_111; + + fn sample_params() -> ConditionalOrderParams { + ConditionalOrderParams { + handler: address!("ffeeddccbbaa00998877665544332211ffeeddcc"), + salt: b256!("0101010101010101010101010101010101010101010101010101010101010101"), + staticInput: hex!("deadbeef").to_vec().into(), + } + } + + fn sample_order() -> GPv2OrderData { + GPv2OrderData { + sellToken: address!("6810e776880C02933D47DB1b9fc05908e5386b96"), + buyToken: address!("DAE5F1590db13E3B40423B5b5c5fbf175515910b"), + receiver: address!("DeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"), + sellAmount: U256::from(1_000_u64), + buyAmount: U256::from(2_000_u64), + validTo: 1_700_000_000, + appData: B256::repeat_byte(0xaa), + feeAmount: U256::ZERO, + kind: B256::repeat_byte(0xbb), + partiallyFillable: false, + sellTokenBalance: B256::repeat_byte(0xcc), + buyTokenBalance: B256::repeat_byte(0xdd), + } + } + + fn submittable_order() -> GPv2OrderData { + GPv2OrderData { + sellToken: address!("6810e776880C02933D47DB1b9fc05908e5386b96"), + buyToken: address!("DAE5F1590db13E3B40423B5b5c5fbf175515910b"), + receiver: Address::ZERO, + sellAmount: U256::from(1_000_000_u64), + buyAmount: U256::from(999_u64), + validTo: 0xffff_ffff, + appData: cowprotocol::EMPTY_APP_DATA_HASH, + feeAmount: U256::ZERO, + kind: OrderKind::SELL, + partiallyFillable: false, + sellTokenBalance: SellTokenSource::ERC20, + buyTokenBalance: BuyTokenDestination::ERC20, + } + } + + // ---- existing pure tests preserved from BLEU-826/827/828/830 ---- + + #[test] + fn decodes_well_formed_log() { + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let params = sample_params(); + let owner_topic = { + let mut t = vec![0u8; 12]; + t.extend_from_slice(owner.as_slice()); + t + }; + let topics = vec![ + ConditionalOrderCreated::SIGNATURE_HASH.to_vec(), + owner_topic, + ]; + let data = params.abi_encode(); + + let (decoded_owner, decoded_params) = + decode_conditional_order_created(&topics, &data).expect("decode succeeds"); + assert_eq!(decoded_owner, owner); + assert_eq!(decoded_params, params); + } + + #[test] + fn rejects_wrong_topic() { + let topics = vec![ + b256!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").to_vec(), + ]; + assert!(decode_conditional_order_created(&topics, &[]).is_none()); + } + + #[test] + fn rejects_empty_topics() { + assert!(decode_conditional_order_created(&[], &[]).is_none()); + } + + #[test] + fn decode_return_round_trip() { + let order = sample_order(); + let sig: Bytes = hex!("c0ffeec0ffeec0ffee").to_vec().into(); + let wire = (order.clone(), sig.clone()).abi_encode_params(); + + match decode_return(&wire).expect("decode succeeds") { + PollOutcome::Ready { + order: o, + signature: s, + } => { + assert_eq!(o.sellToken, order.sellToken); + assert_eq!(o.buyAmount, order.buyAmount); + assert_eq!(s, sig); + } + other => panic!("expected Ready, got {other:?}"), + } + } + + #[test] + fn build_order_creation_succeeds_with_empty_app_data() { + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let sig: Bytes = hex!("c0ffeec0ffeec0ffee").to_vec().into(); + let creation = + build_order_creation(&submittable_order(), sig.clone(), owner).expect("build succeeds"); + assert_eq!(creation.from, owner); + assert_eq!(creation.signing_scheme, cowprotocol::SigningScheme::Eip1271); + assert_eq!(creation.signature.to_bytes(), sig.to_vec()); + assert_eq!(creation.app_data, cowprotocol::EMPTY_APP_DATA_JSON); + assert_eq!(creation.app_data_hash, cowprotocol::EMPTY_APP_DATA_HASH); + } + + #[test] + fn build_order_creation_rejects_non_empty_app_data() { + let mut order = submittable_order(); + order.appData = B256::repeat_byte(0xee); + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let err = build_order_creation(&order, Bytes::new(), owner).unwrap_err(); + assert!(matches!(err, BuildError::Cowprotocol(_))); + } + + #[test] + fn build_order_creation_rejects_zero_from() { + let err = + build_order_creation(&submittable_order(), Bytes::new(), Address::ZERO).unwrap_err(); + assert!(matches!(err, BuildError::Cowprotocol(_))); + } + + #[test] + fn watch_key_round_trips_via_parse() { + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let hash = b256!("0202020202020202020202020202020202020202020202020202020202020202"); + let key = watch_key(&owner, &hash); + let (o, h) = parse_watch_key(&key).expect("parse"); + assert_eq!(o.parse::
().unwrap(), owner); + assert_eq!(h.parse::().unwrap(), hash); + } + + #[test] + fn outcome_try_next_block_is_no_op() { + assert_eq!( + outcome_to_update(&PollOutcome::TryNextBlock), + WatchUpdate::NoOp + ); + } + + #[test] + fn outcome_try_on_block_sets_next_block_gate() { + assert_eq!( + outcome_to_update(&PollOutcome::TryOnBlock(12_345)), + WatchUpdate::SetNextBlock(12_345), + ); + } + + #[test] + fn outcome_try_at_epoch_sets_next_epoch_gate() { + assert_eq!( + outcome_to_update(&PollOutcome::TryAtEpoch(1_700_000_000)), + WatchUpdate::SetNextEpoch(1_700_000_000), + ); + } + + #[test] + fn outcome_dont_try_again_drops_watch() { + assert_eq!( + outcome_to_update(&PollOutcome::DontTryAgain), + WatchUpdate::DropWatch + ); + } + + #[test] + fn outcome_ready_is_handled_by_submit_path_not_lifecycle() { + let order = Box::new(submittable_order()); + let outcome = PollOutcome::Ready { + order, + signature: Bytes::new(), + }; + assert_eq!(outcome_to_update(&outcome), WatchUpdate::NoOp); + } + + // ---- BLEU-854: MockHost dispatch tests ---- + + /// Build the LogView the indexer expects from a well-formed + /// `ConditionalOrderCreated`. + fn make_log_topics_and_data( + owner: Address, + params: &ConditionalOrderParams, + ) -> (Vec>, Vec) { + let mut owner_topic = vec![0u8; 12]; + owner_topic.extend_from_slice(owner.as_slice()); + let topics = vec![ + ConditionalOrderCreated::SIGNATURE_HASH.to_vec(), + owner_topic, + ]; + let data = params.abi_encode(); + (topics, data) + } + + /// Build the `params_json` `poll_one` passes to `host.request`. + fn programmed_eth_call_params(owner: Address, params: &ConditionalOrderParams) -> String { + let call = abi::getTradeableOrderWithSignatureCall { + owner, + params: abi::Params { + handler: params.handler, + salt: params.salt, + staticInput: params.staticInput.clone(), + }, + offchainInput: Bytes::new(), + proof: Vec::new(), + }; + eth_call_params(&COMPOSABLE_COW, &call.abi_encode()) + } + + /// JSON-encode a hex blob as the raw `result` field a JSON-RPC + /// response carries (a quoted hex string). + fn quoted_hex(bytes: &[u8]) -> String { + let hex = alloy_primitives::hex::encode_prefixed(bytes); + serde_json::to_string(&hex).unwrap() + } + + /// Pre-seed a `watch:` row identical to what the indexer would + /// write. + fn seed_watch(host: &MockHost, owner: Address, params: &ConditionalOrderParams) -> String { + let encoded = params.abi_encode(); + let key = watch_key(&owner, &keccak256(&encoded)); + host.store.set(&key, &encoded).unwrap(); + key + } + + fn sample_block(number: u64) -> BlockInfo { + BlockInfo { + chain_id: SEPOLIA, + number, + timestamp: 1_700_000_000_000, + } + } + + #[test] + fn index_records_new_watch_on_conditional_order_created() { + let host = MockHost::new(); + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let params = sample_params(); + let (topics, data) = make_log_topics_and_data(owner, ¶ms); + let view = LogView { + topics: &topics, + data: &data, + }; + + on_logs(&host, &[view]).unwrap(); + + let expected_key = watch_key(&owner, &keccak256(params.abi_encode())); + assert_eq!(host.store.len(), 1); + assert!(host.store.snapshot().contains_key(&expected_key)); + assert!(host.logging.contains("indexed")); + } + + #[test] + fn index_overwrites_in_place_on_redelivered_log() { + // BLEU-826 invariant: re-indexing the same `(owner, params)` + // pair must be a no-op on top of the existing watch - re-org + // replays and overlapping subscription windows are normal. + let host = MockHost::new(); + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let params = sample_params(); + let (topics, data) = make_log_topics_and_data(owner, ¶ms); + let view = LogView { + topics: &topics, + data: &data, + }; + + on_logs(&host, &[view]).unwrap(); + // Re-deliver the same log. + let view2 = LogView { + topics: &topics, + data: &data, + }; + on_logs(&host, &[view2]).unwrap(); + + assert_eq!(host.store.len(), 1, "redelivery must not duplicate watches"); + } + + #[test] + fn poll_skips_when_next_block_gate_is_in_future() { + let host = MockHost::new(); + let owner = address!("00112233445566778899aabbccddeeff00112233"); + let params = sample_params(); + let key = seed_watch(&host, owner, ¶ms); + let (_, hash_hex) = parse_watch_key(&key).unwrap(); + let owner_hex = format!("{owner:#x}"); + // Gate the watch at block 500; poll at block 100. + host.store + .set( + &format!("next_block:{owner_hex}:{hash_hex}"), + &500u64.to_le_bytes(), + ) + .unwrap(); + + on_block(&host, sample_block(100)).unwrap(); + + assert_eq!( + host.chain.call_count(), + 0, + "gated watch must not issue eth_call" + ); + assert_eq!(host.cow_api.call_count(), 0); + } + + #[test] + fn poll_ready_submits_order_and_persists_submitted_uid() { + let host = MockHost::new(); + let owner = address!("0011223344556677889900AABBCCDDEEFF001122"); + let params = sample_params(); + seed_watch(&host, owner, ¶ms); + + let ready_order = submittable_order(); + let signature: Bytes = hex!("c0ffeec0ffeec0ffee").to_vec().into(); + let wire = (ready_order.clone(), signature.clone()).abi_encode_params(); + host.chain.respond_to( + "eth_call", + programmed_eth_call_params(owner, ¶ms), + Ok(quoted_hex(&wire)), + ); + host.cow_api.respond(Ok("0xfeedface".to_string())); + + on_block(&host, sample_block(1_000)).unwrap(); + + assert_eq!(host.chain.call_count(), 1); + assert_eq!(host.cow_api.call_count(), 1); + assert!( + host.store.snapshot().contains_key("submitted:0xfeedface"), + "expected submitted:{{uid}} marker" + ); + } + + #[test] + fn submit_transient_error_leaves_state_unchanged_for_next_block() { + let host = MockHost::new(); + let owner = address!("0011223344556677889900AABBCCDDEEFF001122"); + let params = sample_params(); + let watch_key_str = seed_watch(&host, owner, ¶ms); + + let ready_order = submittable_order(); + let signature: Bytes = hex!("c0ffeec0ffeec0ffee").to_vec().into(); + let wire = (ready_order, signature).abi_encode_params(); + host.chain.respond_to( + "eth_call", + programmed_eth_call_params(owner, ¶ms), + Ok(quoted_hex(&wire)), + ); + + // InsufficientFee classifies as TryNextBlock per + // `OrderPostErrorKind::is_retriable`. + let api_body = serde_json::json!({ + "errorType": "InsufficientFee", + "description": "fee too low", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InsufficientFee".into(), + data: Some(api_body), + })); + + on_block(&host, sample_block(1_000)).unwrap(); + + // Watch still present, no gate written, no submitted marker. + assert!(host.store.snapshot().contains_key(&watch_key_str)); + let (owner_hex, hash_hex) = parse_watch_key(&watch_key_str).unwrap(); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("next_epoch:{owner_hex}:{hash_hex}")), + ); + assert!( + !host + .store + .snapshot() + .keys() + .any(|k| k.starts_with("submitted:")), + ); + assert!(host.logging.contains("retry-next-block")); + } + + #[test] + fn submit_permanent_error_drops_watch() { + let host = MockHost::new(); + let owner = address!("0011223344556677889900AABBCCDDEEFF001122"); + let params = sample_params(); + let watch_key_str = seed_watch(&host, owner, ¶ms); + + let ready_order = submittable_order(); + let signature: Bytes = hex!("c0ffeec0ffeec0ffee").to_vec().into(); + let wire = (ready_order, signature).abi_encode_params(); + host.chain.respond_to( + "eth_call", + programmed_eth_call_params(owner, ¶ms), + Ok(quoted_hex(&wire)), + ); + + // InvalidSignature classifies as Drop. + let api_body = serde_json::json!({ + "errorType": "InvalidSignature", + "description": "bad sig", + }) + .to_string(); + host.cow_api.respond(Err(HostError { + domain: "cow-api".into(), + kind: Kind::Denied, + code: 400, + message: "InvalidSignature".into(), + data: Some(api_body), + })); + + on_block(&host, sample_block(1_000)).unwrap(); + + assert!( + !host.store.snapshot().contains_key(&watch_key_str), + "permanent error must drop the watch" + ); + assert!(host.logging.contains("dropped watch")); + } + + #[test] + fn poll_dont_try_again_drops_watch_and_gates() { + // BLEU-830: when `decode_revert_hex` produces `DontTryAgain`, + // the lifecycle layer must delete the watch and any stale + // gates. Simulate by attaching an `OrderNotValid` revert + // payload to `host-error.data` - that's the wire shape the + // chain backend forwards once it surfaces structured RPC + // errors. + use alloy_sol_types::SolError; + use shepherd_sdk::cow::IConditionalOrder; + + let host = MockHost::new(); + let owner = address!("0011223344556677889900AABBCCDDEEFF001122"); + let params = sample_params(); + let watch_key_str = seed_watch(&host, owner, ¶ms); + let (owner_hex, hash_hex) = parse_watch_key(&watch_key_str).unwrap(); + host.store + .set( + &format!("next_block:{owner_hex}:{hash_hex}"), + &0u64.to_le_bytes(), + ) + .unwrap(); + + let revert = IConditionalOrder::OrderNotValid { + reason: "dead".into(), + } + .abi_encode(); + let revert_hex = serde_json::to_string(&alloy_primitives::hex::encode_prefixed(&revert)) + .expect("hex string serialises"); + host.chain.respond_to( + "eth_call", + programmed_eth_call_params(owner, ¶ms), + Err(HostError { + domain: "chain".into(), + kind: Kind::Internal, + code: -32000, + message: "execution reverted".into(), + data: Some(revert_hex), + }), + ); + + on_block(&host, sample_block(1_000)).unwrap(); + + assert!(!host.store.snapshot().contains_key(&watch_key_str)); + assert!( + !host + .store + .snapshot() + .contains_key(&format!("next_block:{owner_hex}:{hash_hex}")), + ); + assert!(host.logging.contains("dropped watch")); + } +}