Skip to content

Commit d0780f7

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 4268120 commit d0780f7

File tree

1 file changed

+77
-88
lines changed

1 file changed

+77
-88
lines changed

lightning/src/ln/channel.rs

Lines changed: 77 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10793,9 +10793,10 @@ where
1079310793
}
1079410794
}
1079510795

10796-
let mut confirmed_funding_index = None;
10797-
let mut funding_already_confirmed = false;
1079810796
if let Some(pending_splice) = &mut self.pending_splice {
10797+
let mut confirmed_funding_index = None;
10798+
let mut funding_already_confirmed = false;
10799+
1079910800
for (index, funding) in pending_splice.negotiated_candidates.iter_mut().enumerate() {
1080010801
if self.context.check_for_funding_tx_confirmed(
1080110802
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
@@ -10810,40 +10811,36 @@ where
1081010811
funding_already_confirmed = true;
1081110812
}
1081210813
}
10813-
}
1081410814

10815-
if let Some(confirmed_funding_index) = confirmed_funding_index {
10816-
let pending_splice = match self.pending_splice.as_mut() {
10817-
Some(pending_splice) => pending_splice,
10818-
None => {
10819-
// TODO: Move pending_funding into pending_splice
10820-
debug_assert!(false);
10821-
let err = "expected a pending splice".to_string();
10822-
return Err(ClosureReason::ProcessingError { err });
10823-
},
10824-
};
10815+
if let Some(confirmed_funding_index) = confirmed_funding_index {
10816+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
10817+
&self.context,
10818+
confirmed_funding_index,
10819+
height,
10820+
) {
1082510821

10826-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, confirmed_funding_index, height) {
10827-
log_info!(
10828-
logger,
10829-
"Sending splice_locked txid {} to our peer for channel {}",
10830-
splice_locked.splice_txid,
10831-
&self.context.channel_id,
10832-
);
10833-
10834-
let (funding_txo, monitor_update, announcement_sigs, discarded_funding) =
10835-
self.maybe_promote_splice_funding(
10836-
node_signer, chain_hash, user_config, height, logger,
10837-
).map(|splice_promotion| (
10838-
Some(splice_promotion.funding_txo),
10839-
splice_promotion.monitor_update,
10840-
splice_promotion.announcement_sigs,
10841-
splice_promotion.discarded_funding,
10842-
)).unwrap_or((None, None, None, Vec::new()));
10822+
log_info!(
10823+
logger,
10824+
"Sending splice_locked txid {} to our peer for channel {}",
10825+
splice_locked.splice_txid,
10826+
&self.context.channel_id,
10827+
);
1084310828

10844-
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo, monitor_update, discarded_funding)), announcement_sigs));
10829+
let (funding_txo, monitor_update, announcement_sigs, discarded_funding) =
10830+
self.maybe_promote_splice_funding(
10831+
node_signer, chain_hash, user_config, height, logger,
10832+
).map(|splice_promotion| (
10833+
Some(splice_promotion.funding_txo),
10834+
splice_promotion.monitor_update,
10835+
splice_promotion.announcement_sigs,
10836+
splice_promotion.discarded_funding,
10837+
)).unwrap_or((None, None, None, Vec::new()));
10838+
10839+
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo, monitor_update, discarded_funding)), announcement_sigs));
10840+
}
1084510841
}
1084610842
}
10843+
1084710844
}
1084810845

1084910846
Ok((None, None))
@@ -10946,70 +10943,62 @@ where
1094610943
return Err(ClosureReason::FundingTimedOut);
1094710944
}
1094810945

