Merge pull request #131 from tnull/2024-02-align-rustfmt main
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Thu, 25 Apr 2024 17:48:04 +0000 (10:48 -0700)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2024 17:48:04 +0000 (10:48 -0700)
Align `rustfmt.toml` with `rust-lightning`

rustfmt.toml
src/args.rs
src/cli.rs
src/convert.rs
src/disk.rs
src/main.rs
src/sweep.rs

index c00f65546aeeb453c4f89842744cb4d125f65e98..4f88472bec555d9d5a2ae8afc1a5c95320adc50e 100644 (file)
@@ -1,5 +1,12 @@
-hard_tabs = true # use tab characters for indentation, spaces for alignment
+use_small_heuristics = "Max"
+fn_params_layout = "Compressed"
+hard_tabs = true
 use_field_init_shorthand = true
 max_width = 100
 use_field_init_shorthand = true
 max_width = 100
-use_small_heuristics = "Max"
-fn_args_layout = "Compressed"
+match_block_trailing_comma = true
+# UNSTABLE: format_code_in_doc_comments = true
+# UNSTABLE: overflow_delimited_expr = true
+# UNSTABLE: comment_width = 100
+# UNSTABLE: format_macro_matchers = true
+# UNSTABLE: format_strings = true
+# UNSTABLE: group_imports = "StdExternalCrate"
index 3778aa20d24d80395c1c988e80873b683c76d822..34d97bb39eab0de60ca550eb62287e27bcbb7ea4 100644 (file)
@@ -32,11 +32,11 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
                Some(Err(_)) => {
                        ldk_peer_port_set = false;
                        9735
                Some(Err(_)) => {
                        ldk_peer_port_set = false;
                        9735
-               }
+               },
                None => {
                        ldk_peer_port_set = false;
                        9735
                None => {
                        ldk_peer_port_set = false;
                        9735
-               }
+               },
        };
 
        let mut arg_idx = match ldk_peer_port_set {
        };
 
        let mut arg_idx = match ldk_peer_port_set {
@@ -49,7 +49,7 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
                Some("signet") => Network::Signet,
                Some(net) => {
                        panic!("Unsupported network provided. Options are: `regtest`, `testnet`, and `signet`. Got {}", net);
                Some("signet") => Network::Signet,
                Some(net) => {
                        panic!("Unsupported network provided. Options are: `regtest`, `testnet`, and `signet`. Got {}", net);
-               }
+               },
                None => Network::Testnet,
        };
 
                None => Network::Testnet,
        };
 
@@ -78,7 +78,7 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
                        let mut bytes = [0; 32];
                        bytes[..s.len()].copy_from_slice(s.as_bytes());
                        bytes
                        let mut bytes = [0; 32];
                        bytes[..s.len()].copy_from_slice(s.as_bytes());
                        bytes
-               }
+               },
                None => [0; 32],
        };
 
                None => [0; 32],
        };
 
@@ -89,7 +89,7 @@ pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
                                Ok(sa) => {
                                        ldk_announced_listen_addr.push(sa);
                                        arg_idx += 1;
                                Ok(sa) => {
                                        ldk_announced_listen_addr.push(sa);
                                        arg_idx += 1;
-                               }
+                               },
                                Err(_) => panic!("Failed to parse announced-listen-addr into a socket address"),
                        },
                        None => break,
                                Err(_) => panic!("Failed to parse announced-listen-addr into a socket address"),
                        },
                        None => break,
index 72cc484220d818e8cd2a54e4cdd9537dc6383648..fe1b286d7b4d7abaf424495a98dccd522bb56daf 100644 (file)
@@ -21,6 +21,7 @@ use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, Us
 use lightning::util::persist::KVStore;
 use lightning::util::ser::{Writeable, Writer};
 use lightning_invoice::payment::payment_parameters_from_invoice;
 use lightning::util::persist::KVStore;
 use lightning::util::ser::{Writeable, Writer};
 use lightning_invoice::payment::payment_parameters_from_invoice;
+use lightning_invoice::payment::payment_parameters_from_zero_amount_invoice;
 use lightning_invoice::{utils, Bolt11Invoice, Currency};
 use lightning_persister::fs_store::FilesystemStore;
 use std::env;
 use lightning_invoice::{utils, Bolt11Invoice, Currency};
 use lightning_persister::fs_store::FilesystemStore;
 use std::env;
@@ -105,7 +106,7 @@ pub(crate) fn poll_for_user_input(
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
                                                                continue;
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
                                                                continue;
-                                                       }
+                                                       },
                                                };
 
                                        let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
                                                };
 
                                        let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
@@ -135,7 +136,7 @@ pub(crate) fn poll_for_user_input(
                                                        _ => {
                                                                println!("ERROR: invalid boolean flag format. Valid formats: `--option`, `--option=true` `--option=false`");
                                                                continue;
                                                        _ => {
                                                                println!("ERROR: invalid boolean flag format. Valid formats: `--option`, `--option=true` `--option=false`");
                                                                continue;
-                                                       }
+                                                       },
                                                }
                                        }
 
                                                }
                                        }
 
