Skip to content

Commit 1535038

Browse files
committed
Expose AChannelManager trait and use it in lightning-invoice
1 parent d0209ce commit 1535038

File tree

2 files changed

+22
-80
lines changed

2 files changed

+22
-80
lines changed

lightning-invoice/src/payment.rs

Lines changed: 18 additions & 72 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,13 @@ 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>(
35+
pub fn pay_invoice<C: AChannelManager>(
3636
invoice: &Bolt11Invoice, retry_strategy: Retry,
37-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
37+
channelmanager: &C
3838
) -> 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,
4839
{
4940
let payment_id = PaymentId(invoice.payment_hash().into_inner());
50-
pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager)
41+
pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager.get_cm())
5142
.map(|()| payment_id)
5243
}
5344

@@ -61,22 +52,13 @@ where
6152
/// [`PaymentHash`] has never been paid before.
6253
///
6354
/// 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>(
55+
pub fn pay_invoice_with_id<C: AChannelManager>(
6556
invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry,
66-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
57+
channelmanager: &C
6758
) -> 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,
7759
{
7860
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)
61+
pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager.get_cm())
8062
}
8163

8264
/// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
@@ -88,19 +70,10 @@ where
8870
///
8971
/// If you wish to use a different payment idempotency token, see
9072
/// [`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>(
73+
pub fn pay_zero_value_invoice<C: AChannelManager>(
9274
invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry,
93-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
75+
channelmanager: &C
9476
) -> 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,
10477
{
10578
let payment_id = PaymentId(invoice.payment_hash().into_inner());
10679
pay_zero_value_invoice_with_id(invoice, amount_msats, payment_id, retry_strategy,
@@ -119,25 +92,16 @@ where
11992
///
12093
/// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
12194
/// 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>(
95+
pub fn pay_zero_value_invoice_with_id<C: AChannelManager>(
12396
invoice: &Bolt11Invoice, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
124-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
97+
channelmanager: &C
12598
) -> 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,
13599
{
136100
if invoice.amount_milli_satoshis().is_some() {
137101
Err(PaymentError::Invoice("amount unexpected"))
138102
} else {
139103
pay_invoice_using_amount(invoice, amount_msats, payment_id, retry_strategy,
140-
channelmanager)
104+
channelmanager.get_cm())
141105
}
142106
}
143107

@@ -166,18 +130,9 @@ fn pay_invoice_using_amount<P: Deref>(
166130
/// Sends payment probes over all paths of a route that would be used to pay the given invoice.
167131
///
168132
/// 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>
133+
pub fn preflight_probe_invoice<C: AChannelManager>(
134+
invoice: &Bolt11Invoice, channelmanager: &C
171135
) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
172-
where
173-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
174-
T::Target: BroadcasterInterface,
175-
ES::Target: EntropySource,
176-
NS::Target: NodeSigner,
177-
SP::Target: SignerProvider,
178-
F::Target: FeeEstimator,
179-
R::Target: Router,
180-
L::Target: Logger,
181136
{
182137
let amount_msat = if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() {
183138
invoice_amount_msat
@@ -198,25 +153,16 @@ where
198153
}
199154
let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
200155

201-
channelmanager.send_preflight_probes(route_params).map_err(ProbingError::Sending)
156+
channelmanager.get_cm().send_preflight_probes(route_params).map_err(ProbingError::Sending)
202157
}
203158

204159
/// Sends payment probes over all paths of a route that would be used to pay the given zero-value
205160
/// invoice using the given amount.
206161
///
207162
/// See [`ChannelManager::send_preflight_probes`] for more information.
208-
pub fn preflight_probe_zero_value_invoice<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>(
209-
invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, L>
163+
pub fn preflight_probe_zero_value_invoice<C: AChannelManager>(
164+
invoice: &Bolt11Invoice, amount_msat: u64, channelmanager: &C
210165
) -> Result<Vec<(PaymentHash, PaymentId)>, ProbingError>
211-
where
212-
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
213-
T::Target: BroadcasterInterface,
214-
ES::Target: EntropySource,
215-
NS::Target: NodeSigner,
216-
SP::Target: SignerProvider,
217-
F::Target: FeeEstimator,
218-
R::Target: Router,
219-
L::Target: Logger,
220166
{
221167
if invoice.amount_milli_satoshis().is_some() {
222168
return Err(ProbingError::Invoice("amount unexpected"));
@@ -235,7 +181,7 @@ where
235181
}
236182
let route_params = RouteParameters { payment_params, final_value_msat: amount_msat };
237183

238-
channelmanager.send_preflight_probes(route_params).map_err(ProbingError::Sending)
184+
channelmanager.get_cm().send_preflight_probes(route_params).map_err(ProbingError::Sending)
239185
}
240186

241187
fn expiry_time_from_unix_epoch(invoice: &Bolt11Invoice) -> Duration {

lightning/src/ln/channelmanager.rs

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

839-
macro_rules! define_test_pub_trait { ($vis: vis) => {
840-
/// A trivial trait which describes any [`ChannelManager`] used in testing.
841-
$vis trait AChannelManager {
839+
/// A trivial trait which describes any [`ChannelManager`].
840+
pub trait AChannelManager {
842841
type Watch: chain::Watch<Self::Signer> + ?Sized;
843842
type M: Deref<Target = Self::Watch>;
844843
type Broadcaster: BroadcasterInterface + ?Sized;
@@ -856,13 +855,10 @@ $vis trait AChannelManager {
856855
type R: Deref<Target = Self::Router>;
857856
type Logger: Logger + ?Sized;
858857
type L: Deref<Target = Self::Logger>;
858+
/// Returns a reference to the actual [`ChannelManager`] object.
859859
fn get_cm(&self) -> &ChannelManager<Self::M, Self::T, Self::ES, Self::NS, Self::SP, Self::F, Self::R, Self::L>;
860860
}
861-
} }
862-
#[cfg(any(test, feature = "_test_utils"))]
863-
define_test_pub_trait!(pub);
864-
#[cfg(not(any(test, feature = "_test_utils")))]
865-
define_test_pub_trait!(pub(crate));
861+
866862
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> AChannelManager
867863
for ChannelManager<M, T, ES, NS, SP, F, R, L>
868864
where

0 commit comments

Comments
 (0)