Skip to content

Commit a7ba4dd

Browse files
committed
Store current_point in HolderCommitmentPoint
When splicing a channel, sending and receiving the initial commitment_signed message should use the current commitment point rather then the next one. Store this in HolderCommitmentPoint whenever it is advanced.
1 parent 27138eb commit a7ba4dd

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ pub(crate) struct ShutdownResult {
12511251
#[derive(Debug, Copy, Clone)]
12521252
struct HolderCommitmentPoint {
12531253
next_transaction_number: u64,
1254+
current_point: Option<PublicKey>,
12541255
next_point: PublicKey,
12551256
pending_next_point: Option<PublicKey>,
12561257
}
@@ -1262,6 +1263,7 @@ impl HolderCommitmentPoint {
12621263
{
12631264
Some(HolderCommitmentPoint {
12641265
next_transaction_number: INITIAL_COMMITMENT_NUMBER,
1266+
current_point: None,
12651267
next_point: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx).ok()?,
12661268
pending_next_point: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx).ok(),
12671269
})
@@ -1271,6 +1273,14 @@ impl HolderCommitmentPoint {
12711273
self.pending_next_point.is_some()
12721274
}
12731275

1276+
pub fn current_transaction_number(&self) -> u64 {
1277+
self.next_transaction_number + 1
1278+
}
1279+
1280+
pub fn current_point(&self) -> Option<PublicKey> {
1281+
self.current_point
1282+
}
1283+
12741284
pub fn next_transaction_number(&self) -> u64 {
12751285
self.next_transaction_number
12761286
}
@@ -1328,6 +1338,7 @@ impl HolderCommitmentPoint {
13281338
if let Some(next_point) = self.pending_next_point {
13291339
*self = Self {
13301340
next_transaction_number: self.next_transaction_number - 1,
1341+
current_point: Some(self.next_point),
13311342
next_point,
13321343
pending_next_point: None,
13331344
};
@@ -9591,7 +9602,7 @@ where
95919602
}
95929603

95939604
pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
9594-
self.holder_commitment_point.next_transaction_number() + 1
9605+
self.holder_commitment_point.current_transaction_number()
95959606
}
95969607

95979608
pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
@@ -10582,6 +10593,15 @@ where
1058210593
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
1058310594
funding_feerate_per_kw: u32, locktime: u32,
1058410595
) -> Result<msgs::SpliceInit, APIError> {
10596+
if self.holder_commitment_point.current_point().is_none() {
10597+
return Err(APIError::APIMisuseError {
10598+
err: format!(
10599+
"Channel {} cannot be spliced, commitment point needs to be advanced once",
10600+
self.context.channel_id(),
10601+
),
10602+
});
10603+
}
10604+
1058510605
// Check if a splice has been initiated already.
1058610606
// Note: only a single outstanding splice is supported (per spec)
1058710607
if self.pending_splice.is_some() {
@@ -10683,6 +10703,13 @@ where
1068310703

1068410704
// TODO(splicing): Add check that we are the quiescence acceptor
1068510705

10706+
if self.holder_commitment_point.current_point().is_none() {
10707+
return Err(ChannelError::Warn(format!(
10708+
"Channel {} commitment point needs to be advanced once before spliced",
10709+
self.context.channel_id(),
10710+
)));
10711+
}
10712+
1068610713
// Check if a splice has been initiated already.
1068710714
if self.pending_splice.is_some() {
1068810715
return Err(ChannelError::WarnAndDisconnect(format!(
@@ -13198,6 +13225,7 @@ where
1319813225
}
1319913226
let is_manual_broadcast = Some(self.context.is_manual_broadcast);
1320013227

13228+
let holder_commitment_point_current = self.holder_commitment_point.current_point();
1320113229
// `HolderCommitmentPoint::next_point` will become optional when async signing is implemented.
1320213230
let holder_commitment_point_next = Some(self.holder_commitment_point.next_point());
1320313231
let holder_commitment_point_pending_next = self.holder_commitment_point.pending_next_point;
@@ -13250,6 +13278,7 @@ where
1325013278
(59, self.funding.minimum_depth_override, option), // Added in 0.2
1325113279
(60, self.context.historical_scids, optional_vec), // Added in 0.2
1325213280
(61, fulfill_attribution_data, optional_vec), // Added in 0.2
13281+
(63, holder_commitment_point_current, option), // Added in 0.2
1325313282
});
1325413283

1325513284
Ok(())
@@ -13599,6 +13628,7 @@ where
1359913628
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
1360013629
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
1360113630

13631+
let mut holder_commitment_point_current_opt: Option<PublicKey> = None;
1360213632
let mut holder_commitment_point_next_opt: Option<PublicKey> = None;
1360313633
let mut holder_commitment_point_pending_next_opt: Option<PublicKey> = None;
1360413634
let mut is_manual_broadcast = None;
@@ -13652,6 +13682,7 @@ where
1365213682
(59, minimum_depth_override, option), // Added in 0.2
1365313683
(60, historical_scids, optional_vec), // Added in 0.2
1365413684
(61, fulfill_attribution_data, optional_vec), // Added in 0.2
13685+
(63, holder_commitment_point_current_opt, option), // Added in 0.2
1365513686
});
1365613687

1365713688
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -13833,6 +13864,7 @@ where
1383313864
match (holder_commitment_point_next_opt, holder_commitment_point_pending_next_opt) {
1383413865
(Some(next_point), pending_next_point) => HolderCommitmentPoint {
1383513866
next_transaction_number: holder_commitment_next_transaction_number,
13867+
current_point: None,
1383613868
next_point,
1383713869
pending_next_point,
1383813870
},
@@ -13852,6 +13884,7 @@ where
1385213884
);
1385313885
HolderCommitmentPoint {
1385413886
next_transaction_number: holder_commitment_next_transaction_number,
13887+
current_point: holder_commitment_point_current_opt,
1385513888
next_point,
1385613889
pending_next_point: Some(pending_next_point),
1385713890
}

0 commit comments

Comments
 (0)