Skip to content

Commit b3d3560

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 22d4b42 commit b3d3560

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
@@ -10862,6 +10862,23 @@ where
1086210862
now
1086310863
}
1086410864

10865+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10866+
self.per_peer_state.read().unwrap()
10867+
.iter()
10868+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10869+
.filter(|(_, peer)| peer.is_connected)
10870+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10871+
.map(|(node_id, peer)| MessageForwardNode {
10872+
node_id: *node_id,
10873+
short_channel_id: peer.channel_by_id
10874+
.iter()
10875+
.filter(|(_, channel)| channel.context().is_usable())
10876+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10877+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10878+
})
10879+
.collect::<Vec<_>>()
10880+
}
10881+
1086510882
/// Creates a collection of blinded paths by delegating to
1086610883
/// [`MessageRouter::create_blinded_paths`].
1086710884
///
@@ -10870,13 +10887,10 @@ where
1087010887
let recipient = self.get_our_node_id();
1087110888
let secp_ctx = &self.secp_ctx;
1087210889

10873-
let peers = self.per_peer_state.read().unwrap()
10874-
.iter()
10875-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10876-
.filter(|(_, peer)| peer.is_connected)
10877-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10878-
.map(|(node_id, _)| *node_id)
10879-
.collect::<Vec<_>>();
10890+
let peers = self.get_peers_for_blinded_path()
10891+
.into_iter()
10892+
.map(|node| node.node_id)
10893+
.collect();
1088010894

1088110895
self.message_router
1088210896
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10891,20 +10905,7 @@ where
1089110905
let recipient = self.get_our_node_id();
1089210906
let secp_ctx = &self.secp_ctx;
1089310907

10894-
let peers = self.per_peer_state.read().unwrap()
10895-
.iter()
10896-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10897-
.filter(|(_, peer)| peer.is_connected)
10898-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10899-
.map(|(node_id, peer)| MessageForwardNode {
10900-
node_id: *node_id,
10901-
short_channel_id: peer.channel_by_id
10902-
.iter()
10903-
.filter(|(_, channel)| channel.context().is_usable())
10904-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10905-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10906-
})
10907-
.collect::<Vec<_>>();
10908+
let peers = self.get_peers_for_blinded_path();
1090810909

1090910910
self.message_router
1091010911
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)