@@ -666,6 +666,13 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
666
666
// cost of others, but should really just be changed.
667
667
668
668
cur_holder_commitment_transaction_number: u64,
669
+
670
+ // The commitment point corresponding to `cur_holder_commitment_transaction_number`, which is the
671
+ // *next* state. We recompute it each time the state changes because the state changes in places
672
+ // that might be fallible: in particular, if the commitment point must be fetched from a remote
673
+ // source, we want to ensure it happens at a point where we can actually fail somewhat gracefully;
674
+ // i.e., force-closing a channel is better than a panic!
675
+ next_per_commitment_point: PublicKey,
669
676
cur_counterparty_commitment_transaction_number: u64,
670
677
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
671
678
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1441,13 +1448,14 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1441
1448
/// our counterparty!)
1442
1449
/// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
1443
1450
/// TODO Some magic rust shit to compile-time check this?
1444
- fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
1445
- let per_commitment_point = self.holder_signer.get_per_commitment_point(commitment_number, &self.secp_ctx);
1451
+ fn build_holder_transaction_keys(&self) -> TxCreationKeys {
1446
1452
let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
1447
1453
let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
1448
1454
let counterparty_pubkeys = self.get_counterparty_pubkeys();
1449
1455
1450
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1456
+ TxCreationKeys::derive_new(
1457
+ &self.secp_ctx, &self.next_per_commitment_point, delayed_payment_base, htlc_basepoint,
1458
+ &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1451
1459
}
1452
1460
1453
1461
#[inline]
@@ -2492,7 +2500,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2492
2500
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2493
2501
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2494
2502
2495
- let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
2503
+ self.context.next_per_commitment_point =
2504
+ self.context.holder_signer.get_per_commitment_point(
2505
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2506
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2507
+
2508
+ let holder_signer = self.context.build_holder_transaction_keys();
2496
2509
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
2497
2510
{
2498
2511
let trusted_tx = initial_commitment_tx.trust();
@@ -2536,6 +2549,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2536
2549
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2537
2550
self.context.channel_state = ChannelState::FundingSent as u32;
2538
2551
self.context.cur_holder_commitment_transaction_number -= 1;
2552
+ self.context.next_per_commitment_point =
2553
+ self.context.holder_signer.get_per_commitment_point(
2554
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2555
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2556
+
2539
2557
self.context.cur_counterparty_commitment_transaction_number -= 1;
2540
2558
2541
2559
log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.context.channel_id()));
@@ -2857,7 +2875,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2857
2875
2858
2876
let funding_script = self.context.get_funding_redeemscript();
2859
2877
2860
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
2878
+ let keys = self.context.build_holder_transaction_keys();
2861
2879
2862
2880
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
2863
2881
let commitment_txid = {
@@ -3021,6 +3039,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3021
3039
};
3022
3040
3023
3041
self.context.cur_holder_commitment_transaction_number -= 1;
3042
+ self.context.next_per_commitment_point =
3043
+ self.context.holder_signer.get_per_commitment_point(
3044
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
3045
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
3046
+
3024
3047
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
3025
3048
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3026
3049
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
@@ -3472,7 +3495,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3472
3495
// Before proposing a feerate update, check that we can actually afford the new fee.
3473
3496
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3474
3497
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3475
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3498
+ let keys = self.context.build_holder_transaction_keys();
3476
3499
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3477
3500
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + outbound_stats.on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
3478
3501
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3653,10 +3676,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3653
3676
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3654
3677
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3655
3678
self.context.monitor_pending_channel_ready = false;
3656
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3657
3679
Some(msgs::ChannelReady {
3658
3680
channel_id: self.context.channel_id(),
3659
- next_per_commitment_point,
3681
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3660
3682
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3661
3683
})
3662
3684
} else { None };
@@ -3735,12 +3757,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3735
3757
}
3736
3758
3737
3759
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3738
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3739
3760
let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3740
3761
msgs::RevokeAndACK {
3741
3762
channel_id: self.context.channel_id,
3742
3763
per_commitment_secret,
3743
- next_per_commitment_point,
3764
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3744
3765
#[cfg(taproot)]
3745
3766
next_local_nonce: None,
3746
3767
}
@@ -3839,7 +3860,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3839
3860
}
3840
3861
3841
3862
if msg.next_remote_commitment_number > 0 {
3842
- let expected_point = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
3863
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3864
+ let expected_point = self.context.holder_signer.get_per_commitment_point(state_index, &self.context.secp_ctx)
3865
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3843
3866
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3844
3867
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3845
3868
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3904,11 +3927,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3904
3927
}
3905
3928
3906
3929
// We have OurChannelReady set!
3907
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3908
3930
return Ok(ReestablishResponses {
3909
3931
channel_ready: Some(msgs::ChannelReady {
3910
3932
channel_id: self.context.channel_id(),
3911
- next_per_commitment_point,
3933
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3912
3934
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3913
3935
}),
3914
3936
raa: None, commitment_update: None,
@@ -3944,10 +3966,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3944
3966
3945
3967
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3946
3968
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3947
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3948
3969
Some(msgs::ChannelReady {
3949
3970
channel_id: self.context.channel_id(),
3950
- next_per_commitment_point,
3971
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3951
3972
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3952
3973
})
3953
3974
} else { None };
@@ -4633,13 +4654,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4633
4654
if need_commitment_update {
4634
4655
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4635
4656
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4636
- let next_per_commitment_point =
4637
- self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4638
- return Some(msgs::ChannelReady {
4639
- channel_id: self.context.channel_id ,
4640
- next_per_commitment_point ,
4641
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4642
- });
4657
+ if let Ok( next_per_commitment_point) = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx) {
4658
+ return Some(msgs::ChannelReady {
4659
+ channel_id: self.context.channel_id,
4660
+ next_per_commitment_point ,
4661
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4662
+ });
4663
+ }
4643
4664
}
4644
4665
} else {
4645
4666
self.context.monitor_pending_channel_ready = true;
@@ -5568,6 +5589,9 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5568
5589
5569
5590
let temporary_channel_id = entropy_source.get_secure_random_bytes();
5570
5591
5592
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5593
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5594
+
5571
5595
Ok(Self {
5572
5596
context: ChannelContext {
5573
5597
user_id,
@@ -5596,6 +5620,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5596
5620
destination_script,
5597
5621
5598
5622
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5623
+ next_per_commitment_point,
5599
5624
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5600
5625
value_to_self_msat,
5601
5626
@@ -5832,7 +5857,6 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5832
5857
panic!("Tried to send an open_channel for a channel that has already advanced");
5833
5858
}
5834
5859
5835
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5836
5860
let keys = self.context.get_holder_pubkeys();
5837
5861
5838
5862
msgs::OpenChannel {
@@ -5852,7 +5876,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5852
5876
payment_point: keys.payment_point,
5853
5877
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5854
5878
htlc_basepoint: keys.htlc_basepoint,
5855
- first_per_commitment_point,
5879
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5856
5880
channel_flags: if self.context.config.announced_channel {1} else {0},
5857
5881
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5858
5882
Some(script) => script.clone().into_inner(),
@@ -6202,6 +6226,8 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6202
6226
} else {
6203
6227
Some(cmp::max(config.channel_handshake_config.minimum_depth, 1))
6204
6228
};
6229
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
6230
+ .map_err(|_| ChannelError::Close("Unable to generate initial commitment point".to_owned()))?;
6205
6231
6206
6232
let chan = Self {
6207
6233
context: ChannelContext {
@@ -6230,6 +6256,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6230
6256
destination_script,
6231
6257
6232
6258
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6259
+ next_per_commitment_point,
6233
6260
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6234
6261
value_to_self_msat: msg.push_msat,
6235
6262
@@ -6360,7 +6387,6 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6360
6387
///
6361
6388
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6362
6389
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6363
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6364
6390
let keys = self.context.get_holder_pubkeys();
6365
6391
6366
6392
msgs::AcceptChannel {
@@ -6377,7 +6403,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6377
6403
payment_point: keys.payment_point,
6378
6404
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6379
6405
htlc_basepoint: keys.htlc_basepoint,
6380
- first_per_commitment_point,
6406
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6381
6407
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6382
6408
Some(script) => script.clone().into_inner(),
6383
6409
None => Builder::new().into_script(),
@@ -6400,7 +6426,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6400
6426
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6401
6427
let funding_script = self.context.get_funding_redeemscript();
6402
6428
6403
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6429
+ let keys = self.context.build_holder_transaction_keys();
6404
6430
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6405
6431
{
6406
6432
let trusted_tx = initial_commitment_tx.trust();
@@ -6505,6 +6531,13 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6505
6531
self.context.cur_counterparty_commitment_transaction_number -= 1;
6506
6532
self.context.cur_holder_commitment_transaction_number -= 1;
6507
6533
6534
+ let next_per_commitment_point_result = self.context.holder_signer.get_per_commitment_point(
6535
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6536
+ if next_per_commitment_point_result.is_err() {
6537
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6538
+ }
6539
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6540
+
6508
6541
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6509
6542
6510
6543
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7258,6 +7291,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7258
7291
let mut secp_ctx = Secp256k1::new();
7259
7292
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7260
7293
7294
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7295
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7296
+ cur_holder_commitment_transaction_number, &secp_ctx
7297
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7298
+
7261
7299
// `user_id` used to be a single u64 value. In order to remain backwards
7262
7300
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7263
7301
// separate u64 values.
@@ -7310,6 +7348,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7310
7348
destination_script,
7311
7349
7312
7350
cur_holder_commitment_transaction_number,
7351
+ next_per_commitment_point,
7313
7352
cur_counterparty_commitment_transaction_number,
7314
7353
value_to_self_msat,
7315
7354
0 commit comments