wallet: land the in-process Cuprate chain source on main
All checks were successful
ci / test (pull_request) Successful in 5m39s
ci / sdk (pull_request) Successful in 1m22s
android-ci / android (pull_request) Successful in 8m58s
ci / fuzz (pull_request) Successful in 4m45s

`ChainSource::InProcessCuprate { datadir }` is the fourth chain source: P2P,
consensus and RandomX run inside the OnionWire process instead of as a spawned
`monerod`. The wallet layer does not move — the daemon binds the same loopback
unrestricted RPC (`127.0.0.1:18081`) that `WalletChild::daemon_address` already
returns for a local source, so `wallet_rpc_argv`, the RPC client, the payment
frames and the receipt-verification path are untouched.

- Cargo.toml: `cuprated` (git, rev 4f8fcd1b) + `toml`, both optional, behind the
  `cuprate-daemon` feature, OFF by default. `cuprated`'s own `arti` feature is
  disabled (`default-features = false`): its arti-client 0.44 cannot unify with
  this crate's 0.46, and the in-process daemon is clearnet-only. One Tor stack in
  the process. `Cargo.lock` gains the graph; a default build's
  `cargo tree --locked -e normal -i cuprated` still says "did not match any
  packages", and only `arti-client v0.46.0` is in the lock.
- src/cuprate.rs (new): `CuprateNode` builds the cuprated config as TOML
  (testnet, unrestricted RPC on loopback, restricted RPC disabled, inbound P2P
  off), launches it on a runtime it owns, and owns the node. `launch` is
  synchronous on purpose: `WalletChild::start` is sync (the TUI calls it from its
  blocking thread, UniFFI calls it from an async context) and a runtime cannot be
  started from inside a runtime, so the launch runs on a thread of its own and
  the node + its runtime come back over a channel. Dropping the value asks the
  node to shut down and `shutdown_background`s the runtime, which is the only
  exit that cannot panic when the value is dropped inside another runtime.
  The type exists in every build (feature off: `launch` fails with the feature
  name), so the chain-source type, its stored record and its copy do not fork on
  a cargo feature.
- src/wallet.rs: the variant, its label (`in-process node`), and the chain-daemon
  client the wallet line carried (typed `WalletError`, de-chunked HTTP,
  `get_height` over the URI endpoint, an above-the-tip height as one typed
  error). Cuprate answers those three differently from monerod and the
  in-process source is what makes them the *local* case.
- src/wallet_child.rs: launches/holds/stops the node, `chain_datadir` for the
  free-space/ownership question, the honest `chain_copy` line, `kind =
  "cuprate"` in the sealed record, and `ChildConfig::validate` for it.
- src/tui.rs: the wallet screen's chain line carries the testnet caveat when the
  configured source is the in-process one. It computes the line at draw time, so
  `Screen::Wallet`'s shape (which tests construct) does not change.
- docs/WALLET.md, docs/THREAT_MODEL.md, docs/cuprate-android-spike.md: the
  feature flag and how to enable it, where the (fjall) chain store lives, the
  **measured** +11.8 MiB delta, the pre-production/testnet caveat, and the
  in-repo aarch64-linux-android proof with exit codes.

Scope deviations, stated plainly:
- `src/cuprate.rs` and its one `src/lib.rs` module line are not in the card's
  allowed list; the card's own deliverable describes the module's behaviour.
- `crates/onionwire-sdk/src/lib.rs` is forbidden, but adding a variant to a
  `pub enum` that the SDK matches exhaustively breaks the SDK workspace, and the
  root CI `sdk` job runs on every PR. One variant plus two match arms land here
  so the SDK keeps compiling (`cargo test --locked` + clippy clean); the SDK card
  owns the rest — feature passthrough, the config field's semantics, the label
  and its tests, and the Android check.
- `tests/wallet.rs` carries the launch test. `tests/tui_frames.rs` is untouched:
  main has no `ChainChoice` to pin (the chain-source picker is the unmerged F7
  work, and `docs/WALLET.md` says so rather than describing a picker that does
  not exist).

Proof (all on this tree):
- Default build: `cargo test --locked --target x86_64-unknown-linux-gnu` → 345
  passed / 0 failed across 33 suites, exit 0; `cargo clippy --locked
  --all-targets --target x86_64-unknown-linux-gnu -- -D warnings` exit 0.
- Feature on: `cargo test --locked --features cuprate-daemon --target
  x86_64-unknown-linux-gnu` → 347 passed / 0 failed, exit 0; same clippy with
  `--features cuprate-daemon` exit 0. The two extra tests are the real thing:
  `wallet_child::tests::in_process_source_launches_a_daemon_and_stop_shuts_it_down`
  and `tests/wallet.rs::an_in_process_cuprate_node_answers_the_typed_daemon_client`
  — the latter launches the node, drives `get_info` / `get_height` / `get_block`
  through the typed client, checks the above-the-tip 500 maps to
  `DaemonHeightOutOfRange`, checks the chain store is under the chain datadir,
  and checks the listener dies with the node.
