Skip to content

Commit 507a4af

Browse files
committed
Use HolderCommitmentPoint for commitment number
1 parent a95add5 commit 507a4af

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

lightning/src/ln/channel.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13641364
// cost of others, but should really just be changed.
13651365

13661366
holder_commitment_point: HolderCommitmentPoint,
1367-
cur_holder_commitment_transaction_number: u64,
13681367
cur_counterparty_commitment_transaction_number: u64,
13691368
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
13701369
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1840,7 +1839,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18401839
destination_script,
18411840

18421841
holder_commitment_point,
1843-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18441842
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18451843
value_to_self_msat,
18461844

@@ -2069,7 +2067,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20692067
destination_script,
20702068

20712069
holder_commitment_point,
2072-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20732070
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20742071
value_to_self_msat,
20752072

@@ -4460,9 +4457,9 @@ impl<SP: Deref> Channel<SP> where
44604457

44614458
let funding_script = self.context.get_funding_redeemscript();
44624459

4463-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
4460+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
44644461

4465-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
4462+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger);
44664463
let commitment_txid = {
44674464
let trusted_tx = commitment_stats.tx.trust();
44684465
let bitcoin_tx = trusted_tx.built_transaction();
@@ -4625,7 +4622,7 @@ impl<SP: Deref> Channel<SP> where
46254622
channel_id: Some(self.context.channel_id()),
46264623
};
46274624

4628-
self.context.cur_holder_commitment_transaction_number -= 1;
4625+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
46294626
self.context.expecting_peer_commitment_signed = false;
46304627
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
46314628
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -5136,8 +5133,8 @@ impl<SP: Deref> Channel<SP> where
51365133
// Before proposing a feerate update, check that we can actually afford the new fee.
51375134
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
51385135
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
5139-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
5140-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
5136+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
5137+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, true, logger);
51415138
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;
51425139
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
51435140
if holder_balance_msat < buffer_fee_msat + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -5319,7 +5316,7 @@ impl<SP: Deref> Channel<SP> where
53195316
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53205317
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53215318
self.context.monitor_pending_channel_ready = false;
5322-
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);
5319+
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);
53235320
Some(msgs::ChannelReady {
53245321
channel_id: self.context.channel_id(),
53255322
next_per_commitment_point,
@@ -5426,8 +5423,8 @@ impl<SP: Deref> Channel<SP> where
54265423
}
54275424

54285425
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
5429-
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);
5430-
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
5426+
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);
5427+
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
54315428
msgs::RevokeAndACK {
54325429
channel_id: self.context.channel_id,
54335430
per_commitment_secret,
@@ -5560,7 +5557,7 @@ impl<SP: Deref> Channel<SP> where
55605557
return Err(ChannelError::Close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
55615558
}
55625559

5563-
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
5560+
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() - 1;
55645561
if msg.next_remote_commitment_number > 0 {
55655562
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);
55665563
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
@@ -5622,7 +5619,7 @@ impl<SP: Deref> Channel<SP> where
56225619
}
56235620

56245621
// We have OurChannelReady set!
5625-
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);
5622+
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);
56265623
return Ok(ReestablishResponses {
56275624
channel_ready: Some(msgs::ChannelReady {
56285625
channel_id: self.context.channel_id(),
@@ -5665,9 +5662,9 @@ impl<SP: Deref> Channel<SP> where
56655662
}
56665663
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
56675664

5668-
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
5665+
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56695666
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5670-
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);
5667+
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);
56715668
Some(msgs::ChannelReady {
56725669
channel_id: self.context.channel_id(),
56735670
next_per_commitment_point,
@@ -6318,7 +6315,7 @@ impl<SP: Deref> Channel<SP> where
63186315
}
63196316

63206317
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6321-
self.context.cur_holder_commitment_transaction_number + 1
6318+
self.context.holder_commitment_point.transaction_number() + 1
63226319
}
63236320

63246321
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -6433,7 +6430,7 @@ impl<SP: Deref> Channel<SP> where
64336430
debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
64346431
return true;
64356432
}
6436-
if self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
6433+
if self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER - 1 &&
64376434
self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
64386435
// If we're a 0-conf channel, we'll move beyond AwaitingChannelReady immediately even while
64396436
// waiting for the initial monitor persistence. Thus, we check if our commitment
@@ -6995,7 +6992,7 @@ impl<SP: Deref> Channel<SP> where
69956992

69966993
// next_local_commitment_number is the next commitment_signed number we expect to
69976994
// receive (indicating if they need to resend one that we missed).
6998-
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number,
6995+
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number(),
69996996
// We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
70006997
// receive, however we track it by the next commitment number for a remote transaction
70016998
// (which is one further, as they always revoke previous commitment transaction, not
@@ -7549,7 +7546,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
75497546
}
75507547
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
75517548
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7552-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7549+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
75537550
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
75547551
}
75557552

