Skip to content

Commit 14dfc72

Browse files
committed
Sort out PendingSplicePre and -Post, new spliced for ChannelContext and OutboundV2Channel
1 parent fe5f56a commit 14dfc72

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4503,7 +4503,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45034503
// self.channel_state = ChannelState::NegotiatingFunding(
45044504
// NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT
45054505
// );
4506-
log_info!(logger, "Splicing process started, old channel value {}, outgoing {}, channel_id {}",
4506+
log_info!(logger, "Splicing process started, new channel value {}, outgoing {}, channel_id {}",
45074507
self.channel_value_satoshis, is_outgoing, self.channel_id);
45084508
}
45094509

@@ -8913,7 +8913,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
89138913
pubkeys,
89148914
logger,
89158915
)?,
8916-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
8916+
unfunded_context: UnfundedChannelContext::default(),
89178917
};
89188918
Ok(chan)
89198919
}
@@ -9221,7 +9221,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
92219221
msg.push_msat,
92229222
msg.common_fields.clone(),
92239223
)?,
9224-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
9224+
unfunded_context: UnfundedChannelContext::default(),
92259225
};
92269226
Ok(chan)
92279227
}
@@ -9434,7 +9434,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
94349434
pubkeys,
94359435
logger,
94369436
)?,
9437-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
9437+
unfunded_context: UnfundedChannelContext::default(),
94389438
dual_funding_context: DualFundingChannelContext {
94399439
our_funding_satoshis: funding_satoshis,
94409440
their_funding_satoshis: None,
@@ -9443,6 +9443,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
94439443
our_funding_inputs: Some(funding_inputs),
94449444
},
94459445
interactive_tx_constructor: None,
9446+
#[cfg(splicing)]
94469447
pending_splice_post: None,
94479448
};
94489449
Ok(chan)
@@ -9452,7 +9453,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
94529453
#[cfg(splicing)]
94539454
pub fn new_spliced<L: Deref>(
94549455
is_outbound: bool,
9455-
pre_splice_channel: &mut Channel<SP>,
9456+
pre_splice_channel: &Channel<SP>,
94569457
signer_provider: &SP,
94579458
counterparty_funding_pubkey: &PublicKey,
94589459
our_funding_contribution: i64,
@@ -9602,6 +9603,9 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
96029603
pub dual_funding_context: DualFundingChannelContext,
96039604
/// The current interactive transaction construction session under negotiation.
96049605
interactive_tx_constructor: Option<InteractiveTxConstructor>,
9606+
/// Info about an in-progress, pending splice (if any), on the post-splice channel
9607+
#[cfg(splicing)]
9608+
pending_splice_post: Option<PendingSplicePost>,
96059609
}
96069610

96079611
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -9704,10 +9708,82 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
97049708
context,
97059709
dual_funding_context,
97069710
interactive_tx_constructor,
9707-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
9711+
unfunded_context: UnfundedChannelContext::default(),
9712+
#[cfg(splicing)]
9713+
pending_splice_post: None,
97089714
})
97099715
}
97109716

9717+
/// Create new channel for splicing
9718+
#[cfg(splicing)]
9719+
pub fn new_spliced<L: Deref>(
9720+
is_outbound: bool,
9721+
pre_splice_channel: &Channel<SP>,
9722+
signer_provider: &SP,
9723+
counterparty_funding_pubkey: &PublicKey,
9724+
our_funding_contribution: i64,
9725+
their_funding_contribution: i64,
9726+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
9727+
funding_tx_locktime: LockTime,
9728+
funding_feerate_sat_per_1000_weight: u32,
9729+
logger: &L,
9730+
) -> Result<Self, ChannelError> where L::Target: Logger
9731+
{
9732+
if pre_splice_channel.is_splice_pending() {
9733+
return Err(ChannelError::Warn(format!("Internal error: Channel is already splicing, channel_id {}", pre_splice_channel.context.channel_id)));
9734+
}
9735+
9736+
let pre_channel_value = pre_splice_channel.context.get_value_satoshis();
9737+
9738+
// Save the current funding transaction
9739+
let pre_funding_transaction = pre_splice_channel.context.funding_transaction.clone();
9740+
let pre_funding_txo = pre_splice_channel.context.get_funding_txo().clone();
9741+
9742+
let pending_splice_post = PendingSplicePost::new(
9743+
pre_channel_value, our_funding_contribution, their_funding_contribution,
9744+
pre_funding_transaction, pre_funding_txo,
9745+
);
9746+
let post_channel_value = pending_splice_post.post_channel_value();
9747+
9748+
// Create new signer, using the new channel value.
9749+
// Note: channel_keys_id is not changed
9750+
let holder_signer = signer_provider.derive_channel_signer(post_channel_value, pre_splice_channel.context.channel_keys_id);
9751+
9752+
let context = ChannelContext::new_for_splice(
9753+
&pre_splice_channel.context,
9754+
false,
9755+
counterparty_funding_pubkey,
9756+
our_funding_contribution,
9757+
their_funding_contribution,
9758+
holder_signer,
9759+
logger,
9760+
)?;
9761+
9762+
let (our_funding_satoshis, their_funding_satoshis) = calculate_funding_values(
9763+
pre_channel_value,
9764+
our_funding_contribution,
9765+
their_funding_contribution,
9766+
is_outbound,
9767+
)?;
9768+
9769+
let dual_funding_context = DualFundingChannelContext {
9770+
our_funding_satoshis,
9771+
their_funding_satoshis: Some(their_funding_satoshis),
9772+
funding_tx_locktime,
9773+
funding_feerate_sat_per_1000_weight,
9774+
our_funding_inputs: Some(funding_inputs),
9775+
};
9776+
let unfunded_context = UnfundedChannelContext::default();
9777+
let post_chan = Self {
9778+
context,
9779+
dual_funding_context,
9780+
unfunded_context,
9781+
interactive_tx_constructor: None,
9782+
pending_splice_post: Some(pending_splice_post),
9783+
};
9784+
Ok(post_chan)
9785+
}
9786+
97119787
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannelV2`] message which
97129788
/// should be sent back to the counterparty node.
97139789
///
@@ -9787,7 +9863,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
97879863
#[cfg(splicing)]
97889864
pending_splice_pre: None,
97899865
#[cfg(splicing)]
9790-
pending_splice_post: None,
9866+
pending_splice_post: self.pending_splice_post,
97919867
};
97929868

97939869
Ok(channel)

lightning/src/ln/functional_tests_splice.rs

Lines changed: 8 additions & 4 deletions
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)