Skip to content

Commit 408c89c

Browse files
committed
Refactor: Introduce get_peers_for_blinded_path utility
Added a new helper function to encapsulate the logic for fetching peers used in blinded path creation. This keeps the code DRY by reducing duplication and makes the logic easier to reuse across different message routers.
1 parent 90bc04b commit 408c89c

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10870,6 +10870,23 @@ where
1087010870
now
1087110871
}
1087210872

10873+
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
10874+
self.per_peer_state.read().unwrap()
10875+
.iter()
10876+
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10877+
.filter(|(_, peer)| peer.is_connected)
10878+
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10879+
.map(|(node_id, peer)| MessageForwardNode {
10880+
node_id: *node_id,
10881+
short_channel_id: peer.channel_by_id
10882+
.iter()
10883+
.filter(|(_, channel)| channel.context().is_usable())
10884+
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10885+
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10886+
})
10887+
.collect::<Vec<_>>()
10888+
}
10889+
1087310890
/// Creates a collection of blinded paths by delegating to
1087410891
/// [`MessageRouter::create_blinded_paths`].
1087510892
///
@@ -10878,16 +10895,7 @@ where
1087810895
let recipient = self.get_our_node_id();
1087910896
let secp_ctx = &self.secp_ctx;
1088010897

10881-
let peers = self.per_peer_state.read().unwrap()
10882-
.iter()
10883-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10884-
.filter(|(_, peer)| peer.is_connected)
10885-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10886-
.map(|(node_id, _)| MessageForwardNode {
10887-
node_id: *node_id,
10888-
short_channel_id: None
10889-
})
10890-
.collect::<Vec<_>>();
10898+
let peers = self.get_peers_for_blinded_path();
1089110899

1089210900
self.message_router
1089310901
.create_blinded_paths(recipient, context, peers, secp_ctx)
@@ -10902,20 +10910,7 @@ where
1090210910
let recipient = self.get_our_node_id();
1090310911
let secp_ctx = &self.secp_ctx;
1090410912

10905-
let peers = self.per_peer_state.read().unwrap()
10906-
.iter()
10907-
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
10908-
.filter(|(_, peer)| peer.is_connected)
10909-
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
10910-
.map(|(node_id, peer)| MessageForwardNode {
10911-
node_id: *node_id,
10912-
short_channel_id: peer.channel_by_id
10913-
.iter()
10914-
.filter(|(_, channel)| channel.context().is_usable())
10915-
.min_by_key(|(_, channel)| channel.context().channel_creation_height)
10916-
.and_then(|(_, channel)| channel.context().get_short_channel_id()),
10917-
})
10918-
.collect::<Vec<_>>();
10913+
let peers = self.get_peers_for_blinded_path();
1091910914

1092010915
self.message_router
1092110916
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)

0 commit comments

Comments
 (0)