@@ -254,6 +254,10 @@ pub struct ChannelMonitor {
254254 prev_local_signed_commitment_tx : Option < LocalSignedTx > ,
255255 current_local_signed_commitment_tx : Option < LocalSignedTx > ,
256256
257+ // Used just for ChannelManager to make sure it has the latest channel data during
258+ // deserialization
259+ current_remote_commitment_number : u64 ,
260+
257261 payment_preimages : HashMap < [ u8 ; 32 ] , [ u8 ; 32 ] > ,
258262
259263 destination_script : Script ,
@@ -286,6 +290,7 @@ impl PartialEq for ChannelMonitor {
286290 self . remote_commitment_txn_on_chain != other. remote_commitment_txn_on_chain ||
287291 self . remote_hash_commitment_number != other. remote_hash_commitment_number ||
288292 self . prev_local_signed_commitment_tx != other. prev_local_signed_commitment_tx ||
293+ self . current_remote_commitment_number != other. current_remote_commitment_number ||
289294 self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
290295 self . payment_preimages != other. payment_preimages ||
291296 self . destination_script != other. destination_script
@@ -327,6 +332,7 @@ impl ChannelMonitor {
327332
328333 prev_local_signed_commitment_tx : None ,
329334 current_local_signed_commitment_tx : None ,
335+ current_remote_commitment_number : 1 << 48 ,
330336
331337 payment_preimages : HashMap :: new ( ) ,
332338 destination_script : destination_script,
@@ -446,6 +452,7 @@ impl ChannelMonitor {
446452 self . remote_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
447453 }
448454 self . remote_claimable_outpoints . insert ( unsigned_commitment_tx. txid ( ) , htlc_outputs) ;
455+ self . current_remote_commitment_number = commitment_number;
449456 }
450457
451458 /// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
@@ -492,6 +499,8 @@ impl ChannelMonitor {
492499 if our_min_secret > other_min_secret {
493500 self . provide_secret ( other_min_secret, other. get_secret ( other_min_secret) . unwrap ( ) , None ) ?;
494501 }
502+ // TODO: We should use current_remote_commitment_number and the commitment number out of
503+ // local transactions to decide how to merge
495504 if our_min_secret >= other_min_secret {
496505 self . their_cur_revocation_points = other. their_cur_revocation_points ;
497506 for ( txid, htlcs) in other. remote_claimable_outpoints . drain ( ) {
@@ -505,6 +514,7 @@ impl ChannelMonitor {
505514 }
506515 self . payment_preimages = other. payment_preimages ;
507516 }
517+ self . current_remote_commitment_number = cmp:: min ( self . current_remote_commitment_number , other. current_remote_commitment_number ) ;
508518 Ok ( ( ) )
509519 }
510520
@@ -699,6 +709,12 @@ impl ChannelMonitor {
699709 writer. write_all ( & [ 0 ; 1 ] ) ?;
700710 }
701711
712+ if for_local_storage {
713+ writer. write_all ( & byte_utils:: be48_to_array ( self . current_remote_commitment_number ) ) ?;
714+ } else {
715+ writer. write_all ( & byte_utils:: be48_to_array ( 0 ) ) ?;
716+ }
717+
702718 writer. write_all ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ?;
703719 for payment_preimage in self . payment_preimages . values ( ) {
704720 writer. write_all ( payment_preimage) ?;
@@ -757,6 +773,16 @@ impl ChannelMonitor {
757773 min
758774 }
759775
776+ pub ( super ) fn get_cur_remote_commitment_number ( & self ) -> u64 {
777+ self . current_remote_commitment_number
778+ }
779+
780+ pub ( super ) fn get_cur_local_commitment_number ( & self ) -> u64 {
781+ if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
782+ 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 )
783+ } else { 0xffff_ffff_ffff }
784+ }
785+
760786 /// Attempts to claim a remote commitment transaction's outputs using the revocation key and
761787 /// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
762788 /// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
@@ -1187,6 +1213,18 @@ impl ChannelMonitor {
11871213 Vec :: new ( )
11881214 }
11891215
1216+ /// Used by ChannelManager deserialization to broadcast the latest local state if it's copy of
1217+ /// the Channel was out-of-date.
1218+ pub ( super ) fn get_latest_local_commitment_txn ( & self ) -> Vec < Transaction > {
1219+ if let & Some ( ref local_tx) = & self . current_local_signed_commitment_tx {
1220+ let mut res = vec ! [ local_tx. tx. clone( ) ] ;
1221+ res. append ( & mut self . broadcast_by_local_state ( local_tx) ) ;
1222+ res
1223+ } else {
1224+ Vec :: new ( )
1225+ }
1226+ }
1227+
11901228 fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface ) -> Vec < ( Sha256dHash , Vec < TxOut > ) > {
11911229 let mut watch_outputs = Vec :: new ( ) ;
11921230 for tx in txn_matched {
@@ -1437,6 +1475,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
14371475 _ => return Err ( DecodeError :: InvalidValue ) ,
14381476 } ;
14391477
1478+ let current_remote_commitment_number = <U48 as Readable < R > >:: read ( reader) ?. 0 ;
1479+
14401480 let payment_preimages_len: u64 = Readable :: read ( reader) ?;
14411481 let mut payment_preimages = HashMap :: with_capacity ( cmp:: min ( payment_preimages_len as usize , MAX_ALLOC_SIZE / 32 ) ) ;
14421482 let mut sha = Sha256 :: new ( ) ;
@@ -1474,6 +1514,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
14741514
14751515 prev_local_signed_commitment_tx,
14761516 current_local_signed_commitment_tx,
1517+ current_remote_commitment_number,
14771518
14781519 payment_preimages,
14791520
0 commit comments