@@ -1326,6 +1326,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13261326 self . list_channels_with_filter ( |& ( _, ref channel) | channel. is_live ( ) )
13271327 }
13281328
1329+ /// Helper function that issues the channel close events
1330+ fn issue_channel_close_events ( & self , channel : & Channel < Signer > , closure_reason : ClosureReason ) {
1331+ let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
1332+ match channel. unbroadcasted_funding ( ) {
1333+ Some ( transaction) => {
1334+ pending_events_lock. push ( events:: Event :: DiscardFunding { channel_id : channel. channel_id ( ) , transaction } )
1335+ } ,
1336+ None => { } ,
1337+ }
1338+ pending_events_lock. push ( events:: Event :: ChannelClosed { channel_id : channel. channel_id ( ) , reason : closure_reason } ) ;
1339+ }
1340+
13291341 fn close_channel_internal ( & self , channel_id : & [ u8 ; 32 ] , target_feerate_sats_per_1000_weight : Option < u32 > ) -> Result < ( ) , APIError > {
13301342 let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
13311343
@@ -1372,12 +1384,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13721384 msg : channel_update
13731385 } ) ;
13741386 }
1375- if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
1376- pending_events_lock. push ( events:: Event :: ChannelClosed {
1377- channel_id : * channel_id,
1378- reason : ClosureReason :: HolderForceClosed
1379- } ) ;
1380- }
1387+ self . issue_channel_close_events ( & channel, ClosureReason :: HolderForceClosed ) ;
13811388 }
13821389 break Ok ( ( ) ) ;
13831390 } ,
@@ -1468,13 +1475,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14681475 if let Some ( short_id) = chan. get ( ) . get_short_channel_id ( ) {
14691476 channel_state. short_to_id . remove ( & short_id) ;
14701477 }
1471- let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
14721478 if peer_node_id. is_some ( ) {
14731479 if let Some ( peer_msg) = peer_msg {
1474- pending_events_lock . push ( events :: Event :: ChannelClosed { channel_id : * channel_id , reason : ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } } ) ;
1480+ self . issue_channel_close_events ( chan . get ( ) , ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } ) ;
14751481 }
14761482 } else {
1477- pending_events_lock . push ( events :: Event :: ChannelClosed { channel_id : * channel_id , reason : ClosureReason :: HolderForceClosed } ) ;
1483+ self . issue_channel_close_events ( chan . get ( ) , ClosureReason :: HolderForceClosed ) ;
14781484 }
14791485 chan. remove_entry ( ) . 1
14801486 } else {
@@ -3574,7 +3580,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35743580 msg : update
35753581 } ) ;
35763582 }
3577- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : msg . channel_id , reason : ClosureReason :: CooperativeClosure } ) ;
3583+ self . issue_channel_close_events ( & chan , ClosureReason :: CooperativeClosure ) ;
35783584 }
35793585 Ok ( ( ) )
35803586 }
@@ -3986,7 +3992,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39863992 msg : update
39873993 } ) ;
39883994 }
3989- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
3995+ self . issue_channel_close_events ( & chan, ClosureReason :: CommitmentTxConfirmed ) ;
39903996 pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
39913997 node_id : chan. get_counterparty_node_id ( ) ,
39923998 action : msgs:: ErrorAction :: SendErrorMessage {
@@ -4102,12 +4108,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41024108 } ) ;
41034109 }
41044110
4105- if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
4106- pending_events_lock. push ( events:: Event :: ChannelClosed {
4107- channel_id : * channel_id,
4108- reason : ClosureReason :: CooperativeClosure
4109- } ) ;
4110- }
4111+ self . issue_channel_close_events ( chan, ClosureReason :: CooperativeClosure ) ;
41114112
41124113 log_info ! ( self . logger, "Broadcasting {}" , log_tx!( tx) ) ;
41134114 self . tx_broadcaster . broadcast_transaction ( & tx) ;
@@ -4529,7 +4530,7 @@ where
45294530 msg : update
45304531 } ) ;
45314532 }
4532- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : channel. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
4533+ self . issue_channel_close_events ( channel, ClosureReason :: CommitmentTxConfirmed ) ;
45334534 pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
45344535 node_id : channel. get_counterparty_node_id ( ) ,
45354536 action : msgs:: ErrorAction :: SendErrorMessage { msg : e } ,
@@ -4720,7 +4721,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47204721 msg : update
47214722 } ) ;
47224723 }
4723- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4724+ self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
47244725 false
47254726 } else {
47264727 true
@@ -4735,7 +4736,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47354736 if let Some ( short_id) = chan. get_short_channel_id ( ) {
47364737 short_to_id. remove ( & short_id) ;
47374738 }
4738- self . pending_events . lock ( ) . unwrap ( ) . push ( events :: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4739+ self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
47394740 return false ;
47404741 } else {
47414742 no_channels_remain = false ;
0 commit comments