Allow users to specify the `PaymentId` for new outbound payments
[rust-lightning] / lightning-invoice / src / utils.rs
index d287ea67283d6b62b6a290474c1a072a40565dd1..1ef7cdee0d46e8ad3e7dcf6d1fcdf70d8fd4ecc8 100644 (file)
@@ -1,7 +1,7 @@
 //! Convenient utilities to create an invoice.
 
-use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError};
-use payment::{Payer, Router};
+use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
+use crate::payment::{InFlightHtlcs, Payer, Router};
 
 use crate::{prelude::*, Description, InvoiceDescription, Sha256};
 use bech32::ToBase32;
@@ -15,15 +15,14 @@ use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, P
 use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
 use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
 use lightning::ln::msgs::LightningError;
-use lightning::routing::scoring::Score;
-use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
-use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route};
+use lightning::routing::gossip::{NetworkGraph, NodeId, RoutingFees};
+use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop};
+use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
 use lightning::util::logger::Logger;
 use secp256k1::PublicKey;
-use core::convert::TryInto;
 use core::ops::Deref;
 use core::time::Duration;
-use sync::Mutex;
+use crate::sync::Mutex;
 
 #[cfg(feature = "std")]
 /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
@@ -55,14 +54,20 @@ use sync::Mutex;
 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
-pub fn create_phantom_invoice<Signer: Sign, K: Deref>(
-       amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: String, invoice_expiry_delta_secs: u32,
-       phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K, network: Currency,
-) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface {
+pub fn create_phantom_invoice<Signer: Sign, K: Deref, L: Deref>(
+       amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: String,
+       invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
+       logger: L, network: Currency,
+) -> Result<Invoice, SignOrCreationError<()>>
+where
+       K::Target: KeysInterface,
+       L::Target: Logger,
+{
        let description = Description::new(description).map_err(SignOrCreationError::CreationError)?;
        let description = InvoiceDescription::Direct(&description,);
-       _create_phantom_invoice::<Signer, K>(
-               amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints, keys_manager, network,
+       _create_phantom_invoice::<Signer, K, L>(
+               amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
+               keys_manager, logger, network,
        )
 }
 
@@ -98,22 +103,30 @@ pub fn create_phantom_invoice<Signer: Sign, K: Deref>(
 /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
 /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
 /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
-pub fn create_phantom_invoice_with_description_hash<Signer: Sign, K: Deref>(
+pub fn create_phantom_invoice_with_description_hash<Signer: Sign, K: Deref, L: Deref>(
        amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, invoice_expiry_delta_secs: u32,
-       description_hash: Sha256, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K, network: Currency,
-) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface
+       description_hash: Sha256, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
+       logger: L, network: Currency
+) -> Result<Invoice, SignOrCreationError<()>>
+where
+       K::Target: KeysInterface,
+       L::Target: Logger,
 {
-       _create_phantom_invoice::<Signer, K>(
+       _create_phantom_invoice::<Signer, K, L>(
                amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash),
-               invoice_expiry_delta_secs, phantom_route_hints, keys_manager, network,
+               invoice_expiry_delta_secs, phantom_route_hints, keys_manager, logger, network,
        )
 }
 
 #[cfg(feature = "std")]
-fn _create_phantom_invoice<Signer: Sign, K: Deref>(
+fn _create_phantom_invoice<Signer: Sign, K: Deref, L: Deref>(
        amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: InvoiceDescription,
-       invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K, network: Currency,
-) -> Result<Invoice, SignOrCreationError<()>> where K::Target: KeysInterface
+       invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, keys_manager: K,
+       logger: L, network: Currency,
+) -> Result<Invoice, SignOrCreationError<()>>
+where
+       K::Target: KeysInterface,
+       L::Target: Logger,
 {
        use std::time::{SystemTime, UNIX_EPOCH};
 
@@ -122,6 +135,7 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
                        CreationError::MissingRouteHints,
                ));
        }
+
        let invoice = match description {
                InvoiceDescription::Direct(description) => {
                        InvoiceBuilder::new(network).description(description.0.clone())
@@ -158,6 +172,9 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
                .map_err(|_| SignOrCreationError::CreationError(CreationError::InvalidAmount))?
        };
 
+       log_trace!(logger, "Creating phantom invoice from {} participating nodes with payment hash {}",
+               phantom_route_hints.len(), log_bytes!(payment_hash.0));
+
        let mut invoice = invoice
                .current_timestamp()
                .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
@@ -169,7 +186,9 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
        }
 
        for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
-               let mut route_hints = filter_channels(channels, amt_msat);
+               log_trace!(logger, "Generating phantom route hints for node {}",
+                       log_pubkey!(real_node_pubkey));
+               let mut route_hints = filter_channels(channels, amt_msat, &logger);
 
                // If we have any public channel, the route hints from `filter_channels` will be empty.
                // In that case we create a RouteHint on which we will push a single hop with the phantom
@@ -213,9 +232,12 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
 /// that the payment secret is valid when the invoice is paid.
+///
+/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
+/// in excess of the current time.
 pub fn create_invoice_from_channelmanager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
-       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
-       amt_msat: Option<u64>, description: String
+       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, logger: L,
+       network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32
 ) -> Result<Invoice, SignOrCreationError<()>>
 where
        M::Target: chain::Watch<Signer>,
