From 4d2528351b6df2c54ce5506797722f37317047e5 Mon Sep 17 00:00:00 2001 From: Lance Date: Sun, 20 Sep 2026 00:48:53 -0400 Subject: [PATCH] android: poll the live wallet-child probe so the sync line moves MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wallet screen's 2s loop read Wire::wallet_status, which only re-reads the snapshot the last probe left in the store — so the `sync h/t` segment sat frozen until a chain-mode tap ran start_wallet_child (which probes on its way out). The loop now paints once from the cached read (non-dialing, so the screen has a record immediately) and then calls Wire::wallet_child_refresh for every tick: one live probe of the child's loopback wallet RPC, and of the configured local daemon's get_info when that RPC does not answer. Same 2s cadence, same Dispatchers.IO, same sticky walletChildError, same one-poller guarantee. The loop is a top-level function taking its two reads as parameters, which is the only seam a JVM test has — requireWire() is NodeHost.requireOpen(), a process-global only a real Arti bootstrap sets. WalletChildRefreshLoopTest pins that the cached read paints exactly once and every tick after it is the live probe, that a probe slower than the tick is never overlapped, and that cancelling the loop (leaving the screen) stops the dialing. --- .../siriusdevops/onionwire/WireViewModel.kt | 82 +++++++++++-- .../onionwire/WalletChildRefreshLoopTest.kt | 108 ++++++++++++++++++ 2 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 android/app/src/test/java/com/siriusdevops/onionwire/WalletChildRefreshLoopTest.kt 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 1efd980..6f26632 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -652,10 +652,17 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { fun openWalletChild() { if (walletChildPoller?.isActive == true) return walletChildPoller = viewModelScope.launch { - while (currentCoroutineContext().isActive) { - refreshWalletChild() - delay(WALLET_CHILD_TICK_MS) - } + 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, + ) } } @@ -671,17 +678,45 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { } /** - * One `wallet_status()` read. + * The first paint: 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 a 2s cadence is - * cheap. + * 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. */ - suspend fun refreshWalletChild() { - val read = withContext(Dispatchers.IO) { runCatching { requireWire().walletStatus() } } - read.fold( + 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( onSuccess = { _walletChild.value = it }, onFailure = { _walletChildError.value = it.message ?: it.toString() }, ) @@ -856,5 +891,32 @@ 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 new file mode 100644 index 0000000..a1b59c2 --- /dev/null +++ b/android/app/src/test/java/com/siriusdevops/onionwire/WalletChildRefreshLoopTest.kt @@ -0,0 +1,108 @@ +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 + } +}