@@ -278,6 +278,10 @@ pub struct ChannelMonitor {
278
278
prev_local_signed_commitment_tx : Option < LocalSignedTx > ,
279
279
current_local_signed_commitment_tx : Option < LocalSignedTx > ,
280
280
281
+ // Used just for ChannelManager to make sure it has the latest channel data during
282
+ // deserialization
283
+ current_remote_commitment_number : u64 ,
284
+
281
285
payment_preimages : HashMap < [ u8 ; 32 ] , [ u8 ; 32 ] > ,
282
286
283
287
destination_script : Script ,
@@ -309,6 +313,7 @@ impl PartialEq for ChannelMonitor {
309
313
self . remote_commitment_txn_on_chain != other. remote_commitment_txn_on_chain ||
310
314
self . remote_hash_commitment_number != other. remote_hash_commitment_number ||
311
315
self . prev_local_signed_commitment_tx != other. prev_local_signed_commitment_tx ||
316
+ self . current_remote_commitment_number != other. current_remote_commitment_number ||
312
317
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
313
318
self . payment_preimages != other. payment_preimages ||
314
319
self . destination_script != other. destination_script
@@ -352,6 +357,7 @@ impl ChannelMonitor {
352
357
353
358
prev_local_signed_commitment_tx : None ,
354
359
current_local_signed_commitment_tx : None ,
360
+ current_remote_commitment_number : 1 << 48 ,
355
361
356
362
payment_preimages : HashMap :: new ( ) ,
357
363
destination_script : destination_script,
@@ -471,6 +477,7 @@ impl ChannelMonitor {
471
477
self . remote_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
472
478
}
473
479
self . remote_claimable_outpoints . insert ( unsigned_commitment_tx. txid ( ) , htlc_outputs) ;
480
+ self . current_remote_commitment_number = commitment_number;
474
481
}
475
482
476
483
/// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
@@ -528,6 +535,8 @@ impl ChannelMonitor {
528
535
if our_min_secret > other_min_secret {
529
536
self . provide_secret ( other_min_secret, other. get_secret ( other_min_secret) . unwrap ( ) , None ) ?;
530
537
}
538
+ // TODO: We should use current_remote_commitment_number and the commitment number out of
539
+ // local transactions to decide how to merge
531
540
if our_min_secret >= other_min_secret {
532
541
self . their_cur_revocation_points = other. their_cur_revocation_points ;
533
542
for ( txid, htlcs) in other. remote_claimable_outpoints . drain ( ) {
@@ -541,6 +550,7 @@ impl ChannelMonitor {
541
550
}
542
551
self . payment_preimages = other. payment_preimages ;
543
552
}
553
+ self . current_remote_commitment_number = cmp:: min ( self . current_remote_commitment_number , other. current_remote_commitment_number ) ;
544
554
Ok ( ( ) )
545
555
}
546
556
@@ -748,6 +758,12 @@ impl ChannelMonitor {
748
758
writer. write_all ( & [ 0 ; 1 ] ) ?;
749
759
}
750
760
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
+
751
767
writer. write_all ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ?;
752
768
for payment_preimage in self . payment_preimages . values ( ) {
753
769
writer. write_all ( payment_preimage) ?;
@@ -806,6 +822,16 @@ impl ChannelMonitor {
806
822
min
807
823
}
808
824
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
+
809
835
/// Attempts to claim a remote commitment transaction's outputs using the revocation key and
810
836
/// data in remote_claimable_outpoints. Will directly claim any HTLC outputs which expire at a
811
837
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
@@ -1290,6 +1316,23 @@ impl ChannelMonitor {
1290
1316
( Vec :: new ( ) , Vec :: new ( ) )
1291
1317
}
1292
1318
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
+
1293
1336
fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > ) {
1294
1337
let mut watch_outputs = Vec :: new ( ) ;
1295
1338
let mut spendable_outputs = Vec :: new ( ) ;
@@ -1576,6 +1619,8 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
1576
1619
_ => return Err ( DecodeError :: InvalidValue ) ,
1577
1620
} ;
1578
1621
1622
+ let current_remote_commitment_number = <U48 as Readable < R > >:: read ( reader) ?. 0 ;
1623
+
1579
1624
let payment_preimages_len: u64 = Readable :: read ( reader) ?;
1580
1625
let mut payment_preimages = HashMap :: with_capacity ( cmp:: min ( payment_preimages_len as usize , MAX_ALLOC_SIZE / 32 ) ) ;
1581
1626
let mut sha = Sha256 :: new ( ) ;
@@ -1612,6 +1657,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
1612
1657
1613
1658
prev_local_signed_commitment_tx,
1614
1659
current_local_signed_commitment_tx,
1660
+ current_remote_commitment_number,
1615
1661
1616
1662
payment_preimages,
1617
1663
0 commit comments