Skip to content

Commit

Permalink
move all commitment tx builds to new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tankyleo committed Feb 23, 2025
1 parent 27ef8cc commit d9c0952
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
58 changes: 43 additions & 15 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ pub(crate) enum ChannelMonitorUpdateStep {
feerate_per_kw: Option<u32>,
to_broadcaster_value_sat: Option<u64>,
to_countersignatory_value_sat: Option<u64>,
value_to_self_with_offset_msat: Option<u64>,
counterparty_dust_limit_sat: Option<u64>,
},
PaymentPreimage {
payment_preimage: PaymentPreimage,
Expand Down Expand Up @@ -598,6 +600,8 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
(4, their_per_commitment_point, required),
(5, to_countersignatory_value_sat, option),
(6, htlc_outputs, required_vec),
(7, value_to_self_with_offset_msat, option),
(9, counterparty_dust_limit_sat, option),
},
(2, PaymentPreimage) => {
(0, payment_preimage, required),
Expand Down Expand Up @@ -1024,8 +1028,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// monitors created after 0.0.117.
///
/// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
/// to_countersignatory_sats)
initial_counterparty_commitment_info: Option<(PublicKey, u32, u64, u64)>,
/// to_countersignatory_sats, value_to_self_msat, their_dust_limit)
initial_counterparty_commitment_info: Option<(PublicKey, u32, u64, u64, u64, u64)>,

/// The first block height at which we had no remaining claimable balances.
balances_empty_height: Option<u32>,
Expand Down Expand Up @@ -1501,15 +1505,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
pub(crate) fn provide_initial_counterparty_commitment_tx<L: Deref>(
&self, txid: Txid, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
commitment_number: u64, their_cur_per_commitment_point: PublicKey, feerate_per_kw: u32,
to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, logger: &L,
to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, value_to_self_msat: u64, counterparty_dust_limit: u64,
logger: &L,
)
where L::Target: Logger
{
let mut inner = self.inner.lock().unwrap();
let logger = WithChannelMonitor::from_impl(logger, &*inner, None);
inner.provide_initial_counterparty_commitment_tx(txid,
htlc_outputs, commitment_number, their_cur_per_commitment_point, feerate_per_kw,
to_broadcaster_value_sat, to_countersignatory_value_sat, &logger);
to_broadcaster_value_sat, to_countersignatory_value_sat, value_to_self_msat, counterparty_dust_limit, &logger);
}

/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
Expand Down Expand Up @@ -2874,10 +2879,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
fn provide_initial_counterparty_commitment_tx<L: Deref>(
&mut self, txid: Txid, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
commitment_number: u64, their_per_commitment_point: PublicKey, feerate_per_kw: u32,
to_broadcaster_value: u64, to_countersignatory_value: u64, logger: &WithChannelMonitor<L>,
to_broadcaster_value: u64, to_countersignatory_value: u64, value_to_self_msat: u64, counterparty_dust_limit: u64,
logger: &WithChannelMonitor<L>,
) where L::Target: Logger {
self.initial_counterparty_commitment_info = Some((their_per_commitment_point.clone(),
feerate_per_kw, to_broadcaster_value, to_countersignatory_value));
feerate_per_kw, to_broadcaster_value, to_countersignatory_value, value_to_self_msat, counterparty_dust_limit));

#[cfg(debug_assertions)] {
let rebuilt_commitment_tx = self.initial_counterparty_commitment_tx().unwrap();
Expand Down Expand Up @@ -3437,14 +3443,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}

