Skip to content

Commit

Permalink
Add Arc to Mutex fields to make them cloneable
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Jan 7, 2025
1 parent 4c01c4b commit c30a8ed
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use crate::prelude::*;
use core::{cmp,mem,fmt};
use core::ops::Deref;
#[cfg(any(test, fuzzing, debug_assertions))]
use crate::sync::Mutex;
use crate::sync::{Arc, Mutex};
use crate::sign::type_resolver::ChannelSignerType;

use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint};
Expand Down Expand Up @@ -1319,10 +1319,10 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {

#[cfg(debug_assertions)]
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
holder_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,
#[cfg(debug_assertions)]
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
counterparty_max_commitment_tx_output: Arc<Mutex<(u64, u64)>>,

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

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


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

last_sent_closing_fee: None,
last_received_closing_sig: None,
Expand Down Expand Up @@ -2203,9 +2203,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
announcement_sigs: None,

#[cfg(any(test, fuzzing))]
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
#[cfg(any(test, fuzzing))]
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),

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

last_sent_closing_fee: None,
last_received_closing_sig: None,
Expand Down Expand Up @@ -2436,9 +2436,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
announcement_sigs: None,

#[cfg(any(test, fuzzing))]
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
#[cfg(any(test, fuzzing))]
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),

workaround_lnd_bug_4006: None,
sent_message_awaiting_response: None,
Expand Down Expand Up @@ -4105,11 +4105,13 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
self.get_initial_counterparty_commitment_signature(logger)
}

/// Clone, each field, with a few exceptions, notably the channel signer, and
/// a few non-cloneable fields (such as Secp256k1 context)
/// Clone, each field, with the exception of the channel signer.
#[allow(unused)]
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
Self {
// Use provided channel signer
holder_signer: ChannelSignerType::Ecdsa(holder_signer),

config: self.config,
prev_config: self.prev_config,
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
Expand All @@ -4118,12 +4120,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
temporary_channel_id: self.temporary_channel_id,
channel_state: self.channel_state,
announcement_sigs_state: self.announcement_sigs_state.clone(),
// Create new Secp256k context
secp_ctx: Secp256k1::new(),
secp_ctx: self.secp_ctx.clone(),
channel_value_satoshis: self.channel_value_satoshis,
latest_monitor_update_id: self.latest_monitor_update_id,
// Use provided channel signer
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
destination_script: self.destination_script.clone(),
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
Expand Down Expand Up @@ -4153,9 +4152,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
update_time_counter: self.update_time_counter,
// Create new mutex with copied values
#[cfg(debug_assertions)]
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
#[cfg(debug_assertions)]
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
last_received_closing_sig: self.last_received_closing_sig,
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
Expand Down Expand Up @@ -4192,9 +4191,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
announcement_sigs: self.announcement_sigs,
// Create new mutex with copied values
#[cfg(any(test, fuzzing))]
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
#[cfg(any(test, fuzzing))]
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
sent_message_awaiting_response: self.sent_message_awaiting_response,
#[cfg(any(test, fuzzing))]
Expand Down Expand Up @@ -10283,9 +10282,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
feerate_per_kw,

#[cfg(debug_assertions)]
holder_max_commitment_tx_output: Mutex::new((0, 0)),
holder_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),
#[cfg(debug_assertions)]
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
counterparty_max_commitment_tx_output: Arc::new(Mutex::new((0, 0))),

last_sent_closing_fee: None,
last_received_closing_sig: None,
Expand Down Expand Up @@ -10330,9 +10329,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
announcement_sigs,

#[cfg(any(test, fuzzing))]
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
next_local_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),
#[cfg(any(test, fuzzing))]
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
next_remote_commitment_tx_fee_info_cached: Arc::new(Mutex::new(None)),

workaround_lnd_bug_4006: None,
sent_message_awaiting_response: None,
Expand Down

0 comments on commit c30a8ed

Please sign in to comment.