@@ -600,11 +600,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
600
600
to_broadcaster_value_sat : Option < u64 > ,
601
601
to_countersignatory_value_sat : Option < u64 > ,
602
602
} ,
603
- LatestCounterpartyCommitmentTX {
604
- // The dust and non-dust htlcs for that commitment
605
- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
606
- // Contains only the non-dust htlcs
607
- commitment_tx : CommitmentTransaction ,
603
+ LatestCounterpartyCommitment {
604
+ commitment_txs : Vec < CommitmentTransaction > ,
605
+ htlc_data : CommitmentHTLCData ,
608
606
} ,
609
607
PaymentPreimage {
610
608
payment_preimage : PaymentPreimage ,
@@ -639,7 +637,7 @@ impl ChannelMonitorUpdateStep {
639
637
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo" ,
640
638
ChannelMonitorUpdateStep :: LatestHolderCommitment { .. } => "LatestHolderCommitment" ,
641
639
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo" ,
642
- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX " ,
640
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. } => "LatestCounterpartyCommitment " ,
643
641
ChannelMonitorUpdateStep :: PaymentPreimage { .. } => "PaymentPreimage" ,
644
642
ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
645
643
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
@@ -679,9 +677,10 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
679
677
( 5 , ShutdownScript ) => {
680
678
( 0 , scriptpubkey, required) ,
681
679
} ,
682
- ( 6 , LatestCounterpartyCommitmentTX ) => {
683
- ( 0 , htlc_outputs, required_vec) ,
684
- ( 2 , commitment_tx, required) ,
680
+ ( 6 , LatestCounterpartyCommitment ) => {
681
+ ( 1 , commitment_txs, required_vec) ,
682
+ ( 3 , htlc_data, required) ,
683
+ } ,
685
684
( 8 , LatestHolderCommitment ) => {
686
685
( 1 , commitment_txs, required_vec) ,
687
686
( 3 , htlc_data, required) ,
@@ -1768,40 +1767,38 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1768
1767
///
1769
1768
/// This is used to provide the counterparty commitment transaction directly to the monitor
1770
1769
/// before the initial persistence of a new channel.
1771
- pub ( crate ) fn provide_initial_counterparty_commitment_tx<L : Deref >(
1772
- & self , commitment_tx: CommitmentTransaction , logger: & L ,
1773
- ) where L :: Target : Logger
1774
- {
1770
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx (
1771
+ & self , commitment_tx : CommitmentTransaction ,
1772
+ ) {
1775
1773
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1776
- let logger = WithChannelMonitor :: from_impl( logger, & * inner, None ) ;
1777
- inner. provide_initial_counterparty_commitment_tx( commitment_tx, & logger) ;
1774
+ inner. provide_initial_counterparty_commitment_tx ( commitment_tx) ;
1778
1775
}
1779
1776
1780
1777
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
1781
1778
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
1782
1779
/// possibly future revocation/preimage information) to claim outputs where possible.
1783
1780
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1784
1781
#[ cfg( test) ]
1785
- fn provide_latest_counterparty_commitment_tx< L : Deref > (
1782
+ fn provide_latest_counterparty_commitment_tx (
1786
1783
& self ,
1787
1784
txid : Txid ,
1788
1785
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1789
1786
commitment_number : u64 ,
1790
1787
their_per_commitment_point : PublicKey ,
1791
- logger: & L ,
1792
- ) where L :: Target : Logger {
1788
+ ) {
1793
1789
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1794
- let logger = WithChannelMonitor :: from_impl( logger, & * inner, None ) ;
1795
1790
inner. provide_latest_counterparty_commitment_tx (
1796
- txid, htlc_outputs, commitment_number, their_per_commitment_point, & logger )
1791
+ txid, htlc_outputs, commitment_number, their_per_commitment_point)
1797
1792
}
1798
1793
1799
1794
#[ cfg( test) ]
1800
1795
fn provide_latest_holder_commitment_tx (
1801
1796
& self , holder_commitment_tx : HolderCommitmentTransaction ,
1802
1797
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Signature > , Option < HTLCSource > ) > ,
1803
1798
) {
1804
- self . inner. lock( ) . unwrap( ) . provide_latest_holder_commitment_tx( holder_commitment_tx, htlc_outputs, & Vec :: new( ) , Vec :: new( ) )
1799
+ self . inner . lock ( ) . unwrap ( ) . provide_latest_holder_commitment_tx (
1800
+ holder_commitment_tx, htlc_outputs, & Vec :: new ( ) , Vec :: new ( )
1801
+ ) . unwrap ( )
1805
1802
}
1806
1803
1807
1804
/// This is used to provide payment preimage(s) out-of-band during startup without updating the
@@ -3144,9 +3141,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3144
3141
Ok ( ( ) )
3145
3142
}
3146
3143
3147
- fn provide_initial_counterparty_commitment_tx< L : Deref > (
3148
- & mut self , commitment_tx: CommitmentTransaction , logger : & WithChannelMonitor < L > ,
3149
- ) where L :: Target : Logger {
3144
+ fn provide_initial_counterparty_commitment_tx (
3145
+ & mut self , commitment_tx : CommitmentTransaction ,
3146
+ ) {
3150
3147
// We populate this field for downgrades
3151
3148
self . initial_counterparty_commitment_info = Some ( ( commitment_tx. per_commitment_point ( ) ,
3152
3149
commitment_tx. feerate_per_kw ( ) , commitment_tx. to_broadcaster_value_sat ( ) , commitment_tx. to_countersignatory_value_sat ( ) ) ) ;
@@ -3157,16 +3154,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3157
3154
}
3158
3155
3159
3156
self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , Vec :: new ( ) , commitment_tx. commitment_number ( ) ,
3160
- commitment_tx. per_commitment_point( ) , logger ) ;
3157
+ commitment_tx. per_commitment_point ( ) ) ;
3161
3158
// Soon, we will only populate this field
3162
3159
self . initial_counterparty_commitment_tx = Some ( commitment_tx) ;
3163
3160
}
3164
3161
3165
- fn provide_latest_counterparty_commitment_tx<L : Deref >(
3166
- & mut self , txid: Txid ,
3167
- htlc_outputs: Vec <( HTLCOutputInCommitment , Option <Box <HTLCSource >>) >,
3168
- commitment_number: u64 , their_per_commitment_point: PublicKey , logger: & WithChannelMonitor <L >,
3169
- ) where L :: Target : Logger {
3162
+ fn provide_latest_counterparty_commitment_tx (
3163
+ & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
3164
+ commitment_number : u64 , their_per_commitment_point : PublicKey ,
3165
+ ) {
3170
3166
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
3171
3167
// so that a remote monitor doesn't learn anything unless there is a malicious close.
3172
3168
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -3175,11 +3171,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3175
3171
self . counterparty_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
3176
3172
}
3177
3173
3178
- log_trace!( logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs" , txid, commitment_number, htlc_outputs. len( ) ) ;
3179
3174
self . funding . prev_counterparty_commitment_txid = self . funding . current_counterparty_commitment_txid . take ( ) ;
3180
3175
self . funding . current_counterparty_commitment_txid = Some ( txid) ;
3181
3176
self . funding . counterparty_claimable_outpoints . insert ( txid, htlc_outputs. clone ( ) ) ;
3182
3177
self . current_counterparty_commitment_number = commitment_number;
3178
+
3183
3179
//TODO: Merge this into the other per-counterparty-transaction output storage stuff
3184
3180
match self . their_cur_per_commitment_points {
3185
3181
Some ( old_points) => {
@@ -3201,6 +3197,74 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3201
3197
}
3202
3198
}
3203
3199
3200
+ fn update_counterparty_commitment_data (
3201
+ & mut self , commitment_txs : & [ CommitmentTransaction ] , htlc_data : & CommitmentHTLCData ,
3202
+ ) -> Result < ( ) , & ' static str > {
3203
+ if self . pending_funding . len ( ) + 1 != commitment_txs. len ( ) {
3204
+ return Err ( "Commitment transaction(s) mismatch" ) ;
3205
+ }
3206
+
3207
+ let htlcs_for_commitment = |commitment : & CommitmentTransaction | {
3208
+ let mut nondust_htlcs = commitment. nondust_htlcs ( ) . clone ( ) . into_iter ( ) ;
3209
+ let mut sources = htlc_data. nondust_htlc_sources . clone ( ) . into_iter ( ) ;
3210
+ let nondust_htlcs = core:: iter:: from_fn ( move || {
3211
+ let htlc = if let Some ( htlc) = nondust_htlcs. next ( ) {
3212
+ htlc
3213
+ } else {
3214
+ debug_assert ! ( sources. next( ) . is_none( ) ) ;
3215
+ return None ;
3216
+ } ;
3217
+
3218
+ let mut source = None ;
3219
+ if htlc. offered {
3220
+ source = Some ( Box :: new ( sources. next ( ) . expect (
3221
+ "Every offered non-dust HTLC should have a corresponding source"
3222
+ ) ) ) ;
3223
+ }
3224
+
3225
+ Some ( ( htlc, source) )
3226
+ } ) ;
3227
+
3228
+ let dust_htlcs = htlc_data. dust_htlcs . clone ( ) . into_iter ( ) . map ( |( htlc, source) | {
3229
+ ( htlc, source. map ( |source| Box :: new ( source) ) )
3230
+ } ) ;
3231
+
3232
+ nondust_htlcs. chain ( dust_htlcs) . collect :: < Vec < _ > > ( )
3233
+ } ;
3234
+
3235
+ let current_funding_commitment = commitment_txs. first ( ) . unwrap ( ) ;
3236
+ for ( pending_funding, commitment_tx) in self . pending_funding . iter_mut ( ) . zip ( commitment_txs. iter ( ) ) {
3237
+ let trusted_tx = commitment_tx. trust ( ) ;
3238
+ if trusted_tx. commitment_number ( ) != current_funding_commitment. commitment_number ( ) {
3239
+ return Err ( "Commitment number mismatch" ) ;
3240
+ }
3241
+
3242
+ let funding_outpoint_spent =
3243
+ trusted_tx. built_transaction ( ) . transaction . tx_in ( 0 ) . map ( |input| input. previous_output ) . ok ( ) ;
3244
+ let expected_funding_outpoint_spent =
3245
+ pending_funding. channel_parameters . funding_outpoint . map ( |op| op. into_bitcoin_outpoint ( ) ) ;
3246
+ if funding_outpoint_spent != expected_funding_outpoint_spent {
3247
+ return Err ( "Funding outpoint mismatch" ) ;
3248
+ }
3249
+
3250
+ pending_funding. prev_counterparty_commitment_txid =
3251
+ pending_funding. current_counterparty_commitment_txid . take ( ) ;
3252
+ pending_funding. current_counterparty_commitment_txid = Some ( trusted_tx. txid ( ) ) ;
3253
+ pending_funding. counterparty_claimable_outpoints . insert (
3254
+ trusted_tx. txid ( ) , htlcs_for_commitment ( commitment_tx) ,
3255
+ ) ;
3256
+ }
3257
+
3258
+ self . provide_latest_counterparty_commitment_tx (
3259
+ current_funding_commitment. trust ( ) . txid ( ) ,
3260
+ htlcs_for_commitment ( current_funding_commitment) ,
3261
+ current_funding_commitment. commitment_number ( ) ,
3262
+ current_funding_commitment. per_commitment_point ( ) ,
3263
+ ) ;
3264
+
3265
+ Ok ( ( ) )
3266
+ }
3267
+
3204
3268
/// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
3205
3269
/// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
3206
3270
/// is important that any clones of this channel monitor (including remote clones) by kept
@@ -3651,14 +3715,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3651
3715
} ,
3652
3716
// Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitment`.
3653
3717
// For now we just add the code to handle the new updates.
3654
- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
3718
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
3655
3719
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3656
3720
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3657
- self . provide_latest_counterparty_commitment_tx( * commitment_txid, htlc_outputs. clone( ) , * commitment_number, * their_per_commitment_point, logger )
3721
+ self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point)
3658
3722
} ,
3659
- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3660
- log_trace!( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3661
- self . provide_latest_counterparty_commitment_tx( commitment_tx. trust( ) . txid( ) , htlc_outputs. clone( ) , commitment_tx. commitment_number( ) , commitment_tx. per_commitment_point( ) , logger)
3723
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment {
3724
+ commitment_txs, htlc_data,
3725
+ } => {
3726
+ log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment" ) ;
3727
+ if let Err ( e) = self . update_counterparty_commitment_data ( commitment_txs, htlc_data) {
3728
+ log_error ! ( logger, "Failed updating latest counterparty commitment state: {}" , e) ;
3729
+ }
3662
3730
} ,
3663
3731
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
3664
3732
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
@@ -3734,7 +3802,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3734
3802
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. }
3735
3803
|ChannelMonitorUpdateStep :: LatestHolderCommitment { .. }
3736
3804
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3737
- |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3805
+ |ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. }
3738
3806
|ChannelMonitorUpdateStep :: ShutdownScript { .. }
3739
3807
|ChannelMonitorUpdateStep :: CommitmentSecret { .. }
3740
3808
|ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } =>
@@ -3890,7 +3958,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3890
3958
update. updates . iter ( ) . filter_map ( |update| {
3891
3959
// Soon we will drop the first branch here in favor of the second.
3892
3960
// In preparation, we just add the second branch without deleting the first.
3893
- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
3961
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
3894
3962
match update {
3895
3963
& ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid,
3896
3964
ref htlc_outputs, commitment_number, their_per_commitment_point,
@@ -3908,19 +3976,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3908
3976
3909
3977
debug_assert_eq ! ( commitment_tx. trust( ) . txid( ) , commitment_txid) ;
3910
3978
3911
- Some ( commitment_tx)
3979
+ Some ( vec ! [ commitment_tx] )
3912
3980
} ,
3913
- & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX {
3914
- htlc_outputs: _, ref commitment_tx,
3915
- } => {
3916
- Some ( commitment_tx. clone( ) )
3981
+ & ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { ref commitment_txs, .. } => {
3982
+ Some ( commitment_txs. clone ( ) )
3917
3983
} ,
3918
3984
& ChannelMonitorUpdateStep :: RenegotiatedFunding { ref counterparty_commitment_tx, .. } => {
3919
- Some ( counterparty_commitment_tx. clone( ) )
3985
+ Some ( vec ! [ counterparty_commitment_tx. clone( ) ] )
3920
3986
} ,
3921
3987
_ => None ,
3922
3988
}
3923
- } ) . collect( )
3989
+ } ) . flatten ( ) . collect ( )
3924
3990
}
3925
3991
3926
3992
fn sign_to_local_justice_tx (
@@ -5988,9 +6054,9 @@ mod tests {
5988
6054
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
5989
6055
nondust_htlcs. into_iter ( ) . map ( |htlc| ( htlc. clone ( ) , Some ( dummy_sig) , Some ( dummy_source. clone ( ) ) ) ) . collect ( ) ) ;
5990
6056
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
5991
- preimages_slice_to_htlc_outputs!( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger ) ;
6057
+ preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
5992
6058
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
5993
- preimages_slice_to_htlc_outputs!( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key, & logger ) ;
6059
+ preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
5994
6060
for & ( ref preimage, ref hash) in preimages. iter ( ) {
5995
6061
let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & fee_estimator) ;
5996
6062
monitor. provide_payment_preimage_unsafe_legacy (
@@ -6007,7 +6073,7 @@ mod tests {
6007
6073
test_preimages_exist ! ( & preimages[ 15 ..20 ] , monitor) ;
6008
6074
6009
6075
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"3" ) . to_byte_array ( ) ) ,
6010
- preimages_slice_to_htlc_outputs!( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key, & logger ) ;
6076
+ preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
6011
6077
6012
6078
// Now provide a further secret, pruning preimages 15-17
6013
6079
secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964" ) . unwrap ( ) ) ;
@@ -6017,7 +6083,7 @@ mod tests {
6017
6083
test_preimages_exist ! ( & preimages[ 17 ..20 ] , monitor) ;
6018
6084
6019
6085
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"4" ) . to_byte_array ( ) ) ,
6020
- preimages_slice_to_htlc_outputs!( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key, & logger ) ;
6086
+ preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key) ;
6021
6087
6022
6088
// Now update holder commitment tx info, pruning only element 18 as we still care about the
6023
6089
// previous commitment tx's preimages too
0 commit comments