onionwire/android/CUPRATE_DAEMON.md
sirius e6b6bc8dd6
android: build the SDK with cuprate-daemon and offer the on-device node
Part 3 of "cuprated built in": the chain daemon now ships inside the APK and the
wallet screen can pick it.

Build (android/sdk/build.gradle.kts)
- `cargoBuildAndroid` runs `cargo ndk … build --release --lib --features
  cuprate-daemon`. The Android target only: `cargoHostLib` / `uniffiBindgen` stay
  feature-free, because the host cdylib exists only so `uniffi-bindgen --library`
  can dlopen it and the UniFFI surface does not fork on a cargo feature.
- The link needs the NDK's libc++ and compiler-rt builtins explicitly (RandomX is
  C++ and its aarch64 JIT calls `__clear_cache`; `randomx-rs` links the NDK's stub
  `libstdc++.a`). `cargo ndk --link-builtins` covers the builtins only, so the
  whole set is spelled out as per-ABI `CARGO_TARGET_<TRIPLE>_RUSTFLAGS` — a global
  `RUSTFLAGS` would break the host build scripts. The linker is the NDK clang
  driver at the app's API level, named explicitly, because the dev machines pin
  the API 21 wrapper for `aarch64-linux-android` and API 21 fails this link.
- `ANDROID_NDK` is exported alongside `ANDROID_NDK_HOME`/`NDK_HOME`: without it
  `cmake-rs` aborts and `randomx-rs`'s build script panics.
- The task logs the feature it builds with, so CI carries the request, not just
  the task name.

Wallet screen
- Third chain source, **On-device node (Cuprate)**, beside the daemon field and
  Receive-only. Stored as `ChainChoice.InProcessCuprate` with an empty
  `monerod_datadir`, which the SDK resolves to `$ONIONWIRE_HOME/chain`. Existing
  Save daemon / Receive-only / Start / Stop behaviour is unchanged.
- The status line grows `· node <word>` from `WalletChildStatus.node_state` for
  this source only, fail-closed on an unrecognised word. No sync height: the SDK
  exposes none, so the screen does not show a number it cannot get.
- Storage honesty: free space on the chain store's filesystem, and Start is
  refused below a 3 GiB floor or when that filesystem cannot be read. The floor is
  a floor, not a sync estimate — nothing here exposes one.
