Skip to content

Commit 3d588da

Browse files
committed
Handle Error messages by closing channels as required by BOLT 1
1 parent 8f36386 commit 3d588da

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/ln/channelmanager.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,18 @@ impl ChannelMessageHandler for ChannelManager {
20532053
}
20542054
}
20552055
}
2056+
2057+
fn handle_error(&self, their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
2058+
if msg.channel_id == [0; 32] {
2059+
for chan in self.list_channels() {
2060+
if chan.remote_network_id == *their_node_id {
2061+
self.force_close_channel(&chan.channel_id);
2062+
}
2063+
}
2064+
} else {
2065+
self.force_close_channel(&msg.channel_id);
2066+
}
2067+
}
20562068
}
20572069

20582070
#[cfg(test)]

src/ln/msgs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,14 @@ pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
439439
// Channel-to-announce:
440440
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
441441

442-
// Informational:
442+
// Error conditions:
443443
/// Indicates a connection to the peer failed/an existing connection was lost. If no connection
444444
/// is believed to be possible in the future (eg they're sending us messages we don't
445445
/// understand or indicate they require unknown feature bits), no_connection_possible is set
446446
/// and any outstanding channels should be failed.
447447
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
448+
449+
fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
448450
}
449451

450452
pub trait RoutingMessageHandler : Send + Sync {

src/ln/peer_handler.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,26 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
431431
}
432432
},
433433
17 => {
434-
// Error msg
434+
let msg = try_potential_decodeerror!(msgs::ErrorMessage::decode(&msg_data[2..]));
435+
let mut data_is_printable = msg.data.is_ascii();
436+
if data_is_printable {
437+
for b in msg.data.bytes() {
438+
if b < 32 || b > 126 {
439+
data_is_printable = false;
440+
break;
441+
}
442+
}
443+
}
444+
445+
if data_is_printable {
446+
log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
447+
} else {
448+
log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
449+
}
450+
self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
451+
if msg.channel_id == [0; 32] {
452+
return Err(PeerHandleError{ no_connection_possible: true });
453+
}
435454
},
436455

437456
18 => {

src/util/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
115115
Err(HandleError { err: "", action: None })
116116
}
117117
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
118+
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
118119
}
119120

120121
impl events::EventsProvider for TestChannelMessageHandler {

0 commit comments

Comments
 (0)