X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Fmessenger.rs;h=cf0f9e086b5c41a12c2e9b1b5e27f3a03949fae7;hb=1f399b0984d591019320459d804cbad859c608ae;hp=8960058fa9f83803f9ec74d220347bfd9193aefc;hpb=1852715fc8163262e370f49995e81ea6efb1cf83;p=rust-lightning diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 8960058f..cf0f9e08 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -19,6 +19,8 @@ use crate::blinded_path::BlindedPath; use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs}; use crate::blinded_path::utils; use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient}; +#[cfg(not(c_bindings))] +use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler}; use crate::ln::onion_utils; @@ -157,6 +159,7 @@ where /// /// These are obtained when released from [`OnionMessenger`]'s handlers after which they are /// enqueued for sending. +#[cfg(not(c_bindings))] pub struct PendingOnionMessage { /// The message contents to send in an [`OnionMessage`]. pub contents: T, @@ -168,6 +171,22 @@ pub struct PendingOnionMessage { pub reply_path: Option, } +#[cfg(c_bindings)] +/// An [`OnionMessage`] for [`OnionMessenger`] to send. +/// +/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are +/// enqueued for sending. +pub type PendingOnionMessage = (T, Destination, Option); + +pub(crate) fn new_pending_onion_message( + contents: T, destination: Destination, reply_path: Option +) -> PendingOnionMessage { + #[cfg(not(c_bindings))] + return PendingOnionMessage { contents, destination, reply_path }; + #[cfg(c_bindings)] + return (contents, destination, reply_path); +} + /// A trait defining behavior for routing an [`OnionMessage`]. pub trait MessageRouter { /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`]. @@ -176,14 +195,18 @@ pub trait MessageRouter { ) -> Result; } -/// A [`MessageRouter`] that always fails. +/// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. pub struct DefaultMessageRouter; impl MessageRouter for DefaultMessageRouter { fn find_path( - &self, _sender: PublicKey, _peers: Vec, _destination: Destination + &self, _sender: PublicKey, peers: Vec, destination: Destination ) -> Result { - Err(()) + if peers.contains(&destination.first_node()) { + Ok(OnionMessagePath { intermediate_nodes: vec![], destination }) + } else { + Err(()) + } } } @@ -213,6 +236,13 @@ impl Destination { Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => blinded_hops.len(), } } + + fn first_node(&self) -> PublicKey { + match self { + Destination::Node(node_id) => *node_id, + Destination::BlindedPath(BlindedPath { introduction_node_id: node_id, .. }) => *node_id, + } + } } /// Errors that may occur when [sending an onion message]. @@ -273,7 +303,15 @@ pub trait CustomOnionMessageHandler { /// /// Typically, this is used for messages initiating a message flow rather than in response to /// another message. The latter should use the return value of [`Self::handle_custom_message`]. + #[cfg(not(c_bindings))] fn release_pending_custom_messages(&self) -> Vec>; + + /// Releases any [`Self::CustomMessage`]s that need to be sent. + /// + /// Typically, this is used for messages initiating a message flow rather than in response to + /// another message. The latter should use the return value of [`Self::handle_custom_message`]. + #[cfg(c_bindings)] + fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, Destination, Option)>; } /// A processed incoming onion message, containing either a Forward (another onion message) @@ -673,7 +711,10 @@ where fn next_onion_message_for_peer(&self, peer_node_id: PublicKey) -> Option { // Enqueue any initiating `OffersMessage`s to send. for message in self.offers_handler.release_pending_messages() { + #[cfg(not(c_bindings))] let PendingOnionMessage { contents, destination, reply_path } = message; + #[cfg(c_bindings)] + let (contents, destination, reply_path) = message; self.find_path_and_enqueue_onion_message( contents, destination, reply_path, format_args!("when sending OffersMessage") ); @@ -681,7 +722,10 @@ where // Enqueue any initiating `CustomMessage`s to send. for message in self.custom_handler.release_pending_custom_messages() { + #[cfg(not(c_bindings))] let PendingOnionMessage { contents, destination, reply_path } = message; + #[cfg(c_bindings)] + let (contents, destination, reply_path) = message; self.find_path_and_enqueue_onion_message( contents, destination, reply_path, format_args!("when sending CustomMessage") ); @@ -700,32 +744,36 @@ where /// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and /// [`SimpleArcPeerManager`]. See their docs for more details. /// -/// This is not exported to bindings users as `Arc`s don't make sense in bindings. +/// This is not exported to bindings users as type aliases aren't supported in most languages. /// /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager -pub type SimpleArcOnionMessenger = OnionMessenger< +#[cfg(not(c_bindings))] +pub type SimpleArcOnionMessenger = OnionMessenger< Arc, Arc, Arc, Arc, - IgnoringMessageHandler, + Arc>, IgnoringMessageHandler >; /// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and /// [`SimpleRefPeerManager`]. See their docs for more details. /// -/// This is not exported to bindings users as general type aliases don't make sense in bindings. +/// This is not exported to bindings users as type aliases aren't supported in most languages. /// /// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager /// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager -pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger< +#[cfg(not(c_bindings))] +pub type SimpleRefOnionMessenger< + 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, M, T, F, L +> = OnionMessenger< &'a KeysManager, &'a KeysManager, &'b L, - &'c DefaultMessageRouter, - IgnoringMessageHandler, + &'i DefaultMessageRouter, + &'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>, IgnoringMessageHandler >;