Skip to content

Commit ec40dbe

Browse files
jkczyzTibo-lg
authored andcommitted
f - Refactor Encode and TypedMessage
1 parent 6cb642b commit ec40dbe

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

lightning/src/ln/peer_handler.rs

+4-4
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;
@@ -82,8 +82,8 @@ impl Deref for IgnoringMessageHandler {
8282
fn deref(&self) -> &Self { self }
8383
}
8484

85-
impl TypedMessage for () {
86-
fn msg_type(&self) -> u16 {
85+
impl wire::Type for () {
86+
fn type_id(&self) -> MessageType {
8787
// We should never call this for `DummyCustomType`
8888
unreachable!();
8989
}
@@ -721,7 +721,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
721721
}
722722

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

lightning/src/ln/wire.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use util::ser::{Readable, Writeable, Writer};
2020
/// decoders.
2121
pub trait CustomMessageReader {
2222
/// The type of the message decoded by the implementation.
23-
type CustomMessage: core::fmt::Debug + TypedMessage + Writeable;
23+
type CustomMessage: core::fmt::Debug + Type + Writeable;
2424
/// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
2525
/// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
2626
/// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
@@ -32,7 +32,7 @@ pub trait CustomMessageReader {
3232
/// variant contains a message from [`msgs`] or otherwise the message type if unknown.
3333
#[allow(missing_docs)]
3434
#[derive(Debug)]
35-
pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
35+
pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
3636
Init(msgs::Init),
3737
Error(msgs::ErrorMessage),
3838
Ping(msgs::Ping),
@@ -72,7 +72,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
7272
#[derive(Clone, Copy, Debug)]
7373
pub struct MessageType(u16);
7474

75-
impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
75+
impl<T> Message<T> where T: core::fmt::Debug + Type {
7676
#[allow(dead_code)] // This method is only used in tests
7777
/// Returns the type that was used to decode the message payload.
7878
pub fn type_id(&self) -> MessageType {
@@ -106,7 +106,7 @@ impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
106106
&Message::ReplyChannelRange(ref msg) => msg.type_id(),
107107
&Message::GossipTimestampFilter(ref msg) => msg.type_id(),
108108
&Message::Unknown(type_id) => type_id,
109-
&Message::Custom(ref msg) => MessageType(msg.msg_type()),
109+
&Message::Custom(ref msg) => msg.type_id(),
110110
}
111111
}
112112
}
@@ -135,7 +135,7 @@ pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
135135
custom_reader: &H,
136136
) -> Result<Message<T>, msgs::DecodeError>
137137
where
138-
T: core::fmt::Debug + TypedMessage + Writeable,
138+
T: core::fmt::Debug + Type + Writeable,
139139
H::Target: CustomMessageReader<CustomMessage = T>,
140140
{
141141
let message_type = <u16 as Readable>::read(buffer)?;
@@ -240,39 +240,32 @@ where
240240
/// # Errors
241241
///
242242
/// Returns an I/O error if the write could not be completed.
243-
pub(crate) fn write<M: TypedMessage + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
244-
message.msg_type().write(buffer)?;
243+
pub(crate) fn write<M: Type + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), io::Error> {
244+
message.type_id().0.write(buffer)?;
245245
message.write(buffer)
246246
}
247247

248-
pub(crate) mod encode {
249-
use super::*;
250-
/// Defines a type-identified encoding for sending messages over the wire.
251-
///
252-
/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
248+
mod encode {
249+
/// Defines a constant type identifier for reading messages from the wire.
253250
pub trait Encode {
254251
/// The type identifying the message payload.
255252
const TYPE: u16;
256-
257-
/// Returns the type identifying the message payload. Convenience method for accessing
258-
/// [`Self::TYPE`].
259-
fn type_id(&self) -> MessageType {
260-
MessageType(Self::TYPE)
261-
}
262253
}
263254
}
264255

265256
pub(crate) use self::encode::Encode;
266257

267-
/// A message that has an associated type id.
268-
pub trait TypedMessage {
269-
/// The type id for the implementing message.
270-
fn msg_type(&self) -> u16;
258+
/// Defines a type identifier for sending messages over the wire.
259+
///
260+
/// Messages implementing this trait specify a type and must be [`Writeable`].
261+
pub trait Type {
262+
/// Returns the type identifying the message payload.
263+
fn type_id(&self) -> MessageType;
271264
}
272265

273-
impl<T> TypedMessage for T where T: Encode {
274-
fn msg_type(&self) -> u16 {
275-
T::TYPE
266+
impl<T> Type for T where T: Encode {
267+
fn type_id(&self) -> MessageType {
268+
MessageType(T::TYPE)
276269
}
277270
}
278271

@@ -555,9 +548,9 @@ mod tests {
555548

556549
const CUSTOM_MESSAGE_TYPE : u16 = 9000;
557550

558-
impl TypedMessage for TestCustomMessage {
559-
fn msg_type(&self) -> u16 {
560-
CUSTOM_MESSAGE_TYPE
551+
impl Type for TestCustomMessage {
552+
fn type_id(&self) -> MessageType {
553+
MessageType(CUSTOM_MESSAGE_TYPE)
561554
}
562555
}
563556

@@ -591,7 +584,7 @@ mod tests {
591584
let decoded_msg = read(&mut reader, &&TestCustomMessageReader{}).unwrap();
592585
match decoded_msg {
593586
Message::Custom(custom) => {
594-
assert_eq!(custom.msg_type(), CUSTOM_MESSAGE_TYPE);
587+
assert_eq!(custom.type_id().0, CUSTOM_MESSAGE_TYPE);
595588
assert_eq!(custom, TestCustomMessage {});
596589
},
597590
_ => panic!("Expected custom message, found message type: {}", decoded_msg.type_id()),

0 commit comments

Comments
 (0)