Skip to content

Commit a544a13

Browse files
committed
Put PendingV2Channel implementation behind a trait
2 parents b30aabd + 77fc455 commit a544a13

File tree

2 files changed

+134
-54
lines changed

2 files changed

+134
-54
lines changed

lightning/src/ln/channel.rs

Lines changed: 133 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,22 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where
23092309
}
23102310
}
23112311

2312-
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2312+
// TODO Naming
2313+
pub(super) trait PendingV2ChannelTrait<SP: Deref> where SP::Target: SignerProvider {
2314+
fn context(&self) -> &ChannelContext<SP>;
2315+
fn context_mut(&mut self) -> &mut ChannelContext<SP>;
2316+
fn funding(&self) -> &FundingScope;
2317+
fn funding_mut(&mut self) -> &mut FundingScope;
2318+
fn funding_and_context_mut(&mut self) -> (&mut FundingScope, &mut ChannelContext<SP>);
2319+
fn dual_funding_context(&self) -> &DualFundingChannelContext;
2320+
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>);
2321+
fn unfunded_context(&self) -> &UnfundedChannelContext;
2322+
fn interactive_tx_constructor(&self) -> Option<&InteractiveTxConstructor>;
2323+
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor>;
2324+
fn set_interactive_tx_constructor(&mut self, iatxc: Option<InteractiveTxConstructor>);
2325+
fn clear_interactive_tx_constructor(&mut self);
2326+
fn set_interactive_tx_signing_session(&mut self, session: InteractiveTxSigningSession);
2327+
23132328
/// Prepare and start interactive transaction negotiation.
23142329
/// `change_destination_opt` - Optional destination for optional change; if None,
23152330
/// default destination address is used.
@@ -2322,11 +2337,11 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23222337
) -> Result<Option<InteractiveTxMessageSend>, AbortReason>
23232338
where ES::Target: EntropySource
23242339
{
2325-
debug_assert!(matches!(self.context.channel_state, ChannelState::NegotiatingFunding(_)));
2326-
debug_assert!(self.interactive_tx_constructor.is_none());
2340+
debug_assert!(matches!(self.context().channel_state, ChannelState::NegotiatingFunding(_)));
2341+
debug_assert!(self.interactive_tx_constructor().is_none());
23272342

23282343
let mut funding_inputs = Vec::new();
2329-
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
2344+
self.swap_out_dual_funding_context_inputs(&mut funding_inputs);
23302345

23312346
if let Some(prev_funding_input) = prev_funding_input {
23322347
funding_inputs.push(prev_funding_input);
@@ -2339,14 +2354,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23392354
let mut expected_remote_shared_funding_output = None;
23402355

23412356
let shared_funding_output = TxOut {
2342-
value: Amount::from_sat(self.funding.get_value_satoshis()),
2343-
script_pubkey: self.funding.get_funding_redeemscript().to_p2wsh(),
2357+
value: Amount::from_sat(self.funding().get_value_satoshis()),
2358+
script_pubkey: self.funding().get_funding_redeemscript().to_p2wsh(),
23442359
};
23452360

2346-
if self.funding.is_outbound() {
2361+
if self.funding().is_outbound() {
23472362
funding_outputs.push(
23482363
OutputOwned::Shared(SharedOwnedOutput::new(
2349-
shared_funding_output, self.dual_funding_context.our_funding_satoshis,
2364+
shared_funding_output, self.dual_funding_context().our_funding_satoshis,
23502365
))
23512366
);
23522367
} else {
@@ -2358,13 +2373,13 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23582373
let change_script = if let Some(script) = change_destination_opt {
23592374
script
23602375
} else {
2361-
signer_provider.get_destination_script(self.context.channel_keys_id)
2376+
signer_provider.get_destination_script(self.context().channel_keys_id)
23622377
.map_err(|_err| AbortReason::InternalError("Error getting destination script"))?
23632378
};
23642379
let change_value_opt = calculate_change_output_value(
2365-
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
2380+
self.funding().is_outbound(), self.dual_funding_context().our_funding_satoshis,
23662381
&funding_inputs, &funding_outputs,
2367-
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
2382+
self.dual_funding_context().funding_feerate_sat_per_1000_weight,
23682383
change_script.minimal_non_dust().to_sat(),
23692384
)?;
23702385
if let Some(change_value) = change_value_opt {
@@ -2373,10 +2388,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23732388
script_pubkey: change_script,
23742389
};
23752390
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2376-
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2391+
let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
23772392
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
23782393
// Check dust limit again
2379-
if change_value_decreased_with_fee > self.context.holder_dust_limit_satoshis {
2394+
if change_value_decreased_with_fee > self.context().holder_dust_limit_satoshis {
23802395
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
23812396
funding_outputs.push(OutputOwned::Single(change_output));
23822397
}
@@ -2385,70 +2400,71 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23852400
let constructor_args = InteractiveTxConstructorArgs {
23862401
entropy_source,
23872402
holder_node_id,
2388-
counterparty_node_id: self.context.counterparty_node_id,
2389-
channel_id: self.context.channel_id(),
2390-
feerate_sat_per_kw: self.dual_funding_context.funding_feerate_sat_per_1000_weight,
2391-
is_initiator: self.funding.is_outbound(),
2392-
funding_tx_locktime: self.dual_funding_context.funding_tx_locktime,
2403+
counterparty_node_id: self.context().counterparty_node_id,
2404+
channel_id: self.context().channel_id(),
2405+
feerate_sat_per_kw: self.dual_funding_context().funding_feerate_sat_per_1000_weight,
2406+
is_initiator: self.funding().is_outbound(),
2407+
funding_tx_locktime: self.dual_funding_context().funding_tx_locktime,
23932408
inputs_to_contribute: funding_inputs,
23942409
outputs_to_contribute: funding_outputs,
23952410
expected_remote_shared_funding_output,
23962411
};
23972412
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
23982413
let msg = tx_constructor.take_initiator_first_message();
23992414

2400-
self.interactive_tx_constructor = Some(tx_constructor);
2415+
self.set_interactive_tx_constructor(Some(tx_constructor));
24012416

24022417
Ok(msg)
24032418
}
24042419

2405-
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2406-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2420+
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2421+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24072422
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_input(msg).map_err(
2408-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2423+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24092424
None => Err(msgs::TxAbort {
2410-
channel_id: self.context.channel_id(),
2425+
channel_id: self.context().channel_id(),
24112426
data: b"No interactive transaction negotiation in progress".to_vec()
24122427
}),
24132428
})
24142429
}
24152430

2416-
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2417-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2431+
fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2432+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24182433
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_output(msg).map_err(
2419-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2434+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24202435
None => Err(msgs::TxAbort {
2421-
channel_id: self.context.channel_id(),
2436+
channel_id: self.context().channel_id(),
24222437
data: b"No interactive transaction negotiation in progress".to_vec()
24232438
}),
24242439
})
24252440
}
24262441

