//! The router finds paths within a [`NetworkGraph`] for a payment.
-use bitcoin::secp256k1::PublicKey;
+use bitcoin::secp256k1::{PublicKey, Secp256k1, self};
use bitcoin::hashes::Hash;
use bitcoin::hashes::sha256::Hash as Sha256;
use crate::ln::features::{Bolt11InvoiceFeatures, Bolt12InvoiceFeatures, ChannelFeatures, NodeFeatures};
use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice};
+use crate::onion_message::{DefaultMessageRouter, Destination, MessageRouter, OnionMessagePath};
use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
use crate::routing::scoring::{ChannelUsage, LockableScore, ScoreLookUp};
+use crate::sign::EntropySource;
use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
use crate::util::logger::{Level, Logger};
use crate::util::chacha20::ChaCha20;
use core::ops::Deref;
/// A [`Router`] implemented using [`find_route`].
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
+pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
{
logger: L,
random_seed_bytes: Mutex<[u8; 32]>,
scorer: S,
- score_params: SP
+ score_params: SP,
+ message_router: DefaultMessageRouter<G, L>,
}
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
+impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
{
/// Creates a new router.
pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: SP) -> Self {
let random_seed_bytes = Mutex::new(random_seed_bytes);
- Self { network_graph, logger, random_seed_bytes, scorer, score_params }
+ let message_router = DefaultMessageRouter::new(network_graph.clone());
+ Self { network_graph, logger, random_seed_bytes, scorer, score_params, message_router }
}
}
-impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
+impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
{
}
}
+impl< G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> MessageRouter for DefaultRouter<G, L, S, SP, Sc> where
+ L::Target: Logger,
+ S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
+{
+ fn find_path(
+ &self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+ ) -> Result<OnionMessagePath, ()> {
+ self.message_router.find_path(sender, peers, destination)
+ }
+
+ fn create_blinded_paths<
+ ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification
+ >(
+ &self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
+ secp_ctx: &Secp256k1<T>
+ ) -> Result<Vec<BlindedPath>, ()> {
+ self.message_router.create_blinded_paths(recipient, peers, entropy_source, secp_ctx)
+ }
+}
+
/// A trait defining behavior for routing a payment.
-pub trait Router {
+pub trait Router: MessageRouter {
/// Finds a [`Route`] for a payment between the given `payer` and a payee.
///
/// The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
});
#[derive(Eq, PartialEq)]
+#[repr(align(64))] // Force the size to 64 bytes
struct RouteGraphNode {
node_id: NodeId,
- lowest_fee_to_node: u64,
- total_cltv_delta: u32,
+ score: u64,
// The maximum value a yet-to-be-constructed payment path might flow through this node.
// This value is upper-bounded by us by:
// - how much is needed for a path being constructed
// - how much value can channels following this node (up to the destination) can contribute,
// considering their capacity and fees
value_contribution_msat: u64,
- /// The effective htlc_minimum_msat at this hop. If a later hop on the path had a higher HTLC
- /// minimum, we use it, plus the fees required at each earlier hop to meet it.
- path_htlc_minimum_msat: u64,
- /// All penalties incurred from this hop on the way to the destination, as calculated using
- /// channel scoring.
- path_penalty_msat: u64,
+ total_cltv_delta: u32,
/// The number of hops walked up to this node.
path_length_to_node: u8,
}
impl cmp::Ord for RouteGraphNode {
fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
- let other_score = cmp::max(other.lowest_fee_to_node, other.path_htlc_minimum_msat)
- .saturating_add(other.path_penalty_msat);
- let self_score = cmp::max(self.lowest_fee_to_node, self.path_htlc_minimum_msat)
- .saturating_add(self.path_penalty_msat);
- other_score.cmp(&self_score).then_with(|| other.node_id.cmp(&self.node_id))
+ other.score.cmp(&self.score).then_with(|| other.node_id.cmp(&self.node_id))
}
}
}
}
+// While RouteGraphNode can be laid out with fewer bytes, performance appears to be improved
+// substantially when it is laid out at exactly 64 bytes.
+//
+// Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
+// bytes here.
+#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
+const _GRAPH_NODE_SMALL: usize = 64 - core::mem::size_of::<RouteGraphNode>();
+#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
+const _GRAPH_NODE_FIXED_SIZE: usize = core::mem::size_of::<RouteGraphNode>() - 64;
+
/// A wrapper around the various hop representations.
///
/// Can be used to examine the properties of a hop,
score_params);
let path_penalty_msat = $next_hops_path_penalty_msat
.saturating_add(channel_penalty_msat);
- let new_graph_node = RouteGraphNode {
- node_id: src_node_id,
- lowest_fee_to_node: total_fee_msat,
- total_cltv_delta: hop_total_cltv_delta,
- value_contribution_msat,
- path_htlc_minimum_msat,
- path_penalty_msat,
- path_length_to_node,
- };
// Update the way of reaching $candidate.source()
// with the given short_channel_id (from $candidate.target()),
.saturating_add(path_penalty_msat);
if !old_entry.was_processed && new_cost < old_cost {
+ let new_graph_node = RouteGraphNode {
+ node_id: src_node_id,
+ score: cmp::max(total_fee_msat, path_htlc_minimum_msat).saturating_add(path_penalty_msat),
+ total_cltv_delta: hop_total_cltv_delta,
+ value_contribution_msat,
+ path_length_to_node,
+ };
targets.push(new_graph_node);
old_entry.next_hops_fee_msat = $next_hops_fee_msat;
old_entry.hop_use_fee_msat = hop_use_fee_msat;
// meaning how much will be paid in fees after this node (to the best of our knowledge).
// This data can later be helpful to optimize routing (pay lower fees).
macro_rules! add_entries_to_cheapest_to_target_node {
- ( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr,
- $next_hops_path_htlc_minimum_msat: expr, $next_hops_path_penalty_msat: expr,
+ ( $node: expr, $node_id: expr, $next_hops_value_contribution: expr,
$next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => {
+ let fee_to_target_msat;
+ let next_hops_path_htlc_minimum_msat;
+ let next_hops_path_penalty_msat;
let skip_node = if let Some(elem) = dist.get_mut(&$node_id) {
let was_processed = elem.was_processed;
elem.was_processed = true;
+ fee_to_target_msat = elem.total_fee_msat;
+ next_hops_path_htlc_minimum_msat = elem.path_htlc_minimum_msat;
+ next_hops_path_penalty_msat = elem.path_penalty_msat;
was_processed
} else {
// Entries are added to dist in add_entry!() when there is a channel from a node.
// Because there are no channels from payee, it will not have a dist entry at this point.
// If we're processing any other node, it is always be the result of a channel from it.
debug_assert_eq!($node_id, maybe_dummy_payee_node_id);
+ fee_to_target_msat = 0;
+ next_hops_path_htlc_minimum_msat = 0;
+ next_hops_path_penalty_msat = 0;
false
};
let candidate = CandidateRouteHop::FirstHop {
details, payer_node_id: &our_node_id,
};
- add_entry!(&candidate, $fee_to_target_msat,
+ add_entry!(&candidate, fee_to_target_msat,
$next_hops_value_contribution,
- $next_hops_path_htlc_minimum_msat, $next_hops_path_penalty_msat,
+ next_hops_path_htlc_minimum_msat, next_hops_path_penalty_msat,
$next_hops_cltv_delta, $next_hops_path_length);
}
}
short_channel_id: *chan_id,
};
add_entry!(&candidate,
- $fee_to_target_msat,
+ fee_to_target_msat,
$next_hops_value_contribution,
- $next_hops_path_htlc_minimum_msat,
- $next_hops_path_penalty_msat,
+ next_hops_path_htlc_minimum_msat,
+ next_hops_path_penalty_msat,
$next_hops_cltv_delta, $next_hops_path_length);
}
}
// If not, targets.pop() will not even let us enter the loop in step 2.
None => {},
Some(node) => {
- add_entries_to_cheapest_to_target_node!(node, payee, 0, path_value_msat, 0, 0u64, 0, 0);
+ add_entries_to_cheapest_to_target_node!(node, payee, path_value_msat, 0, 0);
},
});
// Both these cases (and other cases except reaching recommended_value_msat) mean that
// paths_collection will be stopped because found_new_path==false.
// This is not necessarily a routing failure.
- 'path_construction: while let Some(RouteGraphNode { node_id, lowest_fee_to_node, total_cltv_delta, mut value_contribution_msat, path_htlc_minimum_msat, path_penalty_msat, path_length_to_node, .. }) = targets.pop() {
+ 'path_construction: while let Some(RouteGraphNode { node_id, total_cltv_delta, mut value_contribution_msat, path_length_to_node, .. }) = targets.pop() {
// Since we're going payee-to-payer, hitting our node as a target means we should stop
// traversing the graph and arrange the path out of what we found.
match network_nodes.get(&node_id) {
None => {},
Some(node) => {
- add_entries_to_cheapest_to_target_node!(node, node_id, lowest_fee_to_node,
- value_contribution_msat, path_htlc_minimum_msat, path_penalty_msat,
+ add_entries_to_cheapest_to_target_node!(node, node_id,
+ value_contribution_msat,
total_cltv_delta, path_length_to_node);
},
}
pub(crate) mod bench_utils {
use super::*;
use std::fs::File;
+ use std::time::Duration;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
if let Ok(route) = route_res {
for path in route.paths {
if seed & 0x80 == 0 {
- scorer.payment_path_successful(&path);
+ scorer.payment_path_successful(&path, Duration::ZERO);
} else {
let short_channel_id = path.hops[path.hops.len() / 2].short_channel_id;
- scorer.payment_path_failed(&path, short_channel_id);
+ scorer.payment_path_failed(&path, short_channel_id, Duration::ZERO);
}
seed = seed.overflowing_mul(6364136223846793005).0.overflowing_add(1).0;
}