@@ -228,7 +250,8 @@ where
        let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
                .expect("for the foreseeable future this shouldn't happen");
        create_invoice_from_channelmanager_and_duration_since_epoch(
-               channelmanager, keys_manager, network, amt_msat, description, duration
+               channelmanager, keys_manager, logger, network, amt_msat, description, duration,
+               invoice_expiry_delta_secs
        )
 }
 
@@ -239,9 +262,13 @@ where
 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
 /// that the payment secret is valid when the invoice is paid.
 /// Use this variant if you want to pass the `description_hash` to the invoice.
+///
+/// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
+/// in excess of the current time.
 pub fn create_invoice_from_channelmanager_with_description_hash<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
-       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
-       amt_msat: Option<u64>, description_hash: Sha256,
+       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, logger: L,
+       network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
+       invoice_expiry_delta_secs: u32
 ) -> Result<Invoice, SignOrCreationError<()>>
 where
        M::Target: chain::Watch<Signer>,
@@ -257,7 +284,8 @@ where
                .expect("for the foreseeable future this shouldn't happen");
 
        create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
-               channelmanager, keys_manager, network, amt_msat, description_hash, duration,
+               channelmanager, keys_manager, logger, network, amt_msat,
+               description_hash, duration, invoice_expiry_delta_secs
        )
 }
 
@@ -265,8 +293,9 @@ where
 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
 /// available and the current time is supplied by the caller.
 pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
-       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
-       amt_msat: Option<u64>, description_hash: Sha256, duration_since_epoch: Duration,
+       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, logger: L,
+       network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
+       duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
 ) -> Result<Invoice, SignOrCreationError<()>>
 where
        M::Target: chain::Watch<Signer>,
@@ -276,9 +305,9 @@ where
        L::Target: Logger,
 {
        _create_invoice_from_channelmanager_and_duration_since_epoch(
-               channelmanager, keys_manager, network, amt_msat,
+               channelmanager, keys_manager, logger, network, amt_msat,
                InvoiceDescription::Hash(&description_hash),
-               duration_since_epoch,
+               duration_since_epoch, invoice_expiry_delta_secs
        )
 }
 
@@ -286,8 +315,9 @@ where
 /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
 /// available and the current time is supplied by the caller.
 pub fn create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
-       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
-       amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
+       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, logger: L,
+       network: Currency, amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
+       invoice_expiry_delta_secs: u32
 ) -> Result<Invoice, SignOrCreationError<()>>
 where
        M::Target: chain::Watch<Signer>,
@@ -297,17 +327,18 @@ where
        L::Target: Logger,
 {
        _create_invoice_from_channelmanager_and_duration_since_epoch(
-               channelmanager, keys_manager, network, amt_msat,
+               channelmanager, keys_manager, logger, network, amt_msat,
                InvoiceDescription::Direct(
                        &Description::new(description).map_err(SignOrCreationError::CreationError)?,
                ),
-               duration_since_epoch,
+               duration_since_epoch, invoice_expiry_delta_secs
        )
 }
 
 fn _create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
