Skip to content

Commit c494eef

Browse files
committed
Use HolderCommitmentPoint for commitment number
1 parent ee53ded commit c494eef

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

lightning/src/ln/channel.rs

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

13751375
holder_commitment_point: HolderCommitmentPoint,
1376-
cur_holder_commitment_transaction_number: u64,
13771376
cur_counterparty_commitment_transaction_number: u64,
13781377
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
13791378
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1848,7 +1847,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18481847
destination_script,
18491848

18501849
holder_commitment_point,
1851-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18521850
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18531851
value_to_self_msat,
18541852

@@ -2076,7 +2074,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20762074
destination_script,
20772075

20782076
holder_commitment_point,
2079-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20802077
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20812078
value_to_self_msat,
20822079

@@ -4467,9 +4464,9 @@ impl<SP: Deref> Channel<SP> where
44674464

44684465
let funding_script = self.context.get_funding_redeemscript();
44694466

4470-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
4467+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
44714468

4472-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
4469+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger);
44734470
let commitment_txid = {
44744471
let trusted_tx = commitment_stats.tx.trust();
44754472
let bitcoin_tx = trusted_tx.built_transaction();
@@ -4632,7 +4629,7 @@ impl<SP: Deref> Channel<SP> where
46324629
channel_id: Some(self.context.channel_id()),
46334630
};
46344631

4635-
self.context.cur_holder_commitment_transaction_number -= 1;
4632+
self.context.holder_commitment_point = self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
46364633
self.context.expecting_peer_commitment_signed = false;
46374634
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
46384635
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -5143,8 +5140,8 @@ impl<SP: Deref> Channel<SP> where
51435140
// Before proposing a feerate update, check that we can actually afford the new fee.
51445141
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
51455142
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
5146-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
5147-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
5143+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
5144+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, true, logger);
51485145
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;
51495146
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
51505147
if holder_balance_msat < buffer_fee_msat + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -5326,7 +5323,7 @@ impl<SP: Deref> Channel<SP> where
53265323
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53275324
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53285325
self.context.monitor_pending_channel_ready = false;
5329-
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);
5326+
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);
53305327
Some(msgs::ChannelReady {
53315328
channel_id: self.context.channel_id(),
53325329
next_per_commitment_point,
@@ -5433,8 +5430,8 @@ impl<SP: Deref> Channel<SP> where
54335430
}
54345431

54355432
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
5436-
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);
5437-
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
5433+
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);
5434+
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
54385435
msgs::RevokeAndACK {
54395436
channel_id: self.context.channel_id,
54405437
per_commitment_secret,
@@ -5567,7 +5564,7 @@ impl<SP: Deref> Channel<SP> where
55675564
return Err(ChannelError::Close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
55685565
}
55695566

5570-
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
5567+
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() - 1;
55715568
if msg.next_remote_commitment_number > 0 {
55725569
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);
55735570
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
@@ -5629,7 +5626,7 @@ impl<SP: Deref> Channel<SP> where
56295626
}
56305627

56315628
// We have OurChannelReady set!
5632-
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);
5629+
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);
56335630
return Ok(ReestablishResponses {
56345631
channel_ready: Some(msgs::ChannelReady {
56355632
channel_id: self.context.channel_id(),
@@ -5672,9 +5669,9 @@ impl<SP: Deref> Channel<SP> where
56725669
}
56735670
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
56745671

5675-
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
5672+
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56765673
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5677-
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);
5674+
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);
56785675
Some(msgs::ChannelReady {
56795676
channel_id: self.context.channel_id(),
56805677
next_per_commitment_point,
@@ -6325,7 +6322,7 @@ impl<SP: Deref> Channel<SP> where
63256322
}
63266323

63276324
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6328-
self.context.cur_holder_commitment_transaction_number + 1
6325+
self.context.holder_commitment_point.transaction_number() + 1
63296326
}
63306327

63316328
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -6440,7 +6437,7 @@ impl<SP: Deref> Channel<SP> where
64406437
debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
64416438
return true;
64426439
}
6443-
if self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
6440+
if self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER - 1 &&
64446441
self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
64456442
// If we're a 0-conf channel, we'll move beyond AwaitingChannelReady immediately even while
64466443
// waiting for the initial monitor persistence. Thus, we check if our commitment
@@ -7002,7 +6999,7 @@ impl<SP: Deref> Channel<SP> where
70026999

