Skip to content

Commit 2c24298

Browse files
committed
Add LSPS2ServiceHandlerSync wrapper
.. to allow access in a non-async context
1 parent afbe962 commit 2c24298

File tree

2 files changed

+144
-7
lines changed

2 files changed

+144
-7
lines changed

lightning-liquidity/src/lsps2/service.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,140 @@ fn calculate_amount_to_forward_per_htlc(
16511651
per_htlc_forwards
16521652
}
16531653

1654+
/// A synchroneous wrapper around [`LSPS2ServiceHandler`] to be used in contexts where async is not
1655+
/// available.
1656+
pub struct LSPS2ServiceHandlerSync<CM: Deref, K: Deref + Clone>
1657+
where
1658+
CM::Target: AChannelManager,
1659+
K::Target: KVStore,
1660+
{
1661+
inner: Arc<LSPS2ServiceHandler<CM, K>>,
1662+
}
1663+
1664+
impl<CM: Deref, K: Deref + Clone> LSPS2ServiceHandlerSync<CM, K>
1665+
where
1666+
CM::Target: AChannelManager,
1667+
K::Target: KVStore,
1668+
{
1669+
pub(crate) fn from_inner(inner: Arc<LSPS2ServiceHandler<CM, K>>) -> Self {
1670+
Self { inner }
1671+
}
1672+
1673+
/// Returns a reference to the used config.
1674+
///
1675+
/// Wraps [`LSPS2ServiceHandler::config`].
1676+
pub fn config(&self) -> &LSPS2ServiceConfig {
1677+
&self.inner.config
1678+
}
1679+
1680+
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
1681+
///
1682+
/// Wraps [`LSPS2ServiceHandler::invalid_token_provided`].
1683+
pub fn invalid_token_provided(
1684+
&self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId,
1685+
) -> Result<(), APIError> {
1686+
self.inner.invalid_token_provided(counterparty_node_id, request_id)
1687+
}
1688+
1689+
/// Used by LSP to provide fee parameters to a client requesting a JIT Channel.
1690+
///
1691+
/// Wraps [`LSPS2ServiceHandler::opening_fee_params_generated`].
1692+
pub fn opening_fee_params_generated(
1693+
&self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId,
1694+
opening_fee_params_menu: Vec<LSPS2RawOpeningFeeParams>,
1695+
) -> Result<(), APIError> {
1696+
self.inner.opening_fee_params_generated(
1697+
counterparty_node_id,
1698+
request_id,
1699+
opening_fee_params_menu,
1700+
)
1701+
}
1702+
1703+
/// Used by LSP to provide the client with the intercept scid and
1704+
/// `cltv_expiry_delta` to include in their invoice.
1705+
///
1706+
/// Wraps [`LSPS2ServiceHandler::invoice_parameters_generated`].
1707+
pub fn invoice_parameters_generated(
1708+
&self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId, intercept_scid: u64,
1709+
cltv_expiry_delta: u32, client_trusts_lsp: bool, user_channel_id: u128,
1710+
) -> Result<(), APIError> {
1711+
self.inner.invoice_parameters_generated(
1712+
counterparty_node_id,
1713+
request_id,
1714+
intercept_scid,
1715+
cltv_expiry_delta,
1716+
client_trusts_lsp,
1717+
user_channel_id,
1718+
)
1719+
}
1720+
1721+
/// Forward [`Event::HTLCIntercepted`] event parameters into this function.
1722+
///
1723+
/// Wraps [`LSPS2ServiceHandler::htlc_intercepted`].
1724+
///
1725+
/// [`Event::HTLCIntercepted`]: lightning::events::Event::HTLCIntercepted
1726+
pub fn htlc_intercepted(
1727+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
1728+
payment_hash: PaymentHash,
1729+
) -> Result<(), APIError> {
1730+
self.inner.htlc_intercepted(
1731+
intercept_scid,
1732+
intercept_id,
1733+
expected_outbound_amount_msat,
1734+
payment_hash,
1735+
)
1736+
}
1737+
1738+
/// Forward [`Event::HTLCHandlingFailed`] event parameter into this function.
1739+
///
1740+
/// Wraps [`LSPS2ServiceHandler::htlc_handling_failed`].
1741+
///
1742+
/// [`Event::HTLCHandlingFailed`]: lightning::events::Event::HTLCHandlingFailed
1743+
pub fn htlc_handling_failed(
1744+
&self, failure_type: HTLCHandlingFailureType,
1745+
) -> Result<(), APIError> {
1746+
self.inner.htlc_handling_failed(failure_type)
1747+
}
1748+
1749+
/// Forward [`Event::PaymentForwarded`] event parameter into this function.
1750+
///
1751+
/// Wraps [`LSPS2ServiceHandler::payment_forwarded`].
1752+
///
1753+
/// [`Event::PaymentForwarded`]: lightning::events::Event::PaymentForwarded
1754+
pub fn payment_forwarded(&self, next_channel_id: ChannelId) -> Result<(), APIError> {
1755+
self.inner.payment_forwarded(next_channel_id)
1756+
}
1757+
1758+
/// Abandons a pending JIT‐open flow for `user_channel_id`, removing all local state.
1759+
///
1760+
/// Wraps [`LSPS2ServiceHandler::channel_open_abandoned`].
1761+
pub fn channel_open_abandoned(
1762+
&self, counterparty_node_id: &PublicKey, user_channel_id: u128,
1763+
) -> Result<(), APIError> {
1764+
self.inner.channel_open_abandoned(counterparty_node_id, user_channel_id)
1765+
}
1766+
1767+
/// Used to fail intercepted HTLCs backwards when a channel open attempt ultimately fails.
1768+
///
1769+
/// Wraps [`LSPS2ServiceHandler::channel_open_failed`].
1770+
pub fn channel_open_failed(
1771+
&self, counterparty_node_id: &PublicKey, user_channel_id: u128,
1772+
) -> Result<(), APIError> {
1773+
self.inner.channel_open_failed(counterparty_node_id, user_channel_id)
1774+
}
1775+
1776+
/// Forward [`Event::ChannelReady`] event parameters into this function.
1777+
///
1778+
/// Wraps [`LSPS2ServiceHandler::channel_ready`].
1779+
///
1780+
/// [`Event::ChannelReady`]: lightning::events::Event::ChannelReady
1781+
pub fn channel_ready(
1782+
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
1783+
) -> Result<(), APIError> {
1784+
self.inner.channel_ready(user_channel_id, channel_id, counterparty_node_id)
1785+
}
1786+
}
1787+
16541788
#[cfg(test)]
16551789
mod tests {
16561790
use super::*;

lightning-liquidity/src/manager.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler};
3535

