Skip to content

Commit 28dffa8

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent 225e2d9 commit 28dffa8

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

13651365
#[cfg(debug_assertions)]
13661366
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1367-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1367+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13681368
#[cfg(debug_assertions)]
13691369
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1370-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1370+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13711371

13721372
// (fee_sats, skip_remote_output, fee_range, holder_sig)
13731373
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1479,9 +1479,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14791479
// be, by comparing the cached values to the fee of the tranaction generated by
14801480
// `build_commitment_transaction`.
14811481
#[cfg(any(test, fuzzing))]
1482-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1482+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14831483
#[cfg(any(test, fuzzing))]
1484-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1484+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14851485

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

22442244

22452245
#[cfg(debug_assertions)]
2246-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2246+
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)))),
22472247
#[cfg(debug_assertions)]
2248-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2248+
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)))),
22492249

22502250
last_sent_closing_fee: None,
22512251
last_received_closing_sig: None,
@@ -2303,9 +2303,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23032303
announcement_sigs: None,
23042304

23052305
#[cfg(any(test, fuzzing))]
2306-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2306+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23072307
#[cfg(any(test, fuzzing))]
2308-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2308+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23092309

23102310
workaround_lnd_bug_4006: None,
23112311
sent_message_awaiting_response: None,
@@ -2481,9 +2481,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24812481
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
24822482
// when we receive `accept_channel2`.
24832483
#[cfg(debug_assertions)]
2484-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2484+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
24852485
#[cfg(debug_assertions)]
2486-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2486+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
24872487

24882488
last_sent_closing_fee: None,
24892489
last_received_closing_sig: None,
@@ -2539,9 +2539,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25392539
announcement_sigs: None,
25402540

25412541
#[cfg(any(test, fuzzing))]
2542-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2542+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25432543
#[cfg(any(test, fuzzing))]
2544-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2544+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25452545

25462546
workaround_lnd_bug_4006: None,
25472547
sent_message_awaiting_response: None,
@@ -4244,11 +4244,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42444244
self.get_initial_counterparty_commitment_signature(logger)
42454245
}
42464246

4247-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4248-
/// a few non-cloneable fields (such as Secp256k1 context)
4247+
/// Clone, each field, with the exception of the channel signer.
42494248
#[allow(unused)]
42504249
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
42514250
Self {
4251+
// Use provided channel signer
4252+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4253+
42524254
config: self.config,
42534255
prev_config: self.prev_config,
42544256
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4257,12 +4259,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42574259
temporary_channel_id: self.temporary_channel_id,
42584260
channel_state: self.channel_state,
42594261
announcement_sigs_state: self.announcement_sigs_state.clone(),
4260-
// Create new Secp256k context
4261-
secp_ctx: Secp256k1::new(),
4262+
secp_ctx: self.secp_ctx.clone(),
42624263
channel_value_satoshis: self.channel_value_satoshis,
42634264
latest_monitor_update_id: self.latest_monitor_update_id,
4264-
// Use provided channel signer
4265-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
42664265
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
42674266
destination_script: self.destination_script.clone(),
42684267
holder_commitment_point: self.holder_commitment_point,
@@ -4292,9 +4291,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42924291
update_time_counter: self.update_time_counter,
42934292
// Create new mutex with copied values
42944293
#[cfg(debug_assertions)]
4295-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4294+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
42964295
#[cfg(debug_assertions)]
4297-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4296+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
42984297
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
42994298
last_received_closing_sig: self.last_received_closing_sig,
43004299
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4331,9 +4330,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43314330
announcement_sigs: self.announcement_sigs,
43324331
// Create new mutex with copied values
43334332
#[cfg(any(test, fuzzing))]
4334-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4333+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
43354334
#[cfg(any(test, fuzzing))]
4336-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4335+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
43374336
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
43384337
sent_message_awaiting_response: self.sent_message_awaiting_response,
43394338
#[cfg(any(test, fuzzing))]
@@ -10524,9 +10523,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1052410523
feerate_per_kw,
1052510524

1052610525
#[cfg(debug_assertions)]
10527-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10526+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1052810527
#[cfg(debug_assertions)]
10529-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10528+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1053010529

1053110530
last_sent_closing_fee: None,
1053210531
last_received_closing_sig: None,
@@ -10571,9 +10570,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1057110570
announcement_sigs,
1057210571

1057310572
#[cfg(any(test, fuzzing))]
10574-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10573+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1057510574
#[cfg(any(test, fuzzing))]
10576-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10575+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1057710576

1057810577
workaround_lnd_bug_4006: None,
1057910578
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)