Skip to content

Commit c7feb38

Browse files
committed
fix Move funding_scope_for_splice() into FundingScope
Simplify handling of pending_splice field check, get rid of one unwrap().
1 parent 49e0a74 commit c7feb38

File tree

1 file changed

+66
-67
lines changed

1 file changed

+66
-67
lines changed

lightning/src/ln/channel.rs

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,59 @@ impl FundingScope {
19571957
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
19581958
&self.channel_transaction_parameters.channel_type_features
19591959
}
1960+
1961+
/// Construct FundingScope for a splicing channel
1962+
#[cfg(splicing)]
1963+
pub fn for_splice<SP: Deref>(prev_funding: &Self, context: &ChannelContext<SP>, our_funding_satoshis: u64, post_channel_value: u64, is_initiator: bool, counterparty_funding_pubkey: PublicKey) -> Self where SP::Target: SignerProvider {
1964+
let post_value_to_self_msat = prev_funding.value_to_self_msat.saturating_add(our_funding_satoshis);
1965+
1966+
let prev_funding_txid = prev_funding.channel_transaction_parameters.funding_outpoint
1967+
.map(|outpoint| outpoint.txid);
1968+
let holder_pubkeys = match &context.holder_signer {
1969+
ChannelSignerType::Ecdsa(ecdsa) => {
1970+
ecdsa.pubkeys(prev_funding_txid, &context.secp_ctx)
1971+
}
1972+
// TODO (taproot|arik)
1973+
#[cfg(taproot)]
1974+
_ => todo!()
1975+
};
1976+
let mut post_channel_transaction_parameters = ChannelTransactionParameters {
1977+
holder_pubkeys,
1978+
holder_selected_contest_delay: prev_funding.channel_transaction_parameters.holder_selected_contest_delay,
1979+
// The 'outbound' attribute may change, if the the splice is being initiated by the previous acceptor
1980+
is_outbound_from_holder: is_initiator,
1981+
counterparty_parameters: prev_funding.channel_transaction_parameters.counterparty_parameters.clone(),
1982+
funding_outpoint: None, // filled later
1983+
splice_parent_funding_txid: prev_funding_txid,
1984+
channel_type_features: prev_funding.channel_transaction_parameters.channel_type_features.clone(),
1985+
channel_value_satoshis: post_channel_value,
1986+
};
1987+
// Update the splicing 'tweak', this will rotate the keys in the signer
1988+
if let Some(ref mut counterparty_parameters) = post_channel_transaction_parameters.counterparty_parameters {
1989+
counterparty_parameters.pubkeys.funding_pubkey = counterparty_funding_pubkey;
1990+
}
1991+
1992+
// New reserve values are based on the new channel value, and v2-specific
1993+
let counterparty_selected_channel_reserve_satoshis = Some(get_v2_channel_reserve_satoshis(
1994+
post_channel_value, context.counterparty_dust_limit_satoshis));
1995+
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
1996+
post_channel_value, MIN_CHAN_DUST_LIMIT_SATOSHIS);
1997+
Self {
1998+
channel_transaction_parameters: post_channel_transaction_parameters,
1999+
value_to_self_msat: post_value_to_self_msat,
2000+
funding_transaction: None,
2001+
counterparty_selected_channel_reserve_satoshis,
2002+
holder_selected_channel_reserve_satoshis,
2003+
#[cfg(debug_assertions)]
2004+
holder_max_commitment_tx_output: Mutex::new((post_value_to_self_msat, (post_channel_value * 1000).saturating_sub(post_value_to_self_msat))),
2005+
#[cfg(debug_assertions)]
2006+
counterparty_max_commitment_tx_output: Mutex::new((post_value_to_self_msat, (post_channel_value * 1000).saturating_sub(post_value_to_self_msat))),
2007+
#[cfg(any(test, fuzzing))]
2008+
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2009+
#[cfg(any(test, fuzzing))]
2010+
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2011+
}
2012+
}
19602013
}
19612014

