Skip to content

Commit eecd4ff

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent e01356d commit eecd4ff

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

lightning/src/ln/channel.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use crate::prelude::*;
6868
use core::{cmp,mem,fmt};
6969
use core::ops::Deref;
7070
#[cfg(any(test, fuzzing, debug_assertions))]
71-
use crate::sync::Mutex;
71+
use crate::sync::{Arc, Mutex};
7272
use crate::sign::type_resolver::ChannelSignerType;
7373

7474
use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint};
@@ -1718,10 +1718,10 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
17181718

17191719
#[cfg(debug_assertions)]
17201720
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1721-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1721+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
17221722
#[cfg(debug_assertions)]
17231723
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1724-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1724+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
17251725

17261726
// (fee_sats, skip_remote_output, fee_range, holder_sig)
17271727
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1833,9 +1833,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
18331833
// be, by comparing the cached values to the fee of the tranaction generated by
18341834
// `build_commitment_transaction`.
18351835
#[cfg(any(test, fuzzing))]
1836-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1836+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
18371837
#[cfg(any(test, fuzzing))]
1838-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1838+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
18391839

18401840
/// lnd has a long-standing bug where, upon reconnection, if the channel is not yet confirmed
18411841
/// they will not send a channel_reestablish until the channel locks in. Then, they will send a
@@ -2658,9 +2658,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26582658

26592659

26602660
#[cfg(debug_assertions)]
2661-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2661+
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)))),
26622662
#[cfg(debug_assertions)]
2663-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2663+
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)))),
26642664

26652665
last_sent_closing_fee: None,
26662666
last_received_closing_sig: None,
@@ -2718,9 +2718,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27182718
announcement_sigs: None,
27192719

27202720
#[cfg(any(test, fuzzing))]
2721-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2721+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
27222722
#[cfg(any(test, fuzzing))]
2723-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2723+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
27242724

27252725
workaround_lnd_bug_4006: None,
27262726
sent_message_awaiting_response: None,
@@ -2890,9 +2890,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28902890
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
28912891
// when we receive `accept_channel2`.
28922892
#[cfg(debug_assertions)]
2893-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2893+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
28942894
#[cfg(debug_assertions)]
2895-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2895+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
28962896

28972897
last_sent_closing_fee: None,
28982898
last_received_closing_sig: None,
@@ -2948,9 +2948,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29482948
announcement_sigs: None,
29492949

29502950
#[cfg(any(test, fuzzing))]
2951-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2951+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
29522952
#[cfg(any(test, fuzzing))]
2953-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2953+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
29542954

29552955
workaround_lnd_bug_4006: None,
29562956
sent_message_awaiting_response: None,
@@ -4645,11 +4645,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
46454645
self.get_initial_counterparty_commitment_signature(logger)
46464646
}
46474647

4648-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4649-
/// a few non-cloneable fields (such as Secp256k1 context)
4648+
/// Clone, each field, with the exception of the channel signer.
46504649
#[allow(unused)]
46514650
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
46524651
Self {
4652+
// Use provided channel signer
4653+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4654+
46534655
config: self.config,
46544656
prev_config: self.prev_config,
46554657
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4658,12 +4660,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
46584660
temporary_channel_id: self.temporary_channel_id,
46594661
channel_state: self.channel_state,
46604662
announcement_sigs_state: self.announcement_sigs_state.clone(),
4661-
// Create new Secp256k context
4662-
secp_ctx: Secp256k1::new(),
4663+
secp_ctx: self.secp_ctx.clone(),
46634664
channel_value_satoshis: self.channel_value_satoshis,
46644665
latest_monitor_update_id: self.latest_monitor_update_id,
4665-
// Use provided channel signer
4666-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
46674666
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
46684667
destination_script: self.destination_script.clone(),
46694668
// holder_commitment_point: self.holder_commitment_point,
@@ -4694,9 +4693,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
46944693
update_time_counter: self.update_time_counter,
46954694
// Create new mutex with copied values
46964695
#[cfg(debug_assertions)]
4697-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4696+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
46984697
#[cfg(debug_assertions)]
4699-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4698+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
47004699
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
47014700
last_received_closing_sig: self.last_received_closing_sig,
47024701
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4733,9 +4732,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
47334732
announcement_sigs: self.announcement_sigs,
47344733
// Create new mutex with copied values
47354734
#[cfg(any(test, fuzzing))]
4736-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4735+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
47374736
#[cfg(any(test, fuzzing))]
4738-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4737+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
47394738
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
47404739
sent_message_awaiting_response: self.sent_message_awaiting_response,
47414740
#[cfg(any(test, fuzzing))]
@@ -10928,9 +10927,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1092810927
feerate_per_kw,
1092910928

1093010929
#[cfg(debug_assertions)]
10931-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10930+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1093210931
#[cfg(debug_assertions)]
10933-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10932+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1093410933

1093510934
last_sent_closing_fee: None,
1093610935
last_received_closing_sig: None,
@@ -10975,9 +10974,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1097510974
announcement_sigs,
1097610975

1097710976
#[cfg(any(test, fuzzing))]
10978-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10977+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1097910978
#[cfg(any(test, fuzzing))]
10980-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10979+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1098110980

1098210981
workaround_lnd_bug_4006: None,
1098310982
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)