Skip to content

Commit f564d45

Browse files
committed
Add channel_reestablish + peer_connected events to channel handler
1 parent d105b63 commit f564d45

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/ln/channelmanager.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,10 @@ impl ChannelMessageHandler for ChannelManager {
21232123
handle_error!(self, self.internal_announcement_signatures(their_node_id, msg), their_node_id)
21242124
}
21252125

2126+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
2127+
Ok((None, None, None))
2128+
}
2129+
21262130
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {
21272131
let mut new_events = Vec::new();
21282132
let mut failed_channels = Vec::new();
@@ -2184,6 +2188,10 @@ impl ChannelMessageHandler for ChannelManager {
21842188
}
21852189
}
21862190

2191+
fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
2192+
Vec::new()
2193+
}
2194+
21872195
fn handle_error(&self, their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
21882196
if msg.channel_id == [0; 32] {
21892197
for chan in self.list_channels() {

src/ln/msgs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,17 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
456456
// Channel-to-announce:
457457
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
458458

459-
// Error conditions:
459+
// Connection loss/reestablish:
460460
/// Indicates a connection to the peer failed/an existing connection was lost. If no connection
461461
/// is believed to be possible in the future (eg they're sending us messages we don't
462462
/// understand or indicate they require unknown feature bits), no_connection_possible is set
463463
/// and any outstanding channels should be failed.
464464
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
465465

466+
fn peer_connected(&self, their_node_id: &PublicKey) -> Vec<ChannelReestablish>;
467+
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>), HandleError>;
468+
469+
// Error:
466470
fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
467471
}
468472

src/ln/peer_handler.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
465465
local_features,
466466
}, 16);
467467
}
468+
469+
for msg in self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap()) {
470+
encode_and_send_msg!(msg, 136);
471+
}
468472
},
469473
17 => {
470474
let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
@@ -596,7 +600,31 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
596600
let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
597601
try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg));
598602
},
599-
136 => { }, // TODO: channel_reestablish
603+
136 => {
604+
let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
605+
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));
606+
if let Some(lock_msg) = funding_locked {
607+
encode_and_send_msg!(lock_msg, 36);
608+
}
609+
if let Some(revoke_msg) = revoke_and_ack {
610+
encode_and_send_msg!(revoke_msg, 133);
611+
}
612+
match commitment_update {
613+
Some(resps) => {
614+
for resp in resps.update_add_htlcs {
615+
encode_and_send_msg!(resp, 128);
616+
}
617+
for resp in resps.update_fulfill_htlcs {
618+
encode_and_send_msg!(resp, 130);
619+
}
620+
for resp in resps.update_fail_htlcs {
621+
encode_and_send_msg!(resp, 131);
622+
}
623+
encode_and_send_msg!(resps.commitment_signed, 132);
624+
},
625+
None => {},
626+
}
627+
},
600628

601629
// Routing control:
602630
259 => {

src/util/test_utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ impl TestChannelMessageHandler {
6868
}
6969

7070
impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
71-
7271
fn handle_open_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
7372
Err(HandleError { err: "", action: None })
7473
}
@@ -114,7 +113,13 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
114113
fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
115114
Err(HandleError { err: "", action: None })
116115
}
116+
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>), HandleError> {
117+
Err(HandleError { err: "", action: None })
118+
}
117119
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
120+
fn peer_connected(&self, _their_node_id: &PublicKey) -> Vec<msgs::ChannelReestablish> {
121+
Vec::new()
122+
}
118123
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
119124
}
120125

0 commit comments

Comments
 (0)