@@ -668,6 +668,13 @@ pub(super) struct ChannelContext<Signer: ChannelSigner> {
668
668
// cost of others, but should really just be changed.
669
669
670
670
cur_holder_commitment_transaction_number: u64,
671
+
672
+ // The commitment point corresponding to `cur_holder_commitment_transaction_number`, which is the
673
+ // *next* state. We recompute it each time the state changes because the state changes in places
674
+ // that might be fallible: in particular, if the commitment point must be fetched from a remote
675
+ // source, we want to ensure it happens at a point where we can actually fail somewhat gracefully;
676
+ // i.e., force-closing a channel is better than a panic!
677
+ next_per_commitment_point: PublicKey,
671
678
cur_counterparty_commitment_transaction_number: u64,
672
679
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
673
680
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1455,13 +1462,14 @@ impl<Signer: ChannelSigner> ChannelContext<Signer> {
1455
1462
/// our counterparty!)
1456
1463
/// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
1457
1464
/// TODO Some magic rust shit to compile-time check this?
1458
- fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
1459
- let per_commitment_point = self.holder_signer.get_per_commitment_point(commitment_number, &self.secp_ctx);
1465
+ fn build_holder_transaction_keys(&self) -> TxCreationKeys {
1460
1466
let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
1461
1467
let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
1462
1468
let counterparty_pubkeys = self.get_counterparty_pubkeys();
1463
1469
1464
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1470
+ TxCreationKeys::derive_new(
1471
+ &self.secp_ctx, &self.next_per_commitment_point, delayed_payment_base, htlc_basepoint,
1472
+ &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1465
1473
}
1466
1474
1467
1475
#[inline]
@@ -2515,7 +2523,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2515
2523
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2516
2524
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2517
2525
2518
- let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
2526
+ self.context.next_per_commitment_point =
2527
+ self.context.holder_signer.get_per_commitment_point(
2528
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2529
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2530
+
2531
+ let holder_signer = self.context.build_holder_transaction_keys();
2519
2532
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
2520
2533
{
2521
2534
let trusted_tx = initial_commitment_tx.trust();
@@ -2559,6 +2572,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2559
2572
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2560
2573
self.context.channel_state = ChannelState::FundingSent as u32;
2561
2574
self.context.cur_holder_commitment_transaction_number -= 1;
2575
+ self.context.next_per_commitment_point =
2576
+ self.context.holder_signer.get_per_commitment_point(
2577
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2578
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2579
+
2562
2580
self.context.cur_counterparty_commitment_transaction_number -= 1;
2563
2581
2564
2582
log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.context.channel_id()));
@@ -2880,7 +2898,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2880
2898
2881
2899
let funding_script = self.context.get_funding_redeemscript();
2882
2900
2883
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
2901
+ let keys = self.context.build_holder_transaction_keys();
2884
2902
2885
2903
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
2886
2904
let commitment_txid = {
@@ -3044,6 +3062,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3044
3062
};
3045
3063
3046
3064
self.context.cur_holder_commitment_transaction_number -= 1;
3065
+ self.context.next_per_commitment_point =
3066
+ self.context.holder_signer.get_per_commitment_point(
3067
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
3068
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
3069
+
3047
3070
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
3048
3071
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3049
3072
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
@@ -3495,7 +3518,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3495
3518
// Before proposing a feerate update, check that we can actually afford the new fee.
3496
3519
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3497
3520
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3498
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3521
+ let keys = self.context.build_holder_transaction_keys();
3499
3522
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3500
3523
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;
3501
3524
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3676,10 +3699,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3676
3699
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3677
3700
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3678
3701
self.context.monitor_pending_channel_ready = false;
3679
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3680
3702
Some(msgs::ChannelReady {
3681
3703
channel_id: self.context.channel_id(),
3682
- next_per_commitment_point,
3704
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3683
3705
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3684
3706
})
3685
3707
} else { None };
@@ -3758,12 +3780,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3758
3780
}
3759
3781
3760
3782
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3761
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3762
3783
let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3763
3784
msgs::RevokeAndACK {
3764
3785
channel_id: self.context.channel_id,
3765
3786
per_commitment_secret,
3766
- next_per_commitment_point,
3787
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3767
3788
#[cfg(taproot)]
3768
3789
next_local_nonce: None,
3769
3790
}
@@ -3862,7 +3883,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3862
3883
}
3863
3884
3864
3885
if msg.next_remote_commitment_number > 0 {
3865
- let expected_point = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
3886
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3887
+ let expected_point = self.context.holder_signer.get_per_commitment_point(state_index, &self.context.secp_ctx)
3888
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3866
3889
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3867
3890
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3868
3891
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3927,11 +3950,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3927
3950
}
3928
3951
3929
3952
// We have OurChannelReady set!
3930
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3931
3953
return Ok(ReestablishResponses {
3932
3954
channel_ready: Some(msgs::ChannelReady {
3933
3955
channel_id: self.context.channel_id(),
3934
- next_per_commitment_point,
3956
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3935
3957
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3936
3958
}),
3937
3959
raa: None, commitment_update: None,
@@ -3967,10 +3989,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3967
3989
3968
3990
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3969
3991
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3970
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3971
3992
Some(msgs::ChannelReady {
3972
3993
channel_id: self.context.channel_id(),
3973
- next_per_commitment_point,
3994
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3974
3995
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3975
3996
})
3976
3997
} else { None };
@@ -4656,13 +4677,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4656
4677
if need_commitment_update {
4657
4678
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4658
4679
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4659
- let next_per_commitment_point =
4660
- self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4661
- return Some(msgs::ChannelReady {
4662
- channel_id: self.context.channel_id ,
4663
- next_per_commitment_point ,
4664
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4665
- });
4680
+ if let Ok( next_per_commitment_point) = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx) {
4681
+ return Some(msgs::ChannelReady {
4682
+ channel_id: self.context.channel_id,
4683
+ next_per_commitment_point ,
4684
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4685
+ });
4686
+ }
4666
4687
}
4667
4688
} else {
4668
4689
self.context.monitor_pending_channel_ready = true;
@@ -5591,6 +5612,9 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5591
5612
5592
5613
let temporary_channel_id = entropy_source.get_secure_random_bytes();
5593
5614
5615
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5616
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5617
+
5594
5618
Ok(Self {
5595
5619
context: ChannelContext {
5596
5620
user_id,
@@ -5619,6 +5643,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5619
5643
destination_script,
5620
5644
5621
5645
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5646
+ next_per_commitment_point,
5622
5647
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5623
5648
value_to_self_msat,
5624
5649
@@ -5857,7 +5882,6 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5857
5882
panic!("Tried to send an open_channel for a channel that has already advanced");
5858
5883
}
5859
5884
5860
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5861
5885
let keys = self.context.get_holder_pubkeys();
5862
5886
5863
5887
msgs::OpenChannel {
@@ -5877,7 +5901,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5877
5901
payment_point: keys.payment_point,
5878
5902
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5879
5903
htlc_basepoint: keys.htlc_basepoint,
5880
- first_per_commitment_point,
5904
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5881
5905
channel_flags: if self.context.config.announced_channel {1} else {0},
5882
5906
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5883
5907
Some(script) => script.clone().into_inner(),
@@ -6222,6 +6246,9 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6222
6246
let mut secp_ctx = Secp256k1::new();
6223
6247
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
6224
6248
6249
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
6250
+ .map_err(|_| ChannelError::Close("Unable to generate initial commitment point".to_owned()))?;
6251
+
6225
6252
let chan = Self {
6226
6253
context: ChannelContext {
6227
6254
user_id,
@@ -6249,6 +6276,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6249
6276
destination_script,
6250
6277
6251
6278
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6279
+ next_per_commitment_point,
6252
6280
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6253
6281
value_to_self_msat: msg.push_msat,
6254
6282
@@ -6397,7 +6425,6 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6397
6425
///
6398
6426
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6399
6427
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6400
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6401
6428
let keys = self.context.get_holder_pubkeys();
6402
6429
6403
6430
msgs::AcceptChannel {
@@ -6414,7 +6441,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6414
6441
payment_point: keys.payment_point,
6415
6442
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6416
6443
htlc_basepoint: keys.htlc_basepoint,
6417
- first_per_commitment_point,
6444
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6418
6445
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6419
6446
Some(script) => script.clone().into_inner(),
6420
6447
None => Builder::new().into_script(),
@@ -6437,7 +6464,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6437
6464
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6438
6465
let funding_script = self.context.get_funding_redeemscript();
6439
6466
6440
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6467
+ let keys = self.context.build_holder_transaction_keys();
6441
6468
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6442
6469
{
6443
6470
let trusted_tx = initial_commitment_tx.trust();
@@ -6545,6 +6572,13 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6545
6572
self.context.cur_counterparty_commitment_transaction_number -= 1;
6546
6573
self.context.cur_holder_commitment_transaction_number -= 1;
6547
6574
6575
+ let next_per_commitment_point_result = self.context.holder_signer.get_per_commitment_point(
6576
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6577
+ if next_per_commitment_point_result.is_err() {
6578
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6579
+ }
6580
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6581
+
6548
6582
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6549
6583
6550
6584
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7298,6 +7332,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7298
7332
let mut secp_ctx = Secp256k1::new();
7299
7333
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7300
7334
7335
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7336
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7337
+ cur_holder_commitment_transaction_number, &secp_ctx
7338
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7339
+
7301
7340
// `user_id` used to be a single u64 value. In order to remain backwards
7302
7341
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7303
7342
// separate u64 values.
@@ -7350,6 +7389,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7350
7389
destination_script,
7351
7390
7352
7391
cur_holder_commitment_transaction_number,
7392
+ next_per_commitment_point,
7353
7393
cur_counterparty_commitment_transaction_number,
7354
7394
value_to_self_msat,
7355
7395
0 commit comments