X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Fpayment.rs;h=0958c79649521b4eec769f090705b34792de9f4e;hb=ce6bcf68a15331c082b34d09902241583bc6ff71;hp=eceaacf86c93a1f8ceb5162c8b4680b6d3999f46;hpb=53eb0d7aa7e7ee2c247a509d259fbdc7cf840426;p=rust-lightning diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index eceaacf8..0958c796 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -44,7 +44,7 @@ //! # use lightning::util::logger::{Logger, Record}; //! # use lightning::util::ser::{Writeable, Writer}; //! # use lightning_invoice::Invoice; -//! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry, ScoringRouter}; +//! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry}; //! # use secp256k1::PublicKey; //! # use std::cell::RefCell; //! # use std::ops::Deref; @@ -76,10 +76,8 @@ //! # impl Router for FakeRouter { //! # fn find_route( //! # &self, payer: &PublicKey, params: &RouteParameters, -//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs +//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: &InFlightHtlcs //! # ) -> Result { unimplemented!() } -//! # } -//! # impl ScoringRouter for FakeRouter { //! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() } //! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() } //! # fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { unimplemented!() } @@ -146,7 +144,7 @@ use crate::prelude::*; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure}; use lightning::ln::msgs::LightningError; -use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, Router}; +use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router}; use lightning::util::events::{Event, EventHandler}; use lightning::util::logger::Logger; use crate::time_utils::Time; @@ -186,7 +184,7 @@ mod sealed { /// (C-not exported) generally all users should use the [`InvoicePayer`] type alias. pub struct InvoicePayerUsingTime< P: Deref, - R: ScoringRouter, + R: Router, L: Deref, E: sealed::BaseEventHandler, T: Time @@ -279,30 +277,6 @@ pub trait Payer { fn inflight_htlcs(&self) -> InFlightHtlcs; } -/// A trait defining behavior for a [`Router`] implementation that also supports scoring channels -/// based on payment and probe success/failure. -/// -/// [`Router`]: lightning::routing::router::Router -pub trait ScoringRouter: Router { - /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes - /// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment. - fn find_route_with_id( - &self, payer: &PublicKey, route_params: &RouteParameters, - first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs, - _payment_hash: PaymentHash, _payment_id: PaymentId - ) -> Result { - self.find_route(payer, route_params, first_hops, inflight_htlcs) - } - /// Lets the router know that payment through a specific path has failed. - fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64); - /// Lets the router know that payment through a specific path was successful. - fn notify_payment_path_successful(&self, path: &[&RouteHop]); - /// Lets the router know that a payment probe was successful. - fn notify_payment_probe_successful(&self, path: &[&RouteHop]); - /// Lets the router know that a payment probe failed. - fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64); -} - /// Strategies available to retry payment path failures for an [`Invoice`]. /// #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -342,7 +316,7 @@ pub enum PaymentError { Sending(PaymentSendFailure), } -impl +impl InvoicePayerUsingTime where P::Target: Payer, @@ -536,7 +510,7 @@ where let first_hops = self.payer.first_hops(); let inflight_htlcs = self.payer.inflight_htlcs(); let route = self.router.find_route( - &payer, ¶ms, Some(&first_hops.iter().collect::>()), inflight_htlcs + &payer, ¶ms, Some(&first_hops.iter().collect::>()), &inflight_htlcs ).map_err(|e| PaymentError::Routing(e))?; match send_payment(&route) { @@ -604,7 +578,7 @@ where let inflight_htlcs = self.payer.inflight_htlcs(); let route = self.router.find_route( - &payer, ¶ms, Some(&first_hops.iter().collect::>()), inflight_htlcs + &payer, ¶ms, Some(&first_hops.iter().collect::>()), &inflight_htlcs ); if route.is_err() { @@ -656,7 +630,7 @@ fn has_expired(route_params: &RouteParameters) -> bool { } else { false } } -impl +impl InvoicePayerUsingTime where P::Target: Payer, @@ -723,7 +697,7 @@ where } } -impl +impl EventHandler for InvoicePayerUsingTime where P::Target: Payer, @@ -737,7 +711,7 @@ where } } -impl F> +impl F> InvoicePayerUsingTime where P::Target: Payer, @@ -757,15 +731,14 @@ where mod tests { use super::*; use crate::{InvoiceBuilder, Currency}; - use crate::utils::{ScorerAccountingForInFlightHtlcs, create_invoice_from_channelmanager_and_duration_since_epoch}; + use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch; use bitcoin_hashes::sha256::Hash as Sha256; use lightning::ln::PaymentPreimage; - use lightning::ln::channelmanager; use lightning::ln::features::{ChannelFeatures, NodeFeatures}; use lightning::ln::functional_test_utils::*; use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError}; use lightning::routing::gossip::{EffectiveCapacity, NodeId}; - use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router}; + use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router, ScorerAccountingForInFlightHtlcs}; use lightning::routing::scoring::{ChannelUsage, LockableScore, Score}; use lightning::util::test_utils::TestLogger; use lightning::util::errors::APIError; @@ -1696,12 +1669,12 @@ mod tests { impl Router for TestRouter { fn find_route( &self, payer: &PublicKey, route_params: &RouteParameters, - _first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs + _first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: &InFlightHtlcs ) -> Result { // Simulate calling the Scorer just as you would in find_route let route = Self::route_for_value(route_params.final_value_msat); - let mut locked_scorer = self.scorer.lock(); - let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer.deref_mut(), inflight_htlcs); + let locked_scorer = self.scorer.lock(); + let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs); for path in route.paths { let mut aggregate_msat = 0u64; for (idx, hop) in path.iter().rev().enumerate() { @@ -1726,9 +1699,7 @@ mod tests { payment_params: Some(route_params.payment_params.clone()), ..Self::route_for_value(route_params.final_value_msat) }) } - } - impl ScoringRouter for TestRouter { fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { self.scorer.lock().payment_path_failed(path, short_channel_id); } @@ -1751,13 +1722,11 @@ mod tests { impl Router for FailingRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs, + _inflight_htlcs: &InFlightHtlcs, ) -> Result { Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError }) } - } - impl ScoringRouter for FailingRouter { fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {} @@ -2041,12 +2010,11 @@ mod tests { impl Router for ManualRouter { fn find_route( &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>, - _inflight_htlcs: InFlightHtlcs + _inflight_htlcs: &InFlightHtlcs ) -> Result { self.0.borrow_mut().pop_front().unwrap() } - } - impl ScoringRouter for ManualRouter { + fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {} @@ -2077,24 +2045,24 @@ mod tests { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); let chans = nodes[0].node.list_usable_channels(); let mut route = Route { paths: vec![ vec![RouteHop { pubkey: nodes[1].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[1].node.node_features(), short_channel_id: chans[0].short_channel_id.unwrap(), - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[1].node.channel_features(), fee_msat: 10_000, cltv_expiry_delta: 100, }], vec![RouteHop { pubkey: nodes[1].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[1].node.node_features(), short_channel_id: chans[1].short_channel_id.unwrap(), - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[1].node.channel_features(), fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than cltv_expiry_delta: 100, }], @@ -2128,16 +2096,16 @@ mod tests { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); let chans = nodes[0].node.list_usable_channels(); let mut route = Route { paths: vec![ vec![RouteHop { pubkey: nodes[1].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[1].node.node_features(), short_channel_id: chans[0].short_channel_id.unwrap(), - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[1].node.channel_features(), fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than cltv_expiry_delta: 100, }], @@ -2186,38 +2154,38 @@ mod tests { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); let nodes = create_network(3, &node_cfgs, &node_chanmgrs); - let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.contents.short_channel_id; - let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.contents.short_channel_id; + let chan_1_scid = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0).0.contents.short_channel_id; + let chan_2_scid = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 0).0.contents.short_channel_id; let mut route = Route { paths: vec![ vec![RouteHop { pubkey: nodes[1].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[1].node.node_features(), short_channel_id: chan_1_scid, - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[1].node.channel_features(), fee_msat: 0, cltv_expiry_delta: 100, }, RouteHop { pubkey: nodes[2].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[2].node.node_features(), short_channel_id: chan_2_scid, - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[2].node.channel_features(), fee_msat: 100_000_000, cltv_expiry_delta: 100, }], vec![RouteHop { pubkey: nodes[1].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[1].node.node_features(), short_channel_id: chan_1_scid, - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[2].node.channel_features(), fee_msat: 0, cltv_expiry_delta: 100, }, RouteHop { pubkey: nodes[2].node.get_our_node_id(), - node_features: channelmanager::provided_node_features(), + node_features: nodes[2].node.node_features(), short_channel_id: chan_2_scid, - channel_features: channelmanager::provided_channel_features(), + channel_features: nodes[2].node.channel_features(), fee_msat: 100_000_000, cltv_expiry_delta: 100, }]