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