Skip to content

Commit

Permalink
fix: Add more resilience when getting the node from state
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
holzeis committed Apr 1, 2024
1 parent 44af564 commit a0dc14b
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,17 +647,26 @@ pub async fn close_channel(is_force_close: bool) -> Result<()> {
}

pub fn get_signed_dlc_channels() -> Result<Vec<SignedChannel>> {
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<Amount> {
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()
}

Expand Down Expand Up @@ -859,14 +868,20 @@ fn update_state_after_collab_revert(
}

pub fn get_signed_dlc_channel() -> Result<Option<SignedChannel>> {
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<Vec<DlcChannel>> {
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()?;

Expand All @@ -884,7 +899,10 @@ pub fn delete_dlc_channel(dlc_channel_id: &DlcChannelId) -> Result<()> {
}

pub fn is_dlc_channel_confirmed() -> Result<bool> {
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,
Expand All @@ -895,12 +913,18 @@ pub fn is_dlc_channel_confirmed() -> Result<bool> {
}

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<Amount> {
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.
Expand Down Expand Up @@ -929,7 +953,10 @@ pub async fn send_payment(amount: u64, address: String, fee: Fee) -> Result<Txid
}

pub fn estimated_funding_tx_fee() -> Result<Amount> {
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.
Expand Down

0 comments on commit a0dc14b

Please sign in to comment.