wallet: in-process Cuprate chain daemon behind the cuprate-daemon feature
All checks were successful
ci / test (pull_request) Successful in 4m35s
ci / sdk (pull_request) Successful in 1m12s

Phase 1 of the Cuprate integration strategy: the chain daemon can run inside
the OnionWire process instead of a spawned `monerod`. `monero-wallet-rpc` is
unchanged — it still dials the unrestricted loopback RPC on 127.0.0.1:18081,
which is the address `WalletChild::daemon_address` already returned — so no
wallet-layer code moves.

- Cargo.toml: `cuprated` (git, rev 4f8fcd1b) + `toml`, both optional, behind a
  `cuprate-daemon` feature that is OFF by default. Cuprate's own `arti` feature
  stays off: its arti-client 0.44 cannot unify with this crate's 0.46, and the
  in-process daemon is clearnet-only, so this keeps one Tor stack in the
  process. `Cargo.lock` gains the graph; no arti 0.44 appears in it.
- src/cuprate.rs (new): `CuprateNode` builds the cuprated config as TOML
  (testnet, unrestricted RPC on loopback, restricted RPC disabled, inbound P2P
  off), launches it, and owns it. Dropping the value is the teardown;
  `shutdown()` is called before the drop so the services can drain. The type
  exists in every build: without the feature `launch` fails with the feature
  name instead of silently doing nothing, so the chain-source type, its stored
  record and the picker do not fork on a cargo feature.
- src/wallet.rs: `ChainSource::InProcessCuprate { datadir }`.
- src/wallet_child.rs: starts/holds/stops the node, serializes the choice as
  `kind = "cuprate"`, and adds `chain_datadir` so the storage gate covers both
  local sources. It takes the app's Tokio handle (`with_runtime`): the launch is
  async and `start()` runs on the TUI's blocking thread.
- src/node.rs: `wallet_child_start(runtime)`, `default_in_process_chain_source`.
- src/tui.rs: a fourth picker row, its testnet caveat, the chain block copy, and
  a stop of the app-owned children on the quit path so they are not orphaned.
- tests/wallet.rs: a `cuprate-daemon`-gated end-to-end test that launches the
  node and drives get_info / get_height / get_block through the typed daemon
  client, including the above-the-tip 500 and the listener closing on drop.
- docs/WALLET.md: the fourth chain source, the feature flag, the +5.6 MB
  stripped overhead, and the two limits (testnet only, no Tor in the chain
  path).

Also repaired tests/tui_spec.rs, which the parent commit left un-compilable
(the Phase 2a commit had dropped all but the new imports and four new tests,
1221 lines of body); it is restored from the pre-breakage version, with the
Phase 2a imports and tests re-applied.
This commit is contained in:
rust-dev 2026-09-17 13:17:32 -04:00
commit 35a19bc37c
No known key found for this signature in database
12 changed files with 3785 additions and 53 deletions

View file

@ -819,10 +819,22 @@ fn chain_choice_never_preselects_a_remote_daemon() {
},
ChainChoice::RemoteDaemon,
),
// The in-process daemon is its own row: it is local (this device's
// chain, this app's process) but it is not a `monerod` spawn.
(
ChainSource::InProcessCuprate {
datadir: std::path::PathBuf::from("/tmp/chain"),
},
ChainChoice::InProcessCuprate,
),
] {
assert_eq!(ChainChoice::of(&source), want);
}
assert_eq!(ChainChoice::ALL.len(), 3, "three choices, never a public node");
assert_eq!(
ChainChoice::ALL.len(),
4,
"four choices, never a public node"
);
for choice in ChainChoice::ALL {
assert!(!choice.label().is_empty());
assert!(!choice.detail().is_empty());

File diff suppressed because it is too large Load diff

View file

@ -1348,6 +1348,109 @@ async fn a_transport_failure_is_not_a_height_error() {
assert!(!err.is_height_out_of_range(), "{err}");
}
// ---------------------------------------------------------------------------
// The in-process chain daemon (feature `cuprate-daemon`).
//
// The only 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. It needs no network — a testnet node with no peers answers RPC and
// stays at genesis.
// ---------------------------------------------------------------------------
/// `WalletChild::pick_port` is the app's own "give me a free loopback port".
#[cfg(feature = "cuprate-daemon")]
fn freed_port() -> u16 {
onionwire::wallet_child::WalletChild::pick_port().expect("free loopback port")
}
#[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_port();
let addr = format!("127.0.0.1:{port}");
let mut node = onionwire::cuprate::CuprateNode::launch(&datadir, port)
.await
.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 budget = Duration::from_secs(5);
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(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, 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, Duration::from_secs(30)).await;
assert!(
matches!(far, Err(WalletError::DaemonHeightOutOfRange)),
"an above-the-tip height must map to the typed height error: {far:?}"
);
// 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(Duration::from_millis(250)).await;
}
assert!(
closed,
"the unrestricted RPC was still accepting after shutdown"
);
}
#[test]
fn only_the_height_error_reports_itself_as_one() {
assert!(WalletError::DaemonHeightOutOfRange.is_height_out_of_range());