X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain.rs;h=9e82f8aa76f079235c8ca4ac87700bab4f77798f;hb=16acfaf7655a9ad2eeddacde0675126ec6795303;hp=d24fcad81177ac75e66016a1b55d9e2f72807c0a;hpb=9c10e5533db6829deeaf26f396fc5dd493baad6c;p=ldk-sample diff --git a/src/main.rs b/src/main.rs index d24fcad..9e82f8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -138,10 +136,24 @@ async fn handle_ldk_events( let final_tx: Transaction = encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap(); // Give the funding transaction back to LDK for opening the channel. - channel_manager.funding_transaction_generated(&temporary_channel_id, final_tx).unwrap(); + if channel_manager + .funding_transaction_generated(&temporary_channel_id, final_tx) + .is_err() + { + println!( + "\nERROR: Channel went away before we could fund it. The peer disconnected or refused the channel."); + print!("> "); + 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!( @@ -159,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)), }); @@ -210,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 { @@ -339,7 +368,8 @@ async fn start_ldk() { let mut channelmonitors = persister.read_channelmonitors(keys_manager.clone()).unwrap(); // Step 8: Initialize the ChannelManager - let user_config = UserConfig::default(); + let mut user_config = UserConfig::default(); + user_config.peer_channel_config_limits.force_announced_channel_preference = false; let mut restarting_node = true; let (channel_manager_blockhash, mut channel_manager) = { if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) { @@ -536,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(), @@ -568,7 +598,7 @@ async fn start_ldk() { // to avoid churn in the global network graph. let chan_manager = Arc::clone(&channel_manager); let network = args.network; - if args.ldk_announced_listen_addr.is_some() { + if !args.ldk_announced_listen_addr.is_empty() { tokio::spawn(async move { let mut interval = tokio::time::interval(Duration::from_secs(60)); loop { @@ -576,7 +606,7 @@ async fn start_ldk() { chan_manager.broadcast_node_announcement( [0; 3], args.ldk_announced_node_name, - vec![args.ldk_announced_listen_addr.as_ref().unwrap().clone()], + args.ldk_announced_listen_addr.clone(), ); } }); @@ -595,6 +625,9 @@ async fn start_ldk() { network, ) .await; + + // Stop the background processor. + background_processor.stop().unwrap(); } #[tokio::main]