Skip to content

Commit 2adce74

Browse files
committed
Produce a single monitor update per commitment number
Such updates will contain a single HTLC-source table for that commitment number, together with a vector of commitment transactions created at that commitment number. This vector will grow with the number of pending splices for a channel. The HTLC-source table produced by channel will stop storing information on which HTLCs were assigned which output index. Instead, this information will be stored in each commitment transaction in the monitor update. This commit deduplicates the `HTLCSource` collection for each commitment number, but `HTLCOutputData` would store information already stored in each `CommitmentTransaction` in a monitor update. This is a partial rework of b8a03cd. That commit has not been shipped in a release yet.
1 parent 83e9e80 commit 2adce74

File tree

3 files changed

+105
-38
lines changed

3 files changed

+105
-38
lines changed

lightning/src/chain/channelmonitor.rs

+60-17
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::types::features::ChannelTypeFeatures;
3838
use crate::types::payment::{PaymentHash, PaymentPreimage};
3939
use crate::ln::msgs::DecodeError;
4040
use crate::ln::channel_keys::{DelayedPaymentKey, DelayedPaymentBasepoint, HtlcBasepoint, HtlcKey, RevocationKey, RevocationBasepoint};
41-
use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction};
41+
use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, HTLCOutputData};
4242
use crate::ln::channelmanager::{HTLCSource, SentHTLCId, PaymentClaimDetails};
4343
use crate::chain;
4444
use crate::chain::{BestBlock, WatchedOutput};
@@ -591,11 +591,16 @@ pub(crate) enum ChannelMonitorUpdateStep {
591591
to_broadcaster_value_sat: Option<u64>,
592592
to_countersignatory_value_sat: Option<u64>,
593593
},
594-
LatestCounterpartyCommitmentTX {
594+
LatestHolderCommitmentTXs {
595+
htlc_data: Vec<(HTLCOutputData, Option<HTLCSource>)>,
596+
commitment_txs: Vec<HolderCommitmentTransaction>,
597+
claimed_htlcs: Vec<(SentHTLCId, PaymentPreimage)>,
598+
},
599+
LatestCounterpartyCommitmentTXs {
595600
// The dust and non-dust htlcs for that commitment
596-
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
601+
htlc_data: Vec<(HTLCOutputData, Option<Box<HTLCSource>>)>,
597602
// Contains only the non-dust htlcs
598-
commitment_tx: CommitmentTransaction,
603+
commitment_txs: Vec<CommitmentTransaction>,
599604
},
600605
PaymentPreimage {
601606
payment_preimage: PaymentPreimage,
@@ -624,7 +629,8 @@ impl ChannelMonitorUpdateStep {
624629
match self {
625630
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo",
626631
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo",
627-
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX",
632+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXs { .. } => "LatestHolderCommitmentTXs",
633+
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXs { .. } => "LatestCounterpartyCommitmentTXs",
628634
ChannelMonitorUpdateStep::PaymentPreimage { .. } => "PaymentPreimage",
629635
ChannelMonitorUpdateStep::CommitmentSecret { .. } => "CommitmentSecret",
630636
ChannelMonitorUpdateStep::ChannelForceClosed { .. } => "ChannelForceClosed",
@@ -663,9 +669,14 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
663669
(5, ShutdownScript) => {
664670
(0, scriptpubkey, required),
665671
},
666-
(6, LatestCounterpartyCommitmentTX) => {
667-
(0, htlc_outputs, required_vec),
668-
(2, commitment_tx, required),
672+
(6, LatestHolderCommitmentTXs) => {
673+
(0, htlc_data, required_vec),
674+
(2, commitment_txs, required_vec),
675+
(4, claimed_htlcs, required_vec),
676+
},
677+
(7, LatestCounterpartyCommitmentTXs) => {
678+
(0, htlc_data, required_vec),
679+
(2, commitment_txs, required_vec),
669680
},
670681
);
671682

@@ -3084,6 +3095,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
30843095
}
30853096
}
30863097

3098+
// This is the sibling of `provide_latest_counterparty_commitment_tx`, but updated for a world
3099+
// in which the HTLC-source table passed from channel does NOT store dust-vs-nondust and
3100+
// index HTLC data.
3101+
fn provide_latest_counterparty_commitment_data<L: Deref>(
3102+
&mut self,
3103+
_htlc_data: Vec<(HTLCOutputData, Option<Box<HTLCSource>>)>,
3104+
_txs: &Vec<CommitmentTransaction>,
3105+
_logger: &WithChannelMonitor<L>,
3106+
) where L::Target: Logger {
3107+
// TODO(splicing, 0.2): Populate this monitor's data structures
3108+
todo!();
3109+
}
3110+
30873111
/// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
30883112
/// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
30893113
/// is important that any clones of this channel monitor (including remote clones) by kept
@@ -3175,6 +3199,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31753199
}
31763200
}
31773201