-       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
-       amt_msat: Option<u64>, description: InvoiceDescription, duration_since_epoch: Duration,
+       channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, logger: L,
+       network: Currency, amt_msat: Option<u64>, description: InvoiceDescription,
+       duration_since_epoch: Duration, invoice_expiry_delta_secs: u32
 ) -> Result<Invoice, SignOrCreationError<()>>
 where
        M::Target: chain::Watch<Signer>,
@@ -316,14 +347,15 @@ where
        F::Target: FeeEstimator,
        L::Target: Logger,
 {
-       let route_hints = filter_channels(channelmanager.list_usable_channels(), amt_msat);
-
        // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
        // supply.
        let (payment_hash, payment_secret) = channelmanager
-               .create_inbound_payment(amt_msat, DEFAULT_EXPIRY_TIME.try_into().unwrap())
+               .create_inbound_payment(amt_msat, invoice_expiry_delta_secs)
                .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
        let our_node_pubkey = channelmanager.get_our_node_id();
+       let channels = channelmanager.list_channels();
+
+       log_trace!(logger, "Creating invoice with payment hash {}", log_bytes!(payment_hash.0));
 
        let invoice = match description {
                InvoiceDescription::Direct(description) => {
@@ -338,10 +370,13 @@ where
                .payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
                .payment_secret(payment_secret)
                .basic_mpp()
-               .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into());
+               .min_final_cltv_expiry(MIN_FINAL_CLTV_EXPIRY.into())
+               .expiry_time(Duration::from_secs(invoice_expiry_delta_secs.into()));
        if let Some(amt) = amt_msat {
                invoice = invoice.amount_milli_satoshis(amt);
        }
+
+       let route_hints = filter_channels(channels, amt_msat, &logger);
        for hint in route_hints {
                invoice = invoice.private_route(hint);
        }
@@ -366,35 +401,68 @@ where
 /// The filtering is based on the following criteria:
 /// * Only one channel per counterparty node
 /// * Always select the channel with the highest inbound capacity per counterparty node
-/// * Filter out channels with a lower inbound capacity than `min_inbound_capacity_msat`, if any
-/// channel with a higher or equal inbound capacity than `min_inbound_capacity_msat` exists
+/// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
+///   `is_usable` (i.e. the peer is connected).
 /// * If any public channel exists, the returned `RouteHint`s will be empty, and the sender will
-/// need to find the path by looking at the public channels instead
-fn filter_channels(channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>) -> Vec<RouteHint>{
-       let mut filtered_channels: HashMap<PublicKey, &ChannelDetails> = HashMap::new();
+///   need to find the path by looking at the public channels instead
+fn filter_channels<L: Deref>(
+       channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>, logger: &L
+) -> Vec<RouteHint> where L::Target: Logger {
+       let mut filtered_channels: HashMap<PublicKey, ChannelDetails> = HashMap::new();
        let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0);
        let mut min_capacity_channel_exists = false;
+       let mut online_channel_exists = false;
+       let mut online_min_capacity_channel_exists = false;
 
-       for channel in channels.iter() {
+       log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
+       for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
                if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
+                       log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id));
                        continue;
                }
 
                if channel.is_public {
                        // If any public channel exists, return no hints and let the sender
                        // look at the public channels instead.
+                       log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
+                               log_bytes!(channel.channel_id));
                        return vec![]
                }
 
                if channel.inbound_capacity_msat >= min_inbound_capacity {
-                       min_capacity_channel_exists = true;
-               };
+                       if !min_capacity_channel_exists {
+                               log_trace!(logger, "Channel with enough inbound capacity exists for invoice route hints");
+                               min_capacity_channel_exists = true;
+                       }
+
+                       if channel.is_usable {
+                               online_min_capacity_channel_exists = true;
+                       }
+               }
+
+               if channel.is_usable {
+                       if !online_channel_exists {
+                               log_trace!(logger, "Channel with connected peer exists for invoice route hints");
+                               online_channel_exists = true;
+                       }
+               }
+
                match filtered_channels.entry(channel.counterparty.node_id) {
                        hash_map::Entry::Occupied(mut entry) => {
                                let current_max_capacity = entry.get().inbound_capacity_msat;
                                if channel.inbound_capacity_msat < current_max_capacity {
+                                       log_trace!(logger,
+                                               "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
+                                               log_pubkey!(channel.counterparty.node_id),
+                                               log_bytes!(entry.get().channel_id), current_max_capacity,
+                                               log_bytes!(channel.channel_id), channel.inbound_capacity_msat);
                                        continue;
                                }
+                               log_trace!(logger,
+                                       "Preferring counterparty {} channel {} ({} msats) over {} ({} msats) for invoice route hints",
+                                       log_pubkey!(channel.counterparty.node_id),
+                                       log_bytes!(channel.channel_id), channel.inbound_capacity_msat,
+                                       log_bytes!(entry.get().channel_id), current_max_capacity);
                                entry.insert(channel);
                        }
                        hash_map::Entry::Vacant(entry) => {
@@ -403,7 +471,7 @@ fn filter_channels(channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Opt
                }
        }
 
-       let route_hint_from_channel = |channel: &ChannelDetails| {
+       let route_hint_from_channel = |channel: ChannelDetails| {
                let forwarding_info = channel.counterparty.forwarding_info.as_ref().unwrap();
                RouteHint(vec![RouteHintHop {
                        src_node_id: channel.counterparty.node_id,
@@ -416,46 +484,104 @@ fn filter_channels(channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Opt
                        htlc_minimum_msat: channel.inbound_htlc_minimum_msat,
                        htlc_maximum_msat: channel.inbound_htlc_maximum_msat,}])
        };
-       // If all channels are private, return the route hint for the highest inbound capacity channel
-       // per counterparty node. If channels with an higher inbound capacity than the
-       // min_inbound_capacity exists, filter out the channels with a lower capacity than that.
-       filtered_channels.into_iter()
-               .filter(|(_counterparty_id, channel)| {
-                       min_capacity_channel_exists && channel.inbound_capacity_msat >= min_inbound_capacity ||
-                       !min_capacity_channel_exists
+       // If all channels are private, prefer to return route hints which have a higher capacity than
+       // the payment value and where we're currently connected to the channel counterparty.
+       // Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
+       // those which meet at least one criteria.
+       filtered_channels
+               .into_iter()
+               .map(|(_, channel)| channel)
+               .filter(|channel| {
+                       let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity;
+                       let include_channel = if online_min_capacity_channel_exists {
+                               has_enough_capacity && channel.is_usable
+                       } else if min_capacity_channel_exists && online_channel_exists {
+                               // If there are some online channels and some min_capacity channels, but no
+                               // online-and-min_capacity channels, just include the min capacity ones and ignore
+                               // online-ness.
+                               has_enough_capacity
+                       } else if min_capacity_channel_exists {
+                               has_enough_capacity
+                       } else if online_channel_exists {
+                               channel.is_usable
+                       } else { true };
+
+                       if include_channel {
+                               log_trace!(logger, "Including channel {} in invoice route hints",
+                                       log_bytes!(channel.channel_id));
+                       } else if !has_enough_capacity {
+                               log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints",
+                                       log_bytes!(channel.channel_id));
+                       } else {
+                               debug_assert!(!channel.is_usable);
+                               log_trace!(logger, "Ignoring channel {} with disconnected peer",
+                                       log_bytes!(channel.channel_id));
+                       }
+
+                       include_channel
                })
-               .map(|(_counterparty_id, channel)| route_hint_from_channel(&channel))
+               .map(route_hint_from_channel)
                .collect::<Vec<RouteHint>>()
 }
 
 /// A [`Router`] implemented using [`find_route`].
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph>, L: Deref> where L::Target: Logger {
+pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> where
+       L::Target: Logger,
+       S::Target: for <'a> LockableScore<'a>,
+{
        network_graph: G,
        logger: L,
        random_seed_bytes: Mutex<[u8; 32]>,
+       scorer: S
 }
 
-impl<G: Deref<Target = NetworkGraph>, L: Deref> DefaultRouter<G, L> where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> DefaultRouter<G, L, S> where
+       L::Target: Logger,
+       S::Target: for <'a> LockableScore<'a>,
+{
        /// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
        /// `random_seed_bytes`.
-       pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32]) -> Self {
+       pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S) -> Self {
                let random_seed_bytes = Mutex::new(random_seed_bytes);
-               Self { network_graph, logger, random_seed_bytes }
+               Self { network_graph, logger, random_seed_bytes, scorer }
        }
 }
 
-impl<G: Deref<Target = NetworkGraph>, L: Deref, S: Score> Router<S> for DefaultRouter<G, L>
-where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for DefaultRouter<G, L, S> where
+       L::Target: Logger,
+       S::Target: for <'a> LockableScore<'a>,
+{
        fn find_route(
                &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
-               first_hops: Option<&[&ChannelDetails]>, scorer: &S
+               first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs
        ) -> Result<Route, LightningError> {
                let random_seed_bytes = {
                        let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
                        *locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
                        *locked_random_seed_bytes
                };
-               find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes)
+
+               find_route(
+                       payer, params, &self.network_graph, first_hops, &*self.logger,
+                       &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs),
+                       &random_seed_bytes
+               )
+       }
+
+       fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
+               self.scorer.lock().payment_path_failed(path, short_channel_id);
+       }
+
+       fn notify_payment_path_successful(&self, path: &[&RouteHop]) {
+               self.scorer.lock().payment_path_successful(path);
+       }
+
+       fn notify_payment_probe_successful(&self, path: &[&RouteHop]) {
+               self.scorer.lock().probe_successful(path);
+       }
+
+       fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) {
+               self.scorer.lock().probe_failed(path, short_channel_id);
        }
 }
 
