@@ -50,7 +50,7 @@ use crate::chain::Filter;
50
50
use crate :: util:: logger:: { Logger , Record } ;
51
51
use crate :: util:: ser:: { Readable , ReadableArgs , RequiredWrapper , MaybeReadable , UpgradableRequired , Writer , Writeable , U48 } ;
52
52
use crate :: util:: byte_utils;
53
- use crate :: events:: { Event , EventHandler } ;
53
+ use crate :: events:: { ClosureReason , Event , EventHandler } ;
54
54
use crate :: events:: bump_transaction:: { AnchorDescriptor , BumpTransactionEvent } ;
55
55
56
56
use crate :: prelude:: * ;
@@ -155,6 +155,17 @@ pub enum MonitorEvent {
155
155
/// A monitor event containing an HTLCUpdate.
156
156
HTLCEvent ( HTLCUpdate ) ,
157
157
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
+
158
169
/// Indicates we broadcasted the channel's latest commitment transaction and thus closed the
159
170
/// channel.
160
171
HolderForceClosed ( OutPoint ) ,
@@ -184,6 +195,11 @@ impl_writeable_tlv_based_enum_upgradable!(MonitorEvent,
184
195
( 2 , monitor_update_id, required) ,
185
196
( 4 , channel_id, required) ,
186
197
} ,
198
+ ( 5 , HolderForceClosedWithInfo ) => {
199
+ ( 0 , reason, upgradable_required) ,
200
+ ( 2 , outpoint, required) ,
201
+ ( 4 , channel_id, required) ,
202
+ } ,
187
203
;
188
204
( 2 , HTLCEvent ) ,
189
205
( 4 , HolderForceClosed ) ,
@@ -1059,6 +1075,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
1059
1075
writer. write_all ( & ( self . pending_monitor_events . iter ( ) . filter ( |ev| match ev {
1060
1076
MonitorEvent :: HTLCEvent ( _) => true ,
1061
1077
MonitorEvent :: HolderForceClosed ( _) => true ,
1078
+ MonitorEvent :: HolderForceClosedWithInfo { .. } => true ,
1062
1079
_ => false ,
1063
1080
} ) . count ( ) as u64 ) . to_be_bytes ( ) ) ?;
1064
1081
for event in self . pending_monitor_events . iter ( ) {
@@ -1068,6 +1085,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
1068
1085
upd. write ( writer) ?;
1069
1086
} ,
1070
1087
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) ?,
1071
1092
_ => { } , // Covered in the TLV writes below
1072
1093
}
1073
1094
}
@@ -1099,10 +1120,23 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
1099
1120
self . lockdown_from_offchain . write ( writer) ?;
1100
1121
self . holder_tx_signed . write ( writer) ?;
1101
1122
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
+
1102
1136
write_tlv_fields ! ( writer, {
1103
1137
( 1 , self . funding_spend_confirmed, option) ,
1104
1138
( 3 , self . htlcs_resolved_on_chain, required_vec) ,
1105
- ( 5 , self . pending_monitor_events, required_vec) ,
1139
+ ( 5 , pending_monitor_events, required_vec) ,
1106
1140
( 7 , self . funding_spend_seen, required) ,
1107
1141
( 9 , self . counterparty_node_id, option) ,
1108
1142
( 11 , self . confirmed_commitment_tx_counterparty_output, option) ,
@@ -2727,7 +2761,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2727
2761
}
2728
2762
}
2729
2763
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 > ) {
2731
2765
let funding_outp = HolderFundingOutput :: build (
2732
2766
self . funding_redeemscript . clone ( ) ,
2733
2767
self . channel_value_satoshis ,
@@ -2739,7 +2773,13 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2739
2773
self . best_block . height , self . best_block . height
2740
2774
) ;
2741
2775
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
+
2743
2783
// Although we aren't signing the transaction directly here, the transaction will be signed
2744
2784
// in the claim that is queued to OnchainTxHandler. We set holder_tx_signed here to reject
2745
2785
// new channel updates.
@@ -2775,7 +2815,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2775
2815
F :: Target : FeeEstimator ,
2776
2816
L :: Target : Logger ,
2777
2817
{
2778
- let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
2818
+ let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ClosureReason :: HolderForceClosed ) ;
2779
2819
self . onchain_tx_handler . update_claims_view_from_requests (
2780
2820
claimable_outpoints, self . best_block . height , self . best_block . height , broadcaster,
2781
2821
fee_estimator, logger
@@ -3778,7 +3818,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3778
3818
3779
3819
let should_broadcast = self . should_broadcast_holder_commitment_txn ( logger) ;
3780
3820
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 ) ;
3782
3822
claimable_outpoints. append ( & mut new_outpoints) ;
3783
3823
watch_outputs. append ( & mut new_outputs) ;
3784
3824
}
@@ -4605,6 +4645,16 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4605
4645
( 19 , channel_id, option) ,
4606
4646
} ) ;
4607
4647
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
+
4608
4658
// Monitors for anchor outputs channels opened in v0.0.116 suffered from a bug in which the
4609
4659
// wrong `counterparty_payment_script` was being tracked. Fix it now on deserialization to
4610
4660
// give them a chance to recognize the spendable output.
0 commit comments