3202+
// This is the sibling of `provide_latest_holder_commitment_tx`, but updated for a world
3203+
// in which the HTLC-source table passed from channel does NOT store dust-vs-nondust and
3204+
// index HTLC data.
3205+
fn provide_latest_holder_commitment_data(
3206+
&mut self,
3207+
_htlc_data: Vec<(HTLCOutputData, Option<HTLCSource>)>,
3208+
_holder_commitment_txs: Vec<HolderCommitmentTransaction>,
3209+
_claimed_htlcs: &[(SentHTLCId, PaymentPreimage)],
3210+
) {
3211+
// TODO(splicing, 0.2): Populate this monitor's data structures
3212+
todo!();
3213+
}
3214+
31783215
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
31793216
/// commitment_tx_infos which contain the payment hash have been revoked.
31803217
///
@@ -3399,9 +3436,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33993436
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
34003437
self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_per_commitment_point, logger)
34013438
},
3402-
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3403-
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
3404-
self.provide_latest_counterparty_commitment_tx(commitment_tx.trust().txid(), htlc_outputs.clone(), commitment_tx.commitment_number(), commitment_tx.per_commitment_point(), logger)
3439+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXs { htlc_data, commitment_txs, claimed_htlcs } => {
3440+
log_trace!(logger, "Updating ChannelMonitor with latest holder commitment transaction(s)");
3441+
if self.lockdown_from_offchain { panic!(); }
3442+
self.provide_latest_holder_commitment_data(htlc_data.clone(), commitment_txs.clone(), claimed_htlcs)
3443+
}
3444+
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXs { htlc_data, commitment_txs } => {
3445+
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction(s)");
3446+
self.provide_latest_counterparty_commitment_data(htlc_data.clone(), commitment_txs, logger)
34053447
},
34063448
ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage, payment_info } => {
34073449
log_trace!(logger, "Updating ChannelMonitor with payment preimage");
@@ -3465,7 +3507,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34653507
match update {
34663508
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { .. }
34673509
|ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { .. }
3468-
|ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTX { .. }
3510+
|ChannelMonitorUpdateStep::LatestHolderCommitmentTXs { .. }
3511+
|ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXs { .. }
34693512
|ChannelMonitorUpdateStep::ShutdownScript { .. }
34703513
|ChannelMonitorUpdateStep::CommitmentSecret { .. } =>
34713514
is_pre_close_update = true,
@@ -3638,16 +3681,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36383681

36393682
debug_assert_eq!(commitment_tx.trust().txid(), commitment_txid);
36403683

3641-
Some(commitment_tx)
3684+
Some(vec![commitment_tx])
36423685
},
3643-
&ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTX {
3644-
htlc_outputs: _, ref commitment_tx,
3686+
&ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXs {
3687+
htlc_data: _, ref commitment_txs,
36453688
} => {
3646-
Some(commitment_tx.clone())
3689+
Some(commitment_txs.clone())
36473690
},
36483691
_ => None,
36493692
}
3650-
}).collect()
3693+
}).flatten().collect()
36513694
}
36523695

36533696
fn sign_to_local_justice_tx(

lightning/src/ln/chan_utils.rs

+26
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,32 @@ pub fn get_counterparty_payment_script(channel_type_features: &ChannelTypeFeatur
586586
}
587587
}
588588

