Update dependencies to LDK 0.0.103
[ldk-sample] / src / main.rs
index 955fc4d308e8360c42761501de25fcb9b8e22f3c..b81e652b1906aad8012cc29b2b103cbdba6295d3 100644 (file)
@@ -9,8 +9,6 @@ use crate::disk::FilesystemLogger;
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::consensus::encode;
-use bitcoin::hashes::sha256::Hash as Sha256;
-use bitcoin::hashes::Hash;
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::Secp256k1;
 use bitcoin::BlockHash;
@@ -184,11 +182,10 @@ async fn handle_ldk_events(
                                }
                        }
                }
-               Event::PaymentSent { payment_preimage } => {
-                       let hashed = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
+               Event::PaymentSent { payment_preimage, payment_hash, .. } => {
                        let mut payments = outbound_payments.lock().unwrap();
-                       for (payment_hash, payment) in payments.iter_mut() {
-                               if *payment_hash == hashed {
+                       for (hash, payment) in payments.iter_mut() {
+                               if *hash == *payment_hash {
                                        payment.preimage = Some(*payment_preimage);
                                        payment.status = HTLCStatus::Succeeded;
                                        println!(
@@ -209,16 +206,21 @@ async fn handle_ldk_events(
                        network_update: _,
                        all_paths_failed,
                        path: _,
+                       short_channel_id,
+                       ..
                } => {
                        print!(
-                               "\nEVENT: Failed to send payment{} to payment hash {:?}",
+                               "\nEVENT: Failed to send payment{} to payment hash {:?}",
                                if *all_paths_failed { "" } else { " along MPP path" },
                                hex_utils::hex_str(&payment_hash.0)
                        );
+                       if let Some(scid) = short_channel_id {
+                               print!(" because of failure at channel {}", scid);
+                       }
                        if *rejected_by_dest {
-                               println!("re-attempting the payment will not succeed");
+                               println!("re-attempting the payment will not succeed");
                        } else {
-                               println!("payment may be retried");
+                               println!("payment may be retried");
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
@@ -271,7 +273,7 @@ async fn handle_ldk_events(
                                .unwrap();
                        bitcoind_client.broadcast_transaction(&spending_tx);
                }
-               Event::ChannelClosed { channel_id, reason } => {
+               Event::ChannelClosed { channel_id, reason, user_channel_id: _ } => {
                        println!(
                                "\nEVENT: Channel {} closed due to: {:?}",
                                hex_utils::hex_str(channel_id),
@@ -280,6 +282,10 @@ async fn handle_ldk_events(
                        print!("> ");
                        io::stdout().flush().unwrap();
                }
+               Event::DiscardFunding { .. } => {
+                       // A "real" node should probably "lock" the UTXOs spent in funding transactions until
+                       // the funding transaction either confirms, or this event is generated.
+               }
        }
 }
 
@@ -299,6 +305,7 @@ async fn start_ldk() {
                args.bitcoind_rpc_port,
                args.bitcoind_rpc_username.clone(),
                args.bitcoind_rpc_password.clone(),
+               tokio::runtime::Handle::current(),
        )
        .await
        {
@@ -473,18 +480,18 @@ async fn start_ldk() {
        // Step 11: Optional: Initialize the NetGraphMsgHandler
        let genesis = genesis_block(args.network).header.block_hash();
        let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone());
-       let network_graph = disk::read_network(Path::new(&network_graph_path), genesis);
-       let router = Arc::new(NetGraphMsgHandler::new(
-               network_graph,
+       let network_graph = Arc::new(disk::read_network(Path::new(&network_graph_path), genesis));
+       let network_gossip = Arc::new(NetGraphMsgHandler::new(
+               Arc::clone(&network_graph),
                None::<Arc<dyn chain::Access + Send + Sync>>,
                logger.clone(),
        ));
-       let router_persist = Arc::clone(&router);
+       let network_graph_persist = Arc::clone(&network_graph);
        tokio::spawn(async move {
                let mut interval = tokio::time::interval(Duration::from_secs(600));
                loop {
                        interval.tick().await;
-                       if disk::persist_network(Path::new(&network_graph_path), &router_persist.network_graph)
+                       if disk::persist_network(Path::new(&network_graph_path), &network_graph_persist)
                                .is_err()
                        {
                                // Persistence errors here are non-fatal as we can just fetch the routing graph
@@ -500,8 +507,10 @@ async fn start_ldk() {
        let channel_manager: Arc<ChannelManager> = Arc::new(channel_manager);
        let mut ephemeral_bytes = [0; 32];
        rand::thread_rng().fill_bytes(&mut ephemeral_bytes);
-       let lightning_msg_handler =
-               MessageHandler { chan_handler: channel_manager.clone(), route_handler: router.clone() };
+       let lightning_msg_handler = MessageHandler {
+               chan_handler: channel_manager.clone(),
+               route_handler: network_gossip.clone(),
+       };
        let peer_manager: Arc<PeerManager> = Arc::new(PeerManager::new(
                lightning_msg_handler,
                keys_manager.get_node_secret(),
@@ -585,7 +594,7 @@ async fn start_ldk() {
                event_handler,
                chain_monitor.clone(),
                channel_manager.clone(),
-               Some(router.clone()),
+               Some(network_gossip.clone()),
                peer_manager.clone(),
                logger.clone(),
        );
@@ -632,7 +641,7 @@ async fn start_ldk() {
                peer_manager.clone(),
                channel_manager.clone(),
                keys_manager.clone(),
-               router.clone(),
+               network_graph.clone(),
                inbound_payments,
                outbound_payments,
                ldk_data_dir.clone(),