Skip to content

Commit d8da168

Browse files
committed
Remove unnecessary FundedChannel::pending_splice checks
Now that PendingFunding directly contains the negotiated candidates, some unnecessary checks can be removed.
1 parent 2f6e346 commit d8da168

File tree

1 file changed

+80
-101
lines changed

1 file changed

+80
-101
lines changed

lightning/src/ln/channel.rs

Lines changed: 80 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9649,12 +9649,11 @@ where
96499649
}
96509650
}
96519651

9652-
#[cfg(splicing)]
9653-
let mut confirmed_funding_index = None;
9654-
#[cfg(splicing)]
9655-
let mut funding_already_confirmed = false;
96569652
#[cfg(splicing)]
96579653
if let Some(pending_splice) = &mut self.pending_splice {
9654+
let mut confirmed_funding_index = None;
9655+
let mut funding_already_confirmed = false;
9656+
96589657
for (index, funding) in pending_splice.negotiated_candidates.iter_mut().enumerate() {
96599658
if self.context.check_for_funding_tx_confirmed(
96609659
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
@@ -9669,51 +9668,40 @@ where
96699668
funding_already_confirmed = true;
96709669
}
96719670
}
9672-
}
96739671

9674-
#[cfg(splicing)]
9675-
if let Some(confirmed_funding_index) = confirmed_funding_index {
9676-
let pending_splice = match self.pending_splice.as_mut() {
9677-
Some(pending_splice) => pending_splice,
9678-
None => {
9679-
// TODO: Move pending_funding into pending_splice
9680-
debug_assert!(false);
9681-
let err = "expected a pending splice".to_string();
9682-
return Err(ClosureReason::ProcessingError { err });
9683-
},
9684-
};
9685-
9686-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9687-
&self.context,
9688-
confirmed_funding_index,
9689-
height,
9690-
) {
9691-
for &(idx, tx) in txdata.iter() {
9692-
if idx > index_in_block {
9693-
let funding = pending_splice.negotiated_candidates.get(confirmed_funding_index).unwrap();
9694-
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9672+
if let Some(confirmed_funding_index) = confirmed_funding_index {
9673+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9674+
&self.context,
9675+
confirmed_funding_index,
9676+
height,
9677+
) {
9678+
for &(idx, tx) in txdata.iter() {
9679+
if idx > index_in_block {
9680+
let funding = pending_splice.negotiated_candidates.get(confirmed_funding_index).unwrap();
9681+
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9682+
}
96959683
}
9696-
}
96979684

9698-
log_info!(
9699-
logger,
9700-
"Sending splice_locked txid {} to our peer for channel {}",
9701-
splice_locked.splice_txid,
9702-
&self.context.channel_id,
9703-
);
9685+
log_info!(
9686+
logger,
9687+
"Sending splice_locked txid {} to our peer for channel {}",
9688+
splice_locked.splice_txid,
9689+
&self.context.channel_id,
9690+
);
97049691

9705-
let funding_promoted =
9706-
self.maybe_promote_splice_funding(confirmed_funding_index, logger);
9707-
let funding_txo = funding_promoted.then(|| {
9708-
self.funding
9709-
.get_funding_txo()
9710-
.expect("Splice FundingScope should always have a funding_txo")
9711-
});
9712-
let announcement_sigs = funding_promoted
9713-
.then(|| self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger))
9714-
.flatten();
9692+
let funding_promoted =
9693+
self.maybe_promote_splice_funding(confirmed_funding_index, logger);
9694+
let funding_txo = funding_promoted.then(|| {
9695+
self.funding
9696+
.get_funding_txo()
9697+
.expect("Splice FundingScope should always have a funding_txo")
9698+
});
9699+
let announcement_sigs = funding_promoted
9700+
.then(|| self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger))
9701+
.flatten();
97159702

9716-
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo)), announcement_sigs));
9703+
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo)), announcement_sigs));
9704+
}
97179705
}
97189706
}
97199707

@@ -9825,72 +9813,63 @@ where
98259813
}
98269814

