]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Expose `AChannelManager` trait and use it in `lightning-invoice`
authorElias Rohrer <dev@tnull.de>
Tue, 12 Sep 2023 11:20:31 +0000 (13:20 +0200)
committerElias Rohrer <dev@tnull.de>
Mon, 18 Sep 2023 13:08:28 +0000 (15:08 +0200)
lightning-invoice/src/payment.rs
lightning/src/ln/channelmanager.rs

index 6fa23d91258af8841cb71a7a6534af738e3d4c89..f5b20d87fa192f25f2ee709749d9cd903d71792d 100644 (file)
@@ -17,7 +17,7 @@ use lightning::chain;
 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use lightning::sign::{NodeSigner, SignerProvider, EntropySource};
 use lightning::ln::PaymentHash;
-use lightning::ln::channelmanager::{ChannelManager, PaymentId, Retry, RetryableSendFailure, RecipientOnionFields, ProbeSendFailure};
+use lightning::ln::channelmanager::{AChannelManager, ChannelManager, PaymentId, Retry, RetryableSendFailure, RecipientOnionFields, ProbeSendFailure};
 use lightning::routing::router::{PaymentParameters, RouteParameters, Router};
 use lightning::util::logger::Logger;
 
@@ -32,22 +32,12 @@ use core::time::Duration;
 /// with the same [`PaymentHash`] is never sent.
 ///
 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
-pub fn pay_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
-       invoice: &Bolt11Invoice, retry_strategy: Retry,
-       channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
+pub fn pay_invoice<C: AChannelManager>(
+       invoice: &Bolt11Invoice, retry_strategy: Retry, channelmanager: &C
 ) -> Result<PaymentId, PaymentError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        let payment_id = PaymentId(invoice.payment_hash().into_inner());
-       pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager)
+       pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager.get_cm())
                .map(|()| payment_id)
 }
 
@@ -61,22 +51,12 @@ where
 /// [`PaymentHash`] has never been paid before.
 ///
 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
-pub fn pay_invoice_with_id<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
-       invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry,
-       channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
+pub fn pay_invoice_with_id<C: AChannelManager>(
+       invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry, channelmanager: &C
 ) -> Result<(), PaymentError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        let amt_msat = invoice.amount_milli_satoshis().ok_or(PaymentError::Invoice("amount missing"))?;
-       pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager)
+       pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager.get_cm())
 }
 
 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
@@ -88,19 +68,9 @@ where
 ///
 /// If you wish to use a different payment idempotency token, see
 /// [`pay_zero_value_invoice_with_id`].
-pub fn pay_zero_value_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
-       invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry,
-       channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
+pub fn pay_zero_value_invoice<C: AChannelManager>(
+       invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry, channelmanager: &C
 ) -> Result<PaymentId, PaymentError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        let payment_id = PaymentId(invoice.payment_hash().into_inner());
        pay_zero_value_invoice_with_id(invoice, amount_msats, payment_id, retry_strategy,
@@ -119,25 +89,16 @@ where
 ///
 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
 /// idempotency token.
-pub fn pay_zero_value_invoice_with_id<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
+pub fn pay_zero_value_invoice_with_id<C: AChannelManager>(
        invoice: &Bolt11Invoice, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
-       channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
+       channelmanager: &C
 ) -> Result<(), PaymentError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        if invoice.amount_milli_satoshis().is_some() {
                Err(PaymentError::Invoice("amount unexpected"))
        } else {
                pay_invoice_using_amount(invoice, amount_msats, payment_id, retry_strategy,
-                       channelmanager)
+                       channelmanager.get_cm())
        }
 }
 
@@ -166,19 +127,9 @@ fn pay_invoice_using_amount<P: Deref>(
 /// Sends payment probes over all paths of a route that would be used to pay the given invoice.
 ///
 /// See [`ChannelManager::send_preflight_probes`] for more information.
-pub fn preflight_probe_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
-       invoice: &Bolt11Invoice, channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>,
-       liquidity_limit_multiplier: Option<u64>,
+pub fn preflight_probe_invoice<C: AChannelManager>(
+       invoice: &Bolt11Invoice, channelmanager: &C, liquidity_limit_multiplier: Option<u64>,
 ) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        let amount_msat = if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() {
                invoice_amount_msat
@@ -199,7 +150,7 @@ where
        }
        let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
 
-       channelmanager.send_preflight_probes(route_params, liquidity_limit_multiplier)
+       channelmanager.get_cm().send_preflight_probes(route_params, liquidity_limit_multiplier)
                .map_err(ProbingError::Sending)
 }
 