fn initial_counterparty_commitment_tx(&mut self) -> Option<CommitmentTransaction> {
let (their_per_commitment_point, feerate_per_kw, to_broadcaster_value,
to_countersignatory_value) = self.initial_counterparty_commitment_info?;
let htlc_outputs = vec![];

let commitment_tx = self.build_counterparty_commitment_tx(INITIAL_COMMITMENT_NUMBER,
&their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
feerate_per_kw, htlc_outputs);
Some(commitment_tx)
let (their_per_commitment_point, feerate_per_kw, _to_broadcaster_value,
_to_countersignatory_value, value_to_self_msat, dust_limit) = self.initial_counterparty_commitment_info?;

let htlcs = Vec::new();
let channel_transaction_parameters = &self.onchain_tx_handler.channel_transaction_parameters;

use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
let tx_builder = SpecTxBuilder {};
let stats = tx_builder.build_commitment_transaction(false, INITIAL_COMMITMENT_NUMBER, &their_per_commitment_point, channel_transaction_parameters, &self.onchain_tx_handler.secp_ctx, self.channel_value_satoshis, value_to_self_msat, htlcs, feerate_per_kw, dust_limit);

Some(stats.tx)
}

fn build_counterparty_commitment_tx(
Expand Down Expand Up @@ -3477,7 +3486,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
ref htlc_outputs, commitment_number, their_per_commitment_point,
feerate_per_kw: Some(feerate_per_kw),
to_broadcaster_value_sat: Some(to_broadcaster_value),
to_countersignatory_value_sat: Some(to_countersignatory_value) } => {
to_countersignatory_value_sat: Some(to_countersignatory_value),
value_to_self_with_offset_msat: None, counterparty_dust_limit_sat: None } => {

let nondust_htlcs = htlc_outputs.iter().filter_map(|(htlc, _)| {
htlc.transaction_output_index.map(|_| (htlc.clone(), None))
Expand All @@ -3491,6 +3501,24 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {

Some(commitment_tx)
},
&ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid,
ref htlc_outputs, commitment_number, their_per_commitment_point,
feerate_per_kw: Some(feerate_per_kw),
to_broadcaster_value_sat: Some(_to_broadcaster_value),
to_countersignatory_value_sat: Some(_to_countersignatory_value),
value_to_self_with_offset_msat: Some(value_to_self_with_offset), counterparty_dust_limit_sat: Some(dust_limit) } => {

let channel_transaction_parameters = &self.onchain_tx_handler.channel_transaction_parameters;
let htlcs = htlc_outputs.iter().map(|(htlc, source)| (htlc.clone(), source.as_ref().map(|s| s.as_ref()))).collect();

use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
let tx_builder = SpecTxBuilder {};
let stats = tx_builder.build_commitment_transaction(false, commitment_number, &their_per_commitment_point, channel_transaction_parameters, &self.onchain_tx_handler.secp_ctx, self.channel_value_satoshis, value_to_self_with_offset, htlcs, feerate_per_kw, dust_limit);

debug_assert_eq!(stats.tx.trust().txid(), commitment_txid);

Some(stats.tx)
},
_ => None,
}
}).collect()
Expand Down
28 changes: 19 additions & 9 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ pub(crate) struct CommitmentStats<'a> {
pub(crate) htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, // the list of HTLCs (dust HTLCs *included*) which were not ignored when building the transaction
pub(crate) local_balance_msat: u64, // local balance before fees *not* considering dust limits
pub(crate) remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
pub(crate) value_to_self_with_offset_msat: u64 // balance without htlcs, just accounting for the offset
}

/// Used when calculating whether we or the remote can afford an additional HTLC.
Expand Down Expand Up @@ -1980,6 +1981,8 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
counterparty_initial_commitment_tx.feerate_per_kw(),
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
counterparty_initial_commitment_tx.to_countersignatory_value_sat(),
context.value_to_self_msat,
context.counterparty_dust_limit_satoshis,
logger);

context.cur_counterparty_commitment_transaction_number -= 1;
Expand Down Expand Up @@ -8186,11 +8189,15 @@ impl<SP: Deref> FundedChannel<SP> where
}
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;

