From db12f4838ba7b43993df265b712061d6f02e0dd4 Mon Sep 17 00:00:00 2001 From: rust-dev Date: Sun, 20 Sep 2026 00:20:32 -0400 Subject: [PATCH] =?UTF-8?q?sdk:=20expose=20wallet=5Fchild=5Frefresh=20?= =?UTF-8?q?=E2=80=94=20a=20live=20wallet-child=20probe=20over=20UniFFI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wire::wallet_status() is the cheap cached read (no dial); only Node::wallet_child_refresh() writes state/height/target_height, and it was reachable over UniFFI from exactly one place — start_wallet_child. An Android refresh loop polling wallet_status() therefore shows a frozen sync line. Add the additive export, reusing the existing child_status mapper so the record is the same shape as wallet_status(), and the core probe as the only probe path. Docs: one sentence in docs/WALLET.md, which enumerates the SDK wallet-child surface. Tests: SDK-level (a Wire cannot be built offline in this crate — open_wire bootstraps Arti): the unprobed child maps to the same 'off' record both reads report, pinned with an exhaustive record literal; and the probe on a stopped handle is a typed WireError.Stopped. --- crates/onionwire-sdk/src/lib.rs | 90 +++++++++++++++++++++++++++++++++ docs/WALLET.md | 7 ++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/crates/onionwire-sdk/src/lib.rs b/crates/onionwire-sdk/src/lib.rs index c9b5b2b..154b602 100644 --- a/crates/onionwire-sdk/src/lib.rs +++ b/crates/onionwire-sdk/src/lib.rs @@ -651,6 +651,32 @@ impl Wire { Ok(child_status(node.wallet_child_status(), &source)) } + /// One live probe of the app-owned wallet child, returning the same record + /// [`Wire::wallet_status`] reports — from a snapshot that was just taken + /// rather than one left behind by the last start. + /// + /// It dials: the child's loopback wallet RPC, and the configured local + /// daemon's `get_info` when the wallet RPC does not answer. That is what + /// makes the chain's height and target move between calls. + /// + /// It never spawns and never starts a child: a node with no child is not + /// launched for this call. + /// + /// Safe to call from a UI refresh loop, but not free — it makes network + /// calls, so a caller should poll on the order of seconds rather than every + /// frame. [`Wire::wallet_status`] stays the cheap, non-dialing read for + /// first paint. + /// + /// The honest limit: with no live child, the core short-circuits and returns + /// the cached `off` / `exited` snapshot, so this can never invent `ready` or + /// any other live state for a child that is not there. `WireError.Stopped` + /// after `shutdown`. + pub async fn wallet_child_refresh(&self) -> WResult { + let node = self.node()?; + let source = node.wallet_child_config().source; + Ok(child_status(node.wallet_child_refresh().await, &source)) + } + /// The chain-source + binary settings the child spawns with, as stored. /// Returns the defaults (receive-only, binaries by name) when nothing was /// ever set, so a settings screen can prefill from it safely. @@ -1997,4 +2023,68 @@ mod tests { assert_eq!(status.height, 0); assert_eq!(status.target_height, 0); } + + /// A live probe on a node with no child is `off` — the same record the + /// cheap read reports — never an error and never an invented state. + /// + /// This crate cannot build the node half of that: `open_wire` is the only + /// constructor of `Wire` and it bootstraps Arti and publishes an onion + /// service, so there is no offline `Node` for a test to hold. What is + /// pinned here is the half the SDK owns — both exported reads hand the same + /// core snapshot to the same `child_status` mapper — plus the record shape, + /// written as an exhaustive literal so a new field on `WalletChildStatus` + /// stops this test compiling. The core's own short-circuit (`wallet_child` + /// is `None` ⇒ return the cached snapshot) is what `src/node.rs`'s tests + /// exercise. + #[test] + fn an_unprobed_child_is_off_whichever_read_reports_it() { + let core = onionwire::wallet_child::WalletChildStatus { + state: WalletChildState::Off, + running: false, + configured: false, + chain_source: "receive-only".to_string(), + height: 0, + target_height: 0, + last_error: None, + }; + let source = ChainSource::None; + let want = WalletChildStatus { + state: "off".to_string(), + running: false, + configured: false, + chain_source: "receive-only".to_string(), + chain_in_process: false, + node_available: onionwire::cuprate::available(), + node_state: String::new(), + height: 0, + target_height: 0, + last_error: None, + }; + + // `wallet_status` maps it once, `wallet_child_refresh` maps the same + // snapshot the same way: one mapper, no second state machine. + for got in [ + child_status(core.clone(), &source), + child_status(core, &source), + ] { + assert_eq!(got.state, want.state); + assert_eq!(got.running, want.running); + assert_eq!(got.configured, want.configured); + assert_eq!(got.chain_source, want.chain_source); + assert_eq!(got.chain_in_process, want.chain_in_process); + assert_eq!(got.node_available, want.node_available); + assert_eq!(got.node_state, want.node_state); + assert_eq!(got.height, want.height); + assert_eq!(got.target_height, want.target_height); + assert_eq!(got.last_error, want.last_error); + } + } + + /// The live probe is an error, not a panic, once the handle is shut down: + /// it goes through the same `node()` accessor as every other call. + #[test] + fn wallet_child_refresh_on_a_stopped_handle_is_stopped_error() { + let err = block_on(stopped_wire().wallet_child_refresh()).expect_err("must fail"); + assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}"); + } } diff --git a/docs/WALLET.md b/docs/WALLET.md index 2a34f03..abf3c26 100644 --- a/docs/WALLET.md +++ b/docs/WALLET.md @@ -371,8 +371,11 @@ credentials live*), so they survive a restart: `Node::start_wallet_child` / `stop_wallet_child` / `wallet_child_status` / `wallet_child_refresh`. - Android (UniFFI): `set_chain_source(ChainConfig)` / `chain_source()`, - `start_wallet_child()`, `stop_wallet_child()`, `wallet_status()`. Pass - absolute paths for `wallet_rpc_bin` / `arti_bin` (`nativeLibraryDir`); the + `start_wallet_child()`, `stop_wallet_child()`, `wallet_status()` (the cached + read: no dial, for first paint) and `wallet_child_refresh()` (a live probe of + the same record — it dials the loopback wallet RPC, and the local daemon's + `get_info` when that RPC does not answer, so poll it on the order of + seconds). Pass absolute paths for `wallet_rpc_bin` / `arti_bin` (`nativeLibraryDir`); the core never resolves a binary the caller named explicitly. `ChainChoice` has a fourth variant, `InProcessCuprate`, for the in-process daemon: no binary path and, with an empty `monerod_datadir`, the SDK resolves the chain directory to