//! # use lightning::ln::{PaymentHash, PaymentSecret};
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
//! # use lightning::ln::msgs::LightningError;
-//! # use lightning::routing::router::{Route, RouteParameters};
+//! # use lightning::routing;
+//! # use lightning::routing::network_graph::NodeId;
+//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
//! # use lightning::util::logger::{Logger, Record};
//! # use lightning_invoice::Invoice;
//! # use lightning_invoice::payment::{InvoicePayer, Payer, RetryAttempts, Router};
//! # use secp256k1::key::PublicKey;
+//! # use std::cell::RefCell;
//! # use std::ops::Deref;
//! #
//! # struct FakeEventProvider {}
//! # }
//! #
//! # struct FakeRouter {};
-//! # impl Router for FakeRouter {
+//! # impl<S: routing::Score> Router<S> for FakeRouter {
//! # fn find_route(
//! # &self, payer: &PublicKey, params: &RouteParameters,
-//! # first_hops: Option<&[&ChannelDetails]>
+//! # first_hops: Option<&[&ChannelDetails]>, scorer: &S
//! # ) -> Result<Route, LightningError> { unimplemented!() }
//! # }
//! #
+//! # struct FakeScorer {};
+//! # impl routing::Score for FakeScorer {
+//! # fn channel_penalty_msat(
+//! # &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
+//! # ) -> u64 { 0 }
+//! # fn payment_path_failed(&mut self, _path: &Vec<RouteHop>, _short_channel_id: u64) {}
+//! # }
+//! #
//! # struct FakeLogger {};
//! # impl Logger for FakeLogger {
//! # fn log(&self, record: &Record) { unimplemented!() }
//! };
//! # let payer = FakePayer {};
//! # let router = FakeRouter {};
+//! # let scorer = RefCell::new(FakeScorer {});
//! # let logger = FakeLogger {};
-//! let invoice_payer = InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+//! let invoice_payer = InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
//!
//! let invoice = "...";
//! let invoice = invoice.parse::<Invoice>().unwrap();
use lightning::ln::{PaymentHash, PaymentSecret};
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
use lightning::ln::msgs::LightningError;
+use lightning::routing;
+use lightning::routing::{LockableScore, Score};
use lightning::routing::router::{Payee, Route, RouteParameters};
use lightning::util::events::{Event, EventHandler};
use lightning::util::logger::Logger;
use std::time::{Duration, SystemTime};
/// A utility for paying [`Invoice]`s.
-pub struct InvoicePayer<P: Deref, R, L: Deref, E>
+pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E>
where
P::Target: Payer,
- R: Router,
+ R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
+ S::Target: for <'a> routing::LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
payer: P,
router: R,
+ scorer: S,
logger: L,
event_handler: E,
payment_cache: Mutex<HashMap<PaymentHash, usize>>,
}
/// A trait defining behavior for routing an [`Invoice`] payment.
-pub trait Router {
+pub trait Router<S: routing::Score> {
/// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
fn find_route(
- &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>
+ &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>,
+ scorer: &S
) -> Result<Route, LightningError>;
}
Sending(PaymentSendFailure),
}
-impl<P: Deref, R, L: Deref, E> InvoicePayer<P, R, L, E>
+impl<P: Deref, R, S: Deref, L: Deref, E> InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
- R: Router,
+ R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
+ S::Target: for <'a> routing::LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
/// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
/// `retry_attempts` has been exceeded for a given [`Invoice`].
pub fn new(
- payer: P, router: R, logger: L, event_handler: E, retry_attempts: RetryAttempts
+ payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
) -> Self {
Self {
payer,
router,
+ scorer,
logger,
event_handler,
payment_cache: Mutex::new(HashMap::new()),
&payer,
¶ms,
Some(&first_hops.iter().collect::<Vec<_>>()),
+ &self.scorer.lock(),
).map_err(|e| PaymentError::Routing(e))?;
let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
let payer = self.payer.node_id();
let first_hops = self.payer.first_hops();
let route = self.router.find_route(
- &payer, ¶ms, Some(&first_hops.iter().collect::<Vec<_>>())
+ &payer, ¶ms, Some(&first_hops.iter().collect::<Vec<_>>()),
+ &self.scorer.lock()
).map_err(|e| PaymentError::Routing(e))?;
self.payer.retry_payment(&route, payment_id).map_err(|e| PaymentError::Sending(e))
}
Invoice::is_expired_from_epoch(&SystemTime::UNIX_EPOCH, expiry_time)
}
-impl<P: Deref, R, L: Deref, E> EventHandler for InvoicePayer<P, R, L, E>
+impl<P: Deref, R, S: Deref, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
- R: Router,
+ R: for <'a> Router<<<S as Deref>::Target as routing::LockableScore<'a>>::Locked>,
+ S::Target: for <'a> routing::LockableScore<'a>,
L::Target: Logger,
E: EventHandler,
{
fn handle_event(&self, event: &Event) {
match event {
- Event::PaymentPathFailed { payment_id, payment_hash, rejected_by_dest, retry, .. } => {
+ Event::PaymentPathFailed {
+ payment_id, payment_hash, rejected_by_dest, path, short_channel_id, retry, ..
+ } => {
+ if let Some(short_channel_id) = short_channel_id {
+ self.scorer.lock().payment_path_failed(path, *short_channel_id);
+ }
+
let mut payment_cache = self.payment_cache.lock().unwrap();
let entry = loop {
let entry = payment_cache.entry(*payment_hash);
use lightning::ln::PaymentPreimage;
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
use lightning::ln::msgs::{ErrorAction, LightningError};
- use lightning::routing::router::{Route, RouteHop};
+ use lightning::routing::network_graph::NodeId;
+ use lightning::routing::router::{Payee, Route, RouteHop};
use lightning::util::test_utils::TestLogger;
use lightning::util::errors::APIError;
use lightning::util::events::Event;
use secp256k1::{SecretKey, PublicKey, Secp256k1};
+ use std::cell::RefCell;
use std::time::{SystemTime, Duration};
fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
assert_eq!(*payer.attempts.borrow(), 1);
.expect_value_msat(final_value_msat)
.expect_value_msat(final_value_msat / 2);
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
assert_eq!(*payer.attempts.borrow(), 1);
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_id = Some(PaymentId([1; 32]));
let event = Event::PaymentPathFailed {
.expect_value_msat(final_value_msat / 2)
.expect_value_msat(final_value_msat / 2);
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
assert_eq!(*payer.attempts.borrow(), 1);
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = expired_invoice(payment_preimage);
.fails_on_attempt(2)
.expect_value_msat(final_value_msat);
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
assert_eq!(*payer.attempts.borrow(), 1);
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(2));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
fn fails_paying_invoice_with_routing_errors() {
let payer = TestPayer::new();
let router = FailingRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, |_: &_| {}, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
fn fails_paying_invoice_with_sending_errors() {
let payer = TestPayer::new().fails_on_attempt(1);
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, |_: &_| {}, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, |_: &_| {}, RetryAttempts(0));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
let payer = TestPayer::new().expect_value_msat(final_value_msat);
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
let payment_id =
Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap());
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new());
let logger = TestLogger::new();
let invoice_payer =
- InvoicePayer::new(&payer, router, &logger, event_handler, RetryAttempts(0));
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(0));
let payment_preimage = PaymentPreimage([1; 32]);
let invoice = invoice(payment_preimage);
}
}
+ #[test]
+ fn scores_failed_channel() {
+ let event_handled = core::cell::RefCell::new(false);
+ let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
+
+ let payment_preimage = PaymentPreimage([1; 32]);
+ let invoice = invoice(payment_preimage);
+ let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
+ let final_value_msat = invoice.amount_milli_satoshis().unwrap();
+ let path = TestRouter::path_for_value(final_value_msat);
+ let short_channel_id = Some(path[0].short_channel_id);
+
+ // Expect that scorer is given short_channel_id upon handling the event.
+ let payer = TestPayer::new();
+ let router = TestRouter {};
+ let scorer = RefCell::new(TestScorer::new().expect_channel_failure(short_channel_id.unwrap()));
+ let logger = TestLogger::new();
+ let invoice_payer =
+ InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, RetryAttempts(2));
+
+ let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap());
+ let event = Event::PaymentPathFailed {
+ payment_id,
+ payment_hash,
+ network_update: None,
+ rejected_by_dest: false,
+ all_paths_failed: false,
+ path,
+ short_channel_id,
+ retry: Some(TestRouter::retry_for_invoice(&invoice)),
+ };
+ invoice_payer.handle_event(&event);
+ }
+
struct TestRouter;
impl TestRouter {
}
}
- impl Router for TestRouter {
+ impl<S: routing::Score> Router<S> for TestRouter {
fn find_route(
&self,
_payer: &PublicKey,
params: &RouteParameters,
_first_hops: Option<&[&ChannelDetails]>,
+ _scorer: &S,
) -> Result<Route, LightningError> {
Ok(Route {
payee: Some(params.payee.clone()), ..Self::route_for_value(params.final_value_msat)
struct FailingRouter;
- impl Router for FailingRouter {
+ impl<S: routing::Score> Router<S> for FailingRouter {
fn find_route(
&self,
_payer: &PublicKey,
_params: &RouteParameters,
_first_hops: Option<&[&ChannelDetails]>,
+ _scorer: &S,
) -> Result<Route, LightningError> {
Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
}
}
+ struct TestScorer {
+ expectations: std::collections::VecDeque<u64>,
+ }
+
+ impl TestScorer {
+ fn new() -> Self {
+ Self {
+ expectations: std::collections::VecDeque::new(),
+ }
+ }
+
+ fn expect_channel_failure(mut self, short_channel_id: u64) -> Self {
+ self.expectations.push_back(short_channel_id);
+ self
+ }
+ }
+
+ impl routing::Score for TestScorer {
+ fn channel_penalty_msat(
+ &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
+ ) -> u64 { 0 }
+
+ fn payment_path_failed(&mut self, _path: &Vec<RouteHop>, short_channel_id: u64) {
+ if let Some(expected_short_channel_id) = self.expectations.pop_front() {
+ assert_eq!(short_channel_id, expected_short_channel_id);
+ }
+ }
+ }
+
+ impl Drop for TestScorer {
+ fn drop(&mut self) {
+ if std::thread::panicking() {
+ return;
+ }
+
+ if !self.expectations.is_empty() {
+ panic!("Unsatisfied channel failure expectations: {:?}", self.expectations);
+ }
+ }
+ }
+
struct TestPayer {
expectations: core::cell::RefCell<std::collections::VecDeque<u64>>,
attempts: core::cell::RefCell<usize>,