Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 93de7cd

Browse files
authored
Merge pull request #113 from tnull/2024-02-fix-lsps1-types
LSPS1: Use suitable types for `Address`es and `Bolt11Invoice`s
2 parents d670469 + c6004e2 commit 93de7cd

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ categories = ["cryptography::cryptocurrencies"]
1515

1616
[features]
1717
default = ["std"]
18-
std = ["lightning/std", "bitcoin/std"]
19-
no-std = ["hashbrown", "lightning/no-std", "bitcoin/no-std", "core2/alloc"]
18+
std = ["lightning/std", "bitcoin/std", "lightning-invoice/std"]
19+
no-std = ["hashbrown", "lightning/no-std", "lightning-invoice/no-std", "bitcoin/no-std", "core2/alloc"]
2020

2121
[dependencies]
2222
lightning = { version = "0.0.121", default-features = false, features = ["max_level_trace"] }
23-
lightning-invoice = "0.29.0"
24-
bitcoin = { version = "0.30.2", default-features = false }
23+
lightning-invoice = { version = "0.29.0", default-features = false, features = ["serde"] }
24+
bitcoin = { version = "0.30.2", default-features = false, features = ["serde"] }
2525
hashbrown = { version = "0.8", optional = true }
2626
core2 = { version = "0.3.0", optional = true, default-features = false }
2727

