@@ -642,16 +642,53 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
642
642
#[ cfg_attr( test, derive( PartialEq ) ) ]
643
643
#[ derive( Clone ) ]
644
644
pub ( super ) enum ChannelMonitorUpdateStep {
645
+ LatestLocalCommitmentTXInfo {
646
+ commitment_tx : LocalCommitmentTransaction ,
647
+ local_keys : chan_utils:: TxCreationKeys ,
648
+ feerate_per_kw : u64 ,
649
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
650
+ } ,
645
651
}
646
652
647
653
impl Writeable for ChannelMonitorUpdateStep {
648
- fn write < W : Writer > ( & self , _w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
654
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
655
+ match self {
656
+ & ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { ref commitment_tx, ref local_keys, ref feerate_per_kw, ref htlc_outputs } => {
657
+ 0u8 . write ( w) ?;
658
+ commitment_tx. write ( w) ?;
659
+ local_keys. write ( w) ?;
660
+ feerate_per_kw. write ( w) ?;
661
+ ( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
662
+ for & ( ref output, ref signature, ref source) in htlc_outputs. iter ( ) {
663
+ output. write ( w) ?;
664
+ signature. write ( w) ?;
665
+ source. write ( w) ?;
666
+ }
667
+ }
668
+ }
649
669
Ok ( ( ) )
650
670
}
651
671
}
652
672
impl < R : :: std:: io:: Read > Readable < R > for ChannelMonitorUpdateStep {
653
- fn read ( _r : & mut R ) -> Result < Self , DecodeError > {
654
- unimplemented ! ( ) // We don't have any enum variants to read (and never provide Monitor Updates)
673
+ fn read ( r : & mut R ) -> Result < Self , DecodeError > {
674
+ match Readable :: read ( r) ? {
675
+ 0u8 => {
676
+ Ok ( ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo {
677
+ commitment_tx : Readable :: read ( r) ?,
678
+ local_keys : Readable :: read ( r) ?,
679
+ feerate_per_kw : Readable :: read ( r) ?,
680
+ htlc_outputs : {
681
+ let len: u64 = Readable :: read ( r) ?;
682
+ let mut res = Vec :: new ( ) ;
683
+ for _ in 0 ..len {
684
+ res. push ( ( Readable :: read ( r) ?, Readable :: read ( r) ?, Readable :: read ( r) ?) ) ;
685
+ }
686
+ res
687
+ } ,
688
+ } )
689
+ } ,
690
+ _ => Err ( DecodeError :: InvalidValue ) ,
691
+ }
655
692
}
656
693
}
657
694
@@ -1298,8 +1335,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1298
1335
/// is important that any clones of this channel monitor (including remote clones) by kept
1299
1336
/// up-to-date as our local commitment transaction is updated.
1300
1337
/// Panics if set_their_to_self_delay has never been called.
1301
- 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 > ) > ) {
1302
- assert ! ( self . their_to_self_delay. is_some( ) ) ;
1338
+ 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 > {
1339
+ if self . their_to_self_delay . is_none ( ) {
1340
+ return Err ( MonitorUpdateError ( "Got a local commitment tx info update before we'd set basic information about the channel" ) ) ;
1341
+ }
1303
1342
self . prev_local_signed_commitment_tx = self . current_local_signed_commitment_tx . take ( ) ;
1304
1343
self . current_local_signed_commitment_tx = Some ( LocalSignedTx {
1305
1344
txid : commitment_tx. txid ( ) ,
@@ -1312,6 +1351,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1312
1351
feerate_per_kw,
1313
1352
htlc_outputs,
1314
1353
} ) ;
1354
+ Ok ( ( ) )
1315
1355
}
1316
1356
1317
1357
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -1330,6 +1370,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1330
1370
}
1331
1371
for update in updates. updates . drain ( ..) {
1332
1372
match update {
1373
+ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } =>
1374
+ self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?,
1333
1375
}
1334
1376
}
1335
1377
self . latest_update_id = updates. update_id ;
@@ -3501,7 +3543,7 @@ mod tests {
3501
3543
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 ( ) ) ;
3502
3544
monitor. their_to_self_delay = Some ( 10 ) ;
3503
3545
3504
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) ;
3546
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
3505
3547
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
3506
3548
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
3507
3549
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
@@ -3527,15 +3569,15 @@ mod tests {
3527
3569
3528
3570
// Now update local commitment tx info, pruning only element 18 as we still care about the
3529
3571
// previous commitment tx's preimages too
3530
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) ;
3572
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) . unwrap ( ) ;
3531
3573
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
3532
3574
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
3533
3575
assert_eq ! ( monitor. payment_preimages. len( ) , 12 ) ;
3534
3576
test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
3535
3577
test_preimages_exist ! ( & preimages[ 18 ..20 ] , monitor) ;
3536
3578
3537
3579
// But if we do it again, we'll prune 5-10
3538
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) ;
3580
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) . unwrap ( ) ;
3539
3581
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
3540
3582
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
3541
3583
assert_eq ! ( monitor. payment_preimages. len( ) , 5 ) ;
0 commit comments