@@ -478,14 +604,16 @@ where
        fn send_payment(
                &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>
        ) -> Result<PaymentId, PaymentSendFailure> {
-               self.send_payment(route, payment_hash, payment_secret)
+               let payment_id = PaymentId(payment_hash.0);
+               self.send_payment(route, payment_hash, payment_secret, payment_id).map(|()| payment_id)
        }
 
        fn send_spontaneous_payment(
                &self, route: &Route, payment_preimage: PaymentPreimage,
        ) -> Result<PaymentId, PaymentSendFailure> {
-               self.send_spontaneous_payment(route, Some(payment_preimage))
-                       .map(|(_, payment_id)| payment_id)
+               let payment_id = PaymentId(sha256::Hash::hash(&payment_preimage.0).into_inner());
+               self.send_spontaneous_payment(route, Some(payment_preimage), payment_id)
+                       .map(|_| payment_id)
        }
 
        fn retry_payment(
@@ -499,17 +627,64 @@ where
        }
 }
 
+
+/// Used to store information about all the HTLCs that are inflight across all payment attempts.
+pub(crate) struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
+       scorer: &'a mut S,
+       /// Maps a channel's short channel id and its direction to the liquidity used up.
+       inflight_htlcs: InFlightHtlcs,
+}
+
+impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
+       pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
+               ScorerAccountingForInFlightHtlcs {
+                       scorer,
+                       inflight_htlcs
+               }
+       }
+}
+
+#[cfg(c_bindings)]
+impl<'a, S:Score> lightning::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
+       fn write<W: lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { self.scorer.write(writer) }
+}
+
+impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
+       fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
+               if let Some(used_liqudity) = self.inflight_htlcs.used_liquidity_msat(
+                       source, target, short_channel_id
+               ) {
+                       let usage = ChannelUsage {
+                               inflight_htlc_msat: usage.inflight_htlc_msat + used_liqudity,
+                               ..usage
+                       };
+
+                       self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
+               } else {
+                       self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
+               }
+       }
+
+       fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
+
+       fn payment_path_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
+
+       fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
+
+       fn probe_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
+}
+
+
 #[cfg(test)]
 mod test {
        use core::time::Duration;
-       use {Currency, Description, InvoiceDescription};
+       use crate::{Currency, Description, InvoiceDescription};
        use bitcoin_hashes::Hash;
        use bitcoin_hashes::sha256::Hash as Sha256;
        use lightning::chain::keysinterface::PhantomKeysManager;
        use lightning::ln::{PaymentPreimage, PaymentHash};
-       use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY};
+       use lightning::ln::channelmanager::{self, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY, PaymentId};
        use lightning::ln::functional_test_utils::*;
