Skip to content

Commit 9070d73

Browse files
committed
Clone for ChannelContext
1 parent 6b5e80d commit 9070d73

File tree

1 file changed

+169
-5
lines changed

1 file changed

+169
-5
lines changed

lightning/src/ln/channel.rs

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

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

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

225+
#[derive(Clone)]
223226
struct InboundHTLCOutput {
224227
htlc_id: u64,
225228
amount_msat: u64,
@@ -228,7 +231,8 @@ struct InboundHTLCOutput {
228231
state: InboundHTLCState,
229232
}
230233

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

313-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
317+
#[derive(Clone)]
318+
#[cfg_attr(test, derive(Debug, PartialEq))]
314319
struct OutboundHTLCOutput {
315320
htlc_id: u64,
316321
amount_msat: u64,
@@ -323,7 +328,8 @@ struct OutboundHTLCOutput {
323328
}
324329

325330
/// See AwaitingRemoteRevoke ChannelState for more info
326-
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
331+
#[derive(Clone)]
332+
#[cfg_attr(test, derive(Debug, PartialEq))]
327333
enum HTLCUpdateAwaitingACK {
328334
AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
329335
// always outbound
@@ -833,7 +839,7 @@ pub(super) enum ChannelUpdateStatus {
833839
}
834840

835841
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
836-
#[derive(PartialEq)]
842+
#[derive(Clone, PartialEq)]
837843
pub enum AnnouncementSigsState {
838844
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
839845
/// we sent the last `AnnouncementSignatures`.
@@ -1161,6 +1167,7 @@ pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
11611167
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
11621168
pub(crate) const COINBASE_MATURITY: u32 = 100;
11631169

1170+
#[derive(Clone)]
11641171
struct PendingChannelMonitorUpdate {
11651172
update: ChannelMonitorUpdate,
11661173
}
@@ -4811,6 +4818,111 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48114818
self.get_initial_counterparty_commitment_signature(funding, logger)
48124819
}
48134820

4821+
/// Clone, each field, with the exception of the channel signer.
4822+
#[allow(unused)]
4823+
fn clone(&self, holder_signer: <SP::Target as SignerProvider>::EcdsaSigner) -> Self {
4824+
Self {
4825+
// Use provided channel signer
4826+
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
4827+
4828+
config: self.config,
4829+
prev_config: self.prev_config,
4830+
inbound_handshake_limits_override: self.inbound_handshake_limits_override,
4831+
user_id: self.user_id,
4832+
channel_id: self.channel_id,
4833+
temporary_channel_id: self.temporary_channel_id,
4834+
channel_state: self.channel_state,
4835+
announcement_sigs_state: self.announcement_sigs_state.clone(),
4836+
secp_ctx: self.secp_ctx.clone(),
4837+
// channel_value_satoshis: self.channel_value_satoshis,
4838+
latest_monitor_update_id: self.latest_monitor_update_id,
4839+
shutdown_scriptpubkey: self.shutdown_scriptpubkey.clone(),
4840+
destination_script: self.destination_script.clone(),
4841+
// holder_commitment_point: self.holder_commitment_point,
4842+
cur_counterparty_commitment_transaction_number: self.cur_counterparty_commitment_transaction_number,
4843+
// value_to_self_msat: self.value_to_self_msat,
4844+
pending_inbound_htlcs: self.pending_inbound_htlcs.clone(),
4845+
pending_outbound_htlcs: self.pending_outbound_htlcs.clone(),
4846+
holding_cell_htlc_updates: self.holding_cell_htlc_updates.clone(),
4847+
resend_order: self.resend_order.clone(),
4848+
monitor_pending_channel_ready: self.monitor_pending_channel_ready,
4849+
monitor_pending_revoke_and_ack: self.monitor_pending_revoke_and_ack,
4850+
monitor_pending_commitment_signed: self.monitor_pending_commitment_signed,
4851+
monitor_pending_forwards: self.monitor_pending_forwards.clone(),
4852+
monitor_pending_failures: self.monitor_pending_failures.clone(),
4853+
monitor_pending_finalized_fulfills: self.monitor_pending_finalized_fulfills.clone(),
4854+
monitor_pending_update_adds: self.monitor_pending_update_adds.clone(),
4855+
monitor_pending_tx_signatures: self.monitor_pending_tx_signatures.clone(),
4856+
signer_pending_revoke_and_ack: self.signer_pending_revoke_and_ack,
4857+
signer_pending_commitment_update: self.signer_pending_commitment_update,
4858+
signer_pending_funding: self.signer_pending_funding,
4859+
signer_pending_closing: self.signer_pending_closing,
4860+
signer_pending_channel_ready: self.signer_pending_channel_ready,
4861+
pending_update_fee: self.pending_update_fee,
4862+
holding_cell_update_fee: self.holding_cell_update_fee,
4863+
next_holder_htlc_id: self.next_holder_htlc_id,
4864+
next_counterparty_htlc_id: self.next_counterparty_htlc_id,
4865+
feerate_per_kw: self.feerate_per_kw,
4866+
update_time_counter: self.update_time_counter,
4867+
// Create new mutex with copied values
4868+
// #[cfg(debug_assertions)]
4869+
// holder_max_commitment_tx_output: self.holder_max_commitment_tx_output.clone(),
4870+
// #[cfg(debug_assertions)]
4871+
// counterparty_max_commitment_tx_output: self.counterparty_max_commitment_tx_output.clone(),
4872+
last_sent_closing_fee: self.last_sent_closing_fee.clone(),
4873+
last_received_closing_sig: self.last_received_closing_sig,
4874+
target_closing_feerate_sats_per_kw: self.target_closing_feerate_sats_per_kw,
4875+
pending_counterparty_closing_signed: self.pending_counterparty_closing_signed.clone(),
4876+
closing_fee_limits: self.closing_fee_limits,
4877+
expecting_peer_commitment_signed: self.expecting_peer_commitment_signed,
4878+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
4879+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
4880+
short_channel_id: self.short_channel_id,
4881+
channel_creation_height: self.channel_creation_height,
4882+
counterparty_dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
4883+
holder_dust_limit_satoshis: self.holder_dust_limit_satoshis,
4884+
counterparty_max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
4885+
holder_max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
4886+
// counterparty_selected_channel_reserve_satoshis: self.counterparty_selected_channel_reserve_satoshis,
4887+
// holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
4888+
counterparty_htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
4889+
holder_htlc_minimum_msat: self.holder_htlc_minimum_msat,
4890+
counterparty_max_accepted_htlcs: self.counterparty_max_accepted_htlcs,
4891+
holder_max_accepted_htlcs: self.holder_max_accepted_htlcs,
4892+
minimum_depth: self.minimum_depth,
4893+
counterparty_forwarding_info: self.counterparty_forwarding_info.clone(),
4894+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
4895+
funding_transaction: self.funding_transaction.clone(),
4896+
is_manual_broadcast: self.is_manual_broadcast,
4897+
is_batch_funding: self.is_batch_funding,
4898+
counterparty_cur_commitment_point: self.counterparty_cur_commitment_point,
4899+
counterparty_prev_commitment_point: self.counterparty_prev_commitment_point,
4900+
counterparty_node_id: self.counterparty_node_id,
4901+
counterparty_shutdown_scriptpubkey: self.counterparty_shutdown_scriptpubkey.clone(),
4902+
commitment_secrets: self.commitment_secrets.clone(),
4903+
channel_update_status: self.channel_update_status,
4904+
closing_signed_in_flight: self.closing_signed_in_flight,
4905+
announcement_sigs: self.announcement_sigs,
4906+
// Create new mutex with copied values
4907+
// #[cfg(any(test, fuzzing))]
4908+
// next_local_commitment_tx_fee_info_cached: self.next_local_commitment_tx_fee_info_cached.clone(),
4909+
// #[cfg(any(test, fuzzing))]
4910+
// next_remote_commitment_tx_fee_info_cached: self.next_remote_commitment_tx_fee_info_cached.clone(),
4911+
workaround_lnd_bug_4006: self.workaround_lnd_bug_4006.clone(),
4912+
sent_message_awaiting_response: self.sent_message_awaiting_response,
4913+
channel_type: self.channel_type.clone(),
4914+
latest_inbound_scid_alias: self.latest_inbound_scid_alias,
4915+
outbound_scid_alias: self.outbound_scid_alias,
4916+
channel_pending_event_emitted: self.channel_pending_event_emitted,
4917+
funding_tx_broadcast_safe_event_emitted: self.funding_tx_broadcast_safe_event_emitted,
4918+
channel_ready_event_emitted: self.channel_ready_event_emitted,
4919+
local_initiated_shutdown: self.local_initiated_shutdown.clone(),
4920+
channel_keys_id: self.channel_keys_id,
4921+
blocked_monitor_updates: self.blocked_monitor_updates.clone(),
4922+
next_funding_txid: self.next_funding_txid.clone(),
4923+
}
4924+
}
4925+
48144926
/// Get the splice message that can be sent during splice initiation.
48154927
#[cfg(splicing)]
48164928
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
@@ -4967,6 +5079,7 @@ pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
49675079
}
49685080

49695081
#[cfg(any(test, fuzzing))]
5082+
#[derive(Clone)]
49705083
struct CommitmentTxInfoCached {
49715084
fee: u64,
49725085
total_pending_htlcs: usize,
@@ -11331,7 +11444,7 @@ mod tests {
1133111444
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
1133211445
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
1133311446
use crate::ln::channel::InitFeatures;
11334-
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
11447+
use crate::ln::channel::{AwaitingChannelReadyFlags, ChannelContext, ChannelState, FundedChannel, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat};
1133511448
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
1133611449
use crate::types::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
1133711450
use crate::ln::msgs;
@@ -13128,6 +13241,57 @@ mod tests {
1312813241
);
1312913242
}
1313013243

13244+
#[test]
13245+
fn channel_context_clone() {
13246+
let fee_estimator = TestFeeEstimator {fee_est: 253 };
13247+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
13248+
let seed = [42; 32];
13249+
let network = Network::Testnet;
13250+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
13251+
let secp_ctx = Secp256k1::new();
13252+
let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
13253+
let config = UserConfig::default();
13254+
13255+
let signer_provider: &TestKeysInterface = &&keys_provider;
13256+
let channel_value_satoshis = 10000000;
13257+
let user_id = 42;
13258+
let channel_keys_id = signer_provider.generate_channel_keys_id(false, channel_value_satoshis, user_id);
13259+
let holder_signer = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
13260+
let logger = test_utils::TestLogger::new();
13261+
let pubkeys = holder_signer.pubkeys().clone();
13262+
13263+
// Create a context
13264+
let context = ChannelContext::<&TestKeysInterface>::new_for_outbound_channel(
13265+
&bounded_fee_estimator,
13266+
&&keys_provider,
13267+
&signer_provider,
13268+
node_a_node_id,
13269+
&channelmanager::provided_init_features(&config),
13270+
channel_value_satoshis,
13271+
100000,
13272+
user_id,
13273+
&config,
13274+
0,
13275+
42,
13276+
None,
13277+
100000,
13278+
[42; 32],
13279+
holder_signer,
13280+
pubkeys,
13281+
&logger,
13282+
).unwrap().1;
13283+
13284+
// Clone it
13285+
let holder_signer2 = signer_provider.derive_channel_signer(channel_value_satoshis, channel_keys_id);
13286+
let context_cloned = context.clone(holder_signer2);
13287+
13288+
// Compare some fields
13289+
// assert_eq!(context_cloned.channel_value_satoshis, context.channel_value_satoshis);
13290+
assert_eq!(context_cloned.channel_id, context.channel_id);
13291+
assert_eq!(context_cloned.funding_tx_broadcast_safe_event_emitted, context.funding_tx_broadcast_safe_event_emitted);
13292+
assert_eq!(context_cloned.channel_keys_id, context.channel_keys_id);
13293+
}
13294+
1313113295
#[cfg(all(test, splicing))]
1313213296
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1313313297
use crate::ln::channel::PendingSplice;

0 commit comments

Comments
 (0)