X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=c43a5dc9c4d4ff5c0b379d6cdc94f31c904bdc12;hb=9373c5993fcc4943f31e20fb0e0367d2bddec596;hp=bc7c0ed98b3c38052df2fa06f8eccb9078a2bd1f;hpb=d8474c9d3c422dddf6cf4b5a4f52157a0277203a;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index bc7c0ed9..c43a5dc9 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -1,44 +1,40 @@ use secp256k1::key::PublicKey; use secp256k1::{Secp256k1, Signature}; -use bitcoin::util::uint::Uint256; +use secp256k1; use bitcoin::util::hash::Sha256dHash; -use bitcoin::network::serialize::{deserialize,serialize}; use bitcoin::blockdata::script::Script; use std::error::Error; -use std::fmt; +use std::{cmp, fmt}; +use std::io::Read; use std::result::Result; -use util::{byte_utils, internal_traits, events}; - -pub trait MsgEncodable { - fn encode(&self) -> Vec; - #[inline] - fn encoded_len(&self) -> usize { self.encode().len() } - #[inline] - fn encode_with_len(&self) -> Vec { - let enc = self.encode(); - let mut res = Vec::with_capacity(enc.len() + 2); - res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16)); - res.extend_from_slice(&enc); - res - } -} +use util::{byte_utils, events}; +use util::ser::{Readable, Writeable, Writer}; + #[derive(Debug)] pub enum DecodeError { /// Unknown realm byte in an OnionHopData packet UnknownRealmByte, + /// Unknown feature mandating we fail to parse message + UnknownRequiredFeature, /// Failed to decode a public key (ie it's invalid) BadPublicKey, /// Failed to decode a signature (ie it's invalid) BadSignature, - /// Buffer not of right length (either too short or too long) - WrongLength, + /// Value expected to be text wasn't decodable as text + BadText, + /// Buffer too short + ShortRead, /// node_announcement included more than one address of a given type! ExtraAddressesPerType, -} -pub trait MsgDecodable: Sized { - fn decode(v: &[u8]) -> Result; + /// A length descriptor in the packet didn't describe the later data correctly + /// (currently only generated in node_announcement) + BadLengthDescriptor, + /// Error from std::io + Io(::std::io::Error), + /// 1 or 0 is not found for boolean value + InvalidValue, } /// Tracks localfeatures which are only in init messages @@ -48,23 +44,23 @@ pub struct LocalFeatures { } impl LocalFeatures { - pub fn new() -> LocalFeatures { + pub(crate) fn new() -> LocalFeatures { LocalFeatures { flags: Vec::new(), } } - pub fn supports_data_loss_protect(&self) -> bool { + pub(crate) fn supports_data_loss_protect(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & 3) != 0 } - pub fn requires_data_loss_protect(&self) -> bool { + pub(crate) fn requires_data_loss_protect(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & 1) != 0 } - pub fn initial_routing_sync(&self) -> bool { + pub(crate) fn initial_routing_sync(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0 } - pub fn set_initial_routing_sync(&mut self) { + pub(crate) fn set_initial_routing_sync(&mut self) { if self.flags.len() == 0 { self.flags.resize(1, 1 << 3); } else { @@ -72,14 +68,14 @@ impl LocalFeatures { } } - pub fn supports_upfront_shutdown_script(&self) -> bool { + pub(crate) fn supports_upfront_shutdown_script(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0 } - pub fn requires_upfront_shutdown_script(&self) -> bool { + pub(crate) fn requires_upfront_shutdown_script(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0 } - pub fn requires_unknown_bits(&self) -> bool { + pub(crate) fn requires_unknown_bits(&self) -> bool { for (idx, &byte) in self.flags.iter().enumerate() { if idx != 0 && (byte & 0x55) != 0 { return true; @@ -90,7 +86,7 @@ impl LocalFeatures { return false; } - pub fn supports_unknown_bits(&self) -> bool { + pub(crate) fn supports_unknown_bits(&self) -> bool { for (idx, &byte) in self.flags.iter().enumerate() { if idx != 0 && byte != 0 { return true; @@ -109,13 +105,13 @@ pub struct GlobalFeatures { } impl GlobalFeatures { - pub fn new() -> GlobalFeatures { + pub(crate) fn new() -> GlobalFeatures { GlobalFeatures { flags: Vec::new(), } } - pub fn requires_unknown_bits(&self) -> bool { + pub(crate) fn requires_unknown_bits(&self) -> bool { for &byte in self.flags.iter() { if (byte & 0x55) != 0 { return true; @@ -124,7 +120,7 @@ impl GlobalFeatures { return false; } - pub fn supports_unknown_bits(&self) -> bool { + pub(crate) fn supports_unknown_bits(&self) -> bool { for &byte in self.flags.iter() { if byte != 0 { return true; @@ -135,151 +131,160 @@ impl GlobalFeatures { } pub struct Init { - pub global_features: GlobalFeatures, - pub local_features: LocalFeatures, + pub(crate) global_features: GlobalFeatures, + pub(crate) local_features: LocalFeatures, +} + +pub struct ErrorMessage { + pub(crate) channel_id: [u8; 32], + pub(crate) data: String, } pub struct Ping { - pub ponglen: u16, - pub byteslen: u16, + pub(crate) ponglen: u16, + pub(crate) byteslen: u16, } pub struct Pong { - pub byteslen: u16, + pub(crate) byteslen: u16, } pub struct OpenChannel { - pub chain_hash: Sha256dHash, - pub temporary_channel_id: Uint256, - pub funding_satoshis: u64, - pub push_msat: u64, - pub dust_limit_satoshis: u64, - pub max_htlc_value_in_flight_msat: u64, - pub channel_reserve_satoshis: u64, - pub htlc_minimum_msat: u64, - pub feerate_per_kw: u32, - pub to_self_delay: u16, - pub max_accepted_htlcs: u16, - pub funding_pubkey: PublicKey, - pub revocation_basepoint: PublicKey, - pub payment_basepoint: PublicKey, - pub delayed_payment_basepoint: PublicKey, - pub htlc_basepoint: PublicKey, - pub first_per_commitment_point: PublicKey, - pub channel_flags: u8, - pub shutdown_scriptpubkey: Option