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