Merge pull request 'android: Start wallet no longer looks like a no-op while Cuprate launches' (#124) from onionwire/android-cuprate-start-feedback into main
Reviewed-on: #124
This commit is contained in:
commit
cd1acd3c0d
7 changed files with 103 additions and 34 deletions
|
|
@ -40,6 +40,7 @@ import uniffi.onionwire_sdk.WalletChildStatus
|
||||||
import uniffi.onionwire_sdk.Wire
|
import uniffi.onionwire_sdk.Wire
|
||||||
import uniffi.onionwire_sdk.decodeInvite
|
import uniffi.onionwire_sdk.decodeInvite
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
sealed interface Phase {
|
sealed interface Phase {
|
||||||
/** No identity unlocked. Nothing Tor-related is running. */
|
/** No identity unlocked. Nothing Tor-related is running. */
|
||||||
|
|
@ -618,6 +619,14 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
||||||
private val _walletChildError = MutableStateFlow<String?>(null)
|
private val _walletChildError = MutableStateFlow<String?>(null)
|
||||||
val walletChildError: StateFlow<String?> = _walletChildError.asStateFlow()
|
val walletChildError: StateFlow<String?> = _walletChildError.asStateFlow()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start is in flight. Core status stays `off` until Cuprate/`monerod` answers,
|
||||||
|
* so the screen must not wait on that snapshot to show that the tap landed.
|
||||||
|
*/
|
||||||
|
private val startInFlight = AtomicBoolean(false)
|
||||||
|
private val _walletChildStarting = MutableStateFlow(false)
|
||||||
|
val walletChildStarting: StateFlow<Boolean> = _walletChildStarting.asStateFlow()
|
||||||
|
|
||||||
/** The stored chain source + binary settings, for prefilling the editor. */
|
/** The stored chain source + binary settings, for prefilling the editor. */
|
||||||
private val _chainSource = MutableStateFlow<ChainConfig?>(null)
|
private val _chainSource = MutableStateFlow<ChainConfig?>(null)
|
||||||
val chainSource: StateFlow<ChainConfig?> = _chainSource.asStateFlow()
|
val chainSource: StateFlow<ChainConfig?> = _chainSource.asStateFlow()
|
||||||
|
|
@ -795,25 +804,28 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
||||||
fun chainFreeBytes(): Long? = ChainStorage.freeBytes(home)
|
fun chainFreeBytes(): Long? = ChainStorage.freeBytes(home)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns the child (and its daemon/SOCKS siblings) and returns what the core
|
* Kick off start on [viewModelScope] so leaving the composition does not
|
||||||
* reached.
|
* cancel Cuprate launch. Returns immediately; watch [walletChildStarting]
|
||||||
*
|
* and [walletChildError]. Core status stays `off` until the node answers.
|
||||||
* The returned state is `connecting` at best on the first call — the wallet
|
|
||||||
* RPC does not answer the instant the process exists — and that is reported
|
|
||||||
* as-is. The spawn is synchronous inside the node and a `.onion` source
|
|
||||||
* waits up to 20s for its SOCKS listener, so this never runs on the main
|
|
||||||
* thread.
|
|
||||||
*/
|
*/
|
||||||
suspend fun startWalletChild(): Result<WalletChildStatus> {
|
fun startWalletChild() {
|
||||||
|
if (!startInFlight.compareAndSet(false, true)) return
|
||||||
|
_walletChildStarting.value = true
|
||||||
_walletChildError.value = null
|
_walletChildError.value = null
|
||||||
val started = withContext(Dispatchers.IO) {
|
viewModelScope.launch {
|
||||||
runCatching { requireWire().startWalletChild() }
|
try {
|
||||||
|
val started = withContext(Dispatchers.IO) {
|
||||||
|
runCatching { requireWire().startWalletChild() }
|
||||||
|
}
|
||||||
|
started.fold(
|
||||||
|
onSuccess = { _walletChild.value = it },
|
||||||
|
onFailure = { _walletChildError.value = it.message ?: it.toString() },
|
||||||
|
)
|
||||||
|
} finally {
|
||||||
|
startInFlight.set(false)
|
||||||
|
_walletChildStarting.value = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
started.fold(
|
|
||||||
onSuccess = { _walletChild.value = it },
|
|
||||||
onFailure = { _walletChildError.value = it.message ?: it.toString() },
|
|
||||||
)
|
|
||||||
return started
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Stops the child and its siblings. Idempotent in the core. */
|
/** Stops the child and its siblings. Idempotent in the core. */
|
||||||
|
|
|
||||||
|
|
@ -303,6 +303,9 @@ const val ONION_NEEDS_ARTI_NOTICE: String =
|
||||||
/** Shown while the first `wallet_status()` read is still in flight. */
|
/** Shown while the first `wallet_status()` read is still in flight. */
|
||||||
const val READING_CHILD_NOTICE: String = "Reading the wallet child status…"
|
const val READING_CHILD_NOTICE: String = "Reading the wallet child status…"
|
||||||
|
|
||||||
|
const val STARTING_CHILD_NOTICE: String =
|
||||||
|
"Start is running. Status stays off until the on-device node answers — that can take a minute, and is not a dead button."
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The caveat the on-device node is offered with, **before** Start.
|
* The caveat the on-device node is offered with, **before** Start.
|
||||||
*
|
*
|
||||||
|
|
@ -388,8 +391,10 @@ fun startBlockedReason(
|
||||||
bins: WalletChildBinaryPaths,
|
bins: WalletChildBinaryPaths,
|
||||||
input: WalletChildInput?,
|
input: WalletChildInput?,
|
||||||
onionDaemon: Boolean = false,
|
onionDaemon: Boolean = false,
|
||||||
|
starting: Boolean = false,
|
||||||
): String? = when {
|
): String? = when {
|
||||||
bins.walletRpc == null -> NO_WALLET_RPC_MESSAGE
|
bins.walletRpc == null -> NO_WALLET_RPC_MESSAGE
|
||||||
|
starting -> STARTING_CHILD_NOTICE
|
||||||
input == null -> READING_CHILD_NOTICE
|
input == null -> READING_CHILD_NOTICE
|
||||||
input.running -> "The wallet child is already running."
|
input.running -> "The wallet child is already running."
|
||||||
!input.configured -> "No chain source yet: a wallet child needs a daemon to dial."
|
!input.configured -> "No chain source yet: a wallet child needs a daemon to dial."
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,7 @@ fun WalletScreen(
|
||||||
val friends by vm.friends.collectAsStateWithLifecycle()
|
val friends by vm.friends.collectAsStateWithLifecycle()
|
||||||
val child by vm.walletChild.collectAsStateWithLifecycle()
|
val child by vm.walletChild.collectAsStateWithLifecycle()
|
||||||
val childError by vm.walletChildError.collectAsStateWithLifecycle()
|
val childError by vm.walletChildError.collectAsStateWithLifecycle()
|
||||||
|
val starting by vm.walletChildStarting.collectAsStateWithLifecycle()
|
||||||
val chain by vm.chainSource.collectAsStateWithLifecycle()
|
val chain by vm.chainSource.collectAsStateWithLifecycle()
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
// Resolved once per composition: the native library directory is fixed for
|
// Resolved once per composition: the native library directory is fixed for
|
||||||
|
|
@ -259,7 +260,8 @@ fun WalletScreen(
|
||||||
.onFailure { daemonError = it.message ?: it.toString() }
|
.onFailure { daemonError = it.message ?: it.toString() }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onStart = { scope.launch { vm.startWalletChild() } },
|
starting = starting,
|
||||||
|
onStart = { vm.startWalletChild() },
|
||||||
onStop = { scope.launch { vm.stopWalletChild() } },
|
onStop = { scope.launch { vm.stopWalletChild() } },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -616,6 +618,7 @@ private fun WalletChildCard(
|
||||||
onSaveDaemon: () -> Unit,
|
onSaveDaemon: () -> Unit,
|
||||||
onReceiveOnly: () -> Unit,
|
onReceiveOnly: () -> Unit,
|
||||||
onInProcessNode: () -> Unit,
|
onInProcessNode: () -> Unit,
|
||||||
|
starting: Boolean = false,
|
||||||
onStart: () -> Unit,
|
onStart: () -> Unit,
|
||||||
onStop: () -> Unit,
|
onStop: () -> Unit,
|
||||||
) {
|
) {
|
||||||
|
|
@ -626,7 +629,7 @@ private fun WalletChildCard(
|
||||||
// read comes back: the warning and the storage line must not wait for the
|
// read comes back: the warning and the storage line must not wait for the
|
||||||
// poller, or the user would press Start having seen neither.
|
// poller, or the user would press Start having seen neither.
|
||||||
val inProcess = input?.chainInProcess == true || chain?.kind == ChainChoice.IN_PROCESS_CUPRATE
|
val inProcess = input?.chainInProcess == true || chain?.kind == ChainChoice.IN_PROCESS_CUPRATE
|
||||||
val startWhy = startBlockedReason(bins, input, onionDaemon)
|
val startWhy = startBlockedReason(bins, input, onionDaemon, starting)
|
||||||
?: if (inProcess) chainStorageBlockedReason(nodeFreeBytes) else null
|
?: if (inProcess) chainStorageBlockedReason(nodeFreeBytes) else null
|
||||||
val stopWhy = stopBlockedReason(input)
|
val stopWhy = stopBlockedReason(input)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -183,6 +183,14 @@ class WalletChildStatusTextTest {
|
||||||
assertNull(startBlockedReason(bundled, input("off")))
|
assertNull(startBlockedReason(bundled, input("off")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun start_is_blocked_while_start_is_in_flight() {
|
||||||
|
assertEquals(
|
||||||
|
STARTING_CHILD_NOTICE,
|
||||||
|
startBlockedReason(bundled, input("off"), starting = true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun start_is_allowed_again_after_the_child_exited() {
|
fun start_is_allowed_again_after_the_child_exited() {
|
||||||
assertNull(
|
assertNull(
|
||||||
|
|
|
||||||
|
|
@ -609,7 +609,10 @@ impl Wire {
|
||||||
///
|
///
|
||||||
/// The spawn itself is synchronous inside the node: argv is built, the
|
/// The spawn itself is synchronous inside the node: argv is built, the
|
||||||
/// process is spawned, and a `.onion` source waits up to 20s for the SOCKS
|
/// process is spawned, and a `.onion` source waits up to 20s for the SOCKS
|
||||||
/// listener. Call it off the main thread, like `send_tip`.
|
/// listener. An in-process Cuprate launch is the same shape and can take
|
||||||
|
/// much longer. That work runs on `spawn_blocking` so it does not stall the
|
||||||
|
/// UniFFI tokio worker (Android: a blocked worker is a Start tap that never
|
||||||
|
/// returns).
|
||||||
///
|
///
|
||||||
/// Idempotent: a live child is reported, not respawned. Fails with
|
/// Idempotent: a live child is reported, not respawned. Fails with
|
||||||
/// `WireError.Failed` when no chain source is configured
|
/// `WireError.Failed` when no chain source is configured
|
||||||
|
|
@ -621,7 +624,11 @@ impl Wire {
|
||||||
/// and call [`Wire::wallet_status`] (or start again) for the later states.
|
/// and call [`Wire::wallet_status`] (or start again) for the later states.
|
||||||
pub async fn start_wallet_child(&self) -> WResult<WalletChildStatus> {
|
pub async fn start_wallet_child(&self) -> WResult<WalletChildStatus> {
|
||||||
let node = self.node()?;
|
let node = self.node()?;
|
||||||
node.start_wallet_child().map_err(WireError::new)?;
|
let blocking = node.clone();
|
||||||
|
tokio::task::spawn_blocking(move || blocking.start_wallet_child())
|
||||||
|
.await
|
||||||
|
.map_err(|e| WireError::new(format!("start wallet child join: {e}")))?
|
||||||
|
.map_err(WireError::new)?;
|
||||||
let source = node.wallet_child_config().source;
|
let source = node.wallet_child_config().source;
|
||||||
Ok(child_status(node.wallet_child_refresh().await, &source))
|
Ok(child_status(node.wallet_child_refresh().await, &source))
|
||||||
}
|
}
|
||||||
|
|
@ -1337,11 +1344,7 @@ mod tests {
|
||||||
Some("sending")
|
Some("sending")
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
delivery_of(
|
delivery_of("out", Some(OutboxStatus::Failed("no route".into()))).as_deref(),
|
||||||
"out",
|
|
||||||
Some(OutboxStatus::Failed("no route".into()))
|
|
||||||
)
|
|
||||||
.as_deref(),
|
|
||||||
Some("failed: no route")
|
Some("failed: no route")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -1492,8 +1495,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn wallet_recent_transfers_on_stopped_handle_is_stopped_error() {
|
fn wallet_recent_transfers_on_stopped_handle_is_stopped_error() {
|
||||||
let err =
|
let err = block_on(stopped_wire().wallet_recent_transfers(8)).expect_err("must fail");
|
||||||
block_on(stopped_wire().wallet_recent_transfers(8)).expect_err("must fail");
|
|
||||||
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
|
assert!(matches!(err, WireError::Stopped { .. }), "got {err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
10
src/node.rs
10
src/node.rs
|
|
@ -617,10 +617,12 @@ impl Node {
|
||||||
cfg.build(self.home.join("wallet"))
|
cfg.build(self.home.join("wallet"))
|
||||||
.map_err(|e| e.to_string())?,
|
.map_err(|e| e.to_string())?,
|
||||||
);
|
);
|
||||||
let client = {
|
// Do not hold SQLCipher across Cuprate/`monerod` launch: `wallet_status`
|
||||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
// also locks the store, and a blocked poller is how Android Start looks
|
||||||
child.start(&store).map_err(|e| e.to_string())?
|
// like it did nothing.
|
||||||
};
|
let client = child
|
||||||
|
.start_unlocking_store(&self.store)
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
let Some(client) = client else {
|
let Some(client) = client else {
|
||||||
return Err("chain source is receive-only — nothing to start".into());
|
return Err("chain source is receive-only — nothing to start".into());
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -435,13 +435,36 @@ impl WalletChild {
|
||||||
/// what gates every spawn: the SOCKS child, like the wallet child, is only
|
/// what gates every spawn: the SOCKS child, like the wallet child, is only
|
||||||
/// reachable through an unlocked store.
|
/// reachable through an unlocked store.
|
||||||
pub fn start(&self, store: &Store) -> Result<Option<WalletClient>> {
|
pub fn start(&self, store: &Store) -> Result<Option<WalletClient>> {
|
||||||
|
self.start_with_creds(|| self.creds(store))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as [`WalletChild::start`], but the store mutex is taken only for
|
||||||
|
/// the generated RPC creds — not across chain-daemon launch.
|
||||||
|
///
|
||||||
|
/// Cuprate's `Node::launch` (and a slow `monerod`) can run for a long time.
|
||||||
|
/// Holding SQLCipher for that whole window freezes every `wallet_status`
|
||||||
|
/// poll, which on Android makes **Start** look like a no-op: the button
|
||||||
|
/// fires, status stays `off`, no error.
|
||||||
|
pub fn start_unlocking_store(&self, store: &Mutex<Store>) -> Result<Option<WalletClient>> {
|
||||||
|
self.start_with_creds(|| {
|
||||||
|
let guard = store
|
||||||
|
.lock()
|
||||||
|
.map_err(|e| ChildError(format!("store lock: {e}")))?;
|
||||||
|
self.creds(&guard)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_with_creds(
|
||||||
|
&self,
|
||||||
|
creds: impl FnOnce() -> Result<(String, Zeroizing<String>)>,
|
||||||
|
) -> Result<Option<WalletClient>> {
|
||||||
if !self.is_enabled() {
|
if !self.is_enabled() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
if self.running() {
|
if self.running() {
|
||||||
return Err(ChildError("wallet child already running".into()));
|
return Err(ChildError("wallet child already running".into()));
|
||||||
}
|
}
|
||||||
match self.start_children(store) {
|
match self.start_children(creds) {
|
||||||
Ok(client) => Ok(client),
|
Ok(client) => Ok(client),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// A failure after the SOCKS child came up must not leave the
|
// A failure after the SOCKS child came up must not leave the
|
||||||
|
|
@ -452,7 +475,10 @@ impl WalletChild {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_children(&self, store: &Store) -> Result<Option<WalletClient>> {
|
fn start_children(
|
||||||
|
&self,
|
||||||
|
creds: impl FnOnce() -> Result<(String, Zeroizing<String>)>,
|
||||||
|
) -> Result<Option<WalletClient>> {
|
||||||
// Local chain source: own the daemon first, so the wallet child has
|
// Local chain source: own the daemon first, so the wallet child has
|
||||||
// something to dial. Either a spawned `monerod` or the in-process
|
// something to dial. Either a spawned `monerod` or the in-process
|
||||||
// Cuprate node — both bind `DEFAULT_MONEROD_RPC_PORT` on loopback, so
|
// Cuprate node — both bind `DEFAULT_MONEROD_RPC_PORT` on loopback, so
|
||||||
|
|
@ -485,7 +511,10 @@ impl WalletChild {
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (user, pass) = self.creds(store)?;
|
// Creds are the only store read. Callers that pass
|
||||||
|
// `start_unlocking_store` take the mutex here, after the chain daemon
|
||||||
|
// is already up, so a long Cuprate launch does not freeze SQLCipher.
|
||||||
|
let (user, pass) = creds()?;
|
||||||
let port = Self::pick_port()?;
|
let port = Self::pick_port()?;
|
||||||
let login = format!("{user}:{}", pass.as_str());
|
let login = format!("{user}:{}", pass.as_str());
|
||||||
let daemon = Self::daemon_address(&self.source);
|
let daemon = Self::daemon_address(&self.source);
|
||||||
|
|
@ -1135,6 +1164,14 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn start_unlocking_store_is_the_same_as_start_for_receive_only() {
|
||||||
|
let dir = tmp_dir("none-unlock");
|
||||||
|
let store = std::sync::Mutex::new(open_store(&dir));
|
||||||
|
let child = WalletChild::new(ChainSource::None, dir.join("wallet"), None).unwrap();
|
||||||
|
assert!(child.start_unlocking_store(&store).unwrap().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
/// A live listener for the readiness gate to dial. `arti proxy -p <port>`
|
/// A live listener for the readiness gate to dial. `arti proxy -p <port>`
|
||||||
/// binds localhost only, so 127.0.0.1 is exactly what the gate probes.
|
/// binds localhost only, so 127.0.0.1 is exactly what the gate probes.
|
||||||
fn held_loopback_port() -> (std::net::TcpListener, u16) {
|
fn held_loopback_port() -> (std::net::TcpListener, u16) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue