Skip to content

Commit b1a96a4

Browse files
committed
Clone for ChannelContext
1 parent fd1c251 commit b1a96a4

File tree

1 file changed

+172
-5
lines changed

1 file changed

+172
-5
lines changed

lightning/src/ln/channel.rs

+172-5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum FeeUpdateState {
115115
Outbound,
116116
}
117117

118+
#[derive(Clone)]
118119
enum InboundHTLCRemovalReason {
119120
FailRelay(msgs::OnionErrorPacket),
120121
FailMalformed(([u8; 32], u16)),
@@ -149,6 +150,7 @@ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
149150
},
150151
);
151152

153+
#[derive(Clone)]
152154
enum InboundHTLCState {
153155
/// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
154156
/// update_add_htlc message for this HTLC.
@@ -223,6 +225,7 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
223225
}
224226
}
225227

228+
#[derive(Clone)]
226229
struct InboundHTLCOutput {
227230
htlc_id: u64,
228231
amount_msat: u64,
@@ -231,7 +234,8 @@ struct InboundHTLCOutput {
231234
state: InboundHTLCState,
232235
}
233236

234-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
237+
#[derive(Clone)]
238+
#[cfg_attr(test, derive(Debug, PartialEq))]
235239
enum OutboundHTLCState {
236240
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
237241
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -313,7 +317,8 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
313317
}
314318
}
315319

316-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
320+
#[derive(Clone)]
321+
#[cfg_attr(test, derive(Debug, PartialEq))]
317322
struct OutboundHTLCOutput {
318323
htlc_id: u64,
319324
amount_msat: u64,
@@ -326,7 +331,8 @@ struct OutboundHTLCOutput {
326331
}
327332

328333
/// See AwaitingRemoteRevoke ChannelState for more info
329-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
334+
#[derive(Clone)]
335+
#[cfg_attr(test, derive(Debug, PartialEq))]
330336
enum HTLCUpdateAwaitingACK {
331337
AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
332338
// always outbound
@@ -836,7 +842,7 @@ pub(super) enum ChannelUpdateStatus {
836842
}
837843

838844
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
839-
#[derive(PartialEq)]
845+
#[derive(Clone, PartialEq)]
840846
pub enum AnnouncementSigsState {
841847
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
842848
/// we sent the last `AnnouncementSignatures`.
@@ -1167,6 +1173,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11671173
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11681174
pub(crate) const COINBASE_MATURITY: u32 = 100;
11691175

1176+
#[derive(Clone)]
11701177
struct PendingChannelMonitorUpdate {
11711178
update: ChannelMonitorUpdate,
11721179
}
@@ -4851,6 +4858,112 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48514858
self.counterparty_cur_commitment_point = Some(counterparty_cur_commitment_point_override);
48524859
self.get_initial_counterparty_commitment_signature(funding, logger)
48534860
}
4861+
4862+
/// Clone, each field, with the exception of the channel signer.
4863+
#[allow(unused)]
4864+
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4865+
Self {
4866+
// Use provided channel signer
4867+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4868+
4869+
config: self.config,
4870+
prev_config: self.prev_config,
4871+
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4872+
user_id: self.user_id,
4873+
channel_id: self.channel_id,
4874+
temporary_channel_id: self.temporary_channel_id,
4875+
channel_state: self.channel_state,
4876+
announcement_sigs_state: self.announcement_sigs_state.clone(),
4877+
secp_ctx: self.secp_ctx.clone(),
4878+
// channel_value_satoshis: self.channel_value_satoshis,
4879+
latest_monitor_update_id: self.latest_monitor_update_id,
4880+
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4881+
destination_script: self.destination_script.clone(),
4882+
// holder_commitment_point: self.holder_commitment_point,
4883+
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4884+
// value_to_self_msat: self.value_to_self_msat,
4885+
pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4886+
pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4887+
holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4888+
resend_order: self.resend_order.clone(),
4889+
monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4890+
monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4891+
monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4892+
monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4893+
monitor_pending_failures: self.monitor_pending_failures.clone(),
4894+
monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4895+
monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4896+
monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4897+
signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4898+
signer_pending_commitment_update: self.signer_pending_commitment_update,
4899+
signer_pending_funding: self.signer_pending_funding,
4900+
signer_pending_closing: self.signer_pending_closing,
4901+
signer_pending_channel_ready: self.signer_pending_channel_ready,
4902+
pending_update_fee: self.pending_update_fee,
4903+
holding_cell_update_fee: self.holding_cell_update_fee,
4904+
next_holder_htlc_id: self.next_holder_htlc_id,
4905+
next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4906+
feerate_per_kw: self.feerate_per_kw,
4907+
update_time_counter: self.update_time_counter,
4908+
// Create new mutex with copied values
4909+
// #[cfg(debug_assertions)]
4910+
// holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
4911+
// #[cfg(debug_assertions)]
4912+
// counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
4913+
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4914+
last_received_closing_sig: self.last_received_closing_sig,
4915+
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4916+
pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4917+
closing_fee_limits: self.closing_fee_limits,
4918+
expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4919+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4920+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4921+
short_channel_id: self.short_channel_id,
4922+
channel_creation_height: self.channel_creation_height,
4923+
counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4924+
holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4925+
counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4926+
holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4927+
// counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4928+
// holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4929+
counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4930+
holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4931+
counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4932+
holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4933+
minimum_depth: self.minimum_depth,
4934+
counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4935+
// channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4936+
// funding_transaction: self.funding_transaction.clone(),
4937+
is_manual_broadcast: self.is_manual_broadcast,
4938+
is_batch_funding: self.is_batch_funding,
4939+
counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4940+
counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4941+
counterparty_node_id: self.counterparty_node_id,
4942+
counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4943+
commitment_secrets: self.commitment_secrets.clone(),
4944+
channel_update_status: self.channel_update_status,
4945+
closing_signed_in_flight: self.closing_signed_in_flight,
4946+
announcement_sigs: self.announcement_sigs,
4947+
// Create new mutex with copied values
4948+
// #[cfg(any(test, fuzzing))]
4949+
// next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
4950+
// #[cfg(any(test, fuzzing))]
4951+
// next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
4952+
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4953+
sent_message_awaiting_response: self.sent_message_awaiting_response,
4954+
channel_type: self.channel_type.clone(),
4955+
latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4956+
outbound_scid_alias: self.outbound_scid_alias,
4957+
channel_pending_event_emitted: self.channel_pending_event_emitted,
4958+
funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4959+
channel_ready_event_emitted: self.channel_ready_event_emitted,
4960+
local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4961+
channel_keys_id: self.channel_keys_id,
4962+
blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4963+
next_funding_txid: self.next_funding_txid.clone(),
4964+
is_holder_quiescence_initiator: self.is_holder_quiescence_initiator,
4965+
}
4966+
}
48544967
}
48554968

48564969
// Internal utility functions for channels
@@ -5030,6 +5143,7 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
50305143
}
50315144

50325145
#[cfg(any(test, fuzzing))]
5146+
#[derive(Clone)]
50335147
struct CommitmentTxInfoCached {
50345148
fee: u64,
50355149
total_pending_htlcs: usize,
@@ -11338,7 +11452,7 @@ mod tests {
1133811452
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1133911453
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1134011454
use crate::ln::channel::InitFeatures;
11341-
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
11455+
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelContext, ChannelId, ChannelPublicKeys, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1134211456
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1134311457
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1134411458
use crate::ln::msgs;
@@ -13238,6 +13352,59 @@ mod tests {
1323813352
}
1323913353
}
1324013354

13355+
#[test]
13356+
fn channel_context_clone() {
13357+
let fee_estimator = TestFeeEstimator {fee_est: 253 };
13358+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
13359+
let seed = [42; 32];
13360+
let network = Network::Testnet;
13361+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
13362+
let secp_ctx = Secp256k1::new();
13363+
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
13364+
let config = UserConfig::default();
13365+
13366+
let signer_provider: &TestKeysInterface = &&keys_provider;
13367+
let channel_value_satoshis = 10000000;
13368+
let user_id = 42;
13369+
let channel_keys_id = signer_provider.generate_channel_keys_id(false, user_id);
13370+
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
13371+
let logger = test_utils::TestLogger::new();
13372+
13373+
let temporary_channel_id_fn = Some(|pubkeys: &ChannelPublicKeys| {
13374+
ChannelId::temporary_v2_from_revocation_basepoint(&pubkeys.revocation_basepoint)
13375+
});
13376+
13377+
// Create a context
13378+
let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
13379+
&bounded_fee_estimator,
13380+
&&keys_provider,
13381+
&signer_provider,
13382+
node_a_node_id,
13383+
&channelmanager::provided_init_features(&config),
13384+
channel_value_satoshis,
13385+
100000,
13386+
user_id,
13387+
&config,
13388+
0,
13389+
42,
13390+
temporary_channel_id_fn,
13391+
100000,
13392+
[42; 32],
13393+
holder_signer,
13394+
&logger,
13395+
).unwrap().1;
13396+
13397+
// Clone it
13398+
let holder_signer2 = signer_provider.derive_channel_signer(channel_keys_id);
13399+
let context_cloned = context.clone(holder_signer2);
13400+
13401+
// Compare some fields
13402+
// assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
13403+
assert_eq!(context_cloned.channel_id, context.channel_id);
13404+
assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
13405+
assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
13406+
}
13407+
1324113408
#[cfg(all(test, splicing))]
1324213409
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1324313410
use crate::ln::channel::PendingSplice;

0 commit comments

Comments
 (0)