Expose the fee paid on a payment in stdout
[ldk-sample] / src / main.rs
index b85fb4baad293aec773cf9abcad5dd636c0abd8f..71b2cdc7c38e3cf63ca51aeb071de1e09c9e5fe7 100644 (file)
@@ -25,7 +25,7 @@ use lightning::ln::channelmanager::{
 use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler, SimpleArcPeerManager};
 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
 use lightning::routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
-use lightning::routing::scorer::Scorer;
+use lightning::routing::scoring::Scorer;
 use lightning::util::config::UserConfig;
 use lightning::util::events::{Event, PaymentPurpose};
 use lightning::util::ser::ReadableArgs;
@@ -195,16 +195,21 @@ async fn handle_ldk_events(
                                }
                        }
                }
-               Event::PaymentSent { payment_preimage, payment_hash, .. } => {
+               Event::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
                        let mut payments = outbound_payments.lock().unwrap();
                        for (hash, payment) in payments.iter_mut() {
                                if *hash == *payment_hash {
                                        payment.preimage = Some(*payment_preimage);
                                        payment.status = HTLCStatus::Succeeded;
                                        println!(
-                                               "\nEVENT: successfully sent payment of {} millisatoshis from \
+                                               "\nEVENT: successfully sent payment of {} millisatoshis{} from \
                                                                 payment hash {:?} with preimage {:?}",
                                                payment.amt_msat,
+                                               if let Some(fee) = fee_paid_msat {
+                                                       format!(" (fee {} msat)", fee)
+                                               } else {
+                                                       "".to_string()
+                                               },
                                                hex_utils::hex_str(&payment_hash.0),
                                                hex_utils::hex_str(&payment_preimage.0)
                                        );
@@ -213,26 +218,13 @@ async fn handle_ldk_events(
                                }
                        }
                }
-               Event::PaymentPathFailed {
-                       payment_hash,
-                       rejected_by_dest,
-                       all_paths_failed,
-                       short_channel_id,
-                       ..
-               } => {
+               Event::PaymentPathSuccessful { .. } => {}
+               Event::PaymentPathFailed { .. } => {}
+               Event::PaymentFailed { payment_hash, .. } => {
                        print!(
-                               "\nEVENT: Failed to send payment{} to payment hash {:?}",
-                               if *all_paths_failed { "" } else { " along MPP path" },
+                               "\nEVENT: Failed to send payment to payment hash {:?}: exhausted payment retry attempts",
                                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");
-                       } else {
-                               println!(": exhausted payment retry attempts");
-                       }
                        print!("> ");
                        io::stdout().flush().unwrap();
 
@@ -596,9 +588,26 @@ async fn start_ldk() {
                ));
        };
 
-       // Step 16: Create InvoicePayer
+       // Step 16: Initialize routing Scorer
+       let scorer_path = format!("{}/scorer", ldk_data_dir.clone());
+       let scorer = Arc::new(Mutex::new(disk::read_scorer(Path::new(&scorer_path))));
+       let scorer_persist = Arc::clone(&scorer);
+       tokio::spawn(async move {
+               let mut interval = tokio::time::interval(Duration::from_secs(600));
+               loop {
+                       interval.tick().await;
+                       if disk::persist_scorer(Path::new(&scorer_path), &scorer_persist.lock().unwrap())
+                               .is_err()
+                       {
+                               // Persistence errors here are non-fatal as channels will be re-scored as payments
+                               // fail, but they may indicate a disk error which could be fatal elsewhere.
+                               eprintln!("Warning: Failed to persist scorer, check your disk and permissions");
+                       }
+               }
+       });
+
+       // Step 17: Create InvoicePayer
        let router = DefaultRouter::new(network_graph.clone(), logger.clone());
-       let scorer = Arc::new(Mutex::new(Scorer::default()));
        let invoice_payer = Arc::new(InvoicePayer::new(
                channel_manager.clone(),
                router,
@@ -608,12 +617,12 @@ async fn start_ldk() {
                payment::RetryAttempts(5),
        ));
 
-       // Step 17: Persist ChannelManager
+       // Step 18: Persist ChannelManager
        let data_dir = ldk_data_dir.clone();
        let persist_channel_manager_callback =
                move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node);
 
-       // Step 18: Background Processing
+       // Step 19: Background Processing
        let background_processor = BackgroundProcessor::start(
                persist_channel_manager_callback,
                invoice_payer.clone(),
@@ -624,22 +633,39 @@ async fn start_ldk() {
                logger.clone(),
        );
 
-       // Reconnect to channel peers if possible.
+       // Regularly reconnect to channel peers.
+       let connect_cm = Arc::clone(&channel_manager);
+       let connect_pm = Arc::clone(&peer_manager);
        let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
-       match disk::read_channel_peer_data(Path::new(&peer_data_path)) {
-               Ok(mut info) => {
-                       for (pubkey, peer_addr) in info.drain() {
-                               for chan_info in channel_manager.list_channels() {
-                                       if pubkey == chan_info.counterparty.node_id {
-                                               let _ =
-                                                       cli::connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone())
+       tokio::spawn(async move {
+               let mut interval = tokio::time::interval(Duration::from_secs(1));
+               loop {
+                       interval.tick().await;
+                       match disk::read_channel_peer_data(Path::new(&peer_data_path)) {
+                               Ok(info) => {
+                                       let peers = connect_pm.get_peer_node_ids();
+                                       for node_id in connect_cm
+                                               .list_channels()
+                                               .iter()
+                                               .map(|chan| chan.counterparty.node_id)
+                                               .filter(|id| !peers.contains(id))
+                                       {
+                                               for (pubkey, peer_addr) in info.iter() {
+                                                       if *pubkey == node_id {
+                                                               let _ = cli::do_connect_peer(
+                                                                       *pubkey,
+                                                                       peer_addr.clone(),
+                                                                       Arc::clone(&connect_pm),
+                                                               )
                                                                .await;
+                                                       }
+                                               }
                                        }
                                }
+                               Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
                        }
                }
-               Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
-       }
+       });
 
        // Regularly broadcast our node_announcement. This is only required (or possible) if we have
        // some public channels, and is only useful if we have public listen address(es) to announce.