Skip to content

Commit b0a15c1

Browse files
committed
f - attempt to move build_commitment_transaction into ScopedChannelContext
Unfortunately, there's lifetime issue with the return value of build_commitment_transaction. It seems the impl block introduced prevents knowing the lifetime on the returned CommitmentStats isn't outlived after the method returns. It should be the same as the ChannelContext, but it seems that information is lost the compiler. Is there anyway to fix this without passing in a FundingScope directly?
1 parent 4424495 commit b0a15c1

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22592259
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
22602260

22612261
self.context.assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
2262-
let commitment_signed = self.context.get_initial_commitment_signed(logger);
2262+
let commitment_signed = self.context_mut().get_initial_commitment_signed(logger);
22632263
let commitment_signed = match commitment_signed {
22642264
Ok(commitment_signed) => {
22652265
self.context.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
@@ -3424,7 +3424,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34243424
!self.channel_state.is_pre_funded_state() &&
34253425
!matches!(self.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH))
34263426
}
3427+
}
34273428

3429+
impl<C, F, SP: Deref> ScopedChannelContext<C, F, SP>
3430+
where
3431+
C: Deref<Target = ChannelContext<SP>>,
3432+
F: Deref<Target = FundingScope>,
3433+
SP::Target: SignerProvider,
3434+
{
34283435
/// Transaction nomenclature is somewhat confusing here as there are many different cases - a
34293436
/// transaction is referred to as "a's transaction" implying that a will be able to broadcast
34303437
/// the transaction. Thus, b will generally be sending a signature over such a transaction to
@@ -3595,7 +3602,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35953602
// AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
35963603
// "violate" their reserve value by couting those against it. Thus, we have to convert
35973604
// everything to i64 before subtracting as otherwise we can overflow.
3598-
let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000) as i64 - (self.value_to_self_msat as i64) - (remote_htlc_total_msat as i64) - value_to_self_msat_offset;
3605+
let value_to_remote_msat: i64 = (self.channel_value_satoshis() * 1000) as i64 - (self.value_to_self_msat as i64) - (remote_htlc_total_msat as i64) - value_to_self_msat_offset;
35993606
assert!(value_to_remote_msat >= 0);
36003607

36013608
#[cfg(debug_assertions)]
@@ -3673,7 +3680,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36733680
outbound_htlc_preimages,
36743681
}
36753682
}
3683+
}
36763684

3685+
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36773686
#[inline]
36783687
/// Creates a set of keys for build_commitment_transaction to generate a transaction which our
36793688
/// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
@@ -4510,7 +4519,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45104519
msg_name);
45114520
}
45124521
}
4522+
}
45134523

4524+
impl<C, F, SP: Deref> ScopedChannelContext<C, F, SP>
4525+
where
4526+
C: Deref<Target = ChannelContext<SP>>,
4527+
F: Deref<Target = FundingScope>,
4528+
SP::Target: SignerProvider,
4529+
{
45144530
fn get_initial_counterparty_commitment_signature<L: Deref>(
45154531
&self, logger: &L
45164532
) -> Result<Signature, ChannelError>
@@ -4537,7 +4553,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
45374553
_ => todo!(),
45384554
}
45394555
}
4556+
}
45404557

4558+
impl<C, F, SP: Deref> ScopedChannelContext<C, F, SP>
4559+
where
4560+
C: DerefMut<Target = ChannelContext<SP>>,
4561+
F: DerefMut<Target = FundingScope>,
4562+
SP::Target: SignerProvider,
4563+
{
45414564
fn get_initial_commitment_signed<L: Deref>(
45424565
&mut self, logger: &L
45434566
) -> Result<msgs::CommitmentSigned, ChannelError>
@@ -5527,7 +5550,8 @@ impl<SP: Deref> FundedChannel<SP> where
55275550

55285551
let keys = self.context.build_holder_transaction_keys(self.holder_commitment_point.current_point());
55295552

5530-
let commitment_stats = self.context.build_commitment_transaction(self.holder_commitment_point.transaction_number(), &keys, true, false, logger);
5553+
let funding_context = self.context();
5554+
let commitment_stats = funding_context.build_commitment_transaction(self.holder_commitment_point.transaction_number(), &keys, true, false, logger);
55315555
let commitment_txid = {
55325556
let trusted_tx = commitment_stats.tx.trust();
55335557
let bitcoin_tx = trusted_tx.built_transaction();
@@ -6279,7 +6303,8 @@ impl<SP: Deref> FundedChannel<SP> where
62796303
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
62806304
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
62816305
let keys = self.context.build_holder_transaction_keys(self.holder_commitment_point.current_point());
6282-
let commitment_stats = self.context.build_commitment_transaction(self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
6306+
let funding_context = self.context();
6307+
let commitment_stats = funding_context.build_commitment_transaction(self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
62836308
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
62846309
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
62856310
if holder_balance_msat < buffer_fee_msat + self.context.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -6580,7 +6605,7 @@ impl<SP: Deref> FundedChannel<SP> where
65806605
}
65816606
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
65826607
let counterparty_keys = self.context.build_remote_transaction_keys();
6583-
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
6608+
let counterparty_initial_commitment_tx = self.context().build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
65846609
self.context.get_funding_signed_msg(logger, counterparty_initial_commitment_tx)
65856610
} else { None };
65866611
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
@@ -8506,7 +8531,8 @@ impl<SP: Deref> FundedChannel<SP> where
85068531
where L::Target: Logger
85078532
{
85088533
let counterparty_keys = self.context.build_remote_transaction_keys();
8509-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8534+
let funding_context = self.context();
8535+
let commitment_stats = funding_context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
85108536
let counterparty_commitment_tx = commitment_stats.tx;
85118537

85128538
#[cfg(any(test, fuzzing))]
@@ -8538,7 +8564,8 @@ impl<SP: Deref> FundedChannel<SP> where
85388564
self.build_commitment_no_state_update(logger);
85398565

85408566
let counterparty_keys = self.context.build_remote_transaction_keys();
8541-
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8567+
let funding_context = self.context();
8568+
let commitment_stats = funding_context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
85428569
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
85438570

85448571
match &self.context.holder_signer {
@@ -8815,7 +8842,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
88158842
/// Only allowed after [`ChannelContext::channel_transaction_parameters`] is set.
88168843
fn get_funding_created_msg<L: Deref>(&mut self, logger: &L) -> Option<msgs::FundingCreated> where L::Target: Logger {
88178844
let counterparty_keys = self.context.build_remote_transaction_keys();
8818-
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
8845+
let counterparty_initial_commitment_tx = self.context().build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
88198846
let signature = match &self.context.holder_signer {
88208847
// TODO (taproot|arik): move match into calling method for Taproot
88218848
ChannelSignerType::Ecdsa(ecdsa) => {

0 commit comments

Comments
 (0)