onionwire/docs/cuprate-android-spike.md
agent ffea4bf9df
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
wallet: land the in-process Cuprate chain source on main
`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.
2026-09-18 13:45:59 -04:00

10 KiB

Cuprate on Android aarch64 — cross-compile spike

Question (kanban t_8293d96d): do the native dependencies of the in-process Cuprate daemon (randomx-rs, fjall, sqlite, the C++ RandomX build) cross-compile for the Android NDK target aarch64-linux-android? If they do not, §7a's Phase 1 ("Cuprate in-process instead of a monerod subprocess" on Android) keeps only its desktop half and the mobile SDK story loses the point of it.

Verdict: yes. cargo check --release --target aarch64-linux-android exits 0 on the Cuprate PoC tree, and a full link of the resulting Android binary also succeeds (ld.lld verdict: ELF 64-bit ARM aarch64 PIE, Android 26, NDK r28c). The cross-compile needs a specific environment and four extra link arguments; both are spelled out below. All of it is build plumbing — no Cuprate or RandomX source change was needed, and no host-arch object leaked into the artifacts.

Environment used

  • Host: Arch x86_64, cargo/rustc 1.98.1, 16 cores.
  • aarch64-linux-android std installed via rustup (~/.cargo/bin must win the PATH lookup — see gotcha 1).
  • NDK r28c at /home/lancelot/Android/Sdk/ndk/28.2.13676358 (matches ndkVersion in android/sdk/build.gradle.kts).
  • API level 26 (matches minSdk = 26 in android/app/build.gradle.kts).
  • Spike tree: the cuprate-poc manifest (cuprated git dep at 4f8fcd1b, randomx-rs git dep at 76c2385, arti-client 0.46 + tor-hsservice), copied to /var/tmp/cuprate-android-poc so it does not race the testnet card editing the shared /home/lancelot/.hermes/tmp/cuprate-poc.

Recipe

export PATH="$HOME/.cargo/bin:/usr/bin:/bin"          # rustup cargo/rustc, not the system ones
export ANDROID_NDK=/home/lancelot/Android/Sdk/ndk/28.2.13676358
export ANDROID_NDK_ROOT="$ANDROID_NDK" ANDROID_NDK_HOME="$ANDROID_NDK"
eval "$(cargo ndk-env -t arm64-v8a -P 26)"            # CC_/CXX_/AR_/CLANG_PATH for cc-rs + bindgen
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=\
"$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang"
export CARGO_TARGET_DIR=/var/tmp/<task>-target TMPDIR=/var/tmp/<task>-tmp

cargo check --release --target aarch64-linux-android   # exit 0

Linking a real binary (what Gradle will do) needs the C++ runtime and the compiler-rt builtins on the link line, because RandomX is C++ and its aarch64 JIT calls __clear_cache:

cargo rustc --release --target aarch64-linux-android -- \
  -C link-arg=-L$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android \
  -C link-arg=-L$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/19/lib/linux \
  -C link-arg=-lclang_rt.builtins-aarch64-android \
  -C link-arg=-lc++_static \
  -C link-arg=-lc++abi \
  -C link-arg=-Wl,--exclude-libs,libc++_static.a \
  -C link-arg=-Wl,--exclude-libs,libc++abi.a

Evidence

cargo check --release --target aarch64-linux-android (cold target dir, same tree):

   Compiling ... 152 crates ...
    Finished `release` profile [optimized] target(s) in 43.66s
### EXIT=0

Full build + link, same env:

    Finished `release` profile [optimized] target(s) in 1m 56s
### EXIT=0
$ file /var/tmp/cuprate-android-target/aarch64-linux-android/release/cuprate-poc
ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked,
interpreter /system/bin/linker64, for Android 26, built by NDK r28c (13676358), not stripped

RandomX specifically — the C++ half that could not be hand-waved:

  • out/lib/librandomx.a exists; member aes_hash.cpp.o is ELF64 / AArch64 (readelf -h) — real cross-built objects, not a host archive.
  • CMakeFiles/randomx.dir/src/ contains jit_compiler_a64.cpp.o and jit_compiler_a64_static.S.o, i.e. the ARM64 JIT path was selected (CMakeCache.txt: CMAKE_SYSTEM_PROCESSOR=aarch64, -DHAVE_HWCAP).
  • Flags actually used: -march=armv8-a+crypto (from RandomX's ARMv8 branch). The DARCH=native that randomx-rs's build.rs passes is a no-op — RandomX's CMakeLists.txt never reads DARCH, so ARCH stays default. That is lucky and worth knowing: had -march=native applied, the ARM64 branch would have targeted the host CPU while cross-compiling.

Cost on the desktop: cold cargo check for this graph ≈ 44 s / 694 MB of target dir; cargo build --release through to a linked binary ≈ 2 min with the dependency graph mostly warm / 1.7 GB (the RandomX C++ build is 26 top-level sources plus the aarch64 JIT). The release demo binary is 51 MB unstripped.

Gotchas (each one cost a run)

  1. PATH order decides which toolchain answers. With /usr/bin ahead of ~/.cargo/bin the system rustc answers and the build dies with error[E0463]: can't find crate for 'std' … the aarch64-linux-android target may not be installed, even though rustup has it.
  2. Plain cargo check --target aarch64-linux-android is not enough: cc-rs cannot find aarch64-linux-android-clang and libsqlite3-sys (bundled sqlite) fails first. cargo ndk-env is what supplies CC_/CXX_/AR_ for the target; the per-target linker in ~/.cargo/config.toml covers only the Rust link step, not the C toolchain (same trap the global [build] target used to cause).
  3. CMake needs the NDK env var too. cmake-rs does pass -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_PROCESSOR=aarch64, but CMake's Android-Determine.cmake still aborts with "Android: Neither the NDK or a standalone toolchain was found" unless ANDROID_NDK (or ANDROID_NDK_ROOT) is exported. randomx-rs's build script then panics inside cmake-0.1.58.
  4. cargo-ndk-env's linker wrapper is broken on this toolchain: CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER points at cargo-ndk itself, and rustc calls linkers with -Wl,…, which that binary re-parses as a cargo invocation (error: unexpected argument '-W' found, then it prints a rustup target install armv7-linux-androideabi hint that has nothing to do with the failure). Point the linker at the NDK clang driver instead, as above. The broken run also leaves a stale directory named librandomx_rs-<hash>.so in <target>/release/deps/, and the next cargo run then dies with "the generated executable … conflicts with the existing directory" — rmdir it.
  5. The C++ runtime is not linked for you. randomx-rs's build.rs emits rustc-link-lib=static=stdc++, and the NDK's libstdc++.a is a stub, so the final link fails with __cxa_allocate_exception / std::runtime_error / typeinfo undefined. -static-libstdc++ does not fix it — NDK clang reports "argument unused during compilation" and ignores it. Link -lc++_static -lc++abi explicitly. Separately, __clear_cache (RandomX aarch64 JIT) is only in libclang_rt.builtins-aarch64-android.a, so that library path and -l are needed too (this is what cargo ndk --link-builtins exists for).
  6. API level matters. With the NDK default (-P 21) the link fails on pthread_atfork, referenced by rand's fork handler; pthread_atfork is API 23+. Use -P 26 to match the app's minSdk. OnionWire's own ~/.cargo/config.toml already hints at 26 (-Wl,-L…/sysroot/usr/lib/aarch64-linux-android/26).

What this means for the payment plan

  • §7a Phase 1 survives the Android lens: the Cuprate-in-process daemon (cuprated + RandomX + fjall) compiles and links for aarch64-linux-android. What remains for Android is packaging plumbing in the Gradle/cargo ndk build — the C++ runtime and builtins flags above have to be wired into android/sdk/build.gradle.kts's cargoBuildAndroid (or expressed as CARGO_TARGET_AARCH64_LINUX_ANDROID_RUSTFLAGS), not a Cuprate feature gap.
  • RANDOMX_DIR / DARCH in randomx-rs's build script are not a cross-compile hazard today only because DARCH is inert. If the fork ever honours it, a -march=native Android build would be a silent host-arch miscompile.
  • Dependency cost is worth restating for the Android AAR: the cuprated graph adds axum/hyper/fjall/RandomX on top of this crate's own. That is build time and binary size, not a correctness problem, but it is real on a phone. The two arti families the spike tree built were an artifact of the PoC manifest enabling cuprated's default arti feature: OnionWire pins default-features = false for cuprated, so its arti-client 0.44 is not in this crate's graph — one Tor stack per process (see docs/WALLET.md).
  • Throwaway harness (not committed, per spike policy): /var/tmp/cuprate-android-poc/{check-acceptance.sh,check-cold.sh,build-android.sh,link-android.sh} and the logs next to them (/var/tmp/cuprate-android-{checkcold,accept,ndk2,link6}.log).

Proven in-repo (2026-09-18, card t_04975420)

The spike above ran on a throwaway PoC tree. The same two commands were re-run in this repository, against cuprated at the same pinned revision (4f8fcd1bf468f566fcd89c460d50de4adb6825bf) and behind this crate's cuprate-daemon feature, with the environment from Recipe above:

$ cargo check --locked --release --target aarch64-linux-android --features cuprate-daemon
    Finished `release` profile [optimized] target(s) in 1m 50s
CHECK_EXIT=0

$ cargo rustc --locked --release --target aarch64-linux-android \
      --features cuprate-daemon --bin onionwire -- <the link args above>
    Finished `release` profile [optimized] target(s) in 2m 05s
LINK_EXIT=0
$ file <target>/aarch64-linux-android/release/onionwire
ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked,
interpreter /system/bin/linker64, for Android 26, built by NDK r28c (13676358),
not stripped

cargo rustc needs --bin onionwire (or --lib) because this crate has both targets and rustc link args may only go to one of them — the PoC had a single bin, so this is new to the real tree.

Sizes, same target and link args, llvm-striped:

build stripped
without the feature 24,725,704 bytes
--features cuprate-daemon 37,053,472 bytes

That is a delta of 12,327,768 bytes (≈ +11.8 MiB), the number docs/WALLET.md quotes.