config: add max_funding_satoshis to enforce for inbound channels
[rust-lightning] / lightning / src / ln / wire.rs
index 4caf3543b0f9f02e5ec3b650c8c9a82fa9cc861c..e7db446a86f17f036ceeccba9aca52c4aa7ea652 100644 (file)
@@ -20,7 +20,7 @@ use util::ser::{Readable, Writeable, Writer};
 /// decoders.
 pub trait CustomMessageReader {
        /// The type of the message decoded by the implementation.
-       type CustomMessage: core::fmt::Debug + Type + Writeable;
+       type CustomMessage: Type;
        /// 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
@@ -35,6 +35,7 @@ pub trait CustomMessageReader {
 pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
        Init(msgs::Init),
        Error(msgs::ErrorMessage),
+       Warning(msgs::WarningMessage),
        Ping(msgs::Ping),
        Pong(msgs::Pong),
        OpenChannel(msgs::OpenChannel),
@@ -74,6 +75,7 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
                match self {
                        &Message::Init(ref msg) => msg.type_id(),
                        &Message::Error(ref msg) => msg.type_id(),
+                       &Message::Warning(ref msg) => msg.type_id(),
                        &Message::Ping(ref msg) => msg.type_id(),
                        &Message::Pong(ref msg) => msg.type_id(),
                        &Message::OpenChannel(ref msg) => msg.type_id(),
@@ -117,15 +119,20 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
 /// # Errors
 ///
 /// Returns an error if the message payload code not be decoded as the specified type.
-pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
-       buffer: &mut R,
-       custom_reader: H,
-) -> Result<Message<T>, msgs::DecodeError>
-where
+pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
+-> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
+       T: core::fmt::Debug + Type + Writeable,
+       H::Target: CustomMessageReader<CustomMessage = T>,
+{
+       let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
+       do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
+}
+
+fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
+-> Result<Message<T>, msgs::DecodeError> where
        T: core::fmt::Debug + Type + Writeable,
        H::Target: CustomMessageReader<CustomMessage = T>,
 {
-       let message_type = <u16 as Readable>::read(buffer)?;
        match message_type {
                msgs::Init::TYPE => {
                        Ok(Message::Init(Readable::read(buffer)?))
@@ -133,6 +140,9 @@ where
                msgs::ErrorMessage::TYPE => {
                        Ok(Message::Error(Readable::read(buffer)?))
                },
+               msgs::WarningMessage::TYPE => {
+                       Ok(Message::Warning(Readable::read(buffer)?))
+               },
                msgs::Ping::TYPE => {
                        Ok(Message::Ping(Readable::read(buffer)?))
                },
@@ -245,12 +255,12 @@ 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`].
-pub trait Type {
+pub trait Type: core::fmt::Debug + Writeable {
        /// Returns the type identifying the message payload.
        fn type_id(&self) -> u16;
 }
 
-impl<T> Type for T where T: Encode {
+impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
        fn type_id(&self) -> u16 {
                T::TYPE
        }
@@ -264,6 +274,10 @@ impl Encode for msgs::ErrorMessage {
        const TYPE: u16 = 17;
 }
 
+impl Encode for msgs::WarningMessage {
+       const TYPE: u16 = 1;
+}
+
 impl Encode for msgs::Ping {
        const TYPE: u16 = 18;
 }
@@ -457,6 +471,10 @@ mod tests {
                }
        }
 
+       impl Type for () {
+               fn type_id(&self) -> u16 { unreachable!(); }
+       }
+
        #[test]
        fn is_even_message_type() {
                let message = Message::<()>::Unknown(42);
@@ -487,7 +505,7 @@ mod tests {
                let mut reader = io::Cursor::new(buffer);
                let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
                match decoded_msg {
-                       Message::Init(msgs::Init { features }) => {
+                       Message::Init(msgs::Init { features, .. }) => {
                                assert!(features.supports_variable_length_onion());
                                assert!(features.supports_upfront_shutdown_script());
                                assert!(features.supports_gossip_queries());