X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fwire.rs;h=0ee280b50e4e72ffc4290333a960a9a09853ae93;hb=7e78fa660cec8a73286c94c1073ee588140e7a01;hp=8197ce151ef9deceb8fa17591c04d642e29385d2;hpb=3220f3b18233518688d2c689270d0adba25ed79e;p=rust-lightning diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 8197ce15..0ee280b5 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -9,28 +9,22 @@ //! 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 +//! 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`]. +//! using [`write()`]. //! //! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md -//! [`read`]: fn.read.html -//! [`write`]: fn.write.html -//! [`Encode`]: trait.Encode.html -//! [`Message`]: enum.Message.html use ln::msgs; use util::ser::{Readable, Writeable, Writer}; -/// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each -/// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown. -/// -/// [`read`]: fn.read.html -/// [`ln::msgs`]: ../msgs/index.html +/// 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 { Init(msgs::Init), Error(msgs::ErrorMessage), @@ -65,10 +59,11 @@ pub enum Message { } /// A number identifying a message to determine how it is encoded on the wire. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub struct MessageType(u16); impl Message { + #[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 { match self { @@ -112,8 +107,8 @@ impl MessageType { } } -impl ::std::fmt::Display for MessageType { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { +impl ::core::fmt::Display for MessageType { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "{}", self.0) } } @@ -230,16 +225,13 @@ pub fn write(message: &M, buffer: &mut W) -> R /// Defines a type-identified encoding for sending messages over the wire. /// -/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write`]. -/// -/// [`Writeable`]: ../../util/ser/trait.Writeable.html -/// [`write`]: fn.write.html +/// 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; /// Returns the type identifying the message payload. Convenience method for accessing - /// [`TYPE`](TYPE). + /// [`Self::TYPE`]. fn type_id(&self) -> MessageType { MessageType(Self::TYPE) } @@ -360,7 +352,8 @@ impl Encode for msgs::GossipTimestampFilter { #[cfg(test)] mod tests { use super::*; - use util::byte_utils; + use prelude::*; + use core::convert::TryInto; // Big-endian wire encoding of Pong message (type = 19, byteslen = 2). const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8]; @@ -406,12 +399,12 @@ mod tests { #[test] fn read_unknown_message() { - let buffer = &byte_utils::be16_to_array(::std::u16::MAX); + let buffer = &::core::u16::MAX.to_be_bytes(); let mut reader = ::std::io::Cursor::new(buffer); let message = read(&mut reader).unwrap(); match message { - Message::Unknown(MessageType(::std::u16::MAX)) => (), - _ => panic!("Expected message type {}; found: {}", ::std::u16::MAX, message.type_id()), + Message::Unknown(MessageType(::core::u16::MAX)) => (), + _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()), } } @@ -421,9 +414,9 @@ mod tests { let mut buffer = Vec::new(); assert!(write(&message, &mut buffer).is_ok()); - let type_length = ::std::mem::size_of::(); + let type_length = ::core::mem::size_of::(); let (type_bytes, payload_bytes) = buffer.split_at(type_length); - assert_eq!(byte_utils::slice_to_be16(type_bytes), msgs::Pong::TYPE); + assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE); assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]); }