@@ -634,16 +634,53 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
634
634
#[ cfg_attr( test, derive( PartialEq ) ) ]
635
635
#[ derive( Clone ) ]
636
636
pub ( super ) enum ChannelMonitorUpdateStep {
637
+ LatestLocalCommitmentTXInfo {
638
+ commitment_tx : LocalCommitmentTransaction ,
639
+ local_keys : chan_utils:: TxCreationKeys ,
640
+ feerate_per_kw : u64 ,
641
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
642
+ } ,
637
643
}
638
644
639
645
impl Writeable for ChannelMonitorUpdateStep {
640
- fn write < W : Writer > ( & self , _w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
646
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
647
+ match self {
648
+ & ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { ref commitment_tx, ref local_keys, ref feerate_per_kw, ref htlc_outputs } => {
649
+ 0u8 . write ( w) ?;
650
+ commitment_tx. write ( w) ?;
651
+ local_keys. write ( w) ?;
652
+ feerate_per_kw. write ( w) ?;
653
+ ( htlc_outputs. len ( ) as u64 ) . write ( w) ?;
654
+ for & ( ref output, ref signature, ref source) in htlc_outputs. iter ( ) {
655
+ output. write ( w) ?;
656
+ signature. write ( w) ?;
657
+ source. write ( w) ?;
658
+ }
659
+ }
660
+ }
641
661
Ok ( ( ) )
642
662
}
643
663
}
644
664
impl < R : :: std:: io:: Read > Readable < R > for ChannelMonitorUpdateStep {
645
- fn read ( _r : & mut R ) -> Result < Self , DecodeError > {
646
- unimplemented ! ( ) // We don't have any enum variants to read (and never provide Monitor Updates)
665
+ fn read ( r : & mut R ) -> Result < Self , DecodeError > {
666
+ match Readable :: read ( r) ? {
667
+ 0u8 => {
668
+ Ok ( ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo {
669
+ commitment_tx : Readable :: read ( r) ?,
670
+ local_keys : Readable :: read ( r) ?,
671
+ feerate_per_kw : Readable :: read ( r) ?,
672
+ htlc_outputs : {
673
+ let len: u64 = Readable :: read ( r) ?;
674
+ let mut res = Vec :: new ( ) ;
675
+ for _ in 0 ..len {
676
+ res. push ( ( Readable :: read ( r) ?, Readable :: read ( r) ?, Readable :: read ( r) ?) ) ;
677
+ }
678
+ res
679
+ } ,
680
+ } )
681
+ } ,
682
+ _ => Err ( DecodeError :: InvalidValue ) ,
683
+ }
647
684
}
648
685
}
649
686
@@ -1295,8 +1332,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1295
1332
/// is important that any clones of this channel monitor (including remote clones) by kept
1296
1333
/// up-to-date as our local commitment transaction is updated.
1297
1334
/// Panics if set_their_to_self_delay has never been called.
1298
- 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 > ) > ) {
1299
- assert ! ( self . their_to_self_delay. is_some( ) ) ;
1335
+ 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 > {
1336
+ if self . their_to_self_delay . is_none ( ) {
1337
+ return Err ( MonitorUpdateError ( "Got a local commitment tx info update before we'd set basic information about the channel" ) ) ;
1338
+ }
1300
1339
self . prev_local_signed_commitment_tx = self . current_local_signed_commitment_tx . take ( ) ;
1301
1340
self . current_local_signed_commitment_tx = Some ( LocalSignedTx {
1302
1341
txid : commitment_tx. txid ( ) ,
@@ -1309,6 +1348,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1309
1348
feerate_per_kw,
1310
1349
htlc_outputs,
1311
1350
} ) ;
1351
+ Ok ( ( ) )
1312
1352
}
1313
1353
1314
1354
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
@@ -1327,6 +1367,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1327
1367
}
1328
1368
for update in updates. updates . drain ( ..) {
1329
1369
match update {
1370
+ ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, local_keys, feerate_per_kw, htlc_outputs } =>
1371
+ self . provide_latest_local_commitment_tx_info ( commitment_tx, local_keys, feerate_per_kw, htlc_outputs) ?,
1330
1372
}
1331
1373
}
1332
1374
self . latest_update_id = updates. update_id ;
@@ -3533,7 +3575,7 @@ mod tests {
3533
3575
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 ( ) ) ;
3534
3576
monitor. their_to_self_delay = Some ( 10 ) ;
3535
3577
3536
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) ;
3578
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
3537
3579
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
3538
3580
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
3539
3581
monitor. provide_latest_remote_commitment_tx_info ( & dummy_tx, preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
@@ -3559,15 +3601,15 @@ mod tests {
3559
3601
3560
3602
// Now update local commitment tx info, pruning only element 18 as we still care about the
3561
3603
// previous commitment tx's preimages too
3562
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) ;
3604
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..5 ] ) ) . unwrap ( ) ;
3563
3605
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8" ) . unwrap ( ) ) ;
3564
3606
monitor. provide_secret ( 281474976710653 , secret. clone ( ) ) . unwrap ( ) ;
3565
3607
assert_eq ! ( monitor. payment_preimages. len( ) , 12 ) ;
3566
3608
test_preimages_exist ! ( & preimages[ 0 ..10 ] , monitor) ;
3567
3609
test_preimages_exist ! ( & preimages[ 18 ..20 ] , monitor) ;
3568
3610
3569
3611
// But if we do it again, we'll prune 5-10
3570
- monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) ;
3612
+ monitor. provide_latest_local_commitment_tx_info ( LocalCommitmentTransaction :: dummy ( ) , dummy_keys ! ( ) , 0 , preimages_to_local_htlcs ! ( preimages[ 0 ..3 ] ) ) . unwrap ( ) ;
3571
3613
secret[ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
3572
3614
monitor. provide_secret ( 281474976710652 , secret. clone ( ) ) . unwrap ( ) ;
3573
3615
assert_eq ! ( monitor. payment_preimages. len( ) , 5 ) ;
0 commit comments