Skip to content

Commit 889e178

Browse files
committed
Use HolderCommitmentPoint for commitment number
1 parent 52a5285 commit 889e178

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
@@ -1358,7 +1358,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13581358
// cost of others, but should really just be changed.
13591359

13601360
holder_commitment_point: HolderCommitmentPoint,
1361-
cur_holder_commitment_transaction_number: u64,
13621361
cur_counterparty_commitment_transaction_number: u64,
13631362
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
13641363
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1833,7 +1832,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18331832
destination_script,
18341833

18351834
holder_commitment_point,
1836-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18371835
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
18381836
value_to_self_msat,
18391837

@@ -2059,7 +2057,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20592057
destination_script,
20602058

20612059
holder_commitment_point,
2062-
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20632060
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
20642061
value_to_self_msat,
20652062

@@ -4450,9 +4447,9 @@ impl<SP: Deref> Channel<SP> where
44504447

44514448
let funding_script = self.context.get_funding_redeemscript();
44524449

4453-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
4450+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
44544451

4455-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
4452+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger);
44564453
let commitment_txid = {
44574454
let trusted_tx = commitment_stats.tx.trust();
44584455
let bitcoin_tx = trusted_tx.built_transaction();
@@ -4615,7 +4612,7 @@ impl<SP: Deref> Channel<SP> where
46154612
channel_id: Some(self.context.channel_id()),
46164613
};
46174614

4618-
self.context.cur_holder_commitment_transaction_number -= 1;
4615+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
46194616
self.context.expecting_peer_commitment_signed = false;
46204617
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
46214618
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -5126,8 +5123,8 @@ impl<SP: Deref> Channel<SP> where
51265123
// Before proposing a feerate update, check that we can actually afford the new fee.
51275124
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
51285125
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
5129-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
5130-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
5126+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
5127+
let commitment_stats = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, true, logger);
51315128
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;
51325129
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
51335130
if holder_balance_msat < buffer_fee_msat + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -5309,7 +5306,7 @@ impl<SP: Deref> Channel<SP> where
53095306
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53105307
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53115308
self.context.monitor_pending_channel_ready = false;
5312-
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);
5309+
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);
53135310
Some(msgs::ChannelReady {
53145311
channel_id: self.context.channel_id(),
53155312
next_per_commitment_point,
@@ -5416,8 +5413,8 @@ impl<SP: Deref> Channel<SP> where
54165413
}
54175414

54185415
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
5419-
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);
5420-
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
5416+
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);
5417+
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
54215418
msgs::RevokeAndACK {
54225419
channel_id: self.context.channel_id,
54235420
per_commitment_secret,
@@ -5550,7 +5547,7 @@ impl<SP: Deref> Channel<SP> where
55505547
return Err(ChannelError::Close("Peer sent an invalid channel_reestablish to force close in a non-standard way".to_owned()));
55515548
}
55525549

5553-
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number - 1;
5550+
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() - 1;
55545551
if msg.next_remote_commitment_number > 0 {
55555552
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);
55565553
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
@@ -5612,7 +5609,7 @@ impl<SP: Deref> Channel<SP> where
56125609
}
56135610

56145611
// We have OurChannelReady set!
5615-
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);
5612+
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);
56165613
return Ok(ReestablishResponses {
56175614
channel_ready: Some(msgs::ChannelReady {
56185615
channel_id: self.context.channel_id(),
@@ -5655,9 +5652,9 @@ impl<SP: Deref> Channel<SP> where
56555652
}
56565653
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
56575654

5658-
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
5655+
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56595656
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5660-
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);
5657+
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);
56615658
Some(msgs::ChannelReady {
56625659
channel_id: self.context.channel_id(),
56635660
next_per_commitment_point,
@@ -6308,7 +6305,7 @@ impl<SP: Deref> Channel<SP> where
63086305
}
63096306

63106307
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
6311-
self.context.cur_holder_commitment_transaction_number + 1
6308+
self.context.holder_commitment_point.transaction_number() + 1
63126309
}
63136310

63146311
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -6423,7 +6420,7 @@ impl<SP: Deref> Channel<SP> where
64236420
debug_assert!(self.context.minimum_depth.unwrap_or(1) > 0);
64246421
return true;
64256422
}
6426-
if self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
6423+
if self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER - 1 &&
64276424
self.context.cur_counterparty_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
64286425
// If we're a 0-conf channel, we'll move beyond AwaitingChannelReady immediately even while
64296426
// waiting for the initial monitor persistence. Thus, we check if our commitment
@@ -6985,7 +6982,7 @@ impl<SP: Deref> Channel<SP> where
69856982

69866983
// next_local_commitment_number is the next commitment_signed number we expect to
69876984
// receive (indicating if they need to resend one that we missed).
6988-
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number,
6985+
next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number(),
69896986
// We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
69906987
// receive, however we track it by the next commitment number for a remote transaction
69916988
// (which is one further, as they always revoke previous commitment transaction, not
@@ -7537,7 +7534,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
75377534
}
75387535
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
75397536
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7540-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7537+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
75417538
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
75427539
}
75437540