-       use lightning::ln::features::InitFeatures;
        use lightning::ln::msgs::ChannelMessageHandler;
        use lightning::routing::router::{PaymentParameters, RouteParameters, find_route};
        use lightning::util::enforcing_trait_impls::EnforcingSigner;
@@ -517,7 +692,7 @@ mod test {
        use lightning::util::test_utils;
        use lightning::util::config::UserConfig;
        use lightning::chain::keysinterface::KeysInterface;
-       use utils::create_invoice_from_channelmanager_and_duration_since_epoch;
+       use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
        use std::collections::HashSet;
 
        #[test]
@@ -526,13 +701,16 @@ mod test {
                let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
-               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let non_default_invoice_expiry_secs = 4200;
                let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
-                       &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000), "test".to_string(),
-                       Duration::from_secs(1234567)).unwrap();
+                       &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
+                       Some(10_000), "test".to_string(), Duration::from_secs(1234567),
+                       non_default_invoice_expiry_secs).unwrap();
                assert_eq!(invoice.amount_pico_btc(), Some(100_000));
                assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
                assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
+               assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
 
                // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
                // available.
@@ -553,19 +731,19 @@ mod test {
                        final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
                };
                let first_hops = nodes[0].node.list_usable_channels();
-               let network_graph = node_cfgs[0].network_graph;
+               let network_graph = &node_cfgs[0].network_graph;
                let logger = test_utils::TestLogger::new();
                let scorer = test_utils::TestScorer::with_penalty(0);
                let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
                let route = find_route(
-                       &nodes[0].node.get_our_node_id(), &route_params, network_graph,
+                       &nodes[0].node.get_our_node_id(), &route_params, &network_graph,
                        Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
                ).unwrap();
 
                let payment_event = {
                        let mut payment_hash = PaymentHash([0; 32]);
                        payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
-                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
+                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
                        let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
                        assert_eq!(added_monitors.len(), 1);
                        added_monitors.clear();
@@ -591,9 +769,9 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
                let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes()));
