@@ -649,11 +649,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
649
649
to_broadcaster_value_sat : Option < u64 > ,
650
650
to_countersignatory_value_sat : Option < u64 > ,
651
651
} ,
652
- LatestCounterpartyCommitmentTX {
653
- // The dust and non-dust htlcs for that commitment
654
- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
655
- // Contains only the non-dust htlcs
656
- commitment_tx : CommitmentTransaction ,
652
+ LatestCounterpartyCommitment {
653
+ commitment_txs : Vec < CommitmentTransaction > ,
654
+ htlc_data : CommitmentHTLCData ,
657
655
} ,
658
656
PaymentPreimage {
659
657
payment_preimage : PaymentPreimage ,
@@ -689,7 +687,7 @@ impl ChannelMonitorUpdateStep {
689
687
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo" ,
690
688
ChannelMonitorUpdateStep :: LatestHolderCommitment { .. } => "LatestHolderCommitment" ,
691
689
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo" ,
692
- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX " ,
690
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. } => "LatestCounterpartyCommitment " ,
693
691
ChannelMonitorUpdateStep :: PaymentPreimage { .. } => "PaymentPreimage" ,
694
692
ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
695
693
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
@@ -729,9 +727,9 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
729
727
( 5 , ShutdownScript ) => {
730
728
( 0 , scriptpubkey, required) ,
731
729
} ,
732
- ( 6 , LatestCounterpartyCommitmentTX ) => {
733
- ( 0 , htlc_outputs , required_vec) ,
734
- ( 2 , commitment_tx , required) ,
730
+ ( 6 , LatestCounterpartyCommitment ) => {
731
+ ( 1 , commitment_txs , required_vec) ,
732
+ ( 3 , htlc_data , required) ,
735
733
} ,
736
734
( 8 , LatestHolderCommitment ) => {
737
735
( 1 , commitment_txs, required_vec) ,
@@ -1847,34 +1845,29 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1847
1845
///
1848
1846
/// This is used to provide the counterparty commitment transaction directly to the monitor
1849
1847
/// before the initial persistence of a new channel.
1850
- pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1851
- & self , commitment_tx : CommitmentTransaction , logger : & L ,
1852
- ) where
1853
- L :: Target : Logger ,
1854
- {
1848
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx (
1849
+ & self , commitment_tx : CommitmentTransaction ,
1850
+ ) {
1855
1851
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1856
- let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
1857
- inner. provide_initial_counterparty_commitment_tx ( commitment_tx, & logger) ;
1852
+ inner. provide_initial_counterparty_commitment_tx ( commitment_tx) ;
1858
1853
}
1859
1854
1860
1855
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
1861
1856
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
1862
1857
/// possibly future revocation/preimage information) to claim outputs where possible.
1863
1858
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1864
1859
#[ cfg( test) ]
1865
- #[ rustfmt:: skip]
1866
- fn provide_latest_counterparty_commitment_tx < L : Deref > (
1867
- & self ,
1868
- txid : Txid ,
1869
- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1870
- commitment_number : u64 ,
1871
- their_per_commitment_point : PublicKey ,
1872
- logger : & L ,
1873
- ) where L :: Target : Logger {
1860
+ fn provide_latest_counterparty_commitment_tx (
1861
+ & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1862
+ commitment_number : u64 , their_per_commitment_point : PublicKey ,
1863
+ ) {
1874
1864
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1875
- let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
1876
1865
inner. provide_latest_counterparty_commitment_tx (
1877
- txid, htlc_outputs, commitment_number, their_per_commitment_point, & logger)
1866
+ txid,
1867
+ htlc_outputs,
1868
+ commitment_number,
1869
+ their_per_commitment_point,
1870
+ )
1878
1871
}
1879
1872
1880
1873
#[ cfg( test) ]
@@ -3261,9 +3254,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3261
3254
}
3262
3255
3263
3256
#[ rustfmt:: skip]
3264
- fn provide_initial_counterparty_commitment_tx < L : Deref > (
3265
- & mut self , commitment_tx : CommitmentTransaction , logger : & WithChannelMonitor < L > ,
3266
- ) where L :: Target : Logger {
3257
+ fn provide_initial_counterparty_commitment_tx (
3258
+ & mut self , commitment_tx : CommitmentTransaction ,
3259
+ ) {
3267
3260
// We populate this field for downgrades
3268
3261
self . initial_counterparty_commitment_info = Some ( ( commitment_tx. per_commitment_point ( ) ,
3269
3262
commitment_tx. feerate_per_kw ( ) , commitment_tx. to_broadcaster_value_sat ( ) , commitment_tx. to_countersignatory_value_sat ( ) ) ) ;
@@ -3274,17 +3267,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3274
3267
}
3275
3268
3276
3269
self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , Vec :: new ( ) , commitment_tx. commitment_number ( ) ,
3277
- commitment_tx. per_commitment_point ( ) , logger ) ;
3270
+ commitment_tx. per_commitment_point ( ) ) ;
3278
3271
// Soon, we will only populate this field
3279
3272
self . initial_counterparty_commitment_tx = Some ( commitment_tx) ;
3280
3273
}
3281
3274
3282
3275
#[ rustfmt:: skip]
3283
- fn provide_latest_counterparty_commitment_tx < L : Deref > (
3284
- & mut self , txid : Txid ,
3285
- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
3286
- commitment_number : u64 , their_per_commitment_point : PublicKey , logger : & WithChannelMonitor < L > ,
3287
- ) where L :: Target : Logger {
3276
+ fn provide_latest_counterparty_commitment_tx (
3277
+ & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
3278
+ commitment_number : u64 , their_per_commitment_point : PublicKey ,
3279
+ ) {
3288
3280
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
3289
3281
// so that a remote monitor doesn't learn anything unless there is a malicious close.
3290
3282
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -3293,11 +3285,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3293
3285
self . counterparty_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
3294
3286
}
3295
3287
3296
- log_trace ! ( logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs" , txid, commitment_number, htlc_outputs. len( ) ) ;
3297
3288
self . funding . prev_counterparty_commitment_txid = self . funding . current_counterparty_commitment_txid . take ( ) ;
3298
3289
self . funding . current_counterparty_commitment_txid = Some ( txid) ;
3299
- self . funding . counterparty_claimable_outpoints . insert ( txid, htlc_outputs. clone ( ) ) ;
3290
+ self . funding . counterparty_claimable_outpoints . insert ( txid, htlc_outputs) ;
3300
3291
self . current_counterparty_commitment_number = commitment_number;
3292
+
3301
3293
//TODO: Merge this into the other per-counterparty-transaction output storage stuff
3302
3294
match self . their_cur_per_commitment_points {
3303
3295
Some ( old_points) => {
@@ -3319,6 +3311,57 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3319
3311
}
3320
3312
}
3321
3313
3314
+ fn update_counterparty_commitment_data (
3315
+ & mut self , commitment_txs : & [ CommitmentTransaction ] , htlc_data : & CommitmentHTLCData ,
3316
+ ) -> Result < ( ) , & ' static str > {
3317
+ self . verify_matching_commitment_transactions ( commitment_txs. iter ( ) ) ?;
3318
+
3319
+ let htlcs_for_commitment = |commitment : & CommitmentTransaction | {
3320
+ debug_assert ! ( htlc_data. nondust_htlc_sources. len( ) <= commitment. nondust_htlcs( ) . len( ) ) ;
3321
+ let mut nondust_htlcs = commitment. nondust_htlcs ( ) . iter ( ) ;
3322
+ let mut sources = htlc_data. nondust_htlc_sources . iter ( ) ;
3323
+ let nondust_htlcs = core:: iter:: from_fn ( move || {
3324
+ let htlc = nondust_htlcs. next ( ) ?. clone ( ) ;
3325
+ let source = ( !htlc. offered ) . then ( || {
3326
+ let source = sources
3327
+ . next ( )
3328
+ . expect ( "Every inbound non-dust HTLC should have a corresponding source" )
3329
+ . clone ( ) ;
3330
+ Box :: new ( source)
3331
+ } ) ;
3332
+ Some ( ( htlc, source) )
3333
+ } ) ;
3334
+
3335
+ let dust_htlcs = htlc_data. dust_htlcs . iter ( ) . map ( |( htlc, source) | {
3336
+ ( htlc. clone ( ) , source. as_ref ( ) . map ( |source| Box :: new ( source. clone ( ) ) ) )
3337
+ } ) ;
3338
+
3339
+ nondust_htlcs. chain ( dust_htlcs) . collect :: < Vec < _ > > ( )
3340
+ } ;
3341
+
3342
+ let current_funding_commitment_tx = commitment_txs. first ( ) . unwrap ( ) ;
3343
+ self . provide_latest_counterparty_commitment_tx (
3344
+ current_funding_commitment_tx. trust ( ) . txid ( ) ,
3345
+ htlcs_for_commitment ( current_funding_commitment_tx) ,
3346
+ current_funding_commitment_tx. commitment_number ( ) ,
3347
+ current_funding_commitment_tx. per_commitment_point ( ) ,
3348
+ ) ;
3349
+
3350
+ for ( pending_funding, commitment_tx) in
3351
+ self . pending_funding . iter_mut ( ) . zip ( commitment_txs. iter ( ) . skip ( 1 ) )
3352
+ {
3353
+ let commitment_txid = commitment_tx. trust ( ) . txid ( ) ;
3354
+ pending_funding. prev_counterparty_commitment_txid =
3355
+ pending_funding. current_counterparty_commitment_txid . take ( ) ;
3356
+ pending_funding. current_counterparty_commitment_txid = Some ( commitment_txid) ;
3357
+ pending_funding
3358
+ . counterparty_claimable_outpoints
3359
+ . insert ( commitment_txid, htlcs_for_commitment ( commitment_tx) ) ;
3360
+ }
3361
+
3362
+ Ok ( ( ) )
3363
+ }
3364
+
3322
3365
/// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
3323
3366
/// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
3324
3367
/// is important that any clones of this channel monitor (including remote clones) by kept
@@ -3851,14 +3894,24 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3851
3894
} ,
3852
3895
// Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitment`.
3853
3896
// For now we just add the code to handle the new updates.
3854
- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
3897
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
3855
3898
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3856
3899
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3857
- self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point, logger)
3900
+ if self . pending_funding . is_empty ( ) {
3901
+ self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point)
3902
+ } else {
3903
+ log_error ! ( logger, "Received unexpected non-splice counterparty commitment monitor update" ) ;
3904
+ ret = Err ( ( ) ) ;
3905
+ }
3858
3906
} ,
3859
- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3860
- log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3861
- self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , htlc_outputs. clone ( ) , commitment_tx. commitment_number ( ) , commitment_tx. per_commitment_point ( ) , logger)
3907
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment {
3908
+ commitment_txs, htlc_data,
3909
+ } => {
3910
+ log_trace ! ( logger, "Updating ChannelMonitor with {} latest counterparty commitments" , commitment_txs. len( ) ) ;
3911
+ if let Err ( e) = self . update_counterparty_commitment_data ( commitment_txs, htlc_data) {
3912
+ log_error ! ( logger, "Failed updating latest counterparty commitment state: {}" , e) ;
3913
+ ret = Err ( ( ) ) ;
3914
+ }
3862
3915
} ,
3863
3916
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
3864
3917
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
@@ -3934,7 +3987,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3934
3987
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. }
3935
3988
|ChannelMonitorUpdateStep :: LatestHolderCommitment { .. }
3936
3989
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3937
- |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3990
+ |ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. }
3938
3991
|ChannelMonitorUpdateStep :: ShutdownScript { .. }
3939
3992
|ChannelMonitorUpdateStep :: CommitmentSecret { .. }
3940
3993
|ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } =>
@@ -4094,7 +4147,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4094
4147
update. updates . iter ( ) . filter_map ( |update| {
4095
4148
// Soon we will drop the first branch here in favor of the second.
4096
4149
// In preparation, we just add the second branch without deleting the first.
4097
- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
4150
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
4098
4151
match update {
4099
4152
& ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid,
4100
4153
ref htlc_outputs, commitment_number, their_per_commitment_point,
@@ -4112,19 +4165,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4112
4165
4113
4166
debug_assert_eq ! ( commitment_tx. trust( ) . txid( ) , commitment_txid) ;
4114
4167
4115
- Some ( commitment_tx)
4168
+ Some ( vec ! [ commitment_tx] )
4116
4169
} ,
4117
- & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX {
4118
- htlc_outputs : _, ref commitment_tx,
4119
- } => {
4120
- Some ( commitment_tx. clone ( ) )
4170
+ & ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { ref commitment_txs, .. } => {
4171
+ Some ( commitment_txs. clone ( ) )
4121
4172
} ,
4122
4173
& ChannelMonitorUpdateStep :: RenegotiatedFunding { ref counterparty_commitment_tx, .. } => {
4123
- Some ( counterparty_commitment_tx. clone ( ) )
4174
+ Some ( vec ! [ counterparty_commitment_tx. clone( ) ] )
4124
4175
} ,
4125
4176
_ => None ,
4126
4177
}
4127
- } ) . collect ( )
4178
+ } ) . flatten ( ) . collect ( )
4128
4179
}
4129
4180
4130
4181
#[ rustfmt:: skip]
@@ -6248,9 +6299,9 @@ mod tests {
6248
6299
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
6249
6300
& nondust_htlcs. iter ( ) . map ( |htlc| ( htlc. clone ( ) , Some ( dummy_sig) , Some ( dummy_source. clone ( ) ) ) ) . collect :: < Vec < _ > > ( ) ) ;
6250
6301
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
6251
- preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger ) ;
6302
+ preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
6252
6303
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
6253
- preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key, & logger ) ;
6304
+ preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
6254
6305
for & ( ref preimage, ref hash) in preimages. iter ( ) {
6255
6306
let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & fee_estimator) ;
6256
6307
monitor. provide_payment_preimage_unsafe_legacy (
@@ -6267,7 +6318,7 @@ mod tests {
6267
6318
test_preimages_exist ! ( & preimages[ 15 ..20 ] , monitor) ;
6268
6319
6269
6320
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"3" ) . to_byte_array ( ) ) ,
6270
- preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key, & logger ) ;
6321
+ preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
6271
6322
6272
6323
// Now provide a further secret, pruning preimages 15-17
6273
6324
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964" ) . unwrap ( ) ) ;
@@ -6277,7 +6328,7 @@ mod tests {
6277
6328
test_preimages_exist ! ( & preimages[ 17 ..20 ] , monitor) ;
6278
6329
6279
6330
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"4" ) . to_byte_array ( ) ) ,
6280
- preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key, & logger ) ;
6331
+ preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key) ;
6281
6332
6282
6333
// Now update holder commitment tx info, pruning only element 18 as we still care about the
6283
6334
// previous commitment tx's preimages too
0 commit comments