Skip to content

Commit 2e9dffc

Browse files
committed
Track channel_keys_id in HolderHTLCOutput
The `ChannelMonitor` and `OnchainTxHandler` have historically been tied together, often tracking some of the same state twice. As we introduce support for splices in the `ChannelMonitor`, we'd like to avoid leaking some of those details to the `OnchainTxHandler`. Ultimately, the `OnchainTxHandler` should stand on its own and support claiming funds from multiple `ChannelMonitor`s, allowing us to save on fees by batching aggregatable claims across multiple in-flight closing channels. This commit tracks the `channel_keys_id` for the channel the `HolderHTLCOutput` claim originated from.
1 parent bea092d commit 2e9dffc

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,7 +4004,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40044004
if let Some(transaction_output_index) = htlc.transaction_output_index {
40054005
let (htlc_output, counterparty_spendable_height) = if htlc.offered {
40064006
let htlc_output = HolderHTLCOutput::build_offered(
4007-
self.funding.channel_parameters.clone(),
4007+
self.funding.channel_parameters.clone(), self.channel_keys_id,
40084008
htlc.amount_msat, htlc.cltv_expiry,
40094009
self.funding.current_holder_commitment.tx.clone(),
40104010
self.funding.prev_holder_commitment.as_ref().map(|c| &c.tx).cloned(),
@@ -4018,7 +4018,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40184018
continue;
40194019
};
40204020
let htlc_output = HolderHTLCOutput::build_accepted(
4021-
self.funding.channel_parameters.clone(),
4021+
self.funding.channel_parameters.clone(), self.channel_keys_id,
40224022
payment_preimage, htlc.amount_msat,
40234023
self.funding.current_holder_commitment.tx.clone(),
40244024
self.funding.prev_holder_commitment.as_ref().map(|c| &c.tx).cloned(),
@@ -4186,15 +4186,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41864186
if let Some(vout) = htlc.transaction_output_index {
41874187
let (htlc_output, preimage) = if htlc.offered {
41884188
let htlc_output = HolderHTLCOutput::build_offered(
4189-
self.funding.channel_parameters.clone(), htlc.amount_msat, htlc.cltv_expiry,
4189+
self.funding.channel_parameters.clone(), self.channel_keys_id,
4190+
htlc.amount_msat, htlc.cltv_expiry,
41904191
self.funding.current_holder_commitment.tx.clone(),
41914192
self.funding.prev_holder_commitment.as_ref().map(|c| &c.tx).cloned(),
41924193
);
41934194
(htlc_output, None)
41944195
} else {
41954196
if let Some((preimage, _)) = self.payment_preimages.get(&htlc.payment_hash) {
41964197
let htlc_output = HolderHTLCOutput::build_accepted(
4197-
self.funding.channel_parameters.clone(), *preimage, htlc.amount_msat,
4198+
self.funding.channel_parameters.clone(), self.channel_keys_id,
4199+
*preimage, htlc.amount_msat,
41984200
self.funding.current_holder_commitment.tx.clone(),
41994201
self.funding.prev_holder_commitment.as_ref().map(|c| &c.tx).cloned(),
42004202
);

lightning/src/chain/onchaintx.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ pub(crate) enum FeerateStrategy {
234234
#[derive(Clone)]
235235
pub struct OnchainTxHandler<ChannelSigner: EcdsaChannelSigner> {
236236
channel_value_satoshis: u64,
237-
pub(crate) channel_keys_id: [u8; 32],
237+
channel_keys_id: [u8; 32], // Deprecated as of 0.2.
238238
destination_script: ScriptBuf, // Deprecated as of 0.2.
239239
holder_commitment: HolderCommitmentTransaction,
240240
prev_holder_commitment: Option<HolderCommitmentTransaction>,
@@ -1207,6 +1207,11 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
12071207
pub(crate) fn channel_type_features(&self) -> &ChannelTypeFeatures {
12081208
&self.channel_transaction_parameters.channel_type_features
12091209
}
1210+
1211+
// Deprecated as of 0.2, only use in cases where it was not previously available.
1212+
pub(crate) fn channel_keys_id(&self) -> [u8; 32] {
1213+
self.channel_keys_id
1214+
}
12101215
}
12111216

12121217
#[cfg(test)]
@@ -1339,7 +1344,7 @@ mod tests {
13391344
holder_commit_txid,
13401345
htlc.transaction_output_index.unwrap(),
13411346
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_offered(
1342-
tx_handler.channel_transaction_parameters.clone(),
1347+
tx_handler.channel_transaction_parameters.clone(), tx_handler.channel_keys_id,
13431348
htlc.amount_msat, htlc.cltv_expiry,
13441349
tx_handler.holder_commitment.clone(), tx_handler.prev_holder_commitment.clone(),
13451350
)),

lightning/src/chain/package.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,13 @@ pub(crate) struct HolderHTLCOutput {
376376
current_holder_commitment: Option<HolderCommitmentTransaction>,
377377
prev_holder_commitment: Option<HolderCommitmentTransaction>,
378378
channel_parameters: Option<ChannelTransactionParameters>,
379+
channel_keys_id: Option<[u8; 32]>,
379380
}
380381

381382
impl HolderHTLCOutput {
382383
pub(crate) fn build_offered(
383-
channel_parameters: ChannelTransactionParameters, amount_msat: u64, cltv_expiry: u32,
384-
current_holder_commitment: HolderCommitmentTransaction,
384+
channel_parameters: ChannelTransactionParameters, channel_keys_id: [u8; 32],
385+
amount_msat: u64, cltv_expiry: u32, current_holder_commitment: HolderCommitmentTransaction,
385386
prev_holder_commitment: Option<HolderCommitmentTransaction>,
386387
) -> Self {
387388
let channel_type_features = channel_parameters.channel_type_features.clone();
@@ -393,11 +394,13 @@ impl HolderHTLCOutput {
393394
current_holder_commitment: Some(current_holder_commitment),
394395
prev_holder_commitment,
395396
channel_parameters: Some(channel_parameters),
397+
channel_keys_id: Some(channel_keys_id),
396398
}
397399
}
398400

399401
pub(crate) fn build_accepted(
400-
channel_parameters: ChannelTransactionParameters, preimage: PaymentPreimage, amount_msat: u64,
402+
channel_parameters: ChannelTransactionParameters, channel_keys_id: [u8; 32],
403+
preimage: PaymentPreimage, amount_msat: u64,
401404
current_holder_commitment: HolderCommitmentTransaction,
402405
prev_holder_commitment: Option<HolderCommitmentTransaction>,
403406
) -> Self {
@@ -410,6 +413,7 @@ impl HolderHTLCOutput {
410413
current_holder_commitment: Some(current_holder_commitment),
411414
prev_holder_commitment,
412415
channel_parameters: Some(channel_parameters),
416+
channel_keys_id: Some(channel_keys_id),
413417
}
414418
}
415419

@@ -419,6 +423,8 @@ impl HolderHTLCOutput {
419423
) -> Option<MaybeSignedTransaction> {
420424
let channel_parameters = self.channel_parameters.as_ref()
421425
.unwrap_or(&onchain_tx_handler.channel_transaction_parameters);
426+
let channel_keys_id = self.channel_keys_id
427+
.unwrap_or(onchain_tx_handler.channel_keys_id());
422428
let get_signed_htlc_tx = |holder_commitment: &HolderCommitmentTransaction| {
423429
let trusted_tx = holder_commitment.trust();
424430
if trusted_tx.txid() != outp.txid {
@@ -435,7 +441,7 @@ impl HolderHTLCOutput {
435441
let htlc_descriptor = HTLCDescriptor {
436442
channel_derivation_parameters: ChannelDerivationParameters {
437443
value_satoshis: channel_parameters.channel_value_satoshis,
438-
keys_id: onchain_tx_handler.channel_keys_id,
444+
keys_id: channel_keys_id,
439445
transaction_parameters: channel_parameters.clone(),
440446
},
441447
commitment_txid: trusted_tx.txid(),
@@ -519,6 +525,7 @@ impl Writeable for HolderHTLCOutput {
519525
(9, self.current_holder_commitment, option), // Added in 0.2.
520526
(11, self.prev_holder_commitment, option), // Added in 0.2.
521527
(13, self.channel_parameters, option), // Added in 0.2.
528+
(15, self.channel_keys_id, option), // Added in 0.2.
522529
});
523530
Ok(())
524531
}
@@ -534,6 +541,7 @@ impl Readable for HolderHTLCOutput {
534541
let mut current_holder_commitment = None;
535542
let mut prev_holder_commitment = None;
536543
let mut channel_parameters = None;
544+
let mut channel_keys_id = None;
537545

538546
read_tlv_fields!(reader, {
539547
(0, amount_msat, required),
@@ -544,6 +552,7 @@ impl Readable for HolderHTLCOutput {
544552
(9, current_holder_commitment, option), // Added in 0.2.
545553
(11, prev_holder_commitment, option), // Added in 0.2.
546554
(13, channel_parameters, (option: ReadableArgs, None)), // Added in 0.2.
555+
(15, channel_keys_id, option), // Added in 0.2.
547556
});
548557

549558
verify_channel_type_features(&channel_type_features, None)?;
@@ -558,6 +567,7 @@ impl Readable for HolderHTLCOutput {
558567
current_holder_commitment,
559568
prev_holder_commitment,
560569
channel_parameters,
570+
channel_keys_id,
561571
})
562572
}
563573
}
@@ -1660,7 +1670,7 @@ mod tests {
16601670
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut Vec::new());
16611671
let preimage = PaymentPreimage([2;32]);
16621672
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(
1663-
channel_parameters, preimage, 0, commitment_tx, None,
1673+
channel_parameters, [0; 32], preimage, 0, commitment_tx, None,
16641674
))
16651675
}
16661676
}
@@ -1673,7 +1683,7 @@ mod tests {
16731683
channel_parameters.channel_type_features = $features;
16741684
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut Vec::new());
16751685
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_offered(
1676-
channel_parameters, 0, $cltv_expiry, commitment_tx, None,
1686+
channel_parameters, [0; 32], 0, $cltv_expiry, commitment_tx, None,
16771687
))
16781688
}
16791689
}

0 commit comments

Comments
 (0)