diff --git a/android/CUPRATE_DAEMON.md b/android/CUPRATE_DAEMON.md index 8689f2a..e3b422b 100644 --- a/android/CUPRATE_DAEMON.md +++ b/android/CUPRATE_DAEMON.md @@ -159,12 +159,12 @@ above is the other half of the proof — request and artifact, separately checka `contentDescription` carries (`walletChildStatusDescription`), so the spoken row is never shorter than the visible one. Cross-compiled and unit-tested on the JVM; still not device-proven. -* **What writes that snapshot on Android.** The wallet screen's loop - (`WireViewModel.openWalletChild` → `walletChildRefreshLoop`) does one cached - `Wire.walletStatus()` paint, then every 2 s a live - `Wire.walletChildRefresh()` (`Wire::wallet_child_refresh`). The live call is - what makes height/target move between ticks; it never spawns. See - [README.md](README.md#what-the-apk-refresh-loop-actually-calls). +* **What writes that snapshot on Android.** The wallet screen's 2 s loop + (`WireViewModel.refreshWalletChild`) calls `Wire.walletStatus()` — the cached + UniFFI read, no dial. The live probe `Wire.walletChildRefresh()` + (`Wire::wallet_child_refresh` in `crates/onionwire-sdk`) exists and is what + makes height/target move between calls; the TUI uses it. The APK poller on + this tree does not. See [README.md](README.md#what-the-apk-refresh-loop-actually-calls). * **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 floor, or when that filesystem cannot be read at all. The floor is a *floor*, @@ -256,5 +256,6 @@ 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. After the first cached paint, the APK -loop probes live every 2 s, so `sync h/t` should move once the daemon answers. +is the thing to watch, not the balance. On this tree the APK loop will not itself +refresh height from the daemon; a live probe is a separate SDK call the TUI +makes and the Android poller does not. diff --git a/android/README.md b/android/README.md index c96aaa4..6d58f6a 100644 --- a/android/README.md +++ b/android/README.md @@ -251,26 +251,17 @@ in-process daemon, the chain probe fails, and the state stays `syncing`. ### What the APK refresh loop actually calls -`WireViewModel.openWalletChild()` runs `walletChildRefreshLoop` with a 2 s -tick (`WALLET_CHILD_TICK_MS = 2_000`): +`WireViewModel.openWalletChild()` polls every 2 s +(`WALLET_CHILD_TICK_MS = 2_000`). Each tick is `Wire.walletStatus()` — the +cached, non-dialing UniFFI read (`Wire::wallet_status`). It never spawns and +it never probes the daemon, so `sync h/t` only moves when something else +wrote a fresh snapshot (start, a previous live probe). -1. **First paint** — `paintWalletChildCached()` → `Wire.walletStatus()` - (`Wire::wallet_status`). Cached, no dial, so the screen has a record - while the first probe is still in flight. -2. **Every tick after that** — `refreshWalletChild()` → - `Wire.walletChildRefresh()` (`Wire::wallet_child_refresh`). That dials - the child's loopback wallet RPC, and the configured local daemon's - `get_info` when the wallet RPC does not answer, which is what makes - `sync h/t` move without another Start. It never spawns a child. With no - live child the core short-circuits to the cached `off` / `exited` - snapshot, so this cannot invent `ready`. - -The loop suspends on each read before it sleeps: a probe slower than 2 s -delays the next tick instead of overlapping it. Do not run two concurrent -`wallet_child_refresh` calls against one child. - -See [docs/WALLET.md](../docs/WALLET.md) (Android / SDK) for the same two -methods as the UniFFI surface. +The SDK also exposes `Wire.walletChildRefresh()` (`Wire::wallet_child_refresh`): +a live probe of the loopback wallet RPC, and of the local daemon's `get_info` +when that RPC does not answer. The TUI uses it. **This APK's poller does not.** +Do not read the moving `sync h/t` docs in [docs/WALLET.md](../docs/WALLET.md) +as a description of the Android loop on this tree. ## CI and releases @@ -382,7 +373,7 @@ android/ RotateScreen.kt Insets.kt Theme.kt - src/test/ # JVM: envelope, store, invite, outbox, keepalive, wallet line, refresh loop + src/test/ # JVM: envelope, store, invite, outbox, keepalive, wallet line src/androidTest/ # device-only: AndroidKeyStore src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so ``` diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt index 6f26632..1efd980 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -652,17 +652,10 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { fun openWalletChild() { if (walletChildPoller?.isActive == true) return walletChildPoller = viewModelScope.launch { - walletChildRefreshLoop( - tickMs = WALLET_CHILD_TICK_MS, - // The first paint is the cached read: it never dials, so the - // screen has a record to render while the first probe is still - // in flight. - cached = ::paintWalletChildCached, - // Every tick after that is a live probe, so `height` / - // `target_height` move on their own instead of only when a - // chain-mode button is tapped. - live = ::refreshWalletChild, - ) + while (currentCoroutineContext().isActive) { + refreshWalletChild() + delay(WALLET_CHILD_TICK_MS) + } } } @@ -678,45 +671,17 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { } /** - * The first paint: one `wallet_status()` read. + * One `wallet_status()` read. * * `Wire::wallet_status` is synchronous in Rust and re-reads the sealed * wallet-child record out of the SQLCipher store, so it goes to * [Dispatchers.IO] — a store decrypt on the main thread is an ANR waiting - * for a slow device. It never dials and never spawns, so it is the right - * read to have a record on screen — and the wrong one to poll with. + * for a slow device. It never dials and never spawns, so a 2s cadence is + * cheap. */ - suspend fun paintWalletChildCached() = readWalletChild { requireWire().walletStatus() } - - /** - * One tick: a live probe, `Wire::wallet_child_refresh()`. - * - * `Wire::wallet_status` above only re-reads what the last probe left behind, - * so polling it pins the `sync h/t` segment on whatever the last - * start/stop saw. This one dials — the child's loopback wallet RPC, and the - * configured local daemon's `get_info` when the wallet RPC does not answer - * — which is what makes the chain's height and target move between ticks. - * - * It never spawns a child, and the core short-circuits with the cached - * snapshot when there is no live child, so it cannot invent `ready` for a - * wallet that is not there. Not free, though: the tick stays on the order of - * seconds rather than frames. - * - * Failure is folded into the sticky [walletChildError], same as the cached - * read: a probe that fails once must not blink the previous error off the - * screen. - */ - suspend fun refreshWalletChild() = readWalletChild { requireWire().walletChildRefresh() } - - /** - * Runs one of the two reads on [Dispatchers.IO] and lands it in the wallet - * state. `requireWire()` throws when the node is locked, so it is called - * *inside* the [runCatching] — a locked node has to surface as - * [walletChildError], not as a crash on the tick. - */ - private suspend fun readWalletChild(read: suspend () -> WalletChildStatus) { - val result = withContext(Dispatchers.IO) { runCatching { read() } } - result.fold( + suspend fun refreshWalletChild() { + val read = withContext(Dispatchers.IO) { runCatching { requireWire().walletStatus() } } + read.fold( onSuccess = { _walletChild.value = it }, onFailure = { _walletChildError.value = it.message ?: it.toString() }, ) @@ -891,32 +856,5 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { ) } -/** - * The wallet-child refresh loop: one [cached] paint so the screen has something - * to render, then a live [live] probe every [tickMs]. - * - * The reads are parameters only so the JVM test tree can drive this loop - * without a `Wire` — `NodeHost.requireOpen()` is a process-global the unit test - * cannot prime. The two call sites in [WireViewModel.openWalletChild] bind them - * to the cached read and to the live probe. - * - * Serialized by construction, and that is the point: the loop suspends on each - * read before it sleeps, so a probe slower than [tickMs] delays the next tick - * instead of overlapping it. Do not turn this into a `Flow` with - * `collectLatest`, and do not `launch` the probe: two concurrent - * `wallet_child_refresh` calls against one wallet child is a racing probe. - */ -suspend fun walletChildRefreshLoop( - tickMs: Long, - cached: suspend () -> Unit, - live: suspend () -> Unit, -) { - cached() - while (currentCoroutineContext().isActive) { - delay(tickMs) - live() - } -} - /** Cadence of the wallet-child refresh loop, in milliseconds. */ private const val WALLET_CHILD_TICK_MS: Long = 2_000 diff --git a/android/app/src/test/java/com/siriusdevops/onionwire/WalletChildRefreshLoopTest.kt b/android/app/src/test/java/com/siriusdevops/onionwire/WalletChildRefreshLoopTest.kt deleted file mode 100644 index a1b59c2..0000000 --- a/android/app/src/test/java/com/siriusdevops/onionwire/WalletChildRefreshLoopTest.kt +++ /dev/null @@ -1,108 +0,0 @@ -package com.siriusdevops.onionwire - -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.test.advanceTimeBy -import kotlinx.coroutines.test.currentTime -import kotlinx.coroutines.test.runTest -import org.junit.Assert.assertEquals -import org.junit.Test - -/** - * The wallet screen's refresh loop. - * - * `Wire` has no seam a JVM test can fake: `requireWire()` reads - * `NodeHost.requireOpen()`, a process-global that only `openWire()` — i.e. a - * real Arti bootstrap — sets, and `Wire` itself is the generated UniFFI - * interface. So the loop takes its two reads as parameters, and these tests - * drive it with recording lambdas to pin the two things that make the - * `sync h/t` segment live: - * - * - the cheap cached read paints once, and every tick after it is the live - * probe (`Wire::wallet_child_refresh`), not the cached status read - * (`Wire::wallet_status`) that only re-reads what the last probe left behind; - * - the ticks are serialized by construction — a probe slower than the tick - * delays the next one instead of overlapping it, so no two - * `wallet_child_refresh` calls are ever in flight against one wallet child. - * - * The binding of `live` to `WireViewModel.refreshWalletChild()` (and `cached` - * to `paintWalletChildCached()`) is the two lines in - * `WireViewModel.openWalletChild()`, and the source there is the evidence for - * it — nothing in this test can observe a `NodeHost` that is not open. - * - * Time is virtual, so the 2s cadence is free to assert. Note the scheduler's - * rule: `advanceTimeBy(n)` runs what is scheduled strictly before `n`, not the - * task at `n` itself. - */ -class WalletChildRefreshLoopTest { - - @Test - fun the_first_paint_is_the_cached_read_and_every_tick_after_it_is_live() = runTest { - var cached = 0 - var live = 0 - val job = launch { - walletChildRefreshLoop(tickMs = TICK_MS, cached = { cached++ }, live = { live++ }) - } - - // Four ticks of virtual time: probes at 2s, 4s and 6s; the 8s one has - // not fired yet. - advanceTimeBy(4 * TICK_MS) - - assertEquals("the cheap read paints once, and only once", 1, cached) - assertEquals("every tick after the paint is a live probe", 3, live) - assertEquals("and they land on the tick, not back to back", 4 * TICK_MS, currentTime) - - job.cancel() - advanceTimeBy(10 * TICK_MS) - assertEquals("cancelling the loop is what stops the dialing", 3, live) - } - - @Test - fun a_probe_slower_than_the_tick_is_never_overlapped() = runTest { - var inFlight = 0 - var maxInFlight = 0 - var probes = 0 - val job = launch { - walletChildRefreshLoop( - tickMs = TICK_MS, - cached = {}, - live = { - inFlight++ - if (inFlight > maxInFlight) maxInFlight = inFlight - delay(5 * TICK_MS) // a probe five ticks long - inFlight-- - probes++ - }, - ) - } - - // Probe 1 at 2s runs to 12s, probe 2 at 14s to 24s, probe 3 at 26s to - // 36s: each of them pushes the next tick out by its own length. - advanceTimeBy(19 * TICK_MS) - - assertEquals("two concurrent wallet_child_refresh probes is a race", 1, maxInFlight) - assertEquals("three probes done, none of them skipped or doubled", 3, probes) - assertEquals(19 * TICK_MS, currentTime) - - job.cancel() - } - - @Test - fun cancelling_the_loop_stops_the_probes() = runTest { - var live = 0 - val job = launch { - walletChildRefreshLoop(tickMs = TICK_MS, cached = {}, live = { live++ }) - } - - advanceTimeBy(3 * TICK_MS) - assertEquals("two ticks have gone by", 2, live) - - job.cancel() - advanceTimeBy(10 * TICK_MS) - assertEquals("leaving the wallet screen stops the dialing", 2, live) - } - - private companion object { - const val TICK_MS = 2_000L - } -}