X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmsgs.rs;h=3042b4d32e0a344e185ef9756654aefa759606aa;hb=31e78ff258195837abafa045c12c036528696d7f;hp=e48080103bd1b1642c390ae3eb8e5ea1fd712a05;hpb=31cc243e6f04e82f7754d26df7a2bb0275d7ebd1;p=rust-lightning diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index e4808010..cb857ed5 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -1,455 +1,486 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + //! Wire messages, traits representing wire message handlers, and a few error types live here. //! //! 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 //! raw socket events into your non-internet-facing system and then send routing events back to //! track the network on the less-secure system. -use secp256k1::key::PublicKey; -use secp256k1::Signature; -use secp256k1; -use bitcoin_hashes::sha256d::Hash as Sha256dHash; +use bitcoin::secp256k1::PublicKey; +use bitcoin::secp256k1::ecdsa::Signature; +use bitcoin::secp256k1; use bitcoin::blockdata::script::Script; +use bitcoin::hash_types::{Txid, BlockHash}; + +use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures}; +use crate::ln::onion_utils; +use crate::onion_message; + +use crate::prelude::*; +use core::fmt; +use core::fmt::Debug; +use crate::io::{self, Read}; +use crate::io_extras::read_to_end; + +use crate::events::{MessageSendEventsProvider, OnionMessageProvider}; +use crate::util::logger; +use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname}; -use std::error::Error; -use std::{cmp, fmt}; -use std::io::Read; -use std::result::Result; -use std::marker::PhantomData; +use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; -use util::events; -use util::ser::{Readable, Writeable, Writer}; +use crate::routing::gossip::NodeId; -use ln::channelmanager::{PaymentPreimage, PaymentHash}; +/// 21 million * 10^8 * 1000 +pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000; /// An error in decoding a message or struct. -#[derive(Debug)] +#[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 + /// 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 - /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc + /// 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. InvalidValue, - /// Buffer too short + /// The buffer to be read was too short. ShortRead, - /// node_announcement included more than one address of a given type! - ExtraAddressesPerType, - /// 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 - Io(::std::io::Error), + /// Error from [`std::io`]. + Io(io::ErrorKind), + /// The message included zlib-compressed values, which we don't support. + UnsupportedCompression, } -/// The context in which a Feature object appears determines which bits of features the node -/// supports will be set. We use this when creating our own Feature objects to select which bits to -/// set and when passing around Feature objects to ensure the bits we're checking for are -/// available. +/// An [`init`] message to be sent to or received from a peer. /// -/// This Context represents when the Feature appears in the init message, sent between peers and not -/// rumored around the P2P network. -pub struct FeatureContextInit {} -/// The context in which a Feature object appears determines which bits of features the node -/// supports will be set. We use this when creating our own Feature objects to select which bits to -/// set and when passing around Feature objects to ensure the bits we're checking for are -/// available. -/// -/// This Context represents when the Feature appears in the node_announcement message, as it is -/// rumored around the P2P network. -pub struct FeatureContextNode {} -/// The context in which a Feature object appears determines which bits of features the node -/// supports will be set. We use this when creating our own Feature objects to select which bits to -/// set and when passing around Feature objects to ensure the bits we're checking for are -/// available. +/// [`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. + 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 [`NodeAnnouncement`] update message containing + /// the new address. + pub remote_network_address: Option, +} + +/// An [`error`] message to be sent to or received from a peer. /// -/// This Context represents when the Feature appears in the ChannelAnnouncement message, as it is -/// rumored around the P2P network. -pub struct FeatureContextChannel {} -/// The context in which a Feature object appears determines which bits of features the node -/// supports will be set. We use this when creating our own Feature objects to select which bits to -/// set and when passing around Feature objects to ensure the bits we're checking for are -/// available. +/// [`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. + /// + /// All-0s indicates a general error unrelated to a specific channel, after which all channels + /// 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 terminal emulator or the logging subsystem. + pub data: String, +} + +/// A [`warning`] message to be sent to or received from a peer. /// -/// This Context represents when the Feature appears in an invoice, used to determine the different -/// options available for routing a payment. +/// [`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. + /// + /// 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 to or received from a peer. /// -/// Note that this is currently unused as invoices come to us via a different crate and are not -/// native to rust-lightning directly. -pub struct FeatureContextInvoice {} - -/// An internal trait capturing the various future context types -pub trait FeatureContext {} -impl FeatureContext for FeatureContextInit {} -impl FeatureContext for FeatureContextNode {} -impl FeatureContext for FeatureContextChannel {} -impl FeatureContext for FeatureContextInvoice {} - -/// An internal trait capturing FeatureContextInit and FeatureContextNode -pub trait FeatureContextInitNode : FeatureContext {} -impl FeatureContextInitNode for FeatureContextInit {} -impl FeatureContextInitNode for FeatureContextNode {} - -/// Tracks the set of features which a node implements, templated by the context in which it -/// appears. -pub struct Features { - #[cfg(not(test))] - flags: Vec, - // Used to test encoding of diverse msgs - #[cfg(test)] - pub flags: Vec, - mark: PhantomData, -} - -impl Clone for Features { - fn clone(&self) -> Self { - Self { - flags: self.flags.clone(), - mark: PhantomData, - } - } -} -impl PartialEq for Features { - fn eq(&self, o: &Self) -> bool { - self.flags.eq(&o.flags) - } -} -impl fmt::Debug for Features { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - self.flags.fmt(fmt) - } -} - -/// A feature message as it appears in an init message -pub type InitFeatures = Features; -/// A feature message as it appears in a node_announcement message -pub type NodeFeatures = Features; -/// A feature message as it appears in a channel_announcement message -pub type ChannelFeatures = Features; - -impl Features { - /// Create a blank Features flags (visibility extended for fuzz tests) - #[cfg(not(feature = "fuzztarget"))] - pub(crate) fn new() -> Features { - Features { - flags: vec![2 | 1 << 5], - mark: PhantomData, - } - } - #[cfg(feature = "fuzztarget")] - pub fn new() -> Features { - Features { - flags: vec![2 | 1 << 5], - mark: PhantomData, - } - } -} - -impl Features { - /// Create a blank Features flags (visibility extended for fuzz tests) - #[cfg(not(feature = "fuzztarget"))] - pub(crate) fn new() -> Features { - Features { - flags: Vec::new(), - mark: PhantomData, - } - } - #[cfg(feature = "fuzztarget")] - pub fn new() -> Features { - Features { - flags: Vec::new(), - mark: PhantomData, - } - } -} - -impl Features { - pub(crate) fn requires_unknown_bits(&self) -> bool { - self.flags.iter().enumerate().any(|(idx, &byte)| { - ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 ) - }) - } - - pub(crate) fn supports_unknown_bits(&self) -> bool { - self.flags.iter().enumerate().any(|(idx, &byte)| { - ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 ) - }) - } - - /// The number of bytes required to represent the feaature flags present. This does not include - /// the length bytes which are included in the serialized form. - pub(crate) fn byte_count(&self) -> usize { - self.flags.len() - } -} - -impl Features { - pub(crate) fn supports_data_loss_protect(&self) -> bool { - self.flags.len() > 0 && (self.flags[0] & 3) != 0 - } - - pub(crate) fn supports_upfront_shutdown_script(&self) -> bool { - self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0 - } - #[cfg(test)] - pub(crate) fn unset_upfront_shutdown_script(&mut self) { - self.flags[0] ^= 1 << 5; - } -} - -impl Features { - pub(crate) fn initial_routing_sync(&self) -> bool { - self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0 - } - pub(crate) fn set_initial_routing_sync(&mut self) { - if self.flags.len() == 0 { - self.flags.resize(1, 1 << 3); - } else { - self.flags[0] |= 1 << 3; - } - } - - /// Writes all features present up to, and including, 13. - pub(crate) fn write_up_to_13(&self, w: &mut W) -> Result<(), ::std::io::Error> { - let len = cmp::min(2, self.flags.len()); - w.size_hint(len + 2); - (len as u16).write(w)?; - for i in (0..len).rev() { - if i == 0 { - self.flags[i].write(w)?; - } else { - (self.flags[i] & ((1 << (14 - 8)) - 1)).write(w)?; - } - } - Ok(()) - } - - /// or's another InitFeatures into this one. - pub(crate) fn or(&mut self, o: &InitFeatures) { - let total_feature_len = cmp::max(self.flags.len(), o.flags.len()); - self.flags.resize(total_feature_len, 0u8); - for (feature, o_feature) in self.flags.iter_mut().zip(o.flags.iter()) { - *feature |= *o_feature; - } - } -} - -impl Writeable for Features { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - w.size_hint(self.flags.len() + 2); - self.flags.write(w) - } -} - -impl Readable for Features { - fn read(r: &mut R) -> Result { - Ok(Self { - flags: Readable::read(r)?, - mark: PhantomData, - }) - } -} -/// An init message to be sent or received from a peer -pub struct Init { - pub(crate) features: InitFeatures, -} - -/// An error message to be sent or received from a peer -#[derive(Clone)] -pub struct ErrorMessage { - pub(crate) channel_id: [u8; 32], - pub(crate) data: String, -} - -/// A ping message to be sent 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 { - pub(crate) ponglen: u16, - pub(crate) byteslen: u16, + /// 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 { - pub(crate) byteslen: u16, + /// 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 -#[derive(Clone)] +/// 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 { - pub(crate) chain_hash: Sha256dHash, - pub(crate) temporary_channel_id: [u8; 32], - pub(crate) funding_satoshis: u64, - pub(crate) push_msat: u64, - pub(crate) dust_limit_satoshis: u64, - pub(crate) max_htlc_value_in_flight_msat: u64, - pub(crate) channel_reserve_satoshis: u64, - pub(crate) htlc_minimum_msat: u64, - pub(crate) feerate_per_kw: u32, - pub(crate) to_self_delay: u16, - pub(crate) max_accepted_htlcs: u16, - pub(crate) funding_pubkey: PublicKey, - pub(crate) revocation_basepoint: PublicKey, - pub(crate) payment_basepoint: PublicKey, - pub(crate) delayed_payment_basepoint: PublicKey, - pub(crate) htlc_basepoint: PublicKey, - pub(crate) first_per_commitment_point: PublicKey, - pub(crate) channel_flags: u8, - pub(crate) shutdown_scriptpubkey: OptionalField