Add is_permanent field to ChannelClosed message and add NodeFailure
[rust-lightning] / src / ln / msgs.rs
index 5dd3165bfc0977728f8c80b03ba9cfeff3ecdb9f..610158503e6656a462d90f25a41e32bd1cbcd6a7 100644 (file)
@@ -1,9 +1,11 @@
 //! Wire messages, traits representing wire message handlers, and a few error types live here.
+//!
 //! For a normal node you probably don't need to use anything here, however, if you wish to split a
 //! node into an internet-facing route/message socket handling daemon and a separate daemon (or
 //! server entirely) which handles only channel-related messages you may wish to implement
 //! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across
 //! daemons/servers.
+//!
 //! Note that if you go with such an architecture (instead of passing raw socket events to a
 //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
 //! source node_id of the mssage, however this does allow you to significantly reduce bandwidth
@@ -30,16 +32,14 @@ use util::ser::{Readable, Writeable, Writer};
 /// An error in decoding a message or struct.
 #[derive(Debug)]
 pub enum DecodeError {
-       /// Unknown realm byte in an OnionHopData packet
-       UnknownRealmByte,
+       /// A version byte specified something we don't know how to handle.
+       /// Includes unknown realm byte in an OnionHopData packet
+       UnknownVersion,
        /// 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,
-       /// Value expected to be text wasn't decodable as text
-       BadText,
+       /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
+       /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
+       InvalidValue,
        /// Buffer too short
        ShortRead,
        /// node_announcement included more than one address of a given type!
@@ -49,8 +49,6 @@ pub enum DecodeError {
        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
@@ -226,6 +224,7 @@ pub struct FundingSigned {
 }
 
 /// A funding_locked message to be sent or received from a peer
+#[derive(Clone)]
 pub struct FundingLocked {
        pub(crate) channel_id: [u8; 32],
        pub(crate) next_per_commitment_point: PublicKey,
@@ -469,6 +468,7 @@ pub struct CommitmentUpdate {
        pub(crate) update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
        pub(crate) update_fail_htlcs: Vec<UpdateFailHTLC>,
        pub(crate) update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
+       pub(crate) update_fee: Option<UpdateFee>,
        pub(crate) commitment_signed: CommitmentSigned,
 }
 
@@ -485,12 +485,25 @@ pub enum HTLCFailChannelUpdate {
        ChannelClosed {
                /// The short_channel_id which has now closed.
                short_channel_id: u64,
+               /// when this true, this channel should be permanently removed from the
+               /// consideration. Otherwise, this channel can be restored as new channel_update is received
+               is_permanent: bool,
        },
+       /// We received an error which indicated only that a node has failed
+       NodeFailure {
+               /// The node_id that has failed.
+               node_id: PublicKey,
+               /// when this true, node should be permanently removed from the
+               /// consideration. Otherwise, the channels connected to this node can be
+               /// restored as new channel_update is received
+               is_permanent: bool,
+       }
 }
 
-/// A trait to describe an object which can receive channel messages. Messages MAY be called in
-/// parallel when they originate from different their_node_ids, however they MUST NOT be called in
-/// parallel when the two calls have the same their_node_id.
+/// A trait to describe an object which can receive channel messages.
+///
+/// Messages MAY be called in parallel when they originate from different their_node_ids, however
+/// they MUST NOT be called in parallel when the two calls have the same their_node_id.
 pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
        //Channel init:
        /// Handle an incoming open_channel message from the given peer.
@@ -614,16 +627,13 @@ pub(crate) struct OnionErrorPacket {
 impl Error for DecodeError {
        fn description(&self) -> &str {
                match *self {
-                       DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
+                       DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
                        DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
-                       DecodeError::BadPublicKey => "Invalid public key in packet",
-                       DecodeError::BadSignature => "Invalid signature in packet",
-                       DecodeError::BadText => "Invalid text in packet",
+                       DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
                        DecodeError::ShortRead => "Packet extended beyond the provided bytes",
                        DecodeError::ExtraAddressesPerType => "More than one address of a single type",
                        DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
                        DecodeError::Io(ref e) => e.description(),
-                       DecodeError::InvalidValue => "0 or 1 is not found for boolean",
                }
        }
 }
@@ -919,7 +929,7 @@ impl<R: Read> Readable<R> for OnionHopData {
                        realm: {
                                let r: u8 = Readable::read(r)?;
                                if r != 0 {
-                                       return Err(DecodeError::UnknownRealmByte);
+                                       return Err(DecodeError::UnknownVersion);
                                }
                                r
                        },
@@ -1087,7 +1097,7 @@ impl<R: Read> Readable<R> for ErrorMessage {
                                sz = cmp::min(data_len, sz);
                                match String::from_utf8(data[..sz as usize].to_vec()) {
                                        Ok(s) => s,
-                                       Err(_) => return Err(DecodeError::BadText),
+                                       Err(_) => return Err(DecodeError::InvalidValue),
                                }
                        }
                })