From 4b62c412a651de515b40dd2740476a4b29922c0c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:11:02 +0000 Subject: [PATCH 1/7] Reduce overuse of `where` clauses in `wire.rs` rustfmt blows up `where` clauses even for trivial lines, so we should generally avoid them where possible. Here we remove some extra ones from `wire.rs`. --- lightning/src/ln/wire.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index c42cc4e4c92..2932018d5d1 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -47,7 +47,7 @@ impl TestEq for T {} #[allow(missing_docs)] #[derive(Debug)] #[cfg_attr(test, derive(PartialEq))] -pub(crate) enum Message where T: core::fmt::Debug + Type + TestEq { +pub(crate) enum Message { Init(msgs::Init), Error(msgs::ErrorMessage), Warning(msgs::WarningMessage), @@ -105,7 +105,7 @@ pub(crate) enum Message where T: core::fmt::Debug + Type + TestEq { Custom(T), } -impl Writeable for Message where T: core::fmt::Debug + Type + TestEq { +impl Writeable for Message { fn write(&self, writer: &mut W) -> Result<(), io::Error> { match self { &Message::Init(ref msg) => msg.write(writer), @@ -164,7 +164,7 @@ impl Writeable for Message where T: core::fmt::Debug + Type + TestEq { } } -impl Type for Message where T: core::fmt::Debug + Type + TestEq { +impl Type for Message { /// Returns the type that was used to decode the message payload. fn type_id(&self) -> u16 { match self { @@ -224,7 +224,7 @@ impl Type for Message where T: core::fmt::Debug + Type + TestEq { } } -impl Message where T: core::fmt::Debug + Type + TestEq { +impl Message { /// Returns whether the message's type is even, indicating both endpoints must support it. pub fn is_even(&self) -> bool { (self.type_id() & 1) == 0 @@ -447,12 +447,12 @@ impl Type for () { } #[cfg(test)] -impl Type for T where T: Encode { +impl Type for T { fn type_id(&self) -> u16 { T::TYPE } } #[cfg(not(test))] -impl Type for T where T: Encode { +impl Type for T { fn type_id(&self) -> u16 { T::TYPE } } From 88a2fb6e5957bab9a4281b3be5240755c5bc9880 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:18:58 +0000 Subject: [PATCH 2/7] Run rustfmt on `lightning/src/ln/wire.rs` --- lightning/src/ln/wire.rs | 296 ++++++++++++++++++--------------------- 1 file changed, 137 insertions(+), 159 deletions(-) diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 2932018d5d1..f56f85eaf0f 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -25,14 +25,16 @@ pub trait CustomMessageReader { /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error. - fn read(&self, message_type: u16, buffer: &mut R) -> Result, msgs::DecodeError>; + fn read( + &self, message_type: u16, buffer: &mut R, + ) -> Result, msgs::DecodeError>; } // TestEq is a dummy trait which requires PartialEq when built in testing, and otherwise is // blanket-implemented for all types. #[cfg(test)] -pub trait TestEq : PartialEq {} +pub trait TestEq: PartialEq {} #[cfg(test)] impl TestEq for T {} @@ -41,7 +43,6 @@ pub(crate) trait TestEq {} #[cfg(not(test))] impl TestEq for T {} - /// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each /// variant contains a message from [`msgs`] or otherwise the message type if unknown. #[allow(missing_docs)] @@ -158,7 +159,7 @@ impl Writeable for Message { &Message::QueryChannelRange(ref msg) => msg.write(writer), &Message::ReplyChannelRange(ref msg) => msg.write(writer), &Message::GossipTimestampFilter(ref msg) => msg.write(writer), - &Message::Unknown(_) => { Ok(()) }, + &Message::Unknown(_) => Ok(()), &Message::Custom(ref msg) => msg.write(writer), } } @@ -237,8 +238,10 @@ impl Message { /// # Errors /// /// Returns an error if the message payload could not be decoded as the specified type. -pub(crate) fn read(buffer: &mut R, custom_reader: H) --> Result, (msgs::DecodeError, Option)> where +pub(crate) fn read( + buffer: &mut R, custom_reader: H, +) -> Result, (msgs::DecodeError, Option)> +where T: core::fmt::Debug + Type + Writeable, H::Target: CustomMessageReader, { @@ -246,153 +249,75 @@ pub(crate) fn read(buffer: &mut R, custom_r do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type))) } -fn do_read(buffer: &mut R, message_type: u16, custom_reader: H) --> Result, msgs::DecodeError> where +fn do_read( + buffer: &mut R, message_type: u16, custom_reader: H, +) -> Result, msgs::DecodeError> +where T: core::fmt::Debug + Type + Writeable, H::Target: CustomMessageReader, { match message_type { - msgs::Init::TYPE => { - Ok(Message::Init(Readable::read(buffer)?)) - }, - msgs::ErrorMessage::TYPE => { - Ok(Message::Error(Readable::read(buffer)?)) - }, - msgs::WarningMessage::TYPE => { - Ok(Message::Warning(Readable::read(buffer)?)) - }, - msgs::Ping::TYPE => { - Ok(Message::Ping(Readable::read(buffer)?)) - }, - msgs::Pong::TYPE => { - Ok(Message::Pong(Readable::read(buffer)?)) - }, - msgs::PeerStorage::TYPE => { - Ok(Message::PeerStorage(Readable::read(buffer)?)) - }, + msgs::Init::TYPE => Ok(Message::Init(Readable::read(buffer)?)), + msgs::ErrorMessage::TYPE => Ok(Message::Error(Readable::read(buffer)?)), + msgs::WarningMessage::TYPE => Ok(Message::Warning(Readable::read(buffer)?)), + msgs::Ping::TYPE => Ok(Message::Ping(Readable::read(buffer)?)), + msgs::Pong::TYPE => Ok(Message::Pong(Readable::read(buffer)?)), + msgs::PeerStorage::TYPE => Ok(Message::PeerStorage(Readable::read(buffer)?)), msgs::PeerStorageRetrieval::TYPE => { Ok(Message::PeerStorageRetrieval(Readable::read(buffer)?)) }, - msgs::OpenChannel::TYPE => { - Ok(Message::OpenChannel(Readable::read(buffer)?)) - }, - msgs::OpenChannelV2::TYPE => { - Ok(Message::OpenChannelV2(Readable::read(buffer)?)) - }, - msgs::AcceptChannel::TYPE => { - Ok(Message::AcceptChannel(Readable::read(buffer)?)) - }, - msgs::AcceptChannelV2::TYPE => { - Ok(Message::AcceptChannelV2(Readable::read(buffer)?)) - }, - msgs::FundingCreated::TYPE => { - Ok(Message::FundingCreated(Readable::read(buffer)?)) - }, - msgs::FundingSigned::TYPE => { - Ok(Message::FundingSigned(Readable::read(buffer)?)) - }, + msgs::OpenChannel::TYPE => Ok(Message::OpenChannel(Readable::read(buffer)?)), + msgs::OpenChannelV2::TYPE => Ok(Message::OpenChannelV2(Readable::read(buffer)?)), + msgs::AcceptChannel::TYPE => Ok(Message::AcceptChannel(Readable::read(buffer)?)), + msgs::AcceptChannelV2::TYPE => Ok(Message::AcceptChannelV2(Readable::read(buffer)?)), + msgs::FundingCreated::TYPE => Ok(Message::FundingCreated(Readable::read(buffer)?)), + msgs::FundingSigned::TYPE => Ok(Message::FundingSigned(Readable::read(buffer)?)), #[cfg(splicing)] - msgs::SpliceInit::TYPE => { - Ok(Message::SpliceInit(Readable::read(buffer)?)) - }, - msgs::Stfu::TYPE => { - Ok(Message::Stfu(Readable::read(buffer)?)) - }, + msgs::SpliceInit::TYPE => Ok(Message::SpliceInit(Readable::read(buffer)?)), + msgs::Stfu::TYPE => Ok(Message::Stfu(Readable::read(buffer)?)), #[cfg(splicing)] - msgs::SpliceAck::TYPE => { - Ok(Message::SpliceAck(Readable::read(buffer)?)) - }, + msgs::SpliceAck::TYPE => Ok(Message::SpliceAck(Readable::read(buffer)?)), #[cfg(splicing)] - msgs::SpliceLocked::TYPE => { - Ok(Message::SpliceLocked(Readable::read(buffer)?)) - }, - msgs::TxAddInput::TYPE => { - Ok(Message::TxAddInput(Readable::read(buffer)?)) - }, - msgs::TxAddOutput::TYPE => { - Ok(Message::TxAddOutput(Readable::read(buffer)?)) - }, - msgs::TxRemoveInput::TYPE => { - Ok(Message::TxRemoveInput(Readable::read(buffer)?)) - }, - msgs::TxRemoveOutput::TYPE => { - Ok(Message::TxRemoveOutput(Readable::read(buffer)?)) - }, - msgs::TxComplete::TYPE => { - Ok(Message::TxComplete(Readable::read(buffer)?)) - }, - msgs::TxSignatures::TYPE => { - Ok(Message::TxSignatures(Readable::read(buffer)?)) - }, - msgs::TxInitRbf::TYPE => { - Ok(Message::TxInitRbf(Readable::read(buffer)?)) - }, - msgs::TxAckRbf::TYPE => { - Ok(Message::TxAckRbf(Readable::read(buffer)?)) - }, - msgs::TxAbort::TYPE => { - Ok(Message::TxAbort(Readable::read(buffer)?)) - }, - msgs::ChannelReady::TYPE => { - Ok(Message::ChannelReady(Readable::read(buffer)?)) - }, - msgs::Shutdown::TYPE => { - Ok(Message::Shutdown(Readable::read(buffer)?)) - }, - msgs::ClosingSigned::TYPE => { - Ok(Message::ClosingSigned(Readable::read(buffer)?)) - }, - msgs::OnionMessage::TYPE => { - Ok(Message::OnionMessage(Readable::read(buffer)?)) - }, - msgs::UpdateAddHTLC::TYPE => { - Ok(Message::UpdateAddHTLC(Readable::read(buffer)?)) - }, - msgs::UpdateFulfillHTLC::TYPE => { - Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?)) - }, - msgs::UpdateFailHTLC::TYPE => { - Ok(Message::UpdateFailHTLC(Readable::read(buffer)?)) - }, + msgs::SpliceLocked::TYPE => Ok(Message::SpliceLocked(Readable::read(buffer)?)), + msgs::TxAddInput::TYPE => Ok(Message::TxAddInput(Readable::read(buffer)?)), + msgs::TxAddOutput::TYPE => Ok(Message::TxAddOutput(Readable::read(buffer)?)), + msgs::TxRemoveInput::TYPE => Ok(Message::TxRemoveInput(Readable::read(buffer)?)), + msgs::TxRemoveOutput::TYPE => Ok(Message::TxRemoveOutput(Readable::read(buffer)?)), + msgs::TxComplete::TYPE => Ok(Message::TxComplete(Readable::read(buffer)?)), + msgs::TxSignatures::TYPE => Ok(Message::TxSignatures(Readable::read(buffer)?)), + msgs::TxInitRbf::TYPE => Ok(Message::TxInitRbf(Readable::read(buffer)?)), + msgs::TxAckRbf::TYPE => Ok(Message::TxAckRbf(Readable::read(buffer)?)), + msgs::TxAbort::TYPE => Ok(Message::TxAbort(Readable::read(buffer)?)), + msgs::ChannelReady::TYPE => Ok(Message::ChannelReady(Readable::read(buffer)?)), + msgs::Shutdown::TYPE => Ok(Message::Shutdown(Readable::read(buffer)?)), + msgs::ClosingSigned::TYPE => Ok(Message::ClosingSigned(Readable::read(buffer)?)), + msgs::OnionMessage::TYPE => Ok(Message::OnionMessage(Readable::read(buffer)?)), + msgs::UpdateAddHTLC::TYPE => Ok(Message::UpdateAddHTLC(Readable::read(buffer)?)), + msgs::UpdateFulfillHTLC::TYPE => Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?)), + msgs::UpdateFailHTLC::TYPE => Ok(Message::UpdateFailHTLC(Readable::read(buffer)?)), msgs::UpdateFailMalformedHTLC::TYPE => { Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?)) }, - msgs::CommitmentSigned::TYPE => { - Ok(Message::CommitmentSigned(Readable::read(buffer)?)) - }, - msgs::RevokeAndACK::TYPE => { - Ok(Message::RevokeAndACK(Readable::read(buffer)?)) - }, - msgs::UpdateFee::TYPE => { - Ok(Message::UpdateFee(Readable::read(buffer)?)) - }, - msgs::ChannelReestablish::TYPE => { - Ok(Message::ChannelReestablish(Readable::read(buffer)?)) - }, + msgs::CommitmentSigned::TYPE => Ok(Message::CommitmentSigned(Readable::read(buffer)?)), + msgs::RevokeAndACK::TYPE => Ok(Message::RevokeAndACK(Readable::read(buffer)?)), + msgs::UpdateFee::TYPE => Ok(Message::UpdateFee(Readable::read(buffer)?)), + msgs::ChannelReestablish::TYPE => Ok(Message::ChannelReestablish(Readable::read(buffer)?)), msgs::AnnouncementSignatures::TYPE => { Ok(Message::AnnouncementSignatures(Readable::read(buffer)?)) }, msgs::ChannelAnnouncement::TYPE => { Ok(Message::ChannelAnnouncement(Readable::read(buffer)?)) }, - msgs::NodeAnnouncement::TYPE => { - Ok(Message::NodeAnnouncement(Readable::read(buffer)?)) - }, - msgs::ChannelUpdate::TYPE => { - Ok(Message::ChannelUpdate(Readable::read(buffer)?)) - }, + msgs::NodeAnnouncement::TYPE => Ok(Message::NodeAnnouncement(Readable::read(buffer)?)), + msgs::ChannelUpdate::TYPE => Ok(Message::ChannelUpdate(Readable::read(buffer)?)), msgs::QueryShortChannelIds::TYPE => { Ok(Message::QueryShortChannelIds(Readable::read(buffer)?)) }, msgs::ReplyShortChannelIdsEnd::TYPE => { Ok(Message::ReplyShortChannelIdsEnd(Readable::read(buffer)?)) }, - msgs::QueryChannelRange::TYPE => { - Ok(Message::QueryChannelRange(Readable::read(buffer)?)) - }, - msgs::ReplyChannelRange::TYPE => { - Ok(Message::ReplyChannelRange(Readable::read(buffer)?)) - } + msgs::QueryChannelRange::TYPE => Ok(Message::QueryChannelRange(Readable::read(buffer)?)), + msgs::ReplyChannelRange::TYPE => Ok(Message::ReplyChannelRange(Readable::read(buffer)?)), msgs::GossipTimestampFilter::TYPE => { Ok(Message::GossipTimestampFilter(Readable::read(buffer)?)) }, @@ -412,7 +337,9 @@ fn do_read(buffer: &mut R, message_type: u1 /// # Errors /// /// Returns an I/O error if the write could not be completed. -pub(crate) fn write(message: &M, buffer: &mut W) -> Result<(), io::Error> { +pub(crate) fn write( + message: &M, buffer: &mut W, +) -> Result<(), io::Error> { message.type_id().write(buffer)?; message.write(buffer) } @@ -443,17 +370,23 @@ pub trait Type: core::fmt::Debug + Writeable + PartialEq { #[cfg(any(feature = "_test_utils", fuzzing, test))] impl Type for () { - fn type_id(&self) -> u16 { unreachable!(); } + fn type_id(&self) -> u16 { + unreachable!(); + } } #[cfg(test)] impl Type for T { - fn type_id(&self) -> u16 { T::TYPE } + fn type_id(&self) -> u16 { + T::TYPE + } } #[cfg(not(test))] impl Type for T { - fn type_id(&self) -> u16 { T::TYPE } + fn type_id(&self) -> u16 { + T::TYPE + } } impl Encode for msgs::Stfu { @@ -648,8 +581,8 @@ impl Encode for msgs::GossipTimestampFilter { #[cfg(test)] mod tests { use super::*; - use crate::prelude::*; use crate::ln::peer_handler::IgnoringMessageHandler; + use crate::prelude::*; // Big-endian wire encoding of Pong message (type = 19, byteslen = 2). const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8]; @@ -658,35 +591,35 @@ mod tests { fn read_empty_buffer() { let buffer = []; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler {}).is_err()); } #[test] fn read_incomplete_type() { let buffer = &ENCODED_PONG[..1]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler {}).is_err()); } #[test] fn read_empty_payload() { let buffer = &ENCODED_PONG[..2]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler {}).is_err()); } #[test] fn read_invalid_message() { let buffer = &ENCODED_PONG[..4]; let mut reader = io::Cursor::new(buffer); - assert!(read(&mut reader, &IgnoringMessageHandler{}).is_err()); + assert!(read(&mut reader, &IgnoringMessageHandler {}).is_err()); } #[test] fn read_known_message() { let buffer = &ENCODED_PONG[..]; let mut reader = io::Cursor::new(buffer); - let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let message = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match message { Message::Pong(_) => (), _ => panic!("Expected pong message; found message type: {}", message.type_id()), @@ -697,7 +630,7 @@ mod tests { fn read_unknown_message() { let buffer = &::core::u16::MAX.to_be_bytes(); let mut reader = io::Cursor::new(buffer); - let message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let message = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match message { Message::Unknown(::core::u16::MAX) => (), _ => panic!("Expected message type {}; found: {}", ::core::u16::MAX, message.type_id()), @@ -723,7 +656,7 @@ mod tests { assert!(write(&message, &mut buffer).is_ok()); let mut reader = io::Cursor::new(buffer); - let decoded_message = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let decoded_message = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match decoded_message { Message::Pong(msgs::Pong { byteslen: 2u16 }) => (), Message::Pong(msgs::Pong { byteslen }) => { @@ -755,13 +688,17 @@ mod tests { #[test] fn read_clightning_init_msg() { // Taken from c-lightning v0.8.0. - 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]; + 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, + ]; check_init_msg(buffer, true); } fn check_init_msg(buffer: Vec, expect_unknown: bool) { let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match decoded_msg { Message::Init(msgs::Init { features, .. }) => { assert!(features.supports_variable_length_onion()); @@ -771,45 +708,88 @@ mod tests { assert!(!features.requires_unknown_bits()); assert!(!features.initial_routing_sync()); }, - _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id()) + _ => panic!("Expected init message, found message type: {}", decoded_msg.type_id()), } } #[test] fn read_lnd_node_announcement() { // Taken from lnd v0.9.0-beta. - 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]; + 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, + ]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match decoded_msg { - Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => { + Message::NodeAnnouncement(msgs::NodeAnnouncement { + contents: msgs::UnsignedNodeAnnouncement { features, .. }, + .. + }) => { assert!(features.supports_variable_length_onion()); assert!(features.supports_upfront_shutdown_script()); assert!(features.supports_gossip_queries()); assert!(!features.requires_unknown_bits()); }, - _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + _ => { + panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + }, } } #[test] fn read_lnd_chan_announcement() { // Taken from lnd v0.9.0-beta. - 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]; + 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, + ]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); + let decoded_msg = read(&mut reader, &IgnoringMessageHandler {}).unwrap(); match decoded_msg { - Message::ChannelAnnouncement(msgs::ChannelAnnouncement { contents: msgs::UnsignedChannelAnnouncement { features, ..}, ..}) => { + Message::ChannelAnnouncement(msgs::ChannelAnnouncement { + contents: msgs::UnsignedChannelAnnouncement { features, .. }, + .. + }) => { assert!(!features.requires_unknown_bits()); }, - _ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + _ => { + panic!("Expected node announcement, found message type: {}", decoded_msg.type_id()) + }, } } #[derive(Eq, PartialEq, Debug)] struct TestCustomMessage {} - const CUSTOM_MESSAGE_TYPE : u16 = 9000; + const CUSTOM_MESSAGE_TYPE: u16 = 9000; impl Type for TestCustomMessage { fn type_id(&self) -> u16 { @@ -828,12 +808,10 @@ mod tests { impl CustomMessageReader for TestCustomMessageReader { type CustomMessage = TestCustomMessage; fn read( - &self, - message_type: u16, - _: &mut R + &self, message_type: u16, _: &mut R, ) -> Result, msgs::DecodeError> { if message_type == CUSTOM_MESSAGE_TYPE { - return Ok(Some(TestCustomMessage{})); + return Ok(Some(TestCustomMessage {})); } Ok(None) @@ -844,7 +822,7 @@ mod tests { fn read_custom_message() { let buffer = vec![35, 40]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap(); + let decoded_msg = read(&mut reader, &TestCustomMessageReader {}).unwrap(); match decoded_msg { Message::Custom(custom) => { assert_eq!(custom.type_id(), CUSTOM_MESSAGE_TYPE); @@ -858,7 +836,7 @@ mod tests { fn read_with_custom_reader_unknown_message_type() { let buffer = vec![35, 42]; let mut reader = io::Cursor::new(buffer); - let decoded_msg = read(&mut reader, &TestCustomMessageReader{}).unwrap(); + let decoded_msg = read(&mut reader, &TestCustomMessageReader {}).unwrap(); match decoded_msg { Message::Unknown(_) => {}, _ => panic!("Expected unknown message, found message type: {}", decoded_msg.type_id()), @@ -869,7 +847,7 @@ mod tests { fn custom_reader_unknown_message_type() { let buffer = Vec::new(); let mut reader = io::Cursor::new(buffer); - let res = TestCustomMessageReader{}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap(); + let res = TestCustomMessageReader {}.read(CUSTOM_MESSAGE_TYPE + 1, &mut reader).unwrap(); assert!(res.is_none()); } } From a979d08e99c8e1612b8ae5f4925e72218b8418af Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:25:56 +0000 Subject: [PATCH 3/7] Remove `lightning/src/ln/wire.rs` from rustfmt exclusions --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index 7899a65ee67..4c7ac636db2 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -37,7 +37,6 @@ lightning/src/ln/reorg_tests.rs lightning/src/ln/script.rs lightning/src/ln/shutdown_tests.rs lightning/src/ln/types.rs -lightning/src/ln/wire.rs lightning/src/routing/mod.rs lightning/src/routing/router.rs lightning/src/routing/scoring.rs From 2ffcdf9d7f41e05ca0c19388e0561e4957d7103f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:49:45 +0000 Subject: [PATCH 4/7] Clean up `types.rs` hex deser to use more intermediate variables ...preventing rustfmt from making a mockery of our code. --- lightning/src/ln/types.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lightning/src/ln/types.rs b/lightning/src/ln/types.rs index 48b663afed7..b20a3568e0a 100644 --- a/lightning/src/ln/types.rs +++ b/lightning/src/ln/types.rs @@ -133,7 +133,6 @@ mod tests { use bitcoin::hashes::{ Hash as _, HashEngine as _, - hex::FromHex as _, sha256::Hash as Sha256, }; use bitcoin::secp256k1::PublicKey; @@ -146,10 +145,13 @@ mod tests { use crate::prelude::*; use crate::io; + use core::str::FromStr; + #[test] fn test_channel_id_v1_from_funding_txid() { let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1); - assert_eq!(channel_id.0.as_hex().to_string(), "0202020202020202020202020202020202020202020202020202020202020203"); + let expected = "0202020202020202020202020202020202020202020202020202020202020203"; + assert_eq!(channel_id.0.as_hex().to_string(), expected); } #[test] @@ -184,14 +186,17 @@ mod tests { #[test] fn test_channel_id_display() { let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1); - assert_eq!(format!("{}", &channel_id), "0202020202020202020202020202020202020202020202020202020202020203"); + let expected = "0202020202020202020202020202020202020202020202020202020202020203"; + assert_eq!(format!("{}", &channel_id), expected); } #[test] fn test_channel_id_v2_from_basepoints() { // Ours greater than theirs - let ours = RevocationBasepoint(PublicKey::from_slice(&>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap()); - let theirs = RevocationBasepoint(PublicKey::from_slice(&>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()); + let our_pk = "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c"; + let ours = RevocationBasepoint(PublicKey::from_str(&our_pk).unwrap()); + let their_pk = "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619"; + let theirs = RevocationBasepoint(PublicKey::from_str(&their_pk).unwrap()); let mut engine = Sha256::engine(); engine.input(&theirs.0.serialize()); @@ -201,8 +206,10 @@ mod tests { assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id); // Theirs greater than ours - let ours = RevocationBasepoint(PublicKey::from_slice(&>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap()); - let theirs = RevocationBasepoint(PublicKey::from_slice(&>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap()); + let our_pk = "027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007"; + let ours = RevocationBasepoint(PublicKey::from_str(&our_pk).unwrap()); + let their_pk = "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619"; + let theirs = RevocationBasepoint(PublicKey::from_str(&their_pk).unwrap()); let mut engine = Sha256::engine(); engine.input(&ours.0.serialize()); From 36f713dfeb02984e1d702ae0a836aba985e70599 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:50:12 +0000 Subject: [PATCH 5/7] Run rustfmt on `lightning/src/ln/types.rs` --- lightning/src/ln/types.rs | 43 +++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/lightning/src/ln/types.rs b/lightning/src/ln/types.rs index b20a3568e0a..e1ce9d0e078 100644 --- a/lightning/src/ln/types.rs +++ b/lightning/src/ln/types.rs @@ -9,22 +9,20 @@ //! Various wrapper types (most around 32-byte arrays) for use in lightning. +use super::channel_keys::RevocationBasepoint; + use crate::chain::transaction::OutPoint; use crate::io; use crate::ln::msgs::DecodeError; use crate::sign::EntropySource; use crate::util::ser::{Readable, Writeable, Writer}; -use super::channel_keys::RevocationBasepoint; #[allow(unused_imports)] use crate::prelude::*; -use bitcoin::hashes::{ - Hash as _, - HashEngine as _, - sha256::Hash as Sha256, -}; +use bitcoin::hashes::{sha256::Hash as Sha256, Hash as _, HashEngine as _}; use bitcoin::hex::display::impl_fmt_traits; + use core::borrow::Borrow; use core::ops::Deref; @@ -56,7 +54,9 @@ impl ChannelId { /// Create a _temporary_ channel ID randomly, based on an entropy source. pub fn temporary_from_entropy_source(entropy_source: &ES) -> Self - where ES::Target: EntropySource { + where + ES::Target: EntropySource, + { Self(entropy_source.get_secure_random_bytes()) } @@ -80,16 +80,11 @@ impl ChannelId { /// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing /// sorted order. pub fn v2_from_revocation_basepoints( - ours: &RevocationBasepoint, - theirs: &RevocationBasepoint, + ours: &RevocationBasepoint, theirs: &RevocationBasepoint, ) -> Self { let ours = ours.0.serialize(); let theirs = theirs.0.serialize(); - let (lesser, greater) = if ours < theirs { - (ours, theirs) - } else { - (theirs, ours) - }; + let (lesser, greater) = if ours < theirs { (ours, theirs) } else { (theirs, ours) }; let mut engine = Sha256::engine(); engine.input(&lesser[..]); engine.input(&greater[..]); @@ -98,8 +93,11 @@ impl ChannelId { /// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder /// revocation basepoint and hashing the result. - pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self { - Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).to_byte_array()) + pub fn temporary_v2_from_revocation_basepoint( + our_revocation_basepoint: &RevocationBasepoint, + ) -> Self { + let our_revocation_point_bytes = our_revocation_basepoint.0.serialize(); + Self(Sha256::hash(&[[0u8; 33], our_revocation_point_bytes].concat()).to_byte_array()) } } @@ -130,20 +128,17 @@ impl_fmt_traits! { #[cfg(test)] mod tests { - use bitcoin::hashes::{ - Hash as _, - HashEngine as _, - sha256::Hash as Sha256, - }; - use bitcoin::secp256k1::PublicKey; + use bitcoin::hashes::{sha256::Hash as Sha256, Hash as _, HashEngine as _}; use bitcoin::hex::DisplayHex; + use bitcoin::secp256k1::PublicKey; use super::ChannelId; + + use crate::io; use crate::ln::channel_keys::RevocationBasepoint; + use crate::prelude::*; use crate::util::ser::{Readable, Writeable}; use crate::util::test_utils; - use crate::prelude::*; - use crate::io; use core::str::FromStr; From b167aabaabf8d542658ebf1efe42aa2b1fbc1d0f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:50:19 +0000 Subject: [PATCH 6/7] Remove `lightning/src/ln/types.rs` from rustfmt exclusions --- rustfmt_excluded_files | 1 - 1 file changed, 1 deletion(-) diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index 4c7ac636db2..a15c1f1613a 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -36,7 +36,6 @@ lightning/src/ln/reload_tests.rs lightning/src/ln/reorg_tests.rs lightning/src/ln/script.rs lightning/src/ln/shutdown_tests.rs -lightning/src/ln/types.rs lightning/src/routing/mod.rs lightning/src/routing/router.rs lightning/src/routing/scoring.rs From 136cac638797d49794e91666b40179af34cd285a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 21 Feb 2025 23:19:26 +0000 Subject: [PATCH 7/7] Use a rustdoc link for URL in public `features` documentation ...addressing a warning that rustc always prints when generating docs. --- lightning-types/src/features.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightning-types/src/features.rs b/lightning-types/src/features.rs index 9b993e1486a..ea1b9b5b522 100644 --- a/lightning-types/src/features.rs +++ b/lightning-types/src/features.rs @@ -73,7 +73,7 @@ //! - `DnsResolver` - supports resolving DNS names to TXT DNSSEC proofs for BIP 353 payments //! (see [bLIP 32](https://github.com/lightning/blips/blob/master/blip-0032.md) for more information). //! - `ProvideStorage` - Indicates that we offer the capability to store data of our peers -//! (see https://github.com/lightning/bolts/pull/1110 for more info). +//! (see [BOLT PR #1110](https://github.com/lightning/bolts/pull/1110) for more info). //! - `Quiescence` - protocol to quiesce a channel by indicating that "SomeThing Fundamental is Underway" //! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#channel-quiescence) for more information). //!