Merge pull request #2101 from TheBlueMatt/2023-03-one-less-sig
[rust-lightning] / lightning / src / ln / wire.rs
index e7db446a86f17f036ceeccba9aca52c4aa7ea652..532eb00b871034ebbc4913b7f0b3ffc75035f145 100644 (file)
@@ -9,12 +9,12 @@
 
 //! 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
+//!
+//! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
 
-use io;
-use ln::msgs;
-use util::ser::{Readable, Writeable, Writer};
+use crate::io;
+use crate::ln::msgs;
+use crate::util::ser::{Readable, Writeable, Writer};
 
 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
 /// decoders.
@@ -28,11 +28,26 @@ pub trait CustomMessageReader {
        fn read<R: io::Read>(&self, message_type: u16, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
 }
 
-/// A Lightning message returned by [`read()`] when decoding bytes received over the wire. Each
+// TestEq is a dummy trait which requires PartialEq when built in testing, and otherwise is
+// blanket-implemented for all types.
+
+#[cfg(test)]
+pub trait TestEq : PartialEq {}
+#[cfg(test)]
+impl<T: PartialEq> TestEq for T {}
+
+#[cfg(not(test))]
+pub(crate) trait TestEq {}
+#[cfg(not(test))]
+impl<T> TestEq for T {}
+
+
+/// 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(crate) enum Message<T> where T: core::fmt::Debug + Type {
+#[cfg_attr(test, derive(PartialEq))]
+pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
        Init(msgs::Init),
        Error(msgs::ErrorMessage),
        Warning(msgs::WarningMessage),
@@ -42,9 +57,10 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
        AcceptChannel(msgs::AcceptChannel),
        FundingCreated(msgs::FundingCreated),
        FundingSigned(msgs::FundingSigned),
-       FundingLocked(msgs::FundingLocked),
+       ChannelReady(msgs::ChannelReady),
        Shutdown(msgs::Shutdown),
        ClosingSigned(msgs::ClosingSigned),
+       OnionMessage(msgs::OnionMessage),
        UpdateAddHTLC(msgs::UpdateAddHTLC),
        UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
        UpdateFailHTLC(msgs::UpdateFailHTLC),
@@ -65,11 +81,11 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
        /// A message that could not be decoded because its type is unknown.
        Unknown(u16),
        /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
-       /// [`::ln::peer_handler::CustomMessageHandler`].
+       /// [`crate::ln::peer_handler::CustomMessageHandler`].
        Custom(T),
 }
 
-impl<T> Message<T> where T: core::fmt::Debug + Type {
+impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
        /// Returns the type that was used to decode the message payload.
        pub fn type_id(&self) -> u16 {
                match self {
@@ -82,9 +98,10 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
                        &Message::AcceptChannel(ref msg) => msg.type_id(),
                        &Message::FundingCreated(ref msg) => msg.type_id(),
                        &Message::FundingSigned(ref msg) => msg.type_id(),
-                       &Message::FundingLocked(ref msg) => msg.type_id(),
+                       &Message::ChannelReady(ref msg) => msg.type_id(),
                        &Message::Shutdown(ref msg) => msg.type_id(),
                        &Message::ClosingSigned(ref msg) => msg.type_id(),
+                       &Message::OnionMessage(ref msg) => msg.type_id(),
                        &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
                        &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
                        &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
@@ -161,8 +178,8 @@ fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u1
                msgs::FundingSigned::TYPE => {
                        Ok(Message::FundingSigned(Readable::read(buffer)?))
                },
-               msgs::FundingLocked::TYPE => {
-                       Ok(Message::FundingLocked(Readable::read(buffer)?))
+               msgs::ChannelReady::TYPE => {
+                       Ok(Message::ChannelReady(Readable::read(buffer)?))
                },
                msgs::Shutdown::TYPE => {
                        Ok(Message::Shutdown(Readable::read(buffer)?))
@@ -170,6 +187,9 @@ fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u1
                msgs::ClosingSigned::TYPE => {
                        Ok(Message::ClosingSigned(Readable::read(buffer)?))
                },
+               msgs::OnionMessage::TYPE => {
+                       Ok(Message::OnionMessage(Readable::read(buffer)?))
+               },
                msgs::UpdateAddHTLC::TYPE => {
                        Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
                },
@@ -252,6 +272,7 @@ mod encode {
 
 pub(crate) use self::encode::Encode;
 
+#[cfg(not(test))]
 /// Defines a type identifier for sending messages over the wire.
 ///
 /// Messages implementing this trait specify a type and must be [`Writeable`].
@@ -260,10 +281,24 @@ pub trait Type: core::fmt::Debug + Writeable {
        fn type_id(&self) -> u16;
 }
 
+#[cfg(test)]
+pub trait Type: core::fmt::Debug + Writeable + PartialEq {
+       fn type_id(&self) -> u16;
+}
+
+#[cfg(any(feature = "_test_utils", fuzzing, test))]
+impl Type for () {
+       fn type_id(&self) -> u16 { unreachable!(); }
+}
+
+#[cfg(test)]
+impl<T: core::fmt::Debug + Writeable + PartialEq> Type for T where T: Encode {
+       fn type_id(&self) -> u16 { T::TYPE }
+}
+
+#[cfg(not(test))]
 impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
-       fn type_id(&self) -> u16 {
-               T::TYPE
-       }
+       fn type_id(&self) -> u16 { T::TYPE }
 }
 
 impl Encode for msgs::Init {
@@ -302,7 +337,7 @@ impl Encode for msgs::FundingSigned {
        const TYPE: u16 = 35;
 }
 
-impl Encode for msgs::FundingLocked {
+impl Encode for msgs::ChannelReady {
        const TYPE: u16 = 36;
 }
 
@@ -314,6 +349,10 @@ impl Encode for msgs::ClosingSigned {
        const TYPE: u16 = 39;
 }
 
+impl Encode for msgs::OnionMessage {
+       const TYPE: u16 = 513;
+}
+
 impl Encode for msgs::UpdateAddHTLC {
        const TYPE: u16 = 128;
 }
@@ -385,9 +424,9 @@ impl Encode for msgs::GossipTimestampFilter {
 #[cfg(test)]
 mod tests {
        use super::*;
-       use prelude::*;
+       use crate::prelude::*;
        use core::convert::TryInto;
-       use ::ln::peer_handler::IgnoringMessageHandler;
+       use crate::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];
@@ -471,10 +510,6 @@ mod tests {
                }
        }
 
-       impl Type for () {
-               fn type_id(&self) -> u16 { unreachable!(); }
-       }
-
        #[test]
        fn is_even_message_type() {
                let message = Message::<()>::Unknown(42);