Skip to content

Commit cd1b44e

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

File tree

2 files changed

+125
-54
lines changed

2 files changed

+125
-54
lines changed

lightning/src/ln/channel.rs

+124-53
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,20 @@ 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) -> &mut Option<InteractiveTxConstructor>;
2307+
fn interactive_tx_signing_session_mut(&mut self) -> &mut Option<InteractiveTxSigningSession>;
2308+
22962309
/// Prepare and start interactive transaction negotiation.
22972310
/// `change_destination_opt` - Optional destination for optional change; if None,
22982311
/// default destination address is used.
@@ -2304,11 +2317,11 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23042317
) -> Result<Option<InteractiveTxMessageSend>, AbortReason>
23052318
where ES::Target: EntropySource
23062319
{
2307-
debug_assert!(matches!(self.context.channel_state, ChannelState::NegotiatingFunding(_)));
2308-
debug_assert!(self.interactive_tx_constructor.is_none());
2320+
debug_assert!(matches!(self.context().channel_state, ChannelState::NegotiatingFunding(_)));
2321+
debug_assert!(self.interactive_tx_constructor().is_none());
23092322

23102323
let mut funding_inputs = Vec::new();
2311-
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
2324+
self.swap_out_dual_funding_context_inputs(&mut funding_inputs);
23122325

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

@@ -2319,14 +2332,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23192332
let mut expected_remote_shared_funding_output = None;
23202333

23212334
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(),
2335+
value: Amount::from_sat(self.funding().get_value_satoshis()),
2336+
script_pubkey: self.funding().get_funding_redeemscript().to_p2wsh(),
23242337
};
23252338

2326-
if self.funding.is_outbound() {
2339+
if self.funding().is_outbound() {
23272340
funding_outputs.push(
23282341
OutputOwned::Shared(SharedOwnedOutput::new(
2329-
shared_funding_output, self.dual_funding_context.our_funding_satoshis,
2342+
shared_funding_output, self.dual_funding_context().our_funding_satoshis,
23302343
))
23312344
);
23322345
} else {
@@ -2338,13 +2351,13 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23382351
let change_script = if let Some(script) = change_destination_opt {
23392352
script
23402353
} else {
2341-
signer_provider.get_destination_script(self.context.channel_keys_id)
2354+
signer_provider.get_destination_script(self.context().channel_keys_id)
23422355
.map_err(|_err| AbortReason::InternalError("Error getting destination script"))?
23432356
};
23442357
let change_value_opt = calculate_change_output_value(
2345-
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
2358+
self.funding().is_outbound(), self.dual_funding_context().our_funding_satoshis,
23462359
&funding_inputs, &funding_outputs,
2347-
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
2360+
self.dual_funding_context().funding_feerate_sat_per_1000_weight,
23482361
change_script.minimal_non_dust().to_sat(),
23492362
)?;
23502363
if let Some(change_value) = change_value_opt {
@@ -2353,10 +2366,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23532366
script_pubkey: change_script,
23542367
};
23552368
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);
2369+
let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
23572370
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
23582371
// Check dust limit again
2359-
if change_value_decreased_with_fee > self.context.holder_dust_limit_satoshis {
2372+
if change_value_decreased_with_fee > self.context().holder_dust_limit_satoshis {
23602373
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
23612374
funding_outputs.push(OutputOwned::Single(change_output));
23622375
}
@@ -2365,70 +2378,71 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23652378
let constructor_args = InteractiveTxConstructorArgs {
23662379
entropy_source,
23672380
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,
2381+
counterparty_node_id: self.context().counterparty_node_id,
2382+
channel_id: self.context().channel_id(),
2383+
feerate_sat_per_kw: self.dual_funding_context().funding_feerate_sat_per_1000_weight,
2384+
is_initiator: self.funding().is_outbound(),
2385+
funding_tx_locktime: self.dual_funding_context().funding_tx_locktime,
23732386
inputs_to_contribute: funding_inputs,
23742387
outputs_to_contribute: funding_outputs,
23752388
expected_remote_shared_funding_output,
23762389
};
23772390
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
23782391
let msg = tx_constructor.take_initiator_first_message();
23792392

2380-
self.interactive_tx_constructor = Some(tx_constructor);
2393+
*self.interactive_tx_constructor_mut() = Some(tx_constructor);
23812394

23822395
Ok(msg)
23832396
}
23842397

2385-
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2386-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2398+
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
2399+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
23872400
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())),
2401+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
23892402
None => Err(msgs::TxAbort {
2390-
channel_id: self.context.channel_id(),
2403+
channel_id: self.context().channel_id(),
23912404
data: b"No interactive transaction negotiation in progress".to_vec()
23922405
}),
23932406
})
23942407
}
23952408

2396-
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2397-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2409+
fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
2410+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
23982411
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())),
2412+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24002413
None => Err(msgs::TxAbort {
2401-
channel_id: self.context.channel_id(),
2414+
channel_id: self.context().channel_id(),
24022415
data: b"No interactive transaction negotiation in progress".to_vec()
24032416
}),
24042417
})
24052418
}
24062419

2407-
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2408-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2420+
fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
2421+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24092422
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())),
2423+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24112424
None => Err(msgs::TxAbort {
2412-
channel_id: self.context.channel_id(),
2425+
channel_id: self.context().channel_id(),
24132426
data: b"No interactive transaction negotiation in progress".to_vec()
24142427
}),
24152428
})
24162429
}
24172430