2427-
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2428-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2442+
fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2443+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24292444
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_input(msg).map_err(
2430-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2445+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24312446
None => Err(msgs::TxAbort {
2432-
channel_id: self.context.channel_id(),
2447+
channel_id: self.context().channel_id(),
24332448
data: b"No interactive transaction negotiation in progress".to_vec()
24342449
}),
24352450
})
24362451
}
24372452

2438-
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2439-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2453+
fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2454+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24402455
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_output(msg).map_err(
2441-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2456+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24422457
None => Err(msgs::TxAbort {
2443-
channel_id: self.context.channel_id(),
2458+
channel_id: self.context().channel_id(),
24442459
data: b"No interactive transaction negotiation in progress".to_vec()
24452460
}),
24462461
})
24472462
}
24482463

2449-
pub fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
2450-
let tx_constructor = match &mut self.interactive_tx_constructor {
2451-
Some(ref mut tx_constructor) => tx_constructor,
2464+
fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
2465+
let interactive_tx_constructor = self.interactive_tx_constructor_mut();
2466+
let tx_constructor = match interactive_tx_constructor {
2467+
Some(tx_constructor) => tx_constructor,
24522468
None => {
24532469
let tx_abort = msgs::TxAbort {
24542470
channel_id: msg.channel_id,
@@ -2466,25 +2482,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24662482
};
24672483

24682484
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2469-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2485+
self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
24702486
};
24712487

24722488
HandleTxCompleteResult(Ok(tx_complete))
24732489
}
24742490

2475-
pub fn funding_tx_constructed<L: Deref>(
2491+
fn funding_tx_constructed<L: Deref>(
24762492
&mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
24772493
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
24782494
where
24792495
L::Target: Logger
24802496
{
2481-
let our_funding_satoshis = self.dual_funding_context.our_funding_satoshis;
2482-
let transaction_number = self.unfunded_context.transaction_number();
2497+
let our_funding_satoshis = self.dual_funding_context().our_funding_satoshis;
2498+
let transaction_number = self.unfunded_context().transaction_number();
24832499

24842500
let mut output_index = None;
2485-
let expected_spk = self.funding.get_funding_redeemscript().to_p2wsh();
2501+
let expected_spk = self.funding().get_funding_redeemscript().to_p2wsh();
24862502
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
2487-
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding.get_value_satoshis() {
2503+
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding().get_value_satoshis() {
24882504
if output_index.is_some() {
24892505
return Err(ChannelError::Close(
24902506
(
@@ -2504,24 +2520,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
25042520
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
25052521
)));
25062522
};
2507-
self.funding.channel_transaction_parameters.funding_outpoint = Some(outpoint);
2523+
self.funding_mut().channel_transaction_parameters.funding_outpoint = Some(outpoint);
25082524

2509-
self.context.assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2510-
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger);
2525+
self.context().assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2526+
let (funding_mut, context_mut) = self.funding_and_context_mut();
2527+
let commitment_signed = context_mut.get_initial_commitment_signed(&funding_mut, logger);
25112528
let commitment_signed = match commitment_signed {
25122529
Ok(commitment_signed) => {
2513-
self.funding.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
2530+
self.funding_mut().funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
25142531
commitment_signed
25152532
},
25162533
Err(err) => {
2517-
self.funding.channel_transaction_parameters.funding_outpoint = None;
2534+
self.funding_mut().channel_transaction_parameters.funding_outpoint = None;
25182535
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })));
25192536
},
25202537
};
25212538

25222539
let funding_ready_for_sig_event = if signing_session.local_inputs_count() == 0 {
25232540
debug_assert_eq!(our_funding_satoshis, 0);
2524-
if signing_session.provide_holder_witnesses(self.context.channel_id, Vec::new()).is_err() {
2541+
if signing_session.provide_holder_witnesses(self.context().channel_id, Vec::new()).is_err() {
25252542
debug_assert!(
25262543
false,
25272544
"Zero inputs were provided & zero witnesses were provided, but a count mismatch was somehow found",
@@ -2557,16 +2574,79 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
25572574
)));
25582575
};
25592576

2560-
self.context.channel_state = ChannelState::FundingNegotiated;
2577+
self.context_mut().channel_state = ChannelState::FundingNegotiated;
25612578

25622579
// Clear the interactive transaction constructor
2563-
self.interactive_tx_constructor.take();
2564-
self.interactive_tx_signing_session = Some(signing_session);
2580+
self.clear_interactive_tx_constructor();
2581+
self.set_interactive_tx_signing_session(signing_session);
25652582

25662583
Ok((commitment_signed, funding_ready_for_sig_event))
25672584
}
25682585
}
25692586

