From 8984205acf1d328837d0f33798e89e479aea1882 Mon Sep 17 00:00:00 2001 From: Max Fang Date: Tue, 4 Oct 2022 19:53:09 -0700 Subject: [PATCH] Make `ldk-sample` `Send` + `Sync` This makes it possible to run ldk-sample in a tokio task, e.g. ```rust pub async fn main() { tokio::spawn(async move { start_ldk().await; }) .await .expect("LDK panicked") } ``` --- src/cli.rs | 13 ++++++------- src/main.rs | 12 ++++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index f50d3f9..7d86515 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,7 +19,7 @@ use lightning_invoice::payment::PaymentError; use lightning_invoice::{utils, Currency, Invoice}; use std::env; use std::io; -use std::io::{BufRead, Write}; +use std::io::Write; use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; use std::ops::Deref; use std::path::Path; @@ -150,15 +150,14 @@ pub(crate) async fn poll_for_user_input( println!("LDK startup successful. To view available commands: \"help\"."); println!("LDK logs are available at /.ldk/logs"); println!("Local Node ID is {}.", channel_manager.get_our_node_id()); - let stdin = io::stdin(); - let mut line_reader = stdin.lock().lines(); loop { print!("> "); io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print - let line = match line_reader.next() { - Some(l) => l.unwrap(), - None => break, - }; + let mut line = String::new(); + if let Err(e) = io::stdin().read_line(&mut line) { + break println!("ERROR: {e:#}"); + } + let mut words = line.split_whitespace(); if let Some(word) = words.next() { match word { diff --git a/src/main.rs b/src/main.rs index 9c85b70..6cff9f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -509,8 +509,10 @@ async fn start_ldk() { let mut cache = UnboundedCache::new(); let mut chain_tip: Option = None; if restarting_node { - let mut chain_listeners = - vec![(channel_manager_blockhash, &channel_manager as &dyn chain::Listen)]; + let mut chain_listeners = vec![( + channel_manager_blockhash, + &channel_manager as &(dyn chain::Listen + Send + Sync), + )]; for (blockhash, channel_monitor) in channelmonitors.drain(..) { let outpoint = channel_monitor.get_funding_txo().0; @@ -522,8 +524,10 @@ async fn start_ldk() { } for monitor_listener_info in chain_listener_channel_monitors.iter_mut() { - chain_listeners - .push((monitor_listener_info.0, &monitor_listener_info.1 as &dyn chain::Listen)); + chain_listeners.push(( + monitor_listener_info.0, + &monitor_listener_info.1 as &(dyn chain::Listen + Send + Sync), + )); } chain_tip = Some( init::synchronize_listeners( -- 2.39.5