Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Penalize large HTLCs relative to channels in default Scorer #1166

Merged
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
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use lightning::ln::msgs::DecodeError;
use lightning::ln::script::ShutdownScript;
use lightning::routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
use lightning::routing::router::{find_route, Payee, RouteParameters};
use lightning::routing::scorer::Scorer;
use lightning::routing::scoring::Scorer;
use lightning::util::config::UserConfig;
use lightning::util::errors::APIError;
use lightning::util::events::Event;
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lightning::ln::channelmanager::{ChannelDetails, ChannelCounterparty};
use lightning::ln::features::InitFeatures;
use lightning::ln::msgs;
use lightning::routing::router::{find_route, Payee, RouteHint, RouteHintHop, RouteParameters};
use lightning::routing::scorer::Scorer;
use lightning::routing::scoring::Scorer;
use lightning::util::logger::Logger;
use lightning::util::ser::Readable;
use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
Expand Down
35 changes: 17 additions & 18 deletions lightning-invoice/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
//! # use lightning::ln::msgs::LightningError;
//! # use lightning::routing;
//! # use lightning::routing::scoring::Score;
//! # use lightning::routing::network_graph::NodeId;
//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
Expand Down Expand Up @@ -63,17 +63,17 @@
//! # }
//! #
//! # struct FakeRouter {};
//! # impl<S: routing::Score> Router<S> for FakeRouter {
//! # impl<S: Score> Router<S> for FakeRouter {
//! # fn find_route(
//! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
//! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
//! # ) -> Result<Route, LightningError> { unimplemented!() }
//! # }
//! #
//! # struct FakeScorer {};
//! # impl routing::Score for FakeScorer {
//! # impl Score for FakeScorer {
//! # fn channel_penalty_msat(
//! # &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
//! # &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
//! # ) -> u64 { 0 }
//! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
//! # }
Expand Down Expand Up @@ -122,8 +122,7 @@ use bitcoin_hashes::sha256::Hash as Sha256;
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
use lightning::ln::msgs::LightningError;
use lightning::routing;
use lightning::routing::{LockableScore, Score};
use lightning::routing::scoring::{LockableScore, Score};
use lightning::routing::router::{Payee, Route, RouteParameters};
use lightning::util::events::{Event, EventHandler};
use lightning::util::logger::Logger;
Expand All @@ -139,8 +138,8 @@ use std::time::{Duration, SystemTime};
pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
where
P::Target: Payer,
R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
S::Target: for <'a> routing::LockableScore<'a>,
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
S::Target: for <'a> LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
Expand Down Expand Up @@ -177,7 +176,7 @@ pub trait Payer {
}

