Skip to content

Commit ed78345

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 436112c commit ed78345

File tree

1 file changed

+88
-50
lines changed

1 file changed

+88
-50
lines changed

lightning/src/ln/channel.rs

Lines changed: 88 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,6 @@ where
18071807
};
18081808
let mut funded_channel = FundedChannel {
18091809
funding: chan.funding,
1810-
pending_funding: vec![],
18111810
context: chan.context,
18121811
interactive_tx_signing_session: chan.interactive_tx_signing_session,
18131812
holder_commitment_point,
@@ -2179,6 +2178,7 @@ impl FundingScope {
21792178
struct PendingSplice {
21802179
pub our_funding_contribution: i64,
21812180
funding: Option<FundingScope>,
2181+
pending_funding: Vec<FundingScope>,
21822182

21832183
/// The funding txid used in the `splice_locked` sent to the counterparty.
21842184
sent_funding_txid: Option<Txid>,
@@ -2190,11 +2190,14 @@ struct PendingSplice {
21902190
#[cfg(splicing)]
21912191
impl PendingSplice {
21922192
fn check_get_splice_locked<SP: Deref>(
2193-
&mut self, context: &ChannelContext<SP>, funding: &FundingScope, height: u32,
2193+
&mut self, context: &ChannelContext<SP>, confirmed_funding_index: usize, height: u32,
21942194
) -> Option<msgs::SpliceLocked>
21952195
where
21962196
SP::Target: SignerProvider,
21972197
{
2198+
debug_assert!(confirmed_funding_index < self.pending_funding.len());
2199+
2200+
let funding = &self.pending_funding[confirmed_funding_index];
21982201
if !context.check_funding_meets_minimum_depth(funding, height) {
21992202
return None;
22002203
}
@@ -5885,7 +5888,6 @@ where
58855888
SP::Target: SignerProvider,
58865889
{
58875890
pub funding: FundingScope,
5888-
pending_funding: Vec<FundingScope>,
58895891
pub context: ChannelContext<SP>,
58905892
/// The signing session for the current interactive tx construction, if any.
58915893
///
@@ -5909,7 +5911,6 @@ macro_rules! promote_splice_funding {
59095911
}
59105912
core::mem::swap(&mut $self.funding, $funding);
59115913
$self.pending_splice = None;
5912-
$self.pending_funding.clear();
59135914
$self.context.announcement_sigs_state = AnnouncementSigsState::NotSent;
59145915
};
59155916
}
@@ -5995,6 +5996,36 @@ where
59955996
SP::Target: SignerProvider,
59965997
<SP::Target as SignerProvider>::EcdsaSigner: EcdsaChannelSigner,
59975998
{
5999+
#[cfg(splicing)]
6000+
fn pending_funding(&self) -> &[FundingScope] {
6001+
if let Some(pending_splice) = &self.pending_splice {
6002+
pending_splice.pending_funding.as_slice()
6003+
} else {
6004+
&[]
6005+
}
6006+
}
6007+
6008+
#[cfg(not(splicing))]
6009+
fn pending_funding(&self) -> &[FundingScope] {
6010+
&[]
6011+
}
6012+
6013+
#[cfg(splicing)]
6014+
fn funding_and_pending_funding_iter_mut(&mut self) -> impl Iterator<Item = &mut FundingScope> {
6015+
core::iter::once(&mut self.funding).chain(
6016+
self.pending_splice
6017+
.as_mut()
6018+
.map(|pending_splice| pending_splice.pending_funding.as_mut_slice())
6019+
.unwrap_or(&mut [])
6020+
.iter_mut(),
6021+
)
6022+
}
6023+
6024+
#[cfg(not(splicing))]
6025+
fn funding_and_pending_funding_iter_mut(&mut self) -> impl Iterator<Item = &mut FundingScope> {
6026+
core::iter::once(&mut self.funding)
6027+
}
6028+
59986029
#[rustfmt::skip]
59996030
fn check_remote_fee<F: Deref, L: Deref>(
60006031
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -6559,7 +6590,7 @@ where
65596590
}
65606591

65616592
core::iter::once(&self.funding)
6562-
.chain(self.pending_funding.iter())
6593+
.chain(self.pending_funding().iter())
65636594
.try_for_each(|funding| self.context.validate_update_add_htlc(funding, msg, fee_estimator))?;
65646595

65656596
// Now update local state:
@@ -6802,7 +6833,7 @@ where
68026833
{
68036834
self.commitment_signed_check_state()?;
68046835

6805-
if !self.pending_funding.is_empty() {
6836+
if !self.pending_funding().is_empty() {
68066837
return Err(ChannelError::close(
68076838
"Got a single commitment_signed message when expecting a batch".to_owned(),
68086839
));
@@ -6861,9 +6892,9 @@ where
68616892

68626893
// Any commitment_signed not associated with a FundingScope is ignored below if a
68636894
// pending splice transaction has confirmed since receiving the batch.
6864-
let mut commitment_txs = Vec::with_capacity(self.pending_funding.len() + 1);
6895+
let mut commitment_txs = Vec::with_capacity(self.pending_funding().len() + 1);
68656896
let mut htlc_data = None;
6866-
for funding in core::iter::once(&self.funding).chain(self.pending_funding.iter()) {
6897+
for funding in core::iter::once(&self.funding).chain(self.pending_funding().iter()) {
68676898
let funding_txid =
68686899
funding.get_funding_txid().expect("Funding txid must be known for pending scope");
68696900
let msg = messages.get(&funding_txid).ok_or_else(|| {
@@ -7265,9 +7296,7 @@ where
72657296

72667297
#[cfg(any(test, fuzzing))]
72677298
{
7268-
for funding in
7269-
core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut())
7270-
{
7299+
for funding in self.funding_and_pending_funding_iter_mut() {
72717300
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
72727301
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
72737302
}
@@ -7470,7 +7499,7 @@ where
74707499
}
74717500
}
74727501

7473-
for funding in core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut()) {
7502+
for funding in self.funding_and_pending_funding_iter_mut() {
74747503
funding.value_to_self_msat =
74757504
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
74767505
}
@@ -7735,7 +7764,7 @@ where
77357764
}
77367765

77377766
let can_send_update_fee = core::iter::once(&self.funding)
7738-
.chain(self.pending_funding.iter())
7767+
.chain(self.pending_funding().iter())
77397768
.all(|funding| self.context.can_send_update_fee(funding, feerate_per_kw, fee_estimator, logger));
77407769
if !can_send_update_fee {
77417770
return None;
@@ -8024,14 +8053,14 @@ where
80248053
}
80258054

80268055
core::iter::once(&self.funding)
8027-
.chain(self.pending_funding.iter())
8056+
.chain(self.pending_funding().iter())
80288057
.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))?;
80298058

80308059
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
80318060
self.context.update_time_counter += 1;
80328061

80338062
core::iter::once(&self.funding)
8034-
.chain(self.pending_funding.iter())
8063+
.chain(self.pending_funding().iter())
80358064
.try_for_each(|funding| self.context.validate_update_fee(funding, fee_estimator, msg))
80368065
}
80378066

@@ -9224,7 +9253,7 @@ where
92249253
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
92259254

92269255
core::iter::once(&self.funding)
9227-
.chain(self.pending_funding.iter())
9256+
.chain(self.pending_funding().iter())
92289257
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
92299258
}
92309259

@@ -9513,9 +9542,11 @@ where
95139542
L::Target: Logger,
95149543
{
95159544
debug_assert!(self.pending_splice.is_some());
9516-
debug_assert!(confirmed_funding_index < self.pending_funding.len());
95179545

95189546
let pending_splice = self.pending_splice.as_mut().unwrap();
9547+
9548+
debug_assert!(confirmed_funding_index < pending_splice.pending_funding.len());
9549+
95199550
let splice_txid = match pending_splice.sent_funding_txid {
95209551
Some(sent_funding_txid) => sent_funding_txid,
95219552
None => {
@@ -9532,7 +9563,7 @@ where
95329563
&self.context.channel_id,
95339564
);
95349565

9535-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9566+
let funding = &mut pending_splice.pending_funding[confirmed_funding_index];
95369567
debug_assert_eq!(Some(splice_txid), funding.get_funding_txid());
95379568
promote_splice_funding!(self, funding);
95389569

@@ -9606,18 +9637,20 @@ where
96069637
#[cfg(splicing)]
96079638
let mut funding_already_confirmed = false;
96089639
#[cfg(splicing)]
9609-
for (index, funding) in self.pending_funding.iter_mut().enumerate() {
9610-
if self.context.check_for_funding_tx_confirmed(
9611-
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
9612-
)? {
9613-
if funding_already_confirmed || confirmed_funding_index.is_some() {
9614-
let err_reason = "splice tx of another pending funding already confirmed";
9615-
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9616-
}
9640+
if let Some(pending_splice) = &mut self.pending_splice {
9641+
for (index, funding) in pending_splice.pending_funding.iter_mut().enumerate() {
9642+
if self.context.check_for_funding_tx_confirmed(
9643+
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
9644+
)? {
9645+
if funding_already_confirmed || confirmed_funding_index.is_some() {
9646+
let err_reason = "splice tx of another pending funding already confirmed";
9647+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9648+
}
96179649

9618-
confirmed_funding_index = Some(index);
9619-
} else if funding.funding_tx_confirmation_height != 0 {
9620-
funding_already_confirmed = true;
9650+
confirmed_funding_index = Some(index);
9651+
} else if funding.funding_tx_confirmation_height != 0 {
9652+
funding_already_confirmed = true;
9653+
}
96219654
}
96229655
}
96239656

@@ -9632,11 +9665,15 @@ where
96329665
return Err(ClosureReason::ProcessingError { err });
96339666
},
96349667
};
9635-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
96369668

9637-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
9669+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9670+
&self.context,
9671+
confirmed_funding_index,
9672+
height,
9673+
) {
96389674
for &(idx, tx) in txdata.iter() {
96399675
if idx > index_in_block {
9676+
let funding = &pending_splice.pending_funding[confirmed_funding_index];
96409677
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
96419678
}
96429679
}
@@ -9665,10 +9702,11 @@ where
96659702

96669703
self.context.check_for_funding_tx_spent(&self.funding, tx, logger)?;
96679704
#[cfg(splicing)]
9668-
for funding in self.pending_funding.iter() {
9669-
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9705+
if let Some(pending_splice) = self.pending_splice.as_ref() {
9706+
for funding in pending_splice.pending_funding.iter() {
9707+
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9708+
}
96709709
}
9671-
96729710
}
96739711

96749712
Ok((None, None))
@@ -9772,7 +9810,7 @@ where
97729810
#[cfg(splicing)]
97739811
let mut confirmed_funding_index = None;
97749812
#[cfg(splicing)]
9775-
for (index, funding) in self.pending_funding.iter().enumerate() {
9813+
for (index, funding) in self.pending_funding().iter().enumerate() {
97769814
if funding.funding_tx_confirmation_height != 0 {
97779815
if confirmed_funding_index.is_some() {
97789816
let err_reason = "splice tx of another pending funding already confirmed";
@@ -9794,7 +9832,7 @@ where
97949832
return Err(ClosureReason::ProcessingError { err });
97959833
},
97969834
};
9797-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9835+
let funding = &mut pending_splice.pending_funding[confirmed_funding_index];
97989836

97999837
// Check if the splice funding transaction was unconfirmed
98009838
if funding.get_funding_tx_confirmations(height) == 0 {
@@ -9813,8 +9851,11 @@ where
98139851
}
98149852

98159853
let pending_splice = self.pending_splice.as_mut().unwrap();
9816-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
9817-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
9854+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9855+
&self.context,
9856+
confirmed_funding_index,
9857+
height,
9858+
) {
98189859
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
98199860

98209861
let funding_promoted =
@@ -9844,7 +9885,7 @@ where
98449885

98459886
pub fn get_relevant_txids(&self) -> impl Iterator<Item = (Txid, u32, Option<BlockHash>)> + '_ {
98469887
core::iter::once(&self.funding)
9847-
.chain(self.pending_funding.iter())
9888+
.chain(self.pending_funding().iter())
98489889
.map(|funding| {
98499890
(
98509891
funding.get_funding_txid(),
@@ -9874,8 +9915,8 @@ where
98749915
where
98759916
L::Target: Logger,
98769917
{
9877-
let unconfirmed_funding = core::iter::once(&mut self.funding)
9878-
.chain(self.pending_funding.iter_mut())
9918+
let unconfirmed_funding = self
9919+
.funding_and_pending_funding_iter_mut()
98799920
.find(|funding| funding.get_funding_txid() == Some(*txid));
98809921

98819922
if let Some(funding) = unconfirmed_funding {
@@ -10232,6 +10273,7 @@ where
1023210273
self.pending_splice = Some(PendingSplice {
1023310274
our_funding_contribution: our_funding_contribution_satoshis,
1023410275
funding: None,
10276+
pending_funding: vec![],
1023510277
sent_funding_txid: None,
1023610278
received_funding_txid: None,
1023710279
});
@@ -10348,7 +10390,7 @@ where
1034810390

1034910391
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
1035010392
if sent_funding_txid == msg.splice_txid {
10351-
if let Some(funding) = self
10393+
if let Some(funding) = pending_splice
1035210394
.pending_funding
1035310395
.iter_mut()
1035410396
.find(|funding| funding.get_funding_txid() == Some(sent_funding_txid))
@@ -10547,7 +10589,7 @@ where
1054710589
F::Target: FeeEstimator,
1054810590
{
1054910591
core::iter::once(&self.funding)
10550-
.chain(self.pending_funding.iter())
10592+
.chain(self.pending_funding().iter())
1055110593
.map(|funding| self.context.get_available_balances_for_scope(funding, fee_estimator))
1055210594
.reduce(|acc, e| {
1055310595
AvailableBalances {
@@ -10594,7 +10636,7 @@ where
1059410636
}
1059510637
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
1059610638

10597-
let update = if self.pending_funding.is_empty() {
10639+
let update = if self.pending_funding().is_empty() {
1059810640
let (htlcs_ref, counterparty_commitment_tx) =
1059910641
self.build_commitment_no_state_update(&self.funding, logger);
1060010642
let htlc_outputs = htlcs_ref.into_iter()
@@ -10617,7 +10659,7 @@ where
1061710659
} else {
1061810660
let mut htlc_data = None;
1061910661
let commitment_txs = core::iter::once(&self.funding)
10620-
.chain(self.pending_funding.iter())
10662+
.chain(self.pending_funding().iter())
1062110663
.map(|funding| {
1062210664
let (htlcs_ref, counterparty_commitment_tx) =
1062310665
self.build_commitment_no_state_update(funding, logger);
@@ -10699,7 +10741,7 @@ where
1069910741
L::Target: Logger,
1070010742
{
1070110743
core::iter::once(&self.funding)
10702-
.chain(self.pending_funding.iter())
10744+
.chain(self.pending_funding().iter())
1070310745
.map(|funding| self.send_commitment_no_state_update_for_funding(funding, logger))
1070410746
.collect::<Result<Vec<_>, ChannelError>>()
1070510747
}
@@ -11476,7 +11518,6 @@ where
1147611518

1147711519
let mut channel = FundedChannel {
1147811520
funding: self.funding,
11479-
pending_funding: vec![],
1148011521
context: self.context,
1148111522
interactive_tx_signing_session: None,
1148211523
holder_commitment_point,
@@ -11768,7 +11809,6 @@ where
1176811809
// `ChannelMonitor`.
1176911810
let mut channel = FundedChannel {
1177011811
funding: self.funding,
11771-
pending_funding: vec![],
1177211812
context: self.context,
1177311813
interactive_tx_signing_session: None,
1177411814
holder_commitment_point,
@@ -12936,7 +12976,6 @@ where
1293612976
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
1293712977
let mut is_manual_broadcast = None;
1293812978

12939-
let mut pending_funding = Some(Vec::new());
1294012979
let mut historical_scids = Some(Vec::new());
1294112980

1294212981
let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
@@ -13160,7 +13199,6 @@ where
1316013199
short_channel_id,
1316113200
minimum_depth_override,
1316213201
},
13163-
pending_funding: pending_funding.unwrap(),
1316413202
context: ChannelContext {
1316513203
user_id,
1316613204

0 commit comments

Comments
 (0)