@@ -544,6 +544,12 @@ pub(crate) enum ChannelMonitorUpdateStep {
544
544
to_broadcaster_value_sat : Option < u64 > ,
545
545
to_countersignatory_value_sat : Option < u64 > ,
546
546
} ,
547
+ LatestCounterpartyCommitmentTX {
548
+ // The dust and non-dust htlcs for that commitment
549
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
550
+ // Contains only the non-dust htlcs
551
+ commitment_tx : CommitmentTransaction ,
552
+ } ,
547
553
PaymentPreimage {
548
554
payment_preimage : PaymentPreimage ,
549
555
/// If this preimage was from an inbound payment claim, information about the claim should
@@ -571,6 +577,7 @@ impl ChannelMonitorUpdateStep {
571
577
match self {
572
578
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo" ,
573
579
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo" ,
580
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX" ,
574
581
ChannelMonitorUpdateStep :: PaymentPreimage { .. } => "PaymentPreimage" ,
575
582
ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
576
583
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
@@ -609,6 +616,10 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
609
616
( 5 , ShutdownScript ) => {
610
617
( 0 , scriptpubkey, required) ,
611
618
} ,
619
+ ( 6 , LatestCounterpartyCommitmentTX ) => {
620
+ ( 0 , htlc_outputs, required_vec) ,
621
+ ( 2 , commitment_tx, required) ,
622
+ } ,
612
623
) ;
613
624
614
625
/// Indicates whether the balance is derived from a cooperative close, a force-close
@@ -1019,6 +1030,11 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
1019
1030
/// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
1020
1031
/// to_countersignatory_sats)
1021
1032
initial_counterparty_commitment_info : Option < ( PublicKey , u32 , u64 , u64 ) > ,
1033
+ /// Initial counterparty commitment transaction
1034
+ ///
1035
+ /// We previously used the field above to re-build the counterparty commitment transaction,
1036
+ /// we now provide the transaction outright.
1037
+ initial_counterparty_commitment_tx : Option < CommitmentTransaction > ,
1022
1038
1023
1039
/// The first block height at which we had no remaining claimable balances.
1024
1040
balances_empty_height : Option < u32 > ,
@@ -1242,6 +1258,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1242
1258
( 23 , self . holder_pays_commitment_tx_fee, option) ,
1243
1259
( 25 , self . payment_preimages, required) ,
1244
1260
( 27 , self . first_confirmed_funding_txo, required) ,
1261
+ ( 29 , self . initial_counterparty_commitment_tx, option) ,
1245
1262
} ) ;
1246
1263
1247
1264
Ok ( ( ) )
@@ -1454,6 +1471,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1454
1471
best_block,
1455
1472
counterparty_node_id : counterparty_node_id,
1456
1473
initial_counterparty_commitment_info : None ,
1474
+ initial_counterparty_commitment_tx : None ,
1457
1475
balances_empty_height : None ,
1458
1476
1459
1477
failed_back_htlc_ids : new_hash_set ( ) ,
@@ -1486,24 +1504,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1486
1504
}
1487
1505
1488
1506
/// A variant of `Self::provide_latest_counterparty_commitment_tx` used to provide
1489
- /// additional information to the monitor to store in order to recreate the initial
1490
- /// counterparty commitment transaction during persistence (mainly for use in third-party
1491
- /// watchtowers).
1507
+ /// the counterparty commitment transaction to the monitor so that the transaction
1508
+ /// can be retrieved during the initial persistence of the monitor (mainly for use in
1509
+ /// third-party watchtowers).
1492
1510
///
1493
- /// This is used to provide the counterparty commitment information directly to the monitor
1511
+ /// This is used to provide the counterparty commitment transaction directly to the monitor
1494
1512
/// before the initial persistence of a new channel.
1495
1513
pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1496
- & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1497
- commitment_number : u64 , their_cur_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
1498
- to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , logger : & L ,
1499
- )
1500
- where L :: Target : Logger
1514
+ & self , commitment_tx : CommitmentTransaction , logger : & L ,
1515
+ ) where L :: Target : Logger
1501
1516
{
1502
1517
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1503
1518
let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
1504
- inner. provide_initial_counterparty_commitment_tx ( txid,
1505
- htlc_outputs, commitment_number, their_cur_per_commitment_point, feerate_per_kw,
1506
- to_broadcaster_value_sat, to_countersignatory_value_sat, & logger) ;
1519
+ inner. provide_initial_counterparty_commitment_tx ( commitment_tx, & logger) ;
1507
1520
}
1508
1521
1509
1522
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
@@ -2872,20 +2885,21 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2872
2885
}
2873
2886
2874
2887
fn provide_initial_counterparty_commitment_tx < L : Deref > (
2875
- & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
2876
- commitment_number : u64 , their_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
2877
- to_broadcaster_value : u64 , to_countersignatory_value : u64 , logger : & WithChannelMonitor < L > ,
2888
+ & mut self , commitment_tx : CommitmentTransaction , logger : & WithChannelMonitor < L > ,
2878
2889
) where L :: Target : Logger {
2879
- self . initial_counterparty_commitment_info = Some ( ( their_per_commitment_point. clone ( ) ,
2880
- feerate_per_kw, to_broadcaster_value, to_countersignatory_value) ) ;
2890
+ // We populate this field for downgrades
2891
+ self . initial_counterparty_commitment_info = Some ( ( commitment_tx. per_commitment_point ( ) ,
2892
+ commitment_tx. feerate_per_kw ( ) , commitment_tx. to_broadcaster_value_sat ( ) , commitment_tx. to_countersignatory_value_sat ( ) ) ) ;
2881
2893
2882
2894
#[ cfg( debug_assertions) ] {
2883
2895
let rebuilt_commitment_tx = self . initial_counterparty_commitment_tx ( ) . unwrap ( ) ;
2884
- debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , txid) ;
2896
+ debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , commitment_tx . trust ( ) . txid( ) ) ;
2885
2897
}
2886
2898
2887
- self . provide_latest_counterparty_commitment_tx ( txid, htlc_outputs, commitment_number,
2888
- their_per_commitment_point, logger) ;
2899
+ self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , Vec :: new ( ) , commitment_tx. commitment_number ( ) ,
2900
+ commitment_tx. per_commitment_point ( ) , logger) ;
2901
+ // Soon, we will only populate this field
2902
+ self . initial_counterparty_commitment_tx = Some ( commitment_tx) ;
2889
2903
}
2890
2904
2891
2905
fn provide_latest_counterparty_commitment_tx < L : Deref > (
@@ -3223,10 +3237,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3223
3237
if self . lockdown_from_offchain { panic ! ( ) ; }
3224
3238
self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) ;
3225
3239
}
3240
+ // Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitmentTX`.
3241
+ // For now we just add the code to handle the new updates.
3242
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX` variant.
3226
3243
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3227
3244
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3228
3245
self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point, logger)
3229
3246
} ,
3247
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3248
+ log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3249
+ self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , htlc_outputs. clone ( ) , commitment_tx. commitment_number ( ) , commitment_tx. per_commitment_point ( ) , logger)
3250
+ } ,
3230
3251
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
3231
3252
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
3232
3253
self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . to_byte_array ( ) ) , & payment_preimage, payment_info, broadcaster, & bounded_fee_estimator, logger)
@@ -3289,6 +3310,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3289
3310
match update {
3290
3311
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. }
3291
3312
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3313
+ |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3292
3314
|ChannelMonitorUpdateStep :: ShutdownScript { .. }
3293
3315
|ChannelMonitorUpdateStep :: CommitmentSecret { .. } =>
3294
3316
is_pre_close_update = true ,
@@ -3419,14 +3441,32 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3419
3441
}
3420
3442
3421
3443
fn initial_counterparty_commitment_tx ( & mut self ) -> Option < CommitmentTransaction > {
3422
- let ( their_per_commitment_point, feerate_per_kw, to_broadcaster_value,
3423
- to_countersignatory_value) = self . initial_counterparty_commitment_info ?;
3424
- let htlc_outputs = vec ! [ ] ;
3425
-
3426
- let commitment_tx = self . build_counterparty_commitment_tx ( INITIAL_COMMITMENT_NUMBER ,
3427
- & their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
3428
- feerate_per_kw, htlc_outputs) ;
3429
- Some ( commitment_tx)
3444
+ self . initial_counterparty_commitment_tx . clone ( ) . or_else ( || {
3445
+ // This provides forward compatibility; an old monitor will not contain the full
3446
+ // transaction; only enough information to rebuild it
3447
+ self . initial_counterparty_commitment_info . map (
3448
+ |(
3449
+ their_per_commitment_point,
3450
+ feerate_per_kw,
3451
+ to_broadcaster_value,
3452
+ to_countersignatory_value,
3453
+ ) | {
3454
+ let htlc_outputs = vec ! [ ] ;
3455
+
3456
+ let commitment_tx = self . build_counterparty_commitment_tx (
3457
+ INITIAL_COMMITMENT_NUMBER ,
3458
+ & their_per_commitment_point,
3459
+ to_broadcaster_value,
3460
+ to_countersignatory_value,
3461
+ feerate_per_kw,
3462
+ htlc_outputs,
3463
+ ) ;
3464
+ // Take the opportunity to populate this recently introduced field
3465
+ self . initial_counterparty_commitment_tx = Some ( commitment_tx. clone ( ) ) ;
3466
+ commitment_tx
3467
+ } ,
3468
+ )
3469
+ } )
3430
3470
}
3431
3471
3432
3472
fn build_counterparty_commitment_tx (
@@ -3454,6 +3494,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3454
3494
3455
3495
fn counterparty_commitment_txs_from_update ( & self , update : & ChannelMonitorUpdate ) -> Vec < CommitmentTransaction > {
3456
3496
update. updates . iter ( ) . filter_map ( |update| {
3497
+ // Soon we will drop the first branch here in favor of the second.
3498
+ // In preparation, we just add the second branch without deleting the first.
3499
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX` variant.
3457
3500
match update {
3458
3501
& ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid,
3459
3502
ref htlc_outputs, commitment_number, their_per_commitment_point,
@@ -3473,6 +3516,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3473
3516
3474
3517
Some ( commitment_tx)
3475
3518
} ,
3519
+ & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX {
3520
+ htlc_outputs : _, ref commitment_tx,
3521
+ } => {
3522
+ Some ( commitment_tx. clone ( ) )
3523
+ } ,
3476
3524
_ => None ,
3477
3525
}
3478
3526
} ) . collect ( )
@@ -5043,6 +5091,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5043
5091
let mut spendable_txids_confirmed = Some ( Vec :: new ( ) ) ;
5044
5092
let mut counterparty_fulfilled_htlcs = Some ( new_hash_map ( ) ) ;
5045
5093
let mut initial_counterparty_commitment_info = None ;
5094
+ let mut initial_counterparty_commitment_tx = None ;
5046
5095
let mut balances_empty_height = None ;
5047
5096
let mut channel_id = None ;
5048
5097
let mut holder_pays_commitment_tx_fee = None ;
@@ -5063,6 +5112,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5063
5112
( 23 , holder_pays_commitment_tx_fee, option) ,
5064
5113
( 25 , payment_preimages_with_info, option) ,
5065
5114
( 27 , first_confirmed_funding_txo, ( default_value, funding_info. 0 ) ) ,
5115
+ ( 29 , initial_counterparty_commitment_tx, option) ,
5066
5116
} ) ;
5067
5117
if let Some ( payment_preimages_with_info) = payment_preimages_with_info {
5068
5118
if payment_preimages_with_info. len ( ) != payment_preimages. len ( ) {
@@ -5166,6 +5216,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5166
5216
best_block,
5167
5217
counterparty_node_id : counterparty_node_id. unwrap ( ) ,
5168
5218
initial_counterparty_commitment_info,
5219
+ initial_counterparty_commitment_tx,
5169
5220
balances_empty_height,
5170
5221
failed_back_htlc_ids : new_hash_set ( ) ,
5171
5222
} ) ) )
0 commit comments