Add channel_reestablish + peer_connected events to channel handler
authorMatt Corallo <git@bluematt.me>
Fri, 7 Sep 2018 19:51:40 +0000 (15:51 -0400)
committerMatt Corallo <git@bluematt.me>
Sat, 15 Sep 2018 14:53:16 +0000 (10:53 -0400)
src/ln/channelmanager.rs
src/ln/msgs.rs
src/ln/peer_handler.rs
src/util/test_utils.rs

index 333d4257564041dff3becfef7e8b0c61174c3b3d..c1f09c50eaf6e219750633b7adf37fff71f576f4 100644 (file)
@@ -2123,6 +2123,10 @@ impl ChannelMessageHandler for ChannelManager {
                handle_error!(self, self.internal_announcement_signatures(their_node_id, msg), their_node_id)
        }
 
+       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
+               Ok((None, None, None))
+       }
+
        fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
                let mut new_events = Vec::new();
                let mut failed_channels = Vec::new();
@@ -2184,6 +2188,10 @@ impl ChannelMessageHandler for ChannelManager {
                }
        }
 
+       fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
+               Vec::new()
+       }
+
        fn handle_error(&self, their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
                if msg.channel_id == [0; 32] {
                        for chan in self.list_channels() {
index fc942ab5a2022dbe30eb303c3d4279362c81d881..b4432c7084d9ebf8340b547ec2ca515700650ca0 100644 (file)
@@ -456,13 +456,17 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
        // Channel-to-announce:
        fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
 
-       // Error conditions:
+       // Connection loss/reestablish:
        /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
        /// is believed to be possible in the future (eg they're sending us messages we don't
        /// understand or indicate they require unknown feature bits), no_connection_possible is set
        /// and any outstanding channels should be failed.
        fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
 
+       fn peer_connected(&self, their_node_id: &PublicKey) -> Vec<ChannelReestablish>;
+       fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>), HandleError>;
+
+       // Error:
        fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
 }
 
index 4e710117bb805037a80457086a434de70a0bb29b..81ae8ca3863363e17aeca9ac6fb3bfc61a78bb04 100644 (file)
@@ -465,6 +465,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                                local_features,
                                                                                                        }, 16);
                                                                                                }
+
+                                                                                               for msg in self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap()) {
+                                                                                                       encode_and_send_msg!(msg, 136);
+                                                                                               }
                                                                                        },
                                                                                        17 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
@@ -596,7 +600,31 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
                                                                                                try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));
                                                                                        },
-                                                                                       136 => { }, // TODO: channel_reestablish
+                                                                                       136 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
+                                                                                               let (funding_locked, revoke_and_ack, commitment_update) = try_potential_handleerror!(self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg));
+                                                                                               if let Some(lock_msg) = funding_locked {
+                                                                                                       encode_and_send_msg!(lock_msg, 36);
+                                                                                               }
+                                                                                               if let Some(revoke_msg) = revoke_and_ack {
+                                                                                                       encode_and_send_msg!(revoke_msg, 133);
+                                                                                               }
+                                                                                               match commitment_update {
+                                                                                                       Some(resps) => {
+                                                                                                               for resp in resps.update_add_htlcs {
+                                                                                                                       encode_and_send_msg!(resp, 128);
+                                                                                                               }
+                                                                                                               for resp in resps.update_fulfill_htlcs {
+                                                                                                                       encode_and_send_msg!(resp, 130);
+                                                                                                               }
+                                                                                                               for resp in resps.update_fail_htlcs {
+                                                                                                                       encode_and_send_msg!(resp, 131);
+                                                                                                               }
+                                                                                                               encode_and_send_msg!(resps.commitment_signed, 132);
+                                                                                                       },
+                                                                                                       None => {},
+                                                                                               }
+                                                                                       },
 
                                                                                        // Routing control:
                                                                                        259 => {
index 52e44f92abca02f5fa14e77dc97ecf3b21f728de..476c9e5741dd19bb296eeb108daa35db6332de8a 100644 (file)
@@ -68,7 +68,6 @@ impl TestChannelMessageHandler {
 }
 
 impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
-
        fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
                Err(HandleError { err: "", action: None })
        }
@@ -114,7 +113,13 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
        fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
                Err(HandleError { err: "", action: None })
        }
+       fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
+               Err(HandleError { err: "", action: None })
+       }
        fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
+       fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
+               Vec::new()
+       }
        fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
 }