Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum Error {
InvalidCustomTlvs,
/// Sending a payment probe has failed.
ProbeSendingFailed,
/// A route to the given destination could not be found.
RouteNotFound,
/// A channel could not be opened.
ChannelCreationFailed,
/// A channel could not be closed.
Expand Down Expand Up @@ -145,6 +147,7 @@ impl fmt::Display for Error {
Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
Self::InvalidCustomTlvs => write!(f, "Failed to construct payment with custom TLVs."),
Self::ProbeSendingFailed => write!(f, "Failed to send the given payment probe."),
Self::RouteNotFound => write!(f, "Failed to find a route to the destination."),
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
Self::ChannelClosingFailed => write!(f, "Failed to close channel."),
Self::ChannelSplicingFailed => write!(f, "Failed to splice channel."),
Expand Down
57 changes: 56 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ use lightning::ln::channelmanager::PaymentId;
use lightning::ln::funding::SpliceContribution;
use lightning::ln::msgs::SocketAddress;
use lightning::routing::gossip::NodeAlias;
use lightning::routing::router::{PaymentParameters, RouteParameters};
pub use lightning::routing::router::{Route, RouteParametersConfig};
use lightning::util::persist::KVStoreSync;
use lightning_background_processor::process_events_async;
use liquidity::{LSPS1Liquidity, LiquiditySource};
Expand All @@ -152,7 +154,7 @@ use payment::asynchronous::om_mailbox::OnionMessageMailbox;
use payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use payment::{
Bolt11Payment, Bolt12Payment, OnchainPayment, PaymentDetails, SpontaneousPayment,
UnifiedQrPayment,
UnifiedQrPayment, LDK_DEFAULT_FINAL_CLTV_EXPIRY_DELTA,
};
use peer_store::{PeerInfo, PeerStore};
use rand::Rng;
Expand Down Expand Up @@ -1888,6 +1890,59 @@ impl Node {
.to_sat_per_kwu()
}

/// Finds a route to `payee` for `amount_msat` using the node's internal router.
///
/// Intended for callers that need to introspect routing fees ahead of
/// time (e.g. computing a max-sendable estimate by inverting per-hop fees).
///
/// `params` overrides the node-wide [`RouteParametersConfig`] when `Some`; otherwise
/// the value from [`Config::route_parameters`] is used.
///
/// [`Config::route_parameters`]: crate::config::Config::route_parameters
pub fn find_route(
&self, payee: PublicKey, amount_msat: u64, params: Option<RouteParametersConfig>,
) -> Result<Route, Error> {
if !*self.is_running.read().unwrap() {
return Err(Error::NotRunning);
}

let mut route_params = RouteParameters::from_payment_params_and_value(
PaymentParameters::from_node_id(payee, LDK_DEFAULT_FINAL_CLTV_EXPIRY_DELTA),
amount_msat,
);

if let Some(RouteParametersConfig {
max_total_routing_fee_msat,
max_total_cltv_expiry_delta,
max_path_count,
max_channel_saturation_power_of_half,
}) = params.as_ref().or(self.config.route_parameters.as_ref())
{
route_params.max_total_routing_fee_msat = *max_total_routing_fee_msat;
route_params.payment_params.max_total_cltv_expiry_delta = *max_total_cltv_expiry_delta;
route_params.payment_params.max_path_count = *max_path_count;
route_params.payment_params.max_channel_saturation_power_of_half =
*max_channel_saturation_power_of_half;
}

let payer = self.channel_manager.get_our_node_id();
let first_hops = self.channel_manager.list_usable_channels();
let first_hops_refs: Vec<&LdkChannelDetails> = first_hops.iter().collect();
let inflight_htlcs = self.channel_manager.compute_inflight_htlcs();

lightning::routing::router::Router::find_route(
&*self._router,
&payer,
&route_params,
Some(&first_hops_refs),
inflight_htlcs,
)
.map_err(|e| {
log_debug!(self.logger, "Failed to find route to {}: {}", payee, e);
Error::RouteNotFound
})
}

/// Exports the current state of the scorer. The result can be shared with and merged by light nodes that only have
/// a limited view of the network.
pub fn export_pathfinding_scores(&self) -> Result<Vec<u8>, Error> {
Expand Down
1 change: 1 addition & 0 deletions src/payment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use bolt11::Bolt11Payment;
pub use bolt12::Bolt12Payment;
pub use onchain::OnchainPayment;
pub use spontaneous::SpontaneousPayment;
pub(crate) use spontaneous::LDK_DEFAULT_FINAL_CLTV_EXPIRY_DELTA;
pub use store::{
ConfirmationStatus, LSPFeeLimits, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus,
};
Expand Down
2 changes: 1 addition & 1 deletion src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::payment::store::{PaymentDetails, PaymentDirection, PaymentKind, Payme
use crate::types::{ChannelManager, CustomTlvRecord, KeysManager, PaymentStore};

// The default `final_cltv_expiry_delta` we apply when not set.
const LDK_DEFAULT_FINAL_CLTV_EXPIRY_DELTA: u32 = 144;
pub(crate) const LDK_DEFAULT_FINAL_CLTV_EXPIRY_DELTA: u32 = 144;

/// A payment handler allowing to send spontaneous ("keysend") payments.
///
Expand Down
Loading