Skip to content

Stop relying on a Cloneable NetworkGraph ref in DefaultRouter #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 44 additions & 21 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ where
I: ExactSizeIterator<Item = ForwardNode>,
T: secp256k1::Signing + secp256k1::Verification
>(
&self, recipient: PublicKey, context: MessageContext, peers: I,
secp_ctx: &Secp256k1<T>, compact_paths: bool
network_graph: &G, recipient: PublicKey, context: MessageContext, peers: I,
entropy_source: &ES, secp_ctx: &Secp256k1<T>, compact_paths: bool,
) -> Result<Vec<BlindedPath>, ()> {
// Limit the number of blinded paths that are computed.
const MAX_PATHS: usize = 3;
Expand All @@ -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));

Expand Down Expand Up @@ -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::<Result<Vec<_>, _>>();
Expand All @@ -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(())
Expand All @@ -571,17 +571,11 @@ where

Ok(paths)
}
}

impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
where
L::Target: Logger,
ES::Target: EntropySource,
{
fn find_path(
&self, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
pub(crate) fn find_path(
network_graph: &G, sender: PublicKey, peers: Vec<PublicKey>, mut destination: Destination
) -> Result<OnionMessagePath, ()> {
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() {
Expand Down Expand Up @@ -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<PublicKey>, secp_ctx: &Secp256k1<T>,
network_graph: &G, recipient: PublicKey, context: MessageContext,
peers: Vec<PublicKey>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<ForwardNode>, entropy_source: &ES, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
Self::create_blinded_paths_from_iter(network_graph, recipient, context, peers.into_iter(), entropy_source, secp_ctx, true)
}
}

impl<G: Deref<Target=NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter for DefaultMessageRouter<G, L, ES>
where
L::Target: Logger,
ES::Target: EntropySource,
{
fn find_path(
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
) -> Result<OnionMessagePath, ()> {
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<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<ForwardNode>, secp_ctx: &Secp256k1<T>,
&self, recipient: PublicKey, context: MessageContext, peers: Vec<ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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`].
Expand Down
18 changes: 8 additions & 10 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use core::ops::Deref;
///
/// Implements [`MessageRouter`] by delegating to [`DefaultMessageRouter`]. See those docs for
/// privacy implications.
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
ES::Target: EntropySource,
Expand All @@ -51,22 +51,20 @@ pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, E
entropy_source: ES,
scorer: S,
score_params: SP,
message_router: DefaultMessageRouter<G, L, ES>,
}

impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref + Clone, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, ES, S, SP, Sc> where
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, ES, S, SP, Sc> 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<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, ES, S, SP, Sc> where
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, ES, S, SP, Sc> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
ES::Target: EntropySource,
Expand Down Expand Up @@ -179,31 +177,31 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref,
}
}

impl< G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> MessageRouter for DefaultRouter<G, L, ES, S, SP, Sc> where
impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> MessageRouter for DefaultRouter<G, L, ES, S, SP, Sc> where
L::Target: Logger,
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
ES::Target: EntropySource,
{
fn find_path(
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
) -> Result<OnionMessagePath, ()> {
self.message_router.find_path(sender, peers, destination)
DefaultMessageRouter::<_, _, ES>::find_path(&self.network_graph, sender, peers, destination)
}

fn create_blinded_paths<
T: secp256k1::Signing + secp256k1::Verification
> (
&self, recipient: PublicKey, context: MessageContext, peers: Vec<PublicKey>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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<
T: secp256k1::Signing + secp256k1::Verification
> (
&self, recipient: PublicKey, context: MessageContext, peers: Vec<message::ForwardNode>, secp_ctx: &Secp256k1<T>,
) -> Result<Vec<BlindedPath>, ()> {
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)
}
}

Expand Down
Loading