Skip to content

Commit eab111d

Browse files
committed
Add InboundV2Channel struct
1 parent fd5c10d commit eab111d

File tree

1 file changed

+203
-4
lines changed

1 file changed

+203
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 203 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,6 +2871,20 @@ pub(crate) fn get_legacy_default_holder_selected_channel_reserve_satoshis(channe
28712871
cmp::min(channel_value_satoshis, cmp::max(q, 1000))
28722872
}
28732873

2874+
/// Returns a minimum channel reserve value each party needs to maintain, fixed in the spec to a
2875+
/// default of 1% of the total channel value.
2876+
///
2877+
/// Guaranteed to return a value no larger than channel_value_satoshis
2878+
///
2879+
/// This is used both for outbound and inbound channels and has lower bound
2880+
/// of `dust_limit_satoshis`.
2881+
fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satoshis: u64) -> u64 {
2882+
let channel_reserve_proportional_millionths = 10_000; // Fixed at 1% in spec.
2883+
let calculated_reserve =
2884+
channel_value_satoshis.saturating_mul(channel_reserve_proportional_millionths) / 1_000_000;
2885+
cmp::min(channel_value_satoshis, cmp::max(calculated_reserve, dust_limit_satoshis))
2886+
}
2887+
28742888
// Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
28752889
// Note that num_htlcs should not include dust HTLCs.
28762890
#[inline]
@@ -2904,6 +2918,8 @@ pub(super) struct DualFundingChannelContext {
29042918
// Counterparty designates channel data owned by the another channel participant entity.
29052919
pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
29062920
pub context: ChannelContext<SP>,
2921+
#[cfg(dual_funding)]
2922+
pub dual_funding_channel_context: Option<DualFundingChannelContext>,
29072923
}
29082924

29092925
#[cfg(any(test, fuzzing))]
@@ -7219,7 +7235,11 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
72197235

72207236
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
72217237

7222-
let mut channel = Channel { context: self.context };
7238+
let mut channel = Channel {
7239+
context: self.context,
7240+
#[cfg(dual_funding)]
7241+
dual_funding_channel_context: None,
7242+
};
72237243

72247244
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
72257245
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
@@ -7249,7 +7269,23 @@ pub(super) fn channel_type_from_open_channel(
72497269
msg: &msgs::OpenChannel, their_features: &InitFeatures,
72507270
our_supported_features: &ChannelTypeFeatures
72517271
) -> Result<ChannelTypeFeatures, ChannelError> {
7252-
if let Some(channel_type) = &msg.channel_type {
7272+
channel_type_from_type_and_flags(&msg.channel_type, msg.channel_flags, their_features, our_supported_features)
7273+
}
7274+
7275+
/// Fetches the [`ChannelTypeFeatures`] that will be used for a channel built from a given
7276+
/// [`msgs::OpenChannelV2`].
7277+
#[cfg(dual_funding)]
7278+
pub(super) fn channel_type_from_open_channel_v2(
7279+
msg: &msgs::OpenChannelV2, their_features: &InitFeatures,
7280+
our_supported_features: &ChannelTypeFeatures
7281+
) -> Result<ChannelTypeFeatures, ChannelError> {
7282+
channel_type_from_type_and_flags(&msg.channel_type, msg.channel_flags, their_features, our_supported_features)
7283+
}
7284+
7285+
fn channel_type_from_type_and_flags(msg_channel_type: &Option<ChannelTypeFeatures>,
7286+
msg_channel_flags: u8, their_features: &InitFeatures, our_supported_features: &ChannelTypeFeatures
7287+
) -> Result<ChannelTypeFeatures, ChannelError> {
7288+
if let Some(channel_type) = &msg_channel_type {
72537289
if channel_type.supports_any_optional_bits() {
72547290
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
72557291
}
@@ -7264,7 +7300,7 @@ pub(super) fn channel_type_from_open_channel(
72647300
if !channel_type.is_subset(our_supported_features) {
72657301
return Err(ChannelError::Close("Channel Type contains unsupported features".to_owned()));
72667302
}
7267-
let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
7303+
let announced_channel = if (msg_channel_flags & 1) == 1 { true } else { false };
72687304
if channel_type.requires_scid_privacy() && announced_channel {
72697305
return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
72707306
}
@@ -7515,6 +7551,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
75157551
// `ChannelMonitor`.
75167552
let mut channel = Channel {
75177553
context: self.context,
7554+
#[cfg(dual_funding)]
7555+
dual_funding_channel_context: None,
75187556
};
75197557
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
75207558
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
@@ -7523,6 +7561,165 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
75237561
}
75247562
}
75257563

7564+
// A not-yet-funded inbound (from counterparty) channel using V2 channel establishment.
7565+
#[cfg(dual_funding)]
7566+
pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
7567+
pub context: ChannelContext<SP>,
7568+
pub unfunded_context: UnfundedChannelContext,
7569+
pub dual_funding_context: DualFundingChannelContext,
7570+
}
7571+
7572+
#[cfg(dual_funding)]
7573+
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
7574+
/// Creates a new dual-funded channel from a remote side's request for one.
7575+
/// Assumes chain_hash has already been checked and corresponds with what we expect!
7576+
pub fn new<ES: Deref, F: Deref, L: Deref>(
7577+
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
7578+
counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
7579+
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, funding_satoshis: u64, user_id: u128,
7580+
config: &UserConfig, current_chain_height: u32, logger: &L,
7581+
) -> Result<InboundV2Channel<SP>, ChannelError>
7582+
where ES::Target: EntropySource,
7583+
F::Target: FeeEstimator,
7584+
L::Target: Logger,
7585+
{
7586+
// TODO(dual_funding): Fix this
7587+
let channel_value_satoshis = funding_satoshis * 1000 + msg.funding_satoshis;
7588+
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
7589+
channel_value_satoshis, msg.dust_limit_satoshis);
7590+
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
7591+
channel_value_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS);
7592+
7593+
// First check the channel type is known, failing before we do anything else if we don't
7594+
// support this channel type.
7595+
let channel_type = channel_type_from_open_channel_v2(msg, their_features, our_supported_features)?;
7596+
7597+
let counterparty_pubkeys = ChannelPublicKeys {
7598+
funding_pubkey: msg.funding_pubkey,
7599+
revocation_basepoint: RevocationBasepoint(msg.revocation_basepoint),
7600+
payment_point: msg.payment_basepoint,
7601+
delayed_payment_basepoint: DelayedPaymentBasepoint(msg.delayed_payment_basepoint),
7602+
htlc_basepoint: HtlcBasepoint(msg.htlc_basepoint)
7603+
};
7604+
7605+
let mut context = ChannelContext::new_for_inbound_channel(
7606+
fee_estimator,
7607+
entropy_source,
7608+
signer_provider,
7609+
counterparty_node_id,
7610+
their_features,
7611+
user_id,
7612+
config,
7613+
current_chain_height,
7614+
logger,
7615+
false,
7616+
7617+
funding_satoshis,
7618+
7619+
counterparty_pubkeys,
7620+
msg.channel_flags,
7621+
channel_type,
7622+
msg.funding_satoshis,
7623+
msg.to_self_delay,
7624+
holder_selected_channel_reserve_satoshis,
7625+
counterparty_selected_channel_reserve_satoshis,
7626+
0 /* push_msat not used in dual-funding */,
7627+
msg.dust_limit_satoshis,
7628+
msg.htlc_minimum_msat,
7629+
msg.commitment_feerate_sat_per_1000_weight,
7630+
msg.max_accepted_htlcs,
7631+
msg.shutdown_scriptpubkey.clone(),
7632+
msg.max_htlc_value_in_flight_msat,
7633+
msg.temporary_channel_id,
7634+
msg.first_per_commitment_point,
7635+
)?;
7636+
let channel_id = ChannelId::v2_from_revocation_basepoints(
7637+
&context.get_holder_pubkeys().revocation_basepoint,
7638+
&context.get_counterparty_pubkeys().revocation_basepoint);
7639+
context.channel_id = channel_id;
7640+
7641+
let chan = Self {
7642+
context,
7643+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
7644+
dual_funding_context: DualFundingChannelContext {
7645+
our_funding_satoshis: funding_satoshis,
7646+
their_funding_satoshis: msg.funding_satoshis,
7647+
funding_tx_locktime: msg.locktime,
7648+
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
7649+
}
7650+
};
7651+
7652+
Ok(chan)
7653+
}
7654+
7655+
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannelV2`] message which
7656+
/// should be sent back to the counterparty node.
7657+
///
7658+
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7659+
pub fn accept_inbound_dual_funded_channel(&mut self) -> msgs::AcceptChannelV2 {
7660+
if self.context.is_outbound() {
7661+
panic!("Tried to send accept_channel for an outbound channel?");
7662+
}
7663+
if !matches!(
7664+
self.context.channel_state, ChannelState::NegotiatingFunding(flags)
7665+
if flags == (NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT)
7666+
) {
7667+
panic!("Tried to send accept_channel2 after channel had moved forward");
7668+
}
7669+
if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7670+
panic!("Tried to send an accept_channel2 for a channel that has already advanced");
7671+
}
7672+
7673+
self.generate_accept_channel_v2_message()
7674+
}
7675+
7676+
/// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
7677+
/// inbound channel. If the intention is to accept an inbound channel, use
7678+
/// [`InboundV1Channel::accept_inbound_channel`] instead.
7679+
///
7680+
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7681+
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
7682+
let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
7683+
self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
7684+
let second_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
7685+
self.context.cur_holder_commitment_transaction_number - 1, &self.context.secp_ctx);
7686+
let keys = self.context.get_holder_pubkeys();
7687+
7688+
msgs::AcceptChannelV2 {
7689+
temporary_channel_id: self.context.temporary_channel_id.unwrap(),
7690+
funding_satoshis: self.dual_funding_context.our_funding_satoshis,
7691+
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
7692+
max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
7693+
htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
7694+
minimum_depth: self.context.minimum_depth.unwrap(),
7695+
to_self_delay: self.context.get_holder_selected_contest_delay(),
7696+
max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
7697+
funding_pubkey: keys.funding_pubkey,
7698+
revocation_basepoint: keys.revocation_basepoint.to_public_key(),
7699+
payment_basepoint: keys.payment_point,
7700+
delayed_payment_basepoint: keys.delayed_payment_basepoint.to_public_key(),
7701+
htlc_basepoint: keys.htlc_basepoint.to_public_key(),
7702+
first_per_commitment_point,
7703+
second_per_commitment_point,
7704+
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
7705+
Some(script) => script.clone().into_inner(),
7706+
None => Builder::new().into_script(),
7707+
}),
7708+
channel_type: Some(self.context.channel_type.clone()),
7709+
require_confirmed_inputs: None,
7710+
}
7711+
}
7712+
7713+
/// Enables the possibility for tests to extract a [`msgs::AcceptChannelV2`] message for an
7714+
/// inbound channel without accepting it.
7715+
///
7716+
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7717+
#[cfg(test)]
7718+
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
7719+
self.generate_accept_channel_v2_message()
7720+
}
7721+
}
7722+
75267723
const SERIALIZATION_VERSION: u8 = 3;
75277724
const MIN_SERIALIZATION_VERSION: u8 = 3;
75287725

@@ -8460,7 +8657,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
84608657
channel_keys_id,
84618658

84628659
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
8463-
}
8660+
},
8661+
#[cfg(dual_funding)]
8662+
dual_funding_channel_context: None,
84648663
})
84658664
}
84668665
}

0 commit comments

Comments
 (0)