10949-
let mut confirmed_funding_index = None;
10950-
for (index, funding) in self.pending_funding().iter().enumerate() {
10951-
if funding.funding_tx_confirmation_height != 0 {
10952-
if confirmed_funding_index.is_some() {
10953-
let err_reason = "splice tx of another pending funding already confirmed";
10954-
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
10955-
}
10946+
if let Some(pending_splice) = &mut self.pending_splice {
10947+
let mut confirmed_funding_index = None;
10948+
10949+
for (index, funding) in pending_splice.negotiated_candidates.iter().enumerate() {
10950+
if funding.funding_tx_confirmation_height != 0 {
10951+
if confirmed_funding_index.is_some() {
10952+
let err_reason = "splice tx of another pending funding already confirmed";
10953+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
10954+
}
1095610955

10957-
confirmed_funding_index = Some(index);
10956+
confirmed_funding_index = Some(index);
10957+
}
1095810958
}
10959-
}
1096010959

10961-
if let Some(confirmed_funding_index) = confirmed_funding_index {
10962-
let pending_splice = match self.pending_splice.as_mut() {
10963-
Some(pending_splice) => pending_splice,
10964-
None => {
10965-
// TODO: Move pending_funding into pending_splice
10966-
debug_assert!(false);
10967-
let err = "expected a pending splice".to_string();
10968-
return Err(ClosureReason::ProcessingError { err });
10969-
},
10970-
};
10971-
let funding = &mut pending_splice.negotiated_candidates[confirmed_funding_index];
10972-
10973-
// Check if the splice funding transaction was unconfirmed
10974-
if funding.get_funding_tx_confirmations(height) == 0 {
10975-
funding.funding_tx_confirmation_height = 0;
10976-
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
10977-
if Some(sent_funding_txid) == funding.get_funding_txid() {
10978-
log_warn!(
10979-
logger,
10980-
"Unconfirming sent splice_locked txid {} for channel {}",
10981-
sent_funding_txid,
10982-
&self.context.channel_id,
10983-
);
10984-
pending_splice.sent_funding_txid = None;
10960+
if let Some(confirmed_funding_index) = confirmed_funding_index {
10961+
let funding = &mut pending_splice.negotiated_candidates[confirmed_funding_index];
10962+
10963+
// Check if the splice funding transaction was unconfirmed
10964+
if funding.get_funding_tx_confirmations(height) == 0 {
10965+
funding.funding_tx_confirmation_height = 0;
10966+
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
10967+
if Some(sent_funding_txid) == funding.get_funding_txid() {
10968+
log_warn!(
10969+
logger,
10970+
"Unconfirming sent splice_locked txid {} for channel {}",
10971+
sent_funding_txid,
10972+
&self.context.channel_id,
10973+
);
10974+
pending_splice.sent_funding_txid = None;
10975+
}
1098510976
}
1098610977
}
10987-
}
1098810978

10989-
let pending_splice = self.pending_splice.as_mut().unwrap();
10990-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
10991-
&self.context,
10992-
confirmed_funding_index,
10993-
height,
10994-
) {
10995-
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
10996-
debug_assert!(chain_node_signer.is_some());
10997-
10998-
let (funding_txo, monitor_update, announcement_sigs, discarded_funding) = chain_node_signer
10999-
.and_then(|(chain_hash, node_signer, user_config)| {
11000-
// We can only promote on blocks connected, which is when we expect
11001-
// `chain_node_signer` to be `Some`.
11002-
self.maybe_promote_splice_funding(node_signer, chain_hash, user_config, height, logger)
11003-
})
11004-
.map(|splice_promotion| (
11005-
Some(splice_promotion.funding_txo),
11006-
splice_promotion.monitor_update,
11007-
splice_promotion.announcement_sigs,
11008-
splice_promotion.discarded_funding,
11009-
))
11010-
.unwrap_or((None, None, None, Vec::new()));
10979+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
10980+
&self.context,
10981+
confirmed_funding_index,
10982+
height,
10983+
) {
10984+
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
1101110985

11012-
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo, monitor_update, discarded_funding)), timed_out_htlcs, announcement_sigs));
10986+
let (funding_txo, monitor_update, announcement_sigs, discarded_funding) = chain_node_signer
10987+
.and_then(|(chain_hash, node_signer, user_config)| {
10988+
// We can only promote on blocks connected, which is when we expect
10989+
// `chain_node_signer` to be `Some`.
10990+
self.maybe_promote_splice_funding(node_signer, chain_hash, user_config, height, logger)
10991+
})
10992+
.map(|splice_promotion| (
10993+
Some(splice_promotion.funding_txo),
10994+
splice_promotion.monitor_update,
10995+
splice_promotion.announcement_sigs,
10996+
splice_promotion.discarded_funding,
10997+
))
10998+
.unwrap_or((None, None, None, Vec::new()));
10999+
11000+
return Ok((Some(FundingConfirmedMessage::Splice(splice_locked, funding_txo, monitor_update, discarded_funding)), timed_out_htlcs, announcement_sigs));
11001+
}
1101311002
}
1101411003
}
1101511004

0 commit comments

Comments
 (0)