@@ -154,7 +155,7 @@ pub(crate) fn poll_for_user_input(
                                                        peer_pubkey_and_ip_addr,
                                                );
                                        }
                                                        peer_pubkey_and_ip_addr,
                                                );
                                        }
-                               }
+                               },
                                "sendpayment" => {
                                        let invoice_str = words.next();
                                        if invoice_str.is_none() {
                                "sendpayment" => {
                                        let invoice_str = words.next();
                                        if invoice_str.is_none() {
@@ -162,20 +163,35 @@ pub(crate) fn poll_for_user_input(
                                                continue;
                                        }
 
                                                continue;
                                        }
 
+                                       let mut user_provided_amt: Option<u64> = None;
+                                       if let Some(amt_msat_str) = words.next() {
+                                               match amt_msat_str.parse() {
+                                                       Ok(amt) => user_provided_amt = Some(amt),
+                                                       Err(e) => {
+                                                               println!("ERROR: couldn't parse amount_msat: {}", e);
+                                                               continue;
+                                                       },
+                                               };
+                                       }
+
                                        if let Ok(offer) = Offer::from_str(invoice_str.unwrap()) {
                                        if let Ok(offer) = Offer::from_str(invoice_str.unwrap()) {
-                                               let offer_hash = Sha256::hash(invoice_str.unwrap().as_bytes());
-                                               let payment_id = PaymentId(*offer_hash.as_ref());
-
-                                               let amt_msat =
-                                                       match offer.amount() {
-                                                               Some(offer::Amount::Bitcoin { amount_msats }) => *amount_msats,
-                                                               amt => {
-                                                                       println!("ERROR: Cannot process non-Bitcoin-denominated offer value {:?}", amt);
-                                                                       continue;
-                                                               }
-                                                       };
-
-                                               loop {
+                                               let random_bytes = keys_manager.get_secure_random_bytes();
+                                               let payment_id = PaymentId(random_bytes);
+
+                                               let amt_msat = match (offer.amount(), user_provided_amt) {
+                                                       (Some(offer::Amount::Bitcoin { amount_msats }), _) => *amount_msats,
+                                                       (_, Some(amt)) => amt,
+                                                       (amt, _) => {
+                                                               println!("ERROR: Cannot process non-Bitcoin-denominated offer value {:?}", amt);
+                                                               continue;
+                                                       },
+                                               };
+                                               if user_provided_amt.is_some() && user_provided_amt != Some(amt_msat) {
+                                                       println!("Amount didn't match offer of {}msat", amt_msat);
+                                                       continue;
+                                               }
+
+                                               while user_provided_amt.is_none() {
                                                        print!("Paying offer for {} msat. Continue (Y/N)? >", amt_msat);
                                                        io::stdout().flush().unwrap();
 
                                                        print!("Paying offer for {} msat. Continue (Y/N)? >", amt_msat);
                                                        io::stdout().flush().unwrap();
 
@@ -211,8 +227,9 @@ pub(crate) fn poll_for_user_input(
                                                        .unwrap();
 
                                                let retry = Retry::Timeout(Duration::from_secs(10));
                                                        .unwrap();
 
                                                let retry = Retry::Timeout(Duration::from_secs(10));
+                                               let amt = Some(amt_msat);
                                                let pay = channel_manager
                                                let pay = channel_manager
-                                                       .pay_for_offer(&offer, None, None, None, payment_id, retry, None);
+                                                       .pay_for_offer(&offer, None, amt, None, payment_id, retry, None);
                                                if pay.is_err() {
                                                        println!("ERROR: Failed to pay: {:?}", pay);
                                                }
                                                if pay.is_err() {
                                                        println!("ERROR: Failed to pay: {:?}", pay);
                                                }
@@ -221,15 +238,16 @@ pub(crate) fn poll_for_user_input(
                                                        Ok(invoice) => send_payment(
                                                                &channel_manager,
                                                                &invoice,
                                                        Ok(invoice) => send_payment(
                                                                &channel_manager,
                                                                &invoice,
+                                                               user_provided_amt,
                                                                &mut outbound_payments.lock().unwrap(),
                                                                Arc::clone(&fs_store),
                                                        ),
                                                        Err(e) => {
                                                                println!("ERROR: invalid invoice: {:?}", e);
                                                                &mut outbound_payments.lock().unwrap(),
                                                                Arc::clone(&fs_store),
                                                        ),
                                                        Err(e) => {
                                                                println!("ERROR: invalid invoice: {:?}", e);
-                                                       }
+                                                       },
                                                }
                                        }
                                                }
                                        }
-                               }
+                               },
                                "keysend" => {
                                        let dest_pubkey = match words.next() {
                                                Some(dest) => match hex_utils::to_compressed_pubkey(dest) {
                                "keysend" => {
                                        let dest_pubkey = match words.next() {
                                                Some(dest) => match hex_utils::to_compressed_pubkey(dest) {
@@ -237,26 +255,26 @@ pub(crate) fn poll_for_user_input(
                                                        None => {
                                                                println!("ERROR: couldn't parse destination pubkey");
                                                                continue;
                                                        None => {
                                                                println!("ERROR: couldn't parse destination pubkey");
                                                                continue;
-                                                       }
+                                                       },
                                                },
                                                None => {
                                                        println!("ERROR: keysend requires a destination pubkey: `keysend <dest_pubkey> <amt_msat>`");
                                                        continue;
                                                },
                                                None => {
                                                        println!("ERROR: keysend requires a destination pubkey: `keysend <dest_pubkey> <amt_msat>`");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let amt_msat_str = match words.next() {
                                                Some(amt) => amt,
                                                None => {
                                                        println!("ERROR: keysend requires an amount in millisatoshis: `keysend <dest_pubkey> <amt_msat>`");
                                                        continue;
                                        };
                                        let amt_msat_str = match words.next() {
                                                Some(amt) => amt,
                                                None => {
                                                        println!("ERROR: keysend requires an amount in millisatoshis: `keysend <dest_pubkey> <amt_msat>`");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let amt_msat: u64 = match amt_msat_str.parse() {
                                                Ok(amt) => amt,
                                                Err(e) => {
                                                        println!("ERROR: couldn't parse amount_msat: {}", e);
                                                        continue;
                                        };
                                        let amt_msat: u64 = match amt_msat_str.parse() {
                                                Ok(amt) => amt,
                                                Err(e) => {
                                                        println!("ERROR: couldn't parse amount_msat: {}", e);
                                                        continue;
-                                               }
+                                               },
                                        };
                                        keysend(
                                                &channel_manager,
                                        };
                                        keysend(
                                                &channel_manager,
@@ -266,7 +284,7 @@ pub(crate) fn poll_for_user_input(
                                                &mut outbound_payments.lock().unwrap(),
                                                Arc::clone(&fs_store),
                                        );
                                                &mut outbound_payments.lock().unwrap(),
                                                Arc::clone(&fs_store),
                                        );
-                               }
+                               },
                                "getoffer" => {
                                        let offer_builder = channel_manager.create_offer_builder(String::new());
                                        if let Err(e) = offer_builder {
                                "getoffer" => {
                                        let offer_builder = channel_manager.create_offer_builder(String::new());
                                        if let Err(e) = offer_builder {
@@ -294,7 +312,7 @@ pub(crate) fn poll_for_user_input(
                                                // correspond with individual payments.
                                                println!("{}", offer.unwrap());
                                        }
                                                // correspond with individual payments.
                                                println!("{}", offer.unwrap());
                                        }
-                               }
+                               },
                                "getinvoice" => {
                                        let amt_str = words.next();
                                        if amt_str.is_none() {
                                "getinvoice" => {
                                        let amt_str = words.next();
                                        if amt_str.is_none() {
@@ -333,7 +351,7 @@ pub(crate) fn poll_for_user_input(
                                        fs_store
                                                .write("", "", INBOUND_PAYMENTS_FNAME, &inbound_payments.encode())
                                                .unwrap();
                                        fs_store
                                                .write("", "", INBOUND_PAYMENTS_FNAME, &inbound_payments.encode())
                                                .unwrap();
-                               }
+                               },
                                "connectpeer" => {
                                        let peer_pubkey_and_ip_addr = words.next();
                                        if peer_pubkey_and_ip_addr.is_none() {
                                "connectpeer" => {
                                        let peer_pubkey_and_ip_addr = words.next();
                                        if peer_pubkey_and_ip_addr.is_none() {
@@ -346,7 +364,7 @@ pub(crate) fn poll_for_user_input(
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
                                                                continue;
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
                                                                continue;
-                                                       }
+                                                       },
                                                };
                                        if tokio::runtime::Handle::current()
                                                .block_on(connect_peer_if_necessary(
                                                };
                                        if tokio::runtime::Handle::current()
                                                .block_on(connect_peer_if_necessary(
@@ -358,7 +376,7 @@ pub(crate) fn poll_for_user_input(
                                        {
                                                println!("SUCCESS: connected to peer {}", pubkey);
                                        }
                                        {
                                                println!("SUCCESS: connected to peer {}", pubkey);
                                        }
-                               }
+                               },
                                "disconnectpeer" => {
                                        let peer_pubkey = words.next();
                                        if peer_pubkey.is_none() {
                                "disconnectpeer" => {
                                        let peer_pubkey = words.next();
                                        if peer_pubkey.is_none() {
@@ -372,7 +390,7 @@ pub(crate) fn poll_for_user_input(
                                                        Err(e) => {
                                                                println!("ERROR: {}", e.to_string());
                                                                continue;
                                                        Err(e) => {
                                                                println!("ERROR: {}", e.to_string());
                                                                continue;
-                                                       }
+                                                       },
                                                };
 
                                        if do_disconnect_peer(
                                                };
 
                                        if do_disconnect_peer(
@@ -384,7 +402,7 @@ pub(crate) fn poll_for_user_input(
                                        {
                                                println!("SUCCESS: disconnected from peer {}", peer_pubkey);
                                        }
                                        {
                                                println!("SUCCESS: disconnected from peer {}", peer_pubkey);
                                        }
-                               }
+                               },
                                "listchannels" => list_channels(&channel_manager, &network_graph),
                                "listpayments" => list_payments(
                                        &inbound_payments.lock().unwrap(),
                                "listchannels" => list_channels(&channel_manager, &network_graph),
                                "listpayments" => list_payments(
                                        &inbound_payments.lock().unwrap(),
@@ -414,18 +432,18 @@ pub(crate) fn poll_for_user_input(
                                                None => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
                                                None => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let peer_pubkey = match PublicKey::from_slice(&peer_pubkey_vec) {
                                                Ok(peer_pubkey) => peer_pubkey,
                                                Err(_) => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
                                        };
                                        let peer_pubkey = match PublicKey::from_slice(&peer_pubkey_vec) {
                                                Ok(peer_pubkey) => peer_pubkey,
                                                Err(_) => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
-                                               }
+                                               },
                                        };
 
                                        close_channel(channel_id, peer_pubkey, channel_manager.clone());
                                        };
 
                                        close_channel(channel_id, peer_pubkey, channel_manager.clone());
-                               }
+                               },
                                "forceclosechannel" => {
                                        let channel_id_str = words.next();
                                        if channel_id_str.is_none() {
                                "forceclosechannel" => {
                                        let channel_id_str = words.next();
                                        if channel_id_str.is_none() {
@@ -450,18 +468,18 @@ pub(crate) fn poll_for_user_input(
                                                None => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
                                                None => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let peer_pubkey = match PublicKey::from_slice(&peer_pubkey_vec) {
                                                Ok(peer_pubkey) => peer_pubkey,
                                                Err(_) => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
                                        };
                                        let peer_pubkey = match PublicKey::from_slice(&peer_pubkey_vec) {
                                                Ok(peer_pubkey) => peer_pubkey,
                                                Err(_) => {
                                                        println!("ERROR: couldn't parse peer_pubkey");
                                                        continue;
-                                               }
+                                               },
                                        };
 
                                        force_close_channel(channel_id, peer_pubkey, channel_manager.clone());
                                        };
 
                                        force_close_channel(channel_id, peer_pubkey, channel_manager.clone());
-                               }
+                               },
                                "nodeinfo" => node_info(&channel_manager, &peer_manager),
                                "listpeers" => list_peers(peer_manager.clone()),
                                "signmessage" => {
                                "nodeinfo" => node_info(&channel_manager, &peer_manager),
                                "listpeers" => list_peers(peer_manager.clone()),
                                "signmessage" => {
@@ -477,7 +495,7 @@ pub(crate) fn poll_for_user_input(
                                                        &keys_manager.get_node_secret_key()
                                                )
                                        );
                                                        &keys_manager.get_node_secret_key()
                                                )
                                        );
-                               }
+                               },
                                "sendonionmessage" => {
                                        let path_pks_str = words.next();
                                        if path_pks_str.is_none() {
                                "sendonionmessage" => {
                                        let path_pks_str = words.next();
                                        if path_pks_str.is_none() {
@@ -495,7 +513,7 @@ pub(crate) fn poll_for_user_input(
                                                                println!("ERROR: couldn't parse peer_pubkey");
                                                                errored = true;
                                                                break;
                                                                println!("ERROR: couldn't parse peer_pubkey");
                                                                errored = true;
                                                                break;
-                                                       }
+                                                       },
                                                };
                                                let node_pubkey = match PublicKey::from_slice(&node_pubkey_vec) {
                                                        Ok(peer_pubkey) => peer_pubkey,
                                                };
                                                let node_pubkey = match PublicKey::from_slice(&node_pubkey_vec) {
                                                        Ok(peer_pubkey) => peer_pubkey,
@@ -503,7 +521,7 @@ pub(crate) fn poll_for_user_input(
                                                                println!("ERROR: couldn't parse peer_pubkey");
                                                                errored = true;
                                                                break;
                                                                println!("ERROR: couldn't parse peer_pubkey");
                                                                errored = true;
                                                                break;
-                                                       }
+                                                       },
                                                };
                                                intermediate_nodes.push(node_pubkey);
                                        }
                                                };
                                                intermediate_nodes.push(node_pubkey);
                                        }
@@ -515,14 +533,14 @@ pub(crate) fn poll_for_user_input(
                                                _ => {
                                                        println!("Need an integral message type above 64");
                                                        continue;
                                                _ => {
                                                        println!("Need an integral message type above 64");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let data = match words.next().map(|s| hex_utils::to_vec(s)) {
                                                Some(Some(data)) => data,
                                                _ => {
                                                        println!("Need a hex data string");
                                                        continue;
                                        };
                                        let data = match words.next().map(|s| hex_utils::to_vec(s)) {
                                                Some(Some(data)) => data,
                                                _ => {
                                                        println!("Need a hex data string");
                                                        continue;
-                                               }
+                                               },
                                        };
                                        let destination = Destination::Node(intermediate_nodes.pop().unwrap());
                                        match onion_messenger.send_onion_message(
                                        };
                                        let destination = Destination::Node(intermediate_nodes.pop().unwrap());
                                        match onion_messenger.send_onion_message(
@@ -532,10 +550,10 @@ pub(crate) fn poll_for_user_input(
                                        ) {
                                                Ok(success) => {
                                                        println!("SUCCESS: forwarded onion message to first hop {:?}", success)
                                        ) {
                                                Ok(success) => {
                                                        println!("SUCCESS: forwarded onion message to first hop {:?}", success)
-                                               }
+                                               },
                                                Err(e) => println!("ERROR: failed to send onion message: {:?}", e),
                                        }
                                                Err(e) => println!("ERROR: failed to send onion message: {:?}", e),
                                        }
-                               }
+                               },
                                "quit" | "exit" => break,
                                _ => println!("Unknown command. See `\"help\" for available commands."),
                        }
                                "quit" | "exit" => break,
                                _ => println!("Unknown command. See `\"help\" for available commands."),
                        }
@@ -563,7 +581,7 @@ fn help() {
        println!("      disconnectpeer <peer_pubkey>");
        println!("      listpeers");
        println!("\n  Payments:");
        println!("      disconnectpeer <peer_pubkey>");
        println!("      listpeers");
        println!("\n  Payments:");
-       println!("      sendpayment <invoice|offer>");
+       println!("      sendpayment <invoice|offer> [<amount_msat>]");
        println!("      keysend <dest_pubkey> <amt_msats>");
        println!("      listpayments");
        println!("\n  Invoices:");
        println!("      keysend <dest_pubkey> <amt_msats>");
        println!("      listpayments");
        println!("\n  Invoices:");
@@ -711,7 +729,7 @@ pub(crate) async fn do_connect_peer(
                                        return Ok(());
                                }
                        }
                                        return Ok(());
                                }
                        }
-               }
+               },
                None => Err(()),
        }
 }
                None => Err(()),
        }
 }
@@ -761,29 +779,50 @@ fn open_channel(
                Ok(_) => {
                        println!("EVENT: initiated channel with peer {}. ", peer_pubkey);
                        return Ok(());
                Ok(_) => {
                        println!("EVENT: initiated channel with peer {}. ", peer_pubkey);
                        return Ok(());
-               }
+               },
                Err(e) => {
                        println!("ERROR: failed to open channel: {:?}", e);
                        return Err(());
                Err(e) => {
                        println!("ERROR: failed to open channel: {:?}", e);
                        return Err(());
-               }
+               },
        }
 }
 
 fn send_payment(
        }
 }
 
 fn send_payment(
-       channel_manager: &ChannelManager, invoice: &Bolt11Invoice,
+       channel_manager: &ChannelManager, invoice: &Bolt11Invoice, required_amount_msat: Option<u64>,
        outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc<FilesystemStore>,
 ) {
        let payment_id = PaymentId((*invoice.payment_hash()).to_byte_array());
        let payment_secret = Some(*invoice.payment_secret());
        outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc<FilesystemStore>,
 ) {
        let payment_id = PaymentId((*invoice.payment_hash()).to_byte_array());
        let payment_secret = Some(*invoice.payment_secret());
-       let (payment_hash, recipient_onion, route_params) =
-               match payment_parameters_from_invoice(invoice) {
-                       Ok(res) => res,
-                       Err(e) => {
-                               println!("Failed to parse invoice");
-                               print!("> ");
-                               return;
-                       }
-               };
+       let zero_amt_invoice =
+               invoice.amount_milli_satoshis().is_none() || invoice.amount_milli_satoshis() == Some(0);
+       let pay_params_opt = if zero_amt_invoice {
+               if let Some(amt_msat) = required_amount_msat {
+                       payment_parameters_from_zero_amount_invoice(invoice, amt_msat)
+               } else {
+                       println!("Need an amount for the given 0-value invoice");
+                       print!("> ");
+                       return;
+               }
+       } else {
+               if required_amount_msat.is_some() && invoice.amount_milli_satoshis() != required_amount_msat
+               {
+                       println!(
+                               "Amount didn't match invoice value of {}msat",
+                               invoice.amount_milli_satoshis().unwrap_or(0)
+                       );
+                       print!("> ");
+                       return;
+               }
+               payment_parameters_from_invoice(invoice)
+       };
+       let (payment_hash, recipient_onion, route_params) = match pay_params_opt {
+               Ok(res) => res,
+               Err(e) => {
+                       println!("Failed to parse invoice: {:?}", e);
+                       print!("> ");
+                       return;
+               },
+       };
        outbound_payments.payments.insert(
                payment_id,
                PaymentInfo {
        outbound_payments.payments.insert(
                payment_id,
                PaymentInfo {
@@ -807,13 +846,13 @@ fn send_payment(
                        let amt_msat = invoice.amount_milli_satoshis().unwrap();
                        println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
                        print!("> ");
                        let amt_msat = invoice.amount_milli_satoshis().unwrap();
                        println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
                        print!("> ");
-               }
+               },
                Err(e) => {
                        println!("ERROR: failed to send payment: {:?}", e);
                        print!("> ");
                        outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
                Err(e) => {
                        println!("ERROR: failed to send payment: {:?}", e);
                        print!("> ");
                        outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
-               }
+               },
        };
 }
 
        };
 }
 
@@ -848,13 +887,13 @@ fn keysend<E: EntropySource>(
                Ok(_payment_hash) => {
                        println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
                        print!("> ");
                Ok(_payment_hash) => {
                        println!("EVENT: initiated sending {} msats to {}", amt_msat, payee_pubkey);
                        print!("> ");
-               }
+               },
                Err(e) => {
                        println!("ERROR: failed to send payment: {:?}", e);
                        print!("> ");
                        outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
                Err(e) => {
                        println!("ERROR: failed to send payment: {:?}", e);
                        print!("> ");
                        outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed;
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap();
-               }
+               },
        };
 }
 
        };
 }
 
@@ -882,11 +921,11 @@ fn get_invoice(
                Ok(inv) => {
                        println!("SUCCESS: generated invoice: {}", inv);
                        inv
                Ok(inv) => {
                        println!("SUCCESS: generated invoice: {}", inv);
                        inv
-               }
+               },
                Err(e) => {
                        println!("ERROR: failed to create invoice: {:?}", e);
                        return;
                Err(e) => {
                        println!("ERROR: failed to create invoice: {:?}", e);
                        return;
-               }
+               },
        };
 
        let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
        };
 
        let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
index 686761fffdc97b138361c5b251298e1e628f2596..52373322c7e2b1b4eee92a9296bac5e150acf942 100644 (file)
@@ -67,7 +67,7 @@ impl TryInto<FeeResponse> for JsonResponse {
                                // to convert virtual-bytes into weight units.
                                Some(feerate_btc_per_kvbyte) => {
                                        Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
                                // to convert virtual-bytes into weight units.
                                Some(feerate_btc_per_kvbyte) => {
                                        Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
-                               }
+                               },
                                None => None,
                        },
                })
                                None => None,
                        },
                })
