Skip to content

Commit 9f3774d

Browse files
committed
f - side effect in check_get_splice_locked
1 parent 5f82072 commit 9f3774d

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 63 additions & 56 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,53 +9132,8 @@ 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)]
@@ -9241,7 +9254,7 @@ where
92419254

92429255
#[cfg(splicing)]
92439256
if let Some(confirmed_funding_index) = confirmed_funding_index {
9244-
let pending_splice = match self.pending_splice.as_ref() {
9257+
let pending_splice = match self.pending_splice.as_mut() {
92459258
Some(pending_splice) => pending_splice,
92469259
None => {
92479260
// TODO: Move pending_funding into pending_splice
@@ -9252,16 +9265,13 @@ where
92529265
};
92539266
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
92549267

9255-
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) {
92569269
for &(idx, tx) in txdata.iter() {
92579270
if idx > index_in_block {
92589271
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
92599272
}
92609273
}
92619274

9262-
let pending_splice = self.pending_splice.as_mut().unwrap();
9263-
pending_splice.sent_funding_txid = Some(splice_locked.splice_txid);
9264-
92659275
log_info!(
92669276
logger,
92679277
"Sending splice_locked txid {} to our peer for channel {}",
@@ -9427,12 +9437,9 @@ where
94279437
}
94289438
}
94299439

9430-
let pending_splice = self.pending_splice.as_ref().unwrap();
9440+
let pending_splice = self.pending_splice.as_mut().unwrap();
94319441
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
9432-
if let Some(splice_locked) = self.check_get_splice_locked(pending_splice, funding, height) {
9433-
let pending_splice = self.pending_splice.as_mut().unwrap();
9434-
pending_splice.sent_funding_txid = Some(splice_locked.splice_txid);
9435-
9442+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
94369443
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
94379444

94389445
let announcement_sigs = self

0 commit comments

Comments
 (0)