Skip to content

Commit b6f743f

Browse files
jkczyzTibo-lg
authored andcommitted
f - Refactor Encode and TypedMessage
1 parent 3b48b4c commit b6f743f

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
2424
use util::ser::{VecWriter, Writeable, Writer};
2525
use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
2626
use ln::wire;
27-
use ln::wire::TypedMessage;
27+
use ln::wire::MessageType;
2828
use util::byte_utils;
2929
use util::events::{MessageSendEvent, MessageSendEventsProvider};
3030
use util::logger::Logger;
@@ -85,8 +85,8 @@ pub struct IgnoringCustomMessageHandler{}
8585

8686
type DummyCustomType = ();
8787

88-
impl TypedMessage for DummyCustomType {
89-
fn msg_type(&self) -> u16 {
88+
impl wire::Type for DummyCustomType {
89+
fn type_id(&self) -> MessageType {
9090
// We should never call this for `DummyCustomType`
9191
unreachable!();
9292
}
@@ -728,7 +728,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
728728
}
729729

730730
/// Append a message to a peer's pending outbound/write buffer, and update the map of peers needing sends accordingly.
731-
fn enqueue_message<M: TypedMessage + Writeable + Debug>(&self, peer: &mut Peer, message: &M) {
731+
fn enqueue_message<M: wire::Type + Writeable + Debug>(&self, peer: &mut Peer, message: &M) {
732732
let mut buffer = VecWriter(Vec::new());
733733
wire::write(message, &mut buffer).unwrap(); // crash if the write failed
734734
let encoded_message = buffer.0;

lightning/src/ln/wire.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! The [`Message`] enum returned by [`read()`] wraps the decoded message or the message type (if
1414
//! unknown) to use with pattern matching.
1515
//!
16-
//! Messages implementing the [`Encode`] trait define a message type and can be sent over the wire
16+
//! Messages implementing the [`Type`] trait define a message type and can be sent over the wire
1717
//! using [`write()`].
1818
//!
1919
//! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
@@ -26,7 +26,7 @@ use util::ser::{Readable, Writeable, Writer};
2626
/// decoders.
2727
pub trait CustomMessageReader {
2828
/// The type of the message decoded by the implementation.
29-
type CustomMessage : core::fmt::Debug + TypedMessage + Writeable;
29+
type CustomMessage : core::fmt::Debug + Type + Writeable;
3030
/// Decodes a custom message to `CustomMessageType`. If the given message type is known to the implementation and
3131
/// the message could be decoded, must return `Ok(Some(message))`. If the message type
3232
/// is unknown to the implementation, must return `Ok(None)`. If a decoding error
@@ -38,7 +38,7 @@ pub trait CustomMessageReader {
3838
/// variant contains a message from [`msgs`] or otherwise the message type if unknown.
3939
#[allow(missing_docs)]
4040
#[derive(Debug)]
41-
pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
41+
pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
4242
Init(msgs::Init),
4343
Error(msgs::ErrorMessage),
4444
Ping(msgs::Ping),
@@ -76,7 +76,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
7676
#[derive(Clone, Copy, Debug)]
7777
pub struct MessageType(u16);
7878

79-
impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
79+
impl<T> Message<T> where T: core::fmt::Debug + Type {
8080
#[allow(dead_code)] // This method is only used in tests
8181
/// Returns the type that was used to decode the message payload.
8282
pub fn type_id(&self) -> MessageType {
@@ -110,7 +110,7 @@ impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
110110
&Message::ReplyChannelRange(ref msg) => msg.type_id(),
111111
&Message::GossipTimestampFilter(ref msg) => msg.type_id(),
112112
&Message::Unknown(type_id) => type_id,
113-
&Message::Custom(ref msg) => MessageType(msg.msg_type()),
113+
&Message::Custom(ref msg) => msg.type_id(),
114114
}
115115
}
116116
}
@@ -139,7 +139,7 @@ pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
139139
custom_reader: &H
140140
) -> Result<Message<T>, msgs::DecodeError>
141141
where
142-
T: core::fmt::Debug + TypedMessage + Writeable,
142+
T: core::fmt::Debug + Type + Writeable,
143143
H::Target: CustomMessageReader<CustomMessage = T>
144144
{
145145
let message_type = <u16 as Readable>::read(buffer)?;
@@ -244,39 +244,32 @@ where
244244
/// # Errors
245245
///
246246
/// Returns an I/O error if the write could not be completed.
247-
pub fn write<M: TypedMessage + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
248-
message.msg_type().write(buffer)?;
247+
pub fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
248+
message.type_id().0.write(buffer)?;
249249
message.write(buffer)
250250
}
251251

252-
pub(crate) mod encode {
253-
use super::*;
254-
/// Defines a type-identified encoding for sending messages over the wire.
255-
///
256-
/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
252+
mod encode {
253+
/// Defines a constant type identifier for reading messages from the wire.
257254
pub trait Encode {
258255
/// The type identifying the message payload.
259256
const TYPE: u16;
260-
261-
/// Returns the type identifying the message payload. Convenience method for accessing
262-
/// [`Self::TYPE`].
263-
fn type_id(&self) -> MessageType {
264-
MessageType(Self::TYPE)
265-
}
266257
}
267258
}
268259

269260
pub(crate) use self::encode::Encode;
270261

271-
/// A message that has an associated type id.
272-
pub trait TypedMessage {
273-
/// The type id for the implementing message.
274-
fn msg_type(&self) -> u16;
262+
/// Defines a type identifier for sending messages over the wire.
263+
///
264+
/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
265+
pub trait Type {
266+
/// Returns the type identifying the message payload.
267+
fn type_id(&self) -> MessageType;
275268
}
276269

277-
impl<T> TypedMessage for T where T: Encode {
278-
fn msg_type(&self) -> u16 {
279-
T::TYPE
270+
impl<T> Type for T where T: Encode {
271+
fn type_id(&self) -> MessageType {
272+
MessageType(T::TYPE)
280273
}
281274
}
282275

@@ -559,9 +552,9 @@ mod tests {
559552

560553
const CUSTOM_MESSAGE_TYPE : u16 = 9000;
561554

562-
impl TypedMessage for TestCustomMessage {
563-
fn msg_type(&self) -> u16 {
564-
CUSTOM_MESSAGE_TYPE
555+
impl Type for TestCustomMessage {
556+
fn type_id(&self) -> MessageType {
557+
MessageType(CUSTOM_MESSAGE_TYPE)
565558
}
566559
}
567560

@@ -598,7 +591,7 @@ mod tests {
598591
let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap();
599592
match decoded_msg {
600593
Message::Custom(custom) => {
601-
assert_eq!(custom.msg_type(), CUSTOM_MESSAGE_TYPE);
594+
assert_eq!(custom.type_id().0, CUSTOM_MESSAGE_TYPE);
602595
assert_eq!(custom, TestCustomMessage {});
603596
},
604597
_ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),

0 commit comments

Comments
 (0)