Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Publish recover dlc background task #1825

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading