diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 5b3ae0c5a12..597d8232a22 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -69,7 +69,7 @@ use crate::prelude::*; use core::{cmp,mem,fmt}; use core::ops::Deref; #[cfg(any(test, fuzzing, debug_assertions))] -use crate::sync::Mutex; +use crate::sync::{Arc, Mutex}; use crate::sign::type_resolver::ChannelSignerType; use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint}; @@ -1319,10 +1319,10 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { #[cfg(debug_assertions)] /// Max to_local and to_remote outputs in a locally-generated commitment transaction - holder_max_commitment_tx_output: Mutex<(u64, u64)>, + holder_max_commitment_tx_output: Arc>, #[cfg(debug_assertions)] /// Max to_local and to_remote outputs in a remote-generated commitment transaction - counterparty_max_commitment_tx_output: Mutex<(u64, u64)>, + counterparty_max_commitment_tx_output: Arc>, // (fee_sats, skip_remote_output, fee_range, holder_sig) last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option)>, @@ -1434,9 +1434,9 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { // be, by comparing the cached values to the fee of the tranaction generated by // `build_commitment_transaction`. #[cfg(any(test, fuzzing))] - next_local_commitment_tx_fee_info_cached: Mutex>, + next_local_commitment_tx_fee_info_cached: Arc>>, #[cfg(any(test, fuzzing))] - next_remote_commitment_tx_fee_info_cached: Mutex>, + next_remote_commitment_tx_fee_info_cached: Arc>>, /// lnd has a long-standing bug where, upon reconnection, if the channel is not yet confirmed /// they will not send a channel_reestablish until the channel locks in. Then, they will send a @@ -2143,9 +2143,9 @@ impl ChannelContext where SP::Target: SignerProvider { #[cfg(debug_assertions)] - holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))), + holder_max_commitment_tx_output: Arc::new(Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat)))), #[cfg(debug_assertions)] - counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))), + counterparty_max_commitment_tx_output: Arc::new(Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat)))), last_sent_closing_fee: None, last_received_closing_sig: None, @@ -2203,9 +2203,9 @@ impl ChannelContext where SP::Target: SignerProvider { announcement_sigs: None, #[cfg(any(test, fuzzing))] - next_local_commitment_tx_fee_info_cached: Mutex::new(None), + next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), #[cfg(any(test, fuzzing))] - next_remote_commitment_tx_fee_info_cached: Mutex::new(None), + next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), workaround_lnd_bug_4006: None, sent_message_awaiting_response: None, @@ -2378,9 +2378,9 @@ impl ChannelContext where SP::Target: SignerProvider { // We'll add our counterparty's `funding_satoshis` to these max commitment output assertions // when we receive `accept_channel2`. #[cfg(debug_assertions)] - holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)), + holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))), #[cfg(debug_assertions)] - counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)), + counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))), last_sent_closing_fee: None, last_received_closing_sig: None, @@ -2436,9 +2436,9 @@ impl ChannelContext where SP::Target: SignerProvider { announcement_sigs: None, #[cfg(any(test, fuzzing))] - next_local_commitment_tx_fee_info_cached: Mutex::new(None), + next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), #[cfg(any(test, fuzzing))] - next_remote_commitment_tx_fee_info_cached: Mutex::new(None), + next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), workaround_lnd_bug_4006: None, sent_message_awaiting_response: None, @@ -4105,11 +4105,13 @@ impl ChannelContext where SP::Target: SignerProvider { self.get_initial_counterparty_commitment_signature(logger) } - /// Clone, each field, with a few exceptions, notably the channel signer, and - /// a few non-cloneable fields (such as Secp256k1 context) + /// Clone, each field, with the exception of the channel signer. #[allow(unused)] fn clone(&self, holder_signer: ::EcdsaSigner) -> Self { Self { + // Use provided channel signer + holder_signer: ChannelSignerType::Ecdsa(holder_signer), + config: self.config, prev_config: self.prev_config, inbound_handshake_limits_override: self.inbound_handshake_limits_override, @@ -4118,12 +4120,9 @@ impl ChannelContext where SP::Target: SignerProvider { temporary_channel_id: self.temporary_channel_id, channel_state: self.channel_state, announcement_sigs_state: self.announcement_sigs_state.clone(), - // Create new Secp256k context - secp_ctx: Secp256k1::new(), + secp_ctx: self.secp_ctx.clone(), channel_value_satoshis: self.channel_value_satoshis, latest_monitor_update_id: self.latest_monitor_update_id, - // Use provided channel signer - holder_signer: ChannelSignerType::Ecdsa(holder_signer), shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(), destination_script: self.destination_script.clone(), cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number, @@ -4153,9 +4152,9 @@ impl ChannelContext where SP::Target: SignerProvider { update_time_counter: self.update_time_counter, // Create new mutex with copied values #[cfg(debug_assertions)] - holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()), + holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(), #[cfg(debug_assertions)] - counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()), + counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(), last_sent_closing_fee: self.last_sent_closing_fee.clone(), last_received_closing_sig: self.last_received_closing_sig, target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw, @@ -4192,9 +4191,9 @@ impl ChannelContext where SP::Target: SignerProvider { announcement_sigs: self.announcement_sigs, // Create new mutex with copied values #[cfg(any(test, fuzzing))] - next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()), + next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(), #[cfg(any(test, fuzzing))] - next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()), + next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(), workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(), sent_message_awaiting_response: self.sent_message_awaiting_response, #[cfg(any(test, fuzzing))] @@ -10283,9 +10282,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch feerate_per_kw, #[cfg(debug_assertions)] - holder_max_commitment_tx_output: Mutex::new((0, 0)), + holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))), #[cfg(debug_assertions)] - counterparty_max_commitment_tx_output: Mutex::new((0, 0)), + counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))), last_sent_closing_fee: None, last_received_closing_sig: None, @@ -10330,9 +10329,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch announcement_sigs, #[cfg(any(test, fuzzing))] - next_local_commitment_tx_fee_info_cached: Mutex::new(None), + next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), #[cfg(any(test, fuzzing))] - next_remote_commitment_tx_fee_info_cached: Mutex::new(None), + next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)), workaround_lnd_bug_4006: None, sent_message_awaiting_response: None,