Skip to content

Commit fa404d1

Browse files
committed
Persist peer info from inbound channel requests
When we get an inbound channel request, we dont persist the peer info who initiated the request. In this commit, we use `[LdkEvent::ChannelPending]` to track channels that are inbound and their counterparty node id is not persisted, and if so, we add their `PeerInfo` to `PeerStore`. Notice that in order to persist this kind of data, we need first to get the listening address of the counterparty node, which is not always available, in that case we dont persist the peer info.
1 parent f42d604 commit fa404d1

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/event.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::peer_store::{PeerInfo, PeerStore};
12
use crate::{
23
hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, UserChannelId, Wallet,
34
};
@@ -251,6 +252,7 @@ where
251252
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
252253
logger: L,
253254
config: Arc<Config>,
255+
peer_store: Arc<PeerStore<K, L>>,
254256
}
255257

256258
impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
@@ -262,6 +264,7 @@ where
262264
channel_manager: Arc<ChannelManager<K>>, network_graph: Arc<NetworkGraph>,
263265
keys_manager: Arc<KeysManager>, payment_store: Arc<PaymentStore<K, L>>,
264266
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
267+
peer_store: Arc<PeerStore<K, L>>,
265268
) -> Self {
266269
Self {
267270
event_queue,
@@ -273,6 +276,7 @@ where
273276
logger,
274277
runtime,
275278
config,
279+
peer_store,
276280
}
277281
}
278282

@@ -739,6 +743,37 @@ where
739743
log_error!(self.logger, "Failed to push to event queue: {}", e);
740744
panic!("Failed to push to event queue");
741745
});
746+
let network_graph = self.network_graph.read_only();
747+
let channels =
748+
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
749+
if let Some(pending_channel) =
750+
channels.into_iter().find(|c| c.channel_id == channel_id)
751+
{
752+
if !pending_channel.is_outbound
753+
&& self.peer_store.get_peer(&counterparty_node_id).is_none()
754+
{
755+
if let Some(address) = network_graph
756+
.nodes()
757+
.get(&NodeId::from_pubkey(&counterparty_node_id))
758+
.and_then(|node_info| node_info.announcement_info.as_ref())
759+
.and_then(|ann_info| ann_info.addresses().first())
760+
{
761+
let peer = PeerInfo {
762+
node_id: counterparty_node_id,
763+
address: address.clone(),
764+
};
765+
766+
self.peer_store.add_peer(peer).unwrap_or_else(|e| {
767+
log_error!(
768+
self.logger,
769+
"Failed to add peer {} to peer store: {}",
770+
counterparty_node_id,
771+
e
772+
);
773+
});
774+
}
775+
}
776+
}
742777
}
743778
LdkEvent::ChannelReady {
744779
channel_id, user_channel_id, counterparty_node_id, ..

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,20 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
570570
.filter(|id| !pm_peers.contains(id))
571571
{
572572
if let Some(peer_info) = connect_peer_store.get_peer(&node_id) {
573-
let _ = do_connect_peer(
573+
let res = do_connect_peer(
574574
peer_info.node_id,
575575
peer_info.address,
576576
Arc::clone(&connect_pm),
577577
Arc::clone(&connect_logger),
578-
)
579-
.await;
578+
).await;
579+
match res {
580+
Ok(_) => {
581+
log_info!(connect_logger, "Successfully reconnected to peer {}", node_id);
582+
},
583+
Err(e) => {
584+
log_error!(connect_logger, "Failed to reconnect to peer {}: {}", node_id, e);
585+
}
586+
}
580587
}
581588
}
582589
}
@@ -656,6 +663,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
656663
Arc::clone(&self.runtime),
657664
Arc::clone(&self.logger),
658665
Arc::clone(&self.config),
666+
Arc::clone(&self.peer_store),
659667
));
660668

661669
// Setup background processing

0 commit comments

Comments
 (0)