Skip to content

Commit ac090a4

Browse files
committed
Don't set sent_funding_txid in maybe_promote_splice_funding
When sending splice_locked, set PendingSplice::sent_funding_txid prior to calling FundedChannel::maybe_promote_splice_funding. This makes the latter idempotent when the splice is not promoted. Otherwise, successive calls could override the previously set sent_funding_txid.
1 parent b5bfa85 commit ac090a4

File tree

1 file changed

+74
-54
lines changed

1 file changed

+74
-54
lines changed

lightning/src/ln/channel.rs

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,41 @@ struct PendingSplice {
21562156
received_funding_txid: Option<Txid>,
21572157
}
21582158

2159+
#[cfg(splicing)]
2160+
impl PendingSplice {
2161+
fn check_get_splice_locked<SP: Deref>(
2162+
&mut self, context: &ChannelContext<SP>, funding: &FundingScope, height: u32,
2163+
) -> Option<msgs::SpliceLocked>
2164+
where
2165+
SP::Target: SignerProvider,
2166+
{
2167+
if !context.check_funding_meets_minimum_depth(funding, height) {
2168+
return None;
2169+
}
2170+
2171+
let confirmed_funding_txid = match funding.get_funding_txid() {
2172+
Some(funding_txid) => funding_txid,
2173+
None => {
2174+
debug_assert!(false);
2175+
return None;
2176+
},
2177+
};
2178+
2179+
match self.sent_funding_txid {
2180+
Some(sent_funding_txid) if confirmed_funding_txid == sent_funding_txid => None,
2181+
_ => {
2182+
let splice_locked = msgs::SpliceLocked {
2183+
channel_id: context.channel_id(),
2184+
splice_txid: confirmed_funding_txid,
2185+
};
2186+
self.sent_funding_txid = Some(splice_locked.splice_txid);
2187+
Some(splice_locked)
2188+
},
2189+
}
2190+
}
2191+
2192+
}
2193+
21592194
/// Wrapper around a [`Transaction`] useful for caching the result of [`Transaction::compute_txid`].
21602195
struct ConfirmedTransaction<'a> {
21612196
tx: &'a Transaction,
@@ -5525,6 +5560,29 @@ where
55255560
self.get_initial_counterparty_commitment_signature(funding, logger)
55265561
}
55275562

5563+
fn check_funding_meets_minimum_depth(&self, funding: &FundingScope, height: u32) -> bool {
5564+
let minimum_depth = self
5565+
.minimum_depth(funding)
5566+
.expect("ChannelContext::minimum_depth should be set for FundedChannel");
5567+
5568+
// Zero-conf channels always meet the minimum depth.
5569+
if minimum_depth == 0 {
5570+
return true;
5571+
}
5572+
5573+
if funding.funding_tx_confirmation_height == 0 {
5574+
return false;
5575+
}
5576+
5577+
let funding_tx_confirmations =
5578+
height as i64 - funding.funding_tx_confirmation_height as i64 + 1;
5579+
if funding_tx_confirmations < minimum_depth as i64 {
5580+
return false;
5581+
}
5582+
5583+
return true;
5584+
}
5585+
55285586
#[rustfmt::skip]
55295587
fn check_for_funding_tx_confirmed<L: Deref>(
55305588
&mut self, funding: &mut FundingScope, block_hash: &BlockHash, height: u32,
@@ -9074,58 +9132,13 @@ where
90749132
}
90759133
}
90769134

