]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Rename OnionMessageContents
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 19 Sep 2023 15:59:53 +0000 (10:59 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 18 Oct 2023 22:18:03 +0000 (17:18 -0500)
In preparation for needing the name OnionMessageContents for a trait to
bound methods, rename it to ParsedOnionMessageContents. In the next
commit, it's use will be limited to reading only, and the new trait will
be a bound on method parameters instead.

lightning/src/onion_message/functional_tests.rs
lightning/src/onion_message/messenger.rs
lightning/src/onion_message/mod.rs
lightning/src/onion_message/packet.rs

index 33aebb0fd9fc8fbdf08b01ae546dbbdc35a9f427..319417e80655efec98008f86fecedfaaee42c396 100644 (file)
@@ -15,7 +15,7 @@ use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
 use crate::sign::{NodeSigner, Recipient};
 use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
 use crate::util::test_utils;
-use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
+use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
 
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
@@ -196,7 +196,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
 #[test]
 fn one_hop() {
        let nodes = create_nodes(2);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+       let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
 
        let path = OnionMessagePath {
                intermediate_nodes: vec![],
@@ -210,7 +210,7 @@ fn one_hop() {
 #[test]
 fn two_unblinded_hops() {
        let nodes = create_nodes(3);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+       let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
 
        let path = OnionMessagePath {
                intermediate_nodes: vec![nodes[1].get_node_pk()],
@@ -224,7 +224,7 @@ fn two_unblinded_hops() {
 #[test]
 fn two_unblinded_two_blinded() {
        let nodes = create_nodes(5);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+       let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
 
        let secp_ctx = Secp256k1::new();
        let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -241,7 +241,7 @@ fn two_unblinded_two_blinded() {
 #[test]
 fn three_blinded_hops() {
        let nodes = create_nodes(4);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+       let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
 
        let secp_ctx = Secp256k1::new();
        let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -259,7 +259,7 @@ fn three_blinded_hops() {
 fn too_big_packet_error() {
        // Make sure we error as expected if a packet is too big to send.
        let nodes = create_nodes(2);
-       let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+       let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
 
        let hop_node_id = nodes[1].get_node_pk();
        let hops = vec![hop_node_id; 400];
@@ -285,7 +285,7 @@ fn we_are_intro_node() {
                destination: Destination::BlindedPath(blinded_path),
        };
 
-       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+       nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
        nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 
@@ -295,7 +295,7 @@ fn we_are_intro_node() {
                intermediate_nodes: vec![],
                destination: Destination::BlindedPath(blinded_path),
        };
-       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+       nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
        nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
        nodes.remove(2);
        pass_along_path(&nodes);
@@ -315,7 +315,7 @@ fn invalid_blinded_path_error() {
                intermediate_nodes: vec![],
                destination: Destination::BlindedPath(blinded_path),
        };
-       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
+       let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
        assert_eq!(err, SendError::TooFewBlindedHops);
 
        // 1 hop
@@ -326,7 +326,7 @@ fn invalid_blinded_path_error() {
                intermediate_nodes: vec![],
                destination: Destination::BlindedPath(blinded_path),
        };
-       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
+       let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
        assert_eq!(err, SendError::TooFewBlindedHops);
 }
 
@@ -342,7 +342,7 @@ fn reply_path() {
                destination: Destination::Node(nodes[3].get_node_pk()),
        };
        let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
-       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
+       nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
        nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
        pass_along_path(&nodes);
        // Make sure the last node successfully decoded the reply path.
@@ -358,7 +358,7 @@ fn reply_path() {
        };
        let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
 
-       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
+       nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
        nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
        pass_along_path(&nodes);
 
@@ -384,7 +384,7 @@ fn invalid_custom_message_type() {
                fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
        }
 
-       let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
+       let test_msg = ParsedOnionMessageContents::Custom(InvalidCustomMessage {});
        let path = OnionMessagePath {
                intermediate_nodes: vec![],
                destination: Destination::Node(nodes[1].get_node_pk()),
@@ -402,9 +402,9 @@ fn peer_buffer_full() {
                destination: Destination::Node(nodes[1].get_node_pk()),
        };
        for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
-               nodes[0].messenger.send_onion_message(path.clone(), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+               nodes[0].messenger.send_onion_message(path.clone(), ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
        }
-       let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
+       let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
        assert_eq!(err, SendError::BufferFull);
 }
 
@@ -425,7 +425,7 @@ fn many_hops() {
                intermediate_nodes,
                destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
        };
-       nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+       nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
        nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
        pass_along_path(&nodes);
 }
index 626e2b05840343394afa87816d4e9434a6b653ff..074a1790f028975ad3ce383614bc358e49025106 100644 (file)
@@ -23,7 +23,7 @@ use crate::ln::features::{InitFeatures, NodeFeatures};
 use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler};
 use crate::ln::onion_utils;
 use crate::ln::peer_handler::IgnoringMessageHandler;
-pub use super::packet::{CustomOnionMessageContents, OnionMessageContents};
+pub use super::packet::{CustomOnionMessageContents, ParsedOnionMessageContents};
 use super::offers::OffersMessageHandler;
 use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, SMALL_PACKET_HOP_DATA_LEN};
 use crate::util::logger::Logger;
@@ -60,7 +60,7 @@ use crate::prelude::*;
 /// # use lightning::blinded_path::BlindedPath;
 /// # use lightning::sign::KeysManager;
 /// # use lightning::ln::peer_handler::IgnoringMessageHandler;
-/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger};
+/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, MessageRouter, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger};
 /// # use lightning::util::logger::{Logger, Record};
 /// # use lightning::util::ser::{Writeable, Writer};
 /// # use lightning::io;
@@ -114,7 +114,7 @@ use crate::prelude::*;
 /// };
 /// let reply_path = None;
 /// # let your_custom_message = YourCustomMessage {};
-/// let message = OnionMessageContents::Custom(your_custom_message);
+/// let message = ParsedOnionMessageContents::Custom(your_custom_message);
 /// onion_messenger.send_onion_message(path, message, reply_path);
 ///
 /// // Create a blinded path to yourself, for someone to send an onion message to.
@@ -129,7 +129,7 @@ use crate::prelude::*;
 /// };
 /// let reply_path = None;
 /// # let your_custom_message = YourCustomMessage {};
-/// let message = OnionMessageContents::Custom(your_custom_message);
+/// let message = ParsedOnionMessageContents::Custom(your_custom_message);
 /// onion_messenger.send_onion_message(path, message, reply_path);
 /// ```
 ///
@@ -260,7 +260,7 @@ pub enum PeeledOnion<CM: CustomOnionMessageContents> {
        /// Forwarded onion, with the next node id and a new onion
        Forward(PublicKey, OnionMessage),
        /// Received onion message, with decrypted contents, path_id, and reply path
-       Receive(OnionMessageContents<CM>, Option<[u8; 32]>, Option<BlindedPath>)
+       Receive(ParsedOnionMessageContents<CM>, Option<[u8; 32]>, Option<BlindedPath>)
 }
 
 /// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
@@ -269,7 +269,8 @@ pub enum PeeledOnion<CM: CustomOnionMessageContents> {
 /// Returns both the node id of the peer to send the message to and the message itself.
 pub fn create_onion_message<ES: Deref, NS: Deref, T: CustomOnionMessageContents>(
        entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1<secp256k1::All>,
-       path: OnionMessagePath, contents: OnionMessageContents<T>, reply_path: Option<BlindedPath>,
+       path: OnionMessagePath, contents: ParsedOnionMessageContents<T>,
+       reply_path: Option<BlindedPath>,
 ) -> Result<(PublicKey, OnionMessage), SendError>
 where
        ES::Target: EntropySource,
@@ -452,7 +453,7 @@ where
        ///
        /// See [`OnionMessenger`] for example usage.
        pub fn send_onion_message<T: CustomOnionMessageContents>(
-               &self, path: OnionMessagePath, contents: OnionMessageContents<T>,
+               &self, path: OnionMessagePath, contents: ParsedOnionMessageContents<T>,
                reply_path: Option<BlindedPath>
        ) -> Result<(), SendError> {
                let (first_node_id, onion_msg) = create_onion_message(
@@ -471,7 +472,7 @@ where
        }
 
        fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
-               &self, contents: OnionMessageContents<T>, destination: Destination,
+               &self, contents: ParsedOnionMessageContents<T>, destination: Destination,
                log_suffix: fmt::Arguments
        ) {
                let sender = match self.node_signer.get_node_id(Recipient::Node) {
@@ -557,13 +558,13 @@ where
                                        "Received an onion message with path_id {:02x?} and {} reply_path",
                                                path_id, if reply_path.is_some() { "a" } else { "no" });
                                let response = match message {
-                                       OnionMessageContents::Offers(msg) => {
+                                       ParsedOnionMessageContents::Offers(msg) => {
                                                self.offers_handler.handle_message(msg)
-                                                       .map(|msg| OnionMessageContents::Offers(msg))
+                                                       .map(|msg| ParsedOnionMessageContents::Offers(msg))
                                        },
-                                       OnionMessageContents::Custom(msg) => {
+                                       ParsedOnionMessageContents::Custom(msg) => {
                                                self.custom_handler.handle_custom_message(msg)
-                                                       .map(|msg| OnionMessageContents::Custom(msg))
+                                                       .map(|msg| ParsedOnionMessageContents::Custom(msg))
                                        },
                                };
                                if let Some(response) = response {
@@ -684,7 +685,7 @@ pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger<
 /// `unblinded_path` to the given `destination`.
 fn packet_payloads_and_keys<T: CustomOnionMessageContents, S: secp256k1::Signing + secp256k1::Verification>(
        secp_ctx: &Secp256k1<S>, unblinded_path: &[PublicKey], destination: Destination,
-       message: OnionMessageContents<T>, mut reply_path: Option<BlindedPath>, session_priv: &SecretKey
+       message: ParsedOnionMessageContents<T>, mut reply_path: Option<BlindedPath>, session_priv: &SecretKey
 ) -> Result<(Vec<(Payload<T>, [u8; 32])>, Vec<onion_utils::OnionKeys>), secp256k1::Error> {
        let num_hops = unblinded_path.len() + destination.num_hops();
        let mut payloads = Vec::with_capacity(num_hops);
index fb2943425dd9b448124459b5329cc7444b3245e1..59c4672126c0eb051da5d9e1a104fd8dc2a38677 100644 (file)
@@ -27,7 +27,7 @@ mod packet;
 mod functional_tests;
 
 // Re-export structs so they can be imported with just the `onion_message::` module prefix.
-pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger, PeeledOnion, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
+pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger, PeeledOnion, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
 pub use self::offers::{OffersMessage, OffersMessageHandler};
 pub use self::packet::Packet;
 pub(crate) use self::packet::ControlTlvs;
index d19bd30edd212be4b19286d5ef5f0bf0f1870a8f..c5411831ab72b09bc6dd710e25659f8c195407c9 100644 (file)
@@ -110,38 +110,37 @@ pub(super) enum Payload<T: CustomOnionMessageContents> {
        Receive {
                control_tlvs: ReceiveControlTlvs,
                reply_path: Option<BlindedPath>,
-               message: OnionMessageContents<T>,
+               message: ParsedOnionMessageContents<T>,
        }
 }
 
+/// The contents of an onion message as read from the wire.
 #[derive(Debug)]
-/// The contents of an onion message. In the context of offers, this would be the invoice, invoice
-/// request, or invoice error.
-pub enum OnionMessageContents<T: CustomOnionMessageContents> {
+pub enum ParsedOnionMessageContents<T: CustomOnionMessageContents> {
        /// A message related to BOLT 12 Offers.
        Offers(OffersMessage),
        /// A custom onion message specified by the user.
        Custom(T),
 }
 
-impl<T: CustomOnionMessageContents> OnionMessageContents<T> {
+impl<T: CustomOnionMessageContents> ParsedOnionMessageContents<T> {
        /// Returns the type that was used to decode the message payload.
        ///
        /// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
        pub fn tlv_type(&self) -> u64 {
                match self {
-                       &OnionMessageContents::Offers(ref msg) => msg.tlv_type(),
-                       &OnionMessageContents::Custom(ref msg) => msg.tlv_type(),
+                       &ParsedOnionMessageContents::Offers(ref msg) => msg.tlv_type(),
+                       &ParsedOnionMessageContents::Custom(ref msg) => msg.tlv_type(),
                }
        }
 }
 
 /// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
-impl<T: CustomOnionMessageContents> Writeable for OnionMessageContents<T> {
+impl<T: CustomOnionMessageContents> Writeable for ParsedOnionMessageContents<T> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                match self {
-                       OnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
-                       OnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
+                       ParsedOnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
+                       ParsedOnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
                }
        }
 }
@@ -236,12 +235,12 @@ ReadableArgs<(SharedSecret, &H, &L)> for Payload<<H as CustomOnionMessageHandler
                        match msg_type {
                                tlv_type if OffersMessage::is_known_type(tlv_type) => {
                                        let msg = OffersMessage::read(msg_reader, (tlv_type, logger))?;
-                                       message = Some(OnionMessageContents::Offers(msg));
+                                       message = Some(ParsedOnionMessageContents::Offers(msg));
                                        Ok(true)
                                },
                                _ => match handler.read_custom_message(msg_type, msg_reader)? {
                                        Some(msg) => {
-                                               message = Some(OnionMessageContents::Custom(msg));
+                                               message = Some(ParsedOnionMessageContents::Custom(msg));
                                                Ok(true)
                                        },
                                        None => Ok(false),