From 9de7c1db3b538c2724d3aa6f4b35f4ec1fddf4ac Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 12 Aug 2024 01:03:00 +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. We've carried this patch for a while on the bindings branch, but there's not a strong reason it can't go upstream. --- lightning/src/onion_message/messenger.rs | 65 ++++++++++++++++-------- lightning/src/routing/router.rs | 18 +++---- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 2d7009819..13e6696c5 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -506,8 +506,8 @@ where I: ExactSizeIterator, T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, peers: I, - secp_ctx: &Secp256k1, compact_paths: bool + network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I, + entropy_source: &ES, secp_ctx: &Secp256k1, compact_paths: bool, ) -> Result, ()> { // Limit the number of blinded paths that are computed. const MAX_PATHS: usize = 3; @@ -516,7 +516,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 is_recipient_announced = network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)); @@ -546,7 +546,7 @@ where let paths = peer_info.into_iter() .map(|(peer, _, _)| { - BlindedPath::new_for_message(&[peer], recipient, context.clone(), &*self.entropy_source, secp_ctx) + BlindedPath::new_for_message(&[peer], recipient, context.clone(), &**entropy_source, secp_ctx) }) .take(MAX_PATHS) .collect::, _>>(); @@ -555,7 +555,7 @@ where Ok(paths) if !paths.is_empty() => Ok(paths), _ => { if is_recipient_announced { - BlindedPath::one_hop_for_message(recipient, context, &*self.entropy_source, secp_ctx) + BlindedPath::one_hop_for_message(recipient, context, &**entropy_source, secp_ctx) .map(|path| vec![path]) } else { Err(()) @@ -571,17 +571,11 @@ where Ok(paths) } -} -impl>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter -where - L::Target: Logger, - ES::Target: EntropySource, -{ - fn find_path( - &self, sender: PublicKey, peers: Vec, mut destination: Destination + pub(crate) fn find_path( + network_graph: &G, sender: PublicKey, peers: Vec, mut destination: Destination ) -> Result { - let network_graph = self.network_graph.deref().read_only(); + let network_graph = network_graph.deref().read_only(); destination.resolve(&network_graph); let first_node = match destination.first_node() { @@ -611,26 +605,55 @@ where } } - fn create_blinded_paths< + pub(crate) fn create_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, - peers: Vec, secp_ctx: &Secp256k1, + network_graph: &G, recipient: PublicKey, context: MessageContext, + peers: Vec, entropy_source: &ES, secp_ctx: &Secp256k1, ) -> Result, ()> { let peers = peers .into_iter() .map(|node_id| ForwardNode { node_id, short_channel_id: None }); - self.create_blinded_paths_from_iter(recipient, context, peers, secp_ctx, false) + Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, false) + } + + pub(crate) fn create_compact_blinded_paths< + T: secp256k1::Signing + secp256k1::Verification + >( + network_graph: &G, recipient: PublicKey, context: MessageContext, + peers: Vec, entropy_source: &ES, secp_ctx: &Secp256k1, + ) -> Result, ()> { + Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, true) + } +} + +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, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, + ) -> Result, ()> { + Self::create_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } fn create_compact_blinded_paths< T: secp256k1::Signing + secp256k1::Verification >( - &self, recipient: PublicKey, context: MessageContext, - peers: Vec, secp_ctx: &Secp256k1, + &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.create_blinded_paths_from_iter(recipient, context, peers.into_iter(), secp_ctx, true) + Self::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } + } /// A path for sending an [`OnionMessage`]. diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index d7c665ff1..1e341ee5d 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -41,7 +41,7 @@ use core::ops::Deref; /// /// Implements [`MessageRouter`] by delegating to [`DefaultMessageRouter`]. See those docs for /// privacy implications. -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, @@ -51,22 +51,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, @@ -179,7 +177,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, @@ -187,7 +185,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< @@ -195,7 +193,7 @@ impl< G: Deref> + Clone, L: Deref, ES: Deref, S: Deref, > ( &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.message_router.create_blinded_paths(recipient, context, peers, secp_ctx) + DefaultMessageRouter::create_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } fn create_compact_blinded_paths< @@ -203,7 +201,7 @@ impl< G: Deref> + Clone, L: Deref, ES: Deref, S: Deref, > ( &self, recipient: PublicKey, context: MessageContext, peers: Vec, secp_ctx: &Secp256k1, ) -> Result, ()> { - self.message_router.create_compact_blinded_paths(recipient, context, peers, secp_ctx) + DefaultMessageRouter::create_compact_blinded_paths(&self.network_graph, recipient, context, peers, &self.entropy_source, secp_ctx) } } -- 2.39.5