@@ -7592,7 +7589,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
75927589
/// Returns true if we can resume the channel by sending the [`msgs::OpenChannel`] again.
75937590
pub fn is_resumable(&self) -> bool {
75947591
!self.context.have_received_message() &&
7595-
self.context.cur_holder_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER
7592+
self.context.holder_commitment_point.transaction_number() == INITIAL_COMMITMENT_NUMBER
75967593
}
75977594

75987595
pub fn get_open_channel(&self, chain_hash: ChainHash) -> msgs::OpenChannel {
@@ -7603,11 +7600,11 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76037600
panic!("Cannot generate an open_channel after we've moved forward");
76047601
}
76057602

7606-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7603+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
76077604
panic!("Tried to send an open_channel for a channel that has already advanced");
76087605
}
76097606

7610-
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);
7607+
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);
76117608
let keys = self.context.get_holder_pubkeys();
76127609

76137610
msgs::OpenChannel {
@@ -7788,7 +7785,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
77887785
}
77897786
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
77907787
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
7791-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7788+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
77927789
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
77937790
}
77947791

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

7805-
let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
7806-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
7802+
let holder_signer = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
7803+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &holder_signer, true, false, logger).tx;
78077804
{
78087805
let trusted_tx = initial_commitment_tx.trust();
78097806
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
@@ -7856,7 +7853,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78567853
} else {
78577854
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
78587855
}
7859-
self.context.cur_holder_commitment_transaction_number -= 1;
7856+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
78607857
self.context.cur_counterparty_commitment_transaction_number -= 1;
78617858

78627859
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -7992,7 +7989,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79927989
) {
79937990
panic!("Tried to send accept_channel after channel had moved forward");
79947991
}
7995-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7992+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
79967993
panic!("Tried to send an accept_channel for a channel that has already advanced");
79977994
}
79987995

@@ -8005,7 +8002,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80058002
///
80068003
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80078004
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
8008-
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);
8005+
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);
80098006
let keys = self.context.get_holder_pubkeys();
80108007

80118008
msgs::AcceptChannel {
@@ -8047,8 +8044,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80478044
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
80488045
let funding_script = self.context.get_funding_redeemscript();
80498046

8050-
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
8051-
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
8047+
let keys = self.context.build_holder_transaction_keys(self.context.holder_commitment_point.transaction_number());
8048+
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
80528049
let trusted_tx = initial_commitment_tx.trust();
80538050
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
80548051
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
@@ -8082,7 +8079,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80828079
}
80838080
if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
80848081
self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
8085-
self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8082+
self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
80868083
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
80878084
}
80888085

@@ -8122,7 +8119,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81228119
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
81238120
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
81248121
self.context.cur_counterparty_commitment_transaction_number -= 1;
8125-
self.context.cur_holder_commitment_transaction_number -= 1;
8122+
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
81268123

81278124
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
81288125

@@ -8243,15 +8240,15 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
82438240
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
82448241
}
82458242

8246-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8243+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
82478244
debug_assert!(false, "Tried to send an open_channel2 for a channel that has already advanced");
82488245
}
82498246

82508247
let first_per_commitment_point = self.context.holder_signer.as_ref()
8251-
.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number,
8248+
.get_per_commitment_point(self.context.holder_commitment_point.transaction_number(),
82528249
&self.context.secp_ctx);
82538250
let second_per_commitment_point = self.context.holder_signer.as_ref()
8254-
.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number - 1,
8251+
.get_per_commitment_point(self.context.holder_commitment_point.transaction_number() - 1,
82558252
&self.context.secp_ctx);
82568253
let keys = self.context.get_holder_pubkeys();
82578254

@@ -8385,7 +8382,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
83858382
) {
83868383
debug_assert!(false, "Tried to send accept_channel2 after channel had moved forward");
83878384
}
8388-
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
8385+
if self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
83898386
debug_assert!(false, "Tried to send an accept_channel2 for a channel that has already advanced");
83908387
}
83918388

@@ -8399,9 +8396,9 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
83998396
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
84008397
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
84018398
let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8402-
self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
8399+
self.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx);
84038400
let second_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
8404-
self.context.cur_holder_commitment_transaction_number - 1, &self.context.secp_ctx);
8401+
self.context.holder_commitment_point.transaction_number() - 1, &self.context.secp_ctx);
84058402
let keys = self.context.get_holder_pubkeys();
84068403

84078404
msgs::AcceptChannelV2 {
@@ -8574,7 +8571,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
85748571
}
85758572
self.context.destination_script.write(writer)?;
85768573

8577-
self.context.cur_holder_commitment_transaction_number.write(writer)?;
8574+
self.context.holder_commitment_point.transaction_number().write(writer)?;
85788575
self.context.cur_counterparty_commitment_transaction_number.write(writer)?;
85798576
self.context.value_to_self_msat.write(writer)?;
85808577

@@ -9390,7 +9387,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
93909387
destination_script,
93919388

93929389
holder_commitment_point,
9393-
cur_holder_commitment_transaction_number,
93949390
cur_counterparty_commitment_transaction_number,
93959391
value_to_self_msat,
93969392

0 commit comments

Comments
 (0)