Skip to content

Commit 787fed4

Browse files
committed
Expose AChannelManager trait and use it in lightning-invoice
1 parent cc7cd8e commit 787fed4

File tree

2 files changed

+39
-84
lines changed

2 files changed

+39
-84
lines changed

lightning-invoice/src/payment.rs

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use lightning::chain;
1717
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
1818
use lightning::sign::{NodeSigner, SignerProvider, EntropySource};
1919
use lightning::ln::PaymentHash;
20-
use lightning::ln::channelmanager::{ChannelManager, PaymentId, Retry, RetryableSendFailure, RecipientOnionFields, ProbeSendFailure};
20+
use lightning::ln::channelmanager::{AChannelManager, ChannelManager, PaymentId, Retry, RetryableSendFailure, RecipientOnionFields, ProbeSendFailure};
2121
use lightning::routing::router::{PaymentParameters, RouteParameters, Router};
2222
use lightning::util::logger::Logger;
2323

@@ -32,22 +32,12 @@ use core::time::Duration;
3232
/// with the same [`PaymentHash`] is never sent.
3333
///
3434
/// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
35-
pub fn pay_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
36-
invoice: &Bolt11Invoice, retry_strategy: Retry,
37-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
35+
pub fn pay_invoice<C: AChannelManager>(
36+
invoice: &Bolt11Invoice, retry_strategy: Retry, channelmanager: &C
3837
) -> Result<PaymentId, PaymentError>
39-
where
40-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
41-
T::Target: BroadcasterInterface,
42-
ES::Target: EntropySource,
43-
NS::Target: NodeSigner,
44-
SP::Target: SignerProvider,
45-
F::Target: FeeEstimator,
46-
R::Target: Router,
47-
L::Target: Logger,
4838
{
4939
let payment_id = PaymentId(invoice.payment_hash().into_inner());
50-
pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager)
40+
pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager.get_cm())
5141
.map(|()| payment_id)
5242
}
5343

@@ -61,22 +51,12 @@ where
6151
/// [`PaymentHash`] has never been paid before.
6252
///
6353
/// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
64-
pub fn pay_invoice_with_id<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
65-
invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry,
66-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
54+
pub fn pay_invoice_with_id<C: AChannelManager>(
55+
invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry, channelmanager: &C
6756
) -> Result<(), PaymentError>
68-
where
69-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
70-
T::Target: BroadcasterInterface,
71-
ES::Target: EntropySource,
72-
NS::Target: NodeSigner,
73-
SP::Target: SignerProvider,
74-
F::Target: FeeEstimator,
75-
R::Target: Router,
76-
L::Target: Logger,
7757
{
7858
let amt_msat = invoice.amount_milli_satoshis().ok_or(PaymentError::Invoice("amount missing"))?;
79-
pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager)
59+
pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager.get_cm())
8060
}
8161