19622015
/// Info about a pending splice
@@ -9138,59 +9191,6 @@ impl<SP: Deref> FundedChannel<SP> where
91389191
Ok(())
91399192
}
91409193

9141-
/// Helper to build the FundingScope for the splicing channel
9142-
#[cfg(splicing)]
9143-
fn funding_scope_for_splice(&self, our_funding_satoshis: u64, post_channel_value: u64, is_initiator: bool, counterparty_funding_pubkey: PublicKey) -> FundingScope {
9144-
let post_value_to_self_msat = self.funding.value_to_self_msat.saturating_add(our_funding_satoshis);
9145-
9146-
let prev_funding_txid = self.funding.channel_transaction_parameters.funding_outpoint
9147-
.map(|outpoint| outpoint.txid);
9148-
let holder_pubkeys = match &self.context.holder_signer {
9149-
ChannelSignerType::Ecdsa(ecdsa) => {
9150-
ecdsa.pubkeys(prev_funding_txid, &self.context.secp_ctx)
9151-
}
9152-
// TODO (taproot|arik)
9153-
#[cfg(taproot)]
9154-
_ => todo!()
9155-
};
9156-
let mut post_channel_transaction_parameters = ChannelTransactionParameters {
9157-
holder_pubkeys,
9158-
holder_selected_contest_delay: self.funding.channel_transaction_parameters.holder_selected_contest_delay,
9159-
// The 'outbound' attribute may change, if the the splice is being initiated by the previous acceptor
9160-
is_outbound_from_holder: is_initiator,
9161-
counterparty_parameters: self.funding.channel_transaction_parameters.counterparty_parameters.clone(),
9162-
funding_outpoint: None, // filled later
9163-
splice_parent_funding_txid: prev_funding_txid,
9164-
channel_type_features: self.funding.channel_transaction_parameters.channel_type_features.clone(),
9165-
channel_value_satoshis: post_channel_value,
9166-
};
9167-
// Update the splicing 'tweak', this will rotate the keys in the signer
9168-
if let Some(ref mut counterparty_parameters) = post_channel_transaction_parameters.counterparty_parameters {
9169-
counterparty_parameters.pubkeys.funding_pubkey = counterparty_funding_pubkey;
9170-
}
9171-
9172-
// New reserve values are based on the new channel value, and v2-specific
9173-
let counterparty_selected_channel_reserve_satoshis = Some(get_v2_channel_reserve_satoshis(
9174-
post_channel_value, self.context.counterparty_dust_limit_satoshis));
9175-
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
9176-
post_channel_value, MIN_CHAN_DUST_LIMIT_SATOSHIS);
9177-
FundingScope {
9178-
channel_transaction_parameters: post_channel_transaction_parameters,
9179-
value_to_self_msat: post_value_to_self_msat,
9180-
funding_transaction: None,
9181-
counterparty_selected_channel_reserve_satoshis,
9182-
holder_selected_channel_reserve_satoshis,
9183-
#[cfg(debug_assertions)]
9184-
holder_max_commitment_tx_output: Mutex::new((post_value_to_self_msat, (post_channel_value * 1000).saturating_sub(post_value_to_self_msat))),
9185-
#[cfg(debug_assertions)]
9186-
counterparty_max_commitment_tx_output: Mutex::new((post_value_to_self_msat, (post_channel_value * 1000).saturating_sub(post_value_to_self_msat))),
9187-
#[cfg(any(test, fuzzing))]
9188-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
9189-
#[cfg(any(test, fuzzing))]
9190-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
9191-
}
9192-
}
9193-
91949194
/// See also [`splice_init_checks`]
91959195
#[cfg(splicing)]
91969196
pub(crate) fn splice_init<ES: Deref, L: Deref>(
@@ -9213,7 +9213,7 @@ impl<SP: Deref> FundedChannel<SP> where
92139213
false, // is_outbound
92149214
)?;
92159215

9216-
let funding_scope = self.funding_scope_for_splice(our_funding_satoshis, post_channel_value, false, msg.funding_pubkey);
9216+
let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_satoshis, post_channel_value, false, msg.funding_pubkey);
92179217

