From 333bbc2ad8a59d00d781797f09e10c5ab3a73e13 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 23 Jan 2024 19:55:24 +0000 Subject: [PATCH] Stop relying on a `Clone`able `NetworkGraph` ref in `DefaultRouter` While there's not really much harm in requiring a `Clone`able reference (they almost always are), it does make our bindings struggle a bit as they don't support multi-trait bounds (as it would require synthesizing a new C trait, which the bindings don't do automatically). Luckily, there's really no reason for it, and we can just call the `DefaultMessageRouter` directly when we want to route a message. --- lightning/src/onion_message/messenger.rs | 39 ++++++++++++++++++------ lightning/src/routing/router.rs | 16 +++++----- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index c8f74b67e..f56f42506 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -313,13 +313,13 @@ where } } -impl>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter +impl>, L: Deref, ES: Deref> DefaultMessageRouter where L::Target: Logger, ES::Target: EntropySource, { - fn find_path( - &self, _sender: PublicKey, peers: Vec, destination: Destination + pub(crate) fn find_path( + network_graph: &G, _sender: PublicKey, peers: Vec, destination: Destination ) -> Result { let first_node = destination.first_node(); if peers.contains(&first_node) { @@ -327,7 +327,7 @@ where intermediate_nodes: vec![], destination, first_node_addresses: None }) } else { - let network_graph = self.network_graph.deref().read_only(); + let network_graph = network_graph.deref().read_only(); let node_announcement = network_graph .node(&NodeId::from_pubkey(&first_node)) .and_then(|node_info| node_info.announcement_info.as_ref()) @@ -346,10 +346,11 @@ where } } - fn create_blinded_paths< + pub(crate) fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, peers: Vec, secp_ctx: &Secp256k1, + network_graph: &G, recipient: PublicKey, peers: Vec, entropy_source: &ES, + secp_ctx: &Secp256k1, ) -> Result, ()> { // Limit the number of blinded paths that are computed. const MAX_PATHS: usize = 3; @@ -358,7 +359,7 @@ where // recipient's node_id. const MIN_PEER_CHANNELS: usize = 3; - let network_graph = self.network_graph.deref().read_only(); + let network_graph = network_graph.deref().read_only(); let paths = peers.iter() // Limit to peers with announced channels .filter(|pubkey| @@ -369,7 +370,7 @@ where .unwrap_or(false) ) .map(|pubkey| vec![*pubkey, recipient]) - .map(|node_pks| BlindedPath::new_for_message(&node_pks, &*self.entropy_source, secp_ctx)) + .map(|node_pks| BlindedPath::new_for_message(&node_pks, &**entropy_source, secp_ctx)) .take(MAX_PATHS) .collect::, _>>(); @@ -377,7 +378,7 @@ where Ok(paths) if !paths.is_empty() => Ok(paths), _ => { if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) { - BlindedPath::one_hop_for_message(recipient, &*self.entropy_source, secp_ctx) + BlindedPath::one_hop_for_message(recipient, &**entropy_source, secp_ctx) .map(|path| vec![path]) } else { Err(()) @@ -387,6 +388,26 @@ where } } +impl>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter +where + L::Target: Logger, + ES::Target: EntropySource, +{ + fn find_path( + &self, sender: PublicKey, peers: Vec, destination: Destination + ) -> Result { + Self::find_path(&self.network_graph, sender, peers, destination) + } + + fn create_blinded_paths< + T: secp256k1::Signing + secp256k1::Verification + >( + &self, recipient: PublicKey, peers: Vec, secp_ctx: &Secp256k1, + ) -> Result, ()> { + Self::create_blinded_paths(&self.network_graph, recipient, peers, &self.entropy_source, secp_ctx) + } +} + /// A path for sending an [`OnionMessage`]. #[derive(Clone)] pub struct OnionMessagePath { diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index b5aa2e561..d49f2c651 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -33,7 +33,7 @@ use core::{cmp, fmt}; use core::ops::Deref; /// A [`Router`] implemented using [`find_route`]. -pub struct DefaultRouter> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> where +pub struct DefaultRouter>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> where L::Target: Logger, S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>, ES::Target: EntropySource, @@ -43,22 +43,20 @@ pub struct DefaultRouter> + Clone, L: Deref, E entropy_source: ES, scorer: S, score_params: SP, - message_router: DefaultMessageRouter, } -impl> + Clone, L: Deref, ES: Deref + Clone, S: Deref, SP: Sized, Sc: ScoreLookUp> DefaultRouter where +impl>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> DefaultRouter where L::Target: Logger, S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>, ES::Target: EntropySource, { /// Creates a new router. pub fn new(network_graph: G, logger: L, entropy_source: ES, scorer: S, score_params: SP) -> Self { - let message_router = DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone()); - Self { network_graph, logger, entropy_source, scorer, score_params, message_router } + Self { network_graph, logger, entropy_source, scorer, score_params } } } -impl> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> Router for DefaultRouter where +impl>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> Router for DefaultRouter where L::Target: Logger, S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>, ES::Target: EntropySource, @@ -154,7 +152,7 @@ impl> + Clone, L: Deref, ES: Deref, S: Deref, } } -impl< G: Deref> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> MessageRouter for DefaultRouter where +impl< G: Deref>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp> MessageRouter for DefaultRouter where L::Target: Logger, S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>, ES::Target: EntropySource, @@ -162,7 +160,7 @@ impl< G: Deref> + Clone, L: Deref, ES: Deref, S: Deref, fn find_path( &self, sender: PublicKey, peers: Vec, destination: Destination ) -> Result { - self.message_router.find_path(sender, peers, destination) + DefaultMessageRouter::<_, _, ES>::find_path(&self.network_graph, sender, peers, destination) } fn create_blinded_paths< @@ -170,7 +168,7 @@ impl< G: Deref> + Clone, L: Deref, ES: Deref, S: Deref, > ( &self, recipient: PublicKey, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.message_router.create_blinded_paths(recipient, peers, secp_ctx) + DefaultMessageRouter::create_blinded_paths(&self.network_graph, recipient, peers, &self.entropy_source, secp_ctx) } } -- 2.39.5