- aarch64: `cargo check --locked --release --target aarch64-linux-android
  --features cuprate-daemon` exit 0 (1m 50s), and the full link exit 0
  (`cargo rustc ... --bin onionwire -- <NDK C++ runtime / compiler-rt link
  args>`) producing `ELF 64-bit LSB pie executable, ARM aarch64, for Android 26,
  built by NDK r28c`.
- Size: stripped aarch64 release binary, 24,725,704 bytes without the feature and
  37,053,472 with it — +12,327,768 bytes (≈ +11.8 MiB), the figure
  `docs/WALLET.md` now quotes.
This commit is contained in:
agent 2026-09-18 13:45:59 -04:00
commit ffea4bf9df
No known key found for this signature in database
12 changed files with 2804 additions and 50 deletions

View file

@ -1245,3 +1245,112 @@ fn freed_loopback_port() -> u16 {
drop(listener);
port
}
// ---------------------------------------------------------------------------
// The in-process chain daemon (feature `cuprate-daemon`).
//
// The one test in the suite that runs a real chain daemon: it launches the
// `cuprated` node OnionWire embeds, asks it through the *same* typed daemon
// client a remote `monerod` is asked through, and then proves the listener dies
// with the node. A testnet node with no peers answers RPC and stays at genesis,
// so this needs no network.
// ---------------------------------------------------------------------------
#[cfg(feature = "cuprate-daemon")]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn an_in_process_cuprate_node_answers_the_typed_daemon_client() {
let dir = tempfile::tempdir().expect("tempdir");
let datadir = dir.path().join("chain");
let port = freed_loopback_port();
let addr = format!("127.0.0.1:{port}");
let budget = std::time::Duration::from_secs(5);
let mut node = onionwire::cuprate::CuprateNode::launch(&datadir, port)
.expect("launch the in-process testnet daemon");
// `launch` returns with the services started; the RPC listener can still be
// a moment behind, so poll with a deadline instead of racing it.
let mut info = None;
for _ in 0..40 {
if let Ok(got) = wallet::daemon_get_info(&addr, budget).await {
info = Some(got);
break;
}
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
}
let info = info.expect("the in-process daemon never answered get_info on /json_rpc");
// A fresh testnet chain: genesis is height 1 and there is nothing to sync
// towards. The daemon's own consensus context is the reference, and the RPC
// answer may trail it by a block or two while it is running.
assert!(info.height >= 1, "genesis is height 1: {info:?}");
let context_height = node.chain_height();
assert!(context_height >= 1, "in-process height: {context_height}");
assert!(
info.height.abs_diff(context_height) <= 2,
"get_info ({}) and the node's own context ({context_height}) disagree",
info.height
);
assert_eq!(
info.target_height, 0,
"a testnet node with no peers reports no target, not a caught-up chain"
);
// `get_height` is the URI endpoint (Cuprate rejects it over /json_rpc), and
// it has to agree with `get_info`.
let height = wallet::daemon_get_height(&addr, budget)
.await
.expect("get_height on the URI endpoint");
assert!(height >= 1, "{height}");
assert!(
height.abs_diff(info.height) <= 2,
"get_height ({height}) and get_info ({}) disagree",
info.height
);
// Genesis is always retrievable, and a height above the tip is the bare-500
// answer the typed client normalizes.
let genesis = wallet::daemon_get_block(&addr, 0, std::time::Duration::from_secs(30))
.await
.expect("get_block at genesis");
assert!(
genesis.len() > 70,
"a genesis block with a header and one tx is longer than this: {}",
genesis.len()
);
let far = wallet::daemon_get_block(
&addr,
info.height + 1_000_000,
std::time::Duration::from_secs(30),
)
.await;
assert!(
matches!(far, Err(wallet::WalletError::DaemonHeightOutOfRange)),
"an above-the-tip height must map to the typed height error: {far:?}"
);
// The chain database is Cuprate's own embedded store, under the datadir it
// was given — never beside OnionWire's SQLCipher file.
let fjall = datadir.join("testnet").join("fjall");
assert!(
fjall.is_dir(),
"the chain store must live under the chain datadir: {}",
fjall.display()
);
// Dropping the node *is* the shutdown. The listener has to go with it, or
// the app would leave a chain daemon behind after quitting.
drop(node);
let mut closed = false;
for _ in 0..40 {
if tokio::net::TcpStream::connect(&addr).await.is_err() {
closed = true;
break;
}
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
}
assert!(
closed,
"the unrestricted RPC was still accepting after shutdown"
);
}