@@ -549,6 +549,12 @@ pub(crate) enum ChannelMonitorUpdateStep {
549
549
to_broadcaster_value_sat : Option < u64 > ,
550
550
to_countersignatory_value_sat : Option < u64 > ,
551
551
} ,
552
+ LatestCounterpartyCommitmentTX {
553
+ // The dust and non-dust htlcs for that commitment
554
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
555
+ // Contains only the non-dust htlcs
556
+ commitment_tx : CommitmentTransaction ,
557
+ } ,
552
558
PaymentPreimage {
553
559
payment_preimage : PaymentPreimage ,
554
560
/// If this preimage was from an inbound payment claim, information about the claim should
@@ -576,6 +582,7 @@ impl ChannelMonitorUpdateStep {
576
582
match self {
577
583
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo" ,
578
584
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo" ,
585
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX" ,
579
586
ChannelMonitorUpdateStep :: PaymentPreimage { .. } => "PaymentPreimage" ,
580
587
ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
581
588
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
@@ -614,6 +621,10 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
614
621
( 5 , ShutdownScript ) => {
615
622
( 0 , scriptpubkey, required) ,
616
623
} ,
624
+ ( 6 , LatestCounterpartyCommitmentTX ) => {
625
+ ( 0 , htlc_outputs, required_vec) ,
626
+ ( 2 , commitment_tx, required) ,
627
+ } ,
617
628
) ;
618
629
619
630
/// Indicates whether the balance is derived from a cooperative close, a force-close
@@ -1024,6 +1035,11 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
1024
1035
/// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
1025
1036
/// to_countersignatory_sats)
1026
1037
initial_counterparty_commitment_info : Option < ( PublicKey , u32 , u64 , u64 ) > ,
1038
+ /// Initial counterparty commitment transaction
1039
+ ///
1040
+ /// We previously used the field above to re-build the counterparty commitment transaction,
1041
+ /// we now provide the transaction outright.
1042
+ initial_counterparty_commitment_tx : Option < CommitmentTransaction > ,
1027
1043
1028
1044
/// The first block height at which we had no remaining claimable balances.
1029
1045
balances_empty_height : Option < u32 > ,
@@ -1247,6 +1263,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1247
1263
( 23 , self . holder_pays_commitment_tx_fee, option) ,
1248
1264
( 25 , self . payment_preimages, required) ,
1249
1265
( 27 , self . first_confirmed_funding_txo, required) ,
1266
+ ( 29 , self . initial_counterparty_commitment_tx, option) ,
1250
1267
} ) ;
1251
1268
1252
1269
Ok ( ( ) )
@@ -1458,6 +1475,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1458
1475
best_block,
1459
1476
counterparty_node_id : Some ( counterparty_node_id) ,
1460
1477
initial_counterparty_commitment_info : None ,
1478
+ initial_counterparty_commitment_tx : None ,
1461
1479
balances_empty_height : None ,
1462
1480
1463
1481
failed_back_htlc_ids : new_hash_set ( ) ,
@@ -1497,17 +1515,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1497
1515
/// This is used to provide the counterparty commitment information directly to the monitor
1498
1516
/// before the initial persistence of a new channel.
1499
1517
pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1500
- & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1501
- commitment_number : u64 , their_cur_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
1502
- to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , logger : & L ,
1503
- )
1504
- where L :: Target : Logger
1518
+ & self , commitment_tx : CommitmentTransaction , logger : & L ,
1519
+ ) where L :: Target : Logger
1505
1520
{
1506
1521
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1507
1522
let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
1508
- inner. provide_initial_counterparty_commitment_tx ( txid,
1509
- htlc_outputs, commitment_number, their_cur_per_commitment_point, feerate_per_kw,
1510
- to_broadcaster_value_sat, to_countersignatory_value_sat, & logger) ;
1523
+ inner. provide_initial_counterparty_commitment_tx ( commitment_tx, & logger) ;
1511
1524
}
1512
1525
1513
1526
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
@@ -2879,20 +2892,21 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2879
2892
}
2880
2893
2881
2894
fn provide_initial_counterparty_commitment_tx < L : Deref > (
2882
- & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
2883
- commitment_number : u64 , their_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
2884
- to_broadcaster_value : u64 , to_countersignatory_value : u64 , logger : & WithChannelMonitor < L > ,
2895
+ & mut self , commitment_tx : CommitmentTransaction , logger : & WithChannelMonitor < L > ,
2885
2896
) where L :: Target : Logger {
2886
- self . initial_counterparty_commitment_info = Some ( ( their_per_commitment_point. clone ( ) ,
2887
- feerate_per_kw, to_broadcaster_value, to_countersignatory_value) ) ;
2897
+ // We populate this field for downgrades
2898
+ self . initial_counterparty_commitment_info = Some ( ( commitment_tx. per_commitment_point ( ) ,
2899
+ commitment_tx. feerate_per_kw ( ) , commitment_tx. to_broadcaster_value_sat ( ) , commitment_tx. to_countersignatory_value_sat ( ) ) ) ;
2888
2900
2889
2901
#[ cfg( debug_assertions) ] {
2890
2902
let rebuilt_commitment_tx = self . initial_counterparty_commitment_tx ( ) . unwrap ( ) ;
2891
- debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , txid) ;
2903
+ debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , commitment_tx . trust ( ) . txid( ) ) ;
2892
2904
}
2893
2905
2894
- self . provide_latest_counterparty_commitment_tx ( txid, htlc_outputs, commitment_number,
2895
- their_per_commitment_point, logger) ;
2906
+ self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , Vec :: new ( ) , commitment_tx. commitment_number ( ) ,
2907
+ commitment_tx. per_commitment_point ( ) , logger) ;
2908
+ // Soon, we will only populate this field
2909
+ self . initial_counterparty_commitment_tx = Some ( commitment_tx) ;
2896
2910
}
2897
2911
2898
2912
fn provide_latest_counterparty_commitment_tx < L : Deref > (
@@ -3238,10 +3252,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3238
3252
if self . lockdown_from_offchain { panic ! ( ) ; }
3239
3253
self . provide_latest_holder_commitment_tx ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) , & claimed_htlcs, nondust_htlc_sources. clone ( ) ) ;
3240
3254
}
3255
+ // Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitmentTX`.
3256
+ // For now we just add the code to handle the new updates.
3257
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX` variant.
3241
3258
ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
3242
3259
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3243
3260
self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point, logger)
3244
3261
} ,
3262
+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3263
+ log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3264
+ self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , htlc_outputs. clone ( ) , commitment_tx. commitment_number ( ) , commitment_tx. per_commitment_point ( ) , logger)
3265
+ } ,
3245
3266
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
3246
3267
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
3247
3268
self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . to_byte_array ( ) ) , & payment_preimage, payment_info, broadcaster, & bounded_fee_estimator, logger)
@@ -3304,6 +3325,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3304
3325
match update {
3305
3326
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. }
3306
3327
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3328
+ |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3307
3329
|ChannelMonitorUpdateStep :: ShutdownScript { .. }
3308
3330
|ChannelMonitorUpdateStep :: CommitmentSecret { .. } =>
3309
3331
is_pre_close_update = true ,
@@ -3440,14 +3462,21 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3440
3462
}
3441
3463
3442
3464
fn initial_counterparty_commitment_tx ( & mut self ) -> Option < CommitmentTransaction > {
3443
- let ( their_per_commitment_point, feerate_per_kw, to_broadcaster_value,
3444
- to_countersignatory_value) = self . initial_counterparty_commitment_info ?;
3445
- let htlc_outputs = vec ! [ ] ;
3446
-
3447
- let commitment_tx = self . build_counterparty_commitment_tx ( INITIAL_COMMITMENT_NUMBER ,
3448
- & their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
3449
- feerate_per_kw, htlc_outputs) ;
3450
- Some ( commitment_tx)
3465
+ self . initial_counterparty_commitment_tx . clone ( ) . or_else ( || {
3466
+ // This provides forward compatibility; an old monitor will not contain the full
3467
+ // transaction; only enough information to rebuild it
3468
+ self . initial_counterparty_commitment_info
3469
+ . map ( |( their_per_commitment_point, feerate_per_kw, to_broadcaster_value, to_countersignatory_value) | {
3470
+ let htlc_outputs = vec ! [ ] ;
3471
+
3472
+ let commitment_tx = self . build_counterparty_commitment_tx ( INITIAL_COMMITMENT_NUMBER ,
3473
+ & their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
3474
+ feerate_per_kw, htlc_outputs) ;
3475
+ // Take the opportunity to populate this recently introduced field
3476
+ self . initial_counterparty_commitment_tx = Some ( commitment_tx. clone ( ) ) ;
3477
+ commitment_tx
3478
+ } )
3479
+ } )
3451
3480
}
3452
3481
3453
3482
fn build_counterparty_commitment_tx (
@@ -3475,12 +3504,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3475
3504
3476
3505
fn counterparty_commitment_txs_from_update ( & self , update : & ChannelMonitorUpdate ) -> Vec < CommitmentTransaction > {
3477
3506
update. updates . iter ( ) . filter_map ( |update| {
3507
+ // Soon we will drop the first branch here in favor of the second.
3508
+ // In preparation, we just add the second branch without deleting the first.
3509
+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX` variant.
3478
3510
match update {
3479
3511
& ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid,
3480
3512
ref htlc_outputs, commitment_number, their_per_commitment_point,
3481
3513
feerate_per_kw : Some ( feerate_per_kw) ,
3482
3514
to_broadcaster_value_sat : Some ( to_broadcaster_value) ,
3483
- to_countersignatory_value_sat : Some ( to_countersignatory_value) } => {
3515
+ to_countersignatory_value_sat : Some ( to_countersignatory_value)
3516
+ } => {
3484
3517
3485
3518
let nondust_htlcs = htlc_outputs. iter ( ) . filter_map ( |( htlc, _) | {
3486
3519
htlc. transaction_output_index . map ( |_| ( htlc. clone ( ) , None ) )
@@ -3494,6 +3527,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3494
3527
3495
3528
Some ( commitment_tx)
3496
3529
} ,
3530
+ & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs : _,
3531
+ ref commitment_tx,
3532
+ } => {
3533
+ Some ( commitment_tx. clone ( ) )
3534
+ } ,
3497
3535
_ => None ,
3498
3536
}
3499
3537
} ) . collect ( )
@@ -5076,6 +5114,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5076
5114
let mut spendable_txids_confirmed = Some ( Vec :: new ( ) ) ;
5077
5115
let mut counterparty_fulfilled_htlcs = Some ( new_hash_map ( ) ) ;
5078
5116
let mut initial_counterparty_commitment_info = None ;
5117
+ let mut initial_counterparty_commitment_tx = None ;
5079
5118
let mut balances_empty_height = None ;
5080
5119
let mut channel_id = None ;
5081
5120
let mut holder_pays_commitment_tx_fee = None ;
@@ -5096,6 +5135,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5096
5135
( 23 , holder_pays_commitment_tx_fee, option) ,
5097
5136
( 25 , payment_preimages_with_info, option) ,
5098
5137
( 27 , first_confirmed_funding_txo, ( default_value, funding_info. 0 ) ) ,
5138
+ ( 29 , initial_counterparty_commitment_tx, option) ,
5099
5139
} ) ;
5100
5140
if let Some ( payment_preimages_with_info) = payment_preimages_with_info {
5101
5141
if payment_preimages_with_info. len ( ) != payment_preimages. len ( ) {
@@ -5192,6 +5232,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5192
5232
best_block,
5193
5233
counterparty_node_id,
5194
5234
initial_counterparty_commitment_info,
5235
+ initial_counterparty_commitment_tx,
5195
5236
balances_empty_height,
5196
5237
failed_back_htlc_ids : new_hash_set ( ) ,
5197
5238
} ) ) )
0 commit comments