2587+
impl<SP: Deref> PendingV2ChannelTrait<SP> for PendingV2Channel<SP> where SP::Target: SignerProvider {
2588+
#[inline]
2589+
fn context(&self) -> &ChannelContext<SP> {
2590+
&self.context
2591+
}
2592+
2593+
#[inline]
2594+
fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2595+
&mut self.context
2596+
}
2597+
2598+
#[inline]
2599+
fn funding(&self) -> &FundingScope {
2600+
&self.funding
2601+
}
2602+
2603+
#[inline]
2604+
fn funding_mut(&mut self) -> &mut FundingScope {
2605+
&mut self.funding
2606+
}
2607+
2608+
#[inline]
2609+
fn funding_and_context_mut(&mut self) -> (&mut FundingScope, &mut ChannelContext<SP>) {
2610+
(&mut self.funding, &mut self.context)
2611+
}
2612+
2613+
#[inline]
2614+
fn dual_funding_context(&self) -> &DualFundingChannelContext {
2615+
&self.dual_funding_context
2616+
}
2617+
2618+
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>) {
2619+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, funding_inputs);
2620+
}
2621+
2622+
#[inline]
2623+
fn unfunded_context(&self) -> &UnfundedChannelContext {
2624+
&self.unfunded_context
2625+
}
2626+
2627+
#[inline]
2628+
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor> {
2629+
self.interactive_tx_constructor.as_mut()
2630+
}
2631+
2632+
#[inline]
2633+
fn interactive_tx_constructor(&self) -> Option<&InteractiveTxConstructor> {
2634+
self.interactive_tx_constructor.as_ref()
2635+
}
2636+
2637+
fn set_interactive_tx_constructor(&mut self, iatxc: Option<InteractiveTxConstructor>) {
2638+
self.interactive_tx_constructor = iatxc;
2639+
}
2640+
2641+
fn clear_interactive_tx_constructor(&mut self) {
2642+
self.interactive_tx_constructor.take();
2643+
}
2644+
2645+
fn set_interactive_tx_signing_session(&mut self, session: InteractiveTxSigningSession) {
2646+
self.interactive_tx_signing_session = Some(session);
2647+
}
2648+
}
2649+
25702650
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25712651
fn new_for_inbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
25722652
fee_estimator: &'a LowerBoundedFeeEstimator<F>,

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::ln::inbound_payment;
5151
use crate::ln::types::ChannelId;
5252
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
5353
use crate::ln::channel::{self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, ReconnectionMsg, InboundV1Channel, WithChannelContext};
54-
use crate::ln::channel::PendingV2Channel;
54+
use crate::ln::channel::{PendingV2Channel, PendingV2ChannelTrait};
5555
use crate::ln::channel_state::ChannelDetails;
5656
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5757
#[cfg(any(feature = "_test_utils", test))]

0 commit comments

Comments
 (0)