@@ -278,6 +278,10 @@ pub struct ChannelMonitor {
278278 prev_local_signed_commitment_tx : Option < LocalSignedTx > ,
279279 current_local_signed_commitment_tx : Option < LocalSignedTx > ,
280280
281+ // Used just for ChannelManager to make sure it has the latest channel data during
282+ // deserialization
283+ current_remote_commitment_number : u64 ,
284+
281285 payment_preimages : HashMap < [ u8 ; 32 ] , [ u8 ; 32 ] > ,
282286
283287 destination_script : Script ,
@@ -309,6 +313,7 @@ impl PartialEq for ChannelMonitor {
309313 self . remote_commitment_txn_on_chain != other. remote_commitment_txn_on_chain ||
310314 self . remote_hash_commitment_number != other. remote_hash_commitment_number ||
311315 self . prev_local_signed_commitment_tx != other. prev_local_signed_commitment_tx ||
316+ self . current_remote_commitment_number != other. current_remote_commitment_number ||
312317 self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
313318 self . payment_preimages != other. payment_preimages ||
314319 self . destination_script != other. destination_script
@@ -352,6 +357,7 @@ impl ChannelMonitor {
352357
353358 prev_local_signed_commitment_tx : None ,
354359 current_local_signed_commitment_tx : None ,
360+ current_remote_commitment_number : 1 << 48 ,
355361
356362 payment_preimages : HashMap :: new ( ) ,
357363 destination_script : destination_script,
@@ -471,6 +477,7 @@ impl ChannelMonitor {
471477 self . remote_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
472478 }
473479 self . remote_claimable_outpoints . insert ( unsigned_commitment_tx. txid ( ) , htlc_outputs) ;
480+ self . current_remote_commitment_number = commitment_number;
474481 }
475482
476483 /// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
@@ -528,6 +535,8 @@ impl ChannelMonitor {
528535 if our_min_secret > other_min_secret {
529536 self . provide_secret ( other_min_secret, other. get_secret ( other_min_secret) . unwrap ( ) , None ) ?;
530537 }
538+ // TODO: We should use current_remote_commitment_number and the commitment number out of
539+ // local transactions to decide how to merge
531540 if our_min_secret >= other_min_secret {
532541 self . their_cur_revocation_points = other. their_cur_revocation_points ;
533542 for ( txid, htlcs) in other. remote_claimable_outpoints . drain ( ) {
@@ -541,6 +550,7 @@ impl ChannelMonitor {
541550 }
542551 self . payment_preimages = other. payment_preimages ;
543552 }
553+ self . current_remote_commitment_number = cmp:: min ( self . current_remote_commitment_number , other. current_remote_commitment_number ) ;
544554 Ok ( ( ) )
545555 }
546556
@@ -748,6 +758,12 @@ impl ChannelMonitor {
748758 writer. write_all ( & [ 0 ; 1 ] ) ?;
749759 }
750760
761+ if for_local_storage {
762+ writer. write_all ( & byte_utils:: be48_to_array ( self . current_remote_commitment_number ) ) ?;
763+ } else {
764+ writer. write_all ( & byte_utils:: be48_to_array ( 0 ) ) ?;
765+ }
766+
751767 writer. write_all ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ?;
752768 for payment_preimage in self . payment_preimages . values ( ) {
753769 writer. write_all ( payment_preimage) ?;
@@ -806,6 +822,16 @@ impl ChannelMonitor {
806822 min
807823 }
808824
825+ pub ( super ) fn get_cur_remote_commitment_number ( & self ) -> u64 {
826+ self . current_remote_commitment_number
827+ }
828+
829+ pub ( super ) fn get_cur_local_commitment_number ( & self ) -> u64 {
830+ if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
831+ 0xffff_ffff_ffff - ( ( ( ( local_tx. tx . input [ 0 ] . sequence as u64 & 0xffffff ) << 3 * 8 ) | ( local_tx. tx . lock_time as u64 & 0xffffff ) ) ^ self . commitment_transaction_number_obscure_factor )
832+ } else { 0xffff_ffff_ffff }
833+ }
834+
809835 /// Attempts to claim a remote commitment transaction's outputs using the revocation key and
810836 /// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
811837 /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
@@ -1290,6 +1316,23 @@ impl ChannelMonitor {
12901316 ( Vec :: new ( ) , Vec :: new ( ) )
12911317 }
12921318
1319+ /// Used by ChannelManager deserialization to broadcast the latest local state if it's copy of
1320+ /// the Channel was out-of-date.
1321+ pub ( super ) fn get_latest_local_commitment_txn ( & self ) -> Vec < Transaction > {
1322+ if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1323+ let mut res = vec ! [ local_tx. tx. clone( ) ] ;
1324+ match self . key_storage {
1325+ KeyStorage :: PrivMode { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
1326+ res. append ( & mut self . broadcast_by_local_state ( local_tx, prev_latest_per_commitment_point, & Some ( * delayed_payment_base_key) ) . 0 ) ;
1327+ } ,
1328+ _ => panic ! ( "Can only broadcast by local channelmonitor" ) ,
1329+ } ;
1330+ res
1331+ } else {
1332+ Vec :: new ( )
1333+ }
1334+ }
1335+
12931336 fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > ) {
12941337 let mut watch_outputs = Vec :: new ( ) ;
12951338 let mut spendable_outputs = Vec :: new ( ) ;
@@ -1576,6 +1619,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
15761619 _ => return Err ( DecodeError :: InvalidValue ) ,
15771620 } ;
15781621
1622+ let current_remote_commitment_number = <U48 as Readable < R > >:: read ( reader) ?. 0 ;
1623+
15791624 let payment_preimages_len: u64 = Readable :: read ( reader) ?;
15801625 let mut payment_preimages = HashMap :: with_capacity ( cmp:: min ( payment_preimages_len as usize , MAX_ALLOC_SIZE / 32 ) ) ;
15811626 let mut sha = Sha256 :: new ( ) ;
@@ -1612,6 +1657,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
16121657
16131658 prev_local_signed_commitment_tx,
16141659 current_local_signed_commitment_tx,
1660+ current_remote_commitment_number,
16151661
16161662 payment_preimages,
16171663
0 commit comments