Skip to content

Commit 1864af6

Browse files
committed
Clone for ChannelContext
1 parent de153f5 commit 1864af6

File tree

1 file changed

+172
-5
lines changed

1 file changed

+172
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 172 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ enum FeeUpdateState {
111111
Outbound,
112112
}
113113

114+
#[derive(Clone)]
114115
enum InboundHTLCRemovalReason {
115116
FailRelay(msgs::OnionErrorPacket),
116117
FailMalformed(([u8; 32], u16)),
@@ -145,6 +146,7 @@ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
145146
},
146147
);
147148

149+
#[derive(Clone)]
148150
enum InboundHTLCState {
149151
/// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
150152
/// update_add_htlc message for this HTLC.
@@ -219,6 +221,7 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
219221
}
220222
}
221223

224+
#[derive(Clone)]
222225
struct InboundHTLCOutput {
223226
htlc_id: u64,
224227
amount_msat: u64,
@@ -227,7 +230,8 @@ struct InboundHTLCOutput {
227230
state: InboundHTLCState,
228231
}
229232

230-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
233+
#[derive(Clone)]
234+
#[cfg_attr(test, derive(Debug, PartialEq))]
231235
enum OutboundHTLCState {
232236
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
233237
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -309,7 +313,8 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
309313
}
310314
}
311315

312-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
316+
#[derive(Clone)]
317+
#[cfg_attr(test, derive(Debug, PartialEq))]
313318
struct OutboundHTLCOutput {
314319
htlc_id: u64,
315320
amount_msat: u64,
@@ -322,7 +327,8 @@ struct OutboundHTLCOutput {
322327
}
323328

324329
/// See AwaitingRemoteRevoke ChannelState for more info
325-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
330+
#[derive(Clone)]
331+
#[cfg_attr(test, derive(Debug, PartialEq))]
326332
enum HTLCUpdateAwaitingACK {
327333
AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
328334
// always outbound
@@ -797,7 +803,7 @@ pub(super) enum ChannelUpdateStatus {
797803
}
798804

799805
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
800-
#[derive(PartialEq)]
806+
#[derive(Clone, PartialEq)]
801807
pub enum AnnouncementSigsState {
802808
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
803809
/// we sent the last `AnnouncementSignatures`.
@@ -1115,6 +1121,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11151121
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11161122
pub(crate) const COINBASE_MATURITY: u32 = 100;
11171123

1124+
#[derive(Clone)]
11181125
struct PendingChannelMonitorUpdate {
11191126
update: ChannelMonitorUpdate,
11201127
}
@@ -4512,6 +4519,114 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45124519
self.get_initial_counterparty_commitment_signature(logger)
45134520
}
45144521

4522+
/// Clone, each field, with a few exceptions, notably the channel signer, and
4523+
/// a few non-cloneable fields (such as Secp256k1 context)
4524+
#[allow(unused)]
4525+
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4526+
Self {
4527+
config: self.config,
4528+
prev_config: self.prev_config,
4529+
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4530+
user_id: self.user_id,
4531+
channel_id: self.channel_id,
4532+
temporary_channel_id: self.temporary_channel_id,
4533+
channel_state: self.channel_state,
4534+
announcement_sigs_state: self.announcement_sigs_state.clone(),
4535+
// Create new Secp256k context
4536+
secp_ctx: Secp256k1::new(),
4537+
channel_value_satoshis: self.channel_value_satoshis,
4538+
latest_monitor_update_id: self.latest_monitor_update_id,
4539+
// Use provided channel signer
4540+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4541+
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4542+
destination_script: self.destination_script.clone(),
4543+
// holder_commitment_point: self.holder_commitment_point,
4544+
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4545+
value_to_self_msat: self.value_to_self_msat,
4546+
pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4547+
pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4548+
holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4549+
resend_order: self.resend_order.clone(),
4550+
monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4551+
monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4552+
monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4553+
monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4554+
monitor_pending_failures: self.monitor_pending_failures.clone(),
4555+
monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4556+
monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4557+
monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4558+
signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4559+
signer_pending_commitment_update: self.signer_pending_commitment_update,
4560+
signer_pending_funding: self.signer_pending_funding,
4561+
signer_pending_closing: self.signer_pending_closing,
4562+
signer_pending_channel_ready: self.signer_pending_channel_ready,
4563+
pending_update_fee: self.pending_update_fee,
4564+
holding_cell_update_fee: self.holding_cell_update_fee,
4565+
next_holder_htlc_id: self.next_holder_htlc_id,
4566+
next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4567+
feerate_per_kw: self.feerate_per_kw,
4568+
update_time_counter: self.update_time_counter,
4569+
// Create new mutex with copied values
4570+
#[cfg(debug_assertions)]
4571+
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4572+
#[cfg(debug_assertions)]
4573+
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4574+
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4575+
last_received_closing_sig: self.last_received_closing_sig,
4576+
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4577+
pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4578+
closing_fee_limits: self.closing_fee_limits,
4579+
expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4580+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4581+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4582+
short_channel_id: self.short_channel_id,
4583+
channel_creation_height: self.channel_creation_height,
4584+
counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4585+
holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4586+
counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4587+
holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4588+
counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4589+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4590+
counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4591+
holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4592+
counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4593+
holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4594+
minimum_depth: self.minimum_depth,
4595+
counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4596+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4597+
funding_transaction: self.funding_transaction.clone(),
4598+
is_manual_broadcast: self.is_manual_broadcast,
4599+
is_batch_funding: self.is_batch_funding,
4600+
counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4601+
counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4602+
counterparty_node_id: self.counterparty_node_id,
4603+
counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4604+
commitment_secrets: self.commitment_secrets.clone(),
4605+
channel_update_status: self.channel_update_status,
4606+
closing_signed_in_flight: self.closing_signed_in_flight,
4607+
announcement_sigs: self.announcement_sigs,
4608+
// Create new mutex with copied values
4609+
#[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()),
4611+
#[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()),
4613+
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4614+
sent_message_awaiting_response: self.sent_message_awaiting_response,
4615+
#[cfg(any(test, fuzzing))]
4616+
historical_inbound_htlc_fulfills: self.historical_inbound_htlc_fulfills.clone(),
4617+
channel_type: self.channel_type.clone(),
4618+
latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4619+
outbound_scid_alias: self.outbound_scid_alias,
4620+
channel_pending_event_emitted: self.channel_pending_event_emitted,
4621+
funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4622+
channel_ready_event_emitted: self.channel_ready_event_emitted,
4623+
local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4624+
channel_keys_id: self.channel_keys_id,
4625+
blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4626+
next_funding_txid: self.next_funding_txid.clone(),
4627+
}
4628+
}
4629+
45154630
/// Splice process starting; update state, log, etc.
45164631
#[cfg(splicing)]
45174632
pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
@@ -4688,6 +4803,7 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
46884803
}
46894804

