@@ -148,6 +148,16 @@ pub enum ChannelMonitorUpdateErr {
148
148
#[ derive( Debug ) ]
149
149
pub struct MonitorUpdateError ( pub & ' static str ) ;
150
150
151
+ /// An event to be processed by the ChannelManager.
152
+ #[ derive( PartialEq ) ]
153
+ pub enum MonitorEvent {
154
+ /// A monitor event containing an HTLCUpdate.
155
+ HTLCEvent ( HTLCUpdate ) ,
156
+
157
+ /// A monitor event that the Channel's commitment transaction was broadcasted.
158
+ CommitmentTxBroadcasted ( OutPoint ) ,
159
+ }
160
+
151
161
/// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
152
162
/// forward channel and from which info are needed to update HTLC in a backward channel.
153
163
#[ derive( Clone , PartialEq ) ]
@@ -292,12 +302,12 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
292
302
}
293
303
}
294
304
295
- fn get_and_clear_pending_htlcs_updated ( & self ) -> Vec < HTLCUpdate > {
296
- let mut pending_htlcs_updated = Vec :: new ( ) ;
305
+ fn get_and_clear_pending_monitor_events ( & self ) -> Vec < MonitorEvent > {
306
+ let mut pending_monitor_events = Vec :: new ( ) ;
297
307
for chan in self . monitors . lock ( ) . unwrap ( ) . values_mut ( ) {
298
- pending_htlcs_updated . append ( & mut chan. get_and_clear_pending_htlcs_updated ( ) ) ;
308
+ pending_monitor_events . append ( & mut chan. get_and_clear_pending_monitor_events ( ) ) ;
299
309
}
300
- pending_htlcs_updated
310
+ pending_monitor_events
301
311
}
302
312
}
303
313
@@ -729,7 +739,7 @@ impl Readable for ChannelMonitorUpdateStep {
729
739
/// information and are actively monitoring the chain.
730
740
///
731
741
/// Pending Events or updated HTLCs which have not yet been read out by
732
- /// get_and_clear_pending_htlcs_updated or get_and_clear_pending_events are serialized to disk and
742
+ /// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
733
743
/// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
734
744
/// gotten are fully handled before re-serializing the new state.
735
745
pub struct ChannelMonitor < ChanSigner : ChannelKeys > {
@@ -784,7 +794,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
784
794
785
795
payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
786
796
787
- pending_htlcs_updated : Vec < HTLCUpdate > ,
797
+ pending_monitor_events : Vec < MonitorEvent > ,
788
798
pending_events : Vec < events:: Event > ,
789
799
790
800
// Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
@@ -881,9 +891,9 @@ pub trait ManyChannelMonitor: Send + Sync {
881
891
/// with success or failure.
882
892
///
883
893
/// You should probably just call through to
884
- /// ChannelMonitor::get_and_clear_pending_htlcs_updated () for each ChannelMonitor and return
894
+ /// ChannelMonitor::get_and_clear_pending_monitor_events () for each ChannelMonitor and return
885
895
/// the full list.
886
- fn get_and_clear_pending_htlcs_updated ( & self ) -> Vec < HTLCUpdate > ;
896
+ fn get_and_clear_pending_monitor_events ( & self ) -> Vec < MonitorEvent > ;
887
897
}
888
898
889
899
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -914,7 +924,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
914
924
self . current_local_commitment_number != other. current_local_commitment_number ||
915
925
self . current_local_commitment_tx != other. current_local_commitment_tx ||
916
926
self . payment_preimages != other. payment_preimages ||
917
- self . pending_htlcs_updated != other. pending_htlcs_updated ||
927
+ self . pending_monitor_events != other. pending_monitor_events ||
918
928
self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
919
929
self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf ||
920
930
self . outputs_to_watch != other. outputs_to_watch ||
@@ -1070,9 +1080,15 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1070
1080
writer. write_all ( & payment_preimage. 0 [ ..] ) ?;
1071
1081
}
1072
1082
1073
- writer. write_all ( & byte_utils:: be64_to_array ( self . pending_htlcs_updated . len ( ) as u64 ) ) ?;
1074
- for data in self . pending_htlcs_updated . iter ( ) {
1075
- data. write ( writer) ?;
1083
+ writer. write_all ( & byte_utils:: be64_to_array ( self . pending_monitor_events . len ( ) as u64 ) ) ?;
1084
+ for event in self . pending_monitor_events . iter ( ) {
1085
+ match event {
1086
+ MonitorEvent :: HTLCEvent ( upd) => {
1087
+ 0u8 . write ( writer) ?;
1088
+ upd. write ( writer) ?;
1089
+ } ,
1090
+ MonitorEvent :: CommitmentTxBroadcasted ( _) => 1u8 . write ( writer) ?
1091
+ }
1076
1092
}
1077
1093
1078
1094
writer. write_all ( & byte_utils:: be64_to_array ( self . pending_events . len ( ) as u64 ) ) ?;
@@ -1187,7 +1203,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1187
1203
current_local_commitment_number : 0xffff_ffff_ffff - ( ( ( ( local_tx_sequence & 0xffffff ) << 3 * 8 ) | ( local_tx_locktime as u64 & 0xffffff ) ) ^ commitment_transaction_number_obscure_factor) ,
1188
1204
1189
1205
payment_preimages : HashMap :: new ( ) ,
1190
- pending_htlcs_updated : Vec :: new ( ) ,
1206
+ pending_monitor_events : Vec :: new ( ) ,
1191
1207
pending_events : Vec :: new ( ) ,
1192
1208
1193
1209
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
@@ -1350,6 +1366,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1350
1366
{
1351
1367
for tx in self . get_latest_local_commitment_txn ( logger) . iter ( ) {
1352
1368
broadcaster. broadcast_transaction ( tx) ;
1369
+ self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
1353
1370
}
1354
1371
}
1355
1372
@@ -1443,10 +1460,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1443
1460
}
1444
1461
1445
1462
/// Get the list of HTLCs who's status has been updated on chain. This should be called by
1446
- /// ChannelManager via ManyChannelMonitor::get_and_clear_pending_htlcs_updated ().
1447
- pub fn get_and_clear_pending_htlcs_updated ( & mut self ) -> Vec < HTLCUpdate > {
1463
+ /// ChannelManager via ManyChannelMonitor::get_and_clear_pending_monitor_events ().
1464
+ pub fn get_and_clear_pending_monitor_events ( & mut self ) -> Vec < MonitorEvent > {
1448
1465
let mut ret = Vec :: new ( ) ;
1449
- mem:: swap ( & mut ret, & mut self . pending_htlcs_updated ) ;
1466
+ mem:: swap ( & mut ret, & mut self . pending_monitor_events ) ;
1450
1467
ret
1451
1468
}
1452
1469
@@ -1951,11 +1968,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1951
1968
match ev {
1952
1969
OnchainEvent :: HTLCUpdate { htlc_update } => {
1953
1970
log_trace ! ( logger, "HTLC {} failure update has got enough confirmations to be passed upstream" , log_bytes!( ( htlc_update. 1 ) . 0 ) ) ;
1954
- self . pending_htlcs_updated . push ( HTLCUpdate {
1971
+ self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
1955
1972
payment_hash : htlc_update. 1 ,
1956
1973
payment_preimage : None ,
1957
1974
source : htlc_update. 0 ,
1958
- } ) ;
1975
+ } ) ) ;
1959
1976
} ,
1960
1977
OnchainEvent :: MaturingOutput { descriptor } => {
1961
1978
log_trace ! ( logger, "Descriptor {} has got enough confirmations to be passed upstream" , log_spendable!( descriptor) ) ;
@@ -1966,7 +1983,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1966
1983
}
1967
1984
}
1968
1985
}
1969
- self . onchain_tx_handler . block_connected ( txn_matched, claimable_outpoints, height, & * broadcaster, & * fee_estimator, & * logger) ;
1986
+
1987
+ let broadcasted_commit_tx = self . onchain_tx_handler . block_connected ( txn_matched, claimable_outpoints, height, & * broadcaster, & * fee_estimator, & * logger) ;
1988
+ if broadcasted_commit_tx {
1989
+ self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
1990
+ }
1970
1991
1971
1992
self . last_block_hash = block_hash. clone ( ) ;
1972
1993
for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
@@ -1988,7 +2009,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1988
2009
//- maturing spendable output has transaction paying us has been disconnected
1989
2010
}
1990
2011
1991
- self . onchain_tx_handler . block_disconnected ( height, broadcaster, fee_estimator, logger) ;
2012
+ let broadcasted_commit_tx = self . onchain_tx_handler . block_disconnected ( height, broadcaster, fee_estimator, logger) ;
2013
+ if broadcasted_commit_tx {
2014
+ self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
2015
+ }
1992
2016
1993
2017
self . last_block_hash = block_hash. clone ( ) ;
1994
2018
}
@@ -2151,22 +2175,24 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2151
2175
if let Some ( ( source, payment_hash) ) = payment_data {
2152
2176
let mut payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
2153
2177
if accepted_preimage_claim {
2154
- if !self . pending_htlcs_updated . iter ( ) . any ( |update| update. source == source) {
2178
+ if !self . pending_monitor_events . iter ( ) . any (
2179
+ |update| if let & MonitorEvent :: HTLCEvent ( ref upd) = update { upd. source == source } else { false } ) {
2155
2180
payment_preimage. 0 . copy_from_slice ( & input. witness [ 3 ] ) ;
2156
- self . pending_htlcs_updated . push ( HTLCUpdate {
2181
+ self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
2157
2182
source,
2158
2183
payment_preimage : Some ( payment_preimage) ,
2159
2184
payment_hash
2160
- } ) ;
2185
+ } ) ) ;
2161
2186
}
2162
2187
} else if offered_preimage_claim {
2163
- if !self . pending_htlcs_updated . iter ( ) . any ( |update| update. source == source) {
2188
+ if !self . pending_monitor_events . iter ( ) . any (
2189
+ |update| if let & MonitorEvent :: HTLCEvent ( ref upd) = update { upd. source == source } else { false } ) {
2164
2190
payment_preimage. 0 . copy_from_slice ( & input. witness [ 1 ] ) ;
2165
- self . pending_htlcs_updated . push ( HTLCUpdate {
2191
+ self . pending_monitor_events . push ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
2166
2192
source,
2167
2193
payment_preimage : Some ( payment_preimage) ,
2168
2194
payment_hash
2169
- } ) ;
2195
+ } ) ) ;
2170
2196
}
2171
2197
} else {
2172
2198
log_info ! ( logger, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})" , log_bytes!( payment_hash. 0 ) , height + ANTI_REORG_DELAY - 1 ) ;
@@ -2422,10 +2448,15 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
2422
2448
}
2423
2449
}
2424
2450
2425
- let pending_htlcs_updated_len: u64 = Readable :: read ( reader) ?;
2426
- let mut pending_htlcs_updated = Vec :: with_capacity ( cmp:: min ( pending_htlcs_updated_len as usize , MAX_ALLOC_SIZE / ( 32 + 8 * 3 ) ) ) ;
2427
- for _ in 0 ..pending_htlcs_updated_len {
2428
- pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
2451
+ let pending_monitor_events_len: u64 = Readable :: read ( reader) ?;
2452
+ let mut pending_monitor_events = Vec :: with_capacity ( cmp:: min ( pending_monitor_events_len as usize , MAX_ALLOC_SIZE / ( 32 + 8 * 3 ) ) ) ;
2453
+ for _ in 0 ..pending_monitor_events_len {
2454
+ let ev = match <u8 as Readable >:: read ( reader) ? {
2455
+ 0 => MonitorEvent :: HTLCEvent ( Readable :: read ( reader) ?) ,
2456
+ 1 => MonitorEvent :: CommitmentTxBroadcasted ( funding_info. 0 ) ,
2457
+ _ => return Err ( DecodeError :: InvalidValue )
2458
+ } ;
2459
+ pending_monitor_events. push ( ev) ;
2429
2460
}
2430
2461
2431
2462
let pending_events_len: u64 = Readable :: read ( reader) ?;
@@ -2516,7 +2547,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
2516
2547
current_local_commitment_number,
2517
2548
2518
2549
payment_preimages,
2519
- pending_htlcs_updated ,
2550
+ pending_monitor_events ,
2520
2551
pending_events,
2521
2552
2522
2553
onchain_events_waiting_threshold_conf,
0 commit comments