Skip to content

Commit defc540

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 2701bc5 commit defc540

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -449,50 +449,12 @@ where
449449
pub fn new(network_graph: G, entropy_source: ES) -> Self {
450450
Self { network_graph, entropy_source }
451451
}
452-
}
453-
454-
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
455-
where
456-
L::Target: Logger,
457-
ES::Target: EntropySource,
458-
{
459-
fn find_path(
460-
&self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
461-
) -> Result<OnionMessagePath, ()> {
462-
let network_graph = self.network_graph.deref().read_only();
463-
destination.resolve(&network_graph);
464-
465-
let first_node = match destination.first_node() {
466-
Some(first_node) => first_node,
467-
None => return Err(()),
468-
};
469-
470-
if peers.contains(&first_node) || sender == first_node {
471-
Ok(OnionMessagePath {
472-
intermediate_nodes: vec![], destination, first_node_addresses: None
473-
})
474-
} else {
475-
let node_details = network_graph
476-
.node(&NodeId::from_pubkey(&first_node))
477-
.and_then(|node_info| node_info.announcement_info.as_ref())
478-
.map(|announcement_info| (announcement_info.features(), announcement_info.addresses()));
479-
480-
match node_details {
481-
Some((features, addresses)) if features.supports_onion_messages() && addresses.len() > 0 => {
482-
let first_node_addresses = Some(addresses.clone());
483-
Ok(OnionMessagePath {
484-
intermediate_nodes: vec![], destination, first_node_addresses
485-
})
486-
},
487-
_ => Err(()),
488-
}
489-
}
490-
}
491452

492-
fn create_blinded_paths<
453+
fn create_blinded_paths_from_iter<
454+
I: Iterator<Item = ForwardNode>,
493455
T: secp256k1::Signing + secp256k1::Verification
494456
>(
495-
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
457+
&self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>,
496458
) -> Result<Vec<BlindedPath>, ()> {
497459
// Limit the number of blinded paths that are computed.
498460
const MAX_PATHS: usize = 3;
@@ -505,7 +467,7 @@ where
505467
let is_recipient_announced =
506468
network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
507469

508-
let mut peer_info = peers.into_iter()
470+
let mut peer_info = peers
509471
// Limit to peers with announced channels
510472
.filter_map(|peer|
511473
network_graph
@@ -548,6 +510,53 @@ where
548510
}
549511
}
550512

513+
impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
514+
where
515+
L::Target: Logger,
516+
ES::Target: EntropySource,
517+
{
518+
fn find_path(
519+
&self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
520+
) -> Result<OnionMessagePath, ()> {
521+
let network_graph = self.network_graph.deref().read_only();
522+
destination.resolve(&network_graph);
523+
524+
let first_node = match destination.first_node() {
525+
Some(first_node) => first_node,
526+
None => return Err(()),
527+
};
528+
529+
if peers.contains(&first_node) || sender == first_node {
530+
Ok(OnionMessagePath {
531+
intermediate_nodes: vec![], destination, first_node_addresses: None
532+
})
533+
} else {
534+
let node_details = network_graph
535+
.node(&NodeId::from_pubkey(&first_node))
536+
.and_then(|node_info| node_info.announcement_info.as_ref())
537+
.map(|announcement_info| (announcement_info.features(), announcement_info.addresses()));
538+
539+
match node_details {
540+
Some((features, addresses)) if features.supports_onion_messages() && addresses.len() > 0 => {
541+
let first_node_addresses = Some(addresses.clone());
542+
Ok(OnionMessagePath {
543+
intermediate_nodes: vec![], destination, first_node_addresses
544+
})
545+
},
546+
_ => Err(()),
547+
}
548+
}
549+
}
550+
551+
fn create_blinded_paths<
552+
T: secp256k1::Signing + secp256k1::Verification
553+
>(
554+
&self, recipient: PublicKey, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
555+
) -> Result<Vec<BlindedPath>, ()> {
556+
self.create_blinded_paths_from_iter(recipient, peers.into_iter(), secp_ctx)
557+
}
558+
}
559+
551560
/// A path for sending an [`OnionMessage`].
552561
#[derive(Clone)]
553562
pub struct OnionMessagePath {

0 commit comments

Comments
 (0)