Skip to content

Commit c7aa6b7

Browse files
committed
Introduce a channel FundingScope
When establishing a channel, the funding transaction may be replaced either: - after the funding transaction has confirmed using splicing, - before the funding transaction has confirmed for v2 channel establishment using tx_init_rbf, or - before the splice's funding transaction has confirmed using tx_init_rbf. In each of these cases, fields in ChannelContext will need to be updated once the funding transaction confirms. Additionally, the same fields for a pending attempt may need to be considered instead of a previously confirmed funding. This commit introduces a FundingScope to hold the aforementioned fields. It lives next to ChannelContext and will be needed whenever these fields are accessed. The next few commits will move the relevant fields to FundingScope and provide access to them whenever needed, allowing to swap in another FundingScope when necessary.
1 parent 2d2c542 commit c7aa6b7

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,12 @@ impl UnfundedChannelContext {
15221522
}
15231523
}
15241524

1525+
/// Information pertaining to an attempt at funding the channel. This is typically constructed
1526+
/// during channel establishment and may be replaced during channel splicing or if the attempted
1527+
/// funding transaction is replaced using tx_init_rbf.
1528+
pub(super) struct FundingScope {
1529+
}
1530+
15251531
/// Contains everything about the channel including state, and various flags.
15261532
pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15271533
config: LegacyChannelConfig,
@@ -2166,6 +2172,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21662172
match self.unfunded_context.holder_commitment_point {
21672173
Some(holder_commitment_point) => {
21682174
let funded_chan = FundedChannel {
2175+
funding: self.funding,
21692176
context: self.context,
21702177
interactive_tx_signing_session: Some(signing_session),
21712178
holder_commitment_point,
@@ -2203,7 +2210,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22032210
msg_channel_reserve_satoshis: u64,
22042211
msg_push_msat: u64,
22052212
open_channel_fields: msgs::CommonOpenChannelFields,
2206-
) -> Result<ChannelContext<SP>, ChannelError>
2213+
) -> Result<(FundingScope, ChannelContext<SP>), ChannelError>
22072214
where
22082215
ES::Target: EntropySource,
22092216
F::Target: FeeEstimator,
@@ -2377,6 +2384,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23772384

23782385
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
23792386

2387+
let funding = FundingScope {
2388+
};
23802389
let channel_context = ChannelContext {
23812390
user_id,
23822391

@@ -2521,7 +2530,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25212530
next_funding_txid: None,
25222531
};
25232532

2524-
Ok(channel_context)
2533+
Ok((funding, channel_context))
25252534
}
25262535

25272536
fn new_for_outbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
@@ -2542,7 +2551,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25422551
holder_signer: <SP::Target as SignerProvider>::EcdsaSigner,
25432552
pubkeys: ChannelPublicKeys,
25442553
_logger: L,
2545-
) -> Result<ChannelContext<SP>, APIError>
2554+
) -> Result<(FundingScope, ChannelContext<SP>), APIError>
25462555
where
25472556
ES::Target: EntropySource,
25482557
F::Target: FeeEstimator,
@@ -2608,7 +2617,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26082617

26092618
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
26102619

2611-
Ok(Self {
2620+
let funding = FundingScope {
2621+
};
2622+
let channel_context = Self {
26122623
user_id,
26132624

26142625
config: LegacyChannelConfig {
@@ -2746,7 +2757,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27462757
local_initiated_shutdown: None,
27472758
is_manual_broadcast: false,
27482759
next_funding_txid: None,
2749-
})
2760+
};
2761+
2762+
Ok((funding, channel_context))
27502763
}
27512764