@@ -92,7 +92,7 @@ impl TryInto<MempoolMinFeeResponse> for JsonResponse {
                                // to convert virtual-bytes into weight units.
                                Some(feerate_btc_per_kvbyte) => {
                                        Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
                                // to convert virtual-bytes into weight units.
                                Some(feerate_btc_per_kvbyte) => {
                                        Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
-                               }
+                               },
                                None => None,
                        },
                })
                                None => None,
                        },
                })
index 9b9a72b229a90dbd2c4fed34a906537470ff7218..780a1232c4936fd8359d5e8ce9543824846d8523 100644 (file)
@@ -68,7 +68,7 @@ pub(crate) fn read_channel_peer_data(
                match cli::parse_peer_info(line.unwrap()) {
                        Ok((pubkey, socket_addr)) => {
                                peer_data.insert(pubkey, socket_addr);
                match cli::parse_peer_info(line.unwrap()) {
                        Ok((pubkey, socket_addr)) => {
                                peer_data.insert(pubkey, socket_addr);
-                       }
+                       },
                        Err(e) => return Err(e),
                }
        }
                        Err(e) => return Err(e),
                }
        }
index 6b4dbdd4260e5779ebe537ce74df3bfb8670188e..add6990056b03bedd823492f841e9b302164a47e 100644 (file)
@@ -228,7 +228,7 @@ async fn handle_ldk_events(
                                print!("> ");
                                io::stdout().flush().unwrap();
                        }
                                print!("> ");
                                io::stdout().flush().unwrap();
                        }
