@@ -652,16 +652,53 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
652
652
#[ cfg_attr( test, derive( PartialEq ) ) ]
653
653
#[ derive( Clone ) ]
654
654
pub ( super ) enum ChannelMonitorUpdateStep {
655
+ LatestLocalCommitmentTXInfo {
656
+ commitment_tx : LocalCommitmentTransaction ,
657
+ local_keys : chan_utils:: TxCreationKeys ,
658
+ feerate_per_kw : u64 ,
659
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
660
+ } ,
655
661
}
656
662
657
663
impl Writeable for ChannelMonitorUpdateStep {
658
- fn write < W : Writer > ( & self , _w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
664
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
665
+ match self {
666
+ & ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { ref commitment_tx, ref local_keys, ref feerate_per_kw, ref htlc_outputs } => {
667
+ 0u8 . write ( w) ?;
668
+ commitment_tx. write ( w) ?;
669
+ local_keys. write ( w) ?;
670
+ feerate_per_kw. write ( w) ?;
671
+ ( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
672
+ for & ( ref output, ref signature, ref source) in htlc_outputs. iter ( ) {
673
+ output. write ( w) ?;
674
+ signature. write ( w) ?;
675
+ source. write ( w) ?;
676
+ }
677
+ }
678
+ }
659
679
Ok ( ( ) )
660
680
}
661
681
}
662
682
impl < R : :: std:: io:: Read > Readable < R > for ChannelMonitorUpdateStep {
663
- fn read ( _r : & mut R ) -> Result < Self , DecodeError > {
664
- unimplemented ! ( ) // We don't have any enum variants to read (and never provide Monitor Updates)
683
+ fn read ( r : & mut R ) -> Result < Self , DecodeError > {
684
+ match Readable :: read ( r) ? {
685
+ 0u8 => {
686
+ Ok ( ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo {
687
+ commitment_tx : Readable :: read ( r) ?,
688
+ local_keys : Readable :: read ( r) ?,
689
+ feerate_per_kw : Readable :: read ( r) ?,
690
+ htlc_outputs : {
691
+ let len: u64 = Readable :: read ( r) ?;
692
+ let mut res = Vec :: new ( ) ;
693
+ for _ in 0 ..len {
694
+ res. push ( ( Readable :: read ( r) ?, Readable :: read ( r) ?, Readable :: read ( r) ?) ) ;
695
+ }
696
+ res
697
+ } ,
698
+ } )
699
+ } ,
700
+ _ => Err ( DecodeError :: InvalidValue ) ,
701
+ }
665
702
}
666
703
}
667
704
@@ -1308,8 +1345,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1308
1345
/// is important that any clones of this channel monitor (including remote clones) by kept
1309
1346
/// up-to-date as our local commitment transaction is updated.
1310
1347
/// Panics if set_their_to_self_delay has never been called.
1311
- pub ( super ) fn provide_latest_local_commitment_tx_info ( & mut self , commitment_tx : LocalCommitmentTransaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) {
1312
- assert ! ( self . their_to_self_delay. is_some( ) ) ;
1348
+ pub ( super ) fn provide_latest_local_commitment_tx_info ( & mut self , commitment_tx : LocalCommitmentTransaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ) -> Result < ( ) , MonitorUpdateError > {
1349
+ if self . their_to_self_delay . is_none ( ) {
1350
+ return Err ( MonitorUpdateError ( "Got a local commitment tx info update before we'd set basic information about the channel" ) ) ;
1351
+ }
1313
1352
self . prev_local_signed_commitment_tx = self . current_local_signed_commitment_tx . take ( ) ;
1314
1353
self . current_local_signed_commitment_tx = Some ( LocalSignedTx {
1315
1354
txid : commitment_tx. txid ( ) ,
@@ -1322,6 +1361,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1322
1361
feerate_per_kw,
1323
1362
htlc_outputs,
1324
1363
} ) ;
1364
+ Ok ( ( ) )
1325
1365
}
1326
1366
1327
1367
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -1340,6 +1380,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1340
1380
}
1341
1381
for update in updates. updates . drain ( ..) {
1342
1382
match update {
1383
+ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } =>
1384
+ self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?,
1343
1385
}
1344
1386
}
1345
1387
self . latest_update_id = updates. update_id ;
@@ -3515,7 +3557,7 @@ mod tests {
3515
3557
let mut monitor = ChannelMonitor :: new ( keys, & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) , & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) , & SecretKey :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) , & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) , & PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) , 0 , Script :: new ( ) , logger. clone ( ) ) ;
3516
3558
monitor. their_to_self_delay = Some ( 10 ) ;
3517
3559
3518
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) ;
3560
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
3519
3561
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
3520
3562
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
3521
3563
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
@@ -3541,15 +3583,15 @@ mod tests {
3541
3583
3542
3584
// Now update local commitment tx info, pruning only element 18 as we still care about the
3543
3585
// previous commitment tx's preimages too
3544
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) ;
3586
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) . unwrap ( ) ;
3545
3587
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
3546
3588
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
3547
3589
assert_eq ! ( monitor. payment_preimages. len( ) , 12 ) ;
3548
3590
test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
3549
3591
test_preimages_exist ! ( & preimages[ 18 ..20 ] , monitor) ;
3550
3592
3551
3593
// But if we do it again, we'll prune 5-10
3552
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) ;
3594
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) . unwrap ( ) ;
3553
3595
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
3554
3596
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
3555
3597
assert_eq ! ( monitor. payment_preimages. len( ) , 5 ) ;
0 commit comments