From: Jeffrey Czyz Date: Tue, 19 Sep 2023 15:59:53 +0000 (-0500) Subject: Rename OnionMessageContents X-Git-Tag: v0.0.118~11^2~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=94573dda33c2e6bf55b2f28ffe7fbdf0a37f6edc;p=rust-lightning Rename OnionMessageContents 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. --- diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 33aebb0f..319417e8 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -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) { #[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(&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); } diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 626e2b05..074a1790 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -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 { /// 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, Option<[u8; 32]>, Option) + Receive(ParsedOnionMessageContents, Option<[u8; 32]>, Option) } /// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of @@ -269,7 +269,8 @@ pub enum PeeledOnion { /// Returns both the node id of the peer to send the message to and the message itself. pub fn create_onion_message( entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1, - path: OnionMessagePath, contents: OnionMessageContents, reply_path: Option, + path: OnionMessagePath, contents: ParsedOnionMessageContents, + reply_path: Option, ) -> Result<(PublicKey, OnionMessage), SendError> where ES::Target: EntropySource, @@ -452,7 +453,7 @@ where /// /// See [`OnionMessenger`] for example usage. pub fn send_onion_message( - &self, path: OnionMessagePath, contents: OnionMessageContents, + &self, path: OnionMessagePath, contents: ParsedOnionMessageContents, reply_path: Option ) -> Result<(), SendError> { let (first_node_id, onion_msg) = create_onion_message( @@ -471,7 +472,7 @@ where } fn find_path_and_enqueue_onion_message( - &self, contents: OnionMessageContents, destination: Destination, + &self, contents: ParsedOnionMessageContents, 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( secp_ctx: &Secp256k1, unblinded_path: &[PublicKey], destination: Destination, - message: OnionMessageContents, mut reply_path: Option, session_priv: &SecretKey + message: ParsedOnionMessageContents, mut reply_path: Option, session_priv: &SecretKey ) -> Result<(Vec<(Payload, [u8; 32])>, Vec), secp256k1::Error> { let num_hops = unblinded_path.len() + destination.num_hops(); let mut payloads = Vec::with_capacity(num_hops); diff --git a/lightning/src/onion_message/mod.rs b/lightning/src/onion_message/mod.rs index fb294342..59c46721 100644 --- a/lightning/src/onion_message/mod.rs +++ b/lightning/src/onion_message/mod.rs @@ -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; diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index d19bd30e..c5411831 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -110,38 +110,37 @@ pub(super) enum Payload { Receive { control_tlvs: ReceiveControlTlvs, reply_path: Option, - message: OnionMessageContents, + message: ParsedOnionMessageContents, } } +/// 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 { +pub enum ParsedOnionMessageContents { /// A message related to BOLT 12 Offers. Offers(OffersMessage), /// A custom onion message specified by the user. Custom(T), } -impl OnionMessageContents { +impl ParsedOnionMessageContents { /// 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 Writeable for OnionMessageContents { +impl Writeable for ParsedOnionMessageContents { fn write(&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< { 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),