-               }
+               },
                Event::PaymentClaimable {
                        payment_hash,
                        purpose,
                Event::PaymentClaimable {
                        payment_hash,
                        purpose,
@@ -251,7 +251,7 @@ async fn handle_ldk_events(
                                PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
                        };
                        channel_manager.claim_funds(payment_preimage.unwrap());
                                PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
                        };
                        channel_manager.claim_funds(payment_preimage.unwrap());
-               }
+               },
                Event::PaymentClaimed {
                        payment_hash,
                        purpose,
                Event::PaymentClaimed {
                        payment_hash,
                        purpose,
@@ -269,7 +269,7 @@ async fn handle_ldk_events(
                        let (payment_preimage, payment_secret) = match purpose {
                                PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
                                        (payment_preimage, Some(payment_secret))
                        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 mut inbound = inbound_payments.lock().unwrap();
                                PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None),
                        };
                        let mut inbound = inbound_payments.lock().unwrap();
@@ -279,7 +279,7 @@ async fn handle_ldk_events(
                                        payment.status = HTLCStatus::Succeeded;
                                        payment.preimage = payment_preimage;
                                        payment.secret = payment_secret;
                                        payment.status = HTLCStatus::Succeeded;
                                        payment.preimage = payment_preimage;
                                        payment.secret = payment_secret;
-                               }
+                               },
                                Entry::Vacant(e) => {
                                        e.insert(PaymentInfo {
                                                preimage: payment_preimage,
                                Entry::Vacant(e) => {
                                        e.insert(PaymentInfo {
                                                preimage: payment_preimage,
@@ -287,10 +287,10 @@ async fn handle_ldk_events(
                                                status: HTLCStatus::Succeeded,
                                                amt_msat: MillisatAmount(Some(amount_msat)),
                                        });
                                                status: HTLCStatus::Succeeded,
                                                amt_msat: MillisatAmount(Some(amount_msat)),
                                        });
-                               }
+                               },
                        }
                        fs_store.write("", "", INBOUND_PAYMENTS_FNAME, &inbound.encode()).unwrap();
                        }
                        fs_store.write("", "", INBOUND_PAYMENTS_FNAME, &inbound.encode()).unwrap();
