Manually accept inbound channels
[ldk-sample] / src / main.rs
index 2012921c334f6a6930693899c11a7549574df601..ba9d8fcb19c644473bd5d3f8a07af782e90857ef 100644 (file)
@@ -278,8 +278,34 @@ async fn handle_ldk_events(
                        }
                        persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound).unwrap();
                }
-               Event::OpenChannelRequest { .. } => {
-                       // Unreachable, we don't set manually_accept_inbound_channels
+               Event::OpenChannelRequest {
+                       ref temporary_channel_id, ref counterparty_node_id, ..
+               } => {
+                       let mut random_bytes = [0u8; 16];
+                       random_bytes.copy_from_slice(&keys_manager.get_secure_random_bytes()[..16]);
+                       let user_channel_id = u128::from_be_bytes(random_bytes);
+                       let res = channel_manager.accept_inbound_channel(
+                               temporary_channel_id,
+                               counterparty_node_id,
+                               user_channel_id,
+                       );
+
+                       if let Err(e) = res {
+                               print!(
+                                       "\nEVENT: Failed to accept inbound channel ({}) from {}: {:?}",
+                                       hex_utils::hex_str(&temporary_channel_id[..]),
+                                       hex_utils::hex_str(&counterparty_node_id.serialize()),
+                                       e,
+                               );
+                       } else {
+                               print!(
+                                       "\nEVENT: Accepted inbound channel ({}) from {}",
+                                       hex_utils::hex_str(&temporary_channel_id[..]),
+                                       hex_utils::hex_str(&counterparty_node_id.serialize()),
+                               );
+                       }
+                       print!("> ");
+                       io::stdout().flush().unwrap();
                }
                Event::PaymentPathSuccessful { .. } => {}
                Event::PaymentPathFailed { .. } => {}
@@ -566,6 +592,7 @@ async fn start_ldk() {
        // Step 11: Initialize the ChannelManager
        let mut user_config = UserConfig::default();
        user_config.channel_handshake_limits.force_announced_channel_preference = false;
+       user_config.manually_accept_inbound_channels = true;
        let mut restarting_node = true;
        let (channel_manager_blockhash, channel_manager) = {
                if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) {
@@ -802,7 +829,7 @@ async fn start_ldk() {
 
        // Step 20: Background Processing
        let (bp_exit, bp_exit_check) = tokio::sync::watch::channel(());
-       let background_processor = tokio::spawn(process_events_async(
+       let mut background_processor = tokio::spawn(process_events_async(
                Arc::clone(&persister),
                event_handler,
                chain_monitor.clone(),
@@ -898,7 +925,7 @@ async fn start_ldk() {
        ));
 
        // Start the CLI.
-       cli::poll_for_user_input(
+       let cli_poll = tokio::spawn(cli::poll_for_user_input(
                Arc::clone(&peer_manager),
                Arc::clone(&channel_manager),
                Arc::clone(&keys_manager),
@@ -910,8 +937,18 @@ async fn start_ldk() {
                network,
                Arc::clone(&logger),
                Arc::clone(&persister),
-       )
-       .await;
+       ));
+
+       // Exit if either CLI polling exits or the background processor exits (which shouldn't happen
+       // unless we fail to write to the filesystem).
+       tokio::select! {
+               _ = cli_poll => {},
+               bg_res = &mut background_processor => {
+                       stop_listen_connect.store(true, Ordering::Release);
+                       peer_manager.disconnect_all_peers();
+                       panic!("ERR: background processing stopped with result {:?}, exiting", bg_res);
+               },
+       }
 
        // Disconnect our peers and stop accepting new connections. This ensures we don't continue
        // updating our channel data after we've stopped the background processor.
@@ -919,8 +956,10 @@ async fn start_ldk() {
        peer_manager.disconnect_all_peers();
 
        // Stop the background processor.
-       bp_exit.send(()).unwrap();
-       background_processor.await.unwrap().unwrap();
+       if !bp_exit.is_closed() {
+               bp_exit.send(()).unwrap();
+               background_processor.await.unwrap().unwrap();
+       }
 }
 
 #[tokio::main]