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 OpenChannelV2(msgs::OpenChannelV2),
58 AcceptChannel(msgs::AcceptChannel),
59 AcceptChannelV2(msgs::AcceptChannelV2),
60 FundingCreated(msgs::FundingCreated),
61 FundingSigned(msgs::FundingSigned),
62 TxAddInput(msgs::TxAddInput),
63 TxAddOutput(msgs::TxAddOutput),
64 TxRemoveInput(msgs::TxRemoveInput),
65 TxRemoveOutput(msgs::TxRemoveOutput),
66 TxComplete(msgs::TxComplete),
67 TxSignatures(msgs::TxSignatures),
68 TxInitRbf(msgs::TxInitRbf),
69 TxAckRbf(msgs::TxAckRbf),
70 TxAbort(msgs::TxAbort),
71 ChannelReady(msgs::ChannelReady),
72 Shutdown(msgs::Shutdown),
73 ClosingSigned(msgs::ClosingSigned),
74 OnionMessage(msgs::OnionMessage),
75 UpdateAddHTLC(msgs::UpdateAddHTLC),
76 UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
77 UpdateFailHTLC(msgs::UpdateFailHTLC),
78 UpdateFailMalformedHTLC(msgs::UpdateFailMalformedHTLC),
79 CommitmentSigned(msgs::CommitmentSigned),
80 RevokeAndACK(msgs::RevokeAndACK),
81 UpdateFee(msgs::UpdateFee),
82 ChannelReestablish(msgs::ChannelReestablish),
83 AnnouncementSignatures(msgs::AnnouncementSignatures),
84 ChannelAnnouncement(msgs::ChannelAnnouncement),
85 NodeAnnouncement(msgs::NodeAnnouncement),
86 ChannelUpdate(msgs::ChannelUpdate),
87 QueryShortChannelIds(msgs::QueryShortChannelIds),
88 ReplyShortChannelIdsEnd(msgs::ReplyShortChannelIdsEnd),
89 QueryChannelRange(msgs::QueryChannelRange),
90 ReplyChannelRange(msgs::ReplyChannelRange),
91 GossipTimestampFilter(msgs::GossipTimestampFilter),
92 /// A message that could not be decoded because its type is unknown.
94 /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
95 /// [`crate::ln::peer_handler::CustomMessageHandler`].
99 impl<T> Message<T> where T: core::fmt::Debug + Type + TestEq {
100 /// Returns the type that was used to decode the message payload.
101 pub fn type_id(&self) -> u16 {
103 &Message::Init(ref msg) => msg.type_id(),
104 &Message::Error(ref msg) => msg.type_id(),
105 &Message::Warning(ref msg) => msg.type_id(),
106 &Message::Ping(ref msg) => msg.type_id(),
107 &Message::Pong(ref msg) => msg.type_id(),
108 &Message::OpenChannel(ref msg) => msg.type_id(),
109 &Message::OpenChannelV2(ref msg) => msg.type_id(),
110 &Message::AcceptChannel(ref msg) => msg.type_id(),
111 &Message::AcceptChannelV2(ref msg) => msg.type_id(),
112 &Message::FundingCreated(ref msg) => msg.type_id(),
113 &Message::FundingSigned(ref msg) => msg.type_id(),
114 &Message::TxAddInput(ref msg) => msg.type_id(),
115 &Message::TxAddOutput(ref msg) => msg.type_id(),
116 &Message::TxRemoveInput(ref msg) => msg.type_id(),
117 &Message::TxRemoveOutput(ref msg) => msg.type_id(),
118 &Message::TxComplete(ref msg) => msg.type_id(),
119 &Message::TxSignatures(ref msg) => msg.type_id(),
120 &Message::TxInitRbf(ref msg) => msg.type_id(),
121 &Message::TxAckRbf(ref msg) => msg.type_id(),
122 &Message::TxAbort(ref msg) => msg.type_id(),
123 &Message::ChannelReady(ref msg) => msg.type_id(),
124 &Message::Shutdown(ref msg) => msg.type_id(),
125 &Message::ClosingSigned(ref msg) => msg.type_id(),
126 &Message::OnionMessage(ref msg) => msg.type_id(),
127 &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
128 &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
129 &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
130 &Message::UpdateFailMalformedHTLC(ref msg) => msg.type_id(),
131 &Message::CommitmentSigned(ref msg) => msg.type_id(),
132 &Message::RevokeAndACK(ref msg) => msg.type_id(),
133 &Message::UpdateFee(ref msg) => msg.type_id(),
134 &Message::ChannelReestablish(ref msg) => msg.type_id(),
135 &Message::AnnouncementSignatures(ref msg) => msg.type_id(),
136 &Message::ChannelAnnouncement(ref msg) => msg.type_id(),
137 &Message::NodeAnnouncement(ref msg) => msg.type_id(),
138 &Message::ChannelUpdate(ref msg) => msg.type_id(),
139 &Message::QueryShortChannelIds(ref msg) => msg.type_id(),
140 &Message::ReplyShortChannelIdsEnd(ref msg) => msg.type_id(),
141 &Message::QueryChannelRange(ref msg) => msg.type_id(),
142 &Message::ReplyChannelRange(ref msg) => msg.type_id(),
143 &Message::GossipTimestampFilter(ref msg) => msg.type_id(),
144 &Message::Unknown(type_id) => type_id,
145 &Message::Custom(ref msg) => msg.type_id(),
149 /// Returns whether the message's type is even, indicating both endpoints must support it.
150 pub fn is_even(&self) -> bool {
151 (self.type_id() & 1) == 0
155 /// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
156 /// variable-length payload conforming to the type.
160 /// Returns an error if the message payload could not be decoded as the specified type.
161 pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
162 -> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
163 T: core::fmt::Debug + Type + Writeable,
164 H::Target: CustomMessageReader<CustomMessage = T>,
166 let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
167 do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
170 fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
171 -> Result<Message<T>, msgs::DecodeError> where
172 T: core::fmt::Debug + Type + Writeable,
173 H::Target: CustomMessageReader<CustomMessage = T>,
176 msgs::Init::TYPE => {
177 Ok(Message::Init(Readable::read(buffer)?))
179 msgs::ErrorMessage::TYPE => {
180 Ok(Message::Error(Readable::read(buffer)?))
182 msgs::WarningMessage::TYPE => {
183 Ok(Message::Warning(Readable::read(buffer)?))
185 msgs::Ping::TYPE => {
186 Ok(Message::Ping(Readable::read(buffer)?))
188 msgs::Pong::TYPE => {
189 Ok(Message::Pong(Readable::read(buffer)?))
191 msgs::OpenChannel::TYPE => {
192 Ok(Message::OpenChannel(Readable::read(buffer)?))
194 msgs::OpenChannelV2::TYPE => {
195 Ok(Message::OpenChannelV2(Readable::read(buffer)?))
197 msgs::AcceptChannel::TYPE => {
198 Ok(Message::AcceptChannel(Readable::read(buffer)?))
200 msgs::AcceptChannelV2::TYPE => {
201 Ok(Message::AcceptChannelV2(Readable::read(buffer)?))
203 msgs::FundingCreated::TYPE => {
204 Ok(Message::FundingCreated(Readable::read(buffer)?))
206 msgs::FundingSigned::TYPE => {
207 Ok(Message::FundingSigned(Readable::read(buffer)?))
209 msgs::TxAddInput::TYPE => {
210 Ok(Message::TxAddInput(Readable::read(buffer)?))
212 msgs::TxAddOutput::TYPE => {
213 Ok(Message::TxAddOutput(Readable::read(buffer)?))
215 msgs::TxRemoveInput::TYPE => {
216 Ok(Message::TxRemoveInput(Readable::read(buffer)?))
218 msgs::TxRemoveOutput::TYPE => {
219 Ok(Message::TxRemoveOutput(Readable::read(buffer)?))
221 msgs::TxComplete::TYPE => {
222 Ok(Message::TxComplete(Readable::read(buffer)?))
224 msgs::TxSignatures::TYPE => {
225 Ok(Message::TxSignatures(Readable::read(buffer)?))
227 msgs::TxInitRbf::TYPE => {
228 Ok(Message::TxInitRbf(Readable::read(buffer)?))
230 msgs::TxAckRbf::TYPE => {
231 Ok(Message::TxAckRbf(Readable::read(buffer)?))
233 msgs::TxAbort::TYPE => {
234 Ok(Message::TxAbort(Readable::read(buffer)?))
236 msgs::ChannelReady::TYPE => {
237 Ok(Message::ChannelReady(Readable::read(buffer)?))
239 msgs::Shutdown::TYPE => {
240 Ok(Message::Shutdown(Readable::read(buffer)?))
242 msgs::ClosingSigned::TYPE => {
243 Ok(Message::ClosingSigned(Readable::read(buffer)?))
245 msgs::OnionMessage::TYPE => {
246 Ok(Message::OnionMessage(Readable::read(buffer)?))
248 msgs::UpdateAddHTLC::TYPE => {
249 Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
251 msgs::UpdateFulfillHTLC::TYPE => {
252 Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?))
254 msgs::UpdateFailHTLC::TYPE => {
255 Ok(Message::UpdateFailHTLC(Readable::read(buffer)?))
257 msgs::UpdateFailMalformedHTLC::TYPE => {
258 Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?))
260 msgs::CommitmentSigned::TYPE => {
261 Ok(Message::CommitmentSigned(Readable::read(buffer)?))
263 msgs::RevokeAndACK::TYPE => {
264 Ok(Message::RevokeAndACK(Readable::read(buffer)?))
266 msgs::UpdateFee::TYPE => {
267 Ok(Message::UpdateFee(Readable::read(buffer)?))
269 msgs::ChannelReestablish::TYPE => {
270 Ok(Message::ChannelReestablish(Readable::read(buffer)?))
272 msgs::AnnouncementSignatures::TYPE => {
273 Ok(Message::AnnouncementSignatures(Readable::read(buffer)?))
275 msgs::ChannelAnnouncement::TYPE => {
276 Ok(Message::ChannelAnnouncement(Readable::read(buffer)?))
278 msgs::NodeAnnouncement::TYPE => {
279 Ok(Message::NodeAnnouncement(Readable::read(buffer)?))
281 msgs::ChannelUpdate::TYPE => {
282 Ok(Message::ChannelUpdate(Readable::read(buffer)?))
284 msgs::QueryShortChannelIds::TYPE => {
285 Ok(Message::QueryShortChannelIds(Readable::read(buffer)?))
287 msgs::ReplyShortChannelIdsEnd::TYPE => {
288 Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?))
290 msgs::QueryChannelRange::TYPE => {
291 Ok(Message::QueryChannelRange(Readable::read(buffer)?))
293 msgs::ReplyChannelRange::TYPE => {
294 Ok(Message::ReplyChannelRange(Readable::read(buffer)?))
296 msgs::GossipTimestampFilter::TYPE => {
297 Ok(Message::GossipTimestampFilter(Readable::read(buffer)?))
300 if let Some(custom) = custom_reader.read(message_type, buffer)? {
301 Ok(Message::Custom(custom))
303 Ok(Message::Unknown(message_type))
309 /// Writes a message to the data buffer encoded as a 2-byte big-endian type and a variable-length
314 /// Returns an I/O error if the write could not be completed.
315 pub(crate) fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
316 message.type_id().write(buffer)?;
317 message.write(buffer)
321 /// Defines a constant type identifier for reading messages from the wire.
323 /// The type identifying the message payload.
328 pub(crate) use self::encode::Encode;
331 /// Defines a type identifier for sending messages over the wire.
333 /// Messages implementing this trait specify a type and must be [`Writeable`].
334 pub trait Type: core::fmt::Debug + Writeable {
335 /// Returns the type identifying the message payload.
336 fn type_id(&self) -> u16;
340 pub trait Type: core::fmt::Debug + Writeable + PartialEq {
341 fn type_id(&self) -> u16;
344 #[cfg(any(feature = "_test_utils", fuzzing, test))]
346 fn type_id(&self) -> u16 { unreachable!(); }
350 impl<T: core::fmt::Debug + Writeable + PartialEq> Type for T where T: Encode {
351 fn type_id(&self) -> u16 { T::TYPE }
355 impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
356 fn type_id(&self) -> u16 { T::TYPE }
359 impl Encode for msgs::Init {
360 const TYPE: u16 = 16;
363 impl Encode for msgs::ErrorMessage {
364 const TYPE: u16 = 17;
367 impl Encode for msgs::WarningMessage {
371 impl Encode for msgs::Ping {
372 const TYPE: u16 = 18;
375 impl Encode for msgs::Pong {
376 const TYPE: u16 = 19;
379 impl Encode for msgs::OpenChannel {
380 const TYPE: u16 = 32;
383 impl Encode for msgs::AcceptChannel {
384 const TYPE: u16 = 33;
387 impl Encode for msgs::FundingCreated {
388 const TYPE: u16 = 34;
391 impl Encode for msgs::FundingSigned {
392 const TYPE: u16 = 35;
395 impl Encode for msgs::ChannelReady {
396 const TYPE: u16 = 36;
399 impl Encode for msgs::Shutdown {
400 const TYPE: u16 = 38;
403 impl Encode for msgs::ClosingSigned {
404 const TYPE: u16 = 39;
407 impl Encode for msgs::OpenChannelV2 {
408 const TYPE: u16 = 64;
411 impl Encode for msgs::AcceptChannelV2 {
412 const TYPE: u16 = 65;
415 impl Encode for msgs::TxAddInput {
416 const TYPE: u16 = 66;
419 impl Encode for msgs::TxAddOutput {
420 const TYPE: u16 = 67;
423 impl Encode for msgs::TxRemoveInput {
424 const TYPE: u16 = 68;
427 impl Encode for msgs::TxRemoveOutput {
428 const TYPE: u16 = 69;
431 impl Encode for msgs::TxComplete {
432 const TYPE: u16 = 70;
435 impl Encode for msgs::TxSignatures {
436 const TYPE: u16 = 71;
439 impl Encode for msgs::TxInitRbf {
440 const TYPE: u16 = 72;
443 impl Encode for msgs::TxAckRbf {
444 const TYPE: u16 = 73;
447 impl Encode for msgs::TxAbort {
448 const TYPE: u16 = 74;
451 impl Encode for msgs::OnionMessage {
452 const TYPE: u16 = 513;
455 impl Encode for msgs::UpdateAddHTLC {
456 const TYPE: u16 = 128;
459 impl Encode for msgs::UpdateFulfillHTLC {
460 const TYPE: u16 = 130;
463 impl Encode for msgs::UpdateFailHTLC {
464 const TYPE: u16 = 131;
467 impl Encode for msgs::UpdateFailMalformedHTLC {
468 const TYPE: u16 = 135;
471 impl Encode for msgs::CommitmentSigned {
472 const TYPE: u16 = 132;
475 impl Encode for msgs::RevokeAndACK {
476 const TYPE: u16 = 133;
479 impl Encode for msgs::UpdateFee {
480 const TYPE: u16 = 134;
483 impl Encode for msgs::ChannelReestablish {
484 const TYPE: u16 = 136;
487 impl Encode for msgs::AnnouncementSignatures {
488 const TYPE: u16 = 259;
491 impl Encode for msgs::ChannelAnnouncement {
492 const TYPE: u16 = 256;
495 impl Encode for msgs::NodeAnnouncement {
496 const TYPE: u16 = 257;
499 impl Encode for msgs::ChannelUpdate {
500 const TYPE: u16 = 258;
503 impl Encode for msgs::QueryShortChannelIds {
504 const TYPE: u16 = 261;
507 impl Encode for msgs::ReplyShortChannelIdsEnd {
508 const TYPE: u16 = 262;
511 impl Encode for msgs::QueryChannelRange {
512 const TYPE: u16 = 263;
515 impl Encode for msgs::ReplyChannelRange {
516 const TYPE: u16 = 264;
519 impl Encode for msgs::GossipTimestampFilter {
520 const TYPE: u16 = 265;
526 use crate::prelude::*;
527 use core::convert::TryInto;
528 use crate::ln::peer_handler::IgnoringMessageHandler;
530 // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
531 const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
534 fn read_empty_buffer() {
536 let mut reader = io::Cursor::new(buffer);
537 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
541 fn read_incomplete_type() {
542 let buffer = &ENCODED_PONG[..1];
543 let mut reader = io::Cursor::new(buffer);
544 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
548 fn read_empty_payload() {
549 let buffer = &ENCODED_PONG[..2];
550 let mut reader = io::Cursor::new(buffer);
551 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
555 fn read_invalid_message() {
556 let buffer = &ENCODED_PONG[..4];
557 let mut reader = io::Cursor::new(buffer);
558 assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err());
562 fn read_known_message() {
563 let buffer = &ENCODED_PONG[..];
564 let mut reader = io::Cursor::new(buffer);
565 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
567 Message::Pong(_) => (),
568 _ => panic!("Expected pong message; found message type: {}", message.type_id()),
573 fn read_unknown_message() {
574 let buffer = &::core::u16::MAX.to_be_bytes();
575 let mut reader = io::Cursor::new(buffer);
576 let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
578 Message::Unknown(::core::u16::MAX) => (),
579 _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()),
584 fn write_message_with_type() {
585 let message = msgs::Pong { byteslen: 2u16 };
586 let mut buffer = Vec::new();
587 assert!(write(&message, &mut buffer).is_ok());
589 let type_length = ::core::mem::size_of::<u16>();
590 let (type_bytes, payload_bytes) = buffer.split_at(type_length);
591 assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE);
592 assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
596 fn read_message_encoded_with_write() {
597 let message = msgs::Pong { byteslen: 2u16 };
598 let mut buffer = Vec::new();
599 assert!(write(&message, &mut buffer).is_ok());
601 let mut reader = io::Cursor::new(buffer);
602 let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
603 match decoded_message {
604 Message::Pong(msgs::Pong { byteslen: 2u16 }) => (),
605 Message::Pong(msgs::Pong { byteslen }) => {
606 panic!("Expected byteslen {}; found: {}", message.byteslen, byteslen);
608 _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
613 fn is_even_message_type() {
614 let message = Message::<()>::Unknown(42);
615 assert!(message.is_even());
619 fn is_odd_message_type() {
620 let message = Message::<()>::Unknown(43);
621 assert!(!message.is_even());
625 fn read_lnd_init_msg() {
626 // Taken from lnd v0.9.0-beta.
627 let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
628 check_init_msg(buffer, false);
632 fn read_clightning_init_msg() {
633 // Taken from c-lightning v0.8.0.
634 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];
635 check_init_msg(buffer, true);
638 fn check_init_msg(buffer: Vec<u8>, expect_unknown: bool) {
639 let mut reader = io::Cursor::new(buffer);
640 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
642 Message::Init(msgs::Init { features, .. }) => {
643 assert!(features.supports_variable_length_onion());
644 assert!(features.supports_upfront_shutdown_script());
645 assert!(features.supports_gossip_queries());
646 assert_eq!(expect_unknown, features.supports_unknown_bits());
647 assert!(!features.requires_unknown_bits());
648 assert!(!features.initial_routing_sync());
650 _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id())
655 fn read_lnd_node_announcement() {
656 // Taken from lnd v0.9.0-beta.
657 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];
658 let mut reader = io::Cursor::new(buffer);
659 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
661 Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
662 assert!(features.supports_variable_length_onion());
663 assert!(features.supports_upfront_shutdown_script());
664 assert!(features.supports_gossip_queries());
665 assert!(!features.requires_unknown_bits());
667 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
672 fn read_lnd_chan_announcement() {
673 // Taken from lnd v0.9.0-beta.
674 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];
675 let mut reader = io::Cursor::new(buffer);
676 let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap();
678 Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => {
679 assert!(!features.requires_unknown_bits());
681 _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())
685 #[derive(Eq, PartialEq, Debug)]
686 struct TestCustomMessage {}
688 const CUSTOM_MESSAGE_TYPE : u16 = 9000;
690 impl Type for TestCustomMessage {
691 fn type_id(&self) -> u16 {
696 impl Writeable for TestCustomMessage {
697 fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
702 struct TestCustomMessageReader {}
704 impl CustomMessageReader for TestCustomMessageReader {
705 type CustomMessage = TestCustomMessage;
706 fn read<R: io::Read>(
710 ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
711 if message_type == CUSTOM_MESSAGE_TYPE {
712 return Ok(Some(TestCustomMessage{}));
720 fn read_custom_message() {
721 let buffer = vec![35, 40];
722 let mut reader = io::Cursor::new(buffer);
723 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
725 Message::Custom(custom) => {
726 assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE);
727 assert_eq!(custom, TestCustomMessage {});
729 _ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),
734 fn read_with_custom_reader_unknown_message_type() {
735 let buffer = vec![35, 42];
736 let mut reader = io::Cursor::new(buffer);
737 let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
739 Message::Unknown(_) => {},
740 _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()),
745 fn custom_reader_unknown_message_type() {
746 let buffer = Vec::new();
747 let mut reader = io::Cursor::new(buffer);
748 let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap();
749 assert!(res.is_none());