Skip to content

Commit 71663af

Browse files
committed
Add Arc to Mutex fields to make them cloneable
2 parents 4c01c4b + 1cdce16 commit 71663af

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

13201320
#[cfg(debug_assertions)]
13211321
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
1322-
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
1322+
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13231323
#[cfg(debug_assertions)]
13241324
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1325-
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1325+
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
13261326

13271327
// (fee_sats, skip_remote_output, fee_range, holder_sig)
13281328
last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
@@ -1434,9 +1434,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14341434
// be, by comparing the cached values to the fee of the tranaction generated by
14351435
// `build_commitment_transaction`.
14361436
#[cfg(any(test, fuzzing))]
1437-
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1437+
next_local_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14381438
#[cfg(any(test, fuzzing))]
1439-
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1439+
next_remote_commitment_tx_fee_info_cached: Arc<Mutex<Option<CommitmentTxInfoCached>>>,
14401440

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

21442144

21452145
#[cfg(debug_assertions)]
2146-
holder_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2146+
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)))),
21472147
#[cfg(debug_assertions)]
2148-
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
2148+
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)))),
21492149

21502150
last_sent_closing_fee: None,
21512151
last_received_closing_sig: None,
@@ -2203,9 +2203,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22032203
announcement_sigs: None,
22042204

22052205
#[cfg(any(test, fuzzing))]
2206-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2206+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
22072207
#[cfg(any(test, fuzzing))]
2208-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2208+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
22092209

22102210
workaround_lnd_bug_4006: None,
22112211
sent_message_awaiting_response: None,
@@ -2378,9 +2378,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23782378
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
23792379
// when we receive `accept_channel2`.
23802380
#[cfg(debug_assertions)]
2381-
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2381+
holder_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
23822382
#[cfg(debug_assertions)]
2383-
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
2383+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat))),
23842384

23852385
last_sent_closing_fee: None,
23862386
last_received_closing_sig: None,
@@ -2436,9 +2436,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24362436
announcement_sigs: None,
24372437

24382438
#[cfg(any(test, fuzzing))]
2439-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
2439+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
24402440
#[cfg(any(test, fuzzing))]
2441-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2441+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
24422442

24432443
workaround_lnd_bug_4006: None,
24442444
sent_message_awaiting_response: None,
@@ -4105,11 +4105,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41054105
self.get_initial_counterparty_commitment_signature(logger)
41064106
}
41074107

4108-
/// Clone, each field, with a few exceptions, notably the channel signer, and
4109-
/// a few non-cloneable fields (such as Secp256k1 context)
4108+
/// Clone, each field, with the exception of the channel signer.
41104109
#[allow(unused)]
41114110
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
41124111
Self {
4112+
// Use provided channel signer
4113+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4114+
41134115
config: self.config,
41144116
prev_config: self.prev_config,
41154117
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
@@ -4118,12 +4120,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41184120
temporary_channel_id: self.temporary_channel_id,
41194121
channel_state: self.channel_state,
41204122
announcement_sigs_state: self.announcement_sigs_state.clone(),
4121-
// Create new Secp256k context
4122-
secp_ctx: Secp256k1::new(),
4123+
secp_ctx: self.secp_ctx.clone(),
41234124
channel_value_satoshis: self.channel_value_satoshis,
41244125
latest_monitor_update_id: self.latest_monitor_update_id,
4125-
// Use provided channel signer
4126-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
41274126
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
41284127
destination_script: self.destination_script.clone(),
41294128
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
@@ -4153,9 +4152,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41534152
update_time_counter: self.update_time_counter,
41544153
// Create new mutex with copied values
41554154
#[cfg(debug_assertions)]
4156-
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4155+
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
41574156
#[cfg(debug_assertions)]
4158-
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4157+
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
41594158
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
41604159
last_received_closing_sig: self.last_received_closing_sig,
41614160
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
@@ -4192,9 +4191,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41924191
announcement_sigs: self.announcement_sigs,
41934192
// Create new mutex with copied values
41944193
#[cfg(any(test, fuzzing))]
4195-
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4194+
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
41964195
#[cfg(any(test, fuzzing))]
4197-
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4196+
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
41984197
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
41994198
sent_message_awaiting_response: self.sent_message_awaiting_response,
42004199
#[cfg(any(test, fuzzing))]
@@ -10283,9 +10282,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1028310282
feerate_per_kw,
1028410283

1028510284
#[cfg(debug_assertions)]
10286-
holder_max_commitment_tx_output: Mutex::new((0, 0)),
10285+
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1028710286
#[cfg(debug_assertions)]
10288-
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
10287+
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
1028910288

1029010289
last_sent_closing_fee: None,
1029110290
last_received_closing_sig: None,
@@ -10330,9 +10329,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1033010329
announcement_sigs,
1033110330

1033210331
#[cfg(any(test, fuzzing))]
10333-
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
10332+
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1033410333
#[cfg(any(test, fuzzing))]
10335-
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10334+
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
1033610335

1033710336
workaround_lnd_bug_4006: None,
1033810337
sent_message_awaiting_response: None,

0 commit comments

Comments
 (0)