From 2e5e40e0b21fbac862478dce4d81cf037f37060d Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Thu, 5 Aug 2021 14:51:17 +0900 Subject: [PATCH 1/2] Add custom message handler to peer manager --- fuzz/src/full_stack.rs | 6 +- lightning-background-processor/src/lib.rs | 11 +- lightning-net-tokio/src/lib.rs | 25 ++-- lightning/src/ln/mod.rs | 2 +- lightning/src/ln/peer_handler.rs | 135 ++++++++++++----- lightning/src/ln/wire.rs | 169 +++++++++++++++++----- 6 files changed, 256 insertions(+), 92 deletions(-) diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 4f474bd3086..510966f0d4a 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -34,7 +34,7 @@ use lightning::chain::transaction::OutPoint; use lightning::chain::keysinterface::{InMemorySigner, KeysInterface}; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::channelmanager::{ChainParameters, ChannelManager}; -use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; +use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler}; use lightning::ln::msgs::DecodeError; use lightning::ln::script::ShutdownScript; use lightning::routing::router::get_route; @@ -160,7 +160,7 @@ type ChannelMan = ChannelManager< EnforcingSigner, Arc, Arc, Arc, Arc, Arc>>, Arc, Arc, Arc, Arc>; -type PeerMan<'a> = PeerManager, Arc, Arc, Arc>>, Arc>; +type PeerMan<'a> = PeerManager, Arc, Arc, Arc>>, Arc, IgnoringMessageHandler>; struct MoneyLossDetector<'a> { manager: Arc, @@ -377,7 +377,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { chan_handler: channelmanager.clone(), route_handler: net_graph_msg_handler.clone(), - }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger))); + }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger), IgnoringMessageHandler{})); let mut should_forward = false; let mut payments_received: Vec = Vec::new(); diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 4fdf2eeff03..7284355c905 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -16,6 +16,7 @@ use lightning::chain::keysinterface::{Sign, KeysInterface}; use lightning::ln::channelmanager::ChannelManager; use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler}; use lightning::ln::peer_handler::{PeerManager, SocketDescriptor}; +use lightning::ln::peer_handler::CustomMessageHandler; use lightning::util::events::{EventHandler, EventsProvider}; use lightning::util::logger::Logger; use std::sync::Arc; @@ -123,7 +124,8 @@ impl BackgroundProcessor { CMP: 'static + Send + ChannelManagerPersister, M: 'static + Deref> + Send + Sync, CM: 'static + Deref> + Send + Sync, - PM: 'static + Deref> + Send + Sync, + UMH: 'static + Deref + Send + Sync, + PM: 'static + Deref> + Send + Sync, > (persister: CMP, event_handler: EH, chain_monitor: M, channel_manager: CM, peer_manager: PM, logger: L) -> Self where @@ -136,6 +138,7 @@ impl BackgroundProcessor { P::Target: 'static + channelmonitor::Persist, CMH::Target: 'static + ChannelMessageHandler, RMH::Target: 'static + RoutingMessageHandler, + UMH::Target: 'static + CustomMessageHandler, { let stop_thread = Arc::new(AtomicBool::new(false)); let stop_thread_clone = stop_thread.clone(); @@ -247,7 +250,7 @@ mod tests { use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager}; use lightning::ln::features::InitFeatures; use lightning::ln::msgs::{ChannelMessageHandler, Init}; - use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor}; + use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler}; use lightning::util::config::UserConfig; use lightning::util::events::{Event, MessageSendEventsProvider, MessageSendEvent}; use lightning::util::ser::Writeable; @@ -275,7 +278,7 @@ mod tests { struct Node { node: Arc>, - peer_manager: Arc, Arc, Arc>>, + peer_manager: Arc, Arc, Arc, IgnoringMessageHandler>>, chain_monitor: Arc, persister: Arc, tx_broadcaster: Arc, @@ -316,7 +319,7 @@ mod tests { let params = ChainParameters { network, best_block }; let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), keys_manager.clone(), UserConfig::default(), params)); let msg_handler = MessageHandler { chan_handler: Arc::new(test_utils::TestChannelMessageHandler::new()), route_handler: Arc::new(test_utils::TestRoutingMessageHandler::new() )}; - let peer_manager = Arc::new(PeerManager::new(msg_handler, keys_manager.get_node_secret(), &seed, logger.clone())); + let peer_manager = Arc::new(PeerManager::new(msg_handler, keys_manager.get_node_secret(), &seed, logger.clone(), IgnoringMessageHandler{})); let node = Node { node: manager, peer_manager, chain_monitor, persister, tx_broadcaster, logger, best_block }; nodes.push(node); } diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index d2ee100281e..25c161d2aeb 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -80,6 +80,7 @@ use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}; use lightning::ln::peer_handler; use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait; +use lightning::ln::peer_handler::CustomMessageHandler; use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler}; use lightning::util::logger::Logger; @@ -119,10 +120,11 @@ struct Connection { id: u64, } impl Connection { - async fn schedule_read(peer_manager: Arc, Arc, Arc>>, us: Arc>, mut reader: io::ReadHalf, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where + async fn schedule_read(peer_manager: Arc, Arc, Arc, Arc>>, us: Arc>, mut reader: io::ReadHalf, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where CMH: ChannelMessageHandler + 'static, RMH: RoutingMessageHandler + 'static, - L: Logger + 'static + ?Sized { + L: Logger + 'static + ?Sized, + UMH: CustomMessageHandler + 'static { // 8KB is nice and big but also should never cause any issues with stack overflowing. let mut buf = [0; 8192]; @@ -215,10 +217,11 @@ impl Connection { /// The returned future will complete when the peer is disconnected and associated handling /// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do /// not need to poll the provided future in order to make progress. -pub fn setup_inbound(peer_manager: Arc, Arc, Arc>>, stream: StdTcpStream) -> impl std::future::Future where +pub fn setup_inbound(peer_manager: Arc, Arc, Arc, Arc>>, stream: StdTcpStream) -> impl std::future::Future where CMH: ChannelMessageHandler + 'static + Send + Sync, RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync { + L: Logger + 'static + ?Sized + Send + Sync, + UMH: CustomMessageHandler + 'static + Send + Sync { let (reader, write_receiver, read_receiver, us) = Connection::new(stream); #[cfg(debug_assertions)] let last_us = Arc::clone(&us); @@ -255,10 +258,11 @@ pub fn setup_inbound(peer_manager: Arc(peer_manager: Arc, Arc, Arc>>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future where +pub fn setup_outbound(peer_manager: Arc, Arc, Arc, Arc>>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future where CMH: ChannelMessageHandler + 'static + Send + Sync, RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync { + L: Logger + 'static + ?Sized + Send + Sync, + UMH: CustomMessageHandler + 'static + Send + Sync { let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream); #[cfg(debug_assertions)] let last_us = Arc::clone(&us); @@ -325,10 +329,11 @@ pub fn setup_outbound(peer_manager: Arc(peer_manager: Arc, Arc, Arc>>, their_node_id: PublicKey, addr: SocketAddr) -> Option> where +pub async fn connect_outbound(peer_manager: Arc, Arc, Arc, Arc>>, their_node_id: PublicKey, addr: SocketAddr) -> Option> where CMH: ChannelMessageHandler + 'static + Send + Sync, RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync { + L: Logger + 'static + ?Sized + Send + Sync, + UMH: CustomMessageHandler + 'static + Send + Sync { if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), async { TcpStream::connect(&addr).await.map(|s| s.into_std().unwrap()) }).await { Some(setup_outbound(peer_manager, their_node_id, stream)) } else { None } @@ -556,7 +561,7 @@ mod tests { let a_manager = Arc::new(PeerManager::new(MessageHandler { chan_handler: Arc::clone(&a_handler), route_handler: Arc::clone(&a_handler), - }, a_key.clone(), &[1; 32], Arc::new(TestLogger()))); + }, a_key.clone(), &[1; 32], Arc::new(TestLogger()), Arc::new(lightning::ln::peer_handler::IgnoringMessageHandler{}))); let (b_connected_sender, mut b_connected) = mpsc::channel(1); let (b_disconnected_sender, mut b_disconnected) = mpsc::channel(1); @@ -570,7 +575,7 @@ mod tests { let b_manager = Arc::new(PeerManager::new(MessageHandler { chan_handler: Arc::clone(&b_handler), route_handler: Arc::clone(&b_handler), - }, b_key.clone(), &[2; 32], Arc::new(TestLogger()))); + }, b_key.clone(), &[2; 32], Arc::new(TestLogger()), Arc::new(lightning::ln::peer_handler::IgnoringMessageHandler{}))); // We bind on localhost, hoping the environment is properly configured with a local // address. This may not always be the case in containers and the like, so if this test is diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index d265888e465..b5e433270a5 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -40,7 +40,7 @@ pub mod channel; mod channel; mod onion_utils; -mod wire; +pub mod wire; // Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 234b509564d..1d164e6e684 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -21,10 +21,10 @@ use ln::features::InitFeatures; use ln::msgs; use ln::msgs::{ChannelMessageHandler, LightningError, RoutingMessageHandler}; use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; -use util::ser::{VecWriter, Writeable}; +use util::ser::{VecWriter, Writeable, Writer}; use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; use ln::wire; -use ln::wire::Encode; +use ln::wire::MessageType; use util::byte_utils; use util::events::{MessageSendEvent, MessageSendEventsProvider}; use util::logger::Logger; @@ -44,6 +44,19 @@ use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::sha256::HashEngine as Sha256Engine; use bitcoin::hashes::{HashEngine, Hash}; +/// Handler for BOLT1-compliant messages. +pub trait CustomMessageHandler: wire::CustomMessageReader { + /// Called with the message type that was received and the buffer to be read. + /// Can return a `MessageHandlingError` if the message could not be handled. + fn handle_custom_message(&self, msg: Self::CustomMessage) -> Result<(), LightningError>; + + /// Gets the list of pending messages which were generated by the custom message + /// handler, clearing the list in the process. The first tuple element must + /// correspond to the intended recipients node ids. If no connection to one of the + /// specified node does not exist, the message is simply not sent to it. + fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)>; +} + /// A dummy struct which implements `RoutingMessageHandler` without storing any routing information /// or doing any processing. You can provide one of these as the route_handler in a MessageHandler. pub struct IgnoringMessageHandler{} @@ -69,6 +82,35 @@ impl Deref for IgnoringMessageHandler { fn deref(&self) -> &Self { self } } +impl wire::Type for () { + fn type_id(&self) -> MessageType { + // We should never call this for `DummyCustomType` + unreachable!(); + } +} + +impl Writeable for () { + fn write(&self, _: &mut W) -> Result<(), io::Error> { + unreachable!(); + } +} + +impl wire::CustomMessageReader for IgnoringMessageHandler { + type CustomMessage = (); + fn read(&self, _message_type: u16, _buffer: &mut R) -> Result, msgs::DecodeError> { + Ok(None) + } +} + +impl CustomMessageHandler for IgnoringMessageHandler { + fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Result<(), LightningError> { + // Since we always return `None` in the read the handle method should never be called. + unreachable!(); + } + + fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() } +} + /// A dummy struct which implements `ChannelMessageHandler` without having any channels. /// You can provide one of these as the route_handler in a MessageHandler. pub struct ErroringMessageHandler { @@ -314,7 +356,7 @@ fn _check_usize_is_32_or_64() { /// lifetimes). Other times you can afford a reference, which is more efficient, in which case /// SimpleRefPeerManager is the more appropriate type. Defining these type aliases prevents /// issues such as overly long function definitions. -pub type SimpleArcPeerManager = PeerManager>, Arc, Arc>>, Arc>; +pub type SimpleArcPeerManager = PeerManager>, Arc, Arc>>, Arc, Arc>; /// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference /// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't @@ -322,7 +364,7 @@ pub type SimpleArcPeerManager = PeerManager = PeerManager, &'e NetGraphMsgHandler<&'g C, &'f L>, &'f L>; +pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, SD, M, T, F, C, L> = PeerManager, &'e NetGraphMsgHandler<&'g C, &'f L>, &'f L, IgnoringMessageHandler>; /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls /// socket events into messages which it passes on to its [`MessageHandler`]. @@ -343,14 +385,16 @@ pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, SD, M, T, F, C, L> = P /// you're using lightning-net-tokio. /// /// [`read_event`]: PeerManager::read_event -pub struct PeerManager where +pub struct PeerManager where CM::Target: ChannelMessageHandler, RM::Target: RoutingMessageHandler, - L::Target: Logger { + L::Target: Logger, + CMH::Target: CustomMessageHandler { message_handler: MessageHandler, peers: Mutex>, our_node_secret: SecretKey, ephemeral_key_midstate: Sha256Engine, + custom_message_handler: CMH, // Usize needs to be at least 32 bits to avoid overflowing both low and high. If usize is 64 // bits we will never realistically count into high: @@ -385,7 +429,7 @@ macro_rules! encode_msg { }} } -impl PeerManager where +impl PeerManager where CM::Target: ChannelMessageHandler, L::Target: Logger { /// Constructs a new PeerManager with the given ChannelMessageHandler. No routing message @@ -399,11 +443,11 @@ impl PeerManager PeerManager where +impl PeerManager where RM::Target: RoutingMessageHandler, L::Target: Logger { /// Constructs a new PeerManager with the given RoutingMessageHandler. No channel message @@ -419,18 +463,19 @@ impl PeerManager PeerManager where +impl PeerManager where CM::Target: ChannelMessageHandler, RM::Target: RoutingMessageHandler, - L::Target: Logger { + L::Target: Logger, + CMH::Target: CustomMessageHandler + wire::CustomMessageReader { /// Constructs a new PeerManager with the given message handlers and node_id secret key /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be /// cryptographically secure random bytes. - pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: L) -> Self { + pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, ephemeral_random_data: &[u8; 32], logger: L, custom_message_handler: CMH) -> Self { let mut ephemeral_key_midstate = Sha256::engine(); ephemeral_key_midstate.input(ephemeral_random_data); @@ -445,6 +490,7 @@ impl PeerManager PeerManager(&self, peer: &mut Peer, message: &M) { + fn enqueue_message(&self, peer: &mut Peer, message: &M) { let mut buffer = VecWriter(Vec::new()); wire::write(message, &mut buffer).unwrap(); // crash if the write failed let encoded_message = buffer.0; @@ -806,7 +852,7 @@ impl PeerManager x, Err(e) => { @@ -869,7 +915,11 @@ impl PeerManager Result, MessageHandlingError> { + fn handle_message( + &self, + peer: &mut Peer, + message: wire::Message<<::Target as wire::CustomMessageReader>::CustomMessage> + ) -> Result::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError> { log_trace!(self.logger, "Received message {:?} from {}", message, log_pubkey!(peer.their_node_id.unwrap())); // Need an Init as first message @@ -1036,12 +1086,15 @@ impl PeerManager { log_trace!(self.logger, "Received unknown odd message of type {}, ignoring", msg_type); - } + }, + wire::Message::Custom(custom) => { + self.custom_message_handler.handle_custom_message(custom)?; + }, }; Ok(should_forward) } - fn forward_broadcast_msg(&self, peers: &mut PeerHolder, msg: &wire::Message, except_node: Option<&PublicKey>) { + fn forward_broadcast_msg(&self, peers: &mut PeerHolder, msg: &wire::Message<<::Target as wire::CustomMessageReader>::CustomMessage>, except_node: Option<&PublicKey>) { match msg { wire::Message::ChannelAnnouncement(ref msg) => { log_trace!(self.logger, "Sending message to all peers except {:?} or the announced channel's counterparties: {:?}", except_node, msg); @@ -1131,27 +1184,27 @@ impl PeerManager { - { - match peers.node_id_to_descriptor.get($node_id) { - Some(descriptor) => match peers.peers.get_mut(&descriptor) { - Some(peer) => { - if peer.their_features.is_none() { - continue; - } - peer - }, - None => panic!("Inconsistent peers set state!"), - }, - None => { - continue; + macro_rules! get_peer_for_forwarding { + ($node_id: expr) => { + { + match peers.node_id_to_descriptor.get($node_id) { + Some(descriptor) => match peers.peers.get_mut(&descriptor) { + Some(peer) => { + if peer.their_features.is_none() { + continue; + } + peer }, - } + None => panic!("Inconsistent peers set state!"), + }, + None => { + continue; + }, } } } + } + for event in events_generated.drain(..) { match event { MessageSendEvent::SendAcceptChannel { ref node_id, ref msg } => { log_debug!(self.logger, "Handling SendAcceptChannel event in peer_handler for node {} for channel {}", @@ -1322,6 +1375,10 @@ impl PeerManager PeerManager(peer_count: usize, cfgs: &'a Vec) -> Vec> { + fn create_network<'a>(peer_count: usize, cfgs: &'a Vec) -> Vec> { let mut peers = Vec::new(); for i in 0..peer_count { let node_secret = SecretKey::from_slice(&[42 + i as u8; 32]).unwrap(); let ephemeral_bytes = [i as u8; 32]; let msg_handler = MessageHandler { chan_handler: &cfgs[i].chan_handler, route_handler: &cfgs[i].routing_handler }; - let peer = PeerManager::new(msg_handler, node_secret, &ephemeral_bytes, &cfgs[i].logger); + let peer = PeerManager::new(msg_handler, node_secret, &ephemeral_bytes, &cfgs[i].logger, IgnoringMessageHandler {}); peers.push(peer); } peers } - fn establish_connection<'a>(peer_a: &PeerManager, peer_b: &PeerManager) -> (FileDescriptor, FileDescriptor) { + fn establish_connection<'a>(peer_a: &PeerManager, peer_b: &PeerManager) -> (FileDescriptor, FileDescriptor) { let secp_ctx = Secp256k1::new(); let a_id = PublicKey::from_secret_key(&secp_ctx, &peer_a.our_node_secret); let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) }; diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index e3ff3095219..05bdb5694d8 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -7,26 +7,32 @@ // You may not use this file except in accordance with one or both of these // licenses. -//! Wire encoding/decoding for Lightning messages according to [BOLT #1]. -//! -//! Messages known by this module can be read from the wire using [`read()`]. -//! The [`Message`] enum returned by [`read()`] wraps the decoded message or the message type (if -//! unknown) to use with pattern matching. -//! -//! Messages implementing the [`Encode`] trait define a message type and can be sent over the wire -//! using [`write()`]. -//! +//! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for +//! custom message through the [`CustomMessageReader`] trait. +//! //! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md use io; use ln::msgs; use util::ser::{Readable, Writeable, Writer}; +/// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers) +/// decoders. +pub trait CustomMessageReader { + /// The type of the message decoded by the implementation. + type CustomMessage: core::fmt::Debug + Type + Writeable; + /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the + /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the + /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error + /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error. + fn read(&self, message_type: u16, buffer: &mut R) -> Result, msgs::DecodeError>; +} + /// A Lightning message returned by [`read()`] when decoding bytes received over the wire. Each /// variant contains a message from [`msgs`] or otherwise the message type if unknown. #[allow(missing_docs)] #[derive(Debug)] -pub enum Message { +pub(crate) enum Message where T: core::fmt::Debug + Type { Init(msgs::Init), Error(msgs::ErrorMessage), Ping(msgs::Ping), @@ -57,13 +63,16 @@ pub enum Message { GossipTimestampFilter(msgs::GossipTimestampFilter), /// A message that could not be decoded because its type is unknown. Unknown(MessageType), + /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a + /// [`::ln::peer_handler::CustomMessageHandler`]. + Custom(T), } /// A number identifying a message to determine how it is encoded on the wire. #[derive(Clone, Copy, Debug)] pub struct MessageType(u16); -impl Message { +impl Message where T: core::fmt::Debug + Type { #[allow(dead_code)] // This method is only used in tests /// Returns the type that was used to decode the message payload. pub fn type_id(&self) -> MessageType { @@ -97,6 +106,7 @@ impl Message { &Message::ReplyChannelRange(ref msg) => msg.type_id(), &Message::GossipTimestampFilter(ref msg) => msg.type_id(), &Message::Unknown(type_id) => type_id, + &Message::Custom(ref msg) => msg.type_id(), } } } @@ -120,7 +130,14 @@ impl ::core::fmt::Display for MessageType { /// # Errors /// /// Returns an error if the message payload code not be decoded as the specified type. -pub fn read(buffer: &mut R) -> Result { +pub(crate) fn read( + buffer: &mut R, + custom_reader: H, +) -> Result, msgs::DecodeError> +where + T: core::fmt::Debug + Type + Writeable, + H::Target: CustomMessageReader, +{ let message_type = ::read(buffer)?; match message_type { msgs::Init::TYPE => { @@ -208,7 +225,11 @@ pub fn read(buffer: &mut R) -> Result { Ok(Message::GossipTimestampFilter(Readable::read(buffer)?)) }, _ => { - Ok(Message::Unknown(MessageType(message_type))) + if let Some(custom) = custom_reader.read(message_type, buffer)? { + Ok(Message::Custom(custom)) + } else { + Ok(Message::Unknown(MessageType(message_type))) + } }, } } @@ -219,22 +240,32 @@ pub fn read(buffer: &mut R) -> Result { /// # Errors /// /// Returns an I/O error if the write could not be completed. -pub fn write(message: &M, buffer: &mut W) -> Result<(), io::Error> { - M::TYPE.write(buffer)?; +pub(crate) fn write(message: &M, buffer: &mut W) -> Result<(), io::Error> { + message.type_id().0.write(buffer)?; message.write(buffer) } -/// Defines a type-identified encoding for sending messages over the wire. +mod encode { + /// Defines a constant type identifier for reading messages from the wire. + pub trait Encode { + /// The type identifying the message payload. + const TYPE: u16; + } +} + +pub(crate) use self::encode::Encode; + +/// Defines a type identifier for sending messages over the wire. /// -/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`]. -pub trait Encode { - /// The type identifying the message payload. - const TYPE: u16; +/// Messages implementing this trait specify a type and must be [`Writeable`]. +pub trait Type { + /// Returns the type identifying the message payload. + fn type_id(&self) -> MessageType; +} - /// Returns the type identifying the message payload. Convenience method for accessing - /// [`Self::TYPE`]. +impl Type for T where T: Encode { fn type_id(&self) -> MessageType { - MessageType(Self::TYPE) + MessageType(T::TYPE) } } @@ -355,6 +386,7 @@ mod tests { use super::*; use prelude::*; use core::convert::TryInto; + use ::ln::peer_handler::IgnoringMessageHandler; // Big-endian wire encoding of Pong message (type = 19, byteslen = 2). const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8]; @@ -363,35 +395,35 @@ mod tests { fn read_empty_buffer() { let buffer = []; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); } #[test] fn read_incomplete_type() { let buffer = &ENCODED_PONG[..1]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); } #[test] fn read_empty_payload() { let buffer = &ENCODED_PONG[..2]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); } #[test] fn read_invalid_message() { let buffer = &ENCODED_PONG[..4]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); } #[test] fn read_known_message() { let buffer = &ENCODED_PONG[..]; let mut reader = io::Cursor::new(buffer); - let message = read(&mut reader).unwrap(); + let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match message { Message::Pong(_) => (), _ => panic!("Expected pong message; found message type: {}", message.type_id()), @@ -402,7 +434,7 @@ mod tests { fn read_unknown_message() { let buffer = &::core::u16::MAX.to_be_bytes(); let mut reader = io::Cursor::new(buffer); - let message = read(&mut reader).unwrap(); + let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match message { Message::Unknown(MessageType(::core::u16::MAX)) => (), _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()), @@ -428,7 +460,7 @@ mod tests { assert!(write(&message, &mut buffer).is_ok()); let mut reader = io::Cursor::new(buffer); - let decoded_message = read(&mut reader).unwrap(); + let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match decoded_message { Message::Pong(msgs::Pong { byteslen: 2u16 }) => (), Message::Pong(msgs::Pong { byteslen }) => { @@ -440,13 +472,13 @@ mod tests { #[test] fn is_even_message_type() { - let message = Message::Unknown(MessageType(42)); + let message = Message::<()>::Unknown(MessageType(42)); assert!(message.type_id().is_even()); } #[test] fn is_odd_message_type() { - let message = Message::Unknown(MessageType(43)); + let message = Message::<()>::Unknown(MessageType(43)); assert!(!message.type_id().is_even()); } @@ -466,7 +498,7 @@ mod tests { fn check_init_msg(buffer: Vec, expect_unknown: bool) { let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match decoded_msg { Message::Init(msgs::Init { features }) => { assert!(features.supports_variable_length_onion()); @@ -485,7 +517,7 @@ mod tests { // Taken from lnd v0.9.0-beta. let buffer = vec![1, 1, 91, 164, 146, 213, 213, 165, 21, 227, 102, 33, 105, 179, 214, 21, 221, 175, 228, 93, 57, 177, 191, 127, 107, 229, 31, 50, 21, 81, 179, 71, 39, 18, 35, 2, 89, 224, 110, 123, 66, 39, 148, 246, 177, 85, 12, 19, 70, 226, 173, 132, 156, 26, 122, 146, 71, 213, 247, 48, 93, 190, 185, 177, 12, 172, 0, 3, 2, 162, 161, 94, 103, 195, 37, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 51, 153, 255, 97, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 172, 21, 0, 2, 38, 7]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match decoded_msg { Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => { assert!(features.supports_variable_length_onion()); @@ -502,7 +534,7 @@ mod tests { // Taken from lnd v0.9.0-beta. let buffer = vec![1, 0, 82, 238, 153, 33, 128, 87, 215, 2, 28, 241, 140, 250, 98, 255, 56, 5, 79, 240, 214, 231, 172, 35, 240, 171, 44, 9, 78, 91, 8, 193, 102, 5, 17, 178, 142, 106, 180, 183, 46, 38, 217, 212, 25, 236, 69, 47, 92, 217, 181, 221, 161, 205, 121, 201, 99, 38, 158, 216, 186, 193, 230, 86, 222, 6, 206, 67, 22, 255, 137, 212, 141, 161, 62, 134, 76, 48, 241, 54, 50, 167, 187, 247, 73, 27, 74, 1, 129, 185, 197, 153, 38, 90, 255, 138, 39, 161, 102, 172, 213, 74, 107, 88, 150, 90, 0, 49, 104, 7, 182, 184, 194, 219, 181, 172, 8, 245, 65, 226, 19, 228, 101, 145, 25, 159, 52, 31, 58, 93, 53, 59, 218, 91, 37, 84, 103, 17, 74, 133, 33, 35, 2, 203, 101, 73, 19, 94, 175, 122, 46, 224, 47, 168, 128, 128, 25, 26, 25, 214, 52, 247, 43, 241, 117, 52, 206, 94, 135, 156, 52, 164, 143, 234, 58, 185, 50, 185, 140, 198, 174, 71, 65, 18, 105, 70, 131, 172, 137, 0, 164, 51, 215, 143, 117, 119, 217, 241, 197, 177, 227, 227, 170, 199, 114, 7, 218, 12, 107, 30, 191, 236, 203, 21, 61, 242, 48, 192, 90, 233, 200, 199, 111, 162, 68, 234, 54, 219, 1, 233, 66, 5, 82, 74, 84, 211, 95, 199, 245, 202, 89, 223, 102, 124, 62, 166, 253, 253, 90, 180, 118, 21, 61, 110, 37, 5, 96, 167, 0, 0, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15, 0, 2, 65, 0, 0, 1, 0, 0, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 3, 54, 61, 144, 88, 171, 247, 136, 208, 99, 9, 135, 37, 201, 178, 253, 136, 0, 185, 235, 68, 160, 106, 110, 12, 46, 21, 125, 204, 18, 75, 234, 16, 3, 42, 171, 28, 52, 224, 11, 30, 30, 253, 156, 148, 175, 203, 121, 250, 111, 122, 195, 84, 122, 77, 183, 56, 135, 101, 88, 41, 60, 191, 99, 232, 85, 2, 36, 17, 156, 11, 8, 12, 189, 177, 68, 88, 28, 15, 207, 21, 179, 151, 56, 226, 158, 148, 3, 120, 113, 177, 243, 184, 17, 173, 37, 46, 222, 16]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match decoded_msg { Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => { assert!(!features.requires_unknown_bits()); @@ -510,4 +542,71 @@ mod tests { _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) } } + + #[derive(Eq, PartialEq, Debug)] + struct TestCustomMessage {} + + const CUSTOM_MESSAGE_TYPE : u16 = 9000; + + impl Type for TestCustomMessage { + fn type_id(&self) -> MessageType { + MessageType(CUSTOM_MESSAGE_TYPE) + } + } + + impl Writeable for TestCustomMessage { + fn write(&self, _: &mut W) -> Result<(), io::Error> { + Ok(()) + } + } + + struct TestCustomMessageReader {} + + impl CustomMessageReader for TestCustomMessageReader { + type CustomMessage = TestCustomMessage; + fn read( + &self, + message_type: u16, + _: &mut R + ) -> Result, msgs::DecodeError> { + if message_type == CUSTOM_MESSAGE_TYPE { + return Ok(Some(TestCustomMessage{})); + } + + Ok(None) + } + } + + #[test] + fn read_custom_message() { + let buffer = vec![35, 40]; + let mut reader = io::Cursor::new(buffer); + let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap(); + match decoded_msg { + Message::Custom(custom) => { + assert_eq!(custom.type_id().0, CUSTOM_MESSAGE_TYPE); + assert_eq!(custom, TestCustomMessage {}); + }, + _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()), + } + } + + #[test] + fn read_with_custom_reader_unknown_message_type() { + let buffer = vec![35, 42]; + let mut reader = io::Cursor::new(buffer); + let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap(); + match decoded_msg { + Message::Unknown(_) => {}, + _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()), + } + } + + #[test] + fn custom_reader_unknown_message_type() { + let buffer = Vec::new(); + let mut reader = io::Cursor::new(buffer); + let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap(); + assert!(res.is_none()); + } } From eb46f477f4b2459731e78c23423415cde278be09 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 18 Aug 2021 10:41:01 -0500 Subject: [PATCH 2/2] Remove wire::MessageType in favor of u16 With custom messages, wire::Type was introduced. wire::MessageType is a bit redundant, so use u16 instead and move is_even to wire::Message. --- lightning/src/ln/peer_handler.rs | 11 ++++---- lightning/src/ln/wire.rs | 47 ++++++++++++-------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 1d164e6e684..2a724c74ad9 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -24,7 +24,6 @@ use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; use util::ser::{VecWriter, Writeable, Writer}; use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; use ln::wire; -use ln::wire::MessageType; use util::byte_utils; use util::events::{MessageSendEvent, MessageSendEventsProvider}; use util::logger::Logger; @@ -83,7 +82,7 @@ impl Deref for IgnoringMessageHandler { } impl wire::Type for () { - fn type_id(&self) -> MessageType { + fn type_id(&self) -> u16 { // We should never call this for `DummyCustomType` unreachable!(); } @@ -1079,13 +1078,13 @@ impl P }, // Unknown messages: - wire::Message::Unknown(msg_type) if msg_type.is_even() => { - log_debug!(self.logger, "Received unknown even message of type {}, disconnecting peer!", msg_type); + wire::Message::Unknown(type_id) if message.is_even() => { + log_debug!(self.logger, "Received unknown even message of type {}, disconnecting peer!", type_id); // Fail the channel if message is an even, unknown type as per BOLT #1. return Err(PeerHandleError{ no_connection_possible: true }.into()); }, - wire::Message::Unknown(msg_type) => { - log_trace!(self.logger, "Received unknown odd message of type {}, ignoring", msg_type); + wire::Message::Unknown(type_id) => { + log_trace!(self.logger, "Received unknown odd message of type {}, ignoring", type_id); }, wire::Message::Custom(custom) => { self.custom_message_handler.handle_custom_message(custom)?; diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 05bdb5694d8..4caf3543b0f 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -62,20 +62,15 @@ pub(crate) enum Message where T: core::fmt::Debug + Type { ReplyChannelRange(msgs::ReplyChannelRange), GossipTimestampFilter(msgs::GossipTimestampFilter), /// A message that could not be decoded because its type is unknown. - Unknown(MessageType), + Unknown(u16), /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a /// [`::ln::peer_handler::CustomMessageHandler`]. Custom(T), } -/// A number identifying a message to determine how it is encoded on the wire. -#[derive(Clone, Copy, Debug)] -pub struct MessageType(u16); - impl Message where T: core::fmt::Debug + Type { - #[allow(dead_code)] // This method is only used in tests /// Returns the type that was used to decode the message payload. - pub fn type_id(&self) -> MessageType { + pub fn type_id(&self) -> u16 { match self { &Message::Init(ref msg) => msg.type_id(), &Message::Error(ref msg) => msg.type_id(), @@ -109,18 +104,10 @@ impl Message where T: core::fmt::Debug + Type { &Message::Custom(ref msg) => msg.type_id(), } } -} -impl MessageType { - /// Returns whether the message type is even, indicating both endpoints must support it. + /// Returns whether the message's type is even, indicating both endpoints must support it. pub fn is_even(&self) -> bool { - (self.0 & 1) == 0 - } -} - -impl ::core::fmt::Display for MessageType { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}", self.0) + (self.type_id() & 1) == 0 } } @@ -228,7 +215,7 @@ where if let Some(custom) = custom_reader.read(message_type, buffer)? { Ok(Message::Custom(custom)) } else { - Ok(Message::Unknown(MessageType(message_type))) + Ok(Message::Unknown(message_type)) } }, } @@ -241,7 +228,7 @@ where /// /// Returns an I/O error if the write could not be completed. pub(crate) fn write(message: &M, buffer: &mut W) -> Result<(), io::Error> { - message.type_id().0.write(buffer)?; + message.type_id().write(buffer)?; message.write(buffer) } @@ -260,12 +247,12 @@ pub(crate) use self::encode::Encode; /// Messages implementing this trait specify a type and must be [`Writeable`]. pub trait Type { /// Returns the type identifying the message payload. - fn type_id(&self) -> MessageType; + fn type_id(&self) -> u16; } impl Type for T where T: Encode { - fn type_id(&self) -> MessageType { - MessageType(T::TYPE) + fn type_id(&self) -> u16 { + T::TYPE } } @@ -436,7 +423,7 @@ mod tests { let mut reader = io::Cursor::new(buffer); let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match message { - Message::Unknown(MessageType(::core::u16::MAX)) => (), + Message::Unknown(::core::u16::MAX) => (), _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()), } } @@ -472,14 +459,14 @@ mod tests { #[test] fn is_even_message_type() { - let message = Message::<()>::Unknown(MessageType(42)); - assert!(message.type_id().is_even()); + let message = Message::<()>::Unknown(42); + assert!(message.is_even()); } #[test] fn is_odd_message_type() { - let message = Message::<()>::Unknown(MessageType(43)); - assert!(!message.type_id().is_even()); + let message = Message::<()>::Unknown(43); + assert!(!message.is_even()); } #[test] @@ -549,8 +536,8 @@ mod tests { const CUSTOM_MESSAGE_TYPE : u16 = 9000; impl Type for TestCustomMessage { - fn type_id(&self) -> MessageType { - MessageType(CUSTOM_MESSAGE_TYPE) + fn type_id(&self) -> u16 { + CUSTOM_MESSAGE_TYPE } } @@ -584,7 +571,7 @@ mod tests { let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap(); match decoded_msg { Message::Custom(custom) => { - assert_eq!(custom.type_id().0, CUSTOM_MESSAGE_TYPE); + assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE); assert_eq!(custom, TestCustomMessage {}); }, _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),