@@ -50,7 +50,7 @@ use crate::chain::Filter;
5050use crate :: util:: logger:: { Logger , Record } ;
5151use crate :: util:: ser:: { Readable , ReadableArgs , RequiredWrapper , MaybeReadable , UpgradableRequired , Writer , Writeable , U48 } ;
5252use crate :: util:: byte_utils;
53- use crate :: events:: { Event , EventHandler } ;
53+ use crate :: events:: { ClosureReason , Event , EventHandler } ;
5454use crate :: events:: bump_transaction:: { AnchorDescriptor , BumpTransactionEvent } ;
5555
5656use crate :: prelude:: * ;
@@ -155,6 +155,17 @@ pub enum MonitorEvent {
155155 /// A monitor event containing an HTLCUpdate.
156156 HTLCEvent ( HTLCUpdate ) ,
157157
158+ /// Indicates we broadcasted the channel's latest commitment transaction and thus closed the
159+ /// channel. Holds information about the channel and why it was closed.
160+ HolderForceClosedWithInfo {
161+ /// The reason the channel was closed.
162+ reason : ClosureReason ,
163+ /// The funding outpoint of the channel.
164+ outpoint : OutPoint ,
165+ /// The channel ID of the channel.
166+ channel_id : ChannelId ,
167+ } ,
168+
158169 /// Indicates we broadcasted the channel's latest commitment transaction and thus closed the
159170 /// channel.
160171 HolderForceClosed ( OutPoint ) ,
@@ -184,6 +195,11 @@ impl_writeable_tlv_based_enum_upgradable!(MonitorEvent,
184195 ( 2 , monitor_update_id, required) ,
185196 ( 4 , channel_id, required) ,
186197 } ,
198+ ( 5 , HolderForceClosedWithInfo ) => {
199+ ( 0 , reason, upgradable_required) ,
200+ ( 2 , outpoint, required) ,
201+ ( 4 , channel_id, required) ,
202+ } ,
187203;
188204 ( 2 , HTLCEvent ) ,
189205 ( 4 , HolderForceClosed ) ,
@@ -1059,6 +1075,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
10591075 writer. write_all ( & ( self . pending_monitor_events . iter ( ) . filter ( |ev| match ev {
10601076 MonitorEvent :: HTLCEvent ( _) => true ,
10611077 MonitorEvent :: HolderForceClosed ( _) => true ,
1078+ MonitorEvent :: HolderForceClosedWithInfo { .. } => true ,
10621079 _ => false ,
10631080 } ) . count ( ) as u64 ) . to_be_bytes ( ) ) ?;
10641081 for event in self . pending_monitor_events . iter ( ) {
@@ -1068,6 +1085,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
10681085 upd. write ( writer) ?;
10691086 } ,
10701087 MonitorEvent :: HolderForceClosed ( _) => 1u8 . write ( writer) ?,
1088+ // `HolderForceClosedWithInfo` replaced `HolderForceClosed` in v0.0.122. To keep
1089+ // backwards compatibility, we write a `HolderForceClosed` event along with the
1090+ // `HolderForceClosedWithInfo` event. This is deduplicated in the reader.
1091+ MonitorEvent :: HolderForceClosedWithInfo { .. } => 1u8 . write ( writer) ?,
10711092 _ => { } , // Covered in the TLV writes below
10721093 }
10731094 }
@@ -1099,10 +1120,23 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
10991120 self . lockdown_from_offchain . write ( writer) ?;
11001121 self . holder_tx_signed . write ( writer) ?;
11011122
1123+ // If we have a `HolderForceClosedWithInfo` event, we need to write the `HolderForceClosed` for backwards compatibility.
1124+ let pending_monitor_events = match self . pending_monitor_events . iter ( ) . find ( |ev| match ev {
1125+ MonitorEvent :: HolderForceClosedWithInfo { .. } => true ,
1126+ _ => false ,
1127+ } ) {
1128+ Some ( MonitorEvent :: HolderForceClosedWithInfo { outpoint, .. } ) => {
1129+ let mut pending_monitor_events = self . pending_monitor_events . clone ( ) ;
1130+ pending_monitor_events. push ( MonitorEvent :: HolderForceClosed ( * outpoint) ) ;
1131+ pending_monitor_events
1132+ }
1133+ _ => self . pending_monitor_events . clone ( ) ,
1134+ } ;
1135+
11021136 write_tlv_fields ! ( writer, {
11031137 ( 1 , self . funding_spend_confirmed, option) ,
11041138 ( 3 , self . htlcs_resolved_on_chain, required_vec) ,
1105- ( 5 , self . pending_monitor_events, required_vec) ,
1139+ ( 5 , pending_monitor_events, required_vec) ,
11061140 ( 7 , self . funding_spend_seen, required) ,
11071141 ( 9 , self . counterparty_node_id, option) ,
11081142 ( 11 , self . confirmed_commitment_tx_counterparty_output, option) ,
@@ -2727,7 +2761,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27272761 }
27282762 }
27292763
2730- fn generate_claimable_outpoints_and_watch_outputs ( & mut self ) -> ( Vec < PackageTemplate > , Vec < TransactionOutputs > ) {
2764+ fn generate_claimable_outpoints_and_watch_outputs ( & mut self , reason : ClosureReason ) -> ( Vec < PackageTemplate > , Vec < TransactionOutputs > ) {
27312765 let funding_outp = HolderFundingOutput :: build (
27322766 self . funding_redeemscript . clone ( ) ,
27332767 self . channel_value_satoshis ,
@@ -2739,7 +2773,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27392773 self . best_block . height , self . best_block . height
27402774 ) ;
27412775 let mut claimable_outpoints = vec ! [ commitment_package] ;
2742- self . pending_monitor_events . push ( MonitorEvent :: HolderForceClosed ( self . funding_info . 0 ) ) ;
2776+ let event = MonitorEvent :: HolderForceClosedWithInfo {
2777+ reason,
2778+ outpoint : self . funding_info . 0 ,
2779+ channel_id : self . channel_id ,
2780+ } ;
2781+ self . pending_monitor_events . push ( event) ;
2782+
27432783 // Although we aren't signing the transaction directly here, the transaction will be signed
27442784 // in the claim that is queued to OnchainTxHandler. We set holder_tx_signed here to reject
27452785 // new channel updates.
@@ -2775,7 +2815,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27752815 F :: Target : FeeEstimator ,
27762816 L :: Target : Logger ,
27772817 {
2778- let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
2818+ let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ClosureReason :: HolderForceClosed ) ;
27792819 self . onchain_tx_handler . update_claims_view_from_requests (
27802820 claimable_outpoints, self . best_block . height , self . best_block . height , broadcaster,
27812821 fee_estimator, logger
@@ -3778,7 +3818,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
37783818
37793819 let should_broadcast = self . should_broadcast_holder_commitment_txn ( logger) ;
37803820 if should_broadcast {
3781- let ( mut new_outpoints, mut new_outputs) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
3821+ let ( mut new_outpoints, mut new_outputs) = self . generate_claimable_outpoints_and_watch_outputs ( ClosureReason :: HTLCsTimedOut ) ;
37823822 claimable_outpoints. append ( & mut new_outpoints) ;
37833823 watch_outputs. append ( & mut new_outputs) ;
37843824 }
@@ -4605,6 +4645,16 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
46054645 ( 19 , channel_id, option) ,
46064646 } ) ;
46074647
4648+ // `HolderForceClosedWithInfo` replaced `HolderForceClosed` in v0.0.122. If we have both
4649+ // events, we can remove the `HolderForceClosed` event and just keep the `HolderForceClosedWithInfo`.
4650+ if let Some ( ref mut pending_monitor_events) = pending_monitor_events {
4651+ if pending_monitor_events. iter ( ) . any ( |e| matches ! ( e, MonitorEvent :: HolderForceClosed ( _) ) ) &&
4652+ pending_monitor_events. iter ( ) . any ( |e| matches ! ( e, MonitorEvent :: HolderForceClosedWithInfo { .. } ) )
4653+ {
4654+ pending_monitor_events. retain ( |e| !matches ! ( e, MonitorEvent :: HolderForceClosed ( _) ) ) ;
4655+ }
4656+ }
4657+
46084658 // Monitors for anchor outputs channels opened in v0.0.116 suffered from a bug in which the
46094659 // wrong `counterparty_payment_script` was being tracked. Fix it now on deserialization to
46104660 // give them a chance to recognize the spendable output.
0 commit comments