@@ -7604,7 +7601,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76047601
/// Returns true if we can resume the channel by sending the [`msgs::OpenChannel`] again.
76057602
pub fn is_resumable(&self) -> bool {
76067603
!self.context.have_received_message() &&
7607-
self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER
7604+
self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER
76087605
}
76097606

76107607
pub fn get_open_channel(&self, chain_hash: ChainHash) -> msgs::OpenChannel {
@@ -7615,11 +7612,11 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76157612
panic!("Cannot generate an open_channel after we've moved forward");
76167613
}
76177614

7618-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7615+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
76197616
panic!("Tried to send an open_channel for a channel that has already advanced");
76207617
}
76217618

7622-
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);
7619+
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);
76237620
let keys = self.context.get_holder_pubkeys();
76247621

76257622
msgs::OpenChannel {
@@ -7800,7 +7797,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78007797
}
78017798
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
78027799
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7803-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7800+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
78047801
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
78057802
}
78067803

@@ -7814,8 +7811,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78147811
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
78157812
&self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
78167813

7817-
let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
7818-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
7814+
let holder_signer = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
7815+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &holder_signer, true, false, logger).tx;
78197816
{
78207817
let trusted_tx = initial_commitment_tx.trust();
78217818
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
@@ -7868,7 +7865,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78687865
} else {
78697866
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
78707867
}
7871-
self.context.cur_holder_commitment_transaction_number -= 1;
7868+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
78727869
self.context.cur_counterparty_commitment_transaction_number -= 1;
78737870

78747871
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -8004,7 +8001,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80048001
) {
80058002
panic!("Tried to send accept_channel after channel had moved forward");
80068003
}
8007-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8004+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
80088005
panic!("Tried to send an accept_channel for a channel that has already advanced");
80098006
}
80108007

@@ -8017,7 +8014,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80178014
///
80188015
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80198016
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
8020-
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);
8017+
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);
80218018
let keys = self.context.get_holder_pubkeys();
80228019

80238020
msgs::AcceptChannel {
@@ -8059,8 +8056,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80598056
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
80608057
let funding_script = self.context.get_funding_redeemscript();
80618058

8062-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
8063-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
8059+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
8060+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
80648061
let trusted_tx = initial_commitment_tx.trust();
80658062
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
80668063
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
@@ -8094,7 +8091,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80948091
}
80958092
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
80968093
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
8097-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8094+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
80988095
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
80998096
}
81008097

@@ -8134,7 +8131,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81348131
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
81358132
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
81368133
self.context.cur_counterparty_commitment_transaction_number -= 1;
8137-
self.context.cur_holder_commitment_transaction_number -= 1;
8134+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
81388135

81398136
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
81408137

@@ -8257,15 +8254,15 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
82578254
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
82588255
}
82598256

8260-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8257+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
82618258
debug_assert!(false, "Tried to send an open_channel2 for a channel that has already advanced");
82628259
}
82638260

82648261
let first_per_commitment_point = self.context.holder_signer.as_ref()
8265-
.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number,
8262+
.get_per_commitment_point(self.context.holder_commitment_point.transaction_number(),
82668263
&self.context.secp_ctx);
82678264
let second_per_commitment_point = self.context.holder_signer.as_ref()
8268-
.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number - 1,
8265+
.get_per_commitment_point(self.context.holder_commitment_point.transaction_number() - 1,
82698266
&self.context.secp_ctx);
82708267
let keys = self.context.get_holder_pubkeys();
82718268

@@ -8399,7 +8396,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
83998396
) {
84008397
debug_assert!(false, "Tried to send accept_channel2 after channel had moved forward");
84018398
}
8402-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8399+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
84038400
debug_assert!(false, "Tried to send an accept_channel2 for a channel that has already advanced");
84048401
}
84058402

@@ -8413,9 +8410,9 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
84138410
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
84148411
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
84158412
let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8416-
self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
8413+
self.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx);
84178414
let second_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8418-
self.context.cur_holder_commitment_transaction_number - 1, &self.context.secp_ctx);
8415+
self.context.holder_commitment_point.transaction_number() - 1, &self.context.secp_ctx);
84198416
let keys = self.context.get_holder_pubkeys();
84208417

84218418
msgs::AcceptChannelV2 {
@@ -8588,7 +8585,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
85888585
}
85898586
self.context.destination_script.write(writer)?;
85908587

8591-
self.context.cur_holder_commitment_transaction_number.write(writer)?;
8588+
self.context.holder_commitment_point.transaction_number().write(writer)?;
85928589
self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
85938590
self.context.value_to_self_msat.write(writer)?;
85948591

@@ -9400,7 +9397,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
94009397
destination_script,
94019398

94029399
holder_commitment_point,
9403-
cur_holder_commitment_transaction_number,
94049400
cur_counterparty_commitment_transaction_number,
94059401
value_to_self_msat,
94069402

0 commit comments

Comments
 (0)