Skip to content

Commit 7a1ad85

Browse files
committed
f: review comments
1 parent 69f1358 commit 7a1ad85

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
lines changed

src/builder.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::liquidity::{
2727
};
2828
use crate::logger::{log_error, LdkLogger, LogLevel, LogWriter, Logger};
2929
use crate::message_handler::NodeCustomMessageHandler;
30-
use crate::om_mailbox::OnionMessageMailbox;
30+
use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
3131
use crate::peer_store::PeerStore;
3232
use crate::runtime::Runtime;
3333
use crate::tx_broadcaster::TransactionBroadcaster;
@@ -1664,7 +1664,11 @@ fn build_with_store_internal(
16641664
},
16651665
};
16661666

1667-
let om_mailbox = Arc::new(OnionMessageMailbox::new());
1667+
let om_mailbox = if config.async_payment_services_enabled {
1668+
Some(Arc::new(OnionMessageMailbox::new()))
1669+
} else {
1670+
None
1671+
};
16681672

16691673
let (stop_sender, _) = tokio::sync::watch::channel(());
16701674
let (background_processor_stop_sender, _) = tokio::sync::watch::channel(());

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
330330
user_config.channel_handshake_limits.force_announced_channel_preference = true;
331331
}
332332

333-
user_config.enable_htlc_hold = true;
333+
if config.async_payment_services_enabled {
334+
user_config.enable_htlc_hold = true;
335+
}
334336

335337
user_config
336338
}

src/event.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use crate::om_mailbox::OnionMessageMailbox;
8+
use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
99
use crate::types::{CustomTlvRecord, DynStore, OnionMessenger, PaymentStore, Sweeper, Wallet};
1010
use crate::{
1111
hex_utils, BumpTransactionEventHandler, ChannelManager, Error, Graph, PeerInfo, PeerStore,
@@ -461,7 +461,7 @@ where
461461
config: Arc<Config>,
462462
static_invoice_store: Option<StaticInvoiceStore>,
463463
onion_messenger: Arc<OnionMessenger>,
464-
om_mailbox: Arc<OnionMessageMailbox>,
464+
om_mailbox: Option<Arc<OnionMessageMailbox>>,
465465
}
466466

