Drop unused change output calculation
[ldk-sample] / src / main.rs
index f2de6d289e4cc413304506e253edd19a8088602f..9e82f8aa76f079235c8ca4ac87700bab4f77798f 100644 (file)
@@ -28,7 +28,7 @@ use lightning::ln::peer_handler::{MessageHandler, SimpleArcPeerManager};
 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
 use lightning::routing::network_graph::NetGraphMsgHandler;
 use lightning::util::config::UserConfig;
-use lightning::util::events::Event;
+use lightning::util::events::{Event, PaymentPurpose};
 use lightning::util::ser::ReadableArgs;
 use lightning_background_processor::BackgroundProcessor;
 use lightning_block_sync::init;
@@ -129,8 +129,6 @@ async fn handle_ldk_events(
                        // Have your wallet put the inputs into the transaction such that the output is
                        // satisfied.
                        let funded_tx = bitcoind_client.fund_raw_transaction(raw_tx).await;
-                       let change_output_position = funded_tx.changepos;
-                       assert!(change_output_position == 0 || change_output_position == 1);
 
                        // Sign the final funding transaction and broadcast it.
                        let signed_tx = bitcoind_client.sign_raw_transaction_with_wallet(funded_tx.hex).await;
@@ -148,8 +146,14 @@ async fn handle_ldk_events(
                                io::stdout().flush().unwrap();
                        }
                }
-               Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, .. } => {
+               Event::PaymentReceived { payment_hash, purpose, amt, .. } => {
                        let mut payments = inbound_payments.lock().unwrap();
+                       let (payment_preimage, payment_secret) = match purpose {
+                               PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
+                                       (payment_preimage, Some(payment_secret))
+                               }
+                               PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None),
+                       };
                        let status = match channel_manager.claim_funds(payment_preimage.unwrap()) {
                                true => {
                                        println!(
@@ -167,13 +171,13 @@ async fn handle_ldk_events(
                                Entry::Occupied(mut e) => {
                                        let payment = e.get_mut();
                                        payment.status = status;
-                                       payment.preimage = Some(payment_preimage.unwrap());
-                                       payment.secret = Some(payment_secret);
+                                       payment.preimage = payment_preimage;
+                                       payment.secret = payment_secret;
                                }
                                Entry::Vacant(e) => {
                                        e.insert(PaymentInfo {
-                                               preimage: Some(payment_preimage.unwrap()),
-                                               secret: Some(payment_secret),
+                                               preimage: payment_preimage,
+                                               secret: payment_secret,
                                                status,
                                                amt_msat: MillisatAmount(Some(amt)),
                                        });
@@ -218,6 +222,23 @@ async fn handle_ldk_events(
                                payment.status = HTLCStatus::Failed;
                        }
                }
+               Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx } => {
+                       let from_onchain_str = if claim_from_onchain_tx {
+                               "from onchain downstream claim"
+                       } else {
+                               "from HTLC fulfill message"
+                       };
+                       if let Some(fee_earned) = fee_earned_msat {
+                               println!(
+                                       "\nEVENT: Forwarded payment, earning {} msat {}",
+                                       fee_earned, from_onchain_str
+                               );
+                       } else {
+                               println!("\nEVENT: Forwarded payment, claiming onchain {}", from_onchain_str);
+                       }
+                       print!("> ");
+                       io::stdout().flush().unwrap();
+               }
                Event::PendingHTLCsForwardable { time_forwardable } => {
                        let forwarding_channel_manager = channel_manager.clone();
                        tokio::spawn(async move {
@@ -545,7 +566,7 @@ async fn start_ldk() {
        let persist_channel_manager_callback =
                move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node);
        // Step 17: Background Processing
-       BackgroundProcessor::start(
+       let background_processor = BackgroundProcessor::start(
                persist_channel_manager_callback,
                event_handler,
                chain_monitor.clone(),
@@ -604,6 +625,9 @@ async fn start_ldk() {
                network,
        )
        .await;
+
+       // Stop the background processor.
+       background_processor.stop().unwrap();
 }
 
 #[tokio::main]