From 3bf2f7189c3afbfb269eef9c923c307ff64c6c79 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Fri, 13 Jan 2023 07:16:18 -0600 Subject: [PATCH 1/5] Fix doc warnings and cleanup in `ser.rs` --- lightning/src/util/ser.rs | 65 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 0f88ccbd544..a656604d458 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -8,7 +8,10 @@ // licenses. //! A very simple serialization framework which is used to serialize/deserialize messages as well -//! as ChannelsManagers and ChannelMonitors. +//! as [`ChannelManager`]s and [`ChannelMonitor`]s. +//! +//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager +//! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor use crate::prelude::*; use crate::io::{self, Read, Seek, Write}; @@ -40,8 +43,8 @@ use crate::util::byte_utils::{be48_to_array, slice_to_be48}; /// serialization buffer size pub const MAX_BUF_SIZE: usize = 64 * 1024; -/// A simplified version of std::io::Write that exists largely for backwards compatibility. -/// An impl is provided for any type that also impls std::io::Write. +/// A simplified version of [`std::io::Write`] that exists largely for backwards compatibility. +/// An impl is provided for any type that also impls [`std::io::Write`]. /// /// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writer { @@ -175,21 +178,21 @@ impl Read for ReadTrackingReader { } } -/// A trait that various rust-lightning types implement allowing them to be written out to a Writer +/// A trait that various LDK types implement allowing them to be written out to a [`Writer`]. /// /// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writeable { - /// Writes self out to the given Writer + /// Writes `self` out to the given [`Writer`]. fn write(&self, writer: &mut W) -> Result<(), io::Error>; - /// Writes self out to a Vec + /// Writes `self` out to a `Vec`. fn encode(&self) -> Vec { let mut msg = VecWriter(Vec::new()); self.write(&mut msg).unwrap(); msg.0 } - /// Writes self out to a Vec + /// Writes `self` out to a `Vec`. #[cfg(test)] fn encode_with_len(&self) -> Vec { let mut msg = VecWriter(Vec::new()); @@ -215,64 +218,64 @@ impl<'a, T: Writeable> Writeable for &'a T { fn write(&self, writer: &mut W) -> Result<(), io::Error> { (*self).write(writer) } } -/// A trait that various rust-lightning types implement allowing them to be read in from a Read +/// A trait that various LDK types implement allowing them to be read in from a [`Read`]. /// /// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Readable where Self: Sized { - /// Reads a Self in from the given Read + /// Reads a `Self` in from the given [`Read`]. fn read(reader: &mut R) -> Result; } -/// A trait that various rust-lightning types implement allowing them to be read in from a -/// `Read + Seek`. +/// A trait that various LDK types implement allowing them to be read in from a +/// [`Read`]` + `[`Seek`]. pub(crate) trait SeekReadable where Self: Sized { - /// Reads a Self in from the given Read + /// Reads a `Self` in from the given [`Read`]. fn read(reader: &mut R) -> Result; } -/// A trait that various higher-level rust-lightning types implement allowing them to be read in -/// from a Read given some additional set of arguments which is required to deserialize. +/// A trait that various higher-level LDK types implement allowing them to be read in +/// from a [`Read`] given some additional set of arguments which is required to deserialize. /// /// (C-not exported) as we only export serialization to/from byte arrays instead pub trait ReadableArgs