92189218
let funding_negotiation_context = FundingNegotiationContext {
92199219
our_funding_satoshis,
@@ -9277,7 +9277,7 @@ impl<SP: Deref> FundedChannel<SP> where
92779277
signer_provider: &SP, entropy_source: &ES, holder_node_id: &PublicKey, logger: &L,
92789278
) -> Result<Option<InteractiveTxMessageSend>, ChannelError> where ES::Target: EntropySource, L::Target: Logger {
92799279
// check if splice is pending
9280-
let pending_splice = if let Some(pending_splice) = &self.pending_splice {
9280+
let pending_splice = if let Some(ref mut pending_splice) = &mut self.pending_splice {
92819281
pending_splice
92829282
} else {
92839283
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
@@ -9308,31 +9308,30 @@ impl<SP: Deref> FundedChannel<SP> where
93089308
true, // is_outbound
93099309
)?;
93109310

9311-
let funding_scope = self.funding_scope_for_splice(our_funding_satoshis, post_channel_value, true, msg.funding_pubkey);
9311+
let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_satoshis, post_channel_value, true, msg.funding_pubkey);
93129312

93139313
let pre_funding_transaction = &self.funding.funding_transaction;
93149314
let pre_funding_txo = &self.funding.get_funding_txo();
93159315
// We need the current funding tx as an extra input
93169316
let prev_funding_input = Self::get_input_of_previous_funding(pre_funding_transaction, pre_funding_txo)?;
9317-
let pending_splice_mut = self.pending_splice.as_mut().unwrap(); // existence checked above
9318-
debug_assert!(pending_splice_mut.funding_scope.is_none());
9319-
pending_splice_mut.funding_scope = Some(funding_scope);
9317+
debug_assert!(pending_splice.funding_scope.is_none());
9318+
pending_splice.funding_scope = Some(funding_scope);
93209319
// update funding values
9321-
pending_splice_mut.funding_negotiation_context.our_funding_satoshis = our_funding_satoshis;
9322-
pending_splice_mut.funding_negotiation_context.their_funding_satoshis = Some(their_funding_satoshis);
9323-
pending_splice_mut.interactive_tx_constructor = None;
9324-
pending_splice_mut.interactive_tx_signing_session = None;
9320+
pending_splice.funding_negotiation_context.our_funding_satoshis = our_funding_satoshis;
9321+
pending_splice.funding_negotiation_context.their_funding_satoshis = Some(their_funding_satoshis);
9322+
pending_splice.interactive_tx_constructor = None;
9323+
pending_splice.interactive_tx_signing_session = None;
93259324

93269325
log_info!(logger, "Splicing process started after splice_ack, new channel value {}, old {}, outgoing {}, channel_id {}",
93279326
post_channel_value, pre_channel_value, true, self.context.channel_id);
93289327

93299328
// Build NegotiatingChannelView lcoally, simmilar to Channel::as_renegotiating_channel()
93309329
let mut negotiating_view = NegotiatingChannelView {
93319330
context: &mut self.context,
9332-
funding: &mut pending_splice_mut.funding_scope.as_mut().unwrap(), // set above
9333-
funding_negotiation_context: &mut pending_splice_mut.funding_negotiation_context,
9334-
interactive_tx_constructor: &mut pending_splice_mut.interactive_tx_constructor,
9335-
interactive_tx_signing_session: &mut pending_splice_mut.interactive_tx_signing_session,
9331+
funding: &mut pending_splice.funding_scope.as_mut().unwrap(), // set above
9332+
funding_negotiation_context: &mut pending_splice.funding_negotiation_context,
9333+
interactive_tx_constructor: &mut pending_splice.interactive_tx_constructor,
9334+
interactive_tx_signing_session: &mut pending_splice.interactive_tx_signing_session,
93369335
holder_commitment_transaction_number: self.holder_commitment_point.transaction_number(),
93379336
is_splice: true,
93389337
};

0 commit comments

Comments
 (0)