Skip to content

Commit d243af1

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 294048f commit d243af1

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
@@ -1804,7 +1804,6 @@ where
18041804
};
18051805
let mut funded_channel = FundedChannel {
18061806
funding: chan.funding,
1807-
pending_funding: vec![],
18081807
context: chan.context,
18091808
interactive_tx_signing_session: chan.interactive_tx_signing_session,
18101809
holder_commitment_point,
@@ -2176,6 +2175,7 @@ impl FundingScope {
21762175
struct PendingSplice {
21772176
pub our_funding_contribution: i64,
21782177
funding: Option<FundingScope>,
2178+
pending_funding: Vec<FundingScope>,
21792179

21802180
/// The funding txid used in the `splice_locked` sent to the counterparty.
21812181
sent_funding_txid: Option<Txid>,
@@ -2187,11 +2187,14 @@ struct PendingSplice {
21872187
#[cfg(splicing)]
21882188
impl PendingSplice {
21892189
fn check_get_splice_locked<SP: Deref>(
2190-
&mut self, context: &ChannelContext<SP>, funding: &FundingScope, height: u32,
2190+
&mut self, context: &ChannelContext<SP>, confirmed_funding_index: usize, height: u32,
21912191
) -> Option<msgs::SpliceLocked>
21922192
where
21932193
SP::Target: SignerProvider,
21942194
{
2195+
debug_assert!(confirmed_funding_index < self.pending_funding.len());
2196+
2197+
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
21952198
if !context.check_funding_meets_minimum_depth(funding, height) {
21962199
return None;
21972200
}
@@ -5931,7 +5934,6 @@ where
59315934
SP::Target: SignerProvider,
59325935
{
59335936
pub funding: FundingScope,
5934-
pending_funding: Vec<FundingScope>,
59355937
pub context: ChannelContext<SP>,
59365938
/// The signing session for the current interactive tx construction, if any.
59375939
///
@@ -5955,7 +5957,6 @@ macro_rules! promote_splice_funding {
59555957
}
59565958
core::mem::swap(&mut $self.funding, $funding);
59575959
$self.pending_splice = None;
5958-
$self.pending_funding.clear();
59595960
$self.context.announcement_sigs_state = AnnouncementSigsState::NotSent;
59605961
};
59615962
}
@@ -6049,6 +6050,14 @@ where
60496050
SP::Target: SignerProvider,
60506051
<SP::Target as SignerProvider>::EcdsaSigner: EcdsaChannelSigner,
60516052
{
6053+
fn pending_funding(&self) -> &[FundingScope] {
6054+
if let Some(pending_splice) = &self.pending_splice {
6055+
pending_splice.pending_funding.as_slice()
6056+
} else {
6057+
&[]
6058+
}
6059+
}
6060+
60526061
#[rustfmt::skip]
60536062
fn check_remote_fee<F: Deref, L: Deref>(
60546063
channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -6613,7 +6622,7 @@ where
66136622
}
66146623

66156624
core::iter::once(&self.funding)
6616-
.chain(self.pending_funding.iter())
6625+
.chain(self.pending_funding().iter())
66176626
.try_for_each(|funding| self.context.validate_update_add_htlc(funding, msg, fee_estimator))?;
66186627

66196628
// Now update local state:
@@ -6839,7 +6848,7 @@ where
68396848
{
68406849
self.commitment_signed_check_state()?;
68416850

6842-
if !self.pending_funding.is_empty() {
6851+
if !self.pending_funding().is_empty() {
68436852
return Err(ChannelError::close(
68446853
"Got a single commitment_signed message when expecting a batch".to_owned(),
68456854
));
@@ -6901,7 +6910,7 @@ where
69016910
// Any commitment_signed not associated with a FundingScope is ignored below if a
69026911
// pending splice transaction has confirmed since receiving the batch.
69036912
let updates = core::iter::once(&self.funding)
6904-
.chain(self.pending_funding.iter())
6913+
.chain(self.pending_funding().iter())
69056914
.map(|funding| {
69066915
let funding_txid = funding.get_funding_txo().unwrap().txid;
69076916
let msg = messages.get(&funding_txid).ok_or_else(|| {
@@ -7293,11 +7302,13 @@ where
72937302

72947303
#[cfg(any(test, fuzzing))]
72957304
{
7296-
for funding in
7297-
core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut())
7298-
{
7299-
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7300-
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7305+
*self.funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7306+
*self.funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7307+
if let Some(pending_splice) = self.pending_splice.as_mut() {
7308+
for funding in pending_splice.pending_funding.iter_mut() {
7309+
*funding.next_local_commitment_tx_fee_info_cached.lock().unwrap() = None;
7310+
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
7311+
}
73017312
}
73027313
}
73037314

@@ -7498,9 +7509,13 @@ where
74987509
}
74997510
}
75007511

7501-
for funding in core::iter::once(&mut self.funding).chain(self.pending_funding.iter_mut()) {
7502-
funding.value_to_self_msat =
7503-
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7512+
self.funding.value_to_self_msat =
7513+
(self.funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7514+
if let Some(pending_splice) = self.pending_splice.as_mut() {
7515+
for funding in pending_splice.pending_funding.iter_mut() {
7516+
funding.value_to_self_msat =
7517+
(funding.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
7518+
}
75047519
}
75057520

75067521
if let Some((feerate, update_state)) = self.context.pending_update_fee {
@@ -7763,7 +7778,7 @@ where
77637778
}
77647779

77657780
let can_send_update_fee = core::iter::once(&self.funding)
7766-
.chain(self.pending_funding.iter())
7781+
.chain(self.pending_funding().iter())
77677782
.all(|funding| self.context.can_send_update_fee(funding, &self.holder_commitment_point, feerate_per_kw, fee_estimator, logger));
77687783
if !can_send_update_fee {
77697784
return None;
@@ -8052,14 +8067,14 @@ where
80528067
}
80538068

80548069
core::iter::once(&self.funding)
8055-
.chain(self.pending_funding.iter())
8070+
.chain(self.pending_funding().iter())
80568071
.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))?;
80578072

80588073
self.context.pending_update_fee = Some((msg.feerate_per_kw, FeeUpdateState::RemoteAnnounced));
80598074
self.context.update_time_counter += 1;
80608075

80618076
core::iter::once(&self.funding)
8062-
.chain(self.pending_funding.iter())
8077+
.chain(self.pending_funding().iter())
80638078
.try_for_each(|funding| self.context.validate_update_fee(funding, fee_estimator, msg))
80648079
}
80658080

@@ -9252,7 +9267,7 @@ where
92529267
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
92539268

92549269
core::iter::once(&self.funding)
9255-
.chain(self.pending_funding.iter())
9270+
.chain(self.pending_funding().iter())
92569271
.try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, msg, dust_exposure_limiting_feerate, &logger))
92579272
}
92589273

@@ -9535,9 +9550,11 @@ where
95359550
L::Target: Logger,
95369551
{
95379552
debug_assert!(self.pending_splice.is_some());
9538-
debug_assert!(confirmed_funding_index < self.pending_funding.len());
95399553

95409554
let pending_splice = self.pending_splice.as_mut().unwrap();
9555+
9556+
debug_assert!(confirmed_funding_index < pending_splice.pending_funding.len());
9557+
95419558
let splice_txid = match pending_splice.sent_funding_txid {
95429559
Some(sent_funding_txid) => sent_funding_txid,
95439560
None => {
@@ -9554,7 +9571,7 @@ where
95549571
&self.context.channel_id,
95559572
);
95569573

9557-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9574+
let funding = pending_splice.pending_funding.get_mut(confirmed_funding_index).unwrap();
95589575
debug_assert_eq!(Some(splice_txid), funding.get_funding_txid());
95599576
promote_splice_funding!(self, funding);
95609577

@@ -9628,18 +9645,20 @@ where
96289645
#[cfg(splicing)]
96299646
let mut funding_already_confirmed = false;
96309647
#[cfg(splicing)]
9631-
for (index, funding) in self.pending_funding.iter_mut().enumerate() {
9632-
if self.context.check_for_funding_tx_confirmed(
9633-
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
9634-
)? {
9635-
if funding_already_confirmed || confirmed_funding_index.is_some() {
9636-
let err_reason = "splice tx of another pending funding already confirmed";
9637-
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9638-
}
9648+
if let Some(pending_splice) = &mut self.pending_splice {
9649+
for (index, funding) in pending_splice.pending_funding.iter_mut().enumerate() {
9650+
if self.context.check_for_funding_tx_confirmed(
9651+
funding, block_hash, height, index_in_block, &mut confirmed_tx, logger,
9652+
)? {
9653+
if funding_already_confirmed || confirmed_funding_index.is_some() {
9654+
let err_reason = "splice tx of another pending funding already confirmed";
9655+
return Err(ClosureReason::ProcessingError { err: err_reason.to_owned() });
9656+
}
96399657

9640-
confirmed_funding_index = Some(index);
9641-
} else if funding.funding_tx_confirmation_height != 0 {
9642-
funding_already_confirmed = true;
9658+
confirmed_funding_index = Some(index);
9659+
} else if funding.funding_tx_confirmation_height != 0 {
9660+
funding_already_confirmed = true;
9661+
}
96439662
}
96449663
}
96459664

@@ -9654,11 +9673,15 @@ where
96549673
return Err(ClosureReason::ProcessingError { err });
96559674
},
96569675
};
9657-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
96589676

9659-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
9677+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9678+
&self.context,
9679+
confirmed_funding_index,
9680+
height,
9681+
) {
96609682
for &(idx, tx) in txdata.iter() {
96619683
if idx > index_in_block {
9684+
let funding = pending_splice.pending_funding.get(confirmed_funding_index).unwrap();
96629685
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
96639686
}
96649687
}
@@ -9687,10 +9710,11 @@ where
96879710

96889711
self.context.check_for_funding_tx_spent(&self.funding, tx, logger)?;
96899712
#[cfg(splicing)]
9690-
for funding in self.pending_funding.iter() {
9691-
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9713+
if let Some(pending_splice) = self.pending_splice.as_ref() {
9714+
for funding in pending_splice.pending_funding.iter() {
9715+
self.context.check_for_funding_tx_spent(funding, tx, logger)?;
9716+
}
96929717
}
9693-
96949718
}
96959719

96969720
Ok((None, None))
@@ -9794,7 +9818,7 @@ where
97949818
#[cfg(splicing)]
97959819
let mut confirmed_funding_index = None;
97969820
#[cfg(splicing)]
9797-
for (index, funding) in self.pending_funding.iter().enumerate() {
9821+
for (index, funding) in self.pending_funding().iter().enumerate() {
97989822
if funding.funding_tx_confirmation_height != 0 {
97999823
if confirmed_funding_index.is_some() {
98009824
let err_reason = "splice tx of another pending funding already confirmed";
@@ -9816,7 +9840,7 @@ where
98169840
return Err(ClosureReason::ProcessingError { err });
98179841
},
98189842
};
9819-
let funding = self.pending_funding.get_mut(confirmed_funding_index).unwrap();
9843+
let funding = pending_splice.pending_funding.get_mut(confirmed_funding_index).unwrap();
98209844

98219845
// Check if the splice funding transaction was unconfirmed
98229846
if funding.get_funding_tx_confirmations(height) == 0 {
@@ -9835,8 +9859,11 @@ where
98359859
}
98369860

98379861
let pending_splice = self.pending_splice.as_mut().unwrap();
9838-
let funding = self.pending_funding.get(confirmed_funding_index).unwrap();
9839-
if let Some(splice_locked) = pending_splice.check_get_splice_locked(&self.context, funding, height) {
9862+
if let Some(splice_locked) = pending_splice.check_get_splice_locked(
9863+
&self.context,
9864+
confirmed_funding_index,
9865+
height,
9866+
) {
98409867
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
98419868

98429869
let funding_promoted =
@@ -9866,7 +9893,7 @@ where
98669893

98679894
pub fn get_relevant_txids(&self) -> impl Iterator<Item = (Txid, u32, Option<BlockHash>)> + '_ {
98689895
core::iter::once(&self.funding)
9869-
.chain(self.pending_funding.iter())
9896+
.chain(self.pending_funding().iter())
98709897
.map(|funding| {
98719898
(
98729899
funding.get_funding_txid(),
@@ -9896,9 +9923,16 @@ where
98969923
where
98979924
L::Target: Logger,
98989925
{
9899-
let unconfirmed_funding = core::iter::once(&mut self.funding)
9900-
.chain(self.pending_funding.iter_mut())
9901-
.find(|funding| funding.get_funding_txid() == Some(*txid));
9926+
let unconfirmed_funding = (self.funding.get_funding_txid() == Some(*txid))
9927+
.then(|| &mut self.funding)
9928+
.or_else(|| {
9929+
self.pending_splice.as_mut().and_then(|pending_splice| {
9930+
pending_splice
9931+
.pending_funding
9932+
.iter_mut()
9933+
.find(|funding| funding.get_funding_txid() == Some(*txid))
9934+
})
9935+
});
99029936

99039937
if let Some(funding) = unconfirmed_funding {
99049938
if funding.funding_tx_confirmation_height != 0 {
@@ -10254,6 +10288,7 @@ where
1025410288
self.pending_splice = Some(PendingSplice {
1025510289
our_funding_contribution: our_funding_contribution_satoshis,
1025610290
funding: None,
10291+
pending_funding: vec![],
1025710292
sent_funding_txid: None,
1025810293
received_funding_txid: None,
1025910294
});
@@ -10370,7 +10405,7 @@ where
1037010405

1037110406
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
1037210407
if sent_funding_txid == msg.splice_txid {
10373-
if let Some(funding) = self
10408+
if let Some(funding) = pending_splice
1037410409
.pending_funding
1037510410
.iter_mut()
1037610411
.find(|funding| funding.get_funding_txid() == Some(sent_funding_txid))
@@ -10569,7 +10604,7 @@ where
1056910604
F::Target: FeeEstimator,
1057010605
{
1057110606
core::iter::once(&self.funding)
10572-
.chain(self.pending_funding.iter())
10607+
.chain(self.pending_funding().iter())
1057310608
.map(|funding| self.context.get_available_balances_for_scope(funding, fee_estimator))
1057410609
.reduce(|acc, e| {
1057510610
AvailableBalances {
@@ -10616,14 +10651,14 @@ where
1061610651
}
1061710652
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
1061810653

10619-
let mut updates = Vec::with_capacity(self.pending_funding.len() + 1);
10620-
for funding in core::iter::once(&self.funding).chain(self.pending_funding.iter()) {
10654+
let mut updates = Vec::with_capacity(self.pending_funding().len() + 1);
10655+
for funding in core::iter::once(&self.funding).chain(self.pending_funding().iter()) {
1062110656
let (htlcs_ref, counterparty_commitment_tx) =
1062210657
self.build_commitment_no_state_update(funding, logger);
1062310658
let htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
1062410659
htlcs_ref.into_iter().map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
1062510660

10626-
if self.pending_funding.is_empty() {
10661+
if self.pending_funding().is_empty() {
1062710662
// Soon, we will switch this to `LatestCounterpartyCommitmentTX`,
1062810663
// and provide the full commit tx instead of the information needed to rebuild it.
1062910664
updates.push(ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
@@ -10700,7 +10735,7 @@ where
1070010735
L::Target: Logger,
1070110736
{
1070210737
core::iter::once(&self.funding)
10703-
.chain(self.pending_funding.iter())
10738+
.chain(self.pending_funding().iter())
1070410739
.map(|funding| self.send_commitment_no_state_update_for_funding(funding, logger))
1070510740
.collect::<Result<Vec<_>, ChannelError>>()
1070610741
}
@@ -11477,7 +11512,6 @@ where
1147711512

1147811513
let mut channel = FundedChannel {
1147911514
funding: self.funding,
11480-
pending_funding: vec![],
1148111515
context: self.context,
1148211516
interactive_tx_signing_session: None,
1148311517
holder_commitment_point,
@@ -11769,7 +11803,6 @@ where
1176911803
// `ChannelMonitor`.
1177011804
let mut channel = FundedChannel {
1177111805
funding: self.funding,
11772-
pending_funding: vec![],
1177311806
context: self.context,
1177411807
interactive_tx_signing_session: None,
1177511808
holder_commitment_point,
@@ -12937,7 +12970,6 @@ where
1293712970
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
1293812971
let mut is_manual_broadcast = None;
1293912972

12940-
let mut pending_funding = Some(Vec::new());
1294112973
let mut historical_scids = Some(Vec::new());
1294212974

1294312975
let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
@@ -13161,7 +13193,6 @@ where
1316113193
short_channel_id,
1316213194
minimum_depth_override,
1316313195
},
13164-
pending_funding: pending_funding.unwrap(),
1316513196
context: ChannelContext {
1316613197
user_id,
1316713198

0 commit comments

Comments
 (0)