70037000
// next_local_commitment_number is the next commitment_signed number we expect to
70047001
// receive (indicating if they need to resend one that we missed).
7005-
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number,
7002+
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number(),
70067003
// We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
70077004
// receive, however we track it by the next commitment number for a remote transaction
70087005
// (which is one further, as they always revoke previous commitment transaction, not
@@ -7556,7 +7553,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
75567553
}
75577554
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
75587555
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7559-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7556+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
75607557
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
75617558
}
75627559

@@ -7611,7 +7608,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76117608
/// Returns true if we can resume the channel by sending the [`msgs::OpenChannel`] again.
76127609
pub fn is_resumable(&self) -> bool {
76137610
!self.context.have_received_message() &&
7614-
self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER
7611+
self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER
76157612
}
76167613

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

7625-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7622+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
76267623
panic!("Tried to send an open_channel for a channel that has already advanced");
76277624
}
76287625

7629-
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);
7626+
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);
76307627
let keys = self.context.get_holder_pubkeys();
76317628

76327629
msgs::OpenChannel {
@@ -7807,7 +7804,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78077804
}
78087805
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
78097806
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7810-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7807+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
78117808
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
78127809
}
78137810

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

7824-
let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
7825-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
7821+
let holder_signer = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
7822+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &holder_signer, true, false, logger).tx;
78267823
{
78277824
let trusted_tx = initial_commitment_tx.trust();
78287825
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
@@ -7875,7 +7872,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78757872
} else {
78767873
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
78777874
}
7878-
self.context.cur_holder_commitment_transaction_number -= 1;
7875+
self.context.holder_commitment_point = self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
78797876
self.context.cur_counterparty_commitment_transaction_number -= 1;
78807877

78817878
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -8011,7 +8008,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80118008
) {
80128009
panic!("Tried to send accept_channel after channel had moved forward");
80138010
}
8014-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8011+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
80158012
panic!("Tried to send an accept_channel for a channel that has already advanced");
80168013
}
80178014

@@ -8024,7 +8021,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80248021
///
80258022
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80268023
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
8027-
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);
8024+
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);
80288025
let keys = self.context.get_holder_pubkeys();
80298026

80308027
msgs::AcceptChannel {
@@ -8066,8 +8063,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80668063
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
80678064
let funding_script = self.context.get_funding_redeemscript();
80688065

8069-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
8070-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
8066+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
8067+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
80718068
let trusted_tx = initial_commitment_tx.trust();
80728069
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
80738070
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
@@ -8101,7 +8098,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81018098
}
81028099
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
81038100
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
8104-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8101+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
81058102
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
81068103
}
81078104

@@ -8141,7 +8138,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81418138
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
81428139
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
81438140
self.context.cur_counterparty_commitment_transaction_number -= 1;
8144-
self.context.cur_holder_commitment_transaction_number -= 1;
8141+
self.context.holder_commitment_point = self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
81458142

81468143
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
81478144

@@ -8593,7 +8590,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
85938590
}
85948591
self.context.destination_script.write(writer)?;
85958592

8596-
self.context.cur_holder_commitment_transaction_number.write(writer)?;
8593+
self.context.holder_commitment_point.transaction_number().write(writer)?;
85978594
self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
85988595
self.context.value_to_self_msat.write(writer)?;
85998596

@@ -9405,7 +9402,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
94059402
destination_script,
94069403

94079404
holder_commitment_point,
9408-
cur_holder_commitment_transaction_number,
94099405
cur_counterparty_commitment_transaction_number,
94109406
value_to_self_msat,
94119407

0 commit comments

Comments
 (0)