-               }
+               },
                Event::PaymentSent {
                        payment_preimage, payment_hash, fee_paid_msat, payment_id, ..
                } => {
                Event::PaymentSent {
                        payment_preimage, payment_hash, fee_paid_msat, payment_id, ..
                } => {
@@ -316,7 +316,7 @@ async fn handle_ldk_events(
                                }
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
                                }
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::OpenChannelRequest {
                        ref temporary_channel_id, ref counterparty_node_id, ..
                } => {
                Event::OpenChannelRequest {
                        ref temporary_channel_id, ref counterparty_node_id, ..
                } => {
@@ -345,11 +345,11 @@ async fn handle_ldk_events(
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
-               Event::PaymentPathSuccessful { .. } => {}
-               Event::PaymentPathFailed { .. } => {}
-               Event::ProbeSuccessful { .. } => {}
-               Event::ProbeFailed { .. } => {}
+               },
+               Event::PaymentPathSuccessful { .. } => {},
+               Event::PaymentPathFailed { .. } => {},
+               Event::ProbeSuccessful { .. } => {},
+               Event::ProbeFailed { .. } => {},
                Event::PaymentFailed { payment_hash, reason, payment_id, .. } => {
                        print!(
                                "\nEVENT: Failed to send payment to payment hash {}: {:?}",
                Event::PaymentFailed { payment_hash, reason, payment_id, .. } => {
                        print!(
                                "\nEVENT: Failed to send payment to payment hash {}: {:?}",
@@ -365,7 +365,7 @@ async fn handle_ldk_events(
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::InvoiceRequestFailed { payment_id } => {
                        print!("\nEVENT: Failed to request invoice to send payment with id {}", payment_id);
                        print!("> ");
                Event::InvoiceRequestFailed { payment_id } => {
                        print!("\nEVENT: Failed to request invoice to send payment with id {}", payment_id);
                        print!("> ");
@@ -377,7 +377,7 @@ async fn handle_ldk_events(
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::PaymentForwarded {
                        prev_channel_id,
                        next_channel_id,
                Event::PaymentForwarded {
                        prev_channel_id,
                        next_channel_id,
@@ -400,10 +400,10 @@ async fn handle_ldk_events(
                                                                None => "unnamed node".to_string(),
                                                                Some(announcement) => {
                                                                        format!("node {}", announcement.alias)
                                                                None => "unnamed node".to_string(),
                                                                Some(announcement) => {
                                                                        format!("node {}", announcement.alias)
-                                                               }
+                                                               },
                                                        },
                                                }
                                                        },
                                                }
-                                       }
+                                       },
                                },
                        };
                        let channel_str = |channel_id: &Option<ChannelId>| {
                                },
                        };
                        let channel_str = |channel_id: &Option<ChannelId>| {
@@ -439,8 +439,8 @@ async fn handle_ldk_events(
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
-               Event::HTLCHandlingFailed { .. } => {}
+               },
+               Event::HTLCHandlingFailed { .. } => {},
                Event::PendingHTLCsForwardable { time_forwardable } => {
                        let forwarding_channel_manager = channel_manager.clone();
                        let min = time_forwardable.as_millis() as u64;
                Event::PendingHTLCsForwardable { time_forwardable } => {
                        let forwarding_channel_manager = channel_manager.clone();
                        let min = time_forwardable.as_millis() as u64;
@@ -449,7 +449,7 @@ async fn handle_ldk_events(
                                tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await;
                                forwarding_channel_manager.process_pending_htlc_forwards();
                        });
                                tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await;
                                forwarding_channel_manager.process_pending_htlc_forwards();
                        });
-               }
+               },
                Event::SpendableOutputs { outputs, channel_id: _ } => {
                        // SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
                        // of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
                Event::SpendableOutputs { outputs, channel_id: _ } => {
                        // SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
                        // of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
@@ -466,7 +466,7 @@ async fn handle_ldk_events(
                                let output: SpendableOutputDescriptor = output;
                                fs_store.write(PENDING_SPENDABLE_OUTPUT_DIR, "", &key, &output.encode()).unwrap();
                        }
                                let output: SpendableOutputDescriptor = output;
                                fs_store.write(PENDING_SPENDABLE_OUTPUT_DIR, "", &key, &output.encode()).unwrap();
                        }
-               }
+               },
                Event::ChannelPending { channel_id, counterparty_node_id, .. } => {
                        println!(
                                "\nEVENT: Channel {} with peer {} is pending awaiting funding lock-in!",
                Event::ChannelPending { channel_id, counterparty_node_id, .. } => {
                        println!(
                                "\nEVENT: Channel {} with peer {} is pending awaiting funding lock-in!",
@@ -475,7 +475,7 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
+               },
                Event::ChannelReady {
                        ref channel_id,
                        user_channel_id: _,
                Event::ChannelReady {
                        ref channel_id,
                        user_channel_id: _,
@@ -489,7 +489,7 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
+               },
                Event::ChannelClosed {
                        channel_id,
                        reason,
                Event::ChannelClosed {
                        channel_id,
                        reason,
@@ -506,12 +506,12 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
                        );
                        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.
                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.
-               }
-               Event::HTLCIntercepted { .. } => {}
+               },
+               Event::HTLCIntercepted { .. } => {},
                Event::BumpTransaction(event) => bump_tx_event_handler.handle_event(&event),
                Event::ConnectionNeeded { node_id, addresses } => {
                        tokio::spawn(async move {
                Event::BumpTransaction(event) => bump_tx_event_handler.handle_event(&event),
                Event::ConnectionNeeded { node_id, addresses } => {
                        tokio::spawn(async move {
@@ -526,7 +526,7 @@ async fn handle_ldk_events(
                                        }
                                }
                        });
                                        }
                                }
                        });
-               }
+               },
        }
 }
 
        }
 }
 
