A few minor test cleanups and remove new warnings from #32
[rust-lightning] / src / ln / msgs.rs
index ed0b3479b78b82060ad85c49f36570f289198840..bc7c0ed98b3c38052df2fa06f8eccb9078a2bd1f 100644 (file)
@@ -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,
@@ -357,7 +373,9 @@ pub enum ErrorAction {
                msg: UpdateFailHTLC
        },
        /// The peer took some action which made us think they were useless. Disconnect them.
-       DisconnectPeer {},
+       DisconnectPeer,
+       /// The peer did something harmless that we weren't able to process, just log and ignore
+       IgnoreError,
 }
 
 pub struct HandleError { //TODO: rename me
@@ -386,7 +404,7 @@ pub enum HTLCFailChannelUpdate {
 /// A trait to describe an object which can receive channel messages. Messages MAY be called in
 /// paralell when they originate from different their_node_ids, however they MUST NOT be called in
 /// paralell when the two calls have the same their_node_id.
-pub trait ChannelMessageHandler : events::EventsProvider {
+pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
        //Channel init:
        fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<AcceptChannel, HandleError>;
        fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
@@ -419,7 +437,7 @@ pub trait ChannelMessageHandler : events::EventsProvider {
        fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
 }
 
-pub trait RoutingMessageHandler {
+pub trait RoutingMessageHandler : Send + Sync {
        fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<(), HandleError>;
        /// Handle a channel_announcement message, returning true if it should be forwarded on, false
        /// or returning an Err otherwise.
@@ -506,7 +524,7 @@ macro_rules! secp_signature {
 
 impl MsgDecodable for LocalFeatures {
        fn decode(v: &[u8]) -> Result<Self, DecodeError> {
-               if v.len() < 3 { return Err(DecodeError::WrongLength); }
+               if v.len() < 2 { return Err(DecodeError::WrongLength); }
                let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
                if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
                let mut flags = Vec::with_capacity(len);
@@ -528,7 +546,7 @@ impl MsgEncodable for LocalFeatures {
 
 impl MsgDecodable for GlobalFeatures {
        fn decode(v: &[u8]) -> Result<Self, DecodeError> {
-               if v.len() < 3 { return Err(DecodeError::WrongLength); }
+               if v.len() < 2 { return Err(DecodeError::WrongLength); }
                let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
                if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
                let mut flags = Vec::with_capacity(len);
@@ -570,6 +588,54 @@ impl MsgEncodable for Init {
        }
 }
 
+impl MsgDecodable for Ping {
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               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<u8> {
+               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<Self, DecodeError> {
+               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<u8> {
+               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<Self, DecodeError> {
                if v.len() < 2*32+6*8+4+2*2+6*33+1 {