-               let invoice = ::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
-                       &nodes[1].node, nodes[1].keys_manager, Currency::BitcoinTestnet, Some(10_000),
-                       description_hash, Duration::from_secs(1234567),
+               let invoice = crate::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
+                       &nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
+                       Some(10_000), description_hash, Duration::from_secs(1234567), 3600
                ).unwrap();
                assert_eq!(invoice.amount_pico_btc(), Some(100_000));
                assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
@@ -607,8 +785,8 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
-               let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let mut scid_aliases = HashSet::new();
                scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
@@ -623,9 +801,9 @@ mod test {
                let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
                let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
-               let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
-               let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
+               let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let mut scid_aliases = HashSet::new();
                scid_aliases.insert(chan_1_0_high_inbound_capacity.0.short_channel_id_alias.unwrap());
@@ -633,24 +811,53 @@ mod test {
                match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
        }
 
+       #[test]
+       fn test_hints_has_only_online_channels() {
+               let chanmon_cfgs = create_chanmon_cfgs(4);
+               let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
+               let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
+               let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
+               let chan_a = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_b = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let _chan_c = create_unannounced_chan_between_nodes_with_value(&nodes, 3, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+
+               // With all peers connected we should get all hints that have sufficient value
+               let mut scid_aliases = HashSet::new();
+               scid_aliases.insert(chan_a.0.short_channel_id_alias.unwrap());
+               scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
+
+               match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
+
+               // With only one sufficient-value peer connected we should only get its hint
+               scid_aliases.remove(&chan_b.0.short_channel_id_alias.unwrap());
+               nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
+               match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
+
+               // If we don't have any sufficient-value peers connected we should get all hints with
+               // sufficient value, even though there is a connected insufficient-value peer.
+               scid_aliases.insert(chan_b.0.short_channel_id_alias.unwrap());
+               nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
+               match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases);
+       }
+
        #[test]
        fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
                let chanmon_cfgs = create_chanmon_cfgs(3);
                let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
-               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                // Create an unannonced channel between `nodes[2]` and `nodes[0]`, for which the
                // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
                // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
                let mut private_chan_cfg = UserConfig::default();
-               private_chan_cfg.channel_options.announced_channel = false;
+               private_chan_cfg.channel_handshake_config.announced_channel = false;
                let temporary_channel_id = nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap();
                let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
-               nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &open_channel);
+               nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
                let accept_channel = get_event_msg!(nodes[0], MessageSendEvent::SendAcceptChannel, nodes[2].node.get_our_node_id());
