X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain.rs;h=6cff9f152492058c156e7fb1cf48ccb4e3910f38;hb=8984205acf1d328837d0f33798e89e479aea1882;hp=653ad8e9fa90b10c0790d451d7251153a1c95a7d;hpb=fdd4a78ee3b6198b9a4e48ea8e8464f53baa716e;p=ldk-sample diff --git a/src/main.rs b/src/main.rs index 653ad8e..6cff9f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -18,24 +16,28 @@ use bitcoin_bech32::WitnessProgram; 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::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient}; +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::onion_message::SimpleArcOnionMessenger; +use lightning::routing::gossip; +use lightning::routing::gossip::{NodeId, P2PGossipSync}; +use lightning::routing::scoring::ProbabilisticScorer; use lightning::util::config::UserConfig; -use lightning::util::events::{Event, EventsProvider}; +use lightning::util::events::{Event, PaymentPurpose}; use lightning::util::ser::ReadableArgs; -use lightning_background_processor::BackgroundProcessor; +use lightning_background_processor::{BackgroundProcessor, GossipSync}; use lightning_block_sync::init; use lightning_block_sync::poll; use lightning_block_sync::SpvClient; use lightning_block_sync::UnboundedCache; +use lightning_invoice::payment; +use lightning_invoice::utils::DefaultRouter; use lightning_net_tokio::SocketDescriptor; use lightning_persister::FilesystemPersister; use rand::{thread_rng, Rng}; @@ -48,10 +50,9 @@ use std::io; use std::io::Write; use std::ops::Deref; use std::path::Path; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, SystemTime}; -use tokio::sync::mpsc; -use tokio::sync::mpsc::Receiver; pub(crate) enum HTLCStatus { Pending, @@ -100,167 +101,260 @@ pub(crate) type PeerManager = SimpleArcPeerManager< pub(crate) type ChannelManager = SimpleArcChannelManager; +pub(crate) type InvoicePayer = payment::InvoicePayer< + Arc, + Router, + Arc, Arc>>>, + Arc, + E, +>; + +type Router = DefaultRouter, Arc>; + +pub(crate) type NetworkGraph = gossip::NetworkGraph>; + +type OnionMessenger = SimpleArcOnionMessenger; + async fn handle_ldk_events( - channel_manager: Arc, chain_monitor: Arc, - bitcoind_client: Arc, keys_manager: Arc, - inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, network: Network, - mut event_receiver: Receiver<()>, + channel_manager: &Arc, bitcoind_client: &BitcoindClient, + network_graph: &NetworkGraph, keys_manager: &KeysManager, + inbound_payments: &PaymentInfoStorage, outbound_payments: &PaymentInfoStorage, + network: Network, event: &Event, ) { - loop { - let received = event_receiver.recv(); - if received.await.is_none() { - println!("LDK Event channel closed!"); - return; + match event { + Event::FundingGenerationReady { + temporary_channel_id, + counterparty_node_id, + channel_value_satoshis, + output_script, + .. + } => { + // Construct the raw transaction with one output, that is paid the amount of the + // channel. + let addr = WitnessProgram::from_scriptpubkey( + &output_script[..], + match network { + Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin, + Network::Testnet => bitcoin_bech32::constants::Network::Testnet, + Network::Regtest => bitcoin_bech32::constants::Network::Regtest, + Network::Signet => bitcoin_bech32::constants::Network::Signet, + }, + ) + .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); + 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; + + // Sign the final funding transaction and broadcast it. + let signed_tx = bitcoind_client.sign_raw_transaction_with_wallet(funded_tx.hex).await; + assert_eq!(signed_tx.complete, true); + 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. + if channel_manager + .funding_transaction_generated( + &temporary_channel_id, + counterparty_node_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(); + } } - let loop_channel_manager = channel_manager.clone(); - let mut events = channel_manager.get_and_clear_pending_events(); - events.append(&mut chain_monitor.get_and_clear_pending_events()); - for event in events { - match event { - Event::FundingGenerationReady { - temporary_channel_id, - channel_value_satoshis, - output_script, - .. - } => { - // Construct the raw transaction with one output, that is paid the amount of the - // channel. - let addr = WitnessProgram::from_scriptpubkey( - &output_script[..], - match network { - Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin, - Network::Testnet => bitcoin_bech32::constants::Network::Testnet, - Network::Regtest => bitcoin_bech32::constants::Network::Regtest, - Network::Signet => panic!("Signet unsupported"), - }, - ) - .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); - 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; - assert_eq!(signed_tx.complete, true); - 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. - loop_channel_manager - .funding_transaction_generated(&temporary_channel_id, final_tx) - .unwrap(); + Event::PaymentReceived { payment_hash, purpose, amount_msat } => { + println!( + "\nEVENT: received payment from payment hash {} of {} millisatoshis", + hex_utils::hex_str(&payment_hash.0), + amount_msat, + ); + print!("> "); + io::stdout().flush().unwrap(); + let payment_preimage = match purpose { + PaymentPurpose::InvoicePayment { payment_preimage, .. } => *payment_preimage, + PaymentPurpose::SpontaneousPayment(preimage) => Some(*preimage), + }; + channel_manager.claim_funds(payment_preimage.unwrap()); + } + Event::PaymentClaimed { payment_hash, purpose, amount_msat } => { + println!( + "\nEVENT: claimed payment from payment hash {} of {} millisatoshis", + hex_utils::hex_str(&payment_hash.0), + amount_msat, + ); + print!("> "); + io::stdout().flush().unwrap(); + let (payment_preimage, payment_secret) = match purpose { + PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { + (*payment_preimage, Some(*payment_secret)) } - Event::PaymentReceived { - payment_hash, - payment_preimage, - payment_secret, - amt, - .. - } => { - let mut payments = inbound_payments.lock().unwrap(); - let status = match loop_channel_manager.claim_funds(payment_preimage.unwrap()) { - true => { - println!( - "\nEVENT: received payment from payment hash {} of {} millisatoshis", - hex_utils::hex_str(&payment_hash.0), - amt - ); - print!("> "); - io::stdout().flush().unwrap(); - HTLCStatus::Succeeded - } - _ => HTLCStatus::Failed, - }; - 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); - } - Entry::Vacant(e) => { - e.insert(PaymentInfo { - preimage: Some(payment_preimage.unwrap()), - secret: Some(payment_secret), - status, - amt_msat: MillisatAmount(Some(amt)), - }); - } - } + PaymentPurpose::SpontaneousPayment(preimage) => (Some(*preimage), None), + }; + let mut payments = inbound_payments.lock().unwrap(); + match payments.entry(*payment_hash) { + Entry::Occupied(mut e) => { + let payment = e.get_mut(); + payment.status = HTLCStatus::Succeeded; + payment.preimage = payment_preimage; + payment.secret = payment_secret; } - Event::PaymentSent { payment_preimage } => { - let hashed = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()); - 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.status = HTLCStatus::Succeeded; - println!( - "\nEVENT: successfully sent payment of {} millisatoshis from \ - payment hash {:?} with preimage {:?}", - payment.amt_msat, - hex_utils::hex_str(&payment_hash.0), - hex_utils::hex_str(&payment_preimage.0) - ); - print!("> "); - io::stdout().flush().unwrap(); - } - } + Entry::Vacant(e) => { + e.insert(PaymentInfo { + preimage: payment_preimage, + secret: payment_secret, + status: HTLCStatus::Succeeded, + amt_msat: MillisatAmount(Some(*amount_msat)), + }); } - Event::PaymentFailed { payment_hash, rejected_by_dest } => { - print!( - "\nEVENT: Failed to send payment to payment hash {:?}: ", - hex_utils::hex_str(&payment_hash.0) + } + } + 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 \ + 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) ); - if rejected_by_dest { - println!("rejected by destination node"); - } else { - println!("route failed"); - } print!("> "); io::stdout().flush().unwrap(); + } + } + } + Event::OpenChannelRequest { .. } => { + // Unreachable, we don't set manually_accept_inbound_channels + } + Event::PaymentPathSuccessful { .. } => {} + Event::PaymentPathFailed { .. } => {} + Event::ProbeSuccessful { .. } => {} + Event::ProbeFailed { .. } => {} + Event::PaymentFailed { payment_hash, .. } => { + print!( + "\nEVENT: Failed to send payment to payment hash {:?}: exhausted payment retry attempts", + hex_utils::hex_str(&payment_hash.0) + ); + print!("> "); + io::stdout().flush().unwrap(); - let mut payments = outbound_payments.lock().unwrap(); - if payments.contains_key(&payment_hash) { - let payment = payments.get_mut(&payment_hash).unwrap(); - payment.status = HTLCStatus::Failed; + let mut payments = outbound_payments.lock().unwrap(); + if payments.contains_key(&payment_hash) { + let payment = payments.get_mut(&payment_hash).unwrap(); + payment.status = HTLCStatus::Failed; + } + } + Event::PaymentForwarded { + prev_channel_id, + next_channel_id, + fee_earned_msat, + claim_from_onchain_tx, + } => { + let read_only_network_graph = network_graph.read_only(); + let nodes = read_only_network_graph.nodes(); + let channels = channel_manager.list_channels(); + + let node_str = |channel_id: &Option<[u8; 32]>| match channel_id { + None => String::new(), + Some(channel_id) => match channels.iter().find(|c| c.channel_id == *channel_id) { + None => String::new(), + Some(channel) => { + match nodes.get(&NodeId::from_pubkey(&channel.counterparty.node_id)) { + None => "private node".to_string(), + Some(node) => match &node.announcement_info { + None => "unnamed node".to_string(), + Some(announcement) => { + format!("node {}", announcement.alias) + } + }, + } } - } - Event::PendingHTLCsForwardable { time_forwardable } => { - let forwarding_channel_manager = loop_channel_manager.clone(); - 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(); - }); - } - Event::SpendableOutputs { outputs } => { - let destination_address = bitcoind_client.get_new_address().await; - let output_descriptors = &outputs.iter().map(|a| a).collect::>(); - let tx_feerate = - bitcoind_client.get_est_sat_per_1000_weight(ConfirmationTarget::Normal); - let spending_tx = keys_manager - .spend_spendable_outputs( - output_descriptors, - Vec::new(), - destination_address.script_pubkey(), - tx_feerate, - &Secp256k1::new(), - ) - .unwrap(); - bitcoind_client.broadcast_transaction(&spending_tx); - } + }, + }; + let channel_str = |channel_id: &Option<[u8; 32]>| { + channel_id + .map(|channel_id| format!(" with channel {}", hex_utils::hex_str(&channel_id))) + .unwrap_or_default() + }; + let from_prev_str = + format!(" from {}{}", node_str(prev_channel_id), channel_str(prev_channel_id)); + let to_next_str = + format!(" to {}{}", node_str(next_channel_id), channel_str(next_channel_id)); + + 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 {}", + from_prev_str, to_next_str, fee_earned, from_onchain_str + ); + } else { + println!( + "\nEVENT: Forwarded payment{}{}, claiming onchain {}", + from_prev_str, to_next_str, from_onchain_str + ); } + print!("> "); + io::stdout().flush().unwrap(); + } + Event::HTLCHandlingFailed { .. } => {} + Event::PendingHTLCsForwardable { time_forwardable } => { + let forwarding_channel_manager = channel_manager.clone(); + let min = time_forwardable.as_millis() as u64; + tokio::spawn(async move { + 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(); + }); + } + Event::SpendableOutputs { outputs } => { + let destination_address = bitcoind_client.get_new_address().await; + let output_descriptors = &outputs.iter().map(|a| a).collect::>(); + let tx_feerate = + bitcoind_client.get_est_sat_per_1000_weight(ConfirmationTarget::Normal); + let spending_tx = keys_manager + .spend_spendable_outputs( + output_descriptors, + Vec::new(), + destination_address.script_pubkey(), + tx_feerate, + &Secp256k1::new(), + ) + .unwrap(); + bitcoind_client.broadcast_transaction(&spending_tx); + } + Event::ChannelClosed { channel_id, reason, user_channel_id: _ } => { + println!( + "\nEVENT: Channel {} closed due to: {:?}", + hex_utils::hex_str(channel_id), + reason + ); + 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. } - tokio::time::sleep(Duration::from_secs(1)).await; } } @@ -280,6 +374,7 @@ async fn start_ldk() { args.bitcoind_rpc_port, args.bitcoind_rpc_username.clone(), args.bitcoind_rpc_password.clone(), + tokio::runtime::Handle::current(), ) .await { @@ -365,9 +460,10 @@ 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.channel_handshake_limits.force_announced_channel_preference = false; let mut restarting_node = true; - let (channel_manager_blockhash, mut channel_manager) = { + let (channel_manager_blockhash, channel_manager) = { if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) { let mut channel_monitor_mut_references = Vec::new(); for (_, channel_monitor) in channelmonitors.iter_mut() { @@ -413,8 +509,10 @@ async fn start_ldk() { let mut cache = UnboundedCache::new(); let mut chain_tip: Option = None; if restarting_node { - let mut chain_listeners = - vec![(channel_manager_blockhash, &mut channel_manager as &mut dyn chain::Listen)]; + let mut chain_listeners = vec![( + channel_manager_blockhash, + &channel_manager as &(dyn chain::Listen + Send + Sync), + )]; for (blockhash, channel_monitor) in channelmonitors.drain(..) { let outpoint = channel_monitor.get_funding_txo().0; @@ -428,7 +526,7 @@ async fn start_ldk() { for monitor_listener_info in chain_listener_channel_monitors.iter_mut() { chain_listeners.push(( monitor_listener_info.0, - &mut monitor_listener_info.1 as &mut dyn chain::Listen, + &monitor_listener_info.1 as &(dyn chain::Listen + Send + Sync), )); } chain_tip = Some( @@ -450,48 +548,58 @@ async fn start_ldk() { chain_monitor.watch_channel(funding_outpoint, channel_monitor).unwrap(); } - // Step 11: Optional: Initialize the NetGraphMsgHandler - // XXX persist routing data + // Step 11: Optional: Initialize the P2PGossipSync let genesis = genesis_block(args.network).header.block_hash(); - let router = Arc::new(NetGraphMsgHandler::new( - genesis, + let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone()); + let network_graph = + Arc::new(disk::read_network(Path::new(&network_graph_path), genesis, logger.clone())); + let gossip_sync = Arc::new(P2PGossipSync::new( + Arc::clone(&network_graph), None::>, logger.clone(), )); // Step 12: Initialize the PeerManager let channel_manager: Arc = Arc::new(channel_manager); + let onion_messenger: Arc = + Arc::new(OnionMessenger::new(keys_manager.clone(), logger.clone())); let mut ephemeral_bytes = [0; 32]; + let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); 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: gossip_sync.clone(), + onion_message_handler: onion_messenger.clone(), + }; let peer_manager: Arc = Arc::new(PeerManager::new( lightning_msg_handler, - keys_manager.get_node_secret(), + keys_manager.get_node_secret(Recipient::Node).unwrap(), + current_time, &ephemeral_bytes, logger.clone(), + IgnoringMessageHandler {}, )); // ## Running LDK // Step 13: Initialize networking - // We poll for events in handle_ldk_events(..) rather than waiting for them over the - // mpsc::channel, so we can leave the event receiver as unused. - let (event_ntfn_sender, event_ntfn_receiver) = mpsc::channel(2); let peer_manager_connection_handler = peer_manager.clone(); - let event_notifier = event_ntfn_sender.clone(); let listening_port = args.ldk_peer_listening_port; + let stop_listen_connect = Arc::new(AtomicBool::new(false)); + let stop_listen = Arc::clone(&stop_listen_connect); tokio::spawn(async move { - let listener = - tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listening_port)).await.unwrap(); + let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listening_port)) + .await + .expect("Failed to bind to listen port - is something else already listening on it?"); loop { let peer_mgr = peer_manager_connection_handler.clone(); - let notifier = event_notifier.clone(); let tcp_stream = listener.accept().await.unwrap().0; + if stop_listen.load(Ordering::Acquire) { + return; + } tokio::spawn(async move { lightning_net_tokio::setup_inbound( peer_mgr.clone(), - notifier.clone(), tcp_stream.into_std().unwrap(), ) .await; @@ -520,9 +628,8 @@ async fn start_ldk() { } }); - // Step 15: Initialize LDK Event Handling + // Step 15: Handle LDK Events let channel_manager_event_listener = channel_manager.clone(); - let chain_monitor_event_listener = chain_monitor.clone(); let keys_manager_listener = keys_manager.clone(); // TODO: persist payment info to disk let inbound_payments: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new())); @@ -531,65 +638,139 @@ async fn start_ldk() { let outbound_pmts_for_events = outbound_payments.clone(); let network = args.network; let bitcoind_rpc = bitcoind_client.clone(); - tokio::spawn(async move { - handle_ldk_events( - channel_manager_event_listener, - chain_monitor_event_listener, - bitcoind_rpc, - keys_manager_listener, - inbound_pmts_for_events, - outbound_pmts_for_events, + let network_graph_events = network_graph.clone(); + let handle = tokio::runtime::Handle::current(); + let event_handler = move |event: &Event| { + handle.block_on(handle_ldk_events( + &channel_manager_event_listener, + &bitcoind_rpc, + &network_graph_events, + &keys_manager_listener, + &inbound_pmts_for_events, + &outbound_pmts_for_events, network, - event_ntfn_receiver, - ) - .await; - }); + event, + )); + }; - // Step 16 & 17: Persist ChannelManager & Background Processing - let data_dir = ldk_data_dir.clone(); - let persist_channel_manager_callback = - move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node); - BackgroundProcessor::start( - persist_channel_manager_callback, + // Step 16: Initialize routing ProbabilisticScorer + let scorer_path = format!("{}/scorer", ldk_data_dir.clone()); + let scorer = Arc::new(Mutex::new(disk::read_scorer( + Path::new(&scorer_path), + Arc::clone(&network_graph), + Arc::clone(&logger), + ))); + + // Step 17: Create InvoicePayer + let router = DefaultRouter::new( + network_graph.clone(), + logger.clone(), + keys_manager.get_secure_random_bytes(), + ); + let invoice_payer = Arc::new(InvoicePayer::new( channel_manager.clone(), + router, + scorer.clone(), + logger.clone(), + event_handler, + payment::Retry::Timeout(Duration::from_secs(10)), + )); + + // Step 18: Persist ChannelManager and NetworkGraph + let persister = Arc::new(FilesystemPersister::new(ldk_data_dir.clone())); + + // Step 19: Background Processing + let background_processor = BackgroundProcessor::start( + persister, + invoice_payer.clone(), + chain_monitor.clone(), + channel_manager.clone(), + GossipSync::p2p(gossip_sync.clone()), peer_manager.clone(), logger.clone(), + Some(scorer.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.remote_network_id { - let _ = cli::connect_peer_if_necessary( - pubkey, - peer_addr, - peer_manager.clone(), - event_ntfn_sender.clone(), - ); + let stop_connect = Arc::clone(&stop_listen_connect); + 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)) + { + if stop_connect.load(Ordering::Acquire) { + return; + } + 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. + // In a production environment, this should occur only after the announcement of new channels + // to avoid churn in the global network graph. + let peer_man = Arc::clone(&peer_manager); + let network = args.network; + if !args.ldk_announced_listen_addr.is_empty() { + tokio::spawn(async move { + let mut interval = tokio::time::interval(Duration::from_secs(60)); + loop { + interval.tick().await; + peer_man.broadcast_node_announcement( + [0; 3], + args.ldk_announced_node_name, + args.ldk_announced_listen_addr.clone(), + ); + } + }); } // Start the CLI. cli::poll_for_user_input( - peer_manager.clone(), - channel_manager.clone(), - keys_manager.clone(), - router.clone(), + Arc::clone(&invoice_payer), + Arc::clone(&peer_manager), + Arc::clone(&channel_manager), + Arc::clone(&keys_manager), + Arc::clone(&network_graph), + Arc::clone(&onion_messenger), inbound_payments, outbound_payments, - event_ntfn_sender, ldk_data_dir.clone(), - logger.clone(), - args.network, + network, ) .await; + + // Disconnect our peers and stop accepting new connections. This ensures we don't continue + // updating our channel data after we've stopped the background processor. + stop_listen_connect.store(true, Ordering::Release); + peer_manager.disconnect_all_peers(); + + // Stop the background processor. + background_processor.stop().unwrap(); } #[tokio::main]