X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=3a0719854cabde3813efe43fdfd3441ab7ede46f;hb=c3362a122e95e9c7cf0280602f736f435a37f734;hp=f0f1d4890e13370f83cc3c8c633da57f7b03ce20;hpb=d7b33fcd847f5480e438b2df176449d73f582090;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index f0f1d489..3a071985 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -61,9 +61,16 @@ impl LocalFeatures { self.flags.len() > 0 && (self.flags[0] & 1) != 0 } - pub fn supports_initial_routing_sync(&self) -> bool { + pub fn initial_routing_sync(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0 } + pub 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 { self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0 @@ -132,6 +139,15 @@ pub struct Init { pub local_features: LocalFeatures, } +pub struct Ping { + pub ponglen: u16, + pub byteslen: u16, +} + +pub struct Pong { + pub byteslen: u16, +} + pub struct OpenChannel { pub chain_hash: Sha256dHash, pub temporary_channel_id: Uint256, @@ -364,7 +380,7 @@ pub enum ErrorAction { pub struct HandleError { //TODO: rename me pub err: &'static str, - pub msg: Option, //TODO: Make this required and rename it + pub action: Option, //TODO: Make this required } /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment @@ -572,6 +588,54 @@ impl MsgEncodable for Init { } } +impl MsgDecodable for Ping { + fn decode(v: &[u8]) -> Result { + if v.len() < 4 { + return Err(DecodeError::WrongLength); + } + let ponglen = byte_utils::slice_to_be16(&v[0..2]); + let byteslen = byte_utils::slice_to_be16(&v[2..4]); + if v.len() < 4 + byteslen as usize { + return Err(DecodeError::WrongLength); + } + Ok(Self { + ponglen, + byteslen, + }) + } +} +impl MsgEncodable for Ping { + fn encode(&self) -> Vec { + let mut res = Vec::with_capacity(self.byteslen as usize + 2); + res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen)); + res.resize(2 + self.byteslen as usize, 0); + res + } +} + +impl MsgDecodable for Pong { + fn decode(v: &[u8]) -> Result { + if v.len() < 2 { + return Err(DecodeError::WrongLength); + } + let byteslen = byte_utils::slice_to_be16(&v[0..2]); + if v.len() < 2 + byteslen as usize { + return Err(DecodeError::WrongLength); + } + Ok(Self { + byteslen + }) + } +} +impl MsgEncodable for Pong { + fn encode(&self) -> Vec { + let mut res = Vec::with_capacity(self.byteslen as usize + 2); + res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen)); + res.resize(2 + self.byteslen as usize, 0); + res + } +} + impl MsgDecodable for OpenChannel { fn decode(v: &[u8]) -> Result { if v.len() < 2*32+6*8+4+2*2+6*33+1 {