Skip to content

Commit 984d4da

Browse files
committed
Add holder commitment point to unfunded channel context
1 parent f8bdcdd commit 984d4da

File tree

1 file changed

+64
-53
lines changed

1 file changed

+64
-53
lines changed

lightning/src/ln/channel.rs

Lines changed: 64 additions & 53 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.
@@ -7643,28 +7649,31 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
76437649
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
76447650
let pubkeys = holder_signer.pubkeys().clone();
76457651

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 }
7652+
let context = ChannelContext::new_for_outbound_channel(
7653+
fee_estimator,
7654+
entropy_source,
7655+
signer_provider,
7656+
counterparty_node_id,
7657+
their_features,
7658+
channel_value_satoshis,
7659+
push_msat,
7660+
user_id,
7661+
config,
7662+
current_chain_height,
7663+
outbound_scid_alias,
7664+
temporary_channel_id,
7665+
holder_selected_channel_reserve_satoshis,
7666+
channel_keys_id,
7667+
holder_signer,
7668+
pubkeys,
7669+
logger,
7670+
)?;
7671+
let unfunded_context = UnfundedChannelContext {
7672+
unfunded_channel_age_ticks: 0,
7673+
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
76677674
};
7675+
7676+
let chan = Self { context, unfunded_context };
76687677
Ok(chan)
76697678
}
76707679

@@ -8020,29 +8029,31 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80208029
htlc_basepoint: HtlcBasepoint::from(msg.common_fields.htlc_basepoint)
80218030
};
80228031

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 }
8032+
let context = ChannelContext::new_for_inbound_channel(
8033+
fee_estimator,
8034+
entropy_source,
8035+
signer_provider,
8036+
counterparty_node_id,
8037+
their_features,
8038+
user_id,
8039+
config,
8040+
current_chain_height,
8041+
&&logger,
8042+
is_0conf,
8043+
0,
8044+
counterparty_pubkeys,
8045+
channel_type,
8046+
holder_selected_channel_reserve_satoshis,
8047+
msg.channel_reserve_satoshis,
8048+
msg.push_msat,
8049+
msg.common_fields.clone(),
8050+
)?;
8051+
let unfunded_context = UnfundedChannelContext {
8052+
unfunded_channel_age_ticks: 0,
8053+
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
80458054
};
8055+
8056+
let chan = Self { context, unfunded_context };
80468057
Ok(chan)
80478058
}
80488059

0 commit comments

Comments
 (0)