X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmsgs.rs;h=e915f04152092f77388f20af1ad5d1d495cc245a;hb=58a4f6c4ad3c262f7ed7009f6c99d36936e958b4;hp=277d96d168733e890be98391b6017aa7df138b50;hpb=06091cee0fd29549e5e24c673bf361ab3a562529;p=rust-lightning diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 277d96d1..eec1a053 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -1,3 +1,12 @@ +// 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 @@ -15,326 +24,338 @@ //! 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::key::PublicKey; +use bitcoin::secp256k1::Signature; +use bitcoin::secp256k1; use bitcoin::blockdata::script::Script; +use bitcoin::hash_types::{Txid, BlockHash}; + +use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; -use std::error::Error; -use std::{cmp, fmt}; +use prelude::*; +use core::{cmp, fmt}; +use core::fmt::Debug; use std::io::Read; -use std::result::Result; -use util::events; -use util::ser::{Readable, Writeable, Writer}; +use util::events::MessageSendEventsProvider; +use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt}; + +use ln::{PaymentPreimage, PaymentHash, PaymentSecret}; -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)] pub enum DecodeError { /// A version byte specified something we don't know how to handle. /// Includes unknown realm byte in an OnionHopData packet UnknownVersion, - /// Unknown feature mandating we fail to parse message + /// Unknown feature mandating we fail to parse message (eg 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 + /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was + /// syntactically incorrect, etc InvalidValue, /// Buffer 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 BadLengthDescriptor, /// Error from std::io - Io(::std::io::Error), -} - -/// Tracks localfeatures which are only in init messages -#[derive(Clone, PartialEq)] -pub struct LocalFeatures { - flags: Vec, -} - -impl LocalFeatures { - /// Create a blank LocalFeatures flags (visibility extended for fuzz tests) - #[cfg(not(feature = "fuzztarget"))] - pub(crate) fn new() -> LocalFeatures { - LocalFeatures { - flags: vec![2 | 1 << 5], - } - } - #[cfg(feature = "fuzztarget")] - pub fn new() -> LocalFeatures { - LocalFeatures { - flags: vec![2 | 1 << 5], - } - } - - pub(crate) fn supports_data_loss_protect(&self) -> bool { - self.flags.len() > 0 && (self.flags[0] & 3) != 0 - } - 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; - } - } - - 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; - } - - 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 ) - }) - } -} - -/// Tracks globalfeatures which are in init messages and routing announcements -#[derive(Clone, PartialEq, Debug)] -pub struct GlobalFeatures { - #[cfg(not(test))] - flags: Vec, - // Used to test encoding of diverse msgs - #[cfg(test)] - pub flags: Vec -} - -impl GlobalFeatures { - pub(crate) fn new() -> GlobalFeatures { - GlobalFeatures { - flags: Vec::new(), - } - } - - pub(crate) fn requires_unknown_bits(&self) -> bool { - for &byte in self.flags.iter() { - if (byte & 0x55) != 0 { - return true; - } - } - return false; - } - - pub(crate) fn supports_unknown_bits(&self) -> bool { - for &byte in self.flags.iter() { - if byte != 0 { - return true; - } - } - return false; - } + Io(/// (C-not exported) as ErrorKind doesn't have a reasonable mapping + ::std::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 +#[derive(Clone, Debug, PartialEq)] pub struct Init { - pub(crate) global_features: GlobalFeatures, - pub(crate) local_features: LocalFeatures, + /// The relevant features which the sender supports + pub features: InitFeatures, } /// An error message to be sent or received from a peer -#[derive(Clone)] +#[derive(Clone, Debug, PartialEq)] pub struct ErrorMessage { - pub(crate) channel_id: [u8; 32], - pub(crate) data: String, + /// The channel ID involved in the error + 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 ping message to be sent or received from a peer +#[derive(Clone, Debug, PartialEq)] 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 +#[derive(Clone, Debug, PartialEq)] 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)] +#[derive(Clone, Debug, PartialEq)] 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