Skip to content

Commit fe46b65

Browse files
committed
Sort out PendingSplicePre and -Post, new spliced for ChannelContext and OutboundV2Channel
1 parent 089cf24 commit fe46b65

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

Diff for: lightning/src/ln/channel.rs

+80-4
Original file line numberDiff line numberDiff line change
@@ -4554,7 +4554,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45544554
// self.channel_state = ChannelState::NegotiatingFunding(
45554555
// NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT
45564556
// );
4557-
log_info!(logger, "Splicing process started, old channel value {}, outgoing {}, channel_id {}",
4557+
log_info!(logger, "Splicing process started, new channel value {}, outgoing {}, channel_id {}",
45584558
self.channel_value_satoshis, is_outgoing, self.channel_id);
45594559
}
45604560

@@ -9496,6 +9496,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
94969496
our_funding_inputs: funding_inputs,
94979497
},
94989498
interactive_tx_constructor: None,
9499+
#[cfg(splicing)]
94999500
pending_splice_post: None,
95009501
};
95019502
Ok(chan)
@@ -9505,7 +9506,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
95059506
#[cfg(splicing)]
95069507
pub fn new_spliced<L: Deref>(
95079508
is_outbound: bool,
9508-
pre_splice_channel: &mut Channel<SP>,
9509+
pre_splice_channel: &Channel<SP>,
95099510
signer_provider: &SP,
95109511
counterparty_funding_pubkey: &PublicKey,
95119512
our_funding_contribution: i64,
@@ -9558,7 +9559,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
95589559
their_funding_satoshis: Some(their_funding_satoshis),
95599560
funding_tx_locktime,
95609561
funding_feerate_sat_per_1000_weight,
9561-
our_funding_inputs: Some(funding_inputs),
9562+
our_funding_inputs: funding_inputs,
95629563
};
95639564
let unfunded_context = UnfundedChannelContext::default();
95649565
let post_chan = Self {
@@ -9655,6 +9656,9 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
96559656
pub dual_funding_context: DualFundingChannelContext,
96569657
/// The current interactive transaction construction session under negotiation.
96579658
interactive_tx_constructor: Option<InteractiveTxConstructor>,
9659+
/// Info about an in-progress, pending splice (if any), on the post-splice channel
9660+
#[cfg(splicing)]
9661+
pending_splice_post: Option<PendingSplicePost>,
96589662
}
96599663

96609664
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -9758,9 +9762,81 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
97589762
dual_funding_context,
97599763
interactive_tx_constructor,
97609764
unfunded_context: UnfundedChannelContext::default(),
9765+
#[cfg(splicing)]
9766+
pending_splice_post: None,
97619767
})
97629768
}
97639769

