Skip to content

Commit 7692855

Browse files
committed
f - Check BOLT2 compatibility in close_channel
1 parent 162a32d commit 7692855

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,7 +4451,7 @@ impl<Signer: Sign> Channel<Signer> {
44514451

44524452
/// Begins the shutdown process, getting a message for the remote peer and returning all
44534453
/// holding cell HTLCs for payment failure.
4454-
pub fn get_shutdown<K: Deref>(&mut self, keys_provider: &K) -> Result<(msgs::Shutdown, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
4454+
pub fn get_shutdown<K: Deref>(&mut self, keys_provider: &K, their_features: &InitFeatures) -> Result<(msgs::Shutdown, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
44554455
where K::Target: KeysInterface<Signer = Signer> {
44564456
for htlc in self.pending_outbound_htlcs.iter() {
44574457
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
@@ -4471,16 +4471,30 @@ impl<Signer: Sign> Channel<Signer> {
44714471
return Err(APIError::ChannelUnavailable{err: "Cannot begin shutdown while peer is disconnected or we're waiting on a monitor update, maybe force-close instead?".to_owned()});
44724472
}
44734473

4474-
let monitor_update = if self.shutdown_scriptpubkey.is_none() {
4475-
self.shutdown_scriptpubkey = Some(keys_provider.get_shutdown_scriptpubkey());
4476-
self.latest_monitor_update_id += 1;
4477-
Some(ChannelMonitorUpdate {
4478-
update_id: self.latest_monitor_update_id,
4479-
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
4480-
scriptpubkey: self.get_closing_scriptpubkey(),
4481-
}],
4482-
})
4483-
} else { None };
4474+
let shutdown_scriptpubkey = match self.shutdown_scriptpubkey {
4475+
Some(_) => None,
4476+
None => {
4477+
let shutdown_scriptpubkey = keys_provider.get_shutdown_scriptpubkey();
4478+
if !shutdown_scriptpubkey.is_compatible(their_features) {
4479+
return Err(APIError::APIMisuseError { err: format!("Provided a scriptpubkey format not accepted by peer. script: ({})", shutdown_scriptpubkey.clone().into_inner().to_bytes().to_hex()) });
4480+
}
4481+
Some(shutdown_scriptpubkey)
4482+
},
4483+
};
4484+
4485+
let monitor_update = match shutdown_scriptpubkey {
4486+
Some(shutdown_scriptpubkey) => {
4487+
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
4488+
self.latest_monitor_update_id += 1;
4489+
Some(ChannelMonitorUpdate {
4490+
update_id: self.latest_monitor_update_id,
4491+
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
4492+
scriptpubkey: self.get_closing_scriptpubkey(),
4493+
}],
4494+
})
4495+
},
4496+
None => None,
4497+
};
44844498

44854499
// From here on out, we may not fail!
44864500
if self.channel_state < ChannelState::FundingSent as u32 {

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
12611261
let channel_state = &mut *channel_state_lock;
12621262
match channel_state.by_id.entry(channel_id.clone()) {
12631263
hash_map::Entry::Occupied(mut chan_entry) => {
1264-
let (shutdown_msg, monitor_update, failed_htlcs) = chan_entry.get_mut().get_shutdown(&self.keys_manager)?;
1264+
let counterparty_node_id = chan_entry.get().get_counterparty_node_id();
1265+
let their_features = {
1266+
let per_peer_state = self.per_peer_state.read().unwrap();
1267+
let peer_state = per_peer_state.get(&counterparty_node_id).unwrap().lock().unwrap();
1268+
peer_state.latest_features.clone()
1269+
};
1270+
let (shutdown_msg, monitor_update, failed_htlcs) = chan_entry.get_mut().get_shutdown(&self.keys_manager, &their_features)?;
12651271
channel_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
1266-
node_id: chan_entry.get().get_counterparty_node_id(),
1272+
node_id: counterparty_node_id,
12671273
msg: shutdown_msg
12681274
});
12691275
if let Some(monitor_update) = monitor_update {

0 commit comments

Comments
 (0)