@@ -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;
@@ -3494,7 +3517,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3494
3517
// Before proposing a feerate update, check that we can actually afford the new fee.
3495
3518
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3496
3519
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3497
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3520
+ let keys = self.context.build_holder_transaction_keys();
3498
3521
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3499
3522
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;
3500
3523
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3675,10 +3698,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3675
3698
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3676
3699
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3677
3700
self.context.monitor_pending_channel_ready = false;
3678
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3679
3701
Some(msgs::ChannelReady {
3680
3702
channel_id: self.context.channel_id(),
3681
- next_per_commitment_point,
3703
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3682
3704
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3683
3705
})
3684
3706
} else { None };
@@ -3757,12 +3779,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3757
3779
}
3758
3780
3759
3781
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3760
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3761
3782
let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3762
3783
msgs::RevokeAndACK {
3763
3784
channel_id: self.context.channel_id,
3764
3785
per_commitment_secret,
3765
- next_per_commitment_point,
3786
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3766
3787
#[cfg(taproot)]
3767
3788
next_local_nonce: None,
3768
3789
}
@@ -3861,7 +3882,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3861
3882
}
3862
3883
3863
3884
if msg.next_remote_commitment_number > 0 {
3864
- let expected_point = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
3885
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3886
+ let expected_point = self.context.holder_signer.get_per_commitment_point(state_index, &self.context.secp_ctx)
3887
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3865
3888
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3866
3889
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3867
3890
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3926,11 +3949,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3926
3949
}
3927
3950
3928
3951
// We have OurChannelReady set!
3929
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3930
3952
return Ok(ReestablishResponses {
3931
3953
channel_ready: Some(msgs::ChannelReady {
3932
3954
channel_id: self.context.channel_id(),
3933
- next_per_commitment_point,
3955
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3934
3956
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3935
3957
}),
3936
3958
raa: None, commitment_update: None,
@@ -3966,10 +3988,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3966
3988
3967
3989
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3968
3990
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3969
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3970
3991
Some(msgs::ChannelReady {
3971
3992
channel_id: self.context.channel_id(),
3972
- next_per_commitment_point,
3993
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3973
3994
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3974
3995
})
3975
3996
} else { None };
@@ -4655,13 +4676,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4655
4676
if need_commitment_update {
4656
4677
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4657
4678
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4658
- let next_per_commitment_point =
4659
- self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4660
- return Some(msgs::ChannelReady {
4661
- channel_id: self.context.channel_id ,
4662
- next_per_commitment_point ,
4663
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4664
- });
4679
+ if let Ok( next_per_commitment_point) = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx) {
4680
+ return Some(msgs::ChannelReady {
4681
+ channel_id: self.context.channel_id,
4682
+ next_per_commitment_point ,
4683
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4684
+ });
4685
+ }
4665
4686
}
4666
4687
} else {
4667
4688
self.context.monitor_pending_channel_ready = true;
@@ -5588,6 +5609,9 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5588
5609
5589
5610
let temporary_channel_id = entropy_source.get_secure_random_bytes();
5590
5611
5612
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5613
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5614
+
5591
5615
Ok(Self {
5592
5616
context: ChannelContext {
5593
5617
user_id,
@@ -5616,6 +5640,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5616
5640
destination_script,
5617
5641
5618
5642
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5643
+ next_per_commitment_point,
5619
5644
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5620
5645
value_to_self_msat,
5621
5646
@@ -5854,7 +5879,6 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5854
5879
panic!("Tried to send an open_channel for a channel that has already advanced");
5855
5880
}
5856
5881
5857
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5858
5882
let keys = self.context.get_holder_pubkeys();
5859
5883
5860
5884
msgs::OpenChannel {
@@ -5874,7 +5898,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5874
5898
payment_point: keys.payment_point,
5875
5899
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5876
5900
htlc_basepoint: keys.htlc_basepoint,
5877
- first_per_commitment_point,
5901
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5878
5902
channel_flags: if self.context.config.announced_channel {1} else {0},
5879
5903
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5880
5904
Some(script) => script.clone().into_inner(),
@@ -6233,6 +6257,9 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6233
6257
let mut secp_ctx = Secp256k1::new();
6234
6258
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
6235
6259
6260
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
6261
+ .map_err(|_| ChannelError::Close("Unable to generate initial commitment point".to_owned()))?;
6262
+
6236
6263
let chan = Self {
6237
6264
context: ChannelContext {
6238
6265
user_id,
@@ -6260,6 +6287,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6260
6287
destination_script,
6261
6288
6262
6289
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6290
+ next_per_commitment_point,
6263
6291
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6264
6292
value_to_self_msat: msg.push_msat,
6265
6293
@@ -6408,7 +6436,6 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6408
6436
///
6409
6437
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6410
6438
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6411
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6412
6439
let keys = self.context.get_holder_pubkeys();
6413
6440
6414
6441
msgs::AcceptChannel {
@@ -6425,7 +6452,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6425
6452
payment_point: keys.payment_point,
6426
6453
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6427
6454
htlc_basepoint: keys.htlc_basepoint,
6428
- first_per_commitment_point,
6455
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6429
6456
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6430
6457
Some(script) => script.clone().into_inner(),
6431
6458
None => Builder::new().into_script(),
@@ -6448,7 +6475,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6448
6475
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6449
6476
let funding_script = self.context.get_funding_redeemscript();
6450
6477
6451
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6478
+ let keys = self.context.build_holder_transaction_keys();
6452
6479
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6453
6480
{
6454
6481
let trusted_tx = initial_commitment_tx.trust();
@@ -6556,6 +6583,13 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6556
6583
self.context.cur_counterparty_commitment_transaction_number -= 1;
6557
6584
self.context.cur_holder_commitment_transaction_number -= 1;
6558
6585
6586
+ let next_per_commitment_point_result = self.context.holder_signer.get_per_commitment_point(
6587
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6588
+ if next_per_commitment_point_result.is_err() {
6589
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6590
+ }
6591
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6592
+
6559
6593
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6560
6594
6561
6595
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7309,6 +7343,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7309
7343
let mut secp_ctx = Secp256k1::new();
7310
7344
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7311
7345
7346
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7347
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7348
+ cur_holder_commitment_transaction_number, &secp_ctx
7349
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7350
+
7312
7351
// `user_id` used to be a single u64 value. In order to remain backwards
7313
7352
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7314
7353
// separate u64 values.
@@ -7361,6 +7400,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7361
7400
destination_script,
7362
7401
7363
7402
cur_holder_commitment_transaction_number,
7403
+ next_per_commitment_point,
7364
7404
cur_counterparty_commitment_transaction_number,
7365
7405
value_to_self_msat,
7366
7406
0 commit comments