@@ -112,6 +112,7 @@ enum FeeUpdateState {
112112 Outbound,
113113}
114114
115+ #[derive(Clone)]
115116enum InboundHTLCRemovalReason {
116117 FailRelay(msgs::OnionErrorPacket),
117118 FailMalformed(([u8; 32], u16)),
@@ -146,6 +147,7 @@ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
146147 },
147148);
148149
150+ #[derive(Clone)]
149151enum InboundHTLCState {
150152 /// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
151153 /// update_add_htlc message for this HTLC.
@@ -220,6 +222,7 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
220222 }
221223}
222224
225+ #[derive(Clone)]
223226struct InboundHTLCOutput {
224227 htlc_id: u64,
225228 amount_msat: u64,
@@ -228,7 +231,8 @@ struct InboundHTLCOutput {
228231 state: InboundHTLCState,
229232}
230233
231- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
234+ #[derive(Clone)]
235+ #[cfg_attr(test, derive(Debug, PartialEq))]
232236enum OutboundHTLCState {
233237 /// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
234238 /// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -310,7 +314,8 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
310314 }
311315}
312316
313- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
317+ #[derive(Clone)]
318+ #[cfg_attr(test, derive(Debug, PartialEq))]
314319struct OutboundHTLCOutput {
315320 htlc_id: u64,
316321 amount_msat: u64,
@@ -323,7 +328,8 @@ struct OutboundHTLCOutput {
323328}
324329
325330/// See AwaitingRemoteRevoke ChannelState for more info
326- #[cfg_attr(test, derive(Clone, Debug, PartialEq))]
331+ #[derive(Clone)]
332+ #[cfg_attr(test, derive(Debug, PartialEq))]
327333enum HTLCUpdateAwaitingACK {
328334 AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
329335 // always outbound
@@ -798,7 +804,7 @@ pub(super) enum ChannelUpdateStatus {
798804}
799805
800806/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
801- #[derive(PartialEq)]
807+ #[derive(Clone, PartialEq)]
802808pub enum AnnouncementSigsState {
803809 /// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
804810 /// we sent the last `AnnouncementSignatures`.
@@ -1116,6 +1122,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11161122/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11171123pub(crate) const COINBASE_MATURITY: u32 = 100;
11181124
1125+ #[derive(Clone)]
11191126struct PendingChannelMonitorUpdate {
11201127 update: ChannelMonitorUpdate,
11211128}
@@ -4290,6 +4297,113 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42904297 self.channel_transaction_parameters = channel_transaction_parameters;
42914298 self.get_initial_counterparty_commitment_signature(logger)
42924299 }
4300+
4301+ /// Clone, each field, with a few exceptions, notably the channel signer, and
4302+ /// a few non-cloneable fields (such as Secp256k1 context)
4303+ #[allow(unused)]
4304+ fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4305+ Self {
4306+ config: self.config,
4307+ prev_config: self.prev_config,
4308+ inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4309+ user_id: self.user_id,
4310+ channel_id: self.channel_id,
4311+ temporary_channel_id: self.temporary_channel_id,
4312+ channel_state: self.channel_state,
4313+ announcement_sigs_state: self.announcement_sigs_state.clone(),
4314+ // Create new Secp256k context
4315+ secp_ctx: Secp256k1::new(),
4316+ channel_value_satoshis: self.channel_value_satoshis,
4317+ latest_monitor_update_id: self.latest_monitor_update_id,
4318+ // Use provided channel signer
4319+ holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4320+ shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4321+ destination_script: self.destination_script.clone(),
4322+ cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4323+ value_to_self_msat: self.value_to_self_msat,
4324+ pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4325+ pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4326+ holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4327+ resend_order: self.resend_order.clone(),
4328+ monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4329+ monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4330+ monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4331+ monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4332+ monitor_pending_failures: self.monitor_pending_failures.clone(),
4333+ monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4334+ monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4335+ monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4336+ signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4337+ signer_pending_commitment_update: self.signer_pending_commitment_update,
4338+ signer_pending_funding: self.signer_pending_funding,
4339+ signer_pending_closing: self.signer_pending_closing,
4340+ signer_pending_channel_ready: self.signer_pending_channel_ready,
4341+ pending_update_fee: self.pending_update_fee,
4342+ holding_cell_update_fee: self.holding_cell_update_fee,
4343+ next_holder_htlc_id: self.next_holder_htlc_id,
4344+ next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4345+ feerate_per_kw: self.feerate_per_kw,
4346+ update_time_counter: self.update_time_counter,
4347+ // Create new mutex with copied values
4348+ #[cfg(debug_assertions)]
4349+ holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4350+ #[cfg(debug_assertions)]
4351+ counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4352+ last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4353+ last_received_closing_sig: self.last_received_closing_sig,
4354+ target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4355+ pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4356+ closing_fee_limits: self.closing_fee_limits,
4357+ expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4358+ funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4359+ funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4360+ short_channel_id: self.short_channel_id,
4361+ channel_creation_height: self.channel_creation_height,
4362+ counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4363+ holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4364+ counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4365+ holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4366+ counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4367+ holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4368+ counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4369+ holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4370+ counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4371+ holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4372+ minimum_depth: self.minimum_depth,
4373+ counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4374+ channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4375+ funding_transaction: self.funding_transaction.clone(),
4376+ is_manual_broadcast: self.is_manual_broadcast,
4377+ is_batch_funding: self.is_batch_funding,
4378+ counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4379+ counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4380+ counterparty_node_id: self.counterparty_node_id,
4381+ counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4382+ commitment_secrets: self.commitment_secrets.clone(),
4383+ channel_update_status: self.channel_update_status,
4384+ closing_signed_in_flight: self.closing_signed_in_flight,
4385+ announcement_sigs: self.announcement_sigs,
4386+ // Create new mutex with copied values
4387+ #[cfg(any(test, fuzzing))]
4388+ next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4389+ #[cfg(any(test, fuzzing))]
4390+ next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4391+ workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4392+ sent_message_awaiting_response: self.sent_message_awaiting_response,
4393+ #[cfg(any(test, fuzzing))]
4394+ historical_inbound_htlc_fulfills: self.historical_inbound_htlc_fulfills.clone(),
4395+ channel_type: self.channel_type.clone(),
4396+ latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4397+ outbound_scid_alias: self.outbound_scid_alias,
4398+ channel_pending_event_emitted: self.channel_pending_event_emitted,
4399+ funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4400+ channel_ready_event_emitted: self.channel_ready_event_emitted,
4401+ local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4402+ channel_keys_id: self.channel_keys_id,
4403+ blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4404+ next_funding_txid: self.next_funding_txid.clone(),
4405+ }
4406+ }
42934407}
42944408
42954409// Internal utility functions for channels
@@ -4417,6 +4531,7 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
44174531}
44184532
44194533#[cfg(any(test, fuzzing))]
4534+ #[derive(Clone)]
44204535struct CommitmentTxInfoCached {
44214536 fee: u64,
44224537 total_pending_htlcs: usize,
@@ -10446,7 +10561,7 @@ mod tests {
1044610561 use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1044710562 use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1044810563 use crate::ln::channel::InitFeatures;
10449- use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
10564+ use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelContext, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1045010565 use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1045110566 use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1045210567 use crate::ln::msgs;
@@ -12210,4 +12325,55 @@ mod tests {
1221012325 assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
1221112326 assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1221212327 }
12328+
12329+ #[test]
12330+ fn channel_context_clone() {
12331+ let fee_estimator = TestFeeEstimator {fee_est: 253 };
12332+ let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
12333+ let seed = [42; 32];
12334+ let network = Network::Testnet;
12335+ let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
12336+ let secp_ctx = Secp256k1::new();
12337+ let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
12338+ let config = UserConfig::default();
12339+
12340+ let signer_provider: &TestKeysInterface = &&keys_provider;
12341+ let channel_value_satoshis = 10000000;
12342+ let user_id = 42;
12343+ let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
12344+ let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12345+ let logger = test_utils::TestLogger::new();
12346+ let pubkeys = holder_signer.pubkeys().clone();
12347+
12348+ // Create a context
12349+ let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
12350+ &bounded_fee_estimator,
12351+ &&keys_provider,
12352+ &signer_provider,
12353+ node_a_node_id,
12354+ &channelmanager::provided_init_features(&config),
12355+ channel_value_satoshis,
12356+ 100000,
12357+ user_id,
12358+ &config,
12359+ 0,
12360+ 42,
12361+ None,
12362+ 100000,
12363+ [42; 32],
12364+ holder_signer,
12365+ pubkeys,
12366+ &logger,
12367+ ).unwrap();
12368+
12369+ // Clone it
12370+ let holder_signer2 = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12371+ let context_cloned = context.clone(holder_signer2);
12372+
12373+ // Compare some fields
12374+ assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
12375+ assert_eq!(context_cloned.channel_id, context.channel_id);
12376+ assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
12377+ assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
12378+ }
1221312379}
0 commit comments