27522765
pub(crate) fn get_value_to_self_msat(&self) -> u64 {self.value_to_self_msat}
@@ -4502,6 +4515,7 @@ pub(super) struct DualFundingChannelContext {
45024515
// Holder designates channel data owned for the benefit of the user client.
45034516
// Counterparty designates channel data owned by the another channel participant entity.
45044517
pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
4518+
pub funding: FundingScope,
45054519
pub context: ChannelContext<SP>,
45064520
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
45074521
holder_commitment_point: HolderCommitmentPoint,
@@ -8518,6 +8532,7 @@ impl<SP: Deref> FundedChannel<SP> where
85188532

85198533
/// A not-yet-funded outbound (from holder) channel using V1 channel establishment.
85208534
pub(super) struct OutboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
8535+
pub funding: FundingScope,
85218536
pub context: ChannelContext<SP>,
85228537
pub unfunded_context: UnfundedChannelContext,
85238538
/// We tried to send an `open_channel` message but our commitment point wasn't ready.
@@ -8549,7 +8564,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
85498564
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
85508565
let pubkeys = holder_signer.pubkeys().clone();
85518566

8552-
let context = ChannelContext::new_for_outbound_channel(
8567+
let (funding, context) = ChannelContext::new_for_outbound_channel(
85538568
fee_estimator,
85548569
entropy_source,
85558570
signer_provider,
@@ -8575,7 +8590,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
85758590

85768591
// We initialize `signer_pending_open_channel` to false, and leave setting the flag
85778592
// for when we try to generate the open_channel message.
8578-
let chan = Self { context, unfunded_context, signer_pending_open_channel: false };
8593+
let chan = Self { funding, context, unfunded_context, signer_pending_open_channel: false };
85798594
Ok(chan)
85808595
}
85818596

@@ -8775,6 +8790,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
87758790
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
87768791

87778792
let mut channel = FundedChannel {
8793+
funding: self.funding,
87788794
context: self.context,
87798795
interactive_tx_signing_session: None,
87808796
holder_commitment_point,
@@ -8815,6 +8831,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
88158831

88168832
/// A not-yet-funded inbound (from counterparty) channel using V1 channel establishment.
88178833
pub(super) struct InboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
8834+
pub funding: FundingScope,
88188835
pub context: ChannelContext<SP>,
88198836
pub unfunded_context: UnfundedChannelContext,
88208837
pub signer_pending_accept_channel: bool,
@@ -8883,7 +8900,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
88838900
htlc_basepoint: HtlcBasepoint::from(msg.common_fields.htlc_basepoint)
88848901
};
88858902

8886-
let context = ChannelContext::new_for_inbound_channel(
8903+
let (funding, context) = ChannelContext::new_for_inbound_channel(
88878904
fee_estimator,
88888905
entropy_source,
88898906
signer_provider,
@@ -8907,7 +8924,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
89078924
unfunded_channel_age_ticks: 0,
89088925
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
89098926
};
8910-
let chan = Self { context, unfunded_context, signer_pending_accept_channel: false };
8927+
let chan = Self { funding, context, unfunded_context, signer_pending_accept_channel: false };
89118928
Ok(chan)
89128929
}
89138930

@@ -9040,6 +9057,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
90409057
// Promote the channel to a full-fledged one now that we have updated the state and have a
90419058
// `ChannelMonitor`.
90429059
let mut channel = FundedChannel {
9060+
funding: self.funding,
90439061
context: self.context,
90449062
interactive_tx_signing_session: None,
90459063
holder_commitment_point,
@@ -9073,6 +9091,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
90739091

90749092
// A not-yet-funded channel using V2 channel establishment.
90759093
pub(super) struct PendingV2Channel<SP: Deref> where SP::Target: SignerProvider {
9094+
pub funding: FundingScope,
90769095
pub context: ChannelContext<SP>,
90779096
pub unfunded_context: UnfundedChannelContext,
90789097
pub dual_funding_context: DualFundingChannelContext,
@@ -9109,7 +9128,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
91099128
"Provided current chain height of {} doesn't make sense for a height-based timelock for the funding transaction",
91109129
current_chain_height) })?;
91119130

9112-
let context = ChannelContext::new_for_outbound_channel(
9131+
let (funding, context) = ChannelContext::new_for_outbound_channel(
91139132
fee_estimator,
91149133
entropy_source,
91159134
signer_provider,
@@ -9133,6 +9152,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
91339152
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
91349153
};
91359154
let chan = Self {
9155+
funding,
91369156
context,
91379157
unfunded_context,
91389158
dual_funding_context: DualFundingChannelContext {
@@ -9255,7 +9275,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
92559275
htlc_basepoint: HtlcBasepoint(msg.common_fields.htlc_basepoint)
92569276
};
92579277

9258-
let mut context = ChannelContext::new_for_inbound_channel(
9278+
let (funding, mut context) = ChannelContext::new_for_inbound_channel(
92599279
fee_estimator,
92609280
entropy_source,
92619281
signer_provider,
@@ -9311,6 +9331,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
93119331
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
93129332
};
93139333
Ok(Self {
9334+
funding,
93149335
context,
93159336
dual_funding_context,
93169337
interactive_tx_constructor,
@@ -10284,6 +10305,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1028410305
};
1028510306

1028610307
Ok(FundedChannel {
10308+
funding: FundingScope {
10309+
},
1028710310
context: ChannelContext {
1028810311
user_id,
1028910312

0 commit comments

Comments
 (0)