Skip to content

Commit 7636b2b

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent 609340d commit 7636b2b

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

lightning/src/ln/channel.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::prelude::*;
6969
use core::{cmp,mem,fmt};
7070
use core::ops::Deref;
7171
#[cfg(any(test, fuzzing, debug_assertions))]
72-
use crate::sync::Mutex;
72+
use crate::sync::{Arc, Mutex};
7373
use crate::sign::type_resolver::ChannelSignerType;
7474

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

16741674
#[cfg(debug_assertions)]
16751675
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1676-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1676+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
16771677
#[cfg(debug_assertions)]
16781678
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1679-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1679+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
16801680

16811681
// (fee_sats, skip_remote_output, fee_range, holder_sig)
16821682
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1788,9 +1788,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
17881788
// be, by comparing the cached values to the fee of the tranaction generated by
17891789
// `build_commitment_transaction`.
17901790
#[cfg(any(test, fuzzing))]
1791-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1791+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
17921792
#[cfg(any(test, fuzzing))]
1793-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1793+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
17941794

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

24622462

24632463
#[cfg(debug_assertions)]
2464-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2464+
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)))),
24652465
#[cfg(debug_assertions)]
2466-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2466+
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)))),
24672467

24682468
last_sent_closing_fee: None,
24692469
last_received_closing_sig: None,
@@ -2521,9 +2521,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25212521
announcement_sigs: None,
25222522

25232523
#[cfg(any(test, fuzzing))]
2524-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2524+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25252525
#[cfg(any(test, fuzzing))]
2526-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2526+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25272527

25282528
workaround_lnd_bug_4006: None,
25292529
sent_message_awaiting_response: None,
@@ -2693,9 +2693,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26932693
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
26942694
// when we receive `accept_channel2`.
26952695
#[cfg(debug_assertions)]
2696-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2696+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
26972697
#[cfg(debug_assertions)]
2698-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2698+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
26992699

27002700
last_sent_closing_fee: None,
27012701
last_received_closing_sig: None,
@@ -2751,9 +2751,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27512751
announcement_sigs: None,
27522752

27532753
#[cfg(any(test, fuzzing))]
2754-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2754+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
27552755
#[cfg(any(test, fuzzing))]
2756-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2756+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
27572757

27582758
workaround_lnd_bug_4006: None,
27592759
sent_message_awaiting_response: None,
@@ -4421,11 +4421,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
44214421
self.get_initial_counterparty_commitment_signature(logger)
44224422
}
44234423

4424-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4425-
/// a few non-cloneable fields (such as Secp256k1 context)
4424+
/// Clone, each field, with the exception of the channel signer.
44264425
#[allow(unused)]
44274426
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
44284427
Self {
4428+
// Use provided channel signer
4429+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4430+
44294431
config: self.config,
44304432
prev_config: self.prev_config,
44314433
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4434,12 +4436,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
44344436
temporary_channel_id: self.temporary_channel_id,
44354437
channel_state: self.channel_state,
44364438
announcement_sigs_state: self.announcement_sigs_state.clone(),
4437-
// Create new Secp256k context
4438-
secp_ctx: Secp256k1::new(),
4439+
secp_ctx: self.secp_ctx.clone(),
44394440
channel_value_satoshis: self.channel_value_satoshis,
44404441
latest_monitor_update_id: self.latest_monitor_update_id,
4441-
// Use provided channel signer
4442-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
44434442
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
44444443
destination_script: self.destination_script.clone(),
44454444
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
@@ -4469,9 +4468,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
44694468
update_time_counter: self.update_time_counter,
44704469
// Create new mutex with copied values
44714470
#[cfg(debug_assertions)]
4472-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4471+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
44734472
#[cfg(debug_assertions)]
4474-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4473+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
44754474
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
44764475
last_received_closing_sig: self.last_received_closing_sig,
44774476
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4508,9 +4507,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45084507
announcement_sigs: self.announcement_sigs,
45094508
// Create new mutex with copied values
45104509
#[cfg(any(test, fuzzing))]
4511-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4510+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
45124511
#[cfg(any(test, fuzzing))]
4513-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4512+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
45144513
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
45154514
sent_message_awaiting_response: self.sent_message_awaiting_response,
45164515
channel_type: self.channel_type.clone(),
@@ -10490,9 +10489,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1049010489
feerate_per_kw,
1049110490

1049210491
#[cfg(debug_assertions)]
10493-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10492+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1049410493
#[cfg(debug_assertions)]
10495-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10494+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1049610495

1049710496
last_sent_closing_fee: None,
1049810497
last_received_closing_sig: None,
@@ -10537,9 +10536,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1053710536
announcement_sigs,
1053810537

1053910538
#[cfg(any(test, fuzzing))]
10540-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10539+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1054110540
#[cfg(any(test, fuzzing))]
10542-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10541+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1054310542

1054410543
workaround_lnd_bug_4006: None,
1054510544
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)