Skip to content

Commit 6d359c1

Browse files
committed
Standardize trait derives in network message objects
In general, trivial structs that have no inner logic can/should all derive, at least, `Clone, Debug, PartialEq`.
1 parent ee68ffa commit 6d359c1

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

lightning/src/ln/msgs.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,14 @@ pub enum DecodeError {
6666
}
6767

6868
/// An init message to be sent or received from a peer
69-
#[derive(Clone)]
69+
#[derive(Clone, Debug, PartialEq)]
7070
pub struct Init {
71-
#[cfg(not(feature = "fuzztarget"))]
72-
pub(crate) features: InitFeatures,
73-
#[cfg(feature = "fuzztarget")]
71+
/// The relevant features which the sender supports
7472
pub features: InitFeatures,
7573
}
7674

7775
/// An error message to be sent or received from a peer
78-
#[derive(Clone)]
76+
#[derive(Clone, Debug, PartialEq)]
7977
pub struct ErrorMessage {
8078
/// The channel ID involved in the error
8179
pub channel_id: [u8; 32],
@@ -87,7 +85,7 @@ pub struct ErrorMessage {
8785
}
8886

8987
/// A ping message to be sent or received from a peer
90-
#[derive(Clone)]
88+
#[derive(Clone, Debug, PartialEq)]
9189
pub struct Ping {
9290
/// The desired response length
9391
pub ponglen: u16,
@@ -97,15 +95,15 @@ pub struct Ping {
9795
}
9896

9997
/// A pong message to be sent or received from a peer
100-
#[derive(Clone)]
98+
#[derive(Clone, Debug, PartialEq)]
10199
pub struct Pong {
102100
/// The pong packet size.
103101
/// This field is not sent on the wire. byteslen zeros are sent.
104102
pub byteslen: u16,
105103
}
106104

107105
/// An open_channel message to be sent or received from a peer
108-
#[derive(Clone)]
106+
#[derive(Clone, Debug, PartialEq)]
109107
pub struct OpenChannel {
110108
/// The genesis hash of the blockchain where the channel is to be opened
111109
pub chain_hash: BlockHash,
@@ -148,7 +146,7 @@ pub struct OpenChannel {
148146
}
149147

150148
/// An accept_channel message to be sent or received from a peer
151-
#[derive(Clone)]
149+
#[derive(Clone, Debug, PartialEq)]
152150
pub struct AcceptChannel {
153151
/// A temporary channel ID, until the funding outpoint is announced
154152
pub temporary_channel_id: [u8; 32],
@@ -183,7 +181,7 @@ pub struct AcceptChannel {
183181
}
184182

185183
/// A funding_created message to be sent or received from a peer
186-
#[derive(Clone)]
184+
#[derive(Clone, Debug, PartialEq)]
187185
pub struct FundingCreated {
188186
/// A temporary channel ID, until the funding is established
189187
pub temporary_channel_id: [u8; 32],
@@ -196,7 +194,7 @@ pub struct FundingCreated {
196194
}
197195

198196
/// A funding_signed message to be sent or received from a peer
199-
#[derive(Clone)]
197+
#[derive(Clone, Debug, PartialEq)]
200198
pub struct FundingSigned {
201199
/// The channel ID
202200
pub channel_id: [u8; 32],
@@ -205,7 +203,7 @@ pub struct FundingSigned {
205203
}
206204

207205
/// A funding_locked message to be sent or received from a peer
208-
#[derive(Clone, PartialEq)]
206+
#[derive(Clone, Debug, PartialEq)]
209207
pub struct FundingLocked {
210208
/// The channel ID
211209
pub channel_id: [u8; 32],
@@ -214,7 +212,7 @@ pub struct FundingLocked {
214212
}
215213

216214
/// A shutdown message to be sent or received from a peer
217-
#[derive(Clone, PartialEq)]
215+
#[derive(Clone, Debug, PartialEq)]
218216
pub struct Shutdown {
219217
/// The channel ID
220218
pub channel_id: [u8; 32],
@@ -224,7 +222,7 @@ pub struct Shutdown {
224222
}
225223

226224
/// A closing_signed message to be sent or received from a peer
227-
#[derive(Clone, PartialEq)]
225+
#[derive(Clone, Debug, PartialEq)]
228226
pub struct ClosingSigned {
229227
/// The channel ID
230228
pub channel_id: [u8; 32],
@@ -235,7 +233,7 @@ pub struct ClosingSigned {
235233
}
236234

237235
/// An update_add_htlc message to be sent or received from a peer
238-
#[derive(Clone, PartialEq)]
236+
#[derive(Clone, Debug, PartialEq)]
239237
pub struct UpdateAddHTLC {
240238
/// The channel ID
241239
pub channel_id: [u8; 32],
@@ -251,7 +249,7 @@ pub struct UpdateAddHTLC {
251249
}
252250

253251
/// An update_fulfill_htlc message to be sent or received from a peer
254-
#[derive(Clone, PartialEq)]
252+
#[derive(Clone, Debug, PartialEq)]
255253
pub struct UpdateFulfillHTLC {
256254
/// The channel ID
257255
pub channel_id: [u8; 32],
@@ -262,7 +260,7 @@ pub struct UpdateFulfillHTLC {
262260
}
263261

264262
/// An update_fail_htlc message to be sent or received from a peer
265-
#[derive(Clone, PartialEq)]
263+
#[derive(Clone, Debug, PartialEq)]
266264
pub struct UpdateFailHTLC {
267265
/// The channel ID
268266
pub channel_id: [u8; 32],
@@ -272,7 +270,7 @@ pub struct UpdateFailHTLC {
272270
}
273271

274272
/// An update_fail_malformed_htlc message to be sent or received from a peer
275-
#[derive(Clone, PartialEq)]
273+
#[derive(Clone, Debug, PartialEq)]
276274
pub struct UpdateFailMalformedHTLC {
277275
/// The channel ID
278276
pub channel_id: [u8; 32],
@@ -284,7 +282,7 @@ pub struct UpdateFailMalformedHTLC {
284282
}
285283

286284
/// A commitment_signed message to be sent or received from a peer
287-
#[derive(Clone, PartialEq)]
285+
#[derive(Clone, Debug, PartialEq)]
288286
pub struct CommitmentSigned {
289287
/// The channel ID
290288
pub channel_id: [u8; 32],
@@ -295,7 +293,7 @@ pub struct CommitmentSigned {
295293
}
296294

297295
/// A revoke_and_ack message to be sent or received from a peer
298-
#[derive(Clone, PartialEq)]
296+
#[derive(Clone, Debug, PartialEq)]
299297
pub struct RevokeAndACK {
300298
/// The channel ID
301299
pub channel_id: [u8; 32],
@@ -306,15 +304,15 @@ pub struct RevokeAndACK {
306304
}
307305

308306
/// An update_fee message to be sent or received from a peer
309-
#[derive(PartialEq, Clone)]
307+
#[derive(Clone, Debug, PartialEq)]
310308
pub struct UpdateFee {
311309
/// The channel ID
312310
pub channel_id: [u8; 32],
313311
/// Fee rate per 1000-weight of the transaction
314312
pub feerate_per_kw: u32,
315313
}
316314

317-
#[derive(PartialEq, Clone)]
315+
#[derive(Clone, Debug, PartialEq)]
318316
/// Proof that the sender knows the per-commitment secret of the previous commitment transaction.
319317
/// This is used to convince the recipient that the channel is at a certain commitment
320318
/// number even if they lost that data due to a local failure. Of course, the peer may lie
@@ -328,7 +326,7 @@ pub struct DataLossProtect {
328326
}
329327

330328
/// A channel_reestablish message to be sent or received from a peer
331-
#[derive(PartialEq, Clone)]
329+
#[derive(Clone, Debug, PartialEq)]
332330
pub struct ChannelReestablish {
333331
/// The channel ID
334332
pub channel_id: [u8; 32],
@@ -341,7 +339,7 @@ pub struct ChannelReestablish {
341339
}
342340

343341
/// An announcement_signatures message to be sent or received from a peer
344-
#[derive(PartialEq, Clone, Debug)]
342+
#[derive(Clone, Debug, PartialEq)]
345343
pub struct AnnouncementSignatures {
346344
/// The channel ID
347345
pub channel_id: [u8; 32],
@@ -354,7 +352,7 @@ pub struct AnnouncementSignatures {
354352
}
355353

356354
/// An address which can be used to connect to a remote peer
357-
#[derive(Clone, PartialEq, Debug)]
355+
#[derive(Clone, Debug, PartialEq)]
358356
pub enum NetAddress {
359357
/// An IPv4 address/port on which the peer is listening.
360358
IPv4 {
@@ -472,7 +470,7 @@ impl Readable for Result<NetAddress, u8> {
472470
}
473471

474472
/// The unsigned part of a node_announcement
475-
#[derive(PartialEq, Clone, Debug)]
473+
#[derive(Clone, Debug, PartialEq)]
476474
pub struct UnsignedNodeAnnouncement {
477475
/// The advertised features
478476
pub features: NodeFeatures,
@@ -491,7 +489,7 @@ pub struct UnsignedNodeAnnouncement {
491489
pub(crate) excess_address_data: Vec<u8>,
492490
pub(crate) excess_data: Vec<u8>,
493491
}
494-
#[derive(PartialEq, Clone, Debug)]
492+
#[derive(Clone, Debug, PartialEq)]
495493
/// A node_announcement message to be sent or received from a peer
496494
pub struct NodeAnnouncement {
497495
/// The signature by the node key
@@ -501,7 +499,7 @@ pub struct NodeAnnouncement {
501499
}
502500

503501
/// The unsigned part of a channel_announcement
504-
#[derive(PartialEq, Clone, Debug)]
502+
#[derive(Clone, Debug, PartialEq)]
505503
pub struct UnsignedChannelAnnouncement {
506504
/// The advertised channel features
507505
pub features: ChannelFeatures,
@@ -520,7 +518,7 @@ pub struct UnsignedChannelAnnouncement {
520518
pub(crate) excess_data: Vec<u8>,
521519
}
522520
/// A channel_announcement message to be sent or received from a peer
523-
#[derive(PartialEq, Clone, Debug)]
521+
#[derive(Clone, Debug, PartialEq)]
524522
pub struct ChannelAnnouncement {
525523
/// Authentication of the announcement by the first public node
526524
pub node_signature_1: Signature,
@@ -535,7 +533,7 @@ pub struct ChannelAnnouncement {
535533
}
536534

537535
/// The unsigned part of a channel_update
538-
#[derive(PartialEq, Clone, Debug)]
536+
#[derive(Clone, Debug, PartialEq)]
539537
pub struct UnsignedChannelUpdate {
540538
/// The genesis hash of the blockchain where the channel is to be opened
541539
pub chain_hash: BlockHash,
@@ -558,7 +556,7 @@ pub struct UnsignedChannelUpdate {
558556
pub(crate) excess_data: Vec<u8>,
559557
}
560558
/// A channel_update message to be sent or received from a peer
561-
#[derive(PartialEq, Clone, Debug)]
559+
#[derive(Clone, Debug, PartialEq)]
562560
pub struct ChannelUpdate {
563561
/// A signature of the channel update
564562
pub signature: Signature,
@@ -570,7 +568,7 @@ pub struct ChannelUpdate {
570568
/// UTXOs in a range of blocks. The recipient of a query makes a best
571569
/// effort to reply to the query using one or more reply_channel_range
572570
/// messages.
573-
#[derive(Clone, Debug)]
571+
#[derive(Clone, Debug, PartialEq)]
574572
pub struct QueryChannelRange {
575573
/// The genesis hash of the blockchain being queried
576574
pub chain_hash: BlockHash,
@@ -587,7 +585,7 @@ pub struct QueryChannelRange {
587585
/// not be a perfect view of the network. The short_channel_ids in the
588586
/// reply are encoded. We only support encoding_type=0 uncompressed
589587
/// serialization and do not support encoding_type=1 zlib serialization.
590-
#[derive(Clone, Debug)]
588+
#[derive(Clone, Debug, PartialEq)]
591589
pub struct ReplyChannelRange {
592590
/// The genesis hash of the blockchain being queried
593591
pub chain_hash: BlockHash,
@@ -609,7 +607,7 @@ pub struct ReplyChannelRange {
609607
/// reply_short_channel_ids_end message. The short_channel_ids sent in
610608
/// this query are encoded. We only support encoding_type=0 uncompressed
611609
/// serialization and do not support encoding_type=1 zlib serialization.
612-
#[derive(Clone, Debug)]
610+
#[derive(Clone, Debug, PartialEq)]
613611
pub struct QueryShortChannelIds {
614612
/// The genesis hash of the blockchain being queried
615613
pub chain_hash: BlockHash,
@@ -621,7 +619,7 @@ pub struct QueryShortChannelIds {
621619
/// query_short_channel_ids message. The query recipient makes a best
622620
/// effort to respond based on their local network view which may not be
623621
/// a perfect view of the network.
624-
#[derive(Clone, Debug)]
622+
#[derive(Clone, Debug, PartialEq)]
625623
pub struct ReplyShortChannelIdsEnd {
626624
/// The genesis hash of the blockchain that was queried
627625
pub chain_hash: BlockHash,
@@ -633,7 +631,7 @@ pub struct ReplyShortChannelIdsEnd {
633631
/// A gossip_timestamp_filter message is used by a node to request
634632
/// gossip relay for messages in the requested time range when the
635633
/// gossip_queries feature has been negotiated.
636-
#[derive(Clone, Debug)]
634+
#[derive(Clone, Debug, PartialEq)]
637635
pub struct GossipTimestampFilter {
638636
/// The genesis hash of the blockchain for channel and node information
639637
pub chain_hash: BlockHash,
@@ -650,7 +648,7 @@ enum EncodingType {
650648
}
651649

652650
/// Used to put an error message in a LightningError
653-
#[derive(Clone)]
651+
#[derive(Clone, Debug)]
654652
pub enum ErrorAction {
655653
/// The peer took some action which made us think they were useless. Disconnect them.
656654
DisconnectPeer {
@@ -667,7 +665,7 @@ pub enum ErrorAction {
667665
}
668666

669667
/// An Err type for failure to process messages.
670-
#[derive(Clone)]
668+
#[derive(Clone, Debug)]
671669
pub struct LightningError {
672670
/// A human-readable message describing the error
673671
pub err: String,
@@ -677,7 +675,7 @@ pub struct LightningError {
677675

678676
/// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
679677
/// transaction updates if they were pending.
680-
#[derive(PartialEq, Clone)]
678+
#[derive(Clone, Debug, PartialEq)]
681679
pub struct CommitmentUpdate {
682680
/// update_add_htlc messages which should be sent
683681
pub update_add_htlcs: Vec<UpdateAddHTLC>,
@@ -696,7 +694,7 @@ pub struct CommitmentUpdate {
696694
/// The information we received from a peer along the route of a payment we originated. This is
697695
/// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
698696
/// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
699-
#[derive(Clone)]
697+
#[derive(Clone, Debug, PartialEq)]
700698
pub enum HTLCFailChannelUpdate {
701699
/// We received an error which included a full ChannelUpdate message.
702700
ChannelUpdateMessage {
@@ -727,7 +725,7 @@ pub enum HTLCFailChannelUpdate {
727725
/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
728726
/// separate enum type for them.
729727
/// (C-not exported) due to a free generic in T
730-
#[derive(Clone, PartialEq, Debug)]
728+
#[derive(Clone, Debug, PartialEq)]
731729
pub enum OptionalField<T> {
732730
/// Optional field is included in message
733731
Present(T),
@@ -912,7 +910,13 @@ impl PartialEq for OnionPacket {
912910
}
913911
}
914912

915-
#[derive(Clone, PartialEq)]
913+
impl fmt::Debug for OnionPacket {
914+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
915+
f.write_fmt(format_args!("OnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
916+
}
917+
}
918+
919+
#[derive(Clone, Debug, PartialEq)]
916920
pub(crate) struct OnionErrorPacket {
917921
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
918922
// (TODO) We limit it in decode to much lower...
@@ -932,12 +936,6 @@ impl fmt::Display for DecodeError {
932936
}
933937
}
934938

935-
impl fmt::Debug for LightningError {
936-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
937-
f.write_str(self.err.as_str())
938-
}
939-
}
940-
941939
impl From<::std::io::Error> for DecodeError {
942940
fn from(e: ::std::io::Error) -> Self {
943941
if e.kind() == ::std::io::ErrorKind::UnexpectedEof {

0 commit comments

Comments
 (0)