Skip to content

Commit c1faa6c

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 1610854 commit c1faa6c

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

lightning/src/ln/channelmanager.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -10763,6 +10763,23 @@ where
1076310763
now
1076410764
}
1076510765

10766+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10767+
self.per_peer_state.read().unwrap()
10768+
.iter()
10769+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10770+
.filter(|(_, peer)| peer.is_connected)
10771+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10772+
.map(|(node_id, peer)| MessageForwardNode {
10773+
node_id: *node_id,
10774+
short_channel_id: peer.channel_by_id
10775+
.iter()
10776+
.filter(|(_, channel)| channel.context().is_usable())
10777+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10778+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10779+
})
10780+
.collect::<Vec<_>>()
10781+
}
10782+
1076610783
/// Creates a collection of blinded paths by delegating to
1076710784
/// [`MessageRouter::create_blinded_paths`].
1076810785
///
@@ -10771,13 +10788,10 @@ where
1077110788
let recipient = self.get_our_node_id();
1077210789
let secp_ctx = &self.secp_ctx;
1077310790

10774-
let peers = self.per_peer_state.read().unwrap()
10775-
.iter()
10776-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10777-
.filter(|(_, peer)| peer.is_connected)
10778-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10779-
.map(|(node_id, _)| *node_id)
10780-
.collect::<Vec<_>>();
10791+
let peers = self.get_peers_for_blinded_path()
10792+
.into_iter()
10793+
.map(|node| node.node_id)
10794+
.collect();
1078110795

1078210796
self.message_router
1078310797
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10792,20 +10806,7 @@ where
1079210806
let recipient = self.get_our_node_id();
1079310807
let secp_ctx = &self.secp_ctx;
1079410808

10795-
let peers = self.per_peer_state.read().unwrap()
10796-
.iter()
10797-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10798-
.filter(|(_, peer)| peer.is_connected)
10799-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10800-
.map(|(node_id, peer)| MessageForwardNode {
10801-
node_id: *node_id,
10802-
short_channel_id: peer.channel_by_id
10803-
.iter()
10804-
.filter(|(_, channel)| channel.context().is_usable())
10805-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10806-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10807-
})
10808-
.collect::<Vec<_>>();
10809+
let peers = self.get_peers_for_blinded_path();
1080910810

1081010811
self.message_router
1081110812
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)