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