Skip to content

Commit 6ebedd0

Browse files
committed
Add CompactMessageRouter and NullMessageRouter
Introduced two new default routers: - `CompactMessageRouter` – for creating compact blinded paths - `NullMessageRouter` – for intentionally creating no blinded paths This makes the purpose of each router explicit and ensures their functions remain unique. It also builds on the flexibility introduced in the previous commit, where `create_blinded_paths` was updated to support a MessageForwardNode.
1 parent 7469bcf commit 6ebedd0

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,119 @@ where
704704
}
705705
}
706706

707+
/// A [`MessageRouter`] that can only route to a directly connected [`Destination`],
708+
/// and tries to create compact blinded path for that purpose.
709+
///
710+
/// # Privacy
711+
///
712+
/// Creating [`BlindedMessagePath`]s may affect privacy since, if a suitable path cannot be found,
713+
/// it will create a one-hop path using the recipient as the introduction node if it is a announced
714+
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
715+
/// message, and thus an `Err` is returned.
716+
///
717+
/// # On Compact Paths
718+
///
719+
/// Like any other router, [`CompactMessageRouter`] operates on a best-effort basis.
720+
/// While it attempts to construct compact blinded paths, it may fall back to a full-length
721+
/// blinded path if the necessary information—such as `short_channel_id`s in
722+
/// [`MessageForwardNode`]—is unavailable.
723+
pub struct CompactMessageRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref>
724+
where
725+
L::Target: Logger,
726+
ES::Target: EntropySource,
727+
{
728+
network_graph: G,
729+
entropy_source: ES,
730+
}
731+
732+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> CompactMessageRouter<G, L, ES>
733+
where
734+
L::Target: Logger,
735+
ES::Target: EntropySource,
736+
{
737+
/// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
738+
pub fn new(network_graph: G, entropy_source: ES) -> Self {
739+
Self { network_graph, entropy_source }
740+
}
741+
}
742+
743+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter
744+
for CompactMessageRouter<G, L, ES>
745+
where
746+
L::Target: Logger,
747+
ES::Target: EntropySource,
748+
{
749+
fn find_path(
750+
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
751+
) -> Result<OnionMessagePath, ()> {
752+
DefaultMessageRouter::<G, L, ES>::find_path(&self.network_graph, sender, peers, destination)
753+
}
754+
755+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
756+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
757+
secp_ctx: &Secp256k1<T>,
758+
) -> Result<Vec<BlindedMessagePath>, ()> {
759+
DefaultMessageRouter::create_blinded_paths_from_iter(
760+
&self.network_graph,
761+
recipient,
762+
context,
763+
peers.into_iter(),
764+
&self.entropy_source,
765+
secp_ctx,
766+
true,
767+
)
768+
}
769+
770+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
771+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
772+
secp_ctx: &Secp256k1<T>,
773+
) -> Result<Vec<BlindedMessagePath>, ()> {
774+
DefaultMessageRouter::create_blinded_paths_from_iter(
775+
&self.network_graph,
776+
recipient,
777+
context,
778+
peers.into_iter(),
779+
&self.entropy_source,
780+
secp_ctx,
781+
true,
782+
)
783+
}
784+
}
785+
786+
/// A special [`MessageRouter`] implementation that performs no routing.
787+
///
788+
/// # Note
789+
/// [`NullMessageRouter`] **must not** be used with [`ChannelManager`] as a parameter.
790+
///
791+
/// # Reason
792+
/// [`ChannelManager`] requires a functioning [`MessageRouter`] to create blinded paths,
793+
/// which are necessary for constructing reply paths in onion message communication.
794+
///
795+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
796+
pub struct NullMessageRouter {}
797+
798+
impl MessageRouter for NullMessageRouter {
799+
fn find_path(
800+
&self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
801+
) -> Result<OnionMessagePath, ()> {
802+
unreachable!()
803+
}
804+
805+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
806+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
807+
_secp_ctx: &Secp256k1<T>,
808+
) -> Result<Vec<BlindedMessagePath>, ()> {
809+
Ok(vec![])
810+
}
811+
812+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
813+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
814+
_secp_ctx: &Secp256k1<T>,
815+
) -> Result<Vec<BlindedMessagePath>, ()> {
816+
Ok(vec![])
817+
}
818+
}
819+
707820
/// A path for sending an [`OnionMessage`].
708821
#[derive(Clone)]
709822
pub struct OnionMessagePath {

0 commit comments

Comments
 (0)