98279815
#[cfg(splicing)]
9828-
let mut confirmed_funding_index = None;
9829-
#[cfg(splicing)]
9830-
for (index, funding) in self.pending_funding().iter().enumerate() {
9831-
if funding.funding_tx_confirmation_height != 0 {
9832-
if confirmed_funding_index.is_some() {
9833-
let err_reason = "splice tx of another pending funding already confirmed";
9834-
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9835-
}
9816+
if let Some(pending_splice) = &mut self.pending_splice {
9817+
let mut confirmed_funding_index = None;
98369818

9837-
confirmed_funding_index = Some(index);
9819+
for (index, funding) in pending_splice.negotiated_candidates.iter().enumerate() {
9820+
if funding.funding_tx_confirmation_height != 0 {
9821+
if confirmed_funding_index.is_some() {
9822+
let err_reason = "splice tx of another pending funding already confirmed";
9823+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9824+
}
9825+
9826+
confirmed_funding_index = Some(index);
9827+
}
98389828
}
9839-
}
98409829

9841-
#[cfg(splicing)]
9842-
if let Some(confirmed_funding_index) = confirmed_funding_index {
9843-
let pending_splice = match self.pending_splice.as_mut() {
9844-
Some(pending_splice) => pending_splice,
9845-
None => {
9846-
// TODO: Move pending_funding into pending_splice
9847-
debug_assert!(false);
9848-
let err = "expected a pending splice".to_string();
9849-
return Err(ClosureReason::ProcessingError { err });
9850-
},
9851-
};
9852-
let funding = pending_splice.negotiated_candidates.get_mut(confirmed_funding_index).unwrap();
9853-
9854-
// Check if the splice funding transaction was unconfirmed
9855-
if funding.get_funding_tx_confirmations(height) == 0 {
9856-
funding.funding_tx_confirmation_height = 0;
9857-
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
9858-
if Some(sent_funding_txid) == funding.get_funding_txid() {
9859-
log_warn!(
9860-
logger,
9861-
"Unconfirming sent splice_locked txid {} for channel {}",
9862-
sent_funding_txid,
9863-
&self.context.channel_id,
9864-
);
9865-
pending_splice.sent_funding_txid = None;
9830+
if let Some(confirmed_funding_index) = confirmed_funding_index {
9831+
let funding = pending_splice.negotiated_candidates.get_mut(confirmed_funding_index).unwrap();
9832+
9833+
// Check if the splice funding transaction was unconfirmed
9834+
if funding.get_funding_tx_confirmations(height) == 0 {
9835+
funding.funding_tx_confirmation_height = 0;
9836+
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
9837+
if Some(sent_funding_txid) == funding.get_funding_txid() {
9838+
log_warn!(
9839+
logger,
9840+
"Unconfirming sent splice_locked txid {} for channel {}",
9841+
sent_funding_txid,
9842+
&self.context.channel_id,
9843+
);
9844+
pending_splice.sent_funding_txid = None;
9845+
}
98669846
}
98679847
}
9868-
}
98699848

9870-
let pending_splice = self.pending_splice.as_mut().unwrap();
9871-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9872-
&self.context,
9873-
confirmed_funding_index,
9874-
height,
9875-
) {
9876-
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
9849+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9850+
&self.context,
9851+
confirmed_funding_index,
9852+
height,
9853+
) {
9854+
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
98779855

9878-
let funding_promoted =
9879-
self.maybe_promote_splice_funding(confirmed_funding_index, logger);
9880-
let funding_txo = funding_promoted.then(|| {
9881-
self.funding
9882-
.get_funding_txo()
9883-
.expect("Splice FundingScope should always have a funding_txo")
9884-
});
9885-
let announcement_sigs = funding_promoted
9886-
.then(|| chain_node_signer
9887-
.and_then(|(chain_hash, node_signer, user_config)|
9888-
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)
9856+
let funding_promoted =
9857+
self.maybe_promote_splice_funding(confirmed_funding_index, logger);
9858+
let funding_txo = funding_promoted.then(|| {
9859+
self.funding
9860+
.get_funding_txo()
9861+
.expect("Splice FundingScope should always have a funding_txo")
9862+
});
9863+
let announcement_sigs = funding_promoted
9864+
.then(|| chain_node_signer
9865+
.and_then(|(chain_hash, node_signer, user_config)|
9866+
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)
9867+
)
98899868
)
9890-
)
9891-
.flatten();
9869+
.flatten();
98929870

9893-
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo)), timed_out_htlcs, announcement_sigs));
9871+
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo)), timed_out_htlcs, announcement_sigs));
9872+
}
98949873
}
98959874
}
98969875

0 commit comments

Comments
 (0)