Do not log_debug when we receive duplicate gossip messages
[rust-lightning] / lightning / src / ln / msgs.rs
index 3c1a549ada269adb56220aa3c021be3b9ec2b202..c45e2277fb8db5a307a2b0c13fb546d97d62ec74 100644 (file)
@@ -32,11 +32,13 @@ use bitcoin::hash_types::{Txid, BlockHash};
 
 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
 
+use prelude::*;
 use core::{cmp, fmt};
 use core::fmt::Debug;
 use std::io::Read;
 
 use util::events::MessageSendEventsProvider;
+use util::logger;
 use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt};
 
 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
@@ -45,7 +47,7 @@ use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
 
 /// An error in decoding a message or struct.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
 pub enum DecodeError {
        /// A version byte specified something we don't know how to handle.
        /// Includes unknown realm byte in an OnionHopData packet
@@ -482,6 +484,17 @@ impl Readable for Result<NetAddress, u8> {
        }
 }
 
+impl Readable for NetAddress {
+       fn read<R: Read>(reader: &mut R) -> Result<NetAddress, DecodeError> {
+               match Readable::read(reader) {
+                       Ok(Ok(res)) => Ok(res),
+                       Ok(Err(_)) => Err(DecodeError::UnknownVersion),
+                       Err(e) => Err(e),
+               }
+       }
+}
+
+
 /// The unsigned part of a node_announcement
 #[derive(Clone, Debug, PartialEq)]
 pub struct UnsignedNodeAnnouncement {
@@ -676,7 +689,11 @@ pub enum ErrorAction {
                msg: Option<ErrorMessage>
        },
        /// The peer did something harmless that we weren't able to process, just log and ignore
+       // New code should *not* use this. New code must use IgnoreAndLog, below!
        IgnoreError,
+       /// The peer did something harmless that we weren't able to meaningfully process.
+       /// If the error is logged, log it at the given level.
+       IgnoreAndLog(logger::Level),
        /// The peer did something incorrect. Tell them.
        SendErrorMessage {
                /// The message to send.
@@ -867,6 +884,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
 }
 
 mod fuzzy_internal_msgs {
+       use prelude::*;
        use ln::PaymentSecret;
 
        // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
@@ -1285,20 +1303,17 @@ impl Writeable for OnionHopData {
                                        (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
                                        (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)),
                                        (6, short_channel_id)
-                               });
+                               }, { });
                        },
-                       OnionHopDataFormat::FinalNode { payment_data: Some(ref final_data) } => {
-                               if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
-                               encode_varint_length_prefixed_tlv!(w, {
-                                       (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
-                                       (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)),
-                                       (8, final_data)
-                               });
-                       },
-                       OnionHopDataFormat::FinalNode { payment_data: None } => {
+                       OnionHopDataFormat::FinalNode { ref payment_data } => {
+                               if let Some(final_data) = payment_data {
+                                       if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
+                               }
                                encode_varint_length_prefixed_tlv!(w, {
                                        (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
                                        (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value))
+                               }, {
+                                       (8, payment_data)
                                });
                        },
                }
@@ -1844,6 +1859,7 @@ mod tests {
        use bitcoin::secp256k1::key::{PublicKey,SecretKey};
        use bitcoin::secp256k1::{Secp256k1, Message};
 
+       use prelude::*;
        use std::io::Cursor;
 
        #[test]