Improve ChannelDetails readability significantly.
[rust-lightning] / lightning-invoice / src / utils.rs
index b90aad82d8263349c280f83a31b3bd86f3b60201..f419f5f7f24077aae76d6048d0935aac50cf3d6f 100644 (file)
@@ -1,15 +1,15 @@
 //! Convenient utilities to create an invoice.
-use {Currency, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
+use {Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
 use bech32::ToBase32;
 use bitcoin_hashes::Hash;
 use lightning::chain;
 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use lightning::chain::keysinterface::{Sign, KeysInterface};
 use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY};
-use lightning::ln::features::InvoiceFeatures;
 use lightning::routing::network_graph::RoutingFees;
-use lightning::routing::router::RouteHintHop;
+use lightning::routing::router::{RouteHint, RouteHintHop};
 use lightning::util::logger::Logger;
+use std::convert::TryInto;
 use std::ops::Deref;
 
 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
@@ -36,12 +36,12 @@ where
                        Some(id) => id,
                        None => continue,
                };
-               let forwarding_info = match channel.counterparty_forwarding_info {
+               let forwarding_info = match channel.counterparty.forwarding_info {
                        Some(info) => info,
                        None => continue,
                };
-               route_hints.push(vec![RouteHintHop {
-                       src_node_id: channel.remote_network_id,
+               route_hints.push(RouteHint(vec![RouteHintHop {
+                       src_node_id: channel.counterparty.node_id,
                        short_channel_id,
                        fees: RoutingFees {
                                base_msat: forwarding_info.fee_base_msat,
@@ -50,12 +50,12 @@ where
                        cltv_expiry_delta: forwarding_info.cltv_expiry_delta,
                        htlc_minimum_msat: None,
                        htlc_maximum_msat: None,
-               }]);
+               }]));
        }
 
        let (payment_hash, payment_secret) = channelmanager.create_inbound_payment(
                amt_msat,
-               7200, // default invoice expiry is 2 hours
+               DEFAULT_EXPIRY_TIME.try_into().unwrap(),
                0,
        );
        let our_node_pubkey = channelmanager.get_our_node_id();
@@ -65,13 +65,13 @@ where
                .payee_pub_key(our_node_pubkey)
                .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
                .payment_secret(payment_secret)
-               .features(InvoiceFeatures::known())
+               .basic_mpp()
                .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into());
        if let Some(amt) = amt_msat {
                invoice = invoice.amount_pico_btc(amt * 10);
        }
-       for hint in route_hints.drain(..) {
-               invoice = invoice.route(hint);
+       for hint in route_hints {
+               invoice = invoice.private_route(hint);
        }
 
        let raw_invoice = match invoice.build_raw() {
@@ -93,6 +93,7 @@ where
 mod test {
        use {Currency, Description, InvoiceDescription};
        use lightning::ln::PaymentHash;
+       use lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY;
        use lightning::ln::functional_test_utils::*;
        use lightning::ln::features::InitFeatures;
        use lightning::ln::msgs::ChannelMessageHandler;
@@ -108,17 +109,12 @@ mod test {
                let _chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
                let invoice = ::utils::create_invoice_from_channelmanager(&nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000), "test".to_string()).unwrap();
                assert_eq!(invoice.amount_pico_btc(), Some(100_000));
-               assert_eq!(invoice.min_final_cltv_expiry(), Some(9));
+               assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
                assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
 
-               let mut route_hints = invoice.routes().clone();
-               let mut last_hops = Vec::new();
-               for hint in route_hints.drain(..) {
-                       last_hops.push(hint[hint.len() - 1].clone());
-               }
                let amt_msat = invoice.amount_pico_btc().unwrap() / 10;
-
                let first_hops = nodes[0].node.list_usable_channels();
+               let last_hops = invoice.route_hints();
                let network_graph = nodes[0].net_graph_msg_handler.network_graph.read().unwrap();
                let logger = test_utils::TestLogger::new();
                let route = router::get_route(
@@ -127,9 +123,9 @@ mod test {
                        &invoice.recover_payee_pub_key(),
                        Some(invoice.features().unwrap().clone()),
                        Some(&first_hops.iter().collect::<Vec<_>>()),
-                       &last_hops.iter().collect::<Vec<_>>(),
+                       &last_hops,
                        amt_msat,
-                       invoice.min_final_cltv_expiry().unwrap() as u32,
+                       invoice.min_final_cltv_expiry() as u32,
                        &logger,
                ).unwrap();