@@ -1367,7 +1367,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1367
1367
// cost of others, but should really just be changed.
1368
1368
1369
1369
holder_commitment_point: HolderCommitmentPoint,
1370
- cur_holder_commitment_transaction_number: u64,
1371
1370
cur_counterparty_commitment_transaction_number: u64,
1372
1371
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
1373
1372
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1842,7 +1841,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1842
1841
destination_script,
1843
1842
1844
1843
holder_commitment_point,
1845
- cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1846
1844
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1847
1845
value_to_self_msat,
1848
1846
@@ -2068,7 +2066,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2068
2066
destination_script,
2069
2067
2070
2068
holder_commitment_point,
2071
- cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
2072
2069
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
2073
2070
value_to_self_msat,
2074
2071
@@ -4459,9 +4456,9 @@ impl<SP: Deref> Channel<SP> where
4459
4456
4460
4457
let funding_script = self.context.get_funding_redeemscript();
4461
4458
4462
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
4459
+ let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number() );
4463
4460
4464
- let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number , &keys, true, false, logger);
4461
+ let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number() , &keys, true, false, logger);
4465
4462
let commitment_txid = {
4466
4463
let trusted_tx = commitment_stats.tx.trust();
4467
4464
let bitcoin_tx = trusted_tx.built_transaction();
@@ -4624,7 +4621,7 @@ impl<SP: Deref> Channel<SP> where
4624
4621
channel_id: Some(self.context.channel_id()),
4625
4622
};
4626
4623
4627
- self.context.cur_holder_commitment_transaction_number -= 1 ;
4624
+ self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger) ;
4628
4625
self.context.expecting_peer_commitment_signed = false;
4629
4626
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
4630
4627
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -5135,8 +5132,8 @@ impl<SP: Deref> Channel<SP> where
5135
5132
// Before proposing a feerate update, check that we can actually afford the new fee.
5136
5133
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
5137
5134
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
5138
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
5139
- let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number , &keys, true, true, logger);
5135
+ let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number() );
5136
+ let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number() , &keys, true, true, logger);
5140
5137
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
5141
5138
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
5142
5139
if holder_balance_msat < buffer_fee_msat + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -5318,7 +5315,7 @@ impl<SP: Deref> Channel<SP> where
5318
5315
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
5319
5316
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
5320
5317
self.context.monitor_pending_channel_ready = false;
5321
- let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
5318
+ let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
5322
5319
Some(msgs::ChannelReady {
5323
5320
channel_id: self.context.channel_id(),
5324
5321
next_per_commitment_point,
@@ -5425,8 +5422,8 @@ impl<SP: Deref> Channel<SP> where
5425
5422
}
5426
5423
5427
5424
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
5428
- let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
5429
- let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
5425
+ let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
5426
+ let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
5430
5427
msgs::RevokeAndACK {
5431
5428
channel_id: self.context.channel_id,
5432
5429
per_commitment_secret,
@@ -5559,7 +5556,7 @@ impl<SP: Deref> Channel<SP> where
5559
5556
return Err(ChannelError::Close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
5560
5557
}
5561
5558
5562
- let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
5559
+ let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() - 1;
5563
5560
if msg.next_remote_commitment_number > 0 {
5564
5561
let expected_point = self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
5565
5562
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
@@ -5621,7 +5618,7 @@ impl<SP: Deref> Channel<SP> where
5621
5618
}
5622
5619
5623
5620
// We have OurChannelReady set!
5624
- let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
5621
+ let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
5625
5622
return Ok(ReestablishResponses {
5626
5623
channel_ready: Some(msgs::ChannelReady {
5627
5624
channel_id: self.context.channel_id(),
@@ -5664,9 +5661,9 @@ impl<SP: Deref> Channel<SP> where
5664
5661
}
5665
5662
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
5666
5663
5667
- let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
5664
+ let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
5668
5665
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5669
- let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
5666
+ let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
5670
5667
Some(msgs::ChannelReady {
5671
5668
channel_id: self.context.channel_id(),
5672
5669
next_per_commitment_point,
@@ -6317,7 +6314,7 @@ impl<SP: Deref> Channel<SP> where
6317
6314
}
6318
6315
6319
6316
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6320
- self.context.cur_holder_commitment_transaction_number + 1
6317
+ self.context.holder_commitment_point.transaction_number() + 1
6321
6318
}
6322
6319
6323
6320
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -6432,7 +6429,7 @@ impl<SP: Deref> Channel<SP> where
6432
6429
debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
6433
6430
return true;
6434
6431
}
6435
- if self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
6432
+ if self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER - 1 &&
6436
6433
self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
6437
6434
// If we're a 0-conf channel, we'll move beyond AwaitingChannelReady immediately even while
6438
6435
// waiting for the initial monitor persistence. Thus, we check if our commitment
@@ -6994,7 +6991,7 @@ impl<SP: Deref> Channel<SP> where
6994
6991
6995
6992
// next_local_commitment_number is the next commitment_signed number we expect to
6996
6993
// receive (indicating if they need to resend one that we missed).
6997
- next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number ,
6994
+ next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() ,
6998
6995
// We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
6999
6996
// receive, however we track it by the next commitment number for a remote transaction
7000
6997
// (which is one further, as they always revoke previous commitment transaction, not
@@ -7546,7 +7543,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7546
7543
}
7547
7544
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
7548
7545
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7549
- self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7546
+ self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
7550
7547
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
7551
7548
}
7552
7549
@@ -7601,7 +7598,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7601
7598
/// Returns true if we can resume the channel by sending the [`msgs::OpenChannel`] again.
7602
7599
pub fn is_resumable(&self) -> bool {
7603
7600
!self.context.have_received_message() &&
7604
- self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER
7601
+ self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER
7605
7602
}
7606
7603
7607
7604
pub fn get_open_channel(&self, chain_hash: ChainHash) -> msgs::OpenChannel {
@@ -7612,11 +7609,11 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7612
7609
panic!("Cannot generate an open_channel after we've moved forward");
7613
7610
}
7614
7611
7615
- if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7612
+ if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
7616
7613
panic!("Tried to send an open_channel for a channel that has already advanced");
7617
7614
}
7618
7615
7619
- let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
7616
+ let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
7620
7617
let keys = self.context.get_holder_pubkeys();
7621
7618
7622
7619
msgs::OpenChannel {
@@ -7797,7 +7794,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7797
7794
}
7798
7795
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
7799
7796
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7800
- self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7797
+ self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
7801
7798
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
7802
7799
}
7803
7800
@@ -7811,8 +7808,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7811
7808
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
7812
7809
&self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
7813
7810
7814
- let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
7815
- let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number , &holder_signer, true, false, logger).tx;
7811
+ let holder_signer = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number() );
7812
+ let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number() , &holder_signer, true, false, logger).tx;
7816
7813
{
7817
7814
let trusted_tx = initial_commitment_tx.trust();
7818
7815
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
@@ -7865,7 +7862,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7865
7862
} else {
7866
7863
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7867
7864
}
7868
- self.context.cur_holder_commitment_transaction_number -= 1 ;
7865
+ self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger) ;
7869
7866
self.context.cur_counterparty_commitment_transaction_number -= 1;
7870
7867
7871
7868
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -8001,7 +7998,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
8001
7998
) {
8002
7999
panic!("Tried to send accept_channel after channel had moved forward");
8003
8000
}
8004
- if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8001
+ if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
8005
8002
panic!("Tried to send an accept_channel for a channel that has already advanced");
8006
8003
}
8007
8004
@@ -8014,7 +8011,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
8014
8011
///
8015
8012
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
8016
8013
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
8017
- let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
8014
+ let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
8018
8015
let keys = self.context.get_holder_pubkeys();
8019
8016
8020
8017
msgs::AcceptChannel {
@@ -8056,8 +8053,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
8056
8053
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
8057
8054
let funding_script = self.context.get_funding_redeemscript();
8058
8055
8059
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
8060
- let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number , &keys, true, false, logger).tx;
8056
+ let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number() );
8057
+ let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number() , &keys, true, false, logger).tx;
8061
8058
let trusted_tx = initial_commitment_tx.trust();
8062
8059
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
8063
8060
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
@@ -8091,7 +8088,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
8091
8088
}
8092
8089
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
8093
8090
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
8094
- self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8091
+ self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
8095
8092
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
8096
8093
}
8097
8094
@@ -8131,7 +8128,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
8131
8128
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
8132
8129
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
8133
8130
self.context.cur_counterparty_commitment_transaction_number -= 1;
8134
- self.context.cur_holder_commitment_transaction_number -= 1 ;
8131
+ self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger) ;
8135
8132
8136
8133
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
8137
8134
@@ -8252,15 +8249,15 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
8252
8249
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
8253
8250
}
8254
8251
8255
- if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8252
+ if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
8256
8253
debug_assert!(false, "Tried to send an open_channel2 for a channel that has already advanced");
8257
8254
}
8258
8255
8259
8256
let first_per_commitment_point = self.context.holder_signer.as_ref()
8260
- .get_per_commitment_point(self.context.cur_holder_commitment_transaction_number ,
8257
+ .get_per_commitment_point(self.context.holder_commitment_point.transaction_number() ,
8261
8258
&self.context.secp_ctx);
8262
8259
let second_per_commitment_point = self.context.holder_signer.as_ref()
8263
- .get_per_commitment_point(self.context.cur_holder_commitment_transaction_number - 1,
8260
+ .get_per_commitment_point(self.context.holder_commitment_point.transaction_number() - 1,
8264
8261
&self.context.secp_ctx);
8265
8262
let keys = self.context.get_holder_pubkeys();
8266
8263
@@ -8394,7 +8391,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
8394
8391
) {
8395
8392
debug_assert!(false, "Tried to send accept_channel2 after channel had moved forward");
8396
8393
}
8397
- if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8394
+ if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
8398
8395
debug_assert!(false, "Tried to send an accept_channel2 for a channel that has already advanced");
8399
8396
}
8400
8397
@@ -8408,9 +8405,9 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
8408
8405
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
8409
8406
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
8410
8407
let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8411
- self.context.cur_holder_commitment_transaction_number , &self.context.secp_ctx);
8408
+ self.context.holder_commitment_point.transaction_number() , &self.context.secp_ctx);
8412
8409
let second_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8413
- self.context.cur_holder_commitment_transaction_number - 1, &self.context.secp_ctx);
8410
+ self.context.holder_commitment_point.transaction_number() - 1, &self.context.secp_ctx);
8414
8411
let keys = self.context.get_holder_pubkeys();
8415
8412
8416
8413
msgs::AcceptChannelV2 {
@@ -8583,7 +8580,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8583
8580
}
8584
8581
self.context.destination_script.write(writer)?;
8585
8582
8586
- self.context.cur_holder_commitment_transaction_number .write(writer)?;
8583
+ self.context.holder_commitment_point.transaction_number() .write(writer)?;
8587
8584
self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
8588
8585
self.context.value_to_self_msat.write(writer)?;
8589
8586
@@ -9399,7 +9396,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9399
9396
destination_script,
9400
9397
9401
9398
holder_commitment_point,
9402
- cur_holder_commitment_transaction_number,
9403
9399
cur_counterparty_commitment_transaction_number,
9404
9400
value_to_self_msat,
9405
9401
0 commit comments