Skip to content

Commit e8a87e2

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 bab3e70 commit e8a87e2

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.
@@ -689,7 +690,9 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
689690
}
690691

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

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: 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: 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 = channel_parameters.channel_value_satoshis;
459+
let channel_type_features = channel_parameters.channel_type_features.clone();
456460
HolderFundingOutput {
457461
funding_redeemscript,
458462
funding_amount: Some(funding_amount),
459463
channel_type_features,
460464
commitment_tx: Some(commitment_tx),
465+
channel_parameters: Some(channel_parameters),
461466
}
462467
}
463468

464469
pub(crate) fn try_sign<Signer: EcdsaChannelSigner>(
465470
&self, onchain_tx_handler: &mut OnchainTxHandler<Signer>,
466471
) -> Option<Signature> {
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
onchain_tx_handler.signer
@@ -482,6 +488,7 @@ impl Writeable for HolderFundingOutput {
482488
(2, legacy_deserialization_prevention_marker, option),
483489
(3, self.funding_amount, option),
484490
(5, self.commitment_tx, option), // Added in 0.2
491+
(7, self.channel_parameters, option), // Added in 0.2.
485492
});
486493
Ok(())
487494
}
@@ -494,22 +501,27 @@ impl Readable for HolderFundingOutput {
494501
let mut channel_type_features = None;
495502
let mut funding_amount = None;
496503
let mut commitment_tx = None;
504+
let mut channel_parameters = None;
497505

498506
read_tlv_fields!(reader, {
499507
(0, funding_redeemscript, required),
500508
(1, channel_type_features, option),
501509
(2, _legacy_deserialization_prevention_marker, option),
502510
(3, funding_amount, option),
503511
(5, commitment_tx, option), // Added in 0.2.
512+
(7, channel_parameters, (option: ReadableArgs, None)), // Added in 0.2.
504513
});
505514

506515
verify_channel_type_features(&channel_type_features, None)?;
516+
let channel_type_features =
517+
channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key());
507518

508519
Ok(Self {
509520
funding_redeemscript: funding_redeemscript.0.unwrap(),
510-
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key()),
511521
funding_amount,
522+
channel_type_features,
512523
commitment_tx,
524+
channel_parameters,
513525
})
514526
}
515527
}
@@ -1440,7 +1452,9 @@ where
14401452
mod tests {
14411453
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedHTLCOutput, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc, feerate_bump};
14421454
use crate::chain::Txid;
1443-
use crate::ln::chan_utils::{HolderCommitmentTransaction, HTLCOutputInCommitment};
1455+
use crate::ln::chan_utils::{
1456+
ChannelTransactionParameters, HolderCommitmentTransaction, HTLCOutputInCommitment,
1457+
};
14441458
use crate::types::payment::{PaymentPreimage, PaymentHash};
14451459
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
14461460

@@ -1544,8 +1558,10 @@ mod tests {
15441558
macro_rules! dumb_funding_output {
15451559
() => {{
15461560
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut Vec::new());
1561+
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
1562+
channel_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
15471563
PackageSolvingData::HolderFundingOutput(HolderFundingOutput::build(
1548-
commitment_tx, ScriptBuf::new(), 0, ChannelTypeFeatures::only_static_remote_key()
1564+
commitment_tx, channel_parameters,
15491565
))
15501566
}}
15511567
}

0 commit comments

Comments
 (0)