//! Convenient utilities to create an invoice.
use {CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
-use payment::{Payer, Router};
+use payment::{InFlightHtlcs, Payer, Router};
use crate::{prelude::*, Description, InvoiceDescription, Sha256};
use bech32::ToBase32;
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::gossip::{NetworkGraph, RoutingFees};
-use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route};
-use lightning::routing::scoring::Score;
+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::ops::Deref;
F::Target: FeeEstimator,
L::Target: Logger,
{
- let route_hints = filter_channels(channelmanager.list_usable_channels(), amt_msat);
+ let route_hints = filter_channels(channelmanager.list_channels(), amt_msat);
// `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
// supply.
/// 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
+/// 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();
+ 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() {
+ 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() {
continue;
}
if channel.inbound_capacity_msat >= min_inbound_capacity {
min_capacity_channel_exists = true;
- };
+ if channel.is_usable {
+ online_min_capacity_channel_exists = true;
+ }
+ }
+ if channel.is_usable {
+ 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;
}
}
- 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,
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| {
+ if online_min_capacity_channel_exists {
+ channel.inbound_capacity_msat >= min_inbound_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.
+ channel.inbound_capacity_msat >= min_inbound_capacity
+ } else if min_capacity_channel_exists {
+ channel.inbound_capacity_msat >= min_inbound_capacity
+ } else if online_channel_exists {
+ channel.is_usable
+ } else { true }
})
- .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>>, 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>>, 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>>, L: Deref> Router for DefaultRouter<G, L>
-where L::Target: Logger {
- fn find_route<S: Score>(
+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);
}
}
}
}
+
+/// 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;
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);