Skip to content

Commit

Permalink
Merge pull request #1825 from get10101/chore/show-recovery-dialog-on-…
Browse files Browse the repository at this point in the history
…interrupted-protocol

chore: Publish recover dlc background task
  • Loading branch information
holzeis authored Jan 17, 2024
2 parents a10af11 + 5442e10 commit f6288bf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 695 deletions.
34 changes: 33 additions & 1 deletion mobile/native/src/dlc_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::db;
use crate::event;
use crate::event::BackgroundTask;
use crate::event::EventInternal;
use crate::event::TaskStatus;
use crate::ln_dlc::node::NodeStorage;
use crate::storage::TenTenOneNodeStorage;
use anyhow::Result;
Expand Down Expand Up @@ -94,10 +98,19 @@ impl DlcHandler {
}) => {
tracing::info!("Accepting pending dlc channel offer.");
// Pending dlc channel offer not yet confirmed on-chain

event::publish(&EventInternal::BackgroundNotification(
BackgroundTask::RecoverDlc(TaskStatus::Pending),
));

if let Err(e) = self.node.accept_dlc_channel_offer(temporary_channel_id) {
tracing::error!("Failed to accept pending dlc channel offer. {e:#}");
tracing::warn!("Rejecting pending dlc channel offer!");
self.node.reject_dlc_channel_offer(temporary_channel_id)?;

event::publish(&EventInternal::BackgroundNotification(
BackgroundTask::RecoverDlc(TaskStatus::Success),
));
}

return Ok(());
Expand All @@ -110,6 +123,11 @@ impl DlcHandler {
tracing::info!("Accepting pending dlc channel settle offer.");
// Pending dlc channel settle offer with a dlc channel already confirmed
// on-chain

event::publish(&EventInternal::BackgroundNotification(
BackgroundTask::RecoverDlc(TaskStatus::Pending),
));

self.node
.accept_dlc_channel_collaborative_settlement(channel_id)?;

Expand Down Expand Up @@ -142,8 +160,22 @@ impl DlcHandler {

return Ok(());
}
Channel::Signed(signed_channel) => {
// If the signed channel state is anything else but `Established`, `Settled` or
// `Closing` at reconnect. It means the protocol got interrupted.
if !matches!(
signed_channel.state,
SignedChannelState::Established { .. }
| SignedChannelState::Settled { .. }
| SignedChannelState::Closing { .. }
) {
event::publish(&EventInternal::BackgroundNotification(
BackgroundTask::RecoverDlc(TaskStatus::Pending),
));
}
}
_ => {}
}
};
}

let mut conn = db::connection()?;
Expand Down
47 changes: 1 addition & 46 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ use trade::ContractSymbol;

mod lightning_subscriber;
pub mod node;
mod recover_rollover;
mod sync_position_to_subchannel;

pub mod channel_status;

Expand All @@ -115,7 +113,6 @@ const PROCESS_INCOMING_DLC_MESSAGES_INTERVAL: Duration = Duration::from_millis(2
const UPDATE_WALLET_HISTORY_INTERVAL: Duration = Duration::from_secs(5);
const CHECK_OPEN_ORDERS_INTERVAL: Duration = Duration::from_secs(60);
const ON_CHAIN_SYNC_INTERVAL: Duration = Duration::from_secs(300);
const WAIT_FOR_CONNECTING_TO_COORDINATOR: Duration = Duration::from_secs(2);

/// Defines a constant from which we treat a transaction as confirmed
const NUMBER_OF_CONFIRMATION_FOR_BEING_CONFIRMED: u64 = 1;
Expand Down Expand Up @@ -383,9 +380,8 @@ pub fn run(seed_dir: String, runtime: &Runtime) -> Result<()> {
let node = node.clone();
async move { node.listen_for_lightning_events(event_receiver).await }
});
let coordinator_info = config::get_coordinator_info();
let coordinator_pk = coordinator_info.pubkey;

let coordinator_info = config::get_coordinator_info();
runtime.spawn({
let node = node.clone();
async move { node.keep_connected(coordinator_info).await }
Expand Down Expand Up @@ -419,47 +415,6 @@ pub fn run(seed_dir: String, runtime: &Runtime) -> Result<()> {

runtime.spawn(track_channel_status(node.clone()));

let inner_node = node.clone();

runtime.spawn_blocking(move || {
let node = inner_node.clone();
async move {
let mut iteration_count = 0;

while !node
.inner
.peer_manager
.get_peer_node_ids()
.iter()
.any(|(a, _)| a == &coordinator_pk)
{
tracing::trace!(
"Not yet connecting to coordinator. Waiting with recovery for a connection"
);

tokio::time::sleep(WAIT_FOR_CONNECTING_TO_COORDINATOR).await;

iteration_count += 1;

if iteration_count >= 30 {
// After 30 retries (randonly chosen) we give up and continue with the
// function nevertheless. Which means, we might see
// an error.
break;
}
}
if let Err(e) = node.sync_position_with_subchannel_state().await {
tracing::error!("Failed to sync position with subchannel state. Error: {e:#}");
}

if let Err(e) = node.recover_rollover().await {
tracing::error!(
"Failed to check and recover from a stuck rollover state. Error: {e:#}"
);
}
}
});

state::set_node(node);

event::publish(&EventInternal::Init("10101 is ready.".to_string()));
Expand Down
100 changes: 0 additions & 100 deletions mobile/native/src/ln_dlc/recover_rollover.rs

This file was deleted.

Loading

0 comments on commit f6288bf

Please sign in to comment.