Rename HandleError to LightningError to stress already-processed error
[rust-lightning] / src / ln / msgs.rs
index 6aa9e05b242fd7a53018c70cb05dd170d5d49432..6939af13e22d331804529388fa3265e814209358 100644 (file)
@@ -59,19 +59,23 @@ pub struct LocalFeatures {
 }
 
 impl LocalFeatures {
+       /// Create a blank LocalFeatures flags (visibility extended for fuzz tests)
+       #[cfg(not(feature = "fuzztarget"))]
        pub(crate) fn new() -> LocalFeatures {
                LocalFeatures {
-                       flags: Vec::new(),
+                       flags: vec![2 | 1 << 5],
+               }
+       }
+       #[cfg(feature = "fuzztarget")]
+       pub fn new() -> LocalFeatures {
+               LocalFeatures {
+                       flags: vec![2 | 1 << 5],
                }
        }
 
        pub(crate) fn supports_data_loss_protect(&self) -> bool {
                self.flags.len() > 0 && (self.flags[0] & 3) != 0
        }
-       pub(crate) fn requires_data_loss_protect(&self) -> bool {
-               self.flags.len() > 0 && (self.flags[0] & 1) != 0
-       }
-
        pub(crate) fn initial_routing_sync(&self) -> bool {
                self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
        }
@@ -86,8 +90,9 @@ impl LocalFeatures {
        pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
                self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
        }
-       pub(crate) fn requires_upfront_shutdown_script(&self) -> bool {
-               self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0
+       #[cfg(test)]
+       pub(crate) fn unset_upfront_shutdown_script(&mut self) {
+               self.flags[0] ^= 1 << 5;
        }
 
        pub(crate) fn requires_unknown_bits(&self) -> bool {
@@ -520,7 +525,7 @@ pub struct ChannelUpdate {
        pub(crate) contents: UnsignedChannelUpdate,
 }
 
-/// Used to put an error message in a HandleError
+/// Used to put an error message in a LightningError
 #[derive(Clone)]
 pub enum ErrorAction {
        /// The peer took some action which made us think they were useless. Disconnect them.
@@ -538,7 +543,7 @@ pub enum ErrorAction {
 }
 
 /// An Err type for failure to process messages.
-pub struct HandleError { //TODO: rename me
+pub struct LightningError {
        /// A human-readable message describing the error
        pub err: &'static str,
        /// The action which should be taken against the offending peer.
@@ -611,42 +616,42 @@ pub enum OptionalField<T> {
 pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
        //Channel init:
        /// Handle an incoming open_channel message from the given peer.
-       fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<(), HandleError>;
+       fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &OpenChannel) -> Result<(), LightningError>;
        /// Handle an incoming accept_channel message from the given peer.
-       fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
+       fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &AcceptChannel) -> Result<(), LightningError>;
        /// Handle an incoming funding_created message from the given peer.
-       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), HandleError>;
+       fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), LightningError>;
        /// Handle an incoming funding_signed message from the given peer.
-       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
+       fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), LightningError>;
        /// Handle an incoming funding_locked message from the given peer.
-       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), HandleError>;
+       fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), LightningError>;
 
        // Channl close:
        /// Handle an incoming shutdown message from the given peer.
-       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), HandleError>;
+       fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), LightningError>;
        /// Handle an incoming closing_signed message from the given peer.
-       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), HandleError>;
+       fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), LightningError>;
 
        // HTLC handling:
        /// Handle an incoming update_add_htlc message from the given peer.
-       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
+       fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), LightningError>;
        /// Handle an incoming update_fulfill_htlc message from the given peer.
-       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
+       fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), LightningError>;
        /// Handle an incoming update_fail_htlc message from the given peer.
-       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>;
+       fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), LightningError>;
        /// Handle an incoming update_fail_malformed_htlc message from the given peer.
-       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
+       fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), LightningError>;
        /// Handle an incoming commitment_signed message from the given peer.
-       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), HandleError>;
+       fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), LightningError>;
        /// Handle an incoming revoke_and_ack message from the given peer.
-       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), HandleError>;
+       fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), LightningError>;
 
        /// Handle an incoming update_fee message from the given peer.
-       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
+       fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), LightningError>;
 
        // Channel-to-announce:
        /// Handle an incoming announcement_signatures message from the given peer.
-       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
+       fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), LightningError>;
 
        // Connection loss/reestablish:
        /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
@@ -658,7 +663,7 @@ pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Syn
        /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
        fn peer_connected(&self, their_node_id: &PublicKey);
        /// Handle an incoming channel_reestablish message from the given peer.
-       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), HandleError>;
+       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), LightningError>;
 
        // Error:
        /// Handle an incoming error message from the given peer.
@@ -669,13 +674,13 @@ pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Syn
 pub trait RoutingMessageHandler : Send + Sync {
        /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
        /// false or returning an Err otherwise.
-       fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, HandleError>;
+       fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, LightningError>;
        /// Handle a channel_announcement message, returning true if it should be forwarded on, false
        /// or returning an Err otherwise.
-       fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
+       fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, LightningError>;
        /// Handle an incoming channel_update message, returning true if it should be forwarded on,
        /// false or returning an Err otherwise.
-       fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
+       fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
        /// Handle some updates to the route graph that we learned due to an outbound failed payment.
        fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
        /// Gets a subset of the channel announcements and updates required to dump our routing table
@@ -705,7 +710,6 @@ mod fuzzy_internal_msgs {
                pub(crate) data: OnionRealm0HopData,
                pub(crate) hmac: [u8; 32],
        }
-       unsafe impl ::util::internal_traits::NoDealloc for OnionHopData{}
 
        pub struct DecodedOnionErrorPacket {
                pub(crate) hmac: [u8; 32],
@@ -766,7 +770,7 @@ impl fmt::Display for DecodeError {
        }
 }
 
-impl fmt::Debug for HandleError {
+impl fmt::Debug for LightningError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str(self.err)
        }
@@ -2010,9 +2014,9 @@ mod tests {
                        target_value.append(&mut hex::decode("0000").unwrap());
                }
                if initial_routing_sync {
-                       target_value.append(&mut hex::decode("000108").unwrap());
+                       target_value.append(&mut hex::decode("00012a").unwrap());
                } else {
-                       target_value.append(&mut hex::decode("0000").unwrap());
+                       target_value.append(&mut hex::decode("000122").unwrap());
                }
                assert_eq!(encoded_value, target_value);
        }