From a0dc14bea8a7cdd3e2728b5912e08aa2fe9a8abc Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Wed, 27 Mar 2024 16:38:30 +0100 Subject: [PATCH] fix: Add more resilience when getting the node from state We can get a price before the node has started, since we are already connected to the coordinator. If that happens the app would crash since the node state has not been set yet. This change makes a couple of apis more resilient towards the node not being available yet and returns a logical default value. IMO this is a safe and better approach than simply crashing the app. --- mobile/native/src/ln_dlc/mod.rs | 45 ++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index 6729c42da..bd0419ec3 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -647,17 +647,26 @@ pub async fn close_channel(is_force_close: bool) -> Result<()> { } pub fn get_signed_dlc_channels() -> Result> { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(vec![]), + }; node.inner.list_signed_dlc_channels() } pub fn get_onchain_balance() -> Balance { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Balance::default(), + }; node.inner.get_on_chain_balance() } pub fn get_usable_dlc_channel_balance() -> Result { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(Amount::ZERO), + }; node.inner.get_dlc_channels_usable_balance() } @@ -859,14 +868,20 @@ fn update_state_after_collab_revert( } pub fn get_signed_dlc_channel() -> Result> { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(None), + }; let signed_channels = node.inner.list_signed_dlc_channels()?; Ok(signed_channels.first().cloned()) } pub fn list_dlc_channels() -> Result> { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(vec![]), + }; let dlc_channels = node.inner.list_dlc_channels()?; @@ -884,7 +899,10 @@ pub fn delete_dlc_channel(dlc_channel_id: &DlcChannelId) -> Result<()> { } pub fn is_dlc_channel_confirmed() -> Result { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(false), + }; let dlc_channel = match get_signed_dlc_channel()? { Some(dlc_channel) => dlc_channel, @@ -895,12 +913,18 @@ pub fn is_dlc_channel_confirmed() -> Result { } pub fn get_fee_rate_for_target(target: ConfirmationTarget) -> FeeRate { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return FeeRate::default_min_relay_fee(), + }; node.inner.fee_rate_estimator.get(target) } pub fn estimated_fee_reserve() -> Result { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(Amount::ZERO), + }; // Here we assume that the coordinator will use the same confirmation target AND that their fee // rate source agrees with ours. @@ -929,7 +953,10 @@ pub async fn send_payment(amount: u64, address: String, fee: Fee) -> Result Result { - let node = state::get_node(); + let node = match state::try_get_node() { + Some(node) => node, + None => return Ok(Amount::ZERO), + }; // Here we assume that the coordinator will use the same confirmation target AND that // their fee rate source agrees with ours.