X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=c43a5dc9c4d4ff5c0b379d6cdc94f31c904bdc12;hb=9373c5993fcc4943f31e20fb0e0367d2bddec596;hp=259f90d7d1040de44443a11c0c116db253c04e71;hpb=f9fc2dfbe20f86ab8bc3e637f3ca33a5a868cb98;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 259f90d7..c43a5dc9 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -1,30 +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; 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}; +use util::{byte_utils, events}; +use util::ser::{Readable, Writeable, Writer}; -pub trait MsgEncodable { - fn encode(&self) -> Vec; -} #[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, - /// Buffer not of right length (either too short or too long) - WrongLength, -} -pub trait MsgDecodable: Sized { - fn decode(v: &[u8]) -> Result; + /// Failed to decode a signature (ie it's invalid) + BadSignature, + /// 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, + /// 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 @@ -34,31 +44,38 @@ 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 supports_initial_routing_sync(&self) -> bool { + 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 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; @@ -69,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; @@ -88,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; @@ -103,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; @@ -114,145 +131,164 @@ 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(crate) ponglen: u16, + pub(crate) byteslen: u16, +} + +pub struct Pong { + 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