Skip to content

Commit d2523ee

Browse files
committed
Move message type parity logic to the wire module
Create a MessageType abstraction and use it throughout the wire module's external interfaces. Include an is_even method for clients to determine how to handle unknown messages.
1 parent 07e9e7a commit d2523ee

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,11 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
749749
},
750750

751751
// Unknown messages:
752-
wire::Message::Unknown(msg_type) => {
752+
wire::Message::Unknown(msg_type) if msg_type.is_even() => {
753753
// Fail the channel if message is an even, unknown type as per BOLT #1.
754-
if (msg_type & 1) == 0 {
755-
return Err(PeerHandleError{ no_connection_possible: true });
756-
}
754+
return Err(PeerHandleError{ no_connection_possible: true });
757755
},
756+
wire::Message::Unknown(_) => {},
758757
}
759758
}
760759
}

lightning/src/ln/wire.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ pub enum Message {
4747
NodeAnnouncement(msgs::NodeAnnouncement),
4848
ChannelUpdate(msgs::ChannelUpdate),
4949
/// A message that could not be decoded because its type is unknown.
50-
Unknown(u16),
50+
Unknown(MessageType),
51+
}
52+
53+
/// A number identifying a message to determine how it is encoded on the wire.
54+
#[derive(Clone, Copy)]
55+
pub struct MessageType {
56+
number: u16,
5157
}
5258

5359
impl Message {
5460
/// Returns the type that was used to decode the message payload.
55-
pub fn r#type(&self) -> u16 {
61+
pub fn r#type(&self) -> MessageType {
5662
match self {
5763
Message::Init(msg) => msg.r#type(),
5864
Message::Error(msg) => msg.r#type(),
@@ -77,11 +83,24 @@ impl Message {
7783
Message::ChannelAnnouncement(msg) => msg.r#type(),
7884
Message::NodeAnnouncement(msg) => msg.r#type(),
7985
Message::ChannelUpdate(msg) => msg.r#type(),
80-
Message::Unknown(r#type) => *r#type,
86+
Message::Unknown(message_type) => *message_type,
8187
}
8288
}
8389
}
8490

91+
impl MessageType {
92+
/// Returns whether the message type is even, indicating both endpoints must support it.
93+
pub fn is_even(&self) -> bool {
94+
(self.number & 1) == 0
95+
}
96+
}
97+
98+
impl ::std::fmt::Display for MessageType {
99+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
100+
write!(f, "{}", self.number)
101+
}
102+
}
103+
85104
/// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
86105
/// variable-length payload conforming to the type.
87106
///
@@ -161,7 +180,7 @@ pub fn read<R: std::io::Read>(buffer: &mut R) -> Result<Message, msgs::DecodeErr
161180
Ok(Message::ChannelUpdate(Readable::read(buffer)?))
162181
},
163182
_ => {
164-
Ok(Message::Unknown(message_type))
183+
Ok(Message::Unknown(MessageType { number: message_type }))
165184
},
166185
}
167186
}
@@ -189,8 +208,8 @@ pub trait Encode: Writeable {
189208

190209
/// Returns the type identifying the message payload. Convenience method for accessing
191210
/// [`TYPE`](TYPE).
192-
fn r#type(&self) -> u16 {
193-
Self::TYPE
211+
fn r#type(&self) -> MessageType {
212+
MessageType { number: Self::TYPE }
194213
}
195214

196215
/// Writes the type and message payload to the given data buffer.
@@ -345,7 +364,7 @@ mod tests {
345364
let mut reader = ::std::io::Cursor::new(buffer);
346365
let message = read(&mut reader)?;
347366
match message {
348-
Message::Unknown(std::u16::MAX) => Ok(()),
367+
Message::Unknown(MessageType { number: std::u16::MAX }) => Ok(()),
349368
_ => panic!("Expected message type {}; found: {}", std::u16::MAX, message.r#type()),
350369
}
351370
}
@@ -378,4 +397,16 @@ mod tests {
378397
_ => panic!("Expected pong message; found message type: {}", decoded_message.r#type()),
379398
}
380399
}
400+
401+
#[test]
402+
fn is_even_message_type() {
403+
let message = Message::Unknown(MessageType { number: 42 });
404+
assert!(message.r#type().is_even());
405+
}
406+
407+
#[test]
408+
fn is_odd_message_type() {
409+
let message = Message::Unknown(MessageType { number: 43 });
410+
assert!(!message.r#type().is_even());
411+
}
381412
}

0 commit comments

Comments
 (0)