Skip to content

Commit 7a115d7

Browse files
committed
Use HolderCommitmentPoint for commitment number
1 parent c08aa34 commit 7a115d7

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

lightning/src/ln/channel.rs

+36-40
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13671367
// cost of others, but should really just be changed.
13681368

13691369
holder_commitment_point: HolderCommitmentPoint,
1370-
cur_holder_commitment_transaction_number: u64,
13711370
cur_counterparty_commitment_transaction_number: u64,
13721371
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
13731372
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1842,7 +1841,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18421841
destination_script,
18431842

18441843
holder_commitment_point,
1845-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18461844
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18471845
value_to_self_msat,
18481846

@@ -2068,7 +2066,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20682066
destination_script,
20692067

20702068
holder_commitment_point,
2071-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20722069
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20732070
value_to_self_msat,
20742071

@@ -4459,9 +4456,9 @@ impl<SP: Deref> Channel<SP> where
44594456

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

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());
44634460

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);
44654462
let commitment_txid = {
44664463
let trusted_tx = commitment_stats.tx.trust();
44674464
let bitcoin_tx = trusted_tx.built_transaction();
@@ -4624,7 +4621,7 @@ impl<SP: Deref> Channel<SP> where
46244621
channel_id: Some(self.context.channel_id()),
46254622
};
46264623

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);
46284625
self.context.expecting_peer_commitment_signed = false;
46294626
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
46304627
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -5135,8 +5132,8 @@ impl<SP: Deref> Channel<SP> where
51355132
// Before proposing a feerate update, check that we can actually afford the new fee.
51365133
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
51375134
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);
51405137
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;
51415138
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
51425139
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
53185315
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53195316
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53205317
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);
53225319
Some(msgs::ChannelReady {
53235320
channel_id: self.context.channel_id(),
53245321
next_per_commitment_point,
@@ -5425,8 +5422,8 @@ impl<SP: Deref> Channel<SP> where
54255422
}
54265423

54275424
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);
54305427
msgs::RevokeAndACK {
54315428
channel_id: self.context.channel_id,
54325429
per_commitment_secret,
@@ -5559,7 +5556,7 @@ impl<SP: Deref> Channel<SP> where
55595556
return Err(ChannelError::Close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
55605557
}
55615558

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;
55635560
if msg.next_remote_commitment_number > 0 {
55645561
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);
55655562
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
@@ -5621,7 +5618,7 @@ impl<SP: Deref> Channel<SP> where
56215618
}
56225619

56235620
// 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);
56255622
return Ok(ReestablishResponses {
56265623
channel_ready: Some(msgs::ChannelReady {
56275624
channel_id: self.context.channel_id(),
@@ -5664,9 +5661,9 @@ impl<SP: Deref> Channel<SP> where
56645661
}
56655662
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
56665663

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 {
56685665
// 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);
56705667
Some(msgs::ChannelReady {
56715668
channel_id: self.context.channel_id(),
56725669
next_per_commitment_point,
@@ -6317,7 +6314,7 @@ impl<SP: Deref> Channel<SP> where
63176314
}
63186315

63196316
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
63216318
}
63226319

63236320
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -6432,7 +6429,7 @@ impl<SP: Deref> Channel<SP> where
64326429
debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
64336430
return true;
64346431
}
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 &&
64366433
self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
64376434
// If we're a 0-conf channel, we'll move beyond AwaitingChannelReady immediately even while
64386435
// waiting for the initial monitor persistence. Thus, we check if our commitment
@@ -6994,7 +6991,7 @@ impl<SP: Deref> Channel<SP> where
69946991

69956992
// next_local_commitment_number is the next commitment_signed number we expect to
69966993
// 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(),
69986995
// We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
69996996
// receive, however we track it by the next commitment number for a remote transaction
70006997
// (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 {
75467543
}
75477544
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
75487545
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 {
75507547
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
75517548
}
75527549

@@ -7601,7 +7598,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76017598
/// Returns true if we can resume the channel by sending the [`msgs::OpenChannel`] again.
76027599
pub fn is_resumable(&self) -> bool {
76037600
!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
76057602
}
76067603

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

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

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);
76207617
let keys = self.context.get_holder_pubkeys();
76217618

76227619
msgs::OpenChannel {
@@ -7797,7 +7794,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
77977794
}
77987795
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
77997796
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 {
78017798
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
78027799
}
78037800

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

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;
78167813
{
78177814
let trusted_tx = initial_commitment_tx.trust();
78187815
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
@@ -7865,7 +7862,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78657862
} else {
78667863
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
78677864
}
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);
78697866
self.context.cur_counterparty_commitment_transaction_number -= 1;
78707867

78717868
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 {
80017998
) {
80027999
panic!("Tried to send accept_channel after channel had moved forward");
80038000
}
8004-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8001+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
80058002
panic!("Tried to send an accept_channel for a channel that has already advanced");
80068003
}
80078004

@@ -8014,7 +8011,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80148011
///
80158012
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80168013
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);
80188015
let keys = self.context.get_holder_pubkeys();
80198016

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

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;
80618058
let trusted_tx = initial_commitment_tx.trust();
80628059
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
80638060
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 {
80918088
}
80928089
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
80938090
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 {
80958092
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
80968093
}
80978094

@@ -8131,7 +8128,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81318128
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
81328129
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
81338130
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);
81358132

81368133
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
81378134

@@ -8252,15 +8249,15 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
82528249
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
82538250
}
82548251

8255-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8252+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
82568253
debug_assert!(false, "Tried to send an open_channel2 for a channel that has already advanced");
82578254
}
82588255

82598256
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(),
82618258
&self.context.secp_ctx);
82628259
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,
82648261
&self.context.secp_ctx);
82658262
let keys = self.context.get_holder_pubkeys();
82668263

@@ -8394,7 +8391,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
83948391
) {
83958392
debug_assert!(false, "Tried to send accept_channel2 after channel had moved forward");
83968393
}
8397-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8394+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
83988395
debug_assert!(false, "Tried to send an accept_channel2 for a channel that has already advanced");
83998396
}
84008397

@@ -8408,9 +8405,9 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
84088405
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
84098406
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
84108407
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);
84128409
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);
84148411
let keys = self.context.get_holder_pubkeys();
84158412

84168413
msgs::AcceptChannelV2 {
@@ -8583,7 +8580,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
85838580
}
85848581
self.context.destination_script.write(writer)?;
85858582

8586-
self.context.cur_holder_commitment_transaction_number.write(writer)?;
8583+
self.context.holder_commitment_point.transaction_number().write(writer)?;
85878584
self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
85888585
self.context.value_to_self_msat.write(writer)?;
85898586

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

94019398
holder_commitment_point,
9402-
cur_holder_commitment_transaction_number,
94039399
cur_counterparty_commitment_transaction_number,
94049400
value_to_self_msat,
94059401

0 commit comments

Comments
 (0)