9077-
#[cfg(splicing)]
9078-
fn check_get_splice_locked(
9079-
&self, pending_splice: &PendingSplice, funding: &FundingScope, height: u32,
9080-
) -> Option<msgs::SpliceLocked> {
9081-
if !self.check_funding_meets_minimum_depth(funding, height) {
9082-
return None;
9083-
}
9084-
9085-
let confirmed_funding_txid = match funding.get_funding_txid() {
9086-
Some(funding_txid) => funding_txid,
9087-
None => {
9088-
debug_assert!(false);
9089-
return None;
9090-
},
9091-
};
9092-
9093-
match pending_splice.sent_funding_txid {
9094-
Some(sent_funding_txid) if confirmed_funding_txid == sent_funding_txid => None,
9095-
_ => Some(msgs::SpliceLocked {
9096-
channel_id: self.context.channel_id(),
9097-
splice_txid: confirmed_funding_txid,
9098-
}),
9099-
}
9100-
}
9101-
91029135
fn check_funding_meets_minimum_depth(&self, funding: &FundingScope, height: u32) -> bool {
9103-
let minimum_depth = self
9104-
.context
9105-
.minimum_depth(funding)
9106-
.expect("ChannelContext::minimum_depth should be set for FundedChannel");
9107-
9108-
// Zero-conf channels always meet the minimum depth.
9109-
if minimum_depth == 0 {
9110-
return true;
9111-
}
9112-
9113-
if funding.funding_tx_confirmation_height == 0 {
9114-
return false;
9115-
}
9116-
9117-
let funding_tx_confirmations =
9118-
height as i64 - funding.funding_tx_confirmation_height as i64 + 1;
9119-
if funding_tx_confirmations < minimum_depth as i64 {
9120-
return false;
9121-
}
9122-
9123-
return true;
9136+
self.context.check_funding_meets_minimum_depth(funding, height)
91249137
}
91259138

91269139
#[cfg(splicing)]
91279140
fn maybe_promote_splice_funding<L: Deref>(
9128-
&mut self, splice_txid: Txid, confirmed_funding_index: usize, logger: &L,
9141+
&mut self, confirmed_funding_index: usize, logger: &L,
91299142
) -> bool
91309143
where
91319144
L::Target: Logger,
@@ -9134,7 +9147,13 @@ where
91349147
debug_assert!(confirmed_funding_index < self.pending_funding.len());
91359148

91369149
let pending_splice = self.pending_splice.as_mut().unwrap();
9137-
pending_splice.sent_funding_txid = Some(splice_txid);
9150+
let splice_txid = match pending_splice.sent_funding_txid {
9151+
Some(sent_funding_txid) => sent_funding_txid,
9152+
None => {
9153+
debug_assert!(false);
9154+
return false;
9155+
},
9156+
};
91389157

91399158
if pending_splice.sent_funding_txid == pending_splice.received_funding_txid {
91409159
log_info!(
@@ -9145,6 +9164,7 @@ where
91459164
);
91469165

91479166
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9167+
debug_assert_eq!(Some(splice_txid), funding.get_funding_txid());
91489168
promote_splice_funding!(self, funding);
91499169

91509170
return true;
@@ -9234,7 +9254,7 @@ where
92349254

92359255
#[cfg(splicing)]
92369256
if let Some(confirmed_funding_index) = confirmed_funding_index {
9237-
let pending_splice = match self.pending_splice.as_ref() {
9257+
let pending_splice = match self.pending_splice.as_mut() {
92389258
Some(pending_splice) => pending_splice,
92399259
None => {
92409260
// TODO: Move pending_funding into pending_splice
@@ -9245,7 +9265,7 @@ where
92459265
};
92469266
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
92479267

9248-
if let Some(splice_locked) = self.check_get_splice_locked(pending_splice, funding, height) {
9268+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
92499269
for &(idx, tx) in txdata.iter() {
92509270
if idx > index_in_block {
92519271
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
@@ -9260,7 +9280,7 @@ where
92609280
);
92619281

92629282
let announcement_sigs = self
9263-
.maybe_promote_splice_funding(splice_locked.splice_txid, confirmed_funding_index, logger)
9283+
.maybe_promote_splice_funding(confirmed_funding_index, logger)
92649284
.then(|| self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger))
92659285
.flatten();
92669286

@@ -9417,13 +9437,13 @@ where
94179437
}
94189438
}
94199439

9420-
let pending_splice = self.pending_splice.as_ref().unwrap();
9440+
let pending_splice = self.pending_splice.as_mut().unwrap();
94219441
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
9422-
if let Some(splice_locked) = self.check_get_splice_locked(pending_splice, funding, height) {
9442+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
94239443
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
94249444

94259445
let announcement_sigs = self
9426-
.maybe_promote_splice_funding(splice_locked.splice_txid, confirmed_funding_index, logger)
9446+
.maybe_promote_splice_funding(confirmed_funding_index, logger)
94279447
.then(|| chain_node_signer
94289448
.and_then(|(chain_hash, node_signer, user_config)|
94299449
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)

0 commit comments

Comments
 (0)