]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Make `ldk-sample` `Send` + `Sync`
authorMax Fang <protonmail.com@maxfa.ng>
Wed, 5 Oct 2022 02:53:09 +0000 (19:53 -0700)
committerMax Fang <protonmail.com@maxfa.ng>
Wed, 5 Oct 2022 08:58:53 +0000 (01:58 -0700)
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
src/main.rs

index f50d3f9fa175aca2e18b49b43d9e93386829a80f..7d865158e6e7e786a93b52ff99a513d766f50b08 100644 (file)
@@ -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<E: EventHandler>(
        println!("LDK startup successful. To view available commands: \"help\".");
        println!("LDK logs are available at <your-supplied-ldk-data-dir-path>/.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 {
index 9c85b70c8cf186623c16130733b76706c4fe434f..6cff9f152492058c156e7fb1cf48ccb4e3910f38 100644 (file)
@@ -509,8 +509,10 @@ async fn start_ldk() {
        let mut cache = UnboundedCache::new();
        let mut chain_tip: Option<poll::ValidatedBlockHeader> = 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(