Skip to content

Commit e311020

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent 1864af6 commit e311020

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

16041604
#[cfg(debug_assertions)]
16051605
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1606-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1606+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
16071607
#[cfg(debug_assertions)]
16081608
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1609-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1609+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
16101610

16111611
// (fee_sats, skip_remote_output, fee_range, holder_sig)
16121612
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1718,9 +1718,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
17181718
// be, by comparing the cached values to the fee of the tranaction generated by
17191719
// `build_commitment_transaction`.
17201720
#[cfg(any(test, fuzzing))]
1721-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1721+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
17221722
#[cfg(any(test, fuzzing))]
1723-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1723+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
17241724

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

25312531

25322532
#[cfg(debug_assertions)]
2533-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2533+
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)))),
25342534
#[cfg(debug_assertions)]
2535-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2535+
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)))),
25362536

25372537
last_sent_closing_fee: None,
25382538
last_received_closing_sig: None,
@@ -2590,9 +2590,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25902590
announcement_sigs: None,
25912591

25922592
#[cfg(any(test, fuzzing))]
2593-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2593+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25942594
#[cfg(any(test, fuzzing))]
2595-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2595+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25962596

25972597
workaround_lnd_bug_4006: None,
25982598
sent_message_awaiting_response: None,
@@ -2765,9 +2765,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27652765
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
27662766
// when we receive `accept_channel2`.
27672767
#[cfg(debug_assertions)]
2768-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2768+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
27692769
#[cfg(debug_assertions)]
2770-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2770+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
27712771

27722772
last_sent_closing_fee: None,
27732773
last_received_closing_sig: None,
@@ -2823,9 +2823,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28232823
announcement_sigs: None,
28242824

28252825
#[cfg(any(test, fuzzing))]
2826-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2826+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
28272827
#[cfg(any(test, fuzzing))]
2828-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2828+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
28292829

28302830
workaround_lnd_bug_4006: None,
28312831
sent_message_awaiting_response: None,
@@ -4519,11 +4519,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45194519
self.get_initial_counterparty_commitment_signature(logger)
45204520
}
45214521

4522-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4523-
/// a few non-cloneable fields (such as Secp256k1 context)
4522+
/// Clone, each field, with the exception of the channel signer.
45244523
#[allow(unused)]
45254524
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
45264525
Self {
4526+
// Use provided channel signer
4527+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4528+
45274529
config: self.config,
45284530
prev_config: self.prev_config,
45294531
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4532,12 +4534,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45324534
temporary_channel_id: self.temporary_channel_id,
45334535
channel_state: self.channel_state,
45344536
announcement_sigs_state: self.announcement_sigs_state.clone(),
4535-
// Create new Secp256k context
4536-
secp_ctx: Secp256k1::new(),
4537+
secp_ctx: self.secp_ctx.clone(),
45374538
channel_value_satoshis: self.channel_value_satoshis,
45384539
latest_monitor_update_id: self.latest_monitor_update_id,
4539-
// Use provided channel signer
4540-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
45414540
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
45424541
destination_script: self.destination_script.clone(),
45434542
// holder_commitment_point: self.holder_commitment_point,
@@ -4568,9 +4567,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45684567
update_time_counter: self.update_time_counter,
45694568
// Create new mutex with copied values
45704569
#[cfg(debug_assertions)]
4571-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4570+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
45724571
#[cfg(debug_assertions)]
4573-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4572+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
45744573
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
45754574
last_received_closing_sig: self.last_received_closing_sig,
45764575
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4607,9 +4606,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
46074606
announcement_sigs: self.announcement_sigs,
46084607
// Create new mutex with copied values
46094608
#[cfg(any(test, fuzzing))]
4610-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4609+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
46114610
#[cfg(any(test, fuzzing))]
4612-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4611+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
46134612
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
46144613
sent_message_awaiting_response: self.sent_message_awaiting_response,
46154614
#[cfg(any(test, fuzzing))]
@@ -10896,9 +10895,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1089610895
feerate_per_kw,
1089710896

1089810897
#[cfg(debug_assertions)]
10899-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10898+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1090010899
#[cfg(debug_assertions)]
10901-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10900+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1090210901

1090310902
last_sent_closing_fee: None,
1090410903
last_received_closing_sig: None,
@@ -10943,9 +10942,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1094310942
announcement_sigs,
1094410943

1094510944
#[cfg(any(test, fuzzing))]
10946-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10945+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1094710946
#[cfg(any(test, fuzzing))]
10948-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10947+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1094910948

1095010949
workaround_lnd_bug_4006: None,
1095110950
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)