Skip to content

Commit af0a8c6

Browse files
committed
Add Arc to Mutex fields to make them cloneable
1 parent c933de6 commit af0a8c6

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

13661366
#[cfg(debug_assertions)]
13671367
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1368-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1368+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13691369
#[cfg(debug_assertions)]
13701370
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1371-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1371+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13721372

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

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

22962296

22972297
#[cfg(debug_assertions)]
2298-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2298+
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)))),
22992299
#[cfg(debug_assertions)]
2300-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2300+
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)))),
23012301

23022302
last_sent_closing_fee: None,
23032303
last_received_closing_sig: None,
@@ -2355,9 +2355,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23552355
announcement_sigs: None,
23562356

23572357
#[cfg(any(test, fuzzing))]
2358-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2358+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23592359
#[cfg(any(test, fuzzing))]
2360-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2360+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
23612361

23622362
workaround_lnd_bug_4006: None,
23632363
sent_message_awaiting_response: None,
@@ -2533,9 +2533,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25332533
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
25342534
// when we receive `accept_channel2`.
25352535
#[cfg(debug_assertions)]
2536-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2536+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
25372537
#[cfg(debug_assertions)]
2538-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2538+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
25392539

25402540
last_sent_closing_fee: None,
25412541
last_received_closing_sig: None,
@@ -2591,9 +2591,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25912591
announcement_sigs: None,
25922592

25932593
#[cfg(any(test, fuzzing))]
2594-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2594+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25952595
#[cfg(any(test, fuzzing))]
2596-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2596+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
25972597

25982598
workaround_lnd_bug_4006: None,
25992599
sent_message_awaiting_response: None,
@@ -4296,11 +4296,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42964296
self.get_initial_counterparty_commitment_signature(logger)
42974297
}
42984298

4299-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4300-
/// a few non-cloneable fields (such as Secp256k1 context)
4299+
/// Clone, each field, with the exception of the channel signer.
43014300
#[allow(unused)]
43024301
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
43034302
Self {
4303+
// Use provided channel signer
4304+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4305+
43044306
config: self.config,
43054307
prev_config: self.prev_config,
43064308
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4309,12 +4311,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43094311
temporary_channel_id: self.temporary_channel_id,
43104312
channel_state: self.channel_state,
43114313
announcement_sigs_state: self.announcement_sigs_state.clone(),
4312-
// Create new Secp256k context
4313-
secp_ctx: Secp256k1::new(),
4314+
secp_ctx: self.secp_ctx.clone(),
43144315
channel_value_satoshis: self.channel_value_satoshis,
43154316
latest_monitor_update_id: self.latest_monitor_update_id,
4316-
// Use provided channel signer
4317-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
43184317
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
43194318
destination_script: self.destination_script.clone(),
43204319
holder_commitment_point: self.holder_commitment_point,
@@ -4344,9 +4343,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43444343
update_time_counter: self.update_time_counter,
43454344
// Create new mutex with copied values
43464345
#[cfg(debug_assertions)]
4347-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4346+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
43484347
#[cfg(debug_assertions)]
4349-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4348+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
43504349
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
43514350
last_received_closing_sig: self.last_received_closing_sig,
43524351
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4383,9 +4382,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43834382
announcement_sigs: self.announcement_sigs,
43844383
// Create new mutex with copied values
43854384
#[cfg(any(test, fuzzing))]
4386-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4385+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
43874386
#[cfg(any(test, fuzzing))]
4388-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4387+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
43894388
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
43904389
sent_message_awaiting_response: self.sent_message_awaiting_response,
43914390
#[cfg(any(test, fuzzing))]
@@ -10578,9 +10577,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1057810577
feerate_per_kw,
1057910578

1058010579
#[cfg(debug_assertions)]
10581-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10580+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1058210581
#[cfg(debug_assertions)]
10583-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10582+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1058410583

1058510584
last_sent_closing_fee: None,
1058610585
last_received_closing_sig: None,
@@ -10625,9 +10624,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1062510624
announcement_sigs,
1062610625

1062710626
#[cfg(any(test, fuzzing))]
10628-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10627+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1062910628
#[cfg(any(test, fuzzing))]
10630-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10629+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1063110630

1063210631
workaround_lnd_bug_4006: None,
1063310632
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)