@@ -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::{EcdsaChannelSigner, WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
38
+ use crate::sign::{EcdsaChannelSigner, 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};
@@ -670,6 +670,13 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
670
670
// cost of others, but should really just be changed.
671
671
672
672
cur_holder_commitment_transaction_number: u64,
673
+
674
+ // The commitment point corresponding to `cur_holder_commitment_transaction_number`, which is the
675
+ // *next* state. We recompute it each time the state changes because the state changes in places
676
+ // that might be fallible: in particular, if the commitment point must be fetched from a remote
677
+ // source, we want to ensure it happens at a point where we can actually fail somewhat gracefully;
678
+ // i.e., force-closing a channel is better than a panic!
679
+ next_per_commitment_point: PublicKey,
673
680
cur_counterparty_commitment_transaction_number: u64,
674
681
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
675
682
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1446,13 +1453,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1446
1453
/// our counterparty!)
1447
1454
/// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
1448
1455
/// TODO Some magic rust shit to compile-time check this?
1449
- fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
1450
- let per_commitment_point = self.holder_signer.as_ref().get_per_commitment_point(commitment_number, &self.secp_ctx);
1456
+ fn build_holder_transaction_keys(&self) -> TxCreationKeys {
1451
1457
let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
1452
1458
let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
1453
1459
let counterparty_pubkeys = self.get_counterparty_pubkeys();
1454
1460
1455
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1461
+ TxCreationKeys::derive_new(
1462
+ &self.secp_ctx, &self.next_per_commitment_point, delayed_payment_base, htlc_basepoint,
1463
+ &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1456
1464
}
1457
1465
1458
1466
#[inline]
@@ -2499,7 +2507,12 @@ impl<SP: Deref> Channel<SP> where
2499
2507
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2500
2508
&self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2501
2509
2502
- let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
2510
+ self.context.next_per_commitment_point =
2511
+ self.context.holder_signer.as_ref().get_per_commitment_point(
2512
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2513
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2514
+
2515
+ let holder_signer = self.context.build_holder_transaction_keys();
2503
2516
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
2504
2517
{
2505
2518
let trusted_tx = initial_commitment_tx.trust();
@@ -2549,6 +2562,11 @@ impl<SP: Deref> Channel<SP> where
2549
2562
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2550
2563
self.context.channel_state = ChannelState::FundingSent as u32;
2551
2564
self.context.cur_holder_commitment_transaction_number -= 1;
2565
+ self.context.next_per_commitment_point =
2566
+ self.context.holder_signer.as_ref().get_per_commitment_point(
2567
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2568
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2569
+
2552
2570
self.context.cur_counterparty_commitment_transaction_number -= 1;
2553
2571
2554
2572
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -2870,7 +2888,7 @@ impl<SP: Deref> Channel<SP> where
2870
2888
2871
2889
let funding_script = self.context.get_funding_redeemscript();
2872
2890
2873
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
2891
+ let keys = self.context.build_holder_transaction_keys();
2874
2892
2875
2893
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
2876
2894
let commitment_txid = {
@@ -3034,6 +3052,11 @@ impl<SP: Deref> Channel<SP> where
3034
3052
};
3035
3053
3036
3054
self.context.cur_holder_commitment_transaction_number -= 1;
3055
+ self.context.next_per_commitment_point =
3056
+ self.context.holder_signer.as_ref().get_per_commitment_point(
3057
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
3058
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
3059
+
3037
3060
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
3038
3061
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3039
3062
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
@@ -3512,7 +3535,7 @@ impl<SP: Deref> Channel<SP> where
3512
3535
// Before proposing a feerate update, check that we can actually afford the new fee.
3513
3536
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3514
3537
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3515
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3538
+ let keys = self.context.build_holder_transaction_keys();
3516
3539
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3517
3540
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;
3518
3541
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3693,10 +3716,9 @@ impl<SP: Deref> Channel<SP> where
3693
3716
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3694
3717
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3695
3718
self.context.monitor_pending_channel_ready = false;
3696
- 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);
3697
3719
Some(msgs::ChannelReady {
3698
3720
channel_id: self.context.channel_id(),
3699
- next_per_commitment_point,
3721
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3700
3722
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3701
3723
})
3702
3724
} else { None };
@@ -3775,12 +3797,13 @@ impl<SP: Deref> Channel<SP> where
3775
3797
}
3776
3798
3777
3799
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3778
- 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);
3779
- let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3800
+ // TODO(waterson): fallible!
3801
+ let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2)
3802
+ .expect("release_per_commitment failed");
3780
3803
msgs::RevokeAndACK {
3781
3804
channel_id: self.context.channel_id,
3782
3805
per_commitment_secret,
3783
- next_per_commitment_point,
3806
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3784
3807
#[cfg(taproot)]
3785
3808
next_local_nonce: None,
3786
3809
}
@@ -3890,7 +3913,9 @@ impl<SP: Deref> Channel<SP> where
3890
3913
}
3891
3914
3892
3915
if msg.next_remote_commitment_number > 0 {
3893
- 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);
3916
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3917
+ let expected_point = self.context.holder_signer.as_ref().get_per_commitment_point(state_index, &self.context.secp_ctx)
3918
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3894
3919
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3895
3920
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3896
3921
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3949,11 +3974,10 @@ impl<SP: Deref> Channel<SP> where
3949
3974
}
3950
3975
3951
3976
// We have OurChannelReady set!
3952
- 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);
3953
3977
return Ok(ReestablishResponses {
3954
3978
channel_ready: Some(msgs::ChannelReady {
3955
3979
channel_id: self.context.channel_id(),
3956
- next_per_commitment_point,
3980
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3957
3981
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3958
3982
}),
3959
3983
raa: None, commitment_update: None,
@@ -3989,10 +4013,9 @@ impl<SP: Deref> Channel<SP> where
3989
4013
3990
4014
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3991
4015
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3992
- 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);
3993
4016
Some(msgs::ChannelReady {
3994
4017
channel_id: self.context.channel_id(),
3995
- next_per_commitment_point,
4018
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3996
4019
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3997
4020
})
3998
4021
} else { None };
@@ -4685,13 +4708,13 @@ impl<SP: Deref> Channel<SP> where
4685
4708
if need_commitment_update {
4686
4709
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4687
4710
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4688
- let next_per_commitment_point =
4689
- self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4690
- return Some(msgs::ChannelReady {
4691
- channel_id: self.context.channel_id ,
4692
- next_per_commitment_point ,
4693
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4694
- });
4711
+ if let Ok( next_per_commitment_point) = self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx) {
4712
+ return Some(msgs::ChannelReady {
4713
+ channel_id: self.context.channel_id,
4714
+ next_per_commitment_point ,
4715
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4716
+ });
4717
+ }
4695
4718
}
4696
4719
} else {
4697
4720
self.context.monitor_pending_channel_ready = true;
@@ -5641,6 +5664,9 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5641
5664
5642
5665
let temporary_channel_id = ChannelId::temporary_from_entropy_source(entropy_source);
5643
5666
5667
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5668
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5669
+
5644
5670
Ok(Self {
5645
5671
context: ChannelContext {
5646
5672
user_id,
@@ -5669,6 +5695,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5669
5695
destination_script,
5670
5696
5671
5697
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5698
+ next_per_commitment_point,
5672
5699
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5673
5700
value_to_self_msat,
5674
5701
@@ -5910,7 +5937,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5910
5937
panic!("Tried to send an open_channel for a channel that has already advanced");
5911
5938
}
5912
5939
5913
- 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);
5914
5940
let keys = self.context.get_holder_pubkeys();
5915
5941
5916
5942
msgs::OpenChannel {
@@ -5930,7 +5956,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5930
5956
payment_point: keys.payment_point,
5931
5957
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5932
5958
htlc_basepoint: keys.htlc_basepoint,
5933
- first_per_commitment_point,
5959
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5934
5960
channel_flags: if self.context.config.announced_channel {1} else {0},
5935
5961
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5936
5962
Some(script) => script.clone().into_inner(),
@@ -6279,6 +6305,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6279
6305
} else {
6280
6306
Some(cmp::max(config.channel_handshake_config.minimum_depth, 1))
6281
6307
};
6308
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
6309
+ .map_err(|_| ChannelError::Close("Unable to generate initial commitment point".to_owned()))?;
6282
6310
6283
6311
let chan = Self {
6284
6312
context: ChannelContext {
@@ -6307,6 +6335,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6307
6335
destination_script,
6308
6336
6309
6337
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6338
+ next_per_commitment_point,
6310
6339
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6311
6340
value_to_self_msat: msg.push_msat,
6312
6341
@@ -6437,7 +6466,6 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6437
6466
///
6438
6467
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6439
6468
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6440
- 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);
6441
6469
let keys = self.context.get_holder_pubkeys();
6442
6470
6443
6471
msgs::AcceptChannel {
@@ -6454,7 +6482,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6454
6482
payment_point: keys.payment_point,
6455
6483
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6456
6484
htlc_basepoint: keys.htlc_basepoint,
6457
- first_per_commitment_point,
6485
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6458
6486
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6459
6487
Some(script) => script.clone().into_inner(),
6460
6488
None => Builder::new().into_script(),
@@ -6477,7 +6505,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6477
6505
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(CommitmentTransaction, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6478
6506
let funding_script = self.context.get_funding_redeemscript();
6479
6507
6480
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6508
+ let keys = self.context.build_holder_transaction_keys();
6481
6509
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6482
6510
{
6483
6511
let trusted_tx = initial_commitment_tx.trust();
@@ -6591,6 +6619,13 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6591
6619
self.context.cur_counterparty_commitment_transaction_number -= 1;
6592
6620
self.context.cur_holder_commitment_transaction_number -= 1;
6593
6621
6622
+ let next_per_commitment_point_result = self.context.holder_signer.as_ref().get_per_commitment_point(
6623
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6624
+ if next_per_commitment_point_result.is_err() {
6625
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6626
+ }
6627
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6628
+
6594
6629
log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6595
6630
6596
6631
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7345,6 +7380,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7345
7380
let mut secp_ctx = Secp256k1::new();
7346
7381
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7347
7382
7383
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7384
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7385
+ cur_holder_commitment_transaction_number, &secp_ctx
7386
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7387
+
7348
7388
// `user_id` used to be a single u64 value. In order to remain backwards
7349
7389
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7350
7390
// separate u64 values.
@@ -7397,6 +7437,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7397
7437
destination_script,
7398
7438
7399
7439
cur_holder_commitment_transaction_number,
7440
+ next_per_commitment_point,
7400
7441
cur_counterparty_commitment_transaction_number,
7401
7442
value_to_self_msat,
7402
7443
0 commit comments