Skip to content

Commit

Permalink
[Custom Transactions] Demo the TxBuilder trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tankyleo committed Feb 24, 2025
1 parent b358cb6 commit 0c96878
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 190 deletions.
56 changes: 43 additions & 13 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
feerate_per_kw: Option<u32>,
to_broadcaster_value_sat: Option<u64>,
to_countersignatory_value_sat: Option<u64>,
// See docs for `CommitmentStats`, and `ChannelContext::build_commitment_transaction`
value_to_self_with_offset_msat: Option<u64>,
counterparty_dust_limit_satoshis: Option<u64>,
},
PaymentPreimage {
payment_preimage: PaymentPreimage,
Expand Down Expand Up @@ -598,6 +601,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_satoshis, option),
},
(2, PaymentPreimage) => {
(0, payment_preimage, required),
Expand Down Expand Up @@ -1024,8 +1029,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_with_offset_msat, counterparty_dust_limit_satoshis)
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 +1506,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_with_offset_msat: u64, counterparty_dust_limit_satoshis: 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_with_offset_msat, counterparty_dust_limit_satoshis, &logger);
}

/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
Expand Down Expand Up @@ -2874,10 +2880,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_with_offset_msat: u64, counterparty_dust_limit_satoshis: 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_with_offset_msat, counterparty_dust_limit_satoshis));

#[cfg(debug_assertions)] {
let rebuilt_commitment_tx = self.initial_counterparty_commitment_tx().unwrap();
Expand Down Expand Up @@ -3437,14 +3444,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 (their_per_commitment_point, feerate_per_kw, _to_broadcaster_value,
_to_countersignatory_value, value_to_self_with_offset_msat, counterparty_dust_limit_satoshis) = self.initial_counterparty_commitment_info?;
let channel_transaction_parameters = &self.onchain_tx_handler.channel_transaction_parameters;
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)
use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
let stats = TxBuilder::build_commitment_transaction(&SpecTxBuilder {}, false, INITIAL_COMMITMENT_NUMBER,
&their_per_commitment_point, channel_transaction_parameters, &self.onchain_tx_handler.secp_ctx,
self.channel_value_satoshis, value_to_self_with_offset_msat, htlc_outputs, feerate_per_kw, counterparty_dust_limit_satoshis);

Some(stats.tx)
}

fn build_counterparty_commitment_tx(
Expand Down Expand Up @@ -3477,7 +3487,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_satoshis: None } => {

let nondust_htlcs = htlc_outputs.iter().filter_map(|(htlc, _)| {
htlc.transaction_output_index.map(|_| (htlc.clone(), None))
Expand All @@ -3491,6 +3502,25 @@ 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_satoshis: Some(dust_limit_satoshis) } => {

let channel_transaction_parameters = &self.onchain_tx_handler.channel_transaction_parameters;
let htlc_outputs = 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 stats = TxBuilder::build_commitment_transaction(&SpecTxBuilder {}, 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,
htlc_outputs, feerate_per_kw, dust_limit_satoshis);

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

Some(stats.tx)
},
_ => None,
}
}).collect()
Expand Down
Loading

0 comments on commit 0c96878

Please sign in to comment.