where Self: Sized { - /// Reads a Self in from the given Read + /// Reads a `Self` in from the given [`Read`]. fn read(reader: &mut R, params: P) -> Result; } -/// A std::io::Read that also provides the total bytes available to read. +/// A [`std::io::Read`] that also provides the total bytes available to be read. pub(crate) trait LengthRead: Read { - /// The total number of bytes available to read. + /// The total number of bytes available to be read. fn total_bytes(&self) -> u64; } -/// A trait that various higher-level rust-lightning types implement allowing them to be read in +/// A trait that various higher-level LDK types implement allowing them to be read in /// from a Read given some additional set of arguments which is required to deserialize, requiring /// the implementer to provide the total length of the read. pub(crate) trait LengthReadableArgs

where Self: Sized { - /// Reads a Self in from the given LengthRead + /// Reads a `Self` in from the given [`LengthRead`]. fn read(reader: &mut R, params: P) -> Result; } -/// A trait that various higher-level rust-lightning types implement allowing them to be read in -/// from a Read, requiring the implementer to provide the total length of the read. +/// A trait that various higher-level LDK types implement allowing them to be read in +/// from a [`Read`], requiring the implementer to provide the total length of the read. pub(crate) trait LengthReadable where Self: Sized { - /// Reads a Self in from the given LengthRead + /// Reads a `Self` in from the given [`LengthRead`]. fn read(reader: &mut R) -> Result; } -/// A trait that various rust-lightning types implement allowing them to (maybe) be read in from a Read +/// A trait that various LDK types implement allowing them to (maybe) be read in from a [`Read`]. /// /// (C-not exported) as we only export serialization to/from byte arrays instead pub trait MaybeReadable where Self: Sized { - /// Reads a Self in from the given Read + /// Reads a `Self` in from the given [`Read`]. fn read(reader: &mut R) -> Result, DecodeError>; } @@ -291,8 +294,8 @@ impl Readable for OptionDeserWrapper { Ok(Self(Some(Readable::read(reader)?))) } } -/// When handling default_values, we want to map the default-value T directly -/// to a OptionDeserWrapper in a way that works for `field: T = t;` as +/// When handling `default_values`, we want to map the default-value T directly +/// to a `OptionDeserWrapper` in a way that works for `field: T = t;` as /// well. Thus, we assume `Into for T` does nothing and use that. impl From for OptionDeserWrapper { fn from(t: T) -> OptionDeserWrapper { OptionDeserWrapper(Some(t)) } @@ -314,7 +317,7 @@ impl Readable for U48 { } } -/// Lightning TLV uses a custom variable-length integer called BigSize. It is similar to Bitcoin's +/// Lightning TLV uses a custom variable-length integer called `BigSize`. It is similar to Bitcoin's /// variable-length integers except that it is serialized in big-endian instead of little-endian. /// /// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be @@ -380,7 +383,7 @@ impl Readable for BigSize { /// In TLV we occasionally send fields which only consist of, or potentially end with, a /// variable-length integer which is simply truncated by skipping high zero bytes. This type -/// encapsulates such integers implementing Readable/Writeable for them. +/// encapsulates such integers implementing [`Readable`]/[`Writeable`] for them. #[cfg_attr(test, derive(PartialEq, Eq, Debug))] pub(crate) struct HighZeroBytesDroppedBigSize(pub T); @@ -532,7 +535,7 @@ impl Readable for [u16; 8] { } } -/// For variable-length values within TLV record where the length is encoded as part of the record. +/// A type for variable-length values within TLV record where the length is encoded as part of the record. /// Used to prevent encoding the length twice. pub struct WithoutLength(pub T); @@ -1042,7 +1045,9 @@ impl Readable for String { /// Only the character set and length will be validated. /// The character set consists of ASCII alphanumeric characters, hyphens, and periods. /// Its length is guaranteed to be representable by a single byte. -/// This serialization is used by BOLT 7 hostnames. +/// This serialization is used by [`BOLT 7`] hostnames. +/// +/// [`BOLT 7`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md #[derive(Clone, Debug, PartialEq, Eq)] pub struct Hostname(String); impl Hostname { From 7eac8977467c06fb5746cb2ec0789ce1f37e02fa Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Fri, 13 Jan 2023 09:18:26 -0600 Subject: [PATCH 2/5] Fix doc warnings and cleanup in `msgs.rs` --- lightning/src/ln/msgs.rs | 393 ++++++++++++++++++++++++--------------- 1 file changed, 244 insertions(+), 149 deletions(-) diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 399feaf6769..278352845ac 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -12,12 +12,12 @@ //! For a normal node you probably don't need to use anything here, however, if you wish to split a //! node into an internet-facing route/message socket handling daemon and a separate daemon (or //! server entirely) which handles only channel-related messages you may wish to implement -//! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across +//! [`ChannelMessageHandler`] yourself and use it to re-serialize messages and pass them across //! daemons/servers. //! //! Note that if you go with such an architecture (instead of passing raw socket events to a //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the -//! source node_id of the message, however this does allow you to significantly reduce bandwidth +//! source `node_id` of the message, however this does allow you to significantly reduce bandwidth //! between the systems as routing messages can represent a significant chunk of bandwidth usage //! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids //! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send @@ -53,37 +53,46 @@ pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000; #[derive(Clone, Debug, PartialEq, Eq)] pub enum DecodeError { /// A version byte specified something we don't know how to handle. - /// Includes unknown realm byte in an OnionHopData packet + /// + /// Includes unknown realm byte in an onion hop data packet. UnknownVersion, - /// Unknown feature mandating we fail to parse message (eg TLV with an even, unknown type) + /// Unknown feature mandating we fail to parse message (e.g., TLV with an even, unknown type) UnknownRequiredFeature, - /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0 + /// Value was invalid. + /// + /// For example, a byte which was supposed to be a bool was something other than a 0 /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was - /// syntactically incorrect, etc + /// syntactically incorrect, etc. InvalidValue, - /// Buffer too short + /// The buffer to be read was too short. ShortRead, - /// A length descriptor in the packet didn't describe the later data correctly + /// A length descriptor in the packet didn't describe the later data correctly. BadLengthDescriptor, - /// Error from std::io + /// Error from [`std::io`]. Io(io::ErrorKind), /// The message included zlib-compressed values, which we don't support. UnsupportedCompression, } -/// An init message to be sent or received from a peer +/// An [`init`] message to be sent to or received from a peer. +/// +/// [`init`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-init-message #[derive(Clone, Debug, PartialEq, Eq)] pub struct Init { - /// The relevant features which the sender supports + /// The relevant features which the sender supports. pub features: InitFeatures, - /// The receipient's network address. This adds the option to report a remote IP address - /// back to a connecting peer using the init message. A node can decide to use that information - /// to discover a potential update to its public IPv4 address (NAT) and use - /// that for a node_announcement update message containing the new address. + /// The receipient's network address. + /// + /// This adds the option to report a remote IP address back to a connecting peer using the init + /// message. A node can decide to use that information to discover a potential update to its + /// public IPv4 address (NAT) and use that for a [`NodeAnnouncement`] update message containing + /// the new address. pub remote_network_address: Option, } -/// An error message to be sent or received from a peer +/// An [`error`] message to be sent to or received from a peer. +/// +/// [`error`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages #[derive(Clone, Debug, PartialEq, Eq)] pub struct ErrorMessage { /// The channel ID involved in the error. @@ -92,13 +101,16 @@ pub struct ErrorMessage { /// with the sending peer should be closed. pub channel_id: [u8; 32], /// A possibly human-readable error description. - /// The string should be sanitized before it is used (e.g. emitted to logs or printed to - /// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in + /// + /// The string should be sanitized before it is used (e.g., emitted to logs or printed to + /// `stdout`). Otherwise, a well crafted error message may trigger a security vulnerability in /// the terminal emulator or the logging subsystem. pub data: String, } -/// A warning message to be sent or received from a peer +/// A [`warning`] message to be sent to or received from a peer. +/// +/// [`warning`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages #[derive(Clone, Debug, PartialEq, Eq)] pub struct WarningMessage { /// The channel ID involved in the warning. @@ -106,31 +118,40 @@ pub struct WarningMessage { /// All-0s indicates a warning unrelated to a specific channel. pub channel_id: [u8; 32], /// A possibly human-readable warning description. + /// /// The string should be sanitized before it is used (e.g. emitted to logs or printed to /// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in /// the terminal emulator or the logging subsystem. pub data: String, } -/// A ping message to be sent or received from a peer +/// A [`ping`] message to be sent to or received from a peer. +/// +/// [`ping`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-ping-and-pong-messages #[derive(Clone, Debug, PartialEq, Eq)] pub struct Ping { - /// The desired response length + /// The desired response length. pub ponglen: u16, /// The ping packet size. + /// /// This field is not sent on the wire. byteslen zeros are sent. pub byteslen: u16, } -/// A pong message to be sent or received from a peer +/// A [`pong`] message to be sent to or received from a peer. +/// +/// [`pong`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-ping-and-pong-messages #[derive(Clone, Debug, PartialEq, Eq)] pub struct Pong { /// The pong packet size. + /// /// This field is not sent on the wire. byteslen zeros are sent. pub byteslen: u16, } -/// An open_channel message to be sent or received from a peer +/// An [`open_channel`] message to be sent to or received from a peer. +/// +/// [`open_channel`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message #[derive(Clone, Debug, PartialEq, Eq)] pub struct OpenChannel { /// The genesis hash of the blockchain where the channel is to be opened @@ -149,9 +170,11 @@ pub struct OpenChannel { pub channel_reserve_satoshis: u64, /// The minimum HTLC size incoming to sender, in milli-satoshi pub htlc_minimum_msat: u64, - /// The feerate per 1000-weight of sender generated transactions, until updated by update_fee + /// The feerate per 1000-weight of sender generated transactions, until updated by + /// [`UpdateFee`] pub feerate_per_kw: u32, - /// The number of blocks which the counterparty will have to wait to claim on-chain funds if they broadcast a commitment transaction + /// The number of blocks which the counterparty will have to wait to claim on-chain funds if + /// they broadcast a commitment transaction pub to_self_delay: u16, /// The maximum number of inbound HTLCs towards sender pub max_accepted_htlcs: u16, @@ -167,17 +190,20 @@ pub struct OpenChannel { pub htlc_basepoint: PublicKey, /// The first to-be-broadcast-by-sender transaction's per commitment point pub first_per_commitment_point: PublicKey, - /// Channel flags + /// The channel flags to be used pub channel_flags: u8, - /// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close + /// Optionally, a request to pre-set the to-sender output's `scriptPubkey` for when we collaboratively close pub shutdown_scriptpubkey: OptionalField