Skip to content

Commit e9f83c8

Browse files
shaavanvalentinewallace
authored andcommitted
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 2c3c72d commit e9f83c8

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
@@ -10899,6 +10899,23 @@ where
1089910899
now
1090010900
}
1090110901

10902+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10903+
self.per_peer_state.read().unwrap()
10904+
.iter()
10905+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10906+
.filter(|(_, peer)| peer.is_connected)
10907+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10908+
.map(|(node_id, peer)| MessageForwardNode {
10909+
node_id: *node_id,
10910+
short_channel_id: peer.channel_by_id
10911+
.iter()
10912+
.filter(|(_, channel)| channel.context().is_usable())
10913+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10914+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10915+
})
10916+
.collect::<Vec<_>>()
10917+
}
10918+
1090210919
/// Creates a collection of blinded paths by delegating to
1090310920
/// [`MessageRouter::create_blinded_paths`].
1090410921
///
@@ -10907,13 +10924,10 @@ where
1090710924
let recipient = self.get_our_node_id();
1090810925
let secp_ctx = &self.secp_ctx;
1090910926

10910-
let peers = self.per_peer_state.read().unwrap()
10911-
.iter()
10912-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10913-
.filter(|(_, peer)| peer.is_connected)
10914-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10915-
.map(|(node_id, _)| *node_id)
10916-
.collect::<Vec<_>>();
10927+
let peers = self.get_peers_for_blinded_path()
10928+
.into_iter()
10929+
.map(|node| node.node_id)
10930+
.collect();
1091710931

1091810932
self.message_router
1091910933
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10928,20 +10942,7 @@ where
1092810942
let recipient = self.get_our_node_id();
1092910943
let secp_ctx = &self.secp_ctx;
1093010944

10931-
let peers = self.per_peer_state.read().unwrap()
10932-
.iter()
10933-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10934-
.filter(|(_, peer)| peer.is_connected)
10935-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10936-
.map(|(node_id, peer)| MessageForwardNode {
10937-
node_id: *node_id,
10938-
short_channel_id: peer.channel_by_id
10939-
.iter()
10940-
.filter(|(_, channel)| channel.context().is_usable())
10941-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10942-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10943-
})
10944-
.collect::<Vec<_>>();
10945+
let peers = self.get_peers_for_blinded_path();
1094510946

1094610947
self.message_router
1094710948
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)