Skip to content

Commit 9854bef

Browse files
committed
Move DefaultMessageRouter::create_blinded_paths
An upcoming change to the MessageRouter trait will require reusing DefaultMessageRouter::create_blinded_paths. Move the code to a utility function so facilitate this.
1 parent f0d81ea commit 9854bef

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -364,51 +364,12 @@ where
364364
pub fn new(network_graph: G, entropy_source: ES) -> Self {
365365
Self { network_graph, entropy_source }
366366
}
367-
}
368-
369-
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
370-
where
371-
L::Target: Logger,
372-
ES::Target: EntropySource,
373-
{
374-
fn find_path(
375-
&self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
376-
) -> Result<OnionMessagePath, ()> {
377-
let network_graph = self.network_graph.deref().read_only();
378-
destination.resolve(&network_graph);
379367

380-
let first_node = match destination.first_node() {
381-
Some(first_node) => first_node,
382-
None => return Err(()),
383-
};
384-
385-
if peers.contains(&first_node) || sender == first_node {
386-
Ok(OnionMessagePath {
387-
intermediate_nodes: vec![], destination, first_node_addresses: None
388-
})
389-
} else {
390-
let node_announcement = network_graph
391-
.node(&NodeId::from_pubkey(&first_node))
392-
.and_then(|node_info| node_info.announcement_info.as_ref())
393-
.and_then(|announcement_info| announcement_info.announcement_message.as_ref())
394-
.map(|node_announcement| &node_announcement.contents);
395-
396-
match node_announcement {
397-
Some(node_announcement) if node_announcement.features.supports_onion_messages() => {
398-
let first_node_addresses = Some(node_announcement.addresses.clone());
399-
Ok(OnionMessagePath {
400-
intermediate_nodes: vec![], destination, first_node_addresses
401-
})
402-
},
403-
_ => Err(()),
404-
}
405-
}
406-
}
407-
408-
fn create_blinded_paths<
368+
fn create_blinded_paths_from_iter<
369+
I: Iterator<Item = ForwardNode>,
409370
T: secp256k1::Signing + secp256k1::Verification
410371
>(
411-
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
372+
&self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>,
412373
) -> Result<Vec<BlindedPath>, ()> {
413374
// Limit the number of blinded paths that are computed.
414375
const MAX_PATHS: usize = 3;
@@ -421,7 +382,7 @@ where
421382
let is_recipient_announced =
422383
network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
423384

424-
let mut peer_info = peers.into_iter()
385+
let mut peer_info = peers
425386
// Limit to peers with announced channels
426387
.filter_map(|peer|
427388
network_graph
@@ -464,6 +425,54 @@ where
464425
}
465426
}
466427

428+
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
429+
where
430+
L::Target: Logger,
431+
ES::Target: EntropySource,
432+
{
433+
fn find_path(
434+
&self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
435+
) -> Result<OnionMessagePath, ()> {
436+
let network_graph = self.network_graph.deref().read_only();
437+
destination.resolve(&network_graph);
438+
439+
let first_node = match destination.first_node() {
440+
Some(first_node) => first_node,
441+
None => return Err(()),
442+
};
443+
444+
if peers.contains(&first_node) || sender == first_node {
445+
Ok(OnionMessagePath {
446+
intermediate_nodes: vec![], destination, first_node_addresses: None
447+
})
448+
} else {
449+
let node_announcement = network_graph
450+
.node(&NodeId::from_pubkey(&first_node))
451+
.and_then(|node_info| node_info.announcement_info.as_ref())
452+
.and_then(|announcement_info| announcement_info.announcement_message.as_ref())
453+
.map(|node_announcement| &node_announcement.contents);
454+
455+
match node_announcement {
456+
Some(node_announcement) if node_announcement.features.supports_onion_messages() => {
457+
let first_node_addresses = Some(node_announcement.addresses.clone());
458+
Ok(OnionMessagePath {
459+
intermediate_nodes: vec![], destination, first_node_addresses
460+
})
461+
},
462+
_ => Err(()),
463+
}
464+
}
465+
}
466+
467+
fn create_blinded_paths<
468+
T: secp256k1::Signing + secp256k1::Verification
469+
>(
470+
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
471+
) -> Result<Vec<BlindedPath>, ()> {
472+
self.create_blinded_paths_from_iter(recipient, peers.into_iter(), secp_ctx)
473+
}
474+
}
475+
467476
/// A path for sending an [`OnionMessage`].
468477
#[derive(Clone)]
469478
pub struct OnionMessagePath {

0 commit comments

Comments
 (0)