46904805
#[cfg(any(test, fuzzing))]
4806+
#[derive(Clone)]
46914807
struct CommitmentTxInfoCached {
46924808
fee: u64,
46934809
total_pending_htlcs: usize,
@@ -10882,7 +10998,7 @@ mod tests {
1088210998
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1088310999
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1088411000
use crate::ln::channel::InitFeatures;
10885-
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
11001+
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelContext, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1088611002
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1088711003
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1088811004
use crate::ln::msgs;
@@ -12647,6 +12763,57 @@ mod tests {
1264712763
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1264812764
}
1264912765

12766+
#[test]
12767+
fn channel_context_clone() {
12768+
let fee_estimator = TestFeeEstimator {fee_est: 253 };
12769+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
12770+
let seed = [42; 32];
12771+
let network = Network::Testnet;
12772+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
12773+
let secp_ctx = Secp256k1::new();
12774+
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
12775+
let config = UserConfig::default();
12776+
12777+
let signer_provider: &TestKeysInterface = &&keys_provider;
12778+
let channel_value_satoshis = 10000000;
12779+
let user_id = 42;
12780+
let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
12781+
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12782+
let logger = test_utils::TestLogger::new();
12783+
let pubkeys = holder_signer.pubkeys().clone();
12784+
12785+
// Create a context
12786+
let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
12787+
&bounded_fee_estimator,
12788+
&&keys_provider,
12789+
&signer_provider,
12790+
node_a_node_id,
12791+
&channelmanager::provided_init_features(&config),
12792+
channel_value_satoshis,
12793+
100000,
12794+
user_id,
12795+
&config,
12796+
0,
12797+
42,
12798+
None,
12799+
100000,
12800+
[42; 32],
12801+
holder_signer,
12802+
pubkeys,
12803+
&logger,
12804+
).unwrap();
12805+
12806+
// Clone it
12807+
let holder_signer2 = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12808+
let context_cloned = context.clone(holder_signer2);
12809+
12810+
// Compare some fields
12811+
assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
12812+
assert_eq!(context_cloned.channel_id, context.channel_id);
12813+
assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
12814+
assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
12815+
}
12816+
1265012817
#[cfg(all(test, splicing))]
1265112818
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1265212819
use crate::ln::channel::PendingSplice;

0 commit comments

Comments
 (0)