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 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 FundingLocked(msgs::FundingLocked),
61 Shutdown(msgs::Shutdown),
62 ClosingSigned(msgs::ClosingSigned),
63 UpdateAddHTLC(msgs::UpdateAddHTLC),
64 UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
65 UpdateFailHTLC(msgs::UpdateFailHTLC),
66 UpdateFailMalformedHTLC(msgs::UpdateFailMalformedHTLC),
67 CommitmentSigned(msgs::CommitmentSigned),
68 RevokeAndACK(msgs::RevokeAndACK),
69 UpdateFee(msgs::UpdateFee),
70 ChannelReestablish(msgs::ChannelReestablish),
71 AnnouncementSignatures(msgs::AnnouncementSignatures),
72 ChannelAnnouncement(msgs::ChannelAnnouncement),
73 NodeAnnouncement(msgs::NodeAnnouncement),
74 ChannelUpdate(msgs::ChannelUpdate),
75 QueryShortChannelIds(msgs::QueryShortChannelIds),
76 ReplyShortChannelIdsEnd(msgs::ReplyShortChannelIdsEnd),
77 QueryChannelRange(msgs::QueryChannelRange),
78 ReplyChannelRange(msgs::ReplyChannelRange),
79 GossipTimestampFilter(msgs::GossipTimestampFilter),
80 /// A message that could not be decoded because its type is unknown.
82 /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
83 /// [`::ln::peer_handler::CustomMessageHandler`].
87 impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
88 /// Returns the type that was used to decode the message payload.
89 pub fn type_id(&self) -> u16 {
91 &Message::Init(ref msg) => msg.type_id(),
92 &Message::Error(ref msg) => msg.type_id(),
93 &Message::Warning(ref msg) => msg.type_id(),
94 &Message::Ping(ref msg) => msg.type_id(),
95 &Message::Pong(ref msg) => msg.type_id(),
96 &Message::OpenChannel(ref msg) => msg.type_id(),
97 &Message::AcceptChannel(ref msg) => msg.type_id(),
98 &Message::FundingCreated(ref msg) => msg.type_id(),
99 &Message::FundingSigned(ref msg) => msg.type_id(),
100 &Message::FundingLocked(ref msg) => msg.type_id(),
101 &Message::Shutdown(ref msg) => msg.type_id(),
102 &Message::ClosingSigned(ref msg) => msg.type_id(),
103 &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
104 &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
105 &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
106 &Message::UpdateFailMalformedHTLC(ref msg) => msg.type_id(),
107 &Message::CommitmentSigned(ref msg) => msg.type_id(),
108 &Message::RevokeAndACK(ref msg) => msg.type_id(),
109 &Message::UpdateFee(ref msg) => msg.type_id(),
110 &Message::ChannelReestablish(ref msg) => msg.type_id(),
111 &Message::AnnouncementSignatures(ref msg) => msg.type_id(),
112 &Message::ChannelAnnouncement(ref msg) => msg.type_id(),
113 &Message::NodeAnnouncement(ref msg) => msg.type_id(),
114 &Message::ChannelUpdate(ref msg) => msg.type_id(),
115 &Message::QueryShortChannelIds(ref msg) => msg.type_id(),
116 &Message::ReplyShortChannelIdsEnd(ref msg) => msg.type_id(),
117 &Message::QueryChannelRange(ref msg) => msg.type_id(),
118 &Message::ReplyChannelRange(ref msg) => msg.type_id(),
119 &Message::GossipTimestampFilter(ref msg) => msg.type_id(),
120 &Message::Unknown(type_id) => type_id,
121 &Message::Custom(ref msg) => msg.type_id(),
125 /// Returns whether the message's type is even, indicating both endpoints must support it.
126 pub fn is_even(&self) -> bool {
127 (self.type_id() & 1) == 0
131 /// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
132 /// variable-length payload conforming to the type.
136 /// Returns an error if the message payload code not be decoded as the specified type.
137 pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
138 -> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
139 T: core::fmt::Debug + Type + Writeable,
140 H::Target: CustomMessageReader<CustomMessage = T>,
142 let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
143 do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
146 fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
147 -> Result<Message<T>, msgs::DecodeError> where
148 T: core::fmt::Debug + Type + Writeable,
149 H::Target: CustomMessageReader<CustomMessage = T>,
152 msgs::Init::TYPE => {
153 Ok(Message::Init(Readable::read(buffer)?))
155 msgs::ErrorMessage::TYPE => {
156 Ok(Message::Error(Readable::read(buffer)?))
158 msgs::WarningMessage::TYPE => {
159 Ok(Message::Warning(Readable::read(buffer)?))
161 msgs::Ping::TYPE => {
162 Ok(Message::Ping(Readable::read(buffer)?))
164 msgs::Pong::TYPE => {
165 Ok(Message::Pong(Readable::read(buffer)?))
167 msgs::OpenChannel::TYPE => {
168 Ok(Message::OpenChannel(Readable::read(buffer)?))
170 msgs::AcceptChannel::TYPE => {
171 Ok(Message::AcceptChannel(Readable::read(buffer)?))
173 msgs::FundingCreated::TYPE => {
174 Ok(Message::FundingCreated(Readable::read(buffer)?))
176 msgs::FundingSigned::TYPE => {
177 Ok(Message::FundingSigned(Readable::read(buffer)?))
179 msgs::FundingLocked::TYPE => {
180 Ok(Message::FundingLocked(Readable::read(buffer)?))
182 msgs::Shutdown::TYPE => {
183 Ok(Message::Shutdown(Readable::read(buffer)?))
185 msgs::ClosingSigned::TYPE => {
186 Ok(Message::ClosingSigned(Readable::read(buffer)?))
188 msgs::UpdateAddHTLC::TYPE => {
189 Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
191 msgs::UpdateFulfillHTLC::TYPE => {
192 Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?))
194 msgs::UpdateFailHTLC::TYPE => {
195 Ok(Message::UpdateFailHTLC(Readable::read(buffer)?))
197 msgs::UpdateFailMalformedHTLC::TYPE => {
198 Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?))
200 msgs::CommitmentSigned::TYPE => {
201 Ok(Message::CommitmentSigned(Readable::read(buffer)?))
203 msgs::RevokeAndACK::TYPE => {
204 Ok(Message::RevokeAndACK(Readable::read(buffer)?))
206 msgs::UpdateFee::TYPE => {
207 Ok(Message::UpdateFee(Readable::read(buffer)?))
209 msgs::ChannelReestablish::TYPE => {
210 Ok(Message::ChannelReestablish(Readable::read(buffer)?))
212 msgs::AnnouncementSignatures::TYPE => {
213 Ok(Message::AnnouncementSignatures(Readable::read(buffer)?))
215 msgs::ChannelAnnouncement::TYPE => {
216 Ok(Message::ChannelAnnouncement(Readable::read(buffer)?))
218 msgs::NodeAnnouncement::TYPE => {
219 Ok(Message::NodeAnnouncement(Readable::read(buffer)?))
221 msgs::ChannelUpdate::TYPE => {
222 Ok(Message::ChannelUpdate(Readable::read(buffer)?))
224 msgs::QueryShortChannelIds::TYPE => {
225 Ok(Message::QueryShortChannelIds(Readable::read(buffer)?))
227 msgs::ReplyShortChannelIdsEnd::TYPE => {
228 Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?))
230 msgs::QueryChannelRange::TYPE => {
231 Ok(Message::QueryChannelRange(Readable::read(buffer)?))
233 msgs::ReplyChannelRange::TYPE => {
234 Ok(Message::ReplyChannelRange(Readable::read(buffer)?))
236 msgs::GossipTimestampFilter::TYPE => {
237 Ok(Message::GossipTimestampFilter(Readable::read(buffer)?))
240 if let Some(custom) = custom_reader.read(message_type, buffer)? {
241 Ok(Message::Custom(custom))
243 Ok(Message::Unknown(message_type))
249 /// Writes a message to the data buffer encoded as a 2-byte big-endian type and a variable-length
254 /// Returns an I/O error if the write could not be completed.
255 pub(crate) fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
256 message.type_id().write(buffer)?;
257 message.write(buffer)
261 /// Defines a constant type identifier for reading messages from the wire.
263 /// The type identifying the message payload.
268 pub(crate) use self::encode::Encode;
271 /// Defines a type identifier for sending messages over the wire.
273 /// Messages implementing this trait specify a type and must be [`Writeable`].
274 pub trait Type: core::fmt::Debug + Writeable {
275 /// Returns the type identifying the message payload.
276 fn type_id(&self) -> u16;
280 pub trait Type: core::fmt::Debug + Writeable + PartialEq {
281 fn type_id(&self) -> u16;
284 #[cfg(any(feature = "_test_utils", fuzzing, test))]
286 fn type_id(&self) -> u16 { unreachable!(); }
290 impl<T: core::fmt::Debug + Writeable + PartialEq> Type for T where T: Encode {
291 fn type_id(&self) -> u16 { T::TYPE }
295 impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
296 fn type_id(&self) -> u16 { T::TYPE }
299 impl Encode for msgs::Init {
300 const TYPE: u16 = 16;
303 impl Encode for msgs::ErrorMessage {
304 const TYPE: u16 = 17;
307 impl Encode for msgs::WarningMessage {
311 impl Encode for msgs::Ping {
312 const TYPE: u16 = 18;
315 impl Encode for msgs::Pong {
316 const TYPE: u16 = 19;
319 impl Encode for msgs::OpenChannel {
320 const TYPE: u16 = 32;
323 impl Encode for msgs::AcceptChannel {
324 const TYPE: u16 = 33;
327 impl Encode for msgs::FundingCreated {
328 const TYPE: u16 = 34;
331 impl Encode for msgs::FundingSigned {
332 const TYPE: u16 = 35;
335 impl Encode for msgs::FundingLocked {
336 const TYPE: u16 = 36;
339 impl Encode for msgs::Shutdown {
340 const TYPE: u16 = 38;
343 impl Encode for msgs::ClosingSigned {
344 const TYPE: u16 = 39;
347 impl Encode for msgs::UpdateAddHTLC {
348 const TYPE: u16 = 128;
351 impl Encode for msgs::UpdateFulfillHTLC {
352 const TYPE: u16 = 130;
355 impl Encode for msgs::UpdateFailHTLC {
356 const TYPE: u16 = 131;
359 impl Encode for msgs::UpdateFailMalformedHTLC {
360 const TYPE: u16 = 135;
363 impl Encode for msgs::CommitmentSigned {
364 const TYPE: u16 = 132;
367 impl Encode for msgs::RevokeAndACK {
368 const TYPE: u16 = 133;
371 impl Encode for msgs::UpdateFee {
372 const TYPE: u16 = 134;
375 impl Encode for msgs::ChannelReestablish {
376 const TYPE: u16 = 136;
379 impl Encode for msgs::AnnouncementSignatures {
380 const TYPE: u16 = 259;
383 impl Encode for msgs::ChannelAnnouncement {
384 const TYPE: u16 = 256;
387 impl Encode for msgs::NodeAnnouncement {
388 const TYPE: u16 = 257;
391 impl Encode for msgs::ChannelUpdate {
392 const TYPE: u16 = 258;
395 impl Encode for msgs::QueryShortChannelIds {
396 const TYPE: u16 = 261;
399 impl Encode for msgs::ReplyShortChannelIdsEnd {
400 const TYPE: u16 = 262;
403 impl Encode for msgs::QueryChannelRange {
404 const TYPE: u16 = 263;
407 impl Encode for msgs::ReplyChannelRange {
408 const TYPE: u16 = 264;
411 impl Encode for msgs::GossipTimestampFilter {
412 const TYPE: u16 = 265;
419 use core::convert::TryInto;
420 use ::ln::peer_handler::IgnoringMessageHandler;
422 // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
423 const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
426 fn read_empty_buffer() {
428 let mut reader = io::Cursor::new(buffer);
429 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
433 fn read_incomplete_type() {
434 let buffer = &ENCODED_PONG[..1];
435 let mut reader = io::Cursor::new(buffer);
436 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
440 fn read_empty_payload() {
441 let buffer = &ENCODED_PONG[..2];
442 let mut reader = io::Cursor::new(buffer);
443 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
447 fn read_invalid_message() {
448 let buffer = &ENCODED_PONG[..4];
449 let mut reader = io::Cursor::new(buffer);
450 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
454 fn read_known_message() {
455 let buffer = &ENCODED_PONG[..];
456 let mut reader = io::Cursor::new(buffer);
457 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
459 Message::Pong(_) => (),
460 _ => panic!("Expected pong message; found message type: {}", message.type_id()),
465 fn read_unknown_message() {
466 let buffer = &::core::u16::MAX.to_be_bytes();
467 let mut reader = io::Cursor::new(buffer);
468 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
470 Message::Unknown(::core::u16::MAX) => (),
471 _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()),
476 fn write_message_with_type() {
477 let message = msgs::Pong { byteslen: 2u16 };
478 let mut buffer = Vec::new();
479 assert!(write(&message, &mut buffer).is_ok());
481 let type_length = ::core::mem::size_of::<u16>();
482 let (type_bytes, payload_bytes) = buffer.split_at(type_length);
483 assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE);
484 assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
488 fn read_message_encoded_with_write() {
489 let message = msgs::Pong { byteslen: 2u16 };
490 let mut buffer = Vec::new();
491 assert!(write(&message, &mut buffer).is_ok());
493 let mut reader = io::Cursor::new(buffer);
494 let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
495 match decoded_message {
496 Message::Pong(msgs::Pong { byteslen: 2u16 }) => (),
497 Message::Pong(msgs::Pong { byteslen }) => {
498 panic!("Expected byteslen {}; found: {}", message.byteslen, byteslen);
500 _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
505 fn is_even_message_type() {
506 let message = Message::<()>::Unknown(42);
507 assert!(message.is_even());
511 fn is_odd_message_type() {
512 let message = Message::<()>::Unknown(43);
513 assert!(!message.is_even());
517 fn read_lnd_init_msg() {
518 // Taken from lnd v0.9.0-beta.
519 let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
520 check_init_msg(buffer, false);
524 fn read_clightning_init_msg() {
525 // Taken from c-lightning v0.8.0.
526 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];
527 check_init_msg(buffer, true);
530 fn check_init_msg(buffer: Vec<u8>, expect_unknown: bool) {
531 let mut reader = io::Cursor::new(buffer);
532 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
534 Message::Init(msgs::Init { features, .. }) => {
535 assert!(features.supports_variable_length_onion());
536 assert!(features.supports_upfront_shutdown_script());
537 assert!(features.supports_gossip_queries());
538 assert_eq!(expect_unknown, features.supports_unknown_bits());
539 assert!(!features.requires_unknown_bits());
540 assert!(!features.initial_routing_sync());
542 _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id())
547 fn read_lnd_node_announcement() {
548 // Taken from lnd v0.9.0-beta.
549 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];
550 let mut reader = io::Cursor::new(buffer);
551 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
553 Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
554 assert!(features.supports_variable_length_onion());
555 assert!(features.supports_upfront_shutdown_script());
556 assert!(features.supports_gossip_queries());
557 assert!(!features.requires_unknown_bits());
559 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
564 fn read_lnd_chan_announcement() {
565 // Taken from lnd v0.9.0-beta.
566 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];
567 let mut reader = io::Cursor::new(buffer);
568 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
570 Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => {
571 assert!(!features.requires_unknown_bits());
573 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
577 #[derive(Eq, PartialEq, Debug)]
578 struct TestCustomMessage {}
580 const CUSTOM_MESSAGE_TYPE : u16 = 9000;
582 impl Type for TestCustomMessage {
583 fn type_id(&self) -> u16 {
588 impl Writeable for TestCustomMessage {
589 fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
594 struct TestCustomMessageReader {}
596 impl CustomMessageReader for TestCustomMessageReader {
597 type CustomMessage = TestCustomMessage;
598 fn read<R: io::Read>(
602 ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
603 if message_type == CUSTOM_MESSAGE_TYPE {
604 return Ok(Some(TestCustomMessage{}));
612 fn read_custom_message() {
613 let buffer = vec![35, 40];
614 let mut reader = io::Cursor::new(buffer);
615 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
617 Message::Custom(custom) => {
618 assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE);
619 assert_eq!(custom, TestCustomMessage {});
621 _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),
626 fn read_with_custom_reader_unknown_message_type() {
627 let buffer = vec![35, 42];
628 let mut reader = io::Cursor::new(buffer);
629 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
631 Message::Unknown(_) => {},
632 _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()),
637 fn custom_reader_unknown_message_type() {
638 let buffer = Vec::new();
639 let mut reader = io::Cursor::new(buffer);
640 let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap();
641 assert!(res.is_none());