Skip to content

Commit b5fb8a3

Browse files
committed
Put PendingV2Channel implementation behind a trait
1 parent 7b45811 commit b5fb8a3

File tree

2 files changed

+134
-54
lines changed

2 files changed

+134
-54
lines changed

lightning/src/ln/channel.rs

+133-53
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,22 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where
22922292
}
22932293
}
22942294

2295-
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2295+
// TODO Naming
2296+
pub(super) trait PendingV2ChannelTrait<SP: Deref> where SP::Target: SignerProvider {
2297+
fn context(&self) -> &ChannelContext<SP>;
2298+
fn context_mut(&mut self) -> &mut ChannelContext<SP>;
2299+
fn funding(&self) -> &FundingScope;
2300+
fn funding_mut(&mut self) -> &mut FundingScope;
2301+
fn funding_and_context_mut(&mut self) -> (&mut FundingScope, &mut ChannelContext<SP>);
2302+
fn dual_funding_context(&self) -> &DualFundingChannelContext;
2303+
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>);
2304+
fn unfunded_context(&self) -> &UnfundedChannelContext;
2305+
fn interactive_tx_constructor(&self) -> Option<&InteractiveTxConstructor>;
2306+
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor>;
2307+
fn set_interactive_tx_constructor(&mut self, iatxc: Option<InteractiveTxConstructor>);
2308+
fn clear_interactive_tx_constructor(&mut self);
2309+
fn set_interactive_tx_signing_session(&mut self, session: InteractiveTxSigningSession);
2310+
22962311
/// Prepare and start interactive transaction negotiation.
22972312
/// `change_destination_opt` - Optional destination for optional change; if None,
22982313
/// default destination address is used.
@@ -2304,11 +2319,11 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23042319
) -> Result<Option<InteractiveTxMessageSend>, AbortReason>
23052320
where ES::Target: EntropySource
23062321
{
2307-
debug_assert!(matches!(self.context.channel_state, ChannelState::NegotiatingFunding(_)));
2308-
debug_assert!(self.interactive_tx_constructor.is_none());
2322+
debug_assert!(matches!(self.context().channel_state, ChannelState::NegotiatingFunding(_)));
2323+
debug_assert!(self.interactive_tx_constructor().is_none());
23092324

23102325
let mut funding_inputs = Vec::new();
2311-
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
2326+
self.swap_out_dual_funding_context_inputs(&mut funding_inputs);
23122327

23132328
// TODO(splicing): Add prev funding tx as input, must be provided as a parameter
23142329

@@ -2319,14 +2334,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23192334
let mut expected_remote_shared_funding_output = None;
23202335

23212336
let shared_funding_output = TxOut {
2322-
value: Amount::from_sat(self.funding.get_value_satoshis()),
2323-
script_pubkey: self.funding.get_funding_redeemscript().to_p2wsh(),
2337+
value: Amount::from_sat(self.funding().get_value_satoshis()),
2338+
script_pubkey: self.funding().get_funding_redeemscript().to_p2wsh(),
23242339
};
23252340

2326-
if self.funding.is_outbound() {
2341+
if self.funding().is_outbound() {
23272342
funding_outputs.push(
23282343
OutputOwned::Shared(SharedOwnedOutput::new(
2329-
shared_funding_output, self.dual_funding_context.our_funding_satoshis,
2344+
shared_funding_output, self.dual_funding_context().our_funding_satoshis,
23302345
))
23312346
);
23322347
} else {
@@ -2338,13 +2353,13 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23382353
let change_script = if let Some(script) = change_destination_opt {
23392354
script
23402355
} else {
2341-
signer_provider.get_destination_script(self.context.channel_keys_id)
2356+
signer_provider.get_destination_script(self.context().channel_keys_id)
23422357
.map_err(|_err| AbortReason::InternalError("Error getting destination script"))?
23432358
};
23442359
let change_value_opt = calculate_change_output_value(
2345-
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
2360+
self.funding().is_outbound(), self.dual_funding_context().our_funding_satoshis,
23462361
&funding_inputs, &funding_outputs,
2347-
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
2362+
self.dual_funding_context().funding_feerate_sat_per_1000_weight,
23482363
change_script.minimal_non_dust().to_sat(),
23492364
)?;
23502365
if let Some(change_value) = change_value_opt {
@@ -2353,10 +2368,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23532368
script_pubkey: change_script,
23542369
};
23552370
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2356-
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2371+
let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
23572372
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
23582373
// Check dust limit again
2359-
if change_value_decreased_with_fee > self.context.holder_dust_limit_satoshis {
2374+
if change_value_decreased_with_fee > self.context().holder_dust_limit_satoshis {
23602375
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
23612376
funding_outputs.push(OutputOwned::Single(change_output));
23622377
}
@@ -2365,70 +2380,71 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23652380
let constructor_args = InteractiveTxConstructorArgs {
23662381
entropy_source,
23672382
holder_node_id,
2368-
counterparty_node_id: self.context.counterparty_node_id,
2369-
channel_id: self.context.channel_id(),
2370-
feerate_sat_per_kw: self.dual_funding_context.funding_feerate_sat_per_1000_weight,
2371-
is_initiator: self.funding.is_outbound(),
2372-
funding_tx_locktime: self.dual_funding_context.funding_tx_locktime,
2383+
counterparty_node_id: self.context().counterparty_node_id,
2384+
channel_id: self.context().channel_id(),
2385+
feerate_sat_per_kw: self.dual_funding_context().funding_feerate_sat_per_1000_weight,
2386+
is_initiator: self.funding().is_outbound(),
2387+
funding_tx_locktime: self.dual_funding_context().funding_tx_locktime,
23732388
inputs_to_contribute: funding_inputs,
23742389
outputs_to_contribute: funding_outputs,
23752390
expected_remote_shared_funding_output,
23762391
};
23772392
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
23782393
let msg = tx_constructor.take_initiator_first_message();
23792394

2380-
self.interactive_tx_constructor = Some(tx_constructor);
2395+
self.set_interactive_tx_constructor(Some(tx_constructor));
23812396

23822397
Ok(msg)
23832398
}
23842399

2385-
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2386-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2400+
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2401+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
23872402
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_input(msg).map_err(
2388-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2403+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
23892404
None => Err(msgs::TxAbort {
2390-
channel_id: self.context.channel_id(),
2405+
channel_id: self.context().channel_id(),
23912406
data: b"No interactive transaction negotiation in progress".to_vec()
23922407
}),
23932408
})
23942409
}
23952410

2396-
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2397-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2411+
fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2412+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
23982413
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_output(msg).map_err(
2399-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2414+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24002415
None => Err(msgs::TxAbort {
2401-
channel_id: self.context.channel_id(),
2416+
channel_id: self.context().channel_id(),
24022417
data: b"No interactive transaction negotiation in progress".to_vec()
24032418
}),
24042419
})
24052420
}
24062421