@@ -207,19 +158,10 @@ where
 /// invoice using the given amount.
 ///
 /// See [`ChannelManager::send_preflight_probes`] for more information.
-pub fn preflight_probe_zero_value_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
-       invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>,
+pub fn preflight_probe_zero_value_invoice<C: AChannelManager>(
+       invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &C,
        liquidity_limit_multiplier: Option<u64>,
 ) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
-where
-               M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
-               T::Target: BroadcasterInterface,
-               ES::Target: EntropySource,
-               NS::Target: NodeSigner,
-               SP::Target: SignerProvider,
-               F::Target: FeeEstimator,
-               R::Target: Router,
-               L::Target: Logger,
 {
        if invoice.amount_milli_satoshis().is_some() {
                return Err(ProbingError::Invoice("amount unexpected"));
@@ -238,7 +180,7 @@ where
        }
        let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
 
-       channelmanager.send_preflight_probes(route_params, liquidity_limit_multiplier)
+       channelmanager.get_cm().send_preflight_probes(route_params, liquidity_limit_multiplier)
                .map_err(ProbingError::Sending)
 }
 
index 2748062c905c3539a7cc43fa59498b733662e5a3..785595a89ef2f82b1fba5f50484171fe5b4d4086 100644 (file)
@@ -835,33 +835,46 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
                &'g L
        >;
 
-macro_rules! define_test_pub_trait { ($vis: vis) => {
-/// A trivial trait which describes any [`ChannelManager`] used in testing.
-$vis trait AChannelManager {
+/// A trivial trait which describes any [`ChannelManager`].
+pub trait AChannelManager {
+       /// A type implementing [`chain::Watch`].
        type Watch: chain::Watch<Self::Signer> + ?Sized;
+       /// A type that may be dereferenced to [`Self::Watch`].
        type M: Deref<Target = Self::Watch>;
+       /// A type implementing [`BroadcasterInterface`].
        type Broadcaster: BroadcasterInterface + ?Sized;
+       /// A type that may be dereferenced to [`Self::Broadcaster`].
        type T: Deref<Target = Self::Broadcaster>;
+       /// A type implementing [`EntropySource`].
        type EntropySource: EntropySource + ?Sized;
+       /// A type that may be dereferenced to [`Self::EntropySource`].
        type ES: Deref<Target = Self::EntropySource>;
+       /// A type implementing [`NodeSigner`].
        type NodeSigner: NodeSigner + ?Sized;
+       /// A type that may be dereferenced to [`Self::NodeSigner`].
        type NS: Deref<Target = Self::NodeSigner>;
+       /// A type implementing [`WriteableEcdsaChannelSigner`].
        type Signer: WriteableEcdsaChannelSigner + Sized;
+       /// A type implementing [`SignerProvider`] for [`Self::Signer`].
        type SignerProvider: SignerProvider<Signer = Self::Signer> + ?Sized;
+       /// A type that may be dereferenced to [`Self::SignerProvider`].
        type SP: Deref<Target = Self::SignerProvider>;
+       /// A type implementing [`FeeEstimator`].
        type FeeEstimator: FeeEstimator + ?Sized;
+       /// A type that may be dereferenced to [`Self::FeeEstimator`].
        type F: Deref<Target = Self::FeeEstimator>;
+       /// A type implementing [`Router`].
        type Router: Router + ?Sized;
+       /// A type that may be dereferenced to [`Self::Router`].
        type R: Deref<Target = Self::Router>;
+       /// A type implementing [`Logger`].
        type Logger: Logger + ?Sized;
+       /// A type that may be dereferenced to [`Self::Logger`].
        type L: Deref<Target = Self::Logger>;
+       /// Returns a reference to the actual [`ChannelManager`] object.
        fn get_cm(&self) -> &ChannelManager<Self::M, Self::T, Self::ES, Self::NS, Self::SP, Self::F, Self::R, Self::L>;
 }
-} }
-#[cfg(any(test, feature = "_test_utils"))]
-define_test_pub_trait!(pub);
-#[cfg(not(any(test, feature = "_test_utils")))]
-define_test_pub_trait!(pub(crate));
+
 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> AChannelManager
 for ChannelManager<M, T, ES, NS, SP, F, R, L>
 where