@@ -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
}
@@ -2727,7 +2748,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2727
2748
}
2728
2749
}
2729
2750
2730
- fn generate_claimable_outpoints_and_watch_outputs ( & mut self ) -> ( Vec < PackageTemplate > , Vec < TransactionOutputs > ) {
2751
+ fn generate_claimable_outpoints_and_watch_outputs ( & mut self , is_htlc_timeout : bool ) -> ( Vec < PackageTemplate > , Vec < TransactionOutputs > ) {
2731
2752
let funding_outp = HolderFundingOutput :: build (
2732
2753
self . funding_redeemscript . clone ( ) ,
2733
2754
self . channel_value_satoshis ,
@@ -2739,7 +2760,18 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2739
2760
self . best_block . height ( ) , self . best_block . height ( )
2740
2761
) ;
2741
2762
let mut claimable_outpoints = vec ! [ commitment_package] ;
2742
- self . pending_monitor_events . push ( MonitorEvent :: HolderForceClosed ( self . funding_info . 0 ) ) ;
2763
+ let reason = if is_htlc_timeout {
2764
+ ClosureReason :: HTLCsTimedOut
2765
+ } else {
2766
+ ClosureReason :: HolderForceClosed
2767
+ } ;
2768
+ let event = MonitorEvent :: HolderForceClosedWithInfo {
2769
+ reason,
2770
+ outpoint : self . funding_info . 0 ,
2771
+ channel_id : self . channel_id ,
2772
+ } ;
2773
+ self . pending_monitor_events . push ( event) ;
2774
+
2743
2775
// Although we aren't signing the transaction directly here, the transaction will be signed
2744
2776
// in the claim that is queued to OnchainTxHandler. We set holder_tx_signed here to reject
2745
2777
// new channel updates.
@@ -2775,7 +2807,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2775
2807
F :: Target : FeeEstimator ,
2776
2808
L :: Target : Logger ,
2777
2809
{
2778
- let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
2810
+ let ( claimable_outpoints, _) = self . generate_claimable_outpoints_and_watch_outputs ( false ) ;
2779
2811
self . onchain_tx_handler . update_claims_view_from_requests (
2780
2812
claimable_outpoints, self . best_block . height ( ) , self . best_block . height ( ) , broadcaster,
2781
2813
fee_estimator, logger
@@ -3778,7 +3810,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3778
3810
3779
3811
let should_broadcast = self . should_broadcast_holder_commitment_txn ( logger) ;
3780
3812
if should_broadcast {
3781
- let ( mut new_outpoints, mut new_outputs) = self . generate_claimable_outpoints_and_watch_outputs ( ) ;
3813
+ let ( mut new_outpoints, mut new_outputs) = self . generate_claimable_outpoints_and_watch_outputs ( true ) ;
3782
3814
claimable_outpoints. append ( & mut new_outpoints) ;
3783
3815
watch_outputs. append ( & mut new_outputs) ;
3784
3816
}
@@ -4605,6 +4637,16 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4605
4637
( 19 , channel_id, option) ,
4606
4638
} ) ;
4607
4639
4640
+ // `HolderForceClosedWithInfo` replaced `HolderForceClosed` in v0.0.122. If we have both
4641
+ // events, we can remove the `HolderForceClosed` event and just keep the `HolderForceClosedWithInfo`.
4642
+ if let Some ( ref mut pending_monitor_events) = pending_monitor_events {
4643
+ if pending_monitor_events. iter ( ) . any ( |e| matches ! ( e, MonitorEvent :: HolderForceClosed ( _) ) ) &&
4644
+ pending_monitor_events. iter ( ) . any ( |e| matches ! ( e, MonitorEvent :: HolderForceClosedWithInfo { .. } ) )
4645
+ {
4646
+ pending_monitor_events. retain ( |e| !matches ! ( e, MonitorEvent :: HolderForceClosed ( _) ) ) ;
4647
+ }
4648
+ }
4649
+
4608
4650
// Monitors for anchor outputs channels opened in v0.0.116 suffered from a bug in which the
4609
4651
// wrong `counterparty_payment_script` was being tracked. Fix it now on deserialization to
4610
4652
// give them a chance to recognize the spendable output.
0 commit comments