@@ -269,9 +269,11 @@ enum HTLCUpdateAwaitingACK {
269
269
/// `ChannelReady` can then get all remaining flags set on it, until we finish shutdown, then we
270
270
/// move on to `ShutdownComplete`, at which point most calls into this channel are disallowed.
271
271
enum ChannelState {
272
- /// Implies we have (or are prepared to) send our open_channel/accept_channel message
272
+ /// Implies we have (or are prepared to) send our open_channel/accept_channel message or in the
273
+ /// case of V2 establishment, our open_channel2/accept_channel2 message
273
274
OurInitSent = 1 << 0,
274
- /// Implies we have received their `open_channel`/`accept_channel` message
275
+ /// Implies we have received their `open_channel`/`accept_channel` message or in the case of
276
+ /// V2 establishment, their `open_channel2`/`accept_channel2` message
275
277
TheirInitSent = 1 << 1,
276
278
/// We have sent `funding_created` and are awaiting a `funding_signed` to advance to `FundingSent`.
277
279
/// Note that this is nonsense for an inbound channel as we immediately generate `funding_signed`
@@ -2651,6 +2653,20 @@ pub(crate) fn get_legacy_default_holder_selected_channel_reserve_satoshis(channe
2651
2653
cmp::min(channel_value_satoshis, cmp::max(q, 1000))
2652
2654
}
2653
2655
2656
+ /// Returns a minimum channel reserve value each party needs to maintain, fixed in the spec to a
2657
+ /// default of 1% of the total channel value.
2658
+ ///
2659
+ /// Guaranteed to return a value no larger than channel_value_satoshis
2660
+ ///
2661
+ /// This is used both for outbound and inbound channels and has lower bound
2662
+ /// of `dust_limit_satoshis`.
2663
+ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satoshis: u64) -> u64 {
2664
+ let channel_reserve_proportional_millionths = 10_000; // Fixed at 1% in spec.
2665
+ let calculated_reserve =
2666
+ channel_value_satoshis.saturating_mul(channel_reserve_proportional_millionths) / 1_000_000;
2667
+ cmp::min(channel_value_satoshis, cmp::max(calculated_reserve, dust_limit_satoshis))
2668
+ }
2669
+
2654
2670
// Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
2655
2671
// Note that num_htlcs should not include dust HTLCs.
2656
2672
#[inline]
@@ -2684,6 +2700,8 @@ pub(super) struct DualFundingChannelContext {
2684
2700
// Counterparty designates channel data owned by the another channel participant entity.
2685
2701
pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
2686
2702
pub context: ChannelContext<SP>,
2703
+ #[cfg(dual_funding)]
2704
+ pub dual_funding_channel_context: Option<DualFundingChannelContext>,
2687
2705
}
2688
2706
2689
2707
#[cfg(any(test, fuzzing))]
@@ -6617,6 +6635,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
6617
6635
6618
6636
let channel = Channel {
6619
6637
context: self.context,
6638
+ #[cfg(dual_funding)]
6639
+ dual_funding_channel_context: None,
6620
6640
};
6621
6641
6622
6642
Ok((channel, funding_created))
@@ -7089,6 +7109,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
7089
7109
// `ChannelMonitor`.
7090
7110
let mut channel = Channel {
7091
7111
context: self.context,
7112
+ #[cfg(dual_funding)]
7113
+ dual_funding_channel_context: None,
7092
7114
};
7093
7115
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
7094
7116
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
@@ -7097,6 +7119,159 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
7097
7119
}
7098
7120
}
7099
7121
7122
+ // A not-yet-funded inbound (from counterparty) channel using V2 channel establishment.
7123
+ #[cfg(dual_funding)]
7124
+ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
7125
+ pub context: ChannelContext<SP>,
7126
+ pub unfunded_context: UnfundedChannelContext,
7127
+ pub dual_funding_context: DualFundingChannelContext,
7128
+ }
7129
+
7130
+ #[cfg(dual_funding)]
7131
+ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
7132
+ /// Creates a new dual-funded channel from a remote side's request for one.
7133
+ /// Assumes chain_hash has already been checked and corresponds with what we expect!
7134
+ pub fn new<ES: Deref, F: Deref, L: Deref>(
7135
+ fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
7136
+ counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
7137
+ their_features: &InitFeatures, msg: &msgs::OpenChannelV2, funding_satoshis: u64, user_id: u128,
7138
+ config: &UserConfig, current_chain_height: u32, logger: &L,
7139
+ ) -> Result<InboundV2Channel<SP>, ChannelError>
7140
+ where ES::Target: EntropySource,
7141
+ F::Target: FeeEstimator,
7142
+ L::Target: Logger,
7143
+ {
7144
+ // TODO(dual_funding): Fix this
7145
+ let channel_value_satoshis = funding_satoshis * 1000 + msg.funding_satoshis;
7146
+ let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
7147
+ channel_value_satoshis, msg.dust_limit_satoshis);
7148
+ let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
7149
+ channel_value_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS);
7150
+
7151
+ let counterparty_pubkeys = ChannelPublicKeys {
7152
+ funding_pubkey: msg.funding_pubkey,
7153
+ revocation_basepoint: RevocationBasepoint(msg.revocation_basepoint),
7154
+ payment_point: msg.payment_basepoint,
7155
+ delayed_payment_basepoint: DelayedPaymentBasepoint(msg.delayed_payment_basepoint),
7156
+ htlc_basepoint: HtlcBasepoint(msg.htlc_basepoint)
7157
+ };
7158
+
7159
+ let mut context = ChannelContext::new_for_inbound_channel(
7160
+ fee_estimator,
7161
+ entropy_source,
7162
+ signer_provider,
7163
+ counterparty_node_id,
7164
+ our_supported_features,
7165
+ their_features,
7166
+ user_id,
7167
+ config,
7168
+ current_chain_height,
7169
+ logger,
7170
+ false,
7171
+
7172
+ funding_satoshis,
7173
+
7174
+ counterparty_pubkeys,
7175
+ msg.channel_flags,
7176
+ msg.channel_type.clone(),
7177
+ msg.funding_satoshis,
7178
+ msg.to_self_delay,
7179
+ holder_selected_channel_reserve_satoshis,
7180
+ counterparty_selected_channel_reserve_satoshis,
7181
+ 0 /* push_msat not used in dual-funding */,
7182
+ msg.dust_limit_satoshis,
7183
+ msg.htlc_minimum_msat,
7184
+ msg.commitment_feerate_sat_per_1000_weight,
7185
+ msg.max_accepted_htlcs,
7186
+ msg.shutdown_scriptpubkey.clone(),
7187
+ msg.max_htlc_value_in_flight_msat,
7188
+ msg.temporary_channel_id,
7189
+ msg.first_per_commitment_point,
7190
+ )?;
7191
+ let channel_id = ChannelId::v2_from_revocation_basepoints(
7192
+ &context.get_holder_pubkeys().revocation_basepoint,
7193
+ &context.get_counterparty_pubkeys().revocation_basepoint);
7194
+ context.channel_id = channel_id;
7195
+
7196
+ let chan = Self {
7197
+ context,
7198
+ unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
7199
+ dual_funding_context: DualFundingChannelContext {
7200
+ our_funding_satoshis: funding_satoshis,
7201
+ their_funding_satoshis: msg.funding_satoshis,
7202
+ funding_tx_locktime: msg.locktime,
7203
+ funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
7204
+ }
7205
+ };
7206
+
7207
+ Ok(chan)
7208
+ }
7209
+
7210
+ /// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannelV2`] message which
7211
+ /// should be sent back to the counterparty node.
7212
+ ///
7213
+ /// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7214
+ pub fn accept_inbound_dual_funded_channel(&mut self) -> msgs::AcceptChannelV2 {
7215
+ if self.context.is_outbound() {
7216
+ panic!("Tried to send accept_channel2 for an outbound channel?");
7217
+ }
7218
+ if self.context.channel_state != (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32) {
7219
+ panic!("Tried to send accept_channel2 after channel had moved forward");
7220
+ }
7221
+ if self.context.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
7222
+ panic!("Tried to send an accept_channel2 for a channel that has already advanced");
7223
+ }
7224
+
7225
+ self.generate_accept_channel_v2_message()
7226
+ }
7227
+
7228
+ /// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
7229
+ /// inbound channel. If the intention is to accept an inbound channel, use
7230
+ /// [`InboundV1Channel::accept_inbound_channel`] instead.
7231
+ ///
7232
+ /// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7233
+ fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
7234
+ let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
7235
+ self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
7236
+ let second_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
7237
+ self.context.cur_holder_commitment_transaction_number - 1, &self.context.secp_ctx);
7238
+ let keys = self.context.get_holder_pubkeys();
7239
+
7240
+ msgs::AcceptChannelV2 {
7241
+ temporary_channel_id: self.context.temporary_channel_id.unwrap(),
7242
+ funding_satoshis: self.dual_funding_context.our_funding_satoshis,
7243
+ dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
7244
+ max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
7245
+ htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
7246
+ minimum_depth: self.context.minimum_depth.unwrap(),
7247
+ to_self_delay: self.context.get_holder_selected_contest_delay(),
7248
+ max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
7249
+ funding_pubkey: keys.funding_pubkey,
7250
+ revocation_basepoint: keys.revocation_basepoint.to_public_key(),
7251
+ payment_basepoint: keys.payment_point,
7252
+ delayed_payment_basepoint: keys.delayed_payment_basepoint.to_public_key(),
7253
+ htlc_basepoint: keys.htlc_basepoint.to_public_key(),
7254
+ first_per_commitment_point,
7255
+ second_per_commitment_point,
7256
+ shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
7257
+ Some(script) => script.clone().into_inner(),
7258
+ None => Builder::new().into_script(),
7259
+ }),
7260
+ channel_type: Some(self.context.channel_type.clone()),
7261
+ require_confirmed_inputs: None,
7262
+ }
7263
+ }
7264
+
7265
+ /// Enables the possibility for tests to extract a [`msgs::AcceptChannelV2`] message for an
7266
+ /// inbound channel without accepting it.
7267
+ ///
7268
+ /// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
7269
+ #[cfg(test)]
7270
+ pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
7271
+ self.generate_accept_channel_v2_message()
7272
+ }
7273
+ }
7274
+
7100
7275
const SERIALIZATION_VERSION: u8 = 3;
7101
7276
const MIN_SERIALIZATION_VERSION: u8 = 3;
7102
7277
@@ -8006,7 +8181,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
8006
8181
channel_keys_id,
8007
8182
8008
8183
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
8009
- }
8184
+ },
8185
+ #[cfg(dual_funding)]
8186
+ dual_funding_channel_context: None,
8010
8187
})
8011
8188
}
8012
8189
}
@@ -8564,7 +8741,11 @@ mod tests {
8564
8741
let config = UserConfig::default();
8565
8742
let features = channelmanager::provided_init_features(&config);
8566
8743
let outbound_chan = OutboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &features, 10000000, 100000, 42, &config, 0, 42, None).unwrap();
8567
- let mut chan = Channel { context: outbound_chan.context };
8744
+ let mut chan = Channel {
8745
+ context: outbound_chan.context,
8746
+ #[cfg(dual_funding)]
8747
+ dual_funding_channel_context: None,
8748
+ };
8568
8749
8569
8750
let dummy_htlc_source = HTLCSource::OutboundRoute {
8570
8751
path: Path {
0 commit comments