Skip to content

Commit 0a89df0

Browse files
committed
Add LSPS2ServiceHandlerSync wrapper
.. to allow access in a non-async context
1 parent b30ffce commit 0a89df0

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

lightning-liquidity/src/lsps2/service.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,140 @@ fn calculate_amount_to_forward_per_htlc(
16541654
per_htlc_forwards
16551655
}
16561656

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

lightning-liquidity/src/manager.rs

Lines changed: 5 additions & 5 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;
@@ -1191,10 +1191,10 @@ where
11911191
/// Returns a reference to the LSPS2 server-side handler.
11921192
///
11931193
/// Wraps [`LiquidityManager::lsps2_service_handler`].
1194-
pub fn lsps2_service_handler(
1195-
&self,
1196-
) -> Option<&LSPS2ServiceHandler<CM, Arc<KVStoreSyncWrapper<KS>>>> {
1197-
self.inner.lsps2_service_handler()
1194+
pub fn lsps2_service_handler<'a>(
1195+
&'a self,
1196+
) -> Option<LSPS2ServiceHandlerSync<'a, CM, Arc<KVStoreSyncWrapper<KS>>>> {
1197+
self.inner.lsps2_service_handler.as_ref().map(|r| LSPS2ServiceHandlerSync::from_inner(r))
11981198
}
11991199

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

0 commit comments

Comments
 (0)