@@ -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};
@@ -667,6 +667,13 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
667
667
// cost of others, but should really just be changed.
668
668
669
669
cur_holder_commitment_transaction_number: u64,
670
+
671
+ // The commitment point corresponding to `cur_holder_commitment_transaction_number`, which is the
672
+ // *next* state. We recompute it each time the state changes because the state changes in places
673
+ // that might be fallible: in particular, if the commitment point must be fetched from a remote
674
+ // source, we want to ensure it happens at a point where we can actually fail somewhat gracefully;
675
+ // i.e., force-closing a channel is better than a panic!
676
+ next_per_commitment_point: PublicKey,
670
677
cur_counterparty_commitment_transaction_number: u64,
671
678
value_to_self_msat: u64, // Excluding all pending_htlcs, excluding fees
672
679
pending_inbound_htlcs: Vec<InboundHTLCOutput>,
@@ -1442,13 +1449,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1442
1449
/// our counterparty!)
1443
1450
/// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
1444
1451
/// TODO Some magic rust shit to compile-time check this?
1445
- fn build_holder_transaction_keys(&self, commitment_number: u64) -> TxCreationKeys {
1446
- let per_commitment_point = self.holder_signer.as_ref().get_per_commitment_point(commitment_number, &self.secp_ctx);
1452
+ fn build_holder_transaction_keys(&self) -> TxCreationKeys {
1447
1453
let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
1448
1454
let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
1449
1455
let counterparty_pubkeys = self.get_counterparty_pubkeys();
1450
1456
1451
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1457
+ TxCreationKeys::derive_new(
1458
+ &self.secp_ctx, &self.next_per_commitment_point, delayed_payment_base, htlc_basepoint,
1459
+ &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
1452
1460
}
1453
1461
1454
1462
#[inline]
@@ -2495,7 +2503,12 @@ impl<SP: Deref> Channel<SP> where
2495
2503
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2496
2504
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2497
2505
2498
- let holder_signer = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
2506
+ self.context.next_per_commitment_point =
2507
+ self.context.holder_signer.as_ref().get_per_commitment_point(
2508
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2509
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2510
+
2511
+ let holder_signer = self.context.build_holder_transaction_keys();
2499
2512
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &holder_signer, true, false, logger).tx;
2500
2513
{
2501
2514
let trusted_tx = initial_commitment_tx.trust();
@@ -2545,6 +2558,11 @@ impl<SP: Deref> Channel<SP> where
2545
2558
assert_eq!(self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32), 0); // We have no had any monitor(s) yet to fail update!
2546
2559
self.context.channel_state = ChannelState::FundingSent as u32;
2547
2560
self.context.cur_holder_commitment_transaction_number -= 1;
2561
+ self.context.next_per_commitment_point =
2562
+ self.context.holder_signer.as_ref().get_per_commitment_point(
2563
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
2564
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
2565
+
2548
2566
self.context.cur_counterparty_commitment_transaction_number -= 1;
2549
2567
2550
2568
log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.context.channel_id()));
@@ -2866,7 +2884,7 @@ impl<SP: Deref> Channel<SP> where
2866
2884
2867
2885
let funding_script = self.context.get_funding_redeemscript();
2868
2886
2869
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
2887
+ let keys = self.context.build_holder_transaction_keys();
2870
2888
2871
2889
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger);
2872
2890
let commitment_txid = {
@@ -3030,6 +3048,11 @@ impl<SP: Deref> Channel<SP> where
3030
3048
};
3031
3049
3032
3050
self.context.cur_holder_commitment_transaction_number -= 1;
3051
+ self.context.next_per_commitment_point =
3052
+ self.context.holder_signer.as_ref().get_per_commitment_point(
3053
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx
3054
+ ).map_err(|_| ChannelError::Close("Unable to generate commitment point".to_owned()))?;
3055
+
3033
3056
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
3034
3057
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3035
3058
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
@@ -3509,7 +3532,7 @@ impl<SP: Deref> Channel<SP> where
3509
3532
// Before proposing a feerate update, check that we can actually afford the new fee.
3510
3533
let inbound_stats = self.context.get_inbound_pending_htlc_stats(Some(feerate_per_kw));
3511
3534
let outbound_stats = self.context.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
3512
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
3535
+ let keys = self.context.build_holder_transaction_keys();
3513
3536
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3514
3537
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;
3515
3538
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
@@ -3690,10 +3713,9 @@ impl<SP: Deref> Channel<SP> where
3690
3713
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
3691
3714
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
3692
3715
self.context.monitor_pending_channel_ready = false;
3693
- 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);
3694
3716
Some(msgs::ChannelReady {
3695
3717
channel_id: self.context.channel_id(),
3696
- next_per_commitment_point,
3718
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3697
3719
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3698
3720
})
3699
3721
} else { None };
@@ -3772,12 +3794,13 @@ impl<SP: Deref> Channel<SP> where
3772
3794
}
3773
3795
3774
3796
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3775
- 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);
3776
- let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3797
+ // TODO(waterson): fallible!
3798
+ let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2)
3799
+ .expect("release_per_commitment failed");
3777
3800
msgs::RevokeAndACK {
3778
3801
channel_id: self.context.channel_id,
3779
3802
per_commitment_secret,
3780
- next_per_commitment_point,
3803
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3781
3804
#[cfg(taproot)]
3782
3805
next_local_nonce: None,
3783
3806
}
@@ -3887,7 +3910,9 @@ impl<SP: Deref> Channel<SP> where
3887
3910
}
3888
3911
3889
3912
if msg.next_remote_commitment_number > 0 {
3890
- 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);
3913
+ let state_index = INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1;
3914
+ let expected_point = self.context.holder_signer.as_ref().get_per_commitment_point(state_index, &self.context.secp_ctx)
3915
+ .map_err(|_| ChannelError::Close(format!("Unable to retrieve per-commitment point for state {state_index}")))?;
3891
3916
let given_secret = SecretKey::from_slice(&msg.your_last_per_commitment_secret)
3892
3917
.map_err(|_| ChannelError::Close("Peer sent a garbage channel_reestablish with unparseable secret key".to_owned()))?;
3893
3918
if expected_point != PublicKey::from_secret_key(&self.context.secp_ctx, &given_secret) {
@@ -3946,11 +3971,10 @@ impl<SP: Deref> Channel<SP> where
3946
3971
}
3947
3972
3948
3973
// We have OurChannelReady set!
3949
- 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);
3950
3974
return Ok(ReestablishResponses {
3951
3975
channel_ready: Some(msgs::ChannelReady {
3952
3976
channel_id: self.context.channel_id(),
3953
- next_per_commitment_point,
3977
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3954
3978
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3955
3979
}),
3956
3980
raa: None, commitment_update: None,
@@ -3986,10 +4010,9 @@ impl<SP: Deref> Channel<SP> where
3986
4010
3987
4011
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
3988
4012
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
3989
- 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);
3990
4013
Some(msgs::ChannelReady {
3991
4014
channel_id: self.context.channel_id(),
3992
- next_per_commitment_point,
4015
+ next_per_commitment_point: self.context.next_per_commitment_point ,
3993
4016
short_channel_id_alias: Some(self.context.outbound_scid_alias),
3994
4017
})
3995
4018
} else { None };
@@ -4682,13 +4705,13 @@ impl<SP: Deref> Channel<SP> where
4682
4705
if need_commitment_update {
4683
4706
if self.context.channel_state & (ChannelState::MonitorUpdateInProgress as u32) == 0 {
4684
4707
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
4685
- let next_per_commitment_point =
4686
- self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
4687
- return Some(msgs::ChannelReady {
4688
- channel_id: self.context.channel_id ,
4689
- next_per_commitment_point ,
4690
- short_channel_id_alias: Some(self.context.outbound_scid_alias),
4691
- });
4708
+ 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) {
4709
+ return Some(msgs::ChannelReady {
4710
+ channel_id: self.context.channel_id,
4711
+ next_per_commitment_point ,
4712
+ short_channel_id_alias: Some(self.context.outbound_scid_alias) ,
4713
+ });
4714
+ }
4692
4715
}
4693
4716
} else {
4694
4717
self.context.monitor_pending_channel_ready = true;
@@ -5638,6 +5661,9 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5638
5661
5639
5662
let temporary_channel_id = entropy_source.get_secure_random_bytes();
5640
5663
5664
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
5665
+ .map_err(|_| APIError::ChannelUnavailable { err: "Unable to generate initial commitment point".to_owned()})?;
5666
+
5641
5667
Ok(Self {
5642
5668
context: ChannelContext {
5643
5669
user_id,
@@ -5666,6 +5692,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5666
5692
destination_script,
5667
5693
5668
5694
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5695
+ next_per_commitment_point,
5669
5696
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
5670
5697
value_to_self_msat,
5671
5698
@@ -5907,7 +5934,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5907
5934
panic!("Tried to send an open_channel for a channel that has already advanced");
5908
5935
}
5909
5936
5910
- 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);
5911
5937
let keys = self.context.get_holder_pubkeys();
5912
5938
5913
5939
msgs::OpenChannel {
@@ -5927,7 +5953,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5927
5953
payment_point: keys.payment_point,
5928
5954
delayed_payment_basepoint: keys.delayed_payment_basepoint,
5929
5955
htlc_basepoint: keys.htlc_basepoint,
5930
- first_per_commitment_point,
5956
+ first_per_commitment_point: self.context.next_per_commitment_point ,
5931
5957
channel_flags: if self.context.config.announced_channel {1} else {0},
5932
5958
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
5933
5959
Some(script) => script.clone().into_inner(),
@@ -6276,6 +6302,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6276
6302
} else {
6277
6303
Some(cmp::max(config.channel_handshake_config.minimum_depth, 1))
6278
6304
};
6305
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, &secp_ctx)
6306
+ .map_err(|_| ChannelError::Close("Unable to generate initial commitment point".to_owned()))?;
6279
6307
6280
6308
let chan = Self {
6281
6309
context: ChannelContext {
@@ -6304,6 +6332,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6304
6332
destination_script,
6305
6333
6306
6334
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6335
+ next_per_commitment_point,
6307
6336
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
6308
6337
value_to_self_msat: msg.push_msat,
6309
6338
@@ -6434,7 +6463,6 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6434
6463
///
6435
6464
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
6436
6465
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
6437
- 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);
6438
6466
let keys = self.context.get_holder_pubkeys();
6439
6467
6440
6468
msgs::AcceptChannel {
@@ -6451,7 +6479,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6451
6479
payment_point: keys.payment_point,
6452
6480
delayed_payment_basepoint: keys.delayed_payment_basepoint,
6453
6481
htlc_basepoint: keys.htlc_basepoint,
6454
- first_per_commitment_point,
6482
+ first_per_commitment_point: self.context.next_per_commitment_point ,
6455
6483
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
6456
6484
Some(script) => script.clone().into_inner(),
6457
6485
None => Builder::new().into_script(),
@@ -6474,7 +6502,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6474
6502
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(CommitmentTransaction, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6475
6503
let funding_script = self.context.get_funding_redeemscript();
6476
6504
6477
- let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number );
6505
+ let keys = self.context.build_holder_transaction_keys();
6478
6506
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6479
6507
{
6480
6508
let trusted_tx = initial_commitment_tx.trust();
@@ -6588,6 +6616,13 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6588
6616
self.context.cur_counterparty_commitment_transaction_number -= 1;
6589
6617
self.context.cur_holder_commitment_transaction_number -= 1;
6590
6618
6619
+ let next_per_commitment_point_result = self.context.holder_signer.as_ref().get_per_commitment_point(
6620
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
6621
+ if next_per_commitment_point_result.is_err() {
6622
+ return Err((self, ChannelError::Close("Unable to generate commitment point".to_owned())));
6623
+ }
6624
+ self.context.next_per_commitment_point = next_per_commitment_point_result.unwrap();
6625
+
6591
6626
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.context.channel_id()));
6592
6627
6593
6628
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -7342,6 +7377,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7342
7377
let mut secp_ctx = Secp256k1::new();
7343
7378
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7344
7379
7380
+ // If we weren't able to load the next_per_commitment_point, ask the signer for it now.
7381
+ let next_per_commitment_point = holder_signer.get_per_commitment_point(
7382
+ cur_holder_commitment_transaction_number, &secp_ctx
7383
+ ).map_err(|_| DecodeError::Io(io::ErrorKind::Other))?;
7384
+
7345
7385
// `user_id` used to be a single u64 value. In order to remain backwards
7346
7386
// compatible with versions prior to 0.0.113, the u128 is serialized as two
7347
7387
// separate u64 values.
@@ -7394,6 +7434,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7394
7434
destination_script,
7395
7435
7396
7436
cur_holder_commitment_transaction_number,
7437
+ next_per_commitment_point,
7397
7438
cur_counterparty_commitment_transaction_number,
7398
7439
value_to_self_msat,
7399
7440
0 commit comments