@@ -2292,7 +2292,20 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where
2292
2292
}
2293
2293
}
2294
2294
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
+
2296
2309
/// Prepare and start interactive transaction negotiation.
2297
2310
/// `change_destination_opt` - Optional destination for optional change; if None,
2298
2311
/// default destination address is used.
@@ -2304,11 +2317,11 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2304
2317
) -> Result<Option<InteractiveTxMessageSend>, AbortReason>
2305
2318
where ES::Target: EntropySource
2306
2319
{
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());
2309
2322
2310
2323
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);
2312
2325
2313
2326
// TODO(splicing): Add prev funding tx as input, must be provided as a parameter
2314
2327
@@ -2319,14 +2332,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2319
2332
let mut expected_remote_shared_funding_output = None;
2320
2333
2321
2334
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(),
2324
2337
};
2325
2338
2326
- if self.funding.is_outbound() {
2339
+ if self.funding() .is_outbound() {
2327
2340
funding_outputs.push(
2328
2341
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,
2330
2343
))
2331
2344
);
2332
2345
} else {
@@ -2338,13 +2351,13 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2338
2351
let change_script = if let Some(script) = change_destination_opt {
2339
2352
script
2340
2353
} else {
2341
- signer_provider.get_destination_script(self.context.channel_keys_id)
2354
+ signer_provider.get_destination_script(self.context() .channel_keys_id)
2342
2355
.map_err(|_err| AbortReason::InternalError("Error getting destination script"))?
2343
2356
};
2344
2357
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,
2346
2359
&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,
2348
2361
change_script.minimal_non_dust().to_sat(),
2349
2362
)?;
2350
2363
if let Some(change_value) = change_value_opt {
@@ -2353,10 +2366,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2353
2366
script_pubkey: change_script,
2354
2367
};
2355
2368
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);
2357
2370
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
2358
2371
// 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 {
2360
2373
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
2361
2374
funding_outputs.push(OutputOwned::Single(change_output));
2362
2375
}
@@ -2365,70 +2378,71 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2365
2378
let constructor_args = InteractiveTxConstructorArgs {
2366
2379
entropy_source,
2367
2380
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,
2373
2386
inputs_to_contribute: funding_inputs,
2374
2387
outputs_to_contribute: funding_outputs,
2375
2388
expected_remote_shared_funding_output,
2376
2389
};
2377
2390
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
2378
2391
let msg = tx_constructor.take_initiator_first_message();
2379
2392
2380
- self.interactive_tx_constructor = Some(tx_constructor);
2393
+ * self.interactive_tx_constructor_mut() = Some(tx_constructor);
2381
2394
2382
2395
Ok(msg)
2383
2396
}
2384
2397
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() {
2387
2400
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())),
2389
2402
None => Err(msgs::TxAbort {
2390
- channel_id: self.context.channel_id(),
2403
+ channel_id: self.context() .channel_id(),
2391
2404
data: b"No interactive transaction negotiation in progress".to_vec()
2392
2405
}),
2393
2406
})
2394
2407
}
2395
2408
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() {
2398
2411
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())),
2400
2413
None => Err(msgs::TxAbort {
2401
- channel_id: self.context.channel_id(),
2414
+ channel_id: self.context() .channel_id(),
2402
2415
data: b"No interactive transaction negotiation in progress".to_vec()
2403
2416
}),
2404
2417
})
2405
2418
}
2406
2419
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() {
2409
2422
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())),
2411
2424
None => Err(msgs::TxAbort {
2412
- channel_id: self.context.channel_id(),
2425
+ channel_id: self.context() .channel_id(),
2413
2426
data: b"No interactive transaction negotiation in progress".to_vec()
2414
2427
}),
2415
2428
})
2416
2429
}
2417
2430
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() {
2420
2433
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())),
2422
2435
None => Err(msgs::TxAbort {
2423
- channel_id: self.context.channel_id(),
2436
+ channel_id: self.context() .channel_id(),
2424
2437
data: b"No interactive transaction negotiation in progress".to_vec()
2425
2438
}),
2426
2439
})
2427
2440
}
2428
2441
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,
2432
2446
None => {
2433
2447
let tx_abort = msgs::TxAbort {
2434
2448
channel_id: msg.channel_id,
@@ -2446,25 +2460,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2446
2460
};
2447
2461
2448
2462
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());
2450
2464
};
2451
2465
2452
2466
HandleTxCompleteResult(Ok(tx_complete))
2453
2467
}
2454
2468
2455
- pub fn funding_tx_constructed<L: Deref>(
2469
+ fn funding_tx_constructed<L: Deref>(
2456
2470
&mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2457
2471
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
2458
2472
where
2459
2473
L::Target: Logger
2460
2474
{
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();
2463
2477
2464
2478
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();
2466
2480
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() {
2468
2482
if output_index.is_some() {
2469
2483
return Err(ChannelError::Close(
2470
2484
(
@@ -2484,24 +2498,25 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2484
2498
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2485
2499
)));
2486
2500
};
2487
- self.funding .channel_transaction_parameters.funding_outpoint = Some(outpoint);
2501
+ self.funding_mut() .channel_transaction_parameters.funding_outpoint = Some(outpoint);
2488
2502
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);
2491
2506
let commitment_signed = match commitment_signed {
2492
2507
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());
2494
2509
commitment_signed
2495
2510
},
2496
2511
Err(err) => {
2497
- self.funding .channel_transaction_parameters.funding_outpoint = None;
2512
+ self.funding_mut() .channel_transaction_parameters.funding_outpoint = None;
2498
2513
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })));
2499
2514
},
2500
2515
};
2501
2516
2502
2517
let funding_ready_for_sig_event = if signing_session.local_inputs_count() == 0 {
2503
2518
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() {
2505
2520
debug_assert!(
2506
2521
false,
2507
2522
"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 {
2537
2552
)));
2538
2553
};
2539
2554
2540
- self.context .channel_state = ChannelState::FundingNegotiated;
2555
+ self.context_mut() .channel_state = ChannelState::FundingNegotiated;
2541
2556
2542
2557
// 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);
2545
2560
2546
2561
Ok((commitment_signed, funding_ready_for_sig_event))
2547
2562
}
2548
2563
}
2549
2564
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
+
2550
2621
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2551
2622
fn new_for_inbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
2552
2623
fee_estimator: &'a LowerBoundedFeeEstimator<F>,
0 commit comments