X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain.rs;h=6f359756475134d64a693841ad9d1240094850df;hb=5c4d2912e04af1d115c64cca32317e548e3c45a7;hp=a8b28f57d12b5056df3031f88dbcf0fdae31cabb;hpb=371fcdf2f8c018b92070383eb137f7ed6b692faa;p=ldk-sample diff --git a/src/main.rs b/src/main.rs index a8b28f5..6f35975 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,17 +19,16 @@ use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning::chain::chainmonitor; use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager}; -use lightning::chain::Filter; -use lightning::chain::Watch; +use lightning::chain::{BestBlock, Filter, Watch}; use lightning::ln::channelmanager; use lightning::ln::channelmanager::{ - BestBlock, ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager, + ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager, }; -use lightning::ln::peer_handler::{MessageHandler, SimpleArcPeerManager}; +use lightning::ln::peer_handler::{IgnoringMessageHandler, 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; @@ -101,7 +100,7 @@ pub(crate) type ChannelManager = async fn handle_ldk_events( channel_manager: Arc, bitcoind_client: Arc, keys_manager: Arc, inbound_payments: PaymentInfoStorage, - outbound_payments: PaymentInfoStorage, network: Network, event: Event, + outbound_payments: PaymentInfoStorage, network: Network, event: &Event, ) { match event { Event::FundingGenerationReady { @@ -124,14 +123,12 @@ async fn handle_ldk_events( .expect("Lightning funding tx should always be to a SegWit output") .to_address(); let mut outputs = vec![HashMap::with_capacity(1)]; - outputs[0].insert(addr, channel_value_satoshis as f64 / 100_000_000.0); + outputs[0].insert(addr, *channel_value_satoshis as f64 / 100_000_000.0); let raw_tx = bitcoind_client.create_raw_transaction(outputs).await; // 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; @@ -139,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!( @@ -156,19 +167,19 @@ async fn handle_ldk_events( } _ => HTLCStatus::Failed, }; - match payments.entry(payment_hash) { + match payments.entry(*payment_hash) { 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)), + amt_msat: MillisatAmount(Some(*amt)), }); } } @@ -178,7 +189,7 @@ async fn handle_ldk_events( let mut payments = outbound_payments.lock().unwrap(); for (payment_hash, payment) in payments.iter_mut() { if *payment_hash == hashed { - payment.preimage = Some(payment_preimage); + payment.preimage = Some(*payment_preimage); payment.status = HTLCStatus::Succeeded; println!( "\nEVENT: successfully sent payment of {} millisatoshis from \ @@ -192,12 +203,19 @@ async fn handle_ldk_events( } } } - Event::PaymentFailed { payment_hash, rejected_by_dest } => { + Event::PaymentPathFailed { + payment_hash, + rejected_by_dest, + network_update: _, + all_paths_failed, + path: _, + } => { 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 rejected_by_dest { + if *rejected_by_dest { println!("re-attempting the payment will not succeed"); } else { println!("payment may be retried"); @@ -211,10 +229,27 @@ 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(); + let min = time_forwardable.as_millis() as u64; tokio::spawn(async move { - let min = time_forwardable.as_millis() as u64; let millis_to_sleep = thread_rng().gen_range(min, min * 5) as u64; tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await; forwarding_channel_manager.process_pending_htlc_forwards(); @@ -236,6 +271,15 @@ async fn handle_ldk_events( .unwrap(); bitcoind_client.broadcast_transaction(&spending_tx); } + Event::ChannelClosed { channel_id, reason } => { + println!( + "\nEVENT: Channel {} closed due to: {:?}", + hex_utils::hex_str(channel_id), + reason + ); + print!("> "); + io::stdout().flush().unwrap(); + } } } @@ -255,6 +299,7 @@ async fn start_ldk() { args.bitcoind_rpc_port, args.bitcoind_rpc_username.clone(), args.bitcoind_rpc_password.clone(), + tokio::runtime::Handle::current(), ) .await { @@ -340,7 +385,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())) { @@ -426,13 +472,30 @@ async fn start_ldk() { } // Step 11: Optional: Initialize the NetGraphMsgHandler - // XXX persist routing data 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( - genesis, + network_graph, None::>, logger.clone(), )); + let router_persist = Arc::clone(&router); + 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) + .is_err() + { + // Persistence errors here are non-fatal as we can just fetch the routing graph + // again later, but they may indicate a disk error which could be fatal elsewhere. + eprintln!( + "Warning: Failed to persist network graph, check your disk and permissions" + ); + } + } + }); // Step 12: Initialize the PeerManager let channel_manager: Arc = Arc::new(channel_manager); @@ -445,6 +508,7 @@ async fn start_ldk() { keys_manager.get_node_secret(), &ephemeral_bytes, logger.clone(), + Arc::new(IgnoringMessageHandler {}), )); // ## Running LDK @@ -490,7 +554,7 @@ async fn start_ldk() { } }); - // Step 15: Event Handling + // Step 15: Handle LDK Events let channel_manager_event_listener = channel_manager.clone(); let keys_manager_listener = keys_manager.clone(); // TODO: persist payment info to disk @@ -501,7 +565,7 @@ async fn start_ldk() { let network = args.network; let bitcoind_rpc = bitcoind_client.clone(); let handle = tokio::runtime::Handle::current(); - let event_handler = move |event| { + let event_handler = move |event: &Event| { handle.block_on(handle_ldk_events( channel_manager_event_listener.clone(), bitcoind_rpc.clone(), @@ -510,18 +574,19 @@ async fn start_ldk() { outbound_pmts_for_events.clone(), network, event, - )) + )); }; - // Step 16: ChannelManager Persisting + // Step 16: 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 17: Background Processing - BackgroundProcessor::start( + let background_processor = BackgroundProcessor::start( persist_channel_manager_callback, event_handler, chain_monitor.clone(), channel_manager.clone(), + Some(router.clone()), peer_manager.clone(), logger.clone(), ); @@ -532,7 +597,7 @@ async fn start_ldk() { Ok(mut info) => { for (pubkey, peer_addr) in info.drain() { for chan_info in channel_manager.list_channels() { - if pubkey == chan_info.remote_network_id { + if pubkey == chan_info.counterparty.node_id { let _ = cli::connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone()) .await; @@ -549,7 +614,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 { @@ -557,7 +622,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(), ); } }); @@ -576,6 +641,9 @@ async fn start_ldk() { network, ) .await; + + // Stop the background processor. + background_processor.stop().unwrap(); } #[tokio::main]