let (mut htlcs_ref, counterparty_commitment_tx) =
let commitment_stats =
self.build_commitment_no_state_update(logger);
let counterparty_commitment_txid = counterparty_commitment_tx.trust().txid();
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
let htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
htlcs_ref.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
commitment_stats.htlcs_included.into_iter().map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
let feerate_per_kw = Some(commitment_stats.feerate_per_kw);
let to_broadcaster_value_sat = Some(commitment_stats.tx.to_broadcaster_value_sat());
let to_countersignatory_value_sat = Some(commitment_stats.tx.to_countersignatory_value_sat());
let value_to_self_with_offset_msat = Some(commitment_stats.value_to_self_with_offset_msat);

if self.context.announcement_sigs_state == AnnouncementSigsState::MessageSent {
self.context.announcement_sigs_state = AnnouncementSigsState::Committed;
Expand All @@ -8205,9 +8212,13 @@ impl<SP: Deref> FundedChannel<SP> where
htlc_outputs: htlcs.clone(),
commitment_number: self.context.cur_counterparty_commitment_transaction_number,
their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap(),
feerate_per_kw: Some(counterparty_commitment_tx.feerate_per_kw()),
to_broadcaster_value_sat: Some(counterparty_commitment_tx.to_broadcaster_value_sat()),
to_countersignatory_value_sat: Some(counterparty_commitment_tx.to_countersignatory_value_sat()),
feerate_per_kw,
to_broadcaster_value_sat,
to_countersignatory_value_sat,
value_to_self_with_offset_msat,
counterparty_dust_limit_sat: Some(self.context.counterparty_dust_limit_satoshis),
//value_to_self_with_offset_msat: None,
//counterparty_dust_limit_sat: None,
}],
channel_id: Some(self.context.channel_id()),
};
Expand All @@ -8216,12 +8227,11 @@ impl<SP: Deref> FundedChannel<SP> where
}

fn build_commitment_no_state_update<L: Deref>(&self, logger: &L)
-> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
-> CommitmentStats
where L::Target: Logger
{
let counterparty_keys = self.context.build_remote_transaction_keys();
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
let counterparty_commitment_tx = commitment_stats.tx;

#[cfg(any(test, fuzzing))]
{
Expand All @@ -8241,7 +8251,7 @@ impl<SP: Deref> FundedChannel<SP> where
}
}

(commitment_stats.htlcs_included, counterparty_commitment_tx)
commitment_stats
}

/// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed
Expand Down
6 changes: 4 additions & 2 deletions lightning/src/sign/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) trait TxBuilder {
fn build_commitment_transaction<'a>(
&self, local: bool, commitment_number: u64, per_commitment_point: &PublicKey,
channel_parameters: &ChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>,
channel_value_satoshis: u64, value_to_self_msat: u64,
channel_value_satoshis: u64, value_to_self_with_offset_msat: u64,
htlcs_in_tx: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, feerate_per_kw: u32,
broadcaster_dust_limit_satoshis: u64,
) -> CommitmentStats<'a>;
Expand All @@ -36,14 +36,15 @@ impl TxBuilder for SpecTxBuilder {
fn build_commitment_transaction<'a>(
&self, local: bool, commitment_number: u64, per_commitment_point: &PublicKey,
channel_parameters: &ChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>,
channel_value_satoshis: u64, mut value_to_self_msat: u64,
channel_value_satoshis: u64, value_to_self_with_offset_msat: u64,
htlcs_in_tx: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, feerate_per_kw: u32,
broadcaster_dust_limit_satoshis: u64,
) -> CommitmentStats<'a> {
let mut local_htlc_total_msat = 0;
let mut remote_htlc_total_msat = 0;
let mut included_non_dust_htlcs = Vec::new();
let mut included_dust_htlcs = Vec::new();
let mut value_to_self_msat = value_to_self_with_offset_msat;

let params = if local {
channel_parameters.as_holder_broadcastable()
Expand Down Expand Up @@ -171,6 +172,7 @@ impl TxBuilder for SpecTxBuilder {
htlcs_included: all_htlcs,
local_balance_msat: value_to_self_msat,
remote_balance_msat: value_to_remote_msat,
value_to_self_with_offset_msat,
}
}
}

0 comments on commit d9c0952

Please sign in to comment.