1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
11 //! custom message through the [`CustomMessageReader`] trait.
13 //! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
17 use crate::util::ser::{Readable, Writeable, Writer};
19 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
21 pub trait CustomMessageReader {
22 /// The type of the message decoded by the implementation.
23 type CustomMessage: Type;
24 /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
25 /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
26 /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
27 /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
28 fn read<R: io::Read>(&self, message_type: u16, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
31 // TestEq is a dummy trait which requires PartialEq when built in testing, and otherwise is
32 // blanket-implemented for all types.
35 pub trait TestEq : PartialEq {}
37 impl<T: PartialEq> TestEq for T {}
40 pub(crate) trait TestEq {}
42 impl<T> TestEq for T {}
45 /// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each
46 /// variant contains a message from [`msgs`] or otherwise the message type if unknown.
47 #[allow(missing_docs)]
49 #[cfg_attr(test, derive(PartialEq))]
50 pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
52 Error(msgs::ErrorMessage),
53 Warning(msgs::WarningMessage),
56 OpenChannel(msgs::OpenChannel),
57 AcceptChannel(msgs::AcceptChannel),
58 FundingCreated(msgs::FundingCreated),
59 FundingSigned(msgs::FundingSigned),
60 ChannelReady(msgs::ChannelReady),
61 Shutdown(msgs::Shutdown),
62 ClosingSigned(msgs::ClosingSigned),
63 OnionMessage(msgs::OnionMessage),
64 UpdateAddHTLC(msgs::UpdateAddHTLC),
65 UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
66 UpdateFailHTLC(msgs::UpdateFailHTLC),
67 UpdateFailMalformedHTLC(msgs::UpdateFailMalformedHTLC),
68 CommitmentSigned(msgs::CommitmentSigned),
69 RevokeAndACK(msgs::RevokeAndACK),
70 UpdateFee(msgs::UpdateFee),
71 ChannelReestablish(msgs::ChannelReestablish),
72 AnnouncementSignatures(msgs::AnnouncementSignatures),
73 ChannelAnnouncement(msgs::ChannelAnnouncement),
74 NodeAnnouncement(msgs::NodeAnnouncement),
75 ChannelUpdate(msgs::ChannelUpdate),
76 QueryShortChannelIds(msgs::QueryShortChannelIds),
77 ReplyShortChannelIdsEnd(msgs::ReplyShortChannelIdsEnd),
78 QueryChannelRange(msgs::QueryChannelRange),
79 ReplyChannelRange(msgs::ReplyChannelRange),
80 GossipTimestampFilter(msgs::GossipTimestampFilter),
81 /// A message that could not be decoded because its type is unknown.
83 /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
84 /// [`crate::ln::peer_handler::CustomMessageHandler`].
88 impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
89 /// Returns the type that was used to decode the message payload.
90 pub fn type_id(&self) -> u16 {
92 &Message::Init(ref msg) => msg.type_id(),
93 &Message::Error(ref msg) => msg.type_id(),
94 &Message::Warning(ref msg) => msg.type_id(),
95 &Message::Ping(ref msg) => msg.type_id(),
96 &Message::Pong(ref msg) => msg.type_id(),
97 &Message::OpenChannel(ref msg) => msg.type_id(),
98 &Message::AcceptChannel(ref msg) => msg.type_id(),
99 &Message::FundingCreated(ref msg) => msg.type_id(),
100 &Message::FundingSigned(ref msg) => msg.type_id(),
101 &Message::ChannelReady(ref msg) => msg.type_id(),
102 &Message::Shutdown(ref msg) => msg.type_id(),
103 &Message::ClosingSigned(ref msg) => msg.type_id(),
104 &Message::OnionMessage(ref msg) => msg.type_id(),
105 &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
106 &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
107 &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
108 &Message::UpdateFailMalformedHTLC(ref msg) => msg.type_id(),
109 &Message::CommitmentSigned(ref msg) => msg.type_id(),
110 &Message::RevokeAndACK(ref msg) => msg.type_id(),
111 &Message::UpdateFee(ref msg) => msg.type_id(),
112 &Message::ChannelReestablish(ref msg) => msg.type_id(),
113 &Message::AnnouncementSignatures(ref msg) => msg.type_id(),
114 &Message::ChannelAnnouncement(ref msg) => msg.type_id(),
115 &Message::NodeAnnouncement(ref msg) => msg.type_id(),
116 &Message::ChannelUpdate(ref msg) => msg.type_id(),
117 &Message::QueryShortChannelIds(ref msg) => msg.type_id(),
118 &Message::ReplyShortChannelIdsEnd(ref msg) => msg.type_id(),
119 &Message::QueryChannelRange(ref msg) => msg.type_id(),
120 &Message::ReplyChannelRange(ref msg) => msg.type_id(),
121 &Message::GossipTimestampFilter(ref msg) => msg.type_id(),
122 &Message::Unknown(type_id) => type_id,
123 &Message::Custom(ref msg) => msg.type_id(),
127 /// Returns whether the message's type is even, indicating both endpoints must support it.
128 pub fn is_even(&self) -> bool {
129 (self.type_id() & 1) == 0
133 /// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
134 /// variable-length payload conforming to the type.
138 /// Returns an error if the message payload code not be decoded as the specified type.
139 pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
140 -> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
141 T: core::fmt::Debug + Type + Writeable,
142 H::Target: CustomMessageReader<CustomMessage = T>,
144 let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
145 do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
148 fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
149 -> Result<Message<T>, msgs::DecodeError> where
150 T: core::fmt::Debug + Type + Writeable,
151 H::Target: CustomMessageReader<CustomMessage = T>,
154 msgs::Init::TYPE => {
155 Ok(Message::Init(Readable::read(buffer)?))
157 msgs::ErrorMessage::TYPE => {
158 Ok(Message::Error(Readable::read(buffer)?))
160 msgs::WarningMessage::TYPE => {
161 Ok(Message::Warning(Readable::read(buffer)?))
163 msgs::Ping::TYPE => {
164 Ok(Message::Ping(Readable::read(buffer)?))
166 msgs::Pong::TYPE => {
167 Ok(Message::Pong(Readable::read(buffer)?))
169 msgs::OpenChannel::TYPE => {
170 Ok(Message::OpenChannel(Readable::read(buffer)?))
172 msgs::AcceptChannel::TYPE => {
173 Ok(Message::AcceptChannel(Readable::read(buffer)?))
175 msgs::FundingCreated::TYPE => {
176 Ok(Message::FundingCreated(Readable::read(buffer)?))
178 msgs::FundingSigned::TYPE => {
179 Ok(Message::FundingSigned(Readable::read(buffer)?))
181 msgs::ChannelReady::TYPE => {
182 Ok(Message::ChannelReady(Readable::read(buffer)?))
184 msgs::Shutdown::TYPE => {
185 Ok(Message::Shutdown(Readable::read(buffer)?))
187 msgs::ClosingSigned::TYPE => {
188 Ok(Message::ClosingSigned(Readable::read(buffer)?))
190 msgs::OnionMessage::TYPE => {
191 Ok(Message::OnionMessage(Readable::read(buffer)?))
193 msgs::UpdateAddHTLC::TYPE => {
194 Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
196 msgs::UpdateFulfillHTLC::TYPE => {
197 Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?))
199 msgs::UpdateFailHTLC::TYPE => {
200 Ok(Message::UpdateFailHTLC(Readable::read(buffer)?))
202 msgs::UpdateFailMalformedHTLC::TYPE => {
203 Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?))
205 msgs::CommitmentSigned::TYPE => {
206 Ok(Message::CommitmentSigned(Readable::read(buffer)?))
208 msgs::RevokeAndACK::TYPE => {
209 Ok(Message::RevokeAndACK(Readable::read(buffer)?))
211 msgs::UpdateFee::TYPE => {
212 Ok(Message::UpdateFee(Readable::read(buffer)?))
214 msgs::ChannelReestablish::TYPE => {
215 Ok(Message::ChannelReestablish(Readable::read(buffer)?))
217 msgs::AnnouncementSignatures::TYPE => {
218 Ok(Message::AnnouncementSignatures(Readable::read(buffer)?))
220 msgs::ChannelAnnouncement::TYPE => {
221 Ok(Message::ChannelAnnouncement(Readable::read(buffer)?))
223 msgs::NodeAnnouncement::TYPE => {
224 Ok(Message::NodeAnnouncement(Readable::read(buffer)?))
226 msgs::ChannelUpdate::TYPE => {
227 Ok(Message::ChannelUpdate(Readable::read(buffer)?))
229 msgs::QueryShortChannelIds::TYPE => {
230 Ok(Message::QueryShortChannelIds(Readable::read(buffer)?))
232 msgs::ReplyShortChannelIdsEnd::TYPE => {
233 Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?))
235 msgs::QueryChannelRange::TYPE => {
236 Ok(Message::QueryChannelRange(Readable::read(buffer)?))
238 msgs::ReplyChannelRange::TYPE => {
239 Ok(Message::ReplyChannelRange(Readable::read(buffer)?))
241 msgs::GossipTimestampFilter::TYPE => {
242 Ok(Message::GossipTimestampFilter(Readable::read(buffer)?))
245 if let Some(custom) = custom_reader.read(message_type, buffer)? {
246 Ok(Message::Custom(custom))
248 Ok(Message::Unknown(message_type))
254 /// Writes a message to the data buffer encoded as a 2-byte big-endian type and a variable-length
259 /// Returns an I/O error if the write could not be completed.
260 pub(crate) fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
261 message.type_id().write(buffer)?;
262 message.write(buffer)
266 /// Defines a constant type identifier for reading messages from the wire.
268 /// The type identifying the message payload.
273 pub(crate) use self::encode::Encode;
276 /// Defines a type identifier for sending messages over the wire.
278 /// Messages implementing this trait specify a type and must be [`Writeable`].
279 pub trait Type: core::fmt::Debug + Writeable {
280 /// Returns the type identifying the message payload.
281 fn type_id(&self) -> u16;
285 pub trait Type: core::fmt::Debug + Writeable + PartialEq {
286 fn type_id(&self) -> u16;
289 #[cfg(any(feature = "_test_utils", fuzzing, test))]
291 fn type_id(&self) -> u16 { unreachable!(); }
295 impl<T: core::fmt::Debug + Writeable + PartialEq> Type for T where T: Encode {
296 fn type_id(&self) -> u16 { T::TYPE }
300 impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
301 fn type_id(&self) -> u16 { T::TYPE }
304 impl Encode for msgs::Init {
305 const TYPE: u16 = 16;
308 impl Encode for msgs::ErrorMessage {
309 const TYPE: u16 = 17;
312 impl Encode for msgs::WarningMessage {
316 impl Encode for msgs::Ping {
317 const TYPE: u16 = 18;
320 impl Encode for msgs::Pong {
321 const TYPE: u16 = 19;
324 impl Encode for msgs::OpenChannel {
325 const TYPE: u16 = 32;
328 impl Encode for msgs::AcceptChannel {
329 const TYPE: u16 = 33;
332 impl Encode for msgs::FundingCreated {
333 const TYPE: u16 = 34;
336 impl Encode for msgs::FundingSigned {
337 const TYPE: u16 = 35;
340 impl Encode for msgs::ChannelReady {
341 const TYPE: u16 = 36;
344 impl Encode for msgs::Shutdown {
345 const TYPE: u16 = 38;
348 impl Encode for msgs::ClosingSigned {
349 const TYPE: u16 = 39;
352 impl Encode for msgs::OnionMessage {
353 const TYPE: u16 = 513;
356 impl Encode for msgs::UpdateAddHTLC {
357 const TYPE: u16 = 128;
360 impl Encode for msgs::UpdateFulfillHTLC {
361 const TYPE: u16 = 130;
364 impl Encode for msgs::UpdateFailHTLC {
365 const TYPE: u16 = 131;
368 impl Encode for msgs::UpdateFailMalformedHTLC {
369 const TYPE: u16 = 135;
372 impl Encode for msgs::CommitmentSigned {
373 const TYPE: u16 = 132;
376 impl Encode for msgs::RevokeAndACK {
377 const TYPE: u16 = 133;
380 impl Encode for msgs::UpdateFee {
381 const TYPE: u16 = 134;
384 impl Encode for msgs::ChannelReestablish {
385 const TYPE: u16 = 136;
388 impl Encode for msgs::AnnouncementSignatures {
389 const TYPE: u16 = 259;
392 impl Encode for msgs::ChannelAnnouncement {
393 const TYPE: u16 = 256;
396 impl Encode for msgs::NodeAnnouncement {
397 const TYPE: u16 = 257;
400 impl Encode for msgs::ChannelUpdate {
401 const TYPE: u16 = 258;
404 impl Encode for msgs::QueryShortChannelIds {
405 const TYPE: u16 = 261;
408 impl Encode for msgs::ReplyShortChannelIdsEnd {
409 const TYPE: u16 = 262;
412 impl Encode for msgs::QueryChannelRange {
413 const TYPE: u16 = 263;
416 impl Encode for msgs::ReplyChannelRange {
417 const TYPE: u16 = 264;
420 impl Encode for msgs::GossipTimestampFilter {
421 const TYPE: u16 = 265;
427 use crate::prelude::*;
428 use core::convert::TryInto;
429 use crate::ln::peer_handler::IgnoringMessageHandler;
431 // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
432 const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
435 fn read_empty_buffer() {
437 let mut reader = io::Cursor::new(buffer);
438 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
442 fn read_incomplete_type() {
443 let buffer = &ENCODED_PONG[..1];
444 let mut reader = io::Cursor::new(buffer);
445 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
449 fn read_empty_payload() {
450 let buffer = &ENCODED_PONG[..2];
451 let mut reader = io::Cursor::new(buffer);
452 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
456 fn read_invalid_message() {
457 let buffer = &ENCODED_PONG[..4];
458 let mut reader = io::Cursor::new(buffer);
459 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
463 fn read_known_message() {
464 let buffer = &ENCODED_PONG[..];
465 let mut reader = io::Cursor::new(buffer);
466 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
468 Message::Pong(_) => (),
469 _ => panic!("Expected pong message; found message type: {}", message.type_id()),
474 fn read_unknown_message() {
475 let buffer = &::core::u16::MAX.to_be_bytes();
476 let mut reader = io::Cursor::new(buffer);
477 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
479 Message::Unknown(::core::u16::MAX) => (),
480 _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()),
485 fn write_message_with_type() {
486 let message = msgs::Pong { byteslen: 2u16 };
487 let mut buffer = Vec::new();
488 assert!(write(&message, &mut buffer).is_ok());
490 let type_length = ::core::mem::size_of::<u16>();
491 let (type_bytes, payload_bytes) = buffer.split_at(type_length);
492 assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE);
493 assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
497 fn read_message_encoded_with_write() {
498 let message = msgs::Pong { byteslen: 2u16 };
499 let mut buffer = Vec::new();
500 assert!(write(&message, &mut buffer).is_ok());
502 let mut reader = io::Cursor::new(buffer);
503 let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
504 match decoded_message {
505 Message::Pong(msgs::Pong { byteslen: 2u16 }) => (),
506 Message::Pong(msgs::Pong { byteslen }) => {
507 panic!("Expected byteslen {}; found: {}", message.byteslen, byteslen);
509 _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
514 fn is_even_message_type() {
515 let message = Message::<()>::Unknown(42);
516 assert!(message.is_even());
520 fn is_odd_message_type() {
521 let message = Message::<()>::Unknown(43);
522 assert!(!message.is_even());
526 fn read_lnd_init_msg() {
527 // Taken from lnd v0.9.0-beta.
528 let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
529 check_init_msg(buffer, false);
533 fn read_clightning_init_msg() {
534 // Taken from c-lightning v0.8.0.
535 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];
536 check_init_msg(buffer, true);
539 fn check_init_msg(buffer: Vec<u8>, expect_unknown: bool) {
540 let mut reader = io::Cursor::new(buffer);
541 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
543 Message::Init(msgs::Init { features, .. }) => {
544 assert!(features.supports_variable_length_onion());
545 assert!(features.supports_upfront_shutdown_script());
546 assert!(features.supports_gossip_queries());
547 assert_eq!(expect_unknown, features.supports_unknown_bits());
548 assert!(!features.requires_unknown_bits());
549 assert!(!features.initial_routing_sync());
551 _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id())
556 fn read_lnd_node_announcement() {
557 // Taken from lnd v0.9.0-beta.
558 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];
559 let mut reader = io::Cursor::new(buffer);
560 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
562 Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
563 assert!(features.supports_variable_length_onion());
564 assert!(features.supports_upfront_shutdown_script());
565 assert!(features.supports_gossip_queries());
566 assert!(!features.requires_unknown_bits());
568 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
573 fn read_lnd_chan_announcement() {
574 // Taken from lnd v0.9.0-beta.
575 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];
576 let mut reader = io::Cursor::new(buffer);
577 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
579 Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => {
580 assert!(!features.requires_unknown_bits());
582 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
586 #[derive(Eq, PartialEq, Debug)]
587 struct TestCustomMessage {}
589 const CUSTOM_MESSAGE_TYPE : u16 = 9000;
591 impl Type for TestCustomMessage {
592 fn type_id(&self) -> u16 {
597 impl Writeable for TestCustomMessage {
598 fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
603 struct TestCustomMessageReader {}
605 impl CustomMessageReader for TestCustomMessageReader {
606 type CustomMessage = TestCustomMessage;
607 fn read<R: io::Read>(
611 ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
612 if message_type == CUSTOM_MESSAGE_TYPE {
613 return Ok(Some(TestCustomMessage{}));
621 fn read_custom_message() {
622 let buffer = vec![35, 40];
623 let mut reader = io::Cursor::new(buffer);
624 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
626 Message::Custom(custom) => {
627 assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE);
628 assert_eq!(custom, TestCustomMessage {});
630 _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),
635 fn read_with_custom_reader_unknown_message_type() {
636 let buffer = vec![35, 42];
637 let mut reader = io::Cursor::new(buffer);
638 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
640 Message::Unknown(_) => {},
641 _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()),
646 fn custom_reader_unknown_message_type() {
647 let buffer = Vec::new();
648 let mut reader = io::Cursor::new(buffer);
649 let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap();
650 assert!(res.is_none());