X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fwire.rs;h=e3ff3095219e7abf1efe8ff829ad1d2b7ac65bd0;hb=0dfcacd22c23f69b6526c9c6507d21427a2b7ccb;hp=dfd11fc7e571760fca83d1364f679e986dc04b60;hpb=e94635703a5a9f51a5f42849ba0ccfb6e5e98ac3;p=rust-lightning diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index dfd11fc7..e3ff3095 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -1,27 +1,31 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + //! 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 io; 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), @@ -46,15 +50,21 @@ pub enum Message { ChannelAnnouncement(msgs::ChannelAnnouncement), NodeAnnouncement(msgs::NodeAnnouncement), ChannelUpdate(msgs::ChannelUpdate), + QueryShortChannelIds(msgs::QueryShortChannelIds), + ReplyShortChannelIdsEnd(msgs::ReplyShortChannelIdsEnd), + QueryChannelRange(msgs::QueryChannelRange), + ReplyChannelRange(msgs::ReplyChannelRange), + GossipTimestampFilter(msgs::GossipTimestampFilter), /// A message that could not be decoded because its type is unknown. Unknown(MessageType), } /// 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 { @@ -81,6 +91,11 @@ impl Message { &Message::ChannelAnnouncement(ref msg) => msg.type_id(), &Message::NodeAnnouncement(ref msg) => msg.type_id(), &Message::ChannelUpdate(ref msg) => msg.type_id(), + &Message::QueryShortChannelIds(ref msg) => msg.type_id(), + &Message::ReplyShortChannelIdsEnd(ref msg) => msg.type_id(), + &Message::QueryChannelRange(ref msg) => msg.type_id(), + &Message::ReplyChannelRange(ref msg) => msg.type_id(), + &Message::GossipTimestampFilter(ref msg) => msg.type_id(), &Message::Unknown(type_id) => type_id, } } @@ -93,8 +108,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) } } @@ -105,7 +120,7 @@ impl ::std::fmt::Display for MessageType { /// # Errors /// /// Returns an error if the message payload code not be decoded as the specified type. -pub fn read(buffer: &mut R) -> Result { +pub fn read(buffer: &mut R) -> Result { let message_type = ::read(buffer)?; match message_type { msgs::Init::TYPE => { @@ -177,6 +192,21 @@ pub fn read(buffer: &mut R) -> Result { Ok(Message::ChannelUpdate(Readable::read(buffer)?)) }, + msgs::QueryShortChannelIds::TYPE => { + Ok(Message::QueryShortChannelIds(Readable::read(buffer)?)) + }, + msgs::ReplyShortChannelIdsEnd::TYPE => { + Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?)) + }, + msgs::QueryChannelRange::TYPE => { + Ok(Message::QueryChannelRange(Readable::read(buffer)?)) + }, + msgs::ReplyChannelRange::TYPE => { + Ok(Message::ReplyChannelRange(Readable::read(buffer)?)) + } + msgs::GossipTimestampFilter::TYPE => { + Ok(Message::GossipTimestampFilter(Readable::read(buffer)?)) + }, _ => { Ok(Message::Unknown(MessageType(message_type))) }, @@ -189,23 +219,20 @@ pub fn read(buffer: &mut R) -> Result(message: &M, buffer: &mut W) -> Result<(), ::std::io::Error> { +pub fn write(message: &M, buffer: &mut W) -> Result<(), io::Error> { M::TYPE.write(buffer)?; message.write(buffer) } /// 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) } @@ -303,10 +330,31 @@ impl Encode for msgs::ChannelUpdate { const TYPE: u16 = 258; } +impl Encode for msgs::QueryShortChannelIds { + const TYPE: u16 = 261; +} + +impl Encode for msgs::ReplyShortChannelIdsEnd { + const TYPE: u16 = 262; +} + +impl Encode for msgs::QueryChannelRange { + const TYPE: u16 = 263; +} + +impl Encode for msgs::ReplyChannelRange { + const TYPE: u16 = 264; +} + +impl Encode for msgs::GossipTimestampFilter { + const TYPE: u16 = 265; +} + #[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]; @@ -314,35 +362,35 @@ mod tests { #[test] fn read_empty_buffer() { let buffer = []; - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); assert!(read(&mut reader).is_err()); } #[test] fn read_incomplete_type() { let buffer = &ENCODED_PONG[..1]; - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); assert!(read(&mut reader).is_err()); } #[test] fn read_empty_payload() { let buffer = &ENCODED_PONG[..2]; - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); assert!(read(&mut reader).is_err()); } #[test] fn read_invalid_message() { let buffer = &ENCODED_PONG[..4]; - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); assert!(read(&mut reader).is_err()); } #[test] fn read_known_message() { let buffer = &ENCODED_PONG[..]; - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); let message = read(&mut reader).unwrap(); match message { Message::Pong(_) => (), @@ -352,12 +400,12 @@ mod tests { #[test] fn read_unknown_message() { - let buffer = &byte_utils::be16_to_array(::std::u16::MAX); - let mut reader = ::std::io::Cursor::new(buffer); + let buffer = &::core::u16::MAX.to_be_bytes(); + let mut reader = 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()), } } @@ -367,9 +415,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..]); } @@ -379,7 +427,7 @@ mod tests { let mut buffer = Vec::new(); assert!(write(&message, &mut buffer).is_ok()); - let mut reader = ::std::io::Cursor::new(buffer); + let mut reader = io::Cursor::new(buffer); let decoded_message = read(&mut reader).unwrap(); match decoded_message { Message::Pong(msgs::Pong { byteslen: 2u16 }) => (), @@ -401,4 +449,65 @@ mod tests { let message = Message::Unknown(MessageType(43)); assert!(!message.type_id().is_even()); } + + #[test] + fn read_lnd_init_msg() { + // Taken from lnd v0.9.0-beta. + let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161]; + check_init_msg(buffer, false); + } + + #[test] + fn read_clightning_init_msg() { + // Taken from c-lightning v0.8.0. + let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 170, 162, 1, 32, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15]; + check_init_msg(buffer, true); + } + + fn check_init_msg(buffer: Vec, expect_unknown: bool) { + let mut reader = io::Cursor::new(buffer); + let decoded_msg = read(&mut reader).unwrap(); + match decoded_msg { + Message::Init(msgs::Init { features }) => { + assert!(features.supports_variable_length_onion()); + assert!(features.supports_upfront_shutdown_script()); + assert!(features.supports_gossip_queries()); + assert_eq!(expect_unknown, features.supports_unknown_bits()); + assert!(!features.requires_unknown_bits()); + assert!(!features.initial_routing_sync()); + }, + _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id()) + } + } + + #[test] + fn read_lnd_node_announcement() { + // Taken from lnd v0.9.0-beta. + let buffer = vec![1, 1, 91, 164, 146, 213, 213, 165, 21, 227, 102, 33, 105, 179, 214, 21, 221, 175, 228, 93, 57, 177, 191, 127, 107, 229, 31, 50, 21, 81, 179, 71, 39, 18, 35, 2, 89, 224, 110, 123, 66, 39, 148, 246, 177, 85, 12, 19, 70, 226, 173, 132, 156, 26, 122, 146, 71, 213, 247, 48, 93, 190, 185, 177, 12, 172, 0, 3, 2, 162, 161, 94, 103, 195, 37, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 51, 153, 255, 97, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 172, 21, 0, 2, 38, 7]; + let mut reader = io::Cursor::new(buffer); + let decoded_msg = read(&mut reader).unwrap(); + match decoded_msg { + Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => { + assert!(features.supports_variable_length_onion()); + assert!(features.supports_upfront_shutdown_script()); + assert!(features.supports_gossip_queries()); + assert!(!features.requires_unknown_bits()); + }, + _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + } + } + + #[test] + fn read_lnd_chan_announcement() { + // Taken from lnd v0.9.0-beta. + let buffer = vec![1, 0, 82, 238, 153, 33, 128, 87, 215, 2, 28, 241, 140, 250, 98, 255, 56, 5, 79, 240, 214, 231, 172, 35, 240, 171, 44, 9, 78, 91, 8, 193, 102, 5, 17, 178, 142, 106, 180, 183, 46, 38, 217, 212, 25, 236, 69, 47, 92, 217, 181, 221, 161, 205, 121, 201, 99, 38, 158, 216, 186, 193, 230, 86, 222, 6, 206, 67, 22, 255, 137, 212, 141, 161, 62, 134, 76, 48, 241, 54, 50, 167, 187, 247, 73, 27, 74, 1, 129, 185, 197, 153, 38, 90, 255, 138, 39, 161, 102, 172, 213, 74, 107, 88, 150, 90, 0, 49, 104, 7, 182, 184, 194, 219, 181, 172, 8, 245, 65, 226, 19, 228, 101, 145, 25, 159, 52, 31, 58, 93, 53, 59, 218, 91, 37, 84, 103, 17, 74, 133, 33, 35, 2, 203, 101, 73, 19, 94, 175, 122, 46, 224, 47, 168, 128, 128, 25, 26, 25, 214, 52, 247, 43, 241, 117, 52, 206, 94, 135, 156, 52, 164, 143, 234, 58, 185, 50, 185, 140, 198, 174, 71, 65, 18, 105, 70, 131, 172, 137, 0, 164, 51, 215, 143, 117, 119, 217, 241, 197, 177, 227, 227, 170, 199, 114, 7, 218, 12, 107, 30, 191, 236, 203, 21, 61, 242, 48, 192, 90, 233, 200, 199, 111, 162, 68, 234, 54, 219, 1, 233, 66, 5, 82, 74, 84, 211, 95, 199, 245, 202, 89, 223, 102, 124, 62, 166, 253, 253, 90, 180, 118, 21, 61, 110, 37, 5, 96, 167, 0, 0, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15, 0, 2, 65, 0, 0, 1, 0, 0, 2, 37, 242, 97, 140, 2, 111, 69, 85, 39, 118, 30, 221, 99, 254, 120, 49, 103, 22, 170, 227, 111, 172, 164, 160, 49, 68, 138, 116, 16, 22, 206, 107, 3, 54, 61, 144, 88, 171, 247, 136, 208, 99, 9, 135, 37, 201, 178, 253, 136, 0, 185, 235, 68, 160, 106, 110, 12, 46, 21, 125, 204, 18, 75, 234, 16, 3, 42, 171, 28, 52, 224, 11, 30, 30, 253, 156, 148, 175, 203, 121, 250, 111, 122, 195, 84, 122, 77, 183, 56, 135, 101, 88, 41, 60, 191, 99, 232, 85, 2, 36, 17, 156, 11, 8, 12, 189, 177, 68, 88, 28, 15, 207, 21, 179, 151, 56, 226, 158, 148, 3, 120, 113, 177, 243, 184, 17, 173, 37, 46, 222, 16]; + let mut reader = io::Cursor::new(buffer); + let decoded_msg = read(&mut reader).unwrap(); + match decoded_msg { + Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => { + assert!(!features.requires_unknown_bits()); + }, + _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + } + } }