@@ -1384,6 +1384,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13841384 self . list_channels_with_filter ( |& ( _, ref channel) | channel. is_live ( ) )
13851385 }
13861386
1387+ /// Helper function that issues the channel close events
1388+ fn issue_channel_close_events ( & self , channel : & Channel < Signer > , closure_reason : ClosureReason ) {
1389+ let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
1390+ match channel. unbroadcasted_funding ( ) {
1391+ Some ( transaction) => {
1392+ pending_events_lock. push ( events:: Event :: DiscardFunding { channel_id : channel. channel_id ( ) , transaction } )
1393+ } ,
1394+ None => { } ,
1395+ }
1396+ pending_events_lock. push ( events:: Event :: ChannelClosed { channel_id : channel. channel_id ( ) , reason : closure_reason } ) ;
1397+ }
1398+
13871399 fn close_channel_internal ( & self , channel_id : & [ u8 ; 32 ] , target_feerate_sats_per_1000_weight : Option < u32 > ) -> Result < ( ) , APIError > {
13881400 let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
13891401
@@ -1430,12 +1442,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14301442 msg : channel_update
14311443 } ) ;
14321444 }
1433- if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
1434- pending_events_lock. push ( events:: Event :: ChannelClosed {
1435- channel_id : * channel_id,
1436- reason : ClosureReason :: HolderForceClosed
1437- } ) ;
1438- }
1445+ self . issue_channel_close_events ( & channel, ClosureReason :: HolderForceClosed ) ;
14391446 }
14401447 break Ok ( ( ) ) ;
14411448 } ,
@@ -1526,13 +1533,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15261533 if let Some ( short_id) = chan. get ( ) . get_short_channel_id ( ) {
15271534 channel_state. short_to_id . remove ( & short_id) ;
15281535 }
1529- let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
15301536 if peer_node_id. is_some ( ) {
15311537 if let Some ( peer_msg) = peer_msg {
1532- pending_events_lock . push ( events :: Event :: ChannelClosed { channel_id : * channel_id , reason : ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } } ) ;
1538+ self . issue_channel_close_events ( chan . get ( ) , ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } ) ;
15331539 }
15341540 } else {
1535- pending_events_lock . push ( events :: Event :: ChannelClosed { channel_id : * channel_id , reason : ClosureReason :: HolderForceClosed } ) ;
1541+ self . issue_channel_close_events ( chan . get ( ) , ClosureReason :: HolderForceClosed ) ;
15361542 }
15371543 chan. remove_entry ( ) . 1
15381544 } else {
@@ -3705,7 +3711,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
37053711 msg : update
37063712 } ) ;
37073713 }
3708- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : msg . channel_id , reason : ClosureReason :: CooperativeClosure } ) ;
3714+ self . issue_channel_close_events ( & chan , ClosureReason :: CooperativeClosure ) ;
37093715 }
37103716 Ok ( ( ) )
37113717 }
@@ -4117,7 +4123,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41174123 msg : update
41184124 } ) ;
41194125 }
4120- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
4126+ self . issue_channel_close_events ( & chan, ClosureReason :: CommitmentTxConfirmed ) ;
41214127 pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
41224128 node_id : chan. get_counterparty_node_id ( ) ,
41234129 action : msgs:: ErrorAction :: SendErrorMessage {
@@ -4233,12 +4239,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42334239 } ) ;
42344240 }
42354241
4236- if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
4237- pending_events_lock. push ( events:: Event :: ChannelClosed {
4238- channel_id : * channel_id,
4239- reason : ClosureReason :: CooperativeClosure
4240- } ) ;
4241- }
4242+ self . issue_channel_close_events ( chan, ClosureReason :: CooperativeClosure ) ;
42424243
42434244 log_info ! ( self . logger, "Broadcasting {}" , log_tx!( tx) ) ;
42444245 self . tx_broadcaster . broadcast_transaction ( & tx) ;
@@ -4675,7 +4676,7 @@ where
46754676 msg : update
46764677 } ) ;
46774678 }
4678- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : channel. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
4679+ self . issue_channel_close_events ( channel, ClosureReason :: CommitmentTxConfirmed ) ;
46794680 pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
46804681 node_id : channel. get_counterparty_node_id ( ) ,
46814682 action : msgs:: ErrorAction :: SendErrorMessage { msg : e } ,
@@ -4866,7 +4867,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
48664867 msg : update
48674868 } ) ;
48684869 }
4869- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4870+ self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
48704871 false
48714872 } else {
48724873 true
@@ -4881,7 +4882,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
48814882 if let Some ( short_id) = chan. get_short_channel_id ( ) {
48824883 short_to_id. remove ( & short_id) ;
48834884 }
4884- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4885+ self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
48854886 return false ;
48864887 } else {
48874888 no_channels_remain = false ;
0 commit comments