src/lsps1/msgs.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use crate::lsps0::ser::{
66

77
use crate::prelude::{String, Vec};
88

9+
use bitcoin::address::{Address, NetworkUnchecked};
10+
11+
use lightning_invoice::Bolt11Invoice;
12+
913
use serde::{Deserialize, Serialize};
1014

1115
use chrono::Utc;
@@ -106,7 +110,7 @@ pub struct OrderParams {
106110
/// May contain arbitrary associated data like a coupon code or a authentication token.
107111
pub token: String,
108112
/// The address where the LSP will send the funds if the order fails.
109-
pub refund_onchain_address: Option<String>,
113+
pub refund_onchain_address: Option<Address<NetworkUnchecked>>,
110114
/// Indicates if the channel should be announced to the network.
111115
pub announce_channel: bool,
112116
}
@@ -153,10 +157,10 @@ pub struct OrderPayment {
153157
#[serde(with = "string_amount")]
154158
pub order_total_sat: u64,
155159
/// A BOLT11 invoice the client can pay to have to channel opened.
156-
pub bolt11_invoice: String,
160+
pub bolt11_invoice: Bolt11Invoice,
157161
/// An on-chain address the client can send [`Self::order_total_sat`] to to have the channel
158162
/// opened.
159-
pub onchain_address: String,
163+
pub onchain_address: Address<NetworkUnchecked>,
160164
/// The minimum number of block confirmations that are required for the on-chain payment to be
161165
/// considered confirmed.
162166
pub min_onchain_payment_confirmations: Option<u8>,

src/manager.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,71 @@ where {
294294
/// ```
295295
///
296296
/// [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
297+
#[cfg(feature = "std")]
297298
pub fn set_process_msgs_callback(&self, callback: impl Fn() + Send + Sync + 'static) {
298299
self.pending_messages.set_process_msgs_callback(callback)
299300
}
300301

302+
/// Allows to set a callback that will be called after new messages are pushed to the message
303+
/// queue.
304+
///
305+
/// Usually, you'll want to use this to call [`PeerManager::process_events`] to clear the
306+
/// message queue. For example:
307+
///
308+
/// ```
309+
/// # use lightning::io;
310+
/// # use lightning_liquidity::LiquidityManager;
311+
/// # use std::sync::{Arc, RwLock};
312+
/// # use std::sync::atomic::{AtomicBool, Ordering};
313+
/// # use std::time::SystemTime;
314+
/// # struct MyStore {}
315+
/// # impl lightning::util::persist::KVStore for MyStore {
316+
/// # fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result<Vec<u8>> { Ok(Vec::new()) }
317+
/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { Ok(()) }
318+
/// # fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> io::Result<()> { Ok(()) }
319+
/// # fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> { Ok(Vec::new()) }
320+
/// # }
321+
/// # struct MyEntropySource {}
322+
/// # impl lightning::sign::EntropySource for MyEntropySource {
323+
/// # fn get_secure_random_bytes(&self) -> [u8; 32] { [0u8; 32] }
324+
/// # }
325+
/// # struct MyEventHandler {}
326+
/// # impl MyEventHandler {
327+
/// # async fn handle_event(&self, _: lightning::events::Event) {}
328+
/// # }
329+
/// # #[derive(Eq, PartialEq, Clone, Hash)]
330+
/// # struct MySocketDescriptor {}
331+
/// # impl lightning::ln::peer_handler::SocketDescriptor for MySocketDescriptor {
332+
/// # fn send_data(&mut self, _data: &[u8], _resume_read: bool) -> usize { 0 }
333+
/// # fn disconnect_socket(&mut self) {}
334+
/// # }
335+
/// # type MyBroadcaster = dyn lightning::chain::chaininterface::BroadcasterInterface;
336+
/// # type MyFeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator;
337+
/// # type MyNodeSigner = dyn lightning::sign::NodeSigner;
338+
/// # type MyUtxoLookup = dyn lightning::routing::utxo::UtxoLookup;
339+
/// # type MyFilter = dyn lightning::chain::Filter;
340+
/// # type MyLogger = dyn lightning::util::logger::Logger;
341+
/// # type MyChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::sign::InMemorySigner, Arc<MyFilter>, Arc<MyBroadcaster>, Arc<MyFeeEstimator>, Arc<MyLogger>, Arc<MyStore>>;
342+
/// # type MyPeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<MySocketDescriptor, MyChainMonitor, MyBroadcaster, MyFeeEstimator, Arc<MyUtxoLookup>, MyLogger>;
343+
/// # type MyNetworkGraph = lightning::routing::gossip::NetworkGraph<Arc<MyLogger>>;
344+
/// # type MyGossipSync = lightning::routing::gossip::P2PGossipSync<Arc<MyNetworkGraph>, Arc<MyUtxoLookup>, Arc<MyLogger>>;
345+
/// # type MyChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<MyChainMonitor, MyBroadcaster, MyFeeEstimator, MyLogger>;
346+
/// # type MyScorer = RwLock<lightning::routing::scoring::ProbabilisticScorer<Arc<MyNetworkGraph>, Arc<MyLogger>>>;
347+
/// # type MyLiquidityManager = LiquidityManager<Arc<MyEntropySource>, Arc<MyChannelManager>, Arc<MyFilter>>;
348+
/// # fn setup_background_processing(my_persister: Arc<MyStore>, my_event_handler: Arc<MyEventHandler>, my_chain_monitor: Arc<MyChainMonitor>, my_channel_manager: Arc<MyChannelManager>, my_logger: Arc<MyLogger>, my_peer_manager: Arc<MyPeerManager>, my_liquidity_manager: Arc<MyLiquidityManager>) {
349+
/// let process_msgs_pm = Arc::clone(&my_peer_manager);
350+
/// let process_msgs_callback = move || process_msgs_pm.process_events();
351+
///
352+
/// my_liquidity_manager.set_process_msgs_callback(process_msgs_callback);
353+
/// # }
354+
/// ```
355+
///
356+
/// [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
357+
#[cfg(feature = "no-std")]
358+
pub fn set_process_msgs_callback(&self, callback: impl Fn() + 'static) {
359+
self.pending_messages.set_process_msgs_callback(callback)
360+
}
361+
301362
/// Blocks the current thread until next event is ready and returns it.
302363
///
303364
/// Typically you would spawn a thread or task that calls this in a loop.

src/message_queue.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use bitcoin::secp256k1::PublicKey;
1111
/// [`LiquidityManager`]: crate::LiquidityManager
1212
pub struct MessageQueue {
1313
queue: Mutex<VecDeque<(PublicKey, LSPSMessage)>>,
14+
#[cfg(feature = "std")]
1415
process_msgs_callback: RwLock<Option<Box<dyn Fn() + Send + Sync + 'static>>>,
16+
#[cfg(feature = "no-std")]
17+
process_msgs_callback: RwLock<Option<Box<dyn Fn() + 'static>>>,
1518
}
1619

1720
impl MessageQueue {
@@ -21,10 +24,16 @@ impl MessageQueue {
2124
Self { queue, process_msgs_callback }
2225
}
2326

27+
#[cfg(feature = "std")]
2428
pub(crate) fn set_process_msgs_callback(&self, callback: impl Fn() + Send + Sync + 'static) {
2529
*self.process_msgs_callback.write().unwrap() = Some(Box::new(callback));
2630
}
2731

32+
#[cfg(feature = "no-std")]
33+
pub(crate) fn set_process_msgs_callback(&self, callback: impl Fn() + 'static) {
34+
*self.process_msgs_callback.write().unwrap() = Some(Box::new(callback));
35+
}
36+
2837
pub(crate) fn get_and_clear_pending_msgs(&self) -> Vec<(PublicKey, LSPSMessage)> {
2938
self.queue.lock().unwrap().drain(..).collect()
3039
}

0 commit comments

Comments
 (0)