2407-
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2408-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2422+
fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2423+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24092424
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_input(msg).map_err(
2410-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2425+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24112426
None => Err(msgs::TxAbort {
2412-
channel_id: self.context.channel_id(),
2427+
channel_id: self.context().channel_id(),
24132428
data: b"No interactive transaction negotiation in progress".to_vec()
24142429
}),
24152430
})
24162431
}
24172432

2418-
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2419-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2433+
fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2434+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24202435
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_output(msg).map_err(
2421-
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
2436+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24222437
None => Err(msgs::TxAbort {
2423-
channel_id: self.context.channel_id(),
2438+
channel_id: self.context().channel_id(),
24242439
data: b"No interactive transaction negotiation in progress".to_vec()
24252440
}),
24262441
})
24272442
}
24282443

2429-
pub fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
2430-
let tx_constructor = match &mut self.interactive_tx_constructor {
2431-
Some(ref mut tx_constructor) => tx_constructor,
2444+
fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
2445+
let interactive_tx_constructor = self.interactive_tx_constructor_mut();
2446+
let tx_constructor = match interactive_tx_constructor {
2447+
Some(tx_constructor) => tx_constructor,
24322448
None => {
24332449
let tx_abort = msgs::TxAbort {
24342450
channel_id: msg.channel_id,
@@ -2446,25 +2462,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24462462
};
24472463

24482464
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2449-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2465+
self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
24502466
};
24512467

24522468
HandleTxCompleteResult(Ok(tx_complete))
24532469
}
24542470

2455-
pub fn funding_tx_constructed<L: Deref>(
2471+
fn funding_tx_constructed<L: Deref>(
24562472
&mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
24572473
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
24582474
where
24592475
L::Target: Logger
24602476
{
2461-
let our_funding_satoshis = self.dual_funding_context.our_funding_satoshis;
2462-
let transaction_number = self.unfunded_context.transaction_number();
2477+
let our_funding_satoshis = self.dual_funding_context().our_funding_satoshis;
2478+
let transaction_number = self.unfunded_context().transaction_number();
24632479

24642480
let mut output_index = None;
2465-
let expected_spk = self.funding.get_funding_redeemscript().to_p2wsh();
2481+
let expected_spk = self.funding().get_funding_redeemscript().to_p2wsh();
24662482
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
2467-
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding.get_value_satoshis() {
2483+
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding().get_value_satoshis() {
24682484
if output_index.is_some() {
24692485
return Err(ChannelError::Close(
24702486
(
@@ -2484,24 +2500,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24842500
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
24852501
)));
24862502
};
2487-
self.funding.channel_transaction_parameters.funding_outpoint = Some(outpoint);
2503+
self.funding_mut().channel_transaction_parameters.funding_outpoint = Some(outpoint);
24882504

2489-
self.context.assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2490-
let commitment_signed = self.context.get_initial_commitment_signed(&self.funding, logger);
2505+
self.context().assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2506+
let (funding_mut, context_mut) = self.funding_and_context_mut();
2507+
let commitment_signed = context_mut.get_initial_commitment_signed(&funding_mut, logger);
24912508
let commitment_signed = match commitment_signed {
24922509
Ok(commitment_signed) => {
2493-
self.funding.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
2510+
self.funding_mut().funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
24942511
commitment_signed
24952512
},
24962513
Err(err) => {
2497-
self.funding.channel_transaction_parameters.funding_outpoint = None;
2514+
self.funding_mut().channel_transaction_parameters.funding_outpoint = None;
24982515
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })));
24992516
},
25002517
};
25012518

25022519
let funding_ready_for_sig_event = if signing_session.local_inputs_count() == 0 {
25032520
debug_assert_eq!(our_funding_satoshis, 0);
2504-
if signing_session.provide_holder_witnesses(self.context.channel_id, Vec::new()).is_err() {
2521+
if signing_session.provide_holder_witnesses(self.context().channel_id, Vec::new()).is_err() {
25052522
debug_assert!(
25062523
false,
25072524
"Zero inputs were provided & zero witnesses were provided, but a count mismatch was somehow found",
@@ -2537,16 +2554,79 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
25372554
)));
25382555
};
25392556

2540-
self.context.channel_state = ChannelState::FundingNegotiated;
2557+
self.context_mut().channel_state = ChannelState::FundingNegotiated;
25412558

25422559
// Clear the interactive transaction constructor
2543-
self.interactive_tx_constructor.take();
2544-
self.interactive_tx_signing_session = Some(signing_session);
2560+
self.clear_interactive_tx_constructor();
2561+
self.set_interactive_tx_signing_session(signing_session);
25452562

25462563
Ok((commitment_signed, funding_ready_for_sig_event))
25472564
}
25482565
}
25492566

