Skip to content

Commit 5bdedc6

Browse files
committed
Add holder commitment point to unfunded channel context
1 parent f8bdcdd commit 5bdedc6

File tree

1 file changed

+68
-55
lines changed

1 file changed

+68
-55
lines changed

lightning/src/ln/channel.rs

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,17 @@ enum HolderCommitmentPoint {
954954
}
955955

956956
impl HolderCommitmentPoint {
957-
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Self
957+
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
958958
where SP::Target: SignerProvider
959959
{
960-
HolderCommitmentPoint::Available {
961-
transaction_number: INITIAL_COMMITMENT_NUMBER,
962-
// TODO(async_signing): remove this expect with the Uninitialized variant
963-
current: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx)
964-
.expect("Signer must be able to provide initial commitment point"),
965-
// TODO(async_signing): remove this expect with the Uninitialized variant
966-
next: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx)
967-
.expect("Signer must be able to provide second commitment point"),
968-
}
960+
let current = signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx).ok()?;
961+
let next = signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx).ok();
962+
let point = if let Some(next) = next {
963+
HolderCommitmentPoint::Available { transaction_number: INITIAL_COMMITMENT_NUMBER, current, next }
964+
} else {
965+
HolderCommitmentPoint::PendingNext { transaction_number: INITIAL_COMMITMENT_NUMBER, current }
966+
};
967+
Some(point)
969968
}
970969

971970
pub fn is_available(&self) -> bool {
@@ -1163,6 +1162,8 @@ pub(super) struct UnfundedChannelContext {
11631162
/// This is so that we don't keep channels around that haven't progressed to a funded state
11641163
/// in a timely manner.
11651164
unfunded_channel_age_ticks: usize,
1165+
/// Tracks the commitment number and commitment point before the channel is funded.
1166+
holder_commitment_point: Option<HolderCommitmentPoint>,
11661167
}
11671168

11681169
impl UnfundedChannelContext {
@@ -1174,6 +1175,11 @@ impl UnfundedChannelContext {
11741175
self.unfunded_channel_age_ticks += 1;
11751176
self.unfunded_channel_age_ticks >= UNFUNDED_CHANNEL_AGE_LIMIT_TICKS
11761177
}
1178+
1179+
fn transaction_number(&self) -> u64 {
1180+
self.holder_commitment_point.as_ref().map(|point| point.transaction_number())
1181+
.unwrap_or(INITIAL_COMMITMENT_NUMBER)
1182+
}
11771183
}
11781184

11791185
/// Contains everything about the channel including state, and various flags.
@@ -1685,7 +1691,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
16851691
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
16861692

16871693
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1688-
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1694+
// Unwrap here since it gets removed in the next commit
1695+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx).unwrap();
16891696

16901697
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
16911698

@@ -1921,7 +1928,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19211928
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
19221929

19231930
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1924-
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1931+
// Unwrap here since it gets removed in the next commit
1932+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx).unwrap();
19251933

19261934
Ok(Self {
19271935
user_id,
@@ -7643,28 +7651,31 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76437651
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
76447652
let pubkeys = holder_signer.pubkeys().clone();
76457653

7646-
let chan = Self {
7647-
context: ChannelContext::new_for_outbound_channel(
7648-
fee_estimator,
7649-
entropy_source,
7650-
signer_provider,
7651-
counterparty_node_id,
7652-
their_features,
7653-
channel_value_satoshis,
7654-
push_msat,
7655-
user_id,
7656-
config,
7657-
current_chain_height,
7658-
outbound_scid_alias,
7659-
temporary_channel_id,
7660-
holder_selected_channel_reserve_satoshis,
7661-
channel_keys_id,
7662-
holder_signer,
7663-
pubkeys,
7664-
logger,
7665-
)?,
7666-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
7654+
let context = ChannelContext::new_for_outbound_channel(
7655+
fee_estimator,
7656+
entropy_source,
7657+
signer_provider,
7658+
counterparty_node_id,
7659+
their_features,
7660+
channel_value_satoshis,
7661+
push_msat,
7662+
user_id,
7663+
config,
7664+
current_chain_height,
7665+
outbound_scid_alias,
7666+
temporary_channel_id,
7667+
holder_selected_channel_reserve_satoshis,
7668+
channel_keys_id,
7669+
holder_signer,
7670+
pubkeys,
7671+
logger,
7672+
)?;
7673+
let unfunded_context = UnfundedChannelContext {
7674+
unfunded_channel_age_ticks: 0,
7675+
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
76677676
};
7677+
7678+
let chan = Self { context, unfunded_context };
76687679
Ok(chan)
76697680
}
76707681

@@ -8020,29 +8031,31 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80208031
htlc_basepoint: HtlcBasepoint::from(msg.common_fields.htlc_basepoint)
80218032
};
80228033

8023-
let chan = Self {
8024-
context: ChannelContext::new_for_inbound_channel(
8025-
fee_estimator,
8026-
entropy_source,
8027-
signer_provider,
8028-
counterparty_node_id,
8029-
their_features,
8030-
user_id,
8031-
config,
8032-
current_chain_height,
8033-
&&logger,
8034-
is_0conf,
8035-
0,
8036-
8037-
counterparty_pubkeys,
8038-
channel_type,
8039-
holder_selected_channel_reserve_satoshis,
8040-
msg.channel_reserve_satoshis,
8041-
msg.push_msat,
8042-
msg.common_fields.clone(),
8043-
)?,
8044-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
8034+
let context = ChannelContext::new_for_inbound_channel(
8035+
fee_estimator,
8036+
entropy_source,
8037+
signer_provider,
8038+
counterparty_node_id,
8039+
their_features,
8040+
user_id,
8041+
config,
8042+
current_chain_height,
8043+
&&logger,
8044+
is_0conf,
8045+
0,
8046+
counterparty_pubkeys,
8047+
channel_type,
8048+
holder_selected_channel_reserve_satoshis,
8049+
msg.channel_reserve_satoshis,
8050+
msg.push_msat,
8051+
msg.common_fields.clone(),
8052+
)?;
8053+
let unfunded_context = UnfundedChannelContext {
8054+
unfunded_channel_age_ticks: 0,
8055+
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
80458056
};
8057+
8058+
let chan = Self { context, unfunded_context };
80468059
Ok(chan)
80478060
}
80488061

0 commit comments

Comments
 (0)