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.
This commit is contained in:
Jeffrey Czyz 2020-01-24 06:43:58 -08:00
parent 7de9f5278c
commit 326076f5e8
2 changed files with 40 additions and 10 deletions

View file

@ -773,12 +773,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
}, },
// Unknown messages: // Unknown messages:
wire::Message::Unknown(msg_type) => { wire::Message::Unknown(msg_type) if msg_type.is_even() => {
// Fail the channel if message is an even, unknown type as per BOLT #1. // Fail the channel if message is an even, unknown type as per BOLT #1.
if (msg_type & 1) == 0 {
return Err(PeerHandleError{ no_connection_possible: true }); return Err(PeerHandleError{ no_connection_possible: true });
}
}, },
wire::Message::Unknown(_) => {},
} }
} }
} }

View file

@ -47,12 +47,18 @@ pub enum Message {
NodeAnnouncement(msgs::NodeAnnouncement), NodeAnnouncement(msgs::NodeAnnouncement),
ChannelUpdate(msgs::ChannelUpdate), ChannelUpdate(msgs::ChannelUpdate),
/// A message that could not be decoded because its type is unknown. /// A message that could not be decoded because its type is unknown.
Unknown(u16), Unknown(MessageType),
}
/// A number identifying a message to determine how it is encoded on the wire.
#[derive(Clone, Copy)]
pub struct MessageType {
number: u16,
} }
impl Message { impl Message {
/// Returns the type that was used to decode the message payload. /// Returns the type that was used to decode the message payload.
pub fn type_id(&self) -> u16 { pub fn type_id(&self) -> MessageType {
match self { match self {
&Message::Init(ref msg) => msg.type_id(), &Message::Init(ref msg) => msg.type_id(),
&Message::Error(ref msg) => msg.type_id(), &Message::Error(ref msg) => msg.type_id(),
@ -82,6 +88,19 @@ impl Message {
} }
} }
impl MessageType {
/// Returns whether the message type is even, indicating both endpoints must support it.
pub fn is_even(&self) -> bool {
(self.number & 1) == 0
}
}
impl ::std::fmt::Display for MessageType {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.number)
}
}
/// Reads a message from the data buffer consisting of a 2-byte big-endian type and a /// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
/// variable-length payload conforming to the type. /// variable-length payload conforming to the type.
/// ///
@ -161,7 +180,7 @@ pub fn read<R: ::std::io::Read>(buffer: &mut R) -> Result<Message, msgs::DecodeE
Ok(Message::ChannelUpdate(Readable::read(buffer)?)) Ok(Message::ChannelUpdate(Readable::read(buffer)?))
}, },
_ => { _ => {
Ok(Message::Unknown(message_type)) Ok(Message::Unknown(MessageType { number: message_type }))
}, },
} }
} }
@ -189,8 +208,8 @@ pub trait Encode {
/// Returns the type identifying the message payload. Convenience method for accessing /// Returns the type identifying the message payload. Convenience method for accessing
/// [`TYPE`](TYPE). /// [`TYPE`](TYPE).
fn type_id(&self) -> u16 { fn type_id(&self) -> MessageType {
Self::TYPE MessageType { number: Self::TYPE }
} }
} }
@ -339,7 +358,7 @@ mod tests {
let mut reader = ::std::io::Cursor::new(buffer); let mut reader = ::std::io::Cursor::new(buffer);
let message = read(&mut reader).unwrap(); let message = read(&mut reader).unwrap();
match message { match message {
Message::Unknown(::std::u16::MAX) => (), Message::Unknown(MessageType { number: ::std::u16::MAX }) => (),
_ => panic!("Expected message type {}; found: {}", ::std::u16::MAX, message.type_id()), _ => panic!("Expected message type {}; found: {}", ::std::u16::MAX, message.type_id()),
} }
} }
@ -372,4 +391,16 @@ mod tests {
_ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()), _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
} }
} }
#[test]
fn is_even_message_type() {
let message = Message::Unknown(MessageType { number: 42 });
assert!(message.type_id().is_even());
}
#[test]
fn is_odd_message_type() {
let message = Message::Unknown(MessageType { number: 43 });
assert!(!message.type_id().is_even());
}
} }