X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fwire.rs;h=e7db446a86f17f036ceeccba9aca52c4aa7ea652;hb=e49f738630559ce2eaa97689f1b4d37546fc7020;hp=4caf3543b0f9f02e5ec3b650c8c9a82fa9cc861c;hpb=b3be420cfbf5eccfeebdd813a4c64a9e2e563711;p=rust-lightning diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 4caf3543..e7db446a 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -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 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 Message 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 Message 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( - buffer: &mut R, - custom_reader: H, -) -> Result, msgs::DecodeError> -where +pub(crate) fn read(buffer: &mut R, custom_reader: H) +-> Result, (msgs::DecodeError, Option)> where + T: core::fmt::Debug + Type + Writeable, + H::Target: CustomMessageReader, +{ + let message_type = ::read(buffer).map_err(|e| (e, None))?; + do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type))) +} + +fn do_read(buffer: &mut R, message_type: u16, 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 => { 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 Type for T where T: Encode { +impl 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());