@@ -35,7 +35,7 @@ use crate::chain::BestBlock;
35
35
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
36
36
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS, CLOSED_CHANNEL_UPDATE_ID};
37
37
use crate::chain::transaction::{OutPoint, TransactionData};
38
- use crate::sign::{WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
38
+ use crate::sign::{WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient, SignerError };
39
39
use crate::events::ClosureReason;
40
40
use crate::routing::gossip::NodeId;
41
41
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
@@ -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;
@@ -3496,7 +3519,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3496
3519
// Before proposing a feerate update, check that we can actually afford the new fee.
3497
3520
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3498
3521
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3499
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3522
+ let keys = self.context.build_holder_transaction_keys();
3500
3523
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3501
3524
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;
3502
3525
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3677,10 +3700,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3677
3700
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3678
3701
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3679
3702
self.context.monitor_pending_channel_ready = false;
3680
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3681
3703
Some(msgs::ChannelReady {
3682
3704
channel_id: self.context.channel_id(),
3683
- next_per_commitment_point,
3705
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3684
3706
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3685
3707
})
3686
3708
} else { None };
@@ -3759,12 +3781,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3759
3781
}
3760
3782
3761
3783
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3762
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3763
- let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3784
+ // TODO(waterson): fallible!
3785
+ let per_commitment_secret = self.context.holder_signer.release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2)
3786
+ .expect("release_per_commitment failed");
3764
3787
msgs::RevokeAndACK {
3765
3788
channel_id: self.context.channel_id,
3766
3789
per_commitment_secret,
3767
- next_per_commitment_point,
3790
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3768
3791
#[cfg(taproot)]
3769
3792
next_local_nonce: None,
3770
3793
}
@@ -3874,7 +3897,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3874
3897
}
3875
3898
3876
3899
if msg.next_remote_commitment_number > 0 {
3877
- let expected_point = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1, &self.context.secp_ctx);
3900
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3901
+ let expected_point = self.context.holder_signer.get_per_commitment_point(state_index, &self.context.secp_ctx)
3902
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3878
3903
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3879
3904
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3880
3905
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3933,11 +3958,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3933
3958
}
3934
3959
3935
3960
// We have OurChannelReady set!
3936
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3937
3961
return Ok(ReestablishResponses {
3938
3962
channel_ready: Some(msgs::ChannelReady {
3939
3963
channel_id: self.context.channel_id(),
3940
- next_per_commitment_point,
3964
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3941
3965
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3942
3966
}),
3943
3967
raa: None, commitment_update: None,
@@ -3973,10 +3997,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3973
3997
3974
3998
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3975
3999
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3976
- let next_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
3977
4000
Some(msgs::ChannelReady {
3978
4001
channel_id: self.context.channel_id(),
3979
- next_per_commitment_point,
4002
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3980
4003
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3981
4004
})
3982
4005
} else { None };
@@ -4662,13 +4685,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4662
4685
if need_commitment_update {
4663
4686
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4664
4687
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4665
- let next_per_commitment_point =
4666
- self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4667
- return Some(msgs::ChannelReady {
4668
- channel_id: self.context.channel_id ,
4669
- next_per_commitment_point ,
4670
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4671
- });
4688
+ if let Ok( next_per_commitment_point) = self.context.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx) {
4689
+ return Some(msgs::ChannelReady {
4690
+ channel_id: self.context.channel_id,
4691
+ next_per_commitment_point ,
4692
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4693
+ });
4694
+ }
4672
4695
}
4673
4696
} else {
4674
4697
self.context.monitor_pending_channel_ready = true;
@@ -5597,6 +5620,9 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5597
5620
5598
5621
let temporary_channel_id = entropy_source.get_secure_random_bytes();
5599
5622
5623
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5624
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5625
+
5600
5626
Ok(Self {
5601
5627
context: ChannelContext {
5602
5628
user_id,
@@ -5625,6 +5651,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5625
5651
destination_script,
5626
5652
5627
5653
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5654
+ next_per_commitment_point,
5628
5655
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5629
5656
value_to_self_msat,
5630
5657
@@ -5861,7 +5888,6 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5861
5888
panic!("Tried to send an open_channel for a channel that has already advanced");
5862
5889
}
5863
5890
5864
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
5865
5891
let keys = self.context.get_holder_pubkeys();
5866
5892
5867
5893
msgs::OpenChannel {
@@ -5881,7 +5907,7 @@ impl<Signer: WriteableEcdsaChannelSigner> OutboundV1Channel<Signer> {
5881
5907
payment_point: keys.payment_point,
5882
5908
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5883
5909
htlc_basepoint: keys.htlc_basepoint,
5884
- first_per_commitment_point,
5910
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5885
5911
channel_flags: if self.context.config.announced_channel {1} else {0},
5886
5912
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5887
5913
Some(script) => script.clone().into_inner(),
@@ -6231,6 +6257,8 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6231
6257
} else {
6232
6258
Some(cmp::max(config.channel_handshake_config.minimum_depth, 1))
6233
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()))?;
6234
6262
6235
6263
let chan = Self {
6236
6264
context: ChannelContext {
@@ -6259,6 +6287,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6259
6287
destination_script,
6260
6288
6261
6289
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6290
+ next_per_commitment_point,
6262
6291
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6263
6292
value_to_self_msat: msg.push_msat,
6264
6293
@@ -6389,7 +6418,6 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6389
6418
///
6390
6419
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6391
6420
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6392
- let first_per_commitment_point = self.context.holder_signer.get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6393
6421
let keys = self.context.get_holder_pubkeys();
6394
6422
6395
6423
msgs::AcceptChannel {
@@ -6406,7 +6434,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6406
6434
payment_point: keys.payment_point,
6407
6435
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6408
6436
htlc_basepoint: keys.htlc_basepoint,
6409
- first_per_commitment_point,
6437
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6410
6438
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6411
6439
Some(script) => script.clone().into_inner(),
6412
6440
None => Builder::new().into_script(),
@@ -6429,7 +6457,7 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6429
6457
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(Txid, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6430
6458
let funding_script = self.context.get_funding_redeemscript();
6431
6459
6432
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6460
+ let keys = self.context.build_holder_transaction_keys();
6433
6461
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6434
6462
{
6435
6463
let trusted_tx = initial_commitment_tx.trust();
@@ -6534,6 +6562,13 @@ impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
6534
6562
self.context.cur_counterparty_commitment_transaction_number -= 1;
6535
6563
self.context.cur_holder_commitment_transaction_number -= 1;
6536
6564
6565
+ let next_per_commitment_point_result = self.context.holder_signer.get_per_commitment_point(
6566
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6567
+ if next_per_commitment_point_result.is_err() {
6568
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6569
+ }
6570
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6571
+
6537
6572
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6538
6573
6539
6574
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7287,6 +7322,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7287
7322
let mut secp_ctx = Secp256k1::new();
7288
7323
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7289
7324
7325
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7326
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7327
+ cur_holder_commitment_transaction_number, &secp_ctx
7328
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7329
+
7290
7330
// `user_id` used to be a single u64 value. In order to remain backwards
7291
7331
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7292
7332
// separate u64 values.
@@ -7339,6 +7379,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7339
7379
destination_script,
7340
7380
7341
7381
cur_holder_commitment_transaction_number,
7382
+ next_per_commitment_point,
7342
7383
cur_counterparty_commitment_transaction_number,
7343
7384
value_to_self_msat,
7344
7385
0 commit comments