@@ -111,6 +111,7 @@ enum FeeUpdateState {
111111 Outbound,
112112}
113113
114+ #[derive(Clone)]
114115enum InboundHTLCRemovalReason {
115116 FailRelay(msgs::OnionErrorPacket),
116117 FailMalformed(([u8; 32], u16)),
@@ -145,6 +146,7 @@ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
145146 },
146147);
147148
149+ #[derive(Clone)]
148150enum InboundHTLCState {
149151 /// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
150152 /// update_add_htlc message for this HTLC.
@@ -219,6 +221,7 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
219221 }
220222}
221223
224+ #[derive(Clone)]
222225struct InboundHTLCOutput {
223226 htlc_id: u64,
224227 amount_msat: u64,
@@ -227,7 +230,8 @@ struct InboundHTLCOutput {
227230 state: InboundHTLCState,
228231}
229232
230- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
233+ #[derive(Clone)]
234+ #[cfg_attr(test, derive(Debug, PartialEq))]
231235enum OutboundHTLCState {
232236 /// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
233237 /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -309,7 +313,8 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
309313 }
310314}
311315
312- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
316+ #[derive(Clone)]
317+ #[cfg_attr(test, derive(Debug, PartialEq))]
313318struct OutboundHTLCOutput {
314319 htlc_id: u64,
315320 amount_msat: u64,
@@ -322,7 +327,8 @@ struct OutboundHTLCOutput {
322327}
323328
324329/// See AwaitingRemoteRevoke ChannelState for more info
325- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
330+ #[derive(Clone)]
331+ #[cfg_attr(test, derive(Debug, PartialEq))]
326332enum HTLCUpdateAwaitingACK {
327333 AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
328334 // always outbound
@@ -797,7 +803,7 @@ pub(super) enum ChannelUpdateStatus {
797803}
798804
799805/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
800- #[derive(PartialEq)]
806+ #[derive(Clone, PartialEq)]
801807pub enum AnnouncementSigsState {
802808 /// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
803809 /// we sent the last `AnnouncementSignatures`.
@@ -1115,6 +1121,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11151121/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11161122pub(crate) const COINBASE_MATURITY: u32 = 100;
11171123
1124+ #[derive(Clone)]
11181125struct PendingChannelMonitorUpdate {
11191126 update: ChannelMonitorUpdate,
11201127}
@@ -4289,6 +4296,113 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42894296 self.get_initial_counterparty_commitment_signature(logger)
42904297 }
42914298
4299+ /// Clone, each field, with a few exceptions, notably the channel signer, and
4300+ /// a few non-cloneable fields (such as Secp256k1 context)
4301+ #[allow(unused)]
4302+ fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4303+ Self {
4304+ config: self.config,
4305+ prev_config: self.prev_config,
4306+ inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4307+ user_id: self.user_id,
4308+ channel_id: self.channel_id,
4309+ temporary_channel_id: self.temporary_channel_id,
4310+ channel_state: self.channel_state,
4311+ announcement_sigs_state: self.announcement_sigs_state.clone(),
4312+ // Create new Secp256k context
4313+ secp_ctx: Secp256k1::new(),
4314+ channel_value_satoshis: self.channel_value_satoshis,
4315+ latest_monitor_update_id: self.latest_monitor_update_id,
4316+ // Use provided channel signer
4317+ holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4318+ shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4319+ destination_script: self.destination_script.clone(),
4320+ holder_commitment_point: self.holder_commitment_point,
4321+ cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4322+ value_to_self_msat: self.value_to_self_msat,
4323+ pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4324+ pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4325+ holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4326+ resend_order: self.resend_order.clone(),
4327+ monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4328+ monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4329+ monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4330+ monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4331+ monitor_pending_failures: self.monitor_pending_failures.clone(),
4332+ monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4333+ monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4334+ monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4335+ signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4336+ signer_pending_commitment_update: self.signer_pending_commitment_update,
4337+ signer_pending_funding: self.signer_pending_funding,
4338+ signer_pending_closing: self.signer_pending_closing,
4339+ pending_update_fee: self.pending_update_fee,
4340+ holding_cell_update_fee: self.holding_cell_update_fee,
4341+ next_holder_htlc_id: self.next_holder_htlc_id,
4342+ next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4343+ feerate_per_kw: self.feerate_per_kw,
4344+ update_time_counter: self.update_time_counter,
4345+ // Create new mutex with copied values
4346+ #[cfg(debug_assertions)]
4347+ holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4348+ #[cfg(debug_assertions)]
4349+ counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4350+ last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4351+ last_received_closing_sig: self.last_received_closing_sig,
4352+ target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4353+ pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4354+ closing_fee_limits: self.closing_fee_limits,
4355+ expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4356+ funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4357+ funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4358+ short_channel_id: self.short_channel_id,
4359+ channel_creation_height: self.channel_creation_height,
4360+ counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4361+ holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4362+ counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4363+ holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4364+ counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4365+ holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4366+ counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4367+ holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4368+ counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4369+ holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4370+ minimum_depth: self.minimum_depth,
4371+ counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4372+ channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4373+ funding_transaction: self.funding_transaction.clone(),
4374+ is_manual_broadcast: self.is_manual_broadcast,
4375+ is_batch_funding: self.is_batch_funding,
4376+ counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4377+ counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4378+ counterparty_node_id: self.counterparty_node_id,
4379+ counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4380+ commitment_secrets: self.commitment_secrets.clone(),
4381+ channel_update_status: self.channel_update_status,
4382+ closing_signed_in_flight: self.closing_signed_in_flight,
4383+ announcement_sigs: self.announcement_sigs,
4384+ // Create new mutex with copied values
4385+ #[cfg(any(test, fuzzing))]
4386+ next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4387+ #[cfg(any(test, fuzzing))]
4388+ next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4389+ workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4390+ sent_message_awaiting_response: self.sent_message_awaiting_response,
4391+ #[cfg(any(test, fuzzing))]
4392+ historical_inbound_htlc_fulfills: self.historical_inbound_htlc_fulfills.clone(),
4393+ channel_type: self.channel_type.clone(),
4394+ latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4395+ outbound_scid_alias: self.outbound_scid_alias,
4396+ channel_pending_event_emitted: self.channel_pending_event_emitted,
4397+ funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4398+ channel_ready_event_emitted: self.channel_ready_event_emitted,
4399+ local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4400+ channel_keys_id: self.channel_keys_id,
4401+ blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4402+ next_funding_txid: self.next_funding_txid.clone(),
4403+ }
4404+ }
4405+
42924406 /// Splice process starting; update state, log, etc.
42934407 #[cfg(splicing)]
42944408 pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
@@ -4463,6 +4577,7 @@ pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
44634577}
44644578
44654579#[cfg(any(test, fuzzing))]
4580+ #[derive(Clone)]
44664581struct CommitmentTxInfoCached {
44674582 fee: u64,
44684583 total_pending_htlcs: usize,
@@ -10564,7 +10679,7 @@ mod tests {
1056410679 use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1056510680 use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1056610681 use crate::ln::channel::InitFeatures;
10567- use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
10682+ use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelContext, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1056810683 use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1056910684 use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1057010685 use crate::ln::msgs;
@@ -12329,6 +12444,57 @@ mod tests {
1232912444 assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1233012445 }
1233112446
12447+ #[test]
12448+ fn channel_context_clone() {
12449+ let fee_estimator = TestFeeEstimator {fee_est: 253 };
12450+ let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
12451+ let seed = [42; 32];
12452+ let network = Network::Testnet;
12453+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
12454+ let secp_ctx = Secp256k1::new();
12455+ let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
12456+ let config = UserConfig::default();
12457+
12458+ let signer_provider: &TestKeysInterface = &&keys_provider;
12459+ let channel_value_satoshis = 10000000;
12460+ let user_id = 42;
12461+ let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
12462+ let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12463+ let logger = test_utils::TestLogger::new();
12464+ let pubkeys = holder_signer.pubkeys().clone();
12465+
12466+ // Create a context
12467+ let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
12468+ &bounded_fee_estimator,
12469+ &&keys_provider,
12470+ &signer_provider,
12471+ node_a_node_id,
12472+ &channelmanager::provided_init_features(&config),
12473+ channel_value_satoshis,
12474+ 100000,
12475+ user_id,
12476+ &config,
12477+ 0,
12478+ 42,
12479+ None,
12480+ 100000,
12481+ [42; 32],
12482+ holder_signer,
12483+ pubkeys,
12484+ &logger,
12485+ ).unwrap();
12486+
12487+ // Clone it
12488+ let holder_signer2 = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12489+ let context_cloned = context.clone(holder_signer2);
12490+
12491+ // Compare some fields
12492+ assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
12493+ assert_eq!(context_cloned.channel_id, context.channel_id);
12494+ assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
12495+ assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
12496+ }
12497+
1233212498 #[cfg(all(test, splicing))]
1233312499 fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1233412500 use crate::ln::channel::PendingSplice;
0 commit comments