589+
/// Information about an HTLC that is generic over multiple commitment transactions
590+
/// for a commitment number
591+
#[derive(Clone, Debug, PartialEq, Eq)]
592+
pub struct HTLCOutputData {
593+
/// Whether the HTLC was "offered" (ie outbound in relation to this commitment transaction).
594+
/// Note that this is not the same as whether it is ountbound *from us*. To determine that you
595+
/// need to compare this value to whether the commitment transaction in question is that of
596+
/// the counterparty or our own.
597+
pub offered: bool,
598+
/// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
599+
/// this divided by 1000.
600+
pub amount_msat: u64,
601+
/// The CLTV lock-time at which this HTLC expires.
602+
pub cltv_expiry: u32,
603+
/// The hash of the preimage which unlocks this HTLC.
604+
pub payment_hash: PaymentHash,
605+
// Do we want anything else ? the HTLC's uni-directional ID ?
606+
}
607+
608+
impl_writeable_tlv_based!(HTLCOutputData, {
609+
(0, offered, required),
610+
(2, amount_msat, required),
611+
(4, cltv_expiry, required),
612+
(6, payment_hash, required),
613+
});
614+
589615
/// Information about an HTLC as it appears in a commitment transaction
590616
#[derive(Clone, Debug, PartialEq, Eq)]
591617
pub struct HTLCOutputInCommitment {

lightning/src/ln/channel.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -9013,31 +9013,29 @@ impl<SP: Deref> FundedChannel<SP> where
90139013
}
90149014
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
90159015

9016-
let mut updates = Vec::with_capacity(self.pending_funding.len() + 1);
9017-
for funding in core::iter::once(&self.funding).chain(self.pending_funding.iter()) {
9016+
// Even in the pending splices case, we will send a single update for each commitment number.
9017+
// In that case, the update will contain multiple commitment transactions.
9018+
let mut updates = vec![];
9019+
if self.pending_funding.is_empty() {
90189020
let (htlcs_ref, counterparty_commitment_tx) =
9019-
self.build_commitment_no_state_update(funding, logger);
9021+
self.build_commitment_no_state_update(&self.funding, logger);
90209022
let htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> =
90219023
htlcs_ref.into_iter().map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
90229024

9023-
if self.pending_funding.is_empty() {
9024-
// Soon, we will switch this to `LatestCounterpartyCommitmentTX`,
9025-
// and provide the full commit tx instead of the information needed to rebuild it.
9026-
updates.push(ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
9027-
commitment_txid: counterparty_commitment_tx.trust().txid(),
9028-
htlc_outputs,
9029-
commitment_number: self.context.cur_counterparty_commitment_transaction_number,
9030-
their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap(),
9031-
feerate_per_kw: Some(counterparty_commitment_tx.feerate_per_kw()),
9032-
to_broadcaster_value_sat: Some(counterparty_commitment_tx.to_broadcaster_value_sat()),
9033-
to_countersignatory_value_sat: Some(counterparty_commitment_tx.to_countersignatory_value_sat()),
9034-
});
9035-
} else {
9036-
updates.push(ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTX {
9037-
htlc_outputs,
9038-
commitment_tx: counterparty_commitment_tx,
9039-
});
9040-
}
9025+
// Soon, we will switch this to `LatestCounterpartyCommitmentTX`,
9026+
// and provide the full commit tx instead of the information needed to rebuild it.
9027+
updates.push(ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
9028+
commitment_txid: counterparty_commitment_tx.trust().txid(),
9029+
htlc_outputs,
9030+
commitment_number: self.context.cur_counterparty_commitment_transaction_number,
9031+
their_per_commitment_point: self.context.counterparty_cur_commitment_point.unwrap(),
9032+
feerate_per_kw: Some(counterparty_commitment_tx.feerate_per_kw()),
9033+
to_broadcaster_value_sat: Some(counterparty_commitment_tx.to_broadcaster_value_sat()),
9034+
to_countersignatory_value_sat: Some(counterparty_commitment_tx.to_countersignatory_value_sat()),
9035+
});
9036+
} else {
9037+
// TODO(splicing): The pending splices case will be addressed later
9038+
todo!();
90419039
}
90429040

90439041
if self.context.announcement_sigs_state == AnnouncementSigsState::MessageSent {

0 commit comments

Comments
 (0)