X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fwire.rs;h=32447e8979e4a1e41fdcae7bb8e373c6bf17e3a4;hb=adc1b55a6fa064852d838ceb91b11e6b228d169e;hp=4caf3543b0f9f02e5ec3b650c8c9a82fa9cc861c;hpb=6bd1af4f9f3a4c3f011e812956d5b090dfe166a0;p=rust-lightning diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 4caf3543..32447e89 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -9,18 +9,18 @@ //! 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. 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 @@ -28,22 +28,39 @@ pub trait CustomMessageReader { 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 +// 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 TestEq for T {} + +#[cfg(not(test))] +pub(crate) trait TestEq {} +#[cfg(not(test))] +impl 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 where T: core::fmt::Debug + Type { +#[cfg_attr(test, derive(PartialEq))] +pub(crate) enum Message where T: core::fmt::Debug + Type + TestEq { Init(msgs::Init), Error(msgs::ErrorMessage), + Warning(msgs::WarningMessage), Ping(msgs::Ping), Pong(msgs::Pong), OpenChannel(msgs::OpenChannel), 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), @@ -64,25 +81,27 @@ pub(crate) enum Message 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 Message where T: core::fmt::Debug + Type { +impl Message 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 { &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(), &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(), @@ -117,15 +136,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 +157,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)?)) }, @@ -151,8 +178,8 @@ where 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)?)) @@ -160,6 +187,9 @@ where 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)?)) }, @@ -242,18 +272,33 @@ 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`]. -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 { - fn type_id(&self) -> u16 { - T::TYPE - } +#[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 Type for T where T: Encode { + fn type_id(&self) -> u16 { T::TYPE } +} + +#[cfg(not(test))] +impl Type for T where T: Encode { + fn type_id(&self) -> u16 { T::TYPE } } impl Encode for msgs::Init { @@ -264,6 +309,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; } @@ -288,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; } @@ -300,6 +349,54 @@ impl Encode for msgs::ClosingSigned { const TYPE: u16 = 39; } +impl Encode for msgs::OpenChannelV2 { + const TYPE: u16 = 64; +} + +impl Encode for msgs::AcceptChannelV2 { + const TYPE: u16 = 65; +} + +impl Encode for msgs::TxAddInput { + const TYPE: u16 = 66; +} + +impl Encode for msgs::TxAddOutput { + const TYPE: u16 = 67; +} + +impl Encode for msgs::TxRemoveInput { + const TYPE: u16 = 68; +} + +impl Encode for msgs::TxRemoveOutput { + const TYPE: u16 = 69; +} + +impl Encode for msgs::TxComplete { + const TYPE: u16 = 70; +} + +impl Encode for msgs::TxSignatures { + const TYPE: u16 = 71; +} + +impl Encode for msgs::TxInitRbf { + const TYPE: u16 = 72; +} + +impl Encode for msgs::TxAckRbf { + const TYPE: u16 = 73; +} + +impl Encode for msgs::TxAbort { + const TYPE: u16 = 74; +} + +impl Encode for msgs::OnionMessage { + const TYPE: u16 = 513; +} + impl Encode for msgs::UpdateAddHTLC { const TYPE: u16 = 128; } @@ -371,9 +468,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]; @@ -487,7 +584,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());