Skip to content

LSPS5 implementation #3662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
36 changes: 32 additions & 4 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ use futures_util::{dummy_waker, OptionalSelector, Selector, SelectorOutput};
/// # use std::sync::atomic::{AtomicBool, Ordering};
/// # use std::time::SystemTime;
/// # use lightning_background_processor::{process_events_async, GossipSync};
/// # use lightning_liquidity::lsps5::service::TimeProvider;
/// # struct Logger {}
/// # impl lightning::util::logger::Logger for Logger {
/// # fn log(&self, _record: lightning::util::logger::Record) {}
Expand All @@ -659,6 +660,16 @@ use futures_util::{dummy_waker, OptionalSelector, Selector, SelectorOutput};
/// # fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> io::Result<()> { Ok(()) }
/// # fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> { Ok(Vec::new()) }
/// # }
/// #
/// # use core::time::Duration;
/// # struct DefaultTimeProvider;
/// #
/// # impl TimeProvider for DefaultTimeProvider {
/// # fn duration_since_epoch(&self) -> Duration {
/// # use std::time::{SystemTime, UNIX_EPOCH};
/// # SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
/// # }
/// # }
/// # struct EventHandler {}
/// # impl EventHandler {
/// # async fn handle_event(&self, _: lightning::events::Event) -> Result<(), ReplayEvent> { Ok(()) }
Expand All @@ -674,7 +685,7 @@ use futures_util::{dummy_waker, OptionalSelector, Selector, SelectorOutput};
/// # type P2PGossipSync<UL> = lightning::routing::gossip::P2PGossipSync<Arc<NetworkGraph>, Arc<UL>, Arc<Logger>>;
/// # type ChannelManager<B, F, FE> = lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor<B, F, FE>, B, FE, Logger>;
/// # type OnionMessenger<B, F, FE> = lightning::onion_message::messenger::OnionMessenger<Arc<lightning::sign::KeysManager>, Arc<lightning::sign::KeysManager>, Arc<Logger>, Arc<ChannelManager<B, F, FE>>, Arc<lightning::onion_message::messenger::DefaultMessageRouter<Arc<NetworkGraph>, Arc<Logger>, Arc<lightning::sign::KeysManager>>>, Arc<ChannelManager<B, F, FE>>, lightning::ln::peer_handler::IgnoringMessageHandler, lightning::ln::peer_handler::IgnoringMessageHandler, lightning::ln::peer_handler::IgnoringMessageHandler>;
/// # type LiquidityManager<B, F, FE> = lightning_liquidity::LiquidityManager<Arc<lightning::sign::KeysManager>, Arc<ChannelManager<B, F, FE>>, Arc<F>>;
/// # type LiquidityManager<B, F, FE> = lightning_liquidity::LiquidityManager<Arc<lightning::sign::KeysManager>, Arc<ChannelManager<B, F, FE>>, Arc<F>, Arc<DefaultTimeProvider>>;
/// # type Scorer = RwLock<lightning::routing::scoring::ProbabilisticScorer<Arc<NetworkGraph>, Arc<Logger>>>;
/// # type PeerManager<B, F, FE, UL> = lightning::ln::peer_handler::SimpleArcPeerManager<SocketDescriptor, ChainMonitor<B, F, FE>, B, FE, Arc<UL>, Logger, F, Store>;
/// #
Expand Down Expand Up @@ -1196,6 +1207,7 @@ mod tests {
use lightning::util::sweep::{OutputSpendStatus, OutputSweeperSync, PRUNE_DELAY_BLOCKS};
use lightning::util::test_utils;
use lightning::{get_event, get_event_msg};
use lightning_liquidity::lsps5::service::TimeProvider;
use lightning_liquidity::LiquidityManager;
use lightning_persister::fs_store::FilesystemStore;
use lightning_rapid_gossip_sync::RapidGossipSync;
Expand Down Expand Up @@ -1292,8 +1304,12 @@ mod tests {
IgnoringMessageHandler,
>;

type LM =
LiquidityManager<Arc<KeysManager>, Arc<ChannelManager>, Arc<dyn Filter + Sync + Send>>;
type LM = LiquidityManager<
Arc<KeysManager>,
Arc<ChannelManager>,
Arc<dyn Filter + Sync + Send>,
Arc<DefaultTimeProvider>,
>;

struct Node {
node: Arc<ChannelManager>,
Expand Down Expand Up @@ -1634,6 +1650,16 @@ mod tests {
path.to_str().unwrap().to_string()
}

pub struct DefaultTimeProvider;

#[cfg(feature = "std")]
impl TimeProvider for DefaultTimeProvider {
fn duration_since_epoch(&self) -> Duration {
use std::time::{SystemTime, UNIX_EPOCH};
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
}
}

fn create_nodes(num_nodes: usize, persist_dir: &str) -> (String, Vec<Node>) {
let persist_temp_path = env::temp_dir().join(persist_dir);
let persist_dir = persist_temp_path.to_string_lossy().to_string();
Expand Down Expand Up @@ -1735,13 +1761,15 @@ mod tests {
Arc::clone(&logger),
Arc::clone(&keys_manager),
));
let liquidity_manager = Arc::new(LiquidityManager::new(
let time_provider = Arc::new(DefaultTimeProvider);
let liquidity_manager = Arc::new(LiquidityManager::new_with_custom_time_provider(
Arc::clone(&keys_manager),
Arc::clone(&manager),
None,
None,
None,
None,
time_provider,
));
let node = Node {
node: manager,
Expand Down
3 changes: 2 additions & 1 deletion lightning-liquidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ categories = ["cryptography::cryptocurrencies"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["std"]
default = ["std", "time"]
std = ["lightning/std"]
time = ["std"]
backtrace = ["dep:backtrace"]

[dependencies]
Expand Down
17 changes: 17 additions & 0 deletions lightning-liquidity/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use event_queue::MAX_EVENT_QUEUE_SIZE;
use crate::lsps0;
use crate::lsps1;
use crate::lsps2;
use crate::lsps5;

/// An event which you should probably take some action in response to.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -38,6 +39,10 @@ pub enum LiquidityEvent {
LSPS2Client(lsps2::event::LSPS2ClientEvent),
/// An LSPS2 (JIT Channel) server event.
LSPS2Service(lsps2::event::LSPS2ServiceEvent),
/// An LSPS5 (Webhook) client event.
LSPS5Client(lsps5::event::LSPS5ClientEvent),
/// An LSPS5 (Webhook) server event.
LSPS5Service(lsps5::event::LSPS5ServiceEvent),
}

impl From<lsps0::event::LSPS0ClientEvent> for LiquidityEvent {
Expand Down Expand Up @@ -70,3 +75,15 @@ impl From<lsps2::event::LSPS2ServiceEvent> for LiquidityEvent {
Self::LSPS2Service(event)
}
}

impl From<lsps5::event::LSPS5ClientEvent> for LiquidityEvent {
fn from(event: lsps5::event::LSPS5ClientEvent) -> Self {
Self::LSPS5Client(event)
}
}

impl From<lsps5::event::LSPS5ServiceEvent> for LiquidityEvent {
fn from(event: lsps5::event::LSPS5ServiceEvent) -> Self {
Self::LSPS5Service(event)
}
}
4 changes: 4 additions & 0 deletions lightning-liquidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
//! an LSP will open a "just-in-time" channel. This is useful for the initial on-boarding of
//! clients as the channel opening fees are deducted from the incoming payment, i.e., no funds are
//! required client-side to initiate this flow.
//! - [bLIP-55 / LSPS5] defines a protocol for sending webhook notifications to clients. This is
//! useful for notifying clients about incoming payments, channel expiries, etc.
//!
//! To get started, you'll want to setup a [`LiquidityManager`] and configure it to be the
//! [`CustomMessageHandler`] of your LDK node. You can then for example call
Expand All @@ -37,6 +39,7 @@
//! [bLIP-50 / LSPS0]: https://github.com/lightning/blips/blob/master/blip-0050.md
//! [bLIP-51 / LSPS1]: https://github.com/lightning/blips/blob/master/blip-0051.md
//! [bLIP-52 / LSPS2]: https://github.com/lightning/blips/blob/master/blip-0052.md
//! [bLIP-55 / LSPS5]: https://github.com/lightning/blips/pull/55/files
//! [`CustomMessageHandler`]: lightning::ln::peer_handler::CustomMessageHandler
//! [`LiquidityManager::next_event`]: crate::LiquidityManager::next_event
#![deny(missing_docs)]
Expand All @@ -59,6 +62,7 @@ pub mod events;
pub mod lsps0;
pub mod lsps1;
pub mod lsps2;
pub mod lsps5;
mod manager;
pub mod message_queue;
#[allow(dead_code)]
Expand Down
1 change: 1 addition & 0 deletions lightning-liquidity/src/lsps0/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl TryFrom<LSPSMessage> for LSPS0Message {
LSPSMessage::LSPS0(message) => Ok(message),
LSPSMessage::LSPS1(_) => Err(()),
LSPSMessage::LSPS2(_) => Err(()),
LSPSMessage::LSPS5(_) => Err(()),
}
}
}
Expand Down
Loading
Loading