android: poll the live wallet-child probe so the sync line moves
All checks were successful
android-ci / android (pull_request) Successful in 10m51s
All checks were successful
android-ci / android (pull_request) Successful in 10m51s
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.
This commit is contained in:
parent
fa0e4b2f6c
commit
4d2528351b
2 changed files with 180 additions and 10 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue