Skip to content

Commit 91181d8

Browse files
committed
Refactor: Extract get_peers_for_blinded_path helper
Encapsulates logic for fetching peers used in blinded path creation. Reduces duplication and improves reusability across functions.
1 parent 8e9547c commit 91181d8

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10588,6 +10588,23 @@ where
1058810588
now
1058910589
}
1059010590

10591+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10592+
self.per_peer_state.read().unwrap()
10593+
.iter()
10594+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10595+
.filter(|(_, peer)| peer.is_connected)
10596+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10597+
.map(|(node_id, peer)| MessageForwardNode {
10598+
node_id: *node_id,
10599+
short_channel_id: peer.channel_by_id
10600+
.iter()
10601+
.filter(|(_, channel)| channel.context().is_usable())
10602+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10603+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10604+
})
10605+
.collect::<Vec<_>>()
10606+
}
10607+
1059110608
/// Creates a collection of blinded paths by delegating to
1059210609
/// [`MessageRouter::create_blinded_paths`].
1059310610
///
@@ -10596,13 +10613,10 @@ where
1059610613
let recipient = self.get_our_node_id();
1059710614
let secp_ctx = &self.secp_ctx;
1059810615

10599-
let peers = self.per_peer_state.read().unwrap()
10600-
.iter()
10601-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10602-
.filter(|(_, peer)| peer.is_connected)
10603-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10604-
.map(|(node_id, _)| *node_id)
10605-
.collect::<Vec<_>>();
10616+
let peers = self.get_peers_for_blinded_path()
10617+
.into_iter()
10618+
.map(|node| node.node_id)
10619+
.collect();
1060610620

1060710621
self.message_router
1060810622
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10617,20 +10631,7 @@ where
1061710631
let recipient = self.get_our_node_id();
1061810632
let secp_ctx = &self.secp_ctx;
1061910633

10620-
let peers = self.per_peer_state.read().unwrap()
10621-
.iter()
10622-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10623-
.filter(|(_, peer)| peer.is_connected)
10624-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10625-
.map(|(node_id, peer)| MessageForwardNode {
10626-
node_id: *node_id,
10627-
short_channel_id: peer.channel_by_id
10628-
.iter()
10629-
.filter(|(_, channel)| channel.context().is_usable())
10630-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10631-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10632-
})
10633-
.collect::<Vec<_>>();
10634+
let peers = self.get_peers_for_blinded_path();
1063410635

1063510636
self.message_router
1063610637
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)