Skip to content

Commit 7b4c270

Browse files
committed
Clone for ChannelContext
1 parent fb2bbf3 commit 7b4c270

File tree

1 file changed

+171
-5
lines changed

1 file changed

+171
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 171 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`.
@@ -1112,6 +1118,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11121118
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11131119
pub(crate) const COINBASE_MATURITY: u32 = 100;
11141120

1121+
#[derive(Clone)]
11151122
struct PendingChannelMonitorUpdate {
11161123
update: ChannelMonitorUpdate,
11171124
}
@@ -4292,6 +4299,113 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42924299
self.get_initial_counterparty_commitment_signature(logger)
42934300
}
42944301

4302+
/// Clone, each field, with a few exceptions, notably the channel signer, and
4303+
/// a few non-cloneable fields (such as Secp256k1 context)
4304+
#[allow(unused)]
4305+
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4306+
Self {
4307+
config: self.config,
4308+
prev_config: self.prev_config,
4309+
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4310+
user_id: self.user_id,
4311+
channel_id: self.channel_id,
4312+
temporary_channel_id: self.temporary_channel_id,
4313+
channel_state: self.channel_state,
4314+
announcement_sigs_state: self.announcement_sigs_state.clone(),
4315+
// Create new Secp256k context
4316+
secp_ctx: Secp256k1::new(),
4317+
channel_value_satoshis: self.channel_value_satoshis,
4318+
latest_monitor_update_id: self.latest_monitor_update_id,
4319+
// Use provided channel signer
4320+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4321+
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4322+
destination_script: self.destination_script.clone(),
4323+
holder_commitment_point: self.holder_commitment_point,
4324+
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4325+
value_to_self_msat: self.value_to_self_msat,
4326+
pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4327+
pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4328+
holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4329+
resend_order: self.resend_order.clone(),
4330+
monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4331+
monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4332+
monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4333+
monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4334+
monitor_pending_failures: self.monitor_pending_failures.clone(),
4335+
monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4336+
monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4337+
monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4338+
signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4339+
signer_pending_commitment_update: self.signer_pending_commitment_update,
4340+
signer_pending_funding: self.signer_pending_funding,
4341+
signer_pending_closing: self.signer_pending_closing,
4342+
pending_update_fee: self.pending_update_fee,
4343+
holding_cell_update_fee: self.holding_cell_update_fee,
4344+
next_holder_htlc_id: self.next_holder_htlc_id,
4345+
next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4346+
feerate_per_kw: self.feerate_per_kw,
4347+
update_time_counter: self.update_time_counter,
4348+
// Create new mutex with copied values
4349+
#[cfg(debug_assertions)]
4350+
holder_max_commitment_tx_output: Mutex::new(*self.holder_max_commitment_tx_output.lock().unwrap()),
4351+
#[cfg(debug_assertions)]
4352+
counterparty_max_commitment_tx_output: Mutex::new(*self.counterparty_max_commitment_tx_output.lock().unwrap()),
4353+
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4354+
last_received_closing_sig: self.last_received_closing_sig,
4355+
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4356+
pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4357+
closing_fee_limits: self.closing_fee_limits,
4358+
expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4359+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4360+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4361+
short_channel_id: self.short_channel_id,
4362+
channel_creation_height: self.channel_creation_height,
4363+
counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4364+
holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4365+
counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4366+
holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4367+
counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4368+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4369+
counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4370+
holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4371+
counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4372+
holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4373+
minimum_depth: self.minimum_depth,
4374+
counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4375+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4376+
funding_transaction: self.funding_transaction.clone(),
4377+
is_manual_broadcast: self.is_manual_broadcast,
4378+
is_batch_funding: self.is_batch_funding,
4379+
counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4380+
counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4381+
counterparty_node_id: self.counterparty_node_id,
4382+
counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4383+
commitment_secrets: self.commitment_secrets.clone(),
4384+
channel_update_status: self.channel_update_status,
4385+
closing_signed_in_flight: self.closing_signed_in_flight,
4386+
announcement_sigs: self.announcement_sigs,
4387+
// Create new mutex with copied values
4388+
#[cfg(any(test, fuzzing))]
4389+
next_local_commitment_tx_fee_info_cached: Mutex::new(self.next_local_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4390+
#[cfg(any(test, fuzzing))]
4391+
next_remote_commitment_tx_fee_info_cached: Mutex::new(self.next_remote_commitment_tx_fee_info_cached.lock().unwrap().clone()),
4392+
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4393+
sent_message_awaiting_response: self.sent_message_awaiting_response,
4394+
#[cfg(any(test, fuzzing))]
4395+
historical_inbound_htlc_fulfills: self.historical_inbound_htlc_fulfills.clone(),
4396+
channel_type: self.channel_type.clone(),
4397+
latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4398+
outbound_scid_alias: self.outbound_scid_alias,
4399+
channel_pending_event_emitted: self.channel_pending_event_emitted,
4400+
funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4401+
channel_ready_event_emitted: self.channel_ready_event_emitted,
4402+
local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4403+
channel_keys_id: self.channel_keys_id,
4404+
blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4405+
next_funding_txid: self.next_funding_txid.clone(),
4406+
}
4407+
}
4408+
42954409
/// Splice process starting; update state, log, etc.
42964410
#[cfg(splicing)]
42974411
pub(crate) fn splice_start<L: Deref>(&mut self, is_outgoing: bool, logger: &L) where L::Target: Logger {
@@ -4468,6 +4582,7 @@ pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
44684582
}
44694583

44704584
#[cfg(any(test, fuzzing))]
4585+
#[derive(Clone)]
44714586
struct CommitmentTxInfoCached {
44724587
fee: u64,
44734588
total_pending_htlcs: usize,
@@ -10679,7 +10794,7 @@ mod tests {
1067910794
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1068010795
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1068110796
use crate::ln::channel::InitFeatures;
10682-
use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
10797+
use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelContext, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1068310798
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1068410799
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1068510800
use crate::ln::msgs;
@@ -12444,6 +12559,57 @@ mod tests {
1244412559
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1244512560
}
1244612561

12562+
#[test]
12563+
fn channel_context_clone() {
12564+
let fee_estimator = TestFeeEstimator {fee_est: 253 };
12565+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
12566+
let seed = [42; 32];
12567+
let network = Network::Testnet;
12568+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
12569+
let secp_ctx = Secp256k1::new();
12570+
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
12571+
let config = UserConfig::default();
12572+
12573+
let signer_provider: &TestKeysInterface = &&keys_provider;
12574+
let channel_value_satoshis = 10000000;
12575+
let user_id = 42;
12576+
let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
12577+
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12578+
let logger = test_utils::TestLogger::new();
12579+
let pubkeys = holder_signer.pubkeys().clone();
12580+
12581+
// Create a context
12582+
let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
12583+
&bounded_fee_estimator,
12584+
&&keys_provider,
12585+
&signer_provider,
12586+
node_a_node_id,
12587+
&channelmanager::provided_init_features(&config),
12588+
channel_value_satoshis,
12589+
100000,
12590+
user_id,
12591+
&config,
12592+
0,
12593+
42,
12594+
None,
12595+
100000,
12596+
[42; 32],
12597+
holder_signer,
12598+
pubkeys,
12599+
&logger,
12600+
).unwrap();
12601+
12602+
// Clone it
12603+
let holder_signer2 = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
12604+
let context_cloned = context.clone(holder_signer2);
12605+
12606+
// Compare some fields
12607+
assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
12608+
assert_eq!(context_cloned.channel_id, context.channel_id);
12609+
assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
12610+
assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
12611+
}
12612+
1244712613
#[cfg(all(test, splicing))]
1244812614
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1244912615
use crate::ln::channel::PendingSplice;

0 commit comments

Comments
 (0)