]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Split payments hashmap into inbound and outbound
authorValentine Wallace <vwallace@protonmail.com>
Wed, 14 Apr 2021 18:25:14 +0000 (14:25 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Mon, 3 May 2021 22:31:02 +0000 (18:31 -0400)
To prevent clashes between payment hashes

src/cli.rs
src/main.rs

index 2dc6b7df614731296ab9e479a9b697d2f4c086a7..677348090da7aabc6bc60b0802a687903e946d29 100644 (file)
@@ -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<LdkUserInfo, ()> {
 pub(crate) async fn poll_for_user_input(
        peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
        router: Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<FilesystemLogger>>>,
-       payment_storage: PaymentInfoStorage, node_privkey: SecretKey, event_notifier: mpsc::Sender<()>,
-       ldk_data_dir: String, logger: Arc<FilesystemLogger>, network: Network,
+       inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage,
+       node_privkey: SecretKey, event_notifier: mpsc::Sender<()>, ldk_data_dir: String,
+       logger: Arc<FilesystemLogger>, network: Network,
 ) {
        println!("LDK startup successful. To view available commands: \"help\".\nLDK logs are available at <your-supplied-ldk-data-dir-path>/.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<ChannelManager>) {
        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 {
index 32981a4ac23845322d5a301c1edf62a7090daf4c..700112920c6cb860f6e52e5632fe01a92d351d95 100644 (file)
@@ -101,7 +101,7 @@ pub(crate) type ChannelManager =
 async fn handle_ldk_events(
        channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
        bitcoind_client: Arc<BitcoindClient>, keys_manager: Arc<KeysManager>,
-       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(),