@@ -1946,12 +1946,15 @@ impl FundingScope {
19461946
19471947 /// Construct FundingScope for a splicing channel
19481948 #[cfg(splicing)]
1949- pub fn for_splice<SP: Deref>(prev_funding: &Self, context: &ChannelContext<SP>, our_funding_contribution_sats: i64, post_channel_value: u64, counterparty_funding_pubkey: PublicKey) -> Self where SP::Target: SignerProvider {
1950- let post_value_to_self_msat = if our_funding_contribution_sats < 0 {
1951- prev_funding.value_to_self_msat.saturating_sub((-our_funding_contribution_sats as u64) * 1000)
1952- } else {
1953- prev_funding.value_to_self_msat.saturating_add((our_funding_contribution_sats as u64) * 1000)
1954- };
1949+ pub fn for_splice<SP: Deref>(prev_funding: &Self, context: &ChannelContext<SP>, our_funding_contribution_sats: i64, post_channel_value: u64, counterparty_funding_pubkey: PublicKey) -> Result<Self, ChannelError> where SP::Target: SignerProvider {
1950+ let post_value_to_self_msat_signed = (prev_funding.value_to_self_msat as i64).saturating_add(our_funding_contribution_sats * 1000);
1951+ if post_value_to_self_msat_signed < 0 {
1952+ // Splice out and more than our balance, error
1953+ return Err(ChannelError::Warn(format!("Cannot splice out more than the current balance, {} sats, {} msats",
1954+ post_value_to_self_msat_signed, prev_funding.value_to_self_msat)));
1955+ }
1956+ debug_assert!(post_value_to_self_msat_signed >= 0);
1957+ let post_value_to_self_msat = post_value_to_self_msat_signed as u64;
19551958
19561959 let prev_funding_txid = prev_funding.channel_transaction_parameters.funding_outpoint
19571960 .map(|outpoint| outpoint.txid);
@@ -1984,7 +1987,7 @@ impl FundingScope {
19841987 post_channel_value, context.counterparty_dust_limit_satoshis));
19851988 let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
19861989 post_channel_value, MIN_CHAN_DUST_LIMIT_SATOSHIS);
1987- Self {
1990+ Ok( Self {
19881991 channel_transaction_parameters: post_channel_transaction_parameters,
19891992 value_to_self_msat: post_value_to_self_msat,
19901993 funding_transaction: None,
@@ -1998,7 +2001,7 @@ impl FundingScope {
19982001 next_local_commitment_tx_fee_info_cached: Mutex::new(None),
19992002 #[cfg(any(test, fuzzing))]
20002003 next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2001- }
2004+ })
20022005 }
20032006}
20042007
@@ -9203,7 +9206,7 @@ impl<SP: Deref> FundedChannel<SP> where
92039206 false, // is_outbound
92049207 )?;
92059208
9206- let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_contribution, post_channel_value, msg.funding_pubkey);
9209+ let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_contribution, post_channel_value, msg.funding_pubkey)? ;
92079210
92089211 let funding_negotiation_context = FundingNegotiationContext {
92099212 our_funding_satoshis,
@@ -9298,7 +9301,7 @@ impl<SP: Deref> FundedChannel<SP> where
92989301 true, // is_outbound
92999302 )?;
93009303
9301- let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_contribution, post_channel_value, msg.funding_pubkey);
9304+ let funding_scope = FundingScope::for_splice(&self.funding, &self.context, our_funding_contribution, post_channel_value, msg.funding_pubkey)? ;
93029305
93039306 let pre_funding_transaction = &self.funding.funding_transaction;
93049307 let pre_funding_txo = &self.funding.get_funding_txo();
0 commit comments