Skip to content

Commit cdecb8c

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent 7b4c270 commit cdecb8c

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};
@@ -1371,10 +1371,10 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
13711371

13721372
#[cfg(debug_assertions)]
13731373
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1374-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1374+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13751375
#[cfg(debug_assertions)]
13761376
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1377-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1377+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13781378

13791379
// (fee_sats, skip_remote_output, fee_range, holder_sig)
13801380
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1486,9 +1486,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14861486
// be, by comparing the cached values to the fee of the tranaction generated by
14871487
// `build_commitment_transaction`.
14881488
#[cfg(any(test, fuzzing))]
1489-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1489+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14901490
#[cfg(any(test, fuzzing))]
1491-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1491+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14921492

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

23112311

23122312
#[cfg(debug_assertions)]
2313-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2313+
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)))),
23142314
#[cfg(debug_assertions)]
2315-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2315+
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)))),
23162316

23172317
last_sent_closing_fee: None,
23182318
last_received_closing_sig: None,
@@ -2370,9 +2370,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23702370
announcement_sigs: None,
23712371

23722372
#[cfg(any(test, fuzzing))]
2373-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2373+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23742374
#[cfg(any(test, fuzzing))]
2375-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2375+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23762376

23772377
workaround_lnd_bug_4006: None,
23782378
sent_message_awaiting_response: None,
@@ -2545,9 +2545,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25452545
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
25462546
// when we receive `accept_channel2`.
25472547
#[cfg(debug_assertions)]
2548-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2548+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
25492549
#[cfg(debug_assertions)]
2550-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2550+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
25512551

25522552
last_sent_closing_fee: None,
25532553
last_received_closing_sig: None,
@@ -2603,9 +2603,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26032603
announcement_sigs: None,
26042604

26052605
#[cfg(any(test, fuzzing))]
2606-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2606+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
26072607
#[cfg(any(test, fuzzing))]
2608-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2608+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
26092609

26102610
workaround_lnd_bug_4006: None,
26112611
sent_message_awaiting_response: None,
@@ -4299,11 +4299,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42994299
self.get_initial_counterparty_commitment_signature(logger)
43004300
}
43014301

4302-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4303-
/// a few non-cloneable fields (such as Secp256k1 context)
4302+
/// Clone, each field, with the exception of the channel signer.
43044303
#[allow(unused)]
43054304
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
43064305
Self {
4306+
// Use provided channel signer
4307+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4308+
43074309
config: self.config,
43084310
prev_config: self.prev_config,
43094311
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4312,12 +4314,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43124314
temporary_channel_id: self.temporary_channel_id,
43134315
channel_state: self.channel_state,
43144316
announcement_sigs_state: self.announcement_sigs_state.clone(),
4315-
// Create new Secp256k context
4316-
secp_ctx: Secp256k1::new(),
4317+
secp_ctx: self.secp_ctx.clone(),
43174318
channel_value_satoshis: self.channel_value_satoshis,
43184319
latest_monitor_update_id: self.latest_monitor_update_id,
4319-
// Use provided channel signer
4320-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
43214320
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
43224321
destination_script: self.destination_script.clone(),
43234322
holder_commitment_point: self.holder_commitment_point,
@@ -4347,9 +4346,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43474346
update_time_counter: self.update_time_counter,
43484347
// Create new mutex with copied values
43494348
#[cfg(debug_assertions)]
4350-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4349+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
43514350
#[cfg(debug_assertions)]
4352-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4351+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
43534352
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
43544353
last_received_closing_sig: self.last_received_closing_sig,
43554354
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4386,9 +4385,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43864385
announcement_sigs: self.announcement_sigs,
43874386
// Create new mutex with copied values
43884387
#[cfg(any(test, fuzzing))]
4389-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4388+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
43904389
#[cfg(any(test, fuzzing))]
4391-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4390+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
43924391
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
43934392
sent_message_awaiting_response: self.sent_message_awaiting_response,
43944393
#[cfg(any(test, fuzzing))]
@@ -10692,9 +10691,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1069210691
feerate_per_kw,
1069310692

1069410693
#[cfg(debug_assertions)]
10695-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10694+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1069610695
#[cfg(debug_assertions)]
10697-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10696+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1069810697

1069910698
last_sent_closing_fee: None,
1070010699
last_received_closing_sig: None,
@@ -10739,9 +10738,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1073910738
announcement_sigs,
1074010739

1074110740
#[cfg(any(test, fuzzing))]
10742-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10741+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1074310742
#[cfg(any(test, fuzzing))]
10744-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10743+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1074510744

1074610745
workaround_lnd_bug_4006: None,
1074710746
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)