3636
use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler};
3737
use crate::lsps2::msgs::LSPS2Message;
38-
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
38+
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler, LSPS2ServiceHandlerSync};
3939
use crate::prelude::{new_hash_map, new_hash_set, HashMap, HashSet};
4040
use crate::sync::{Arc, Mutex, RwLock};
4141
use crate::utils::async_poll::dummy_waker;
@@ -304,7 +304,7 @@ pub struct LiquidityManager<
304304
#[cfg(lsps1_service)]
305305
lsps1_service_handler: Option<LSPS1ServiceHandler<ES, CM, C, K>>,
306306
lsps1_client_handler: Option<LSPS1ClientHandler<ES, K>>,
307-
lsps2_service_handler: Option<LSPS2ServiceHandler<CM, K>>,
307+
lsps2_service_handler: Option<Arc<LSPS2ServiceHandler<CM, K>>>,
308308
lsps2_client_handler: Option<LSPS2ClientHandler<ES, K>>,
309309
lsps5_service_handler: Option<LSPS5ServiceHandler<CM, NS, K, TP>>,
310310
lsps5_client_handler: Option<LSPS5ClientHandler<ES, K>>,
@@ -418,14 +418,14 @@ where
418418
}
419419

420420
let peer_states = read_lsps2_service_peer_states(kv_store.clone()).await?;
421-
Some(LSPS2ServiceHandler::new(
421+
Some(Arc::new(LSPS2ServiceHandler::new(
422422
peer_states,
423423
Arc::clone(&pending_messages),
424424
Arc::clone(&pending_events),
425425
channel_manager.clone(),
426426
kv_store.clone(),
427427
lsps2_service_config.clone(),
428-
))
428+
)))
429429
} else {
430430
None
431431
}
@@ -571,7 +571,7 @@ where
571571
///
572572
/// The returned hendler allows to initiate the LSPS2 service-side flow.
573573
pub fn lsps2_service_handler(&self) -> Option<&LSPS2ServiceHandler<CM, K>> {
574-
self.lsps2_service_handler.as_ref()
574+
self.lsps2_service_handler.as_ref().map(|r| &**r)
575575
}
576576

577577
/// Returns a reference to the LSPS5 client-side handler.
@@ -1193,8 +1193,11 @@ where
11931193
/// Wraps [`LiquidityManager::lsps2_service_handler`].
11941194
pub fn lsps2_service_handler(
11951195
&self,
1196-
) -> Option<&LSPS2ServiceHandler<CM, Arc<KVStoreSyncWrapper<KS>>>> {
1197-
self.inner.lsps2_service_handler()
1196+
) -> Option<Arc<LSPS2ServiceHandlerSync<CM, Arc<KVStoreSyncWrapper<KS>>>>> {
1197+
self.inner
1198+
.lsps2_service_handler
1199+
.as_ref()
1200+
.map(|r| Arc::new(LSPS2ServiceHandlerSync::from_inner(Arc::clone(&r))))
11981201
}
11991202

12001203
/// Returns a reference to the LSPS5 client-side handler.

0 commit comments

Comments
 (0)