Merge pull request 'android: poll the live wallet-child probe so the wallet sync line moves' (#134) from wt/t_07542df0 into main
All checks were successful
android-ci / android (push) Successful in 10m26s
All checks were successful
android-ci / android (push) Successful in 10m26s
Reviewed-on: #134
This commit is contained in:
commit
d2d49d27c5
2 changed files with 180 additions and 10 deletions
|
|
@ -652,10 +652,17 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
||||||
fun openWalletChild() {
|
fun openWalletChild() {
|
||||||
if (walletChildPoller?.isActive == true) return
|
if (walletChildPoller?.isActive == true) return
|
||||||
walletChildPoller = viewModelScope.launch {
|
walletChildPoller = viewModelScope.launch {
|
||||||
while (currentCoroutineContext().isActive) {
|
walletChildRefreshLoop(
|
||||||
refreshWalletChild()
|
tickMs = WALLET_CHILD_TICK_MS,
|
||||||
delay(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
|
* `Wire::wallet_status` is synchronous in Rust and re-reads the sealed
|
||||||
* wallet-child record out of the SQLCipher store, so it goes to
|
* 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
|
* [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
|
* for a slow device. It never dials and never spawns, so it is the right
|
||||||
* cheap.
|
* read to have a record on screen — and the wrong one to poll with.
|
||||||
*/
|
*/
|
||||||
suspend fun refreshWalletChild() {
|
suspend fun paintWalletChildCached() = readWalletChild { requireWire().walletStatus() }
|
||||||
val read = withContext(Dispatchers.IO) { runCatching { requireWire().walletStatus() } }
|
|
||||||
read.fold(
|
/**
|
||||||
|
* 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 },
|
onSuccess = { _walletChild.value = it },
|
||||||
onFailure = { _walletChildError.value = it.message ?: it.toString() },
|
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. */
|
/** Cadence of the wallet-child refresh loop, in milliseconds. */
|
||||||
private const val WALLET_CHILD_TICK_MS: Long = 2_000
|
private const val WALLET_CHILD_TICK_MS: Long = 2_000
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue