//! # use lightning::ln::{PaymentHash, PaymentSecret};
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
//! # use lightning::ln::msgs::LightningError;
+//! # use lightning::routing;
+//! # use lightning::routing::network_graph::NodeId;
//! # use lightning::routing::router::{Route, RouteParameters};
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
//! # use lightning::util::logger::{Logger, Record};
//! #
//! # struct FakeRouter {};
//! # impl Router for FakeRouter {
-//! # fn find_route(
+//! # fn find_route<S: routing::Score>(
//! # &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 }
+//! # }
+//! #
//! # struct FakeLogger {};
//! # impl Logger for FakeLogger {
//! # fn log(&self, record: &Record) { unimplemented!() }
//! };
//! # let payer = FakePayer {};
//! # let router = FakeRouter {};
+//! # let scorer = 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::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, L: Deref, E>
where
P::Target: Payer,
R: Router,
+ S: routing::Score,
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 {
/// 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]>
+ fn find_route<S: routing::Score>(
+ &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, L: Deref, E> InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
R: Router,
+ S: routing::Score,
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,
).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
).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, L: Deref, E> EventHandler for InvoicePayer<P, R, S, L, E>
where
P::Target: Payer,
R: Router,
+ S: routing::Score,
L::Target: Logger,
E: EventHandler,
{
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;
let payer = TestPayer::new();
let router = TestRouter {};
+ let scorer = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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);
}
impl Router for TestRouter {
- fn find_route(
+ fn find_route<S: routing::Score>(
&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 {
- fn find_route(
+ fn find_route<S: routing::Score>(
&self,
_payer: &PublicKey,
_params: &RouteParameters,
_first_hops: Option<&[&ChannelDetails]>,
+ _scorer: &S,
) -> Result<Route, LightningError> {
Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
}
}
+ struct TestScorer;
+
+ impl TestScorer {
+ fn new() -> Self { Self {} }
+ }
+
+ impl routing::Score for TestScorer {
+ fn channel_penalty_msat(
+ &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
+ ) -> u64 { 0 }
+ }
+
struct TestPayer {
expectations: core::cell::RefCell<std::collections::VecDeque<u64>>,
attempts: core::cell::RefCell<usize>,