Skip to content

Commit 6fff756

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 6fff756

File tree

1 file changed

+86
-55
lines changed

1 file changed

+86
-55
lines changed

lightning/src/ln/channel.rs

Lines changed: 86 additions & 55 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.get(confirmed_funding_index).unwrap();
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,14 @@ where
59955996
SP::Target: SignerProvider,
59965997
<SP::Target as SignerProvider>::EcdsaSigner: EcdsaChannelSigner,
59975998
{
5999+
fn pending_funding(&self) -> &[FundingScope] {
6000+
if let Some(pending_splice) = &self.pending_splice {
6001+
pending_splice.pending_funding.as_slice()
6002+
} else {
6003+
&[]
6004+
}
6005+
}
6006+
59986007
#[rustfmt::skip]
59996008
fn check_remote_fee<F: Deref, L: Deref>(
60006009
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -6559,7 +6568,7 @@ where
65596568
}
65606569

65616570
core::iter::once(&self.funding)
6562-
.chain(self.pending_funding.iter())
6571+
.chain(self.pending_funding().iter())
65636572
.try_for_each(|funding| self.context.validate_update_add_htlc(funding, msg, fee_estimator))?;
65646573

65656574
// Now update local state:
@@ -6802,7 +6811,7 @@ where
68026811
{
68036812
self.commitment_signed_check_state()?;
68046813

6805-
if !self.pending_funding.is_empty() {
6814+
if !self.pending_funding().is_empty() {
68066815
return Err(ChannelError::close(
68076816
"Got a single commitment_signed message when expecting a batch".to_owned(),
68086817
));
@@ -6861,9 +6870,9 @@ where
68616870

68626871
// Any commitment_signed not associated with a FundingScope is ignored below if a
68636872
// pending splice transaction has confirmed since receiving the batch.
6864-
let mut commitment_txs = Vec::with_capacity(self.pending_funding.len() + 1);
6873+
let mut commitment_txs = Vec::with_capacity(self.pending_funding().len() + 1);
68656874
let mut htlc_data = None;
6866-
for funding in core::iter::once(&self.funding).chain(self.pending_funding.iter()) {
6875+
for funding in core::iter::once(&self.funding).chain(self.pending_funding().iter()) {
68676876
let funding_txid =
68686877
funding.get_funding_txid().expect("Funding txid must be known for pending scope");
68696878
let msg = messages.get(&funding_txid).ok_or_else(|| {
@@ -7265,11 +7274,13 @@ where
72657274

72667275
#[cfg(any(test, fuzzing))]
72677276
{
7268-
for funding in
7269-
core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut())
7270-
{
7271-
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7272-
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7277+
*self.funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7278+
*self.funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7279+
if let Some(pending_splice) = self.pending_splice.as_mut() {
7280+
for funding in pending_splice.pending_funding.iter_mut() {
7281+
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7282+
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7283+
}
72737284
}
72747285
}
72757286

@@ -7470,9 +7481,13 @@ where
74707481
}
74717482
}
74727483

7473-
for funding in core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut()) {
7474-
funding.value_to_self_msat =
7475-
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7484+
self.funding.value_to_self_msat =
7485+
(self.funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7486+
if let Some(pending_splice) = self.pending_splice.as_mut() {
7487+
for funding in pending_splice.pending_funding.iter_mut() {
7488+
funding.value_to_self_msat =
7489+
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7490+
}
74767491
}
74777492

74787493
if let Some((feerate, update_state)) = self.context.pending_update_fee {
@@ -7735,7 +7750,7 @@ where
77357750
}
77367751

77377752
let can_send_update_fee = core::iter::once(&self.funding)
7738-
.chain(self.pending_funding.iter())
7753+
.chain(self.pending_funding().iter())
77397754
.all(|funding| self.context.can_send_update_fee(funding, feerate_per_kw, fee_estimator, logger));
77407755
if !can_send_update_fee {
77417756
return None;
@@ -8024,14 +8039,14 @@ where
80248039
}
80258040

80268041
core::iter::once(&self.funding)
8027-
.chain(self.pending_funding.iter())
8042+
.chain(self.pending_funding().iter())
80288043
.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))?;
80298044

80308045
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
80318046
self.context.update_time_counter += 1;
80328047

80338048
core::iter::once(&self.funding)
8034-
.chain(self.pending_funding.iter())
8049+
.chain(self.pending_funding().iter())
80358050
.try_for_each(|funding| self.context.validate_update_fee(funding, fee_estimator, msg))
80368051
}
80378052

@@ -9224,7 +9239,7 @@ where
92249239
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
92259240

92269241
core::iter::once(&self.funding)
9227-
.chain(self.pending_funding.iter())
9242+
.chain(self.pending_funding().iter())
92289243
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
92299244
}
92309245

@@ -9513,9 +9528,11 @@ where
95139528
L::Target: Logger,
95149529
{
95159530
debug_assert!(self.pending_splice.is_some());
9516-
debug_assert!(confirmed_funding_index < self.pending_funding.len());
95179531

95189532
let pending_splice = self.pending_splice.as_mut().unwrap();
9533+
9534+
debug_assert!(confirmed_funding_index < pending_splice.pending_funding.len());
9535+
95199536
let splice_txid = match pending_splice.sent_funding_txid {
95209537
Some(sent_funding_txid) => sent_funding_txid,
95219538
None => {
@@ -9532,7 +9549,7 @@ where
95329549
&self.context.channel_id,
95339550
);
95349551

9535-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9552+
let funding = pending_splice.pending_funding.get_mut(confirmed_funding_index).unwrap();
95369553
debug_assert_eq!(Some(splice_txid), funding.get_funding_txid());
95379554
promote_splice_funding!(self, funding);
95389555

@@ -9606,18 +9623,20 @@ where
96069623
#[cfg(splicing)]
96079624
let mut funding_already_confirmed = false;
96089625
#[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-
}
9626+
if let Some(pending_splice) = &mut self.pending_splice {
9627+
for (index, funding) in pending_splice.pending_funding.iter_mut().enumerate() {
9628+
if self.context.check_for_funding_tx_confirmed(
9629+
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
9630+
)? {
9631+
if funding_already_confirmed || confirmed_funding_index.is_some() {
9632+
let err_reason = "splice tx of another pending funding already confirmed";
9633+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9634+
}
96179635

9618-
confirmed_funding_index = Some(index);
9619-
} else if funding.funding_tx_confirmation_height != 0 {
9620-
funding_already_confirmed = true;
9636+
confirmed_funding_index = Some(index);
9637+
} else if funding.funding_tx_confirmation_height != 0 {
9638+
funding_already_confirmed = true;
9639+
}
96219640
}
96229641
}
96239642

@@ -9632,11 +9651,15 @@ where
96329651
return Err(ClosureReason::ProcessingError { err });
96339652
},
96349653
};
9635-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
96369654

9637-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
9655+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9656+
&self.context,
9657+
confirmed_funding_index,
9658+
height,
9659+
) {
96389660
for &(idx, tx) in txdata.iter() {
96399661
if idx > index_in_block {
9662+
let funding = pending_splice.pending_funding.get(confirmed_funding_index).unwrap();
96409663
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
96419664
}
96429665
}
@@ -9665,10 +9688,11 @@ where
96659688

96669689
self.context.check_for_funding_tx_spent(&self.funding, tx, logger)?;
96679690
#[cfg(splicing)]
9668-
for funding in self.pending_funding.iter() {
9669-
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9691+
if let Some(pending_splice) = self.pending_splice.as_ref() {
9692+
for funding in pending_splice.pending_funding.iter() {
9693+
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9694+
}
96709695
}
9671-
96729696
}
96739697

96749698
Ok((None, None))
@@ -9772,7 +9796,7 @@ where
97729796
#[cfg(splicing)]
97739797
let mut confirmed_funding_index = None;
97749798
#[cfg(splicing)]
9775-
for (index, funding) in self.pending_funding.iter().enumerate() {
9799+
for (index, funding) in self.pending_funding().iter().enumerate() {
97769800
if funding.funding_tx_confirmation_height != 0 {
97779801
if confirmed_funding_index.is_some() {
97789802
let err_reason = "splice tx of another pending funding already confirmed";
@@ -9794,7 +9818,7 @@ where
97949818
return Err(ClosureReason::ProcessingError { err });
97959819
},
97969820
};
9797-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9821+
let funding = pending_splice.pending_funding.get_mut(confirmed_funding_index).unwrap();
97989822

97999823
// Check if the splice funding transaction was unconfirmed
98009824
if funding.get_funding_tx_confirmations(height) == 0 {
@@ -9813,8 +9837,11 @@ where
98139837
}
98149838

98159839
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) {
9840+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9841+
&self.context,
9842+
confirmed_funding_index,
9843+
height,
9844+
) {
98189845
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
98199846

98209847
let funding_promoted =
@@ -9844,7 +9871,7 @@ where
98449871

98459872
pub fn get_relevant_txids(&self) -> impl Iterator<Item = (Txid, u32, Option<BlockHash>)> + '_ {
98469873
core::iter::once(&self.funding)
9847-
.chain(self.pending_funding.iter())
9874+
.chain(self.pending_funding().iter())
98489875
.map(|funding| {
98499876
(
98509877
funding.get_funding_txid(),
@@ -9874,9 +9901,16 @@ where
98749901
where
98759902
L::Target: Logger,
98769903
{
9877-
let unconfirmed_funding = core::iter::once(&mut self.funding)
9878-
.chain(self.pending_funding.iter_mut())
9879-
.find(|funding| funding.get_funding_txid() == Some(*txid));
9904+
let unconfirmed_funding = (self.funding.get_funding_txid() == Some(*txid))
9905+
.then(|| &mut self.funding)
9906+
.or_else(|| {
9907+
self.pending_splice.as_mut().and_then(|pending_splice| {
9908+
pending_splice
9909+
.pending_funding
9910+
.iter_mut()
9911+
.find(|funding| funding.get_funding_txid() == Some(*txid))
9912+
})
9913+
});
98809914

98819915
if let Some(funding) = unconfirmed_funding {
98829916
if funding.funding_tx_confirmation_height != 0 {
@@ -10232,6 +10266,7 @@ where
1023210266
self.pending_splice = Some(PendingSplice {
1023310267
our_funding_contribution: our_funding_contribution_satoshis,
1023410268
funding: None,
10269+
pending_funding: vec![],
1023510270
sent_funding_txid: None,
1023610271
received_funding_txid: None,
1023710272
});
@@ -10348,7 +10383,7 @@ where
1034810383

1034910384
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
1035010385
if sent_funding_txid == msg.splice_txid {
10351-
if let Some(funding) = self
10386+
if let Some(funding) = pending_splice
1035210387
.pending_funding
1035310388
.iter_mut()
1035410389
.find(|funding| funding.get_funding_txid() == Some(sent_funding_txid))
@@ -10547,7 +10582,7 @@ where
1054710582
F::Target: FeeEstimator,
1054810583
{
1054910584
core::iter::once(&self.funding)
10550-
.chain(self.pending_funding.iter())
10585+
.chain(self.pending_funding().iter())
1055110586
.map(|funding| self.context.get_available_balances_for_scope(funding, fee_estimator))
1055210587
.reduce(|acc, e| {
1055310588
AvailableBalances {
@@ -10594,7 +10629,7 @@ where
1059410629
}
1059510630
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
1059610631

10597-
let update = if self.pending_funding.is_empty() {
10632+
let update = if self.pending_funding().is_empty() {
1059810633
let (htlcs_ref, counterparty_commitment_tx) =
1059910634
self.build_commitment_no_state_update(&self.funding, logger);
1060010635
let htlc_outputs = htlcs_ref.into_iter()
@@ -10617,7 +10652,7 @@ where
1061710652
} else {
1061810653
let mut htlc_data = None;
1061910654
let commitment_txs = core::iter::once(&self.funding)
10620-
.chain(self.pending_funding.iter())
10655+
.chain(self.pending_funding().iter())
1062110656
.map(|funding| {
1062210657
let (htlcs_ref, counterparty_commitment_tx) =
1062310658
self.build_commitment_no_state_update(funding, logger);
@@ -10699,7 +10734,7 @@ where
1069910734
L::Target: Logger,
1070010735
{
1070110736
core::iter::once(&self.funding)
10702-
.chain(self.pending_funding.iter())
10737+
.chain(self.pending_funding().iter())
1070310738
.map(|funding| self.send_commitment_no_state_update_for_funding(funding, logger))
1070410739
.collect::<Result<Vec<_>, ChannelError>>()
1070510740
}
@@ -11476,7 +11511,6 @@ where
1147611511

1147711512
let mut channel = FundedChannel {
1147811513
funding: self.funding,
11479-
pending_funding: vec![],
1148011514
context: self.context,
1148111515
interactive_tx_signing_session: None,
1148211516
holder_commitment_point,
@@ -11768,7 +11802,6 @@ where
1176811802
// `ChannelMonitor`.
1176911803
let mut channel = FundedChannel {
1177011804
funding: self.funding,
11771-
pending_funding: vec![],
1177211805
context: self.context,
1177311806
interactive_tx_signing_session: None,
1177411807
holder_commitment_point,
@@ -12936,7 +12969,6 @@ where
1293612969
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
1293712970
let mut is_manual_broadcast = None;
1293812971

12939-
let mut pending_funding = Some(Vec::new());
1294012972
let mut historical_scids = Some(Vec::new());
1294112973

1294212974
let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
@@ -13160,7 +13192,6 @@ where
1316013192
short_channel_id,
1316113193
minimum_depth_override,
1316213194
},
13163-
pending_funding: pending_funding.unwrap(),
1316413195
context: ChannelContext {
1316513196
user_id,
1316613197

0 commit comments

Comments
 (0)