@@ -148,6 +148,16 @@ pub enum ChannelMonitorUpdateErr {
148148#[ derive( Debug ) ]
149149pub struct MonitorUpdateError ( pub & ' static str ) ;
150150
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+
151161/// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
152162/// forward channel and from which info are needed to update HTLC in a backward channel.
153163#[ derive( Clone , PartialEq ) ]
@@ -292,12 +302,12 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
292302 }
293303 }
294304
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 ( ) ;
297307 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 ( ) ) ;
299309 }
300- pending_htlcs_updated
310+ pending_monitor_events
301311 }
302312}
303313
@@ -729,7 +739,7 @@ impl Readable for ChannelMonitorUpdateStep {
729739/// information and are actively monitoring the chain.
730740///
731741/// 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
733743/// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
734744/// gotten are fully handled before re-serializing the new state.
735745pub struct ChannelMonitor < ChanSigner : ChannelKeys > {
@@ -784,7 +794,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
784794
785795 payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
786796
787- pending_htlcs_updated : Vec < HTLCUpdate > ,
797+ pending_monitor_events : Vec < MonitorEvent > ,
788798 pending_events : Vec < events:: Event > ,
789799
790800 // 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 {
881891 /// with success or failure.
882892 ///
883893 /// 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
885895 /// 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 > ;
887897}
888898
889899#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -914,7 +924,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
914924 self . current_local_commitment_number != other. current_local_commitment_number ||
915925 self . current_local_commitment_tx != other. current_local_commitment_tx ||
916926 self . payment_preimages != other. payment_preimages ||
917- self . pending_htlcs_updated != other. pending_htlcs_updated ||
927+ self . pending_monitor_events != other. pending_monitor_events ||
918928 self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
919929 self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf ||
920930 self . outputs_to_watch != other. outputs_to_watch ||
@@ -1070,9 +1080,15 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
10701080 writer. write_all ( & payment_preimage. 0 [ ..] ) ?;
10711081 }
10721082
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+ }
10761092 }
10771093
10781094 writer. write_all ( & byte_utils:: be64_to_array ( self . pending_events . len ( ) as u64 ) ) ?;
@@ -1187,7 +1203,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
11871203 current_local_commitment_number : 0xffff_ffff_ffff - ( ( ( ( local_tx_sequence & 0xffffff ) << 3 * 8 ) | ( local_tx_locktime as u64 & 0xffffff ) ) ^ commitment_transaction_number_obscure_factor) ,
11881204
11891205 payment_preimages : HashMap :: new ( ) ,
1190- pending_htlcs_updated : Vec :: new ( ) ,
1206+ pending_monitor_events : Vec :: new ( ) ,
11911207 pending_events : Vec :: new ( ) ,
11921208
11931209 onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
@@ -1350,6 +1366,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13501366 {
13511367 for tx in self . get_latest_local_commitment_txn ( logger) . iter ( ) {
13521368 broadcaster. broadcast_transaction ( tx) ;
1369+ self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
13531370 }
13541371 }
13551372
@@ -1443,10 +1460,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
14431460 }
14441461
14451462 /// 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 > {
14481465 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 ) ;
14501467 ret
14511468 }
14521469
@@ -1951,11 +1968,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19511968 match ev {
19521969 OnchainEvent :: HTLCUpdate { htlc_update } => {
19531970 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 {
19551972 payment_hash : htlc_update. 1 ,
19561973 payment_preimage : None ,
19571974 source : htlc_update. 0 ,
1958- } ) ;
1975+ } ) ) ;
19591976 } ,
19601977 OnchainEvent :: MaturingOutput { descriptor } => {
19611978 log_trace ! ( logger, "Descriptor {} has got enough confirmations to be passed upstream" , log_spendable!( descriptor) ) ;
@@ -1966,7 +1983,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19661983 }
19671984 }
19681985 }
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+ }
19701991
19711992 self . last_block_hash = block_hash. clone ( ) ;
19721993 for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
@@ -1988,7 +2009,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
19882009 //- maturing spendable output has transaction paying us has been disconnected
19892010 }
19902011
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+ }
19922016
19932017 self . last_block_hash = block_hash. clone ( ) ;
19942018 }
@@ -2151,22 +2175,24 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
21512175 if let Some ( ( source, payment_hash) ) = payment_data {
21522176 let mut payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
21532177 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 } ) {
21552180 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 {
21572182 source,
21582183 payment_preimage : Some ( payment_preimage) ,
21592184 payment_hash
2160- } ) ;
2185+ } ) ) ;
21612186 }
21622187 } 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 } ) {
21642190 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 {
21662192 source,
21672193 payment_preimage : Some ( payment_preimage) ,
21682194 payment_hash
2169- } ) ;
2195+ } ) ) ;
21702196 }
21712197 } else {
21722198 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
24222448 }
24232449 }
24242450
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) ;
24292460 }
24302461
24312462 let pending_events_len: u64 = Readable :: read ( reader) ?;
@@ -2516,7 +2547,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
25162547 current_local_commitment_number,
25172548
25182549 payment_preimages,
2519- pending_htlcs_updated ,
2550+ pending_monitor_events ,
25202551 pending_events,
25212552
25222553 onchain_events_waiting_threshold_conf,
0 commit comments