2418-
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2419-
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
2431+
fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
2432+
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
24202433
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())),
2434+
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
24222435
None => Err(msgs::TxAbort {
2423-
channel_id: self.context.channel_id(),
2436+
channel_id: self.context().channel_id(),
24242437
data: b"No interactive transaction negotiation in progress".to_vec()
24252438
}),
24262439
})
24272440
}
24282441

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,
2442+
fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
2443+
let interactive_tx_constructor = self.interactive_tx_constructor_mut();
2444+
let tx_constructor = match interactive_tx_constructor {
2445+
Some(tx_constructor) => tx_constructor,
24322446
None => {
24332447
let tx_abort = msgs::TxAbort {
24342448
channel_id: msg.channel_id,
@@ -2446,25 +2460,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24462460
};
24472461

24482462
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
2449-
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
2463+
self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
24502464
};
24512465

24522466
HandleTxCompleteResult(Ok(tx_complete))
24532467
}
24542468

2455-
pub fn funding_tx_constructed<L: Deref>(
2469+
fn funding_tx_constructed<L: Deref>(
24562470
&mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
24572471
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
24582472
where
24592473
L::Target: Logger
24602474
{
2461-
let our_funding_satoshis = self.dual_funding_context.our_funding_satoshis;
2462-
let transaction_number = self.unfunded_context.transaction_number();
2475+
let our_funding_satoshis = self.dual_funding_context().our_funding_satoshis;
2476+
let transaction_number = self.unfunded_context().transaction_number();
24632477

24642478
let mut output_index = None;
2465-
let expected_spk = self.funding.get_funding_redeemscript().to_p2wsh();
2479+
let expected_spk = self.funding().get_funding_redeemscript().to_p2wsh();
24662480
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
2467-
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding.get_value_satoshis() {
2481+
if outp.script_pubkey() == &expected_spk && outp.value() == self.funding().get_value_satoshis() {
24682482
if output_index.is_some() {
24692483
return Err(ChannelError::Close(
24702484
(
@@ -2484,24 +2498,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
24842498
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
24852499
)));
24862500
};
2487-
self.funding.channel_transaction_parameters.funding_outpoint = Some(outpoint);
2501+
self.funding_mut().channel_transaction_parameters.funding_outpoint = Some(outpoint);
24882502

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);
2503+
self.context().assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2504+
let (funding_mut, context_mut) = self.funding_and_context_mut();
2505+
let commitment_signed = context_mut.get_initial_commitment_signed(&funding_mut, logger);
24912506
let commitment_signed = match commitment_signed {
24922507
Ok(commitment_signed) => {
2493-
self.funding.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
2508+
self.funding_mut().funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
24942509
commitment_signed
24952510
},
24962511
Err(err) => {
2497-
self.funding.channel_transaction_parameters.funding_outpoint = None;
2512+
self.funding_mut().channel_transaction_parameters.funding_outpoint = None;
24982513
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })));
24992514
},
25002515
};
25012516

25022517
let funding_ready_for_sig_event = if signing_session.local_inputs_count() == 0 {
25032518
debug_assert_eq!(our_funding_satoshis, 0);
2504-
if signing_session.provide_holder_witnesses(self.context.channel_id, Vec::new()).is_err() {
2519+
if signing_session.provide_holder_witnesses(self.context().channel_id, Vec::new()).is_err() {
25052520
debug_assert!(
25062521
false,
25072522
"Zero inputs were provided & zero witnesses were provided, but a count mismatch was somehow found",
@@ -2537,16 +2552,72 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
25372552
)));
25382553
};
25392554

2540-
self.context.channel_state = ChannelState::FundingNegotiated;
2555+
self.context_mut().channel_state = ChannelState::FundingNegotiated;
25412556

25422557
// Clear the interactive transaction constructor
2543-
self.interactive_tx_constructor.take();
2544-
self.interactive_tx_signing_session = Some(signing_session);
2558+
*self.interactive_tx_constructor_mut() = None;
2559+
*self.interactive_tx_signing_session_mut() = Some(signing_session);
25452560

25462561
Ok((commitment_signed, funding_ready_for_sig_event))
25472562
}
25482563
}
25492564

2565+
impl<SP: Deref> PendingV2ChannelTrait<SP> for PendingV2Channel<SP> where SP::Target: SignerProvider {
2566+
#[inline]
2567+
fn context(&self) -> &ChannelContext<SP> {
2568+
&self.context
2569+
}
2570+
2571+
#[inline]
2572+
fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2573+
&mut self.context
2574+
}
2575+
2576+
#[inline]
2577+
fn funding(&self) -> &FundingScope {
2578+
&self.funding
2579+
}
2580+
2581+
#[inline]
2582+
fn funding_mut(&mut self) -> &mut FundingScope {
2583+
&mut self.funding
2584+
}
2585+
2586+
#[inline]
2587+
fn funding_and_context_mut(&mut self) -> (&mut FundingScope, &mut ChannelContext<SP>) {
2588+
(&mut self.funding, &mut self.context)
2589+
}
2590+
2591+
#[inline]
2592+
fn dual_funding_context(&self) -> &DualFundingChannelContext {
2593+
&self.dual_funding_context
2594+
}
2595+
2596+
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>) {
2597+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, funding_inputs);
2598+
}
2599+
2600+
#[inline]
2601+
fn unfunded_context(&self) -> &UnfundedChannelContext {
2602+
&self.unfunded_context
2603+
}
2604+
2605+
#[inline]
2606+
fn interactive_tx_constructor(&self) -> Option<&InteractiveTxConstructor> {
2607+
self.interactive_tx_constructor.as_ref()
2608+
}
2609+
2610+
#[inline]
2611+
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
2612+
&mut self.interactive_tx_constructor
2613+
}
2614+
2615+
#[inline]
2616+
fn interactive_tx_signing_session_mut(&mut self) -> &mut Option<InteractiveTxSigningSession> {
2617+
&mut self.interactive_tx_signing_session
2618+
}
2619+
}
2620+
25502621
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25512622
fn new_for_inbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
25522623
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)