Skip to content

Commit 6349685

Browse files
committed
Move FundedChannel::pending_funding into PendingSplice
FundedChannel::pending_funding and FundedChannel::pending_splice were developed independently, but the former will only contain values when the latter is set.
1 parent 78347f0 commit 6349685

File tree

1 file changed

+75
-49
lines changed

1 file changed

+75
-49
lines changed

lightning/src/ln/channel.rs

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,6 @@ where
20372037
};
20382038
let mut funded_channel = FundedChannel {
20392039
funding: chan.funding,
2040-
pending_funding: vec![],
20412040
context: chan.context,
20422041
interactive_tx_signing_session: chan.interactive_tx_signing_session,
20432042
holder_commitment_point,
@@ -2550,6 +2549,7 @@ impl AddSigned for u64 {
25502549
/// Info about a pending splice
25512550
struct PendingSplice {
25522551
funding_negotiation: Option<FundingNegotiation>,
2552+
pending_funding: Vec<FundingScope>,
25532553

25542554
/// The funding txid used in the `splice_locked` sent to the counterparty.
25552555
sent_funding_txid: Option<Txid>,
@@ -2576,11 +2576,14 @@ impl FundingNegotiation {
25762576

25772577
impl PendingSplice {
25782578
fn check_get_splice_locked<SP: Deref>(
2579-
&mut self, context: &ChannelContext<SP>, funding: &FundingScope, height: u32,
2579+
&mut self, context: &ChannelContext<SP>, confirmed_funding_index: usize, height: u32,
25802580
) -> Option<msgs::SpliceLocked>
25812581
where
25822582
SP::Target: SignerProvider,
25832583
{
2584+
debug_assert!(confirmed_funding_index < self.pending_funding.len());
2585+
2586+
let funding = &self.pending_funding[confirmed_funding_index];
25842587
if !context.check_funding_meets_minimum_depth(funding, height) {
25852588
return None;
25862589
}
@@ -6632,7 +6635,6 @@ where
66326635
SP::Target: SignerProvider,
66336636
{
66346637
pub funding: FundingScope,
6635-
pending_funding: Vec<FundingScope>,
66366638
pub context: ChannelContext<SP>,
66376639
/// The signing session for the current interactive tx construction, if any.
66386640
///
@@ -6654,19 +6656,16 @@ where
66546656
}
66556657

66566658
macro_rules! promote_splice_funding {
6657-
($self: expr, $funding: expr) => {{
6659+
($self: expr, $pending_splice: expr, $funding: expr) => {{
66586660
let prev_funding_txid = $self.funding.get_funding_txid();
66596661
if let Some(scid) = $self.funding.short_channel_id {
66606662
$self.context.historical_scids.push(scid);
66616663
}
6664+
66626665
core::mem::swap(&mut $self.funding, $funding);
6663-
$self.interactive_tx_signing_session = None;
6664-
$self.pending_splice = None;
6665-
$self.context.announcement_sigs = None;
6666-
$self.context.announcement_sigs_state = AnnouncementSigsState::NotSent;
66676666

66686667
// The swap above places the previous `FundingScope` into `pending_funding`.
6669-
let discarded_funding = $self
6668+
let discarded_funding = $pending_splice
66706669
.pending_funding
66716670
.drain(..)
66726671
.filter(|funding| funding.get_funding_txid() != prev_funding_txid)
@@ -6682,6 +6681,12 @@ macro_rules! promote_splice_funding {
66826681
})
66836682
})
66846683
.collect::<Vec<_>>();
6684+
6685+
$self.interactive_tx_signing_session = None;
6686+
$self.pending_splice = None;
6687+
$self.context.announcement_sigs = None;
6688+
$self.context.announcement_sigs_state = AnnouncementSigsState::NotSent;
6689+
66856690
discarded_funding
66866691
}};
66876692
}
@@ -6796,6 +6801,24 @@ where
67966801
})
67976802
}
67986803

6804+
fn pending_funding(&self) -> &[FundingScope] {
6805+
if let Some(pending_splice) = &self.pending_splice {
6806+
pending_splice.pending_funding.as_slice()
6807+
} else {
6808+
&[]
6809+
}
6810+
}
6811+
6812+
fn funding_and_pending_funding_iter_mut(&mut self) -> impl Iterator<Item = &mut FundingScope> {
6813+
core::iter::once(&mut self.funding).chain(
6814+
self.pending_splice
6815+
.as_mut()
6816+
.map(|pending_splice| pending_splice.pending_funding.as_mut_slice())
6817+
.unwrap_or(&mut [])
6818+
.iter_mut(),
6819+
)
6820+
}
6821+
67996822
#[rustfmt::skip]
68006823
fn check_remote_fee<F: Deref, L: Deref>(
68016824
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -7425,7 +7448,7 @@ where
74257448
}
74267449

74277450
core::iter::once(&self.funding)
7428-
.chain(self.pending_funding.iter())
7451+
.chain(self.pending_funding().iter())
74297452
.try_for_each(|funding| self.context.validate_update_add_htlc(funding, msg, fee_estimator))?;
74307453

74317454
// Now update local state:
@@ -7680,7 +7703,7 @@ where
76807703
{
76817704
self.commitment_signed_check_state()?;
76827705

7683-
if !self.pending_funding.is_empty() {
7706+
if !self.pending_funding().is_empty() {
76847707
return Err(ChannelError::close(
76857708
"Got a single commitment_signed message when expecting a batch".to_owned(),
76867709
));
@@ -7747,9 +7770,9 @@ where
77477770

77487771
// Any commitment_signed not associated with a FundingScope is ignored below if a
77497772
// pending splice transaction has confirmed since receiving the batch.
7750-
let mut commitment_txs = Vec::with_capacity(self.pending_funding.len() + 1);
7773+
let mut commitment_txs = Vec::with_capacity(self.pending_funding().len() + 1);
77517774
let mut htlc_data = None;
7752-
for funding in core::iter::once(&self.funding).chain(self.pending_funding.iter()) {
7775+
for funding in core::iter::once(&self.funding).chain(self.pending_funding().iter()) {
77537776
let funding_txid =
77547777
funding.get_funding_txid().expect("Funding txid must be known for pending scope");
77557778
let msg = messages.get(&funding_txid).ok_or_else(|| {
@@ -8419,7 +8442,7 @@ where
84198442
}
84208443
}
84218444

8422-
for funding in core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut()) {
8445+
for funding in self.funding_and_pending_funding_iter_mut() {
84238446
funding.value_to_self_msat =
84248447
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
84258448
}
@@ -8749,7 +8772,7 @@ where
87498772
debug_assert!(!self.funding.get_channel_type().supports_anchor_zero_fee_commitments());
87508773

87518774
let can_send_update_fee = core::iter::once(&self.funding)
8752-
.chain(self.pending_funding.iter())
8775+
.chain(self.pending_funding().iter())
87538776
.all(|funding| self.context.can_send_update_fee(funding, feerate_per_kw, fee_estimator, logger));
87548777
if !can_send_update_fee {
87558778
return None;
@@ -9052,14 +9075,14 @@ where
90529075
}
90539076

90549077
core::iter::once(&self.funding)
9055-
.chain(self.pending_funding.iter())
9078+
.chain(self.pending_funding().iter())
90569079
.try_for_each(|funding| FundedChannel::<SP>::check_remote_fee(funding.get_channel_type(), fee_estimator, msg.feerate_per_kw, Some(self.context.feerate_per_kw), logger))?;
90579080

90589081
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
90599082
self.context.update_time_counter += 1;
90609083

90619084
core::iter::once(&self.funding)
9062-
.chain(self.pending_funding.iter())
9085+
.chain(self.pending_funding().iter())
90639086
.try_for_each(|funding| self.context.validate_update_fee(funding, fee_estimator, msg))
90649087
}
90659088

@@ -9584,7 +9607,7 @@ where
95849607
// - MUST process `my_current_funding_locked` as if it was receiving `splice_locked`
95859608
// for this `txid`.
95869609
let inferred_splice_locked = msg.my_current_funding_locked.as_ref().and_then(|funding_locked| {
9587-
self.pending_funding
9610+
self.pending_funding()
95889611
.iter()
95899612
.find(|funding| funding.get_funding_txid() == Some(funding_locked.txid))
95909613
.and_then(|_| {
@@ -10358,7 +10381,7 @@ where
1035810381
);
1035910382

1036010383
core::iter::once(&self.funding)
10361-
.chain(self.pending_funding.iter())
10384+
.chain(self.pending_funding().iter())
1036210385
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, dust_exposure_limiting_feerate, &logger))
1036310386
}
1036410387

@@ -10692,12 +10715,12 @@ where
1069210715
let discarded_funding = {
1069310716
// Scope `funding` since it is swapped within `promote_splice_funding` and we don't want
1069410717
// to unintentionally use it.
10695-
let funding = self
10718+
let funding = pending_splice
1069610719
.pending_funding
1069710720
.iter_mut()
1069810721
.find(|funding| funding.get_funding_txid() == Some(splice_txid))
1069910722
.unwrap();
10700-
promote_splice_funding!(self, funding)
10723+
promote_splice_funding!(self, pending_splice, funding)
1070110724
};
1070210725

1070310726
let funding_txo = self
@@ -10767,18 +10790,20 @@ where
1076710790

1076810791
let mut confirmed_funding_index = None;
1076910792
let mut funding_already_confirmed = false;
10770-
for (index, funding) in self.pending_funding.iter_mut().enumerate() {
10771-
if self.context.check_for_funding_tx_confirmed(
10772-
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
10773-
)? {
10774-
if funding_already_confirmed || confirmed_funding_index.is_some() {
10775-
let err_reason = "splice tx of another pending funding already confirmed";
10776-
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
10777-
}
10793+
if let Some(pending_splice) = &mut self.pending_splice {
10794+
for (index, funding) in pending_splice.pending_funding.iter_mut().enumerate() {
10795+
if self.context.check_for_funding_tx_confirmed(
10796+
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
10797+
)? {
10798+
if funding_already_confirmed || confirmed_funding_index.is_some() {
10799+
let err_reason = "splice tx of another pending funding already confirmed";
10800+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
10801+
}
1077810802

10779-
confirmed_funding_index = Some(index);
10780-
} else if funding.funding_tx_confirmation_height != 0 {
10781-
funding_already_confirmed = true;
10803+
confirmed_funding_index = Some(index);
10804+
} else if funding.funding_tx_confirmation_height != 0 {
10805+
funding_already_confirmed = true;
10806+
}
1078210807
}
1078310808
}
1078410809

@@ -10792,9 +10817,8 @@ where
1079210817
return Err(ClosureReason::ProcessingError { err });
1079310818
},
1079410819
};
10795-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
1079610820

10797-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
10821+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, confirmed_funding_index, height) {
1079810822
log_info!(
1079910823
logger,
1080010824
"Sending splice_locked txid {} to our peer for channel {}",
@@ -10918,7 +10942,7 @@ where
1091810942
}
1091910943

1092010944
let mut confirmed_funding_index = None;
10921-
for (index, funding) in self.pending_funding.iter().enumerate() {
10945+
for (index, funding) in self.pending_funding().iter().enumerate() {
1092210946
if funding.funding_tx_confirmation_height != 0 {
1092310947
if confirmed_funding_index.is_some() {
1092410948
let err_reason = "splice tx of another pending funding already confirmed";
@@ -10939,7 +10963,7 @@ where
1093910963
return Err(ClosureReason::ProcessingError { err });
1094010964
},
1094110965
};
10942-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
10966+
let funding = &mut pending_splice.pending_funding[confirmed_funding_index];
1094310967

1094410968
// Check if the splice funding transaction was unconfirmed
1094510969
if funding.get_funding_tx_confirmations(height) == 0 {
@@ -10958,8 +10982,11 @@ where
1095810982
}
1095910983

1096010984
let pending_splice = self.pending_splice.as_mut().unwrap();
10961-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
10962-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
10985+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
10986+
&self.context,
10987+
confirmed_funding_index,
10988+
height,
10989+
) {
1096310990
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
1096410991
debug_assert!(chain_node_signer.is_some());
1096510992

@@ -10989,7 +11016,7 @@ where
1098911016

1099011017
pub fn get_relevant_txids(&self) -> impl Iterator<Item = (Txid, u32, Option<BlockHash>)> + '_ {
1099111018
core::iter::once(&self.funding)
10992-
.chain(self.pending_funding.iter())
11019+
.chain(self.pending_funding().iter())
1099311020
.map(|funding| {
1099411021
(
1099511022
funding.get_funding_txid(),
@@ -11019,8 +11046,8 @@ where
1101911046
where
1102011047
L::Target: Logger,
1102111048
{
11022-
let unconfirmed_funding = core::iter::once(&mut self.funding)
11023-
.chain(self.pending_funding.iter_mut())
11049+
let unconfirmed_funding = self
11050+
.funding_and_pending_funding_iter_mut()
1102411051
.find(|funding| funding.get_funding_txid() == Some(*txid));
1102511052

1102611053
if let Some(funding) = unconfirmed_funding {
@@ -11515,6 +11542,7 @@ where
1151511542

1151611543
self.pending_splice = Some(PendingSplice {
1151711544
funding_negotiation: Some(FundingNegotiation::AwaitingAck(funding_negotiation_context)),
11545+
pending_funding: vec![],
1151811546
sent_funding_txid: None,
1151911547
received_funding_txid: None,
1152011548
});
@@ -11737,6 +11765,7 @@ where
1173711765
splice_funding,
1173811766
interactive_tx_constructor,
1173911767
)),
11768+
pending_funding: Vec::new(),
1174011769
received_funding_txid: None,
1174111770
sent_funding_txid: None,
1174211771
});
@@ -11924,7 +11953,7 @@ where
1192411953
},
1192511954
};
1192611955

11927-
if !self
11956+
if !pending_splice
1192811957
.pending_funding
1192911958
.iter()
1193011959
.any(|funding| funding.get_funding_txid() == Some(msg.splice_txid))
@@ -12121,7 +12150,7 @@ where
1212112150
F::Target: FeeEstimator,
1212212151
{
1212312152
core::iter::once(&self.funding)
12124-
.chain(self.pending_funding.iter())
12153+
.chain(self.pending_funding().iter())
1212512154
.map(|funding| self.context.get_available_balances_for_scope(funding, fee_estimator))
1212612155
.reduce(|acc, e| {
1212712156
AvailableBalances {
@@ -12168,7 +12197,7 @@ where
1216812197
}
1216912198
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
1217012199

12171-
let update = if self.pending_funding.is_empty() {
12200+
let update = if self.pending_funding().is_empty() {
1217212201
let (htlcs_ref, counterparty_commitment_tx) =
1217312202
self.build_commitment_no_state_update(&self.funding, logger);
1217412203
let htlc_outputs = htlcs_ref.into_iter()
@@ -12191,7 +12220,7 @@ where
1219112220
} else {
1219212221
let mut htlc_data = None;
1219312222
let commitment_txs = core::iter::once(&self.funding)
12194-
.chain(self.pending_funding.iter())
12223+
.chain(self.pending_funding().iter())
1219512224
.map(|funding| {
1219612225
let (htlcs_ref, counterparty_commitment_tx) =
1219712226
self.build_commitment_no_state_update(funding, logger);
@@ -12264,7 +12293,7 @@ where
1226412293
L::Target: Logger,
1226512294
{
1226612295
core::iter::once(&self.funding)
12267-
.chain(self.pending_funding.iter())
12296+
.chain(self.pending_funding().iter())
1226812297
.map(|funding| self.send_commitment_no_state_update_for_funding(funding, logger))
1226912298
.collect::<Result<Vec<_>, ChannelError>>()
1227012299
}
@@ -13064,7 +13093,6 @@ where
1306413093

1306513094
let mut channel = FundedChannel {
1306613095
funding: self.funding,
13067-
pending_funding: vec![],
1306813096
context: self.context,
1306913097
interactive_tx_signing_session: None,
1307013098
holder_commitment_point,
@@ -13350,7 +13378,6 @@ where
1335013378
// `ChannelMonitor`.
1335113379
let mut channel = FundedChannel {
1335213380
funding: self.funding,
13353-
pending_funding: vec![],
1335413381
context: self.context,
1335513382
interactive_tx_signing_session: None,
1335613383
holder_commitment_point,
@@ -14926,7 +14953,6 @@ where
1492614953
short_channel_id,
1492714954
minimum_depth_override,
1492814955
},
14929-
pending_funding: Vec::new(),
1493014956
context: ChannelContext {
1493114957
user_id,
1493214958

0 commit comments

Comments
 (0)