@@ -4706,31 +4706,52 @@ where
47064706 .lock()
47074707 .unwrap();
47084708 let peer_state = &mut *peer_state_lock;
4709- let channel = peer_state
4710- .channel_by_id
4711- .get_mut(&shutdown_res.channel_id)
4712- .expect("We should only be finishing the closure of a channel which existed");
4713- let funded_chan = channel
4714- .as_funded_mut()
4715- .expect("We should only be finishing the closure of a funded channel here");
4716-
4717- let update_id = funded_chan.get_latest_unblocked_monitor_update_id();
4709+ let channel = peer_state.channel_by_id.get_mut(&shutdown_res.channel_id);
4710+
47184711 let monitor_update_step =
47194712 ChannelMonitorUpdateStep::EventGenerated { event: closed_event.0.clone() };
4720- let monitor_update = ChannelMonitorUpdate {
4721- update_id,
4722- updates: vec![monitor_update_step],
4723- channel_id: Some(shutdown_res.channel_id),
4724- };
4725- handle_new_monitor_update!(
4726- self,
4727- funded_chan.funding.get_funding_txo().unwrap(),
4728- monitor_update,
4729- peer_state_lock,
4730- peer_state,
4731- per_peer_state,
4732- funded_chan
4733- );
4713+ if let Some(channel) = channel {
4714+ let funded_chan = channel
4715+ .as_funded_mut()
4716+ .expect("We should only be finishing the closure of a funded channel here");
4717+
4718+ let update_id = funded_chan.get_latest_unblocked_monitor_update_id();
4719+ let monitor_update = ChannelMonitorUpdate {
4720+ update_id,
4721+ updates: vec![monitor_update_step],
4722+ channel_id: Some(shutdown_res.channel_id),
4723+ };
4724+ handle_new_monitor_update!(
4725+ self,
4726+ funded_chan.funding.get_funding_txo().unwrap(),
4727+ monitor_update,
4728+ peer_state_lock,
4729+ peer_state,
4730+ per_peer_state,
4731+ funded_chan
4732+ );
4733+ } else {
4734+ let update_id = *peer_state
4735+ .closed_channel_monitor_update_ids
4736+ .get(&shutdown_res.channel_id)
4737+ .unwrap();
4738+ let monitor_update = ChannelMonitorUpdate {
4739+ update_id,
4740+ updates: vec![monitor_update_step],
4741+ channel_id: Some(shutdown_res.channel_id),
4742+ };
4743+ handle_post_close_monitor_update!(
4744+ self,
4745+ shutdown_res.channel_funding_txo
4746+ .expect("We must have had a funding txo if we are applying a post-close monitor update"),
4747+ monitor_update,
4748+ peer_state_lock,
4749+ peer_state,
4750+ per_peer_state,
4751+ shutdown_res.counterparty_node_id,
4752+ shutdown_res.channel_id
4753+ );
4754+ }
47344755
47354756 let mut pending_events = self.pending_events.lock().unwrap();
47364757 pending_events.push_back(closed_event);
0 commit comments