From: Valentine Wallace Date: Wed, 14 Apr 2021 18:25:14 +0000 (-0400) Subject: Split payments hashmap into inbound and outbound X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=ed6f3c60e3b6cea7041b3b7582afc8b4932c2d86;p=ldk-sample Split payments hashmap into inbound and outbound To prevent clashes between payment hashes --- diff --git a/src/cli.rs b/src/cli.rs index 2dc6b7d..6773480 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,8 @@ use crate::disk; use crate::hex_utils; use crate::{ - ChannelManager, FilesystemLogger, HTLCStatus, MillisatAmount, PaymentInfo, - PaymentInfoStorage, PeerManager, + ChannelManager, FilesystemLogger, HTLCStatus, MillisatAmount, PaymentInfo, PaymentInfoStorage, + PeerManager, }; use bitcoin::hashes::sha256::Hash as Sha256Hash; use bitcoin::hashes::Hash; @@ -101,8 +101,9 @@ pub(crate) fn parse_startup_args() -> Result { pub(crate) async fn poll_for_user_input( peer_manager: Arc, channel_manager: Arc, router: Arc, Arc>>, - payment_storage: PaymentInfoStorage, node_privkey: SecretKey, event_notifier: mpsc::Sender<()>, - ldk_data_dir: String, logger: Arc, network: Network, + inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, + node_privkey: SecretKey, event_notifier: mpsc::Sender<()>, ldk_data_dir: String, + logger: Arc, network: Network, ) { println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available at /.ldk/logs"); let stdin = io::stdin(); @@ -278,7 +279,7 @@ pub(crate) async fn poll_for_user_input( route_hints, router.clone(), channel_manager.clone(), - payment_storage.clone(), + outbound_payments.clone(), logger.clone(), ); } @@ -300,7 +301,7 @@ pub(crate) async fn poll_for_user_input( } get_invoice( amt_msat.unwrap(), - payment_storage.clone(), + inbound_payments.clone(), node_privkey.clone(), channel_manager.clone(), network, @@ -336,7 +337,9 @@ pub(crate) async fn poll_for_user_input( } } "listchannels" => list_channels(channel_manager.clone()), - "listpayments" => list_payments(payment_storage.clone()), + "listpayments" => { + list_payments(inbound_payments.clone(), outbound_payments.clone()) + } "closechannel" => { let channel_id_str = words.next(); if channel_id_str.is_none() { @@ -419,15 +422,34 @@ fn list_channels(channel_manager: Arc) { println!("]"); } -fn list_payments(payment_storage: PaymentInfoStorage) { - let payments = payment_storage.lock().unwrap(); +fn list_payments(inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage) { + let inbound = inbound_payments.lock().unwrap(); + let outbound = outbound_payments.lock().unwrap(); print!("["); - for (payment_hash, payment_info) in payments.deref() { + for (payment_hash, payment_info) in inbound.deref() { + println!(""); + println!("\t{{"); + println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat); + println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0)); + println!("\t\thtlc_direction: inbound,"); + println!( + "\t\thtlc_status: {},", + match payment_info.status { + HTLCStatus::Pending => "pending", + HTLCStatus::Succeeded => "succeeded", + HTLCStatus::Failed => "failed", + } + ); + + println!("\t}},"); + } + + for (payment_hash, payment_info) in outbound.deref() { println!(""); println!("\t{{"); println!("\t\tamount_millisatoshis: {},", payment_info.amt_msat); println!("\t\tpayment_hash: {},", hex_utils::hex_str(&payment_hash.0)); - // println!("\t\thtlc_direction: {},", direction_str); + println!("\t\thtlc_direction: outbound,"); println!( "\t\thtlc_status: {},", match payment_info.status { diff --git a/src/main.rs b/src/main.rs index 32981a4..7001129 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,7 @@ pub(crate) type ChannelManager = async fn handle_ldk_events( channel_manager: Arc, chain_monitor: Arc, bitcoind_client: Arc, keys_manager: Arc, - payment_storage: PaymentInfoStorage, network: Network, + inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, network: Network, ) { loop { let loop_channel_manager = channel_manager.clone(); @@ -150,7 +150,7 @@ async fn handle_ldk_events( .unwrap(); } Event::PaymentReceived { payment_hash, .. } => { - let mut payments = payment_storage.lock().unwrap(); + let mut payments = inbound_payments.lock().unwrap(); if let Some(payment) = payments.get_mut(&payment_hash) { assert!(loop_channel_manager.claim_funds( payment.preimage.unwrap().clone(), @@ -183,7 +183,7 @@ async fn handle_ldk_events( } Event::PaymentSent { payment_preimage } => { let hashed = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()); - let mut payments = payment_storage.lock().unwrap(); + let mut payments = outbound_payments.lock().unwrap(); for (payment_hash, payment) in payments.iter_mut() { if *payment_hash == hashed { payment.preimage = Some(payment_preimage); @@ -213,7 +213,7 @@ async fn handle_ldk_events( print!("> "); io::stdout().flush().unwrap(); - let mut payments = payment_storage.lock().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; @@ -497,8 +497,10 @@ async fn start_ldk() { let chain_monitor_event_listener = chain_monitor.clone(); let keys_manager_listener = keys_manager.clone(); // TODO: persist payment info to disk - let payment_info: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new())); - let payment_info_for_events = payment_info.clone(); + let inbound_payments: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new())); + let outbound_payments: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new())); + let inbound_pmts_for_events = inbound_payments.clone(); + let outbound_pmts_for_events = outbound_payments.clone(); let network = args.network; let bitcoind_rpc = bitcoind_client.clone(); tokio::spawn(async move { @@ -507,7 +509,8 @@ async fn start_ldk() { chain_monitor_event_listener, bitcoind_rpc, keys_manager_listener, - payment_info_for_events, + inbound_pmts_for_events, + outbound_pmts_for_events, network, ) .await; @@ -538,7 +541,8 @@ async fn start_ldk() { peer_manager.clone(), channel_manager.clone(), router.clone(), - payment_info, + inbound_payments, + outbound_payments, keys_manager.get_node_secret(), event_ntfn_sender, ldk_data_dir.clone(),