- The pre-production warning (quoted user book, testnet only, Cuprate issue #278)
  is a sentence above Start, assertable in a test. Opt-in, never a default.
- The daemon field's hint no longer suggests a loopback daemon on the phone.

CI
- `android-ci.yml` extracts `lib/<abi>/libonionwire_sdk.so` from the built APK and
  fails the job unless it contains `target_max_memory`, a rodata literal compiled
  only under `cuprate-daemon`. v0.7.0's shipped `.so` has zero of them, so this
  separates "in the artifact" from "requested".

Docs
- `android/CUPRATE_DAEMON.md`: the build wiring, the link line, the CI gate, and
  an explicit "not verified on a device" section. `MONERO_BINARIES.md` points at
  it for the chain source that is not a binary.
2026-09-18 17:16:05 -04:00

7.9 KiB

The in-process chain daemon (cuprate-daemon) in the APK

The wallet's third chain source — Cuprate's chain daemon running inside the OnionWire process — is compiled into the Android build. This document is the provenance record for that: how the feature is enabled, why the link line looks the way it does, what CI checks, and what has not been proven.

Companion reading: docs/WALLET.md ("The in-process chain daemon") for what the daemon is, docs/cuprate-android-spike.md for the cross-compile spike this plumbing comes from, and MONERO_BINARIES.md for the monero-wallet-rpc executable, which is still a child process — Cuprate has no wallet layer.

What changed in the build

android/sdk/build.gradle.kts:

Feature --features cuprate-daemon on the Android build only
Where cargoBuildAndroid (the cargo ndk cross-compile)
Not where cargoHostLib / uniffiBindgen stay feature-free

The host cdylib exists only so uniffi-bindgen --library can dlopen it. Building the Cuprate graph there would add ~152 crates and a C++ RandomX build to every build for a library that never runs the daemon, and the UniFFI surface does not fork on a cargo feature (ChainChoice.InProcessCuprate is present either way, and WalletChildStatus carries the same fields) — so the generated Kotlin bindings are the same bytes.

cargoBuildAndroid prints the feature it is building with, so the CI log carries the request rather than only the task name:

cargo ndk build for arm64-v8a features=cuprate-daemon api=26 ndk=28.2.13676358

RandomX is C++ and its aarch64 JIT calls __clear_cache, and randomx-rs's build script emits rustc-link-lib=static=stdc++ against the NDK's stub libstdc++.a. Without libc++ and the compiler-rt builtins the link fails on __cxa_allocate_exception / std::runtime_error / typeinfo / __clear_cache. cargo ndk --link-builtins covers the builtins but not libc++, and -static-libstdc++ is ignored by NDK clang, so the full set is spelled out:

-L$NDK/toolchains/llvm/prebuilt/<host>/sysroot/usr/lib/<triple>
-L$NDK/toolchains/llvm/prebuilt/<host>/lib/clang/<ver>/lib/linux
-lclang_rt.builtins-<abi>-android
-lc++_static
-lc++abi
-Wl,--exclude-libs,libc++_static.a
-Wl,--exclude-libs,libc++abi.a

Three more things the build has to get right, each of which fails as something else:

  1. ANDROID_NDK must be exported, not only ANDROID_NDK_HOME / NDK_HOME. cmake-rs (RandomX's C++ half) aborts with "Android: Neither the NDK or a standalone toolchain was found", and randomx-rs's build script then panics inside cmake. The task exports all three.
  2. The linker is the NDK clang driver, named explicitly. The dev machines' ~/.cargo/config.toml pins [target.aarch64-linux-android] linker to the API 21 wrapper, and API 21 fails this link (pthread_atfork is API 23+). cargo-ndk's own linker wrapper is bypassed so the -Wl,… args reach clang as link args.
  3. The flags are CARGO_TARGET_<TRIPLE>_RUSTFLAGS, not RUSTFLAGS. A global rustflags would also apply to build scripts and proc macros, which are host-targeted, where -lc++_static breaks the link. Setting the per-target variable also replaces the target.<triple>.rustflags in ~/.cargo/config.toml, so the sysroot -L it carried is repeated in the list.

What ends up in the APK

lib/arm64-v8a/libandroidx.graphics.path.so
lib/arm64-v8a/libjnidispatch.so
lib/arm64-v8a/libmonerowalletrpc.so
lib/arm64-v8a/libonionwire_sdk.so
lib/arm64-v8a/librandomx_rs-<hash>.so     <- new, see below

librandomx_rs-<hash>.so is new and is not a load-time dependency of the SDK: readelf -d libonionwire_sdk.so lists liblog.so and nothing else, and the RandomX code is statically inside the SDK's own .so (its symbols and strings are there). cargo-ndk copies every cdylib the target directory produced into jniLibs, and randomx-rs also builds one. It is left in place rather than filtered out: it is 1,122,296 bytes uncompressed / 387,996 deflated, and dropping it would be a packaging rule that silently breaks the day something does link it.

The CI gate

.forgejo/workflows/android-ci.yml extracts lib/<abi>/libonionwire_sdk.so from the built APK and greps it for target_max_memory — a rodata literal from src/cuprate.rs's config TOML, compiled only under #[cfg(feature = "cuprate-daemon")]. The job fails if it is absent.

The negative control is v0.7.0's shipped APK: its libonionwire_sdk.so contains zero occurrences of that string (and of randomx / cuprated), so a match means the daemon is in the artifact, not merely requested. The build log line above is the other half of the proof — request and artifact, separately checkable.

What the wallet screen does with it

ui/WalletScreen.kt grew a third chain source beside the daemon field and Receive-only (ui/WalletChildStatusText.kt holds the pure model):

  • Selecting On-device node (Cuprate) stores ChainChoice.InProcessCuprate with an empty monerod_datadir, which the SDK resolves to $ONIONWIRE_HOME/chain — the same directory a spawned monerod would use. Kotlin never invents an absolute path.
  • monero-wallet-rpc is still required and still a subprocess: Start is gated on the bundled binary exactly as before, and an in-process node does not bundle arti, so a .onion daemon is still refused.
  • The status line renders the core's words verbatim and grows a node <word> segment from WalletChildStatus.node_state for this source only. An unrecognised word is never rendered as ready.
  • No sync height. WalletChildStatus has no height field — the core keeps a chain_height() internally (src/cuprate.rs) but does not expose it over UniFFI, so the screen does not show a number it cannot get. A height on the status line is a follow-up on the SDK surface, not something the UI can invent.
  • Storage honesty. The card shows the free space on the filesystem the fjall store grows into (StatFs on the app's data dir) and refuses Start below a 3 GiB floor, or when that filesystem cannot be read at all. The floor is a floor, not a sync estimate, and the doc comment says why: nothing in this stack exposes one — WalletChildStatus has no size field, and Cuprate's restricted RPC reports a hardcoded 5 GiB database_size placeholder.
  • Pre-production warning, above the Start button, in a sentence: CUPRATE_PREPRODUCTION_WARNING — quoted user book, testnet-only, and Cuprate issue #278, assertable in WalletChildStatusTextTest. Opt-in, never a default.

Not verified

The daemon was not executed on a device or an emulator. There is no arm64-v8a AVD on the build host (the same gap MONERO_BINARIES.md records), and no aarch64 Android sysroot to run it under qemu-user. What is proven: it cross-compiles and links against the NDK's C++ runtime, the feature is in the shipped .so, the artifact packages, and the Kotlin that drives it compiles and passes its unit tests. What is not: that cuprated starts, opens its fjall store and answers on loopback under Android's runtime — and, on top of that, Cuprate itself is pre-production and testnet-only.

To close the gap, on an arm64 device or a software-emulated arm64 AVD:

sdkmanager "system-images;android-35;google_apis;arm64-v8a"
avdmanager create avd -n ow-arm64 -k "system-images;android-35;google_apis;arm64-v8a"
emulator -avd ow-arm64          # software-emulated on an x86_64 host: slow
adb install -r dist/onionwire-<version>-android-arm64-v8a.apk

then unlock the node, open Wallet, tap On-device node (Cuprate), read the warning, press Start wallet, and watch the status line for connecting → syncing · in-process node · node starting → node syncing. A wallet balance needs the chain synced, which on a phone is a long wait — the status line is the thing to watch, not the balance.