-               nodes[2].node.handle_accept_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
+               nodes[2].node.handle_accept_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
 
                let tx = sign_funding_transaction(&nodes[2], &nodes[0], 1_000_000, temporary_channel_id);
 
@@ -659,10 +866,10 @@ mod test {
                connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
                confirm_transaction_at(&nodes[0], &tx, conf_height);
                connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH - 1);
-               let as_funding_locked = get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id());
-               nodes[2].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id()));
+               let as_channel_ready = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id());
+               nodes[2].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id()));
                get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
-               nodes[0].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &as_funding_locked);
+               nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready);
                get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
 
                // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second
@@ -679,9 +886,9 @@ mod test {
                let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
-               let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
-               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
                nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
 
@@ -697,11 +904,11 @@ mod test {
                let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
-               let chan_1_0 = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_1_0 = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_1_0.0);
                nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_1_0.1);
 
-               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
                nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
 
@@ -715,8 +922,8 @@ mod test {
                let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
-               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
+               let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                // As the invoice amt is 1 msat above chan_1_0's inbound capacity, it shouldn't be included
                let mut scid_aliases_99_000_001_msat = HashSet::new();
@@ -752,8 +959,9 @@ mod test {
                mut chan_ids_to_match: HashSet<u64>
        ) {
                let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
-                       &invoice_node.node, invoice_node.keys_manager, Currency::BitcoinTestnet, invoice_amt, "test".to_string(),
-                       Duration::from_secs(1234567)).unwrap();
+                       &invoice_node.node, invoice_node.keys_manager, invoice_node.logger,
+                       Currency::BitcoinTestnet, invoice_amt, "test".to_string(), Duration::from_secs(1234567),
+                       3600).unwrap();
                let hints = invoice.private_routes();
 
                for hint in hints {
@@ -781,10 +989,10 @@ mod test {
                let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
-               let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan_0_1.1);
                nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_1.0);
-               let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
                nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
 
@@ -802,12 +1010,11 @@ mod test {
                };
                let non_default_invoice_expiry_secs = 4200;
 
-               let invoice = ::utils::create_phantom_invoice::<
-                       EnforcingSigner, &test_utils::TestKeysInterface
-               >(
-                       Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
-                       route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet
-               ).unwrap();
+               let invoice =
+                       crate::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface, &test_utils::TestLogger>(
+                               Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
+                               route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
+                       ).unwrap();
                let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
                let payment_preimage = if user_generated_pmt_hash {
                        user_payment_preimage
@@ -830,18 +1037,18 @@ mod test {
                        final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
                };
                let first_hops = nodes[0].node.list_usable_channels();
-               let network_graph = node_cfgs[0].network_graph;
+               let network_graph = &node_cfgs[0].network_graph;
                let logger = test_utils::TestLogger::new();
                let scorer = test_utils::TestScorer::with_penalty(0);
                let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
                let route = find_route(
-                       &nodes[0].node.get_our_node_id(), &params, network_graph,
+                       &nodes[0].node.get_our_node_id(), &params, &network_graph,
                        Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
                ).unwrap();
                let (payment_event, fwd_idx) = {
                        let mut payment_hash = PaymentHash([0; 32]);
                        payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]);
-                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap();
+                       nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap();
                        let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
                        assert_eq!(added_monitors.len(), 1);
                        added_monitors.clear();
@@ -903,8 +1110,8 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
-               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let payment_amt = 20_000;
                let (payment_hash, _payment_secret) = nodes[1].node.create_inbound_payment(Some(payment_amt), 3600).unwrap();
@@ -913,7 +1120,7 @@ mod test {
                        nodes[2].node.get_phantom_route_hints(),
                ];
 
-               let invoice = ::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface>(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet).unwrap();
+               let invoice = crate::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet).unwrap();
 
                let chan_0_1 = &nodes[1].node.list_usable_channels()[0];
                assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat);
@@ -940,11 +1147,11 @@ mod test {
 
                let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes()));
                let non_default_invoice_expiry_secs = 4200;
