Skip to content

Commit 0f8acd3

Browse files
committed
fix: Merge PedingSpliceInit struct into PendingSplice struct, with extra flag
1 parent c3778bc commit 0f8acd3

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

lightning/src/ln/channel.rs

+32-26
Original file line numberDiff line numberDiff line change
@@ -1897,20 +1897,19 @@ impl FundingScope {
18971897
}
18981898
}
18991899

1900-
/// Info about a pending splice_init, used in the pre-splice channel
1900+
/// Info about a pending splice
19011901
#[cfg(splicing)]
1902-
struct PendingSpliceInit {
1902+
struct PendingSplice {
1903+
/// Intended contributions to the splice from our end
19031904
pub our_funding_contribution: i64,
19041905
pub funding_feerate_per_kw: u32,
19051906
pub locktime: u32,
19061907
/// The funding inputs that we plan to contributing to the splice.
1908+
/// Stored between [`splice_channel`] and [`splice_ack`]
19071909
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
1908-
}
1909-
1910-
/// Info about a pending splice
1911-
#[cfg(splicing)]
1912-
struct PendingSplice {
1913-
pub pending_splice_init: Option<PendingSpliceInit>,
1910+
/// Set when splice_ack has been processed (on the initiator side),
1911+
/// used to prevent processing of multiple splice_ack's.
1912+
awaiting_splice_ack: bool,
19141913
pub refunding_scope: Option<RefundingScope>,
19151914
}
19161915

@@ -8975,7 +8974,7 @@ impl<SP: Deref> FundedChannel<SP> where
89758974
return Err(APIError::APIMisuseError { err: format!(
89768975
"Channel {} cannot be spliced, as it has already a splice pending (contribution {})",
89778976
self.context.channel_id(),
8978-
pending_splice.pending_splice_init.as_ref().map(|ps| ps.our_funding_contribution).unwrap_or_default(),
8977+
pending_splice.our_funding_contribution,
89798978
)});
89808979
}
89818980

@@ -9014,14 +9013,12 @@ impl<SP: Deref> FundedChannel<SP> where
90149013
funding_inputs.push((tx_in.clone(), tx16));
90159014
}
90169015

9017-
let pending_splice_init = Some(PendingSpliceInit {
9016+
self.pending_splice = Some(PendingSplice {
90189017
our_funding_contribution: our_funding_contribution_satoshis,
90199018
funding_feerate_per_kw,
90209019
locktime,
90219020
our_funding_inputs: funding_inputs,
9022-
});
9023-
self.pending_splice = Some(PendingSplice {
9024-
pending_splice_init,
9021+
awaiting_splice_ack: true, // we await splice_ack
90259022
refunding_scope: None,
90269023
});
90279024

@@ -9059,7 +9056,7 @@ impl<SP: Deref> FundedChannel<SP> where
90599056
return Err(ChannelError::Warn(format!(
90609057
"Channel {} has already a splice pending, contribution {}",
90619058
self.context.channel_id(),
9062-
pending_splice.pending_splice_init.as_ref().map(|si| si.our_funding_contribution).unwrap_or_default(),
9059+
pending_splice.our_funding_contribution,
90639060
)));
90649061
}
90659062

@@ -9153,7 +9150,11 @@ impl<SP: Deref> FundedChannel<SP> where
91539150
pending_interactive_tx_signing_session: None,
91549151
});
91559152
self.pending_splice = Some(PendingSplice {
9156-
pending_splice_init: None,
9153+
our_funding_contribution,
9154+
funding_feerate_per_kw: msg.funding_feerate_per_kw,
9155+
locktime: msg.locktime,
9156+
our_funding_inputs: Vec::new(), // inputs go directly to [`DualFundingChannelContext`] above
9157+
awaiting_splice_ack: false, // we don't need any additional message for the handshake
91579158
refunding_scope,
91589159
});
91599160
// TODO(splicing): Store msg.funding_pubkey
@@ -9192,17 +9193,17 @@ impl<SP: Deref> FundedChannel<SP> where
91929193
signer_provider: &SP, entropy_source: &ES, holder_node_id: &PublicKey, logger: &L,
91939194
) -> Result<Option<InteractiveTxMessageSend>, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
91949195
// check if splice is pending
9195-
let pending_splice_init = if let Some(pending_splice) = &self.pending_splice {
9196-
if let Some(pending_splice_init) = &pending_splice.pending_splice_init {
9197-
pending_splice_init
9198-
} else {
9199-
return Err(ChannelError::Warn(format!("Channel is not in pending splice_init")));
9200-
}
9196+
let pending_splice = if let Some(pending_splice) = &self.pending_splice {
9197+
pending_splice
92019198
} else {
92029199
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
92039200
};
92049201

9205-
let our_funding_contribution = pending_splice_init.our_funding_contribution;
9202+
if !pending_splice.awaiting_splice_ack {
9203+
return Err(ChannelError::Warn(format!("Received unexpected splice_ack")));
9204+
}
9205+
9206+
let our_funding_contribution = pending_splice.our_funding_contribution;
92069207
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
92079208

92089209
// TODO(splicing): Pre-check for reserve requirement
@@ -9237,12 +9238,15 @@ impl<SP: Deref> FundedChannel<SP> where
92379238
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
92389239
};
92399240

9240-
let pending_dual_funding_context = DualFundingChannelContext {
9241+
let mut pending_dual_funding_context = DualFundingChannelContext {
92419242
our_funding_satoshis,
92429243
their_funding_satoshis: Some(their_funding_satoshis),
9243-
funding_tx_locktime: LockTime::from_consensus(pending_splice_init.locktime),
9244-
funding_feerate_sat_per_1000_weight: pending_splice_init.funding_feerate_per_kw,
9245-
our_funding_inputs: pending_splice_init.our_funding_inputs.clone(),
9244+
funding_tx_locktime: LockTime::from_consensus(pending_splice.locktime),
9245+
funding_feerate_sat_per_1000_weight: pending_splice.funding_feerate_per_kw,
9246+
our_funding_inputs: Vec::new(), // set below
9247+
};
9248+
if let Some(ref mut pending_splice_mut) = &mut self.pending_splice {
9249+
pending_dual_funding_context.our_funding_inputs = std::mem::take(&mut pending_splice_mut.our_funding_inputs);
92469250
};
92479251
let pending_unfunded_context = UnfundedChannelContext {
92489252
unfunded_channel_age_ticks: 0,
@@ -9262,6 +9266,8 @@ impl<SP: Deref> FundedChannel<SP> where
92629266
let prev_funding_input = RefundingScope::get_input_of_previous_funding(pre_funding_transaction, pre_funding_txo)?;
92639267
if let Some(ref mut pending_splice) = &mut self.pending_splice {
92649268
pending_splice.refunding_scope = Some(refunding_scope);
9269+
debug_assert!(pending_splice.awaiting_splice_ack);
9270+
pending_splice.awaiting_splice_ack = false;
92659271
} else {
92669272
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
92679273
};

0 commit comments

Comments
 (0)