467467
impl<L: Deref + Clone + Sync + Send + 'static> EventHandler<L>
@@ -476,7 +476,7 @@ where
476476
liquidity_source: Option<Arc<LiquiditySource<Arc<Logger>>>>,
477477
payment_store: Arc<PaymentStore>, peer_store: Arc<PeerStore<L>>,
478478
static_invoice_store: Option<StaticInvoiceStore>, onion_messenger: Arc<OnionMessenger>,
479-
om_mailbox: Arc<OnionMessageMailbox>, runtime: Arc<Runtime>, logger: L,
479+
om_mailbox: Option<Arc<OnionMessageMailbox>>, runtime: Arc<Runtime>, logger: L,
480480
config: Arc<Config>,
481481
) -> Self {
482482
Self {
@@ -1498,13 +1498,22 @@ where
14981498
self.bump_tx_event_handler.handle_event(&bte).await;
14991499
},
15001500
LdkEvent::OnionMessageIntercepted { peer_node_id, message } => {
1501-
self.om_mailbox.onion_message_intercepted(peer_node_id, message);
1501+
if let Some(om_mailbox) = self.om_mailbox.as_ref() {
1502+
om_mailbox.onion_message_intercepted(peer_node_id, message);
1503+
} else {
1504+
log_trace!(
1505+
self.logger,
1506+
"Onion message intercepted, but no onion message mailbox available"
1507+
);
1508+
}
15021509
},
15031510
LdkEvent::OnionMessagePeerConnected { peer_node_id } => {
1504-
let messages = self.om_mailbox.onion_message_peer_connected(peer_node_id);
1511+
if let Some(om_mailbox) = self.om_mailbox.as_ref() {
1512+
let messages = om_mailbox.onion_message_peer_connected(peer_node_id);
15051513

1506-
for message in messages {
1507-
let _ = self.onion_messenger.forward_onion_message(message, &peer_node_id);
1514+
for message in messages {
1515+
let _ = self.onion_messenger.forward_onion_message(message, &peer_node_id);
1516+
}
15081517
}
15091518
},
15101519

src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub mod io;
9292
pub mod liquidity;
9393
pub mod logger;
9494
mod message_handler;
95-
mod om_mailbox;
9695
pub mod payment;
9796
mod peer_store;
9897
mod runtime;
@@ -137,7 +136,7 @@ use gossip::GossipSource;
137136
use graph::NetworkGraph;
138137
use io::utils::write_node_metrics;
139138
use liquidity::{LSPS1Liquidity, LiquiditySource};
140-
use om_mailbox::OnionMessageMailbox;
139+
use payment::asynchronous::om_mailbox::OnionMessageMailbox;
141140
use payment::asynchronous::static_invoice_store::StaticInvoiceStore;
142141
use payment::{
143142
Bolt11Payment, Bolt12Payment, OnchainPayment, PaymentDetails, SpontaneousPayment,
@@ -207,7 +206,7 @@ pub struct Node {
207206
is_running: Arc<RwLock<bool>>,
208207
is_listening: Arc<AtomicBool>,
209208
node_metrics: Arc<RwLock<NodeMetrics>>,
210-
om_mailbox: Arc<OnionMessageMailbox>,
209+
om_mailbox: Option<Arc<OnionMessageMailbox>>,
211210
}
212211

213212
impl Node {
@@ -521,7 +520,7 @@ impl Node {
521520
Arc::clone(&self.peer_store),
522521
static_invoice_store,
523522
Arc::clone(&self.onion_messenger),
524-
Arc::clone(&self.om_mailbox),
523+
self.om_mailbox.clone(),
525524
Arc::clone(&self.runtime),
526525
Arc::clone(&self.logger),
527526
Arc::clone(&self.config),
@@ -1496,11 +1495,6 @@ impl Node {
14961495
Error::PersistenceFailed
14971496
})
14981497
}
1499-
1500-
#[allow(missing_docs)]
1501-
pub fn om_mailbox_is_empty(&self) -> bool {
1502-
self.om_mailbox.is_empty()
1503-
}
15041498
}
15051499

15061500
impl Drop for Node {

src/payment/asynchronous/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8+
pub(crate) mod om_mailbox;
89
mod rate_limiter;
910
pub(crate) mod static_invoice_store;

src/om_mailbox.rs renamed to src/payment/asynchronous/om_mailbox.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
use std::{
2-
collections::{HashMap, VecDeque},
3-
sync::Mutex,
4-
};
1+
use std::collections::{HashMap, VecDeque};
2+
use std::sync::Mutex;
53

4+
use bitcoin::secp256k1::PublicKey;
65
use lightning::ln::msgs::OnionMessage;
76

87
pub(crate) struct OnionMessageMailbox {
98
map: Mutex<HashMap<bitcoin::secp256k1::PublicKey, VecDeque<lightning::ln::msgs::OnionMessage>>>,
109
}
1110

1211
impl OnionMessageMailbox {
13-
const MAX_MESSAGES_PER_PEER: usize = 100;
14-
const MAX_PEERS: usize = 100;
12+
const MAX_MESSAGES_PER_PEER: usize = 30;
13+
const MAX_PEERS: usize = 300;
1514

1615
pub fn new() -> Self {
17-
Self { map: Mutex::new(HashMap::new()) }
16+
Self { map: Mutex::new(HashMap::with_capacity(Self::MAX_PEERS)) }
1817
}
1918

20-
pub(crate) fn onion_message_intercepted(
21-
&self, peer_node_id: bitcoin::secp256k1::PublicKey,
22-
message: lightning::ln::msgs::OnionMessage,
23-
) {
19+
pub(crate) fn onion_message_intercepted(&self, peer_node_id: PublicKey, message: OnionMessage) {
2420
let mut map = self.map.lock().unwrap();
2521

2622
let queue = map.entry(peer_node_id).or_insert_with(VecDeque::new);
@@ -31,11 +27,8 @@ impl OnionMessageMailbox {
3127

3228
// Enforce a peers limit. If exceeded, evict the peer with the longest queue.
3329
if map.len() > Self::MAX_PEERS {
34-
let peer_to_remove = map
35-
.iter()
36-
.max_by_key(|(_, queue)| queue.len())
37-
.map(|(peer, _)| peer.clone())
38-
.unwrap();
30+
let peer_to_remove =
31+
map.iter().max_by_key(|(_, queue)| queue.len()).map(|(peer, _)| *peer).unwrap();
3932

4033
map.remove(&peer_to_remove);
4134
}
@@ -53,6 +46,7 @@ impl OnionMessageMailbox {
5346
}
5447
}
5548

49+
#[cfg(test)]
5650
pub(crate) fn is_empty(&self) -> bool {
5751
let map = self.map.lock().unwrap();
5852
map.is_empty()
@@ -61,13 +55,11 @@ impl OnionMessageMailbox {
6155

6256
#[cfg(test)]
6357
mod tests {
64-
use bitcoin::{
65-
key::Secp256k1,
66-
secp256k1::{PublicKey, SecretKey},
67-
};
58+
use bitcoin::key::Secp256k1;
59+
use bitcoin::secp256k1::{PublicKey, SecretKey};
6860
use lightning::onion_message;
6961

70-
use crate::om_mailbox::OnionMessageMailbox;
62+
use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
7163

7264
#[test]
7365
fn onion_message_mailbox() {

tests/integration_tests_rust.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,7 @@ fn async_payment() {
12471247
node_sender.bolt12_payment().send_using_amount(&offer, 5_000, None, None).unwrap();
12481248

12491249
// Sleep to allow the payment reach a state where the htlc is held and waiting for the receiver to come online.
1250-
while node_receiver_lsp.om_mailbox_is_empty() {
1251-
std::thread::sleep(std::time::Duration::from_millis(100));
1252-
}
1250+
std::thread::sleep(std::time::Duration::from_millis(3000));
12531251

12541252
node_receiver.start().unwrap();
12551253

0 commit comments

Comments
 (0)