9770+
/// Create new channel for splicing
9771+
#[cfg(splicing)]
9772+
pub fn new_spliced<L: Deref>(
9773+
is_outbound: bool,
9774+
pre_splice_channel: &Channel<SP>,
9775+
signer_provider: &SP,
9776+
counterparty_funding_pubkey: &PublicKey,
9777+
our_funding_contribution: i64,
9778+
their_funding_contribution: i64,
9779+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
9780+
funding_tx_locktime: LockTime,
9781+
funding_feerate_sat_per_1000_weight: u32,
9782+
logger: &L,
9783+
) -> Result<Self, ChannelError> where L::Target: Logger
9784+
{
9785+
if pre_splice_channel.is_splice_pending() {
9786+
return Err(ChannelError::Warn(format!("Internal error: Channel is already splicing, channel_id {}", pre_splice_channel.context.channel_id)));
9787+
}
9788+
9789+
let pre_channel_value = pre_splice_channel.context.get_value_satoshis();
9790+
9791+
// Save the current funding transaction
9792+
let pre_funding_transaction = pre_splice_channel.context.funding_transaction.clone();
9793+
let pre_funding_txo = pre_splice_channel.context.get_funding_txo().clone();
9794+
9795+
let pending_splice_post = PendingSplicePost::new(
9796+
pre_channel_value, our_funding_contribution, their_funding_contribution,
9797+
pre_funding_transaction, pre_funding_txo,
9798+
);
9799+
let post_channel_value = pending_splice_post.post_channel_value();
9800+
9801+
// Create new signer, using the new channel value.
9802+
// Note: channel_keys_id is not changed
9803+
let holder_signer = signer_provider.derive_channel_signer(post_channel_value, pre_splice_channel.context.channel_keys_id);
9804+
9805+
let context = ChannelContext::new_for_splice(
9806+
&pre_splice_channel.context,
9807+
false,
9808+
counterparty_funding_pubkey,
9809+
our_funding_contribution,
9810+
their_funding_contribution,
9811+
holder_signer,
9812+
logger,
9813+
)?;
9814+
9815+
let (our_funding_satoshis, their_funding_satoshis) = calculate_funding_values(
9816+
pre_channel_value,
9817+
our_funding_contribution,
9818+
their_funding_contribution,
9819+
is_outbound,
9820+
)?;
9821+
9822+
let dual_funding_context = DualFundingChannelContext {
9823+
our_funding_satoshis,
9824+
their_funding_satoshis: Some(their_funding_satoshis),
9825+
funding_tx_locktime,
9826+
funding_feerate_sat_per_1000_weight,
9827+
our_funding_inputs: funding_inputs,
9828+
};
9829+
let unfunded_context = UnfundedChannelContext::default();
9830+
let post_chan = Self {
9831+
context,
9832+
dual_funding_context,
9833+
unfunded_context,
9834+
interactive_tx_constructor: None,
9835+
pending_splice_post: Some(pending_splice_post),
9836+
};
9837+
Ok(post_chan)
9838+
}
9839+
97649840
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannelV2`] message which
97659841
/// should be sent back to the counterparty node.
97669842
///
@@ -9840,7 +9916,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
98409916
#[cfg(splicing)]
98419917
pending_splice_pre: None,
98429918
#[cfg(splicing)]
9843-
pending_splice_post: None,
9919+
pending_splice_post: self.pending_splice_post,
98449920
};
98459921

98469922
Ok(channel)

Diff for: lightning/src/ln/functional_tests_splice.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ fn test_v1_splice_in() {
221221

222222
// ==== Channel is now ready for normal operation
223223

224+
// Expected balances
225+
let mut exp_balance1 = 1000 * channel_value_sat;
226+
let mut exp_balance2 = 0;
227+
224228
// === Start of Splicing
225229
println!("Start of Splicing ..., channel_id {}", channel_id2);
226230

@@ -276,8 +280,8 @@ fn test_v1_splice_in() {
276280
assert!(channel.is_usable);
277281
assert!(channel.is_channel_ready);
278282
assert_eq!(channel.channel_value_satoshis, channel_value_sat);
279-
assert_eq!(channel.outbound_capacity_msat, 0);
280-
assert!(channel.funding_txo.is_some());
283+
assert_eq!(channel.outbound_capacity_msat, exp_balance2);
284+
assert_eq!(channel.funding_txo.unwrap().txid, funding_tx.compute_txid());
281285
assert!(channel.confirmations.unwrap() > 0);
282286
}
283287

@@ -295,9 +299,9 @@ fn test_v1_splice_in() {
295299
assert_eq!(channel.channel_value_satoshis, channel_value_sat);
296300
assert_eq!(
297301
channel.outbound_capacity_msat,
298-
1000 * (channel_value_sat - channel_reserve_amnt_sat)
302+
exp_balance1 - 1000 * channel_reserve_amnt_sat
299303
);
300-
assert!(channel.funding_txo.is_some());
304+
assert_eq!(channel.funding_txo.unwrap().txid, funding_tx.compute_txid());
301305
assert!(channel.confirmations.unwrap() > 0);
302306
}
303307

0 commit comments

Comments
 (0)