Skip to content

Commit abfcc5b

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 78fee88 commit abfcc5b

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
@@ -10891,6 +10891,23 @@ where
1089110891
now
1089210892
}
1089310893

10894+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10895+
self.per_peer_state.read().unwrap()
10896+
.iter()
10897+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10898+
.filter(|(_, peer)| peer.is_connected)
10899+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10900+
.map(|(node_id, peer)| MessageForwardNode {
10901+
node_id: *node_id,
10902+
short_channel_id: peer.channel_by_id
10903+
.iter()
10904+
.filter(|(_, channel)| channel.context().is_usable())
10905+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10906+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10907+
})
10908+
.collect::<Vec<_>>()
10909+
}
10910+
1089410911
/// Creates a collection of blinded paths by delegating to
1089510912
/// [`MessageRouter::create_blinded_paths`].
1089610913
///
@@ -10899,13 +10916,10 @@ where
1089910916
let recipient = self.get_our_node_id();
1090010917
let secp_ctx = &self.secp_ctx;
1090110918

10902-
let peers = self.per_peer_state.read().unwrap()
10903-
.iter()
10904-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10905-
.filter(|(_, peer)| peer.is_connected)
10906-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10907-
.map(|(node_id, _)| *node_id)
10908-
.collect::<Vec<_>>();
10919+
let peers = self.get_peers_for_blinded_path()
10920+
.into_iter()
10921+
.map(|node| node.node_id)
10922+
.collect();
1090910923

1091010924
self.message_router
1091110925
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10920,20 +10934,7 @@ where
1092010934
let recipient = self.get_our_node_id();
1092110935
let secp_ctx = &self.secp_ctx;
1092210936

10923-
let peers = self.per_peer_state.read().unwrap()
10924-
.iter()
10925-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10926-
.filter(|(_, peer)| peer.is_connected)
10927-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10928-
.map(|(node_id, peer)| MessageForwardNode {
10929-
node_id: *node_id,
10930-
short_channel_id: peer.channel_by_id
10931-
.iter()
10932-
.filter(|(_, channel)| channel.context().is_usable())
10933-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10934-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10935-
})
10936-
.collect::<Vec<_>>();
10937+
let peers = self.get_peers_for_blinded_path();
1093710938

1093810939
self.message_router
1093910940
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)