Skip to content

Commit 1551141

Browse files
committed
Track ChannelTransactionParameters in HolderFundingOutput
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. Once splices are supported, we may run into cases where we are attempting to claim an output from a specific `FundingScope`, while also having an additional pending `FundingScope` for a splice. If the pending splice confirms over the output claim, we need to cancel the claim and re-offer it with the set of relevant parameters in the new `FundingScope`. This commit tracks the `ChannelTransactionParameters` for the specific `FundingScope` the `HolderFundingOutput` claim originated from. This allows us to remove the dependency on `OnchainTxHandler` when obtaining the current `ChannelTransactionParameters` and ensures any alternative state due to splicing does not leak into the `OnchainTxHandler`.
1 parent b6a8e5e commit 1551141

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,9 +3279,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32793279
fn generate_claimable_outpoints_and_watch_outputs(&mut self, reason: ClosureReason) -> (Vec<PackageTemplate>, Vec<TransactionOutputs>) {
32803280
let funding_outp = HolderFundingOutput::build(
32813281
self.funding.current_holder_commitment.tx.clone(),
3282-
self.funding.redeem_script.clone(),
3283-
self.funding.channel_parameters.channel_value_satoshis,
3284-
self.channel_type_features().clone(),
3282+
self.funding.channel_parameters.clone(),
32853283
);
32863284
let funding_outpoint = self.get_funding_txo();
32873285
let commitment_package = PackageTemplate::build_package(
@@ -3533,6 +3531,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35333531
ClaimEvent::BumpCommitment {
35343532
package_target_feerate_sat_per_1000_weight, commitment_tx,
35353533
commitment_tx_fee_satoshis, pending_htlcs, anchor_output_idx,
3534+
channel_parameters,
35363535
} => {
35373536
let channel_id = self.channel_id;
35383537
let counterparty_node_id = self.counterparty_node_id;
@@ -3547,8 +3546,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35473546
anchor_descriptor: AnchorDescriptor {
35483547
channel_derivation_parameters: ChannelDerivationParameters {
35493548
keys_id: self.channel_keys_id,
3550-
value_satoshis: self.funding.channel_parameters.channel_value_satoshis,
3551-
transaction_parameters: self.funding.channel_parameters.clone(),
3549+
value_satoshis: channel_parameters.channel_value_satoshis,
3550+
transaction_parameters: channel_parameters,
35523551
},
35533552
outpoint: BitcoinOutPoint {
35543553
txid: commitment_txid,

lightning/src/chain/onchaintx.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub(crate) enum ClaimEvent {
196196
commitment_tx_fee_satoshis: u64,
197197
pending_htlcs: Vec<HTLCOutputInCommitment>,
198198
anchor_output_idx: u32,
199+
channel_parameters: ChannelTransactionParameters,
199200
},
200201
/// Event yielded to signal that the commitment transaction has confirmed and its HTLCs must be
201202
/// resolved by broadcasting a transaction with sufficient fee to claim them.
@@ -690,7 +691,9 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
690691
}
691692

692693
// We'll locate an anchor output we can spend within the commitment transaction.
693-
let funding_pubkey = &self.channel_transaction_parameters.holder_pubkeys.funding_pubkey;
694+
let channel_parameters = output.channel_parameters.as_ref()
695+
.unwrap_or(&self.channel_transaction_parameters);
696+
let funding_pubkey = &channel_parameters.holder_pubkeys.funding_pubkey;
694697
match chan_utils::get_keyed_anchor_output(&tx, funding_pubkey) {
695698
// An anchor output was found, so we should yield a funding event externally.
696699
Some((idx, _)) => {
@@ -705,6 +708,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
705708
pending_htlcs: holder_commitment.htlcs().to_vec(),
706709
commitment_tx_fee_satoshis: fee_sat,
707710
anchor_output_idx: idx,
711+
channel_parameters: channel_parameters.clone(),
708712
}),
709713
))
710714
},

lightning/src/chain/package.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use bitcoin::transaction::Version;
2626

2727
use crate::types::payment::PaymentPreimage;
2828
use crate::ln::chan_utils::{
29-
self, HolderCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment,
29+
self, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys,
30+
HTLCOutputInCommitment,
3031
};
3132
use crate::types::features::ChannelTypeFeatures;
3233
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
@@ -38,7 +39,7 @@ use crate::chain::transaction::MaybeSignedTransaction;
3839
use crate::sign::ecdsa::EcdsaChannelSigner;
3940
use crate::chain::onchaintx::{FeerateStrategy, ExternalHTLCClaim, OnchainTxHandler};
4041
use crate::util::logger::Logger;
41-
use crate::util::ser::{Readable, Writer, Writeable, RequiredWrapper};
42+
use crate::util::ser::{Readable, ReadableArgs, Writer, Writeable, RequiredWrapper};
4243

4344
use crate::io;
4445
use core::cmp;
@@ -445,26 +446,31 @@ pub(crate) struct HolderFundingOutput {
445446
pub(crate) funding_amount_sats: Option<u64>,
446447
channel_type_features: ChannelTypeFeatures,
447448
pub(crate) commitment_tx: Option<HolderCommitmentTransaction>,
449+
pub(crate) channel_parameters: Option<ChannelTransactionParameters>,
448450
}
449451

450452

451453
impl HolderFundingOutput {
452454
pub(crate) fn build(
453-
commitment_tx: HolderCommitmentTransaction, funding_redeemscript: ScriptBuf,
454-
funding_amount_sats: u64, channel_type_features: ChannelTypeFeatures,
455+
commitment_tx: HolderCommitmentTransaction, channel_parameters: ChannelTransactionParameters,
455456
) -> Self {
457+
let funding_redeemscript = channel_parameters.make_funding_redeemscript();
458+
let funding_amount_sats = channel_parameters.channel_value_satoshis;
459+
let channel_type_features = channel_parameters.channel_type_features.clone();
456460
HolderFundingOutput {
457461
funding_redeemscript,
458462
funding_amount_sats: Some(funding_amount_sats),
459463
channel_type_features,
460464
commitment_tx: Some(commitment_tx),
465+
channel_parameters: Some(channel_parameters),
461466
}
462467
}
463468

464469
pub(crate) fn get_maybe_signed_commitment_tx<Signer: EcdsaChannelSigner>(
465470
&self, onchain_tx_handler: &mut OnchainTxHandler<Signer>,
466471
) -> MaybeSignedTransaction {
467-
let channel_parameters = &onchain_tx_handler.channel_transaction_parameters;
472+
let channel_parameters = self.channel_parameters.as_ref().
473+
unwrap_or(&onchain_tx_handler.channel_transaction_parameters);
468474
let commitment_tx = self.commitment_tx.as_ref()
469475
.unwrap_or(onchain_tx_handler.current_holder_commitment_tx());
470476
let maybe_signed_tx = onchain_tx_handler.signer
@@ -488,6 +494,7 @@ impl Writeable for HolderFundingOutput {
488494
(2, legacy_deserialization_prevention_marker, option),
489495
(3, self.funding_amount_sats, option),
490496
(5, self.commitment_tx, option), // Added in 0.2
497+
(7, self.channel_parameters, option), // Added in 0.2.
491498
});
492499
Ok(())
493500
}
@@ -500,22 +507,27 @@ impl Readable for HolderFundingOutput {
500507
let mut channel_type_features = None;
501508
let mut funding_amount_sats = None;
502509
let mut commitment_tx = None;
510+
let mut channel_parameters = None;
503511

504512
read_tlv_fields!(reader, {
505513
(0, funding_redeemscript, required),
506514
(1, channel_type_features, option),
507515
(2, _legacy_deserialization_prevention_marker, option),
508516
(3, funding_amount_sats, option),
509517
(5, commitment_tx, option), // Added in 0.2.
518+
(7, channel_parameters, (option: ReadableArgs, None)), // Added in 0.2.
510519
});
511520

512521
verify_channel_type_features(&channel_type_features, None)?;
522+
let channel_type_features =
523+
channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key());
513524

514525
Ok(Self {
515526
funding_redeemscript: funding_redeemscript.0.unwrap(),
516-
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
517527
funding_amount_sats,
528+
channel_type_features,
518529
commitment_tx,
530+
channel_parameters,
519531
})
520532
}
521533
}
@@ -1434,7 +1446,9 @@ where
14341446
mod tests {
14351447
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedHTLCOutput, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc, feerate_bump};
14361448
use crate::chain::Txid;
1437-
use crate::ln::chan_utils::{HolderCommitmentTransaction, HTLCOutputInCommitment};
1449+
use crate::ln::chan_utils::{
1450+
ChannelTransactionParameters, HolderCommitmentTransaction, HTLCOutputInCommitment,
1451+
};
14381452
use crate::types::payment::{PaymentPreimage, PaymentHash};
14391453
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
14401454

@@ -1538,8 +1552,10 @@ mod tests {
15381552
macro_rules! dumb_funding_output {
15391553
() => {{
15401554
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut Vec::new());
1555+
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
1556+
channel_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
15411557
PackageSolvingData::HolderFundingOutput(HolderFundingOutput::build(
1542-
commitment_tx, ScriptBuf::new(), 0, ChannelTypeFeatures::only_static_remote_key()
1558+
commitment_tx, channel_parameters,
15431559
))
15441560
}}
15451561
}

0 commit comments

Comments
 (0)