8262
/// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
@@ -88,19 +68,9 @@ where
8868
///
8969
/// If you wish to use a different payment idempotency token, see
9070
/// [`pay_zero_value_invoice_with_id`].
91-
pub fn pay_zero_value_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
92-
invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry,
93-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
71+
pub fn pay_zero_value_invoice<C: AChannelManager>(
72+
invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry, channelmanager: &C
9473
) -> Result<PaymentId, PaymentError>
95-
where
96-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
97-
T::Target: BroadcasterInterface,
98-
ES::Target: EntropySource,
99-
NS::Target: NodeSigner,
100-
SP::Target: SignerProvider,
101-
F::Target: FeeEstimator,
102-
R::Target: Router,
103-
L::Target: Logger,
10474
{
10575
let payment_id = PaymentId(invoice.payment_hash().into_inner());
10676
pay_zero_value_invoice_with_id(invoice, amount_msats, payment_id, retry_strategy,
@@ -119,25 +89,16 @@ where
11989
///
12090
/// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
12191
/// idempotency token.
122-
pub fn pay_zero_value_invoice_with_id<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
92+
pub fn pay_zero_value_invoice_with_id<C: AChannelManager>(
12393
invoice: &Bolt11Invoice, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
124-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
94+
channelmanager: &C
12595
) -> Result<(), PaymentError>
126-
where
127-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
128-
T::Target: BroadcasterInterface,
129-
ES::Target: EntropySource,
130-
NS::Target: NodeSigner,
131-
SP::Target: SignerProvider,
132-
F::Target: FeeEstimator,
133-
R::Target: Router,
134-
L::Target: Logger,
13596
{
13697
if invoice.amount_milli_satoshis().is_some() {
13798
Err(PaymentError::Invoice("amount unexpected"))
13899
} else {
139100
pay_invoice_using_amount(invoice, amount_msats, payment_id, retry_strategy,
140-
channelmanager)
101+
channelmanager.get_cm())
141102
}
142103
}
143104

@@ -166,19 +127,9 @@ fn pay_invoice_using_amount<P: Deref>(
166127
/// Sends payment probes over all paths of a route that would be used to pay the given invoice.
167128
///
168129
/// See [`ChannelManager::send_preflight_probes`] for more information.
169-
pub fn preflight_probe_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
170-
invoice: &Bolt11Invoice, channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>,
171-
liquidity_limit_multiplier: Option<u64>,
130+
pub fn preflight_probe_invoice<C: AChannelManager>(
131+
invoice: &Bolt11Invoice, channelmanager: &C, liquidity_limit_multiplier: Option<u64>,
172132
) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
173-
where
174-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
175-
T::Target: BroadcasterInterface,
176-
ES::Target: EntropySource,
177-
NS::Target: NodeSigner,
178-
SP::Target: SignerProvider,
179-
F::Target: FeeEstimator,
180-
R::Target: Router,
181-
L::Target: Logger,
182133
{
183134
let amount_msat = if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() {
184135
invoice_amount_msat
@@ -199,27 +150,18 @@ where
199150
}
200151
let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
201152

202-
channelmanager.send_preflight_probes(route_params, liquidity_limit_multiplier)
153+
channelmanager.get_cm().send_preflight_probes(route_params, liquidity_limit_multiplier)
203154
.map_err(ProbingError::Sending)
204155
}
205156

206157
/// Sends payment probes over all paths of a route that would be used to pay the given zero-value
207158
/// invoice using the given amount.
208159
///
209160
/// See [`ChannelManager::send_preflight_probes`] for more information.
210-
pub fn preflight_probe_zero_value_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
211-
invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>,
161+
pub fn preflight_probe_zero_value_invoice<C: AChannelManager>(
162+
invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &C,
212163
liquidity_limit_multiplier: Option<u64>,
213164
) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
214-
where
215-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
216-
T::Target: BroadcasterInterface,
217-
ES::Target: EntropySource,
218-
NS::Target: NodeSigner,
219-
SP::Target: SignerProvider,
220-
F::Target: FeeEstimator,
221-
R::Target: Router,
222-
L::Target: Logger,
223165
{
224166
if invoice.amount_milli_satoshis().is_some() {
225167
return Err(ProbingError::Invoice("amount unexpected"));
@@ -238,7 +180,7 @@ where
238180
}
239181
let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
240182

241-
channelmanager.send_preflight_probes(route_params, liquidity_limit_multiplier)
183+
channelmanager.get_cm().send_preflight_probes(route_params, liquidity_limit_multiplier)
242184
.map_err(ProbingError::Sending)
243185
}
244186

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -835,33 +835,46 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
835835
&'g L
836836
>;
837837

838-
macro_rules! define_test_pub_trait { ($vis: vis) => {
839-
/// A trivial trait which describes any [`ChannelManager`] used in testing.
840-
$vis trait AChannelManager {
838+
/// A trivial trait which describes any [`ChannelManager`].
839+
pub trait AChannelManager {
840+
/// A type implementing [`chain::Watch`].
841841
type Watch: chain::Watch<Self::Signer> + ?Sized;
842+
/// A type that may be dereferenced to [`Self::Watch`].
842843
type M: Deref<Target = Self::Watch>;
844+
/// A type implementing [`BroadcasterInterface`].
843845
type Broadcaster: BroadcasterInterface + ?Sized;
846+
/// A type that may be dereferenced to [`Self::Broadcaster`].
844847
type T: Deref<Target = Self::Broadcaster>;
848+
/// A type implementing [`EntropySource`].
845849
type EntropySource: EntropySource + ?Sized;
850+
/// A type that may be dereferenced to [`Self::EntropySource`].
846851
type ES: Deref<Target = Self::EntropySource>;
852+
/// A type implementing [`NodeSigner`].
847853
type NodeSigner: NodeSigner + ?Sized;
854+
/// A type that may be dereferenced to [`Self::NodeSigner`].
848855
type NS: Deref<Target = Self::NodeSigner>;
856+
/// A type implementing [`WriteableEcdsaChannelSigner`].
849857
type Signer: WriteableEcdsaChannelSigner + Sized;
858+
/// A type implementing [`SignerProvider`] for [`Self::Signer`].
850859
type SignerProvider: SignerProvider<Signer = Self::Signer> + ?Sized;
860+
/// A type that may be dereferenced to [`Self::SignerProvider`].
851861
type SP: Deref<Target = Self::SignerProvider>;
862+
/// A type implementing [`FeeEstimator`].
852863
type FeeEstimator: FeeEstimator + ?Sized;
864+
/// A type that may be dereferenced to [`Self::FeeEstimator`].
853865
type F: Deref<Target = Self::FeeEstimator>;
866+
/// A type implementing [`Router`].
854867
type Router: Router + ?Sized;
868+
/// A type that may be dereferenced to [`Self::Router`].
855869
type R: Deref<Target = Self::Router>;
870+
/// A type implementing [`Logger`].
856871
type Logger: Logger + ?Sized;
872+
/// A type that may be dereferenced to [`Self::Logger`].
857873
type L: Deref<Target = Self::Logger>;
874+
/// Returns a reference to the actual [`ChannelManager`] object.
858875
fn get_cm(&self) -> &ChannelManager<Self::M, Self::T, Self::ES, Self::NS, Self::SP, Self::F, Self::R, Self::L>;
859876
}
860-
} }
861-
#[cfg(any(test, feature = "_test_utils"))]
862-
define_test_pub_trait!(pub);
863-
#[cfg(not(any(test, feature = "_test_utils")))]
864-
define_test_pub_trait!(pub(crate));
877+
865878
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> AChannelManager
866879
for ChannelManager<M, T, ES, NS, SP, F, R, L>
867880
where

0 commit comments

Comments
 (0)