2567+
impl<SP: Deref> PendingV2ChannelTrait<SP> for PendingV2Channel<SP> where SP::Target: SignerProvider {
2568+
#[inline]
2569+
fn context(&self) -> &ChannelContext<SP> {
2570+
&self.context
2571+
}
2572+
2573+
#[inline]
2574+
fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2575+
&mut self.context
2576+
}
2577+
2578+
#[inline]
2579+
fn funding(&self) -> &FundingScope {
2580+
&self.funding
2581+
}
2582+
2583+
#[inline]
2584+
fn funding_mut(&mut self) -> &mut FundingScope {
2585+
&mut self.funding
2586+
}
2587+
2588+
#[inline]
2589+
fn funding_and_context_mut(&mut self) -> (&mut FundingScope, &mut ChannelContext<SP>) {
2590+
(&mut self.funding, &mut self.context)
2591+
}
2592+
2593+
#[inline]
2594+
fn dual_funding_context(&self) -> &DualFundingChannelContext {
2595+
&self.dual_funding_context
2596+
}
2597+
2598+
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>) {
2599+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, funding_inputs);
2600+
}
2601+
2602+
#[inline]
2603+
fn unfunded_context(&self) -> &UnfundedChannelContext {
2604+
&self.unfunded_context
2605+
}
2606+
2607+
#[inline]
2608+
fn interactive_tx_constructor_mut(&mut self) -> Option<&mut InteractiveTxConstructor> {
2609+
self.interactive_tx_constructor.as_mut()
2610+
}
2611+
2612+
#[inline]
2613+
fn interactive_tx_constructor(&self) -> Option<&InteractiveTxConstructor> {
2614+
self.interactive_tx_constructor.as_ref()
2615+
}
2616+
2617+
fn set_interactive_tx_constructor(&mut self, iatxc: Option<InteractiveTxConstructor>) {
2618+
self.interactive_tx_constructor = iatxc;
2619+
}
2620+
2621+
fn clear_interactive_tx_constructor(&mut self) {
2622+
self.interactive_tx_constructor.take();
2623+
}
2624+
2625+
fn set_interactive_tx_signing_session(&mut self, session: InteractiveTxSigningSession) {
2626+
self.interactive_tx_signing_session = Some(session);
2627+
}
2628+
}
2629+
25502630
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25512631
fn new_for_inbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
25522632
fee_estimator: &'a LowerBoundedFeeEstimator<F>,

lightning/src/ln/channelmanager.rs

+1-1
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)