@@ -560,7 +560,7 @@ async fn start_ldk() {
                Err(e) => {
                        println!("Failed to connect to bitcoind client: {}", e);
                        return;
                Err(e) => {
                        println!("Failed to connect to bitcoind client: {}", e);
                        return;
-               }
+               },
        };
 
        // Check that the bitcoind we've connected to is running the network we expect
        };
 
        // Check that the bitcoind we've connected to is running the network we expect
@@ -607,11 +607,11 @@ async fn start_ldk() {
                        Ok(mut f) => {
                                Write::write_all(&mut f, &key).expect("Failed to write node keys seed to disk");
                                f.sync_all().expect("Failed to sync node keys seed to disk");
                        Ok(mut f) => {
                                Write::write_all(&mut f, &key).expect("Failed to write node keys seed to disk");
                                f.sync_all().expect("Failed to sync node keys seed to disk");
-                       }
+                       },
                        Err(e) => {
                                println!("ERROR: Unable to create keys seed file {}: {}", keys_seed_path, e);
                                return;
                        Err(e) => {
                                println!("ERROR: Unable to create keys seed file {}: {}", keys_seed_path, e);
                                return;
-                       }
+                       },
                }
                key
        };
                }
                key
        };
@@ -990,7 +990,7 @@ async fn start_ldk() {
                                                        }
                                                }
                                        }
                                                        }
                                                }
                                        }
-                               }
+                               },
                                Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
                        }
                }
                                Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
                        }
                }
index b4ee017914f57d7f1a90a44084bbb9f7a374213d..fbfa62c97c41c1b6ab5d69bed5e83c6cc7e5ac22 100644 (file)
@@ -101,7 +101,7 @@ pub(crate) async fn periodic_sweep(
                                        match file.read_exact(&mut [0; 1]) {
                                                Ok(_) => {
                                                        file.seek(SeekFrom::Current(-1)).unwrap();
                                        match file.read_exact(&mut [0; 1]) {
                                                Ok(_) => {
                                                        file.seek(SeekFrom::Current(-1)).unwrap();
-                                               }
+                                               },
                                                Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
                                                Err(e) => Err(e).unwrap(),
                                        }
                                                Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
                                                Err(e) => Err(e).unwrap(),
                                        }