-               let invoice = ::utils::create_phantom_invoice_with_description_hash::<
-                       EnforcingSigner, &test_utils::TestKeysInterface,
+               let invoice = crate::utils::create_phantom_invoice_with_description_hash::<
+                       EnforcingSigner, &test_utils::TestKeysInterface, &test_utils::TestLogger,
                >(
                        Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash,
-                       route_hints, &nodes[1].keys_manager, Currency::BitcoinTestnet
+                       route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet
                )
                .unwrap();
                assert_eq!(invoice.amount_pico_btc(), Some(200_000));
@@ -966,8 +1173,8 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
-               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let mut scid_aliases = HashSet::new();
                scid_aliases.insert(chan_0_1.0.short_channel_id_alias.unwrap());
@@ -995,9 +1202,9 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
                let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
-               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, InitFeatures::known(), InitFeatures::known());
-               let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 3_000_000, 10005, InitFeatures::known(), InitFeatures::known());
+               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 3_000_000, 10005, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let mut scid_aliases = HashSet::new();
                scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
@@ -1026,19 +1233,19 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
                let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
-               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 1000000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                // Create an unannonced channel between `nodes[1]` and `nodes[3]`, for which the
                // `msgs::ChannelUpdate` is never handled for the node(s). As the `msgs::ChannelUpdate`
                // is never handled, the `channel.counterparty.forwarding_info` is never assigned.
                let mut private_chan_cfg = UserConfig::default();
-               private_chan_cfg.channel_options.announced_channel = false;
+               private_chan_cfg.channel_handshake_config.announced_channel = false;
                let temporary_channel_id = nodes[1].node.create_channel(nodes[3].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap();
                let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id());
-               nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel);
+               nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel);
                let accept_channel = get_event_msg!(nodes[3], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
-               nodes[1].node.handle_accept_channel(&nodes[3].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
+               nodes[1].node.handle_accept_channel(&nodes[3].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel);
 
                let tx = sign_funding_transaction(&nodes[1], &nodes[3], 1_000_000, temporary_channel_id);
 
@@ -1047,10 +1254,10 @@ mod test {
                connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
                confirm_transaction_at(&nodes[3], &tx, conf_height);
                connect_blocks(&nodes[3], CHAN_CONFIRM_DEPTH - 1);
-               let as_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[3].node.get_our_node_id());
-               nodes[1].node.handle_funding_locked(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
+               let as_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[3].node.get_our_node_id());
+               nodes[1].node.handle_channel_ready(&nodes[3].node.get_our_node_id(), &get_event_msg!(nodes[3], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
                get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
-               nodes[3].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked);
+               nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready);
                get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
 
                // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third
@@ -1082,9 +1289,9 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_1 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
-               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_2_0 = create_announced_chan_between_nodes_with_value(&nodes, 2, 0, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_2_0.1);
                nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_2_0.0);
 
@@ -1115,12 +1322,12 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
                let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
                nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan_0_2.1);
                nodes[2].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &chan_0_2.0);
-               let _chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let _chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
-               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                // Hints should include `chan_0_3` from as `nodes[3]` only have private channels, and no
                // channels for `nodes[2]` as it contains a mix of public and private channels.
@@ -1149,10 +1356,10 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
                let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-               let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known());
-               let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
+               let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                let mut scid_aliases = HashSet::new();
                scid_aliases.insert(chan_0_1_high_inbound_capacity.0.short_channel_id_alias.unwrap());
@@ -1180,9 +1387,9 @@ mod test {
                let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
                let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
 
-               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100_000, 0, InitFeatures::known(), InitFeatures::known());
-               let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 200_000, 0, InitFeatures::known(), InitFeatures::known());
+               let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 100_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
+               let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 200_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
 
                // Since the invoice 1 msat above chan_0_3's inbound capacity, it should be filtered out.
                let mut scid_aliases_99_000_001_msat = HashSet::new();
@@ -1256,7 +1463,7 @@ mod test {
                        .map(|route_hint| route_hint.phantom_scid)
                        .collect::<HashSet<u64>>();
 
-               let invoice = ::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface>(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, Currency::BitcoinTestnet).unwrap();
+               let invoice = crate::utils::create_phantom_invoice::<EnforcingSigner, &test_utils::TestKeysInterface, &test_utils::TestLogger>(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, &invoice_node.logger, Currency::BitcoinTestnet).unwrap();
 
                let invoice_hints = invoice.private_routes();