/// A trait defining behavior for routing an [`Invoice`] payment.
pub trait Router<S: routing::Score> {
pub trait Router<S: Score> {
/// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
fn find_route(
&self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
Expand Down Expand Up @@ -207,8 +206,8 @@ pub enum PaymentError {
impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
S::Target: for <'a> routing::LockableScore<'a>,
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
S::Target: for <'a> LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
Expand Down Expand Up @@ -441,8 +440,8 @@ fn has_expired(params: &RouteParameters) -> bool {
impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
S::Target: for <'a> routing::LockableScore<'a>,
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
S::Target: for <'a> LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
Expand Down Expand Up @@ -1186,7 +1185,7 @@ mod tests {
}
}

impl<S: routing::Score> Router<S> for TestRouter {
impl<S: Score> Router<S> for TestRouter {
fn find_route(
&self, _payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
_first_hops: Option<&[&ChannelDetails]>, _scorer: &S
Expand All @@ -1199,7 +1198,7 @@ mod tests {

struct FailingRouter;

impl<S: routing::Score> Router<S> for FailingRouter {
impl<S: Score> Router<S> for FailingRouter {
fn find_route(
&self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
_first_hops: Option<&[&ChannelDetails]>, _scorer: &S
Expand All @@ -1225,9 +1224,9 @@ mod tests {
}
}

impl routing::Score for TestScorer {
impl Score for TestScorer {
fn channel_penalty_msat(
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
&self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId
) -> u64 { 0 }

fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
Expand Down Expand Up @@ -1364,7 +1363,7 @@ mod tests {
// *** Full Featured Functional Tests with a Real ChannelManager ***
struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);

impl<S: routing::Score> Router<S> for ManualRouter {
impl<S: Score> Router<S> for ManualRouter {
fn find_route(
&self, _payer: &PublicKey, _params: &RouteParameters, _payment_hash: &PaymentHash,
_first_hops: Option<&[&ChannelDetails]>, _scorer: &S
Expand Down
4 changes: 2 additions & 2 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lightning::chain::keysinterface::{Sign, KeysInterface};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
use lightning::ln::msgs::LightningError;
use lightning::routing;
use lightning::routing::scoring::Score;
use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route};
use lightning::util::logger::Logger;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<G, L: Deref> DefaultRouter<G, L> where G: Deref<Target = NetworkGraph>, L::
}
}

impl<G, L: Deref, S: routing::Score> Router<S> for DefaultRouter<G, L>
impl<G, L: Deref, S: Score> Router<S> for DefaultRouter<G, L>
where G: Deref<Target = NetworkGraph>, L::Target: Logger {
fn find_route(
&self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6552,7 +6552,7 @@ pub mod bench {
use ln::msgs::{ChannelMessageHandler, Init};
use routing::network_graph::NetworkGraph;
use routing::router::{Payee, get_route};
use routing::scorer::Scorer;
use routing::scoring::Scorer;
use util::test_utils;
use util::config::UserConfig;
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
Expand Down
63 changes: 1 addition & 62 deletions lightning/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,4 @@

pub mod network_graph;
pub mod router;
pub mod scorer;

use routing::network_graph::NodeId;
use routing::router::RouteHop;

use core::cell::{RefCell, RefMut};
use core::ops::DerefMut;
use sync::{Mutex, MutexGuard};

/// An interface used to score payment channels for path finding.
///
/// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
pub trait Score {
/// Returns the fee in msats willing to be paid to avoid routing through the given channel
/// in the direction from `source` to `target`.
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64;

/// Handles updating channel penalties after failing to route through a channel.
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
}

/// A scorer that is accessed under a lock.
///
/// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
/// having shared ownership of a scorer but without requiring internal locking in [`Score`]
/// implementations. Internal locking would be detrimental to route finding performance and could
/// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
///
/// [`find_route`]: crate::routing::router::find_route
pub trait LockableScore<'a> {
/// The locked [`Score`] type.
type Locked: 'a + Score;

/// Returns the locked scorer.
fn lock(&'a self) -> Self::Locked;
}

impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
type Locked = MutexGuard<'a, T>;

fn lock(&'a self) -> MutexGuard<'a, T> {
Mutex::lock(self).unwrap()
}
}

impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
type Locked = RefMut<'a, T>;

fn lock(&'a self) -> RefMut<'a, T> {
self.borrow_mut()
}
}

impl<S: Score, T: DerefMut<Target=S>> Score for T {
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64 {
self.deref().channel_penalty_msat(short_channel_id, source, target)
}

fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
self.deref_mut().payment_path_failed(path, short_channel_id)
}
}
pub mod scoring;
26 changes: 13 additions & 13 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bitcoin::secp256k1::key::PublicKey;
use ln::channelmanager::ChannelDetails;
use ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures};
use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
use routing;
use routing::scoring::Score;
use routing::network_graph::{NetworkGraph, NodeId, RoutingFees};
use util::ser::{Writeable, Readable};
use util::logger::{Level, Logger};
Expand Down Expand Up @@ -529,7 +529,7 @@ fn compute_fees(amount_msat: u64, channel_fees: RoutingFees) -> Option<u64> {
///
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
/// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
pub fn find_route<L: Deref, S: routing::Score>(
pub fn find_route<L: Deref, S: Score>(
our_node_pubkey: &PublicKey, params: &RouteParameters, network: &NetworkGraph,
first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S
) -> Result<Route, LightningError>
Expand All @@ -540,7 +540,7 @@ where L::Target: Logger {
)
}

pub(crate) fn get_route<L: Deref, S: routing::Score>(
pub(crate) fn get_route<L: Deref, S: Score>(
our_node_pubkey: &PublicKey, payee: &Payee, network: &NetworkGraph,
first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, final_cltv_expiry_delta: u32,
logger: L, scorer: &S
Expand Down Expand Up @@ -892,9 +892,9 @@ where L::Target: Logger {
}
}

let path_penalty_msat = $next_hops_path_penalty_msat
.checked_add(scorer.channel_penalty_msat($chan_id.clone(), &$src_node_id, &$dest_node_id))
.unwrap_or_else(|| u64::max_value());
let path_penalty_msat = $next_hops_path_penalty_msat.checked_add(
scorer.channel_penalty_msat($chan_id.clone(), amount_to_transfer_over_msat, Some(*available_liquidity_msat),
&$src_node_id, &$dest_node_id)).unwrap_or_else(|| u64::max_value());
let new_graph_node = RouteGraphNode {
node_id: $src_node_id,
lowest_fee_to_peer_through_node: total_fee_msat,
Expand Down Expand Up @@ -1121,7 +1121,7 @@ where L::Target: Logger {
let src_node_id = NodeId::from_pubkey(&hop.src_node_id);
let dest_node_id = NodeId::from_pubkey(&prev_hop_id);
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
.checked_add(scorer.channel_penalty_msat(hop.short_channel_id, &src_node_id, &dest_node_id))
.checked_add(scorer.channel_penalty_msat(hop.short_channel_id, final_value_msat, None, &src_node_id, &dest_node_id))
.unwrap_or_else(|| u64::max_value());

// We assume that the recipient only included route hints for routes which had
Expand Down Expand Up @@ -1472,7 +1472,7 @@ where L::Target: Logger {

#[cfg(test)]
mod tests {
use routing;
use routing::scoring::Score;
use routing::network_graph::{NetworkGraph, NetGraphMsgHandler, NodeId};
use routing::router::{get_route, Payee, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees};
use chain::transaction::OutPoint;
Expand Down Expand Up @@ -4549,8 +4549,8 @@ mod tests {
short_channel_id: u64,
}

impl routing::Score for BadChannelScorer {
fn channel_penalty_msat(&self, short_channel_id: u64, _source: &NodeId, _target: &NodeId) -> u64 {
impl Score for BadChannelScorer {
fn channel_penalty_msat(&self, short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, _target: &NodeId) -> u64 {
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
}

Expand All @@ -4561,8 +4561,8 @@ mod tests {
node_id: NodeId,
}

impl routing::Score for BadNodeScorer {
fn channel_penalty_msat(&self, _short_channel_id: u64, _source: &NodeId, target: &NodeId) -> u64 {
impl Score for BadNodeScorer {
fn channel_penalty_msat(&self, _short_channel_id: u64, _send_amt: u64, _chan_amt: Option<u64>, _source: &NodeId, target: &NodeId) -> u64 {
if *target == self.node_id { u64::max_value() } else { 0 }
}

Expand Down Expand Up @@ -4787,7 +4787,7 @@ pub(crate) mod test_utils {
#[cfg(all(test, feature = "unstable", not(feature = "no-std")))]
mod benches {
use super::*;
use routing::scorer::Scorer;
use routing::scoring::Scorer;
use util::logger::{Logger, Record};

use test::Bencher;
Expand Down
Loading