Skip to content

Commit 1d7a1d8

Browse files
committed
Remove exclusive reference and generic from CommitmentTransaction API
This commit reworks how channel is told about which HTLCs were assigned which index upon the build of a `CommitmentTransaction`. Previously, `CommitmentTransaction` was given an exclusive reference to channel's HTLC-source table, and `CommitmentTransaction` populated the transaction output indices in that table via this exclusive reference. As a result, the public API of `CommitmentTransaction` included a generic parameter, and an exclusive reference. We remove both of these in preparation for the upcoming `TxBuilder` trait. This cleans up the API, and makes it more bindings-friendly. Henceforth, channel populates the HTLC-source table via a brute-force search of each htlc in `CommitmentTransaction`. This is an O(n^2) operation, but n is small enough that we ignore the performance hit.
1 parent ef0fcab commit 1d7a1d8

File tree

6 files changed

+166
-119
lines changed

6 files changed

+166
-119
lines changed

lightning/src/chain/channelmonitor.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -3588,15 +3588,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35883588
to_broadcaster_value,
35893589
to_countersignatory_value,
35903590
)| {
3591-
let htlc_outputs = vec![];
3591+
let nondust_htlcs = vec![];
35923592

35933593
let commitment_tx = self.build_counterparty_commitment_tx(
35943594
INITIAL_COMMITMENT_NUMBER,
35953595
&their_per_commitment_point,
35963596
to_broadcaster_value,
35973597
to_countersignatory_value,
35983598
feerate_per_kw,
3599-
htlc_outputs,
3599+
nondust_htlcs,
36003600
);
36013601
// Take the opportunity to populate this recently introduced field
36023602
self.initial_counterparty_commitment_tx = Some(commitment_tx.clone());
@@ -3609,11 +3609,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36093609
fn build_counterparty_commitment_tx(
36103610
&self, commitment_number: u64, their_per_commitment_point: &PublicKey,
36113611
to_broadcaster_value: u64, to_countersignatory_value: u64, feerate_per_kw: u32,
3612-
mut nondust_htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>
3612+
nondust_htlcs: Vec<HTLCOutputInCommitment>
36133613
) -> CommitmentTransaction {
36143614
let channel_parameters = &self.funding.channel_parameters.as_counterparty_broadcastable();
3615-
CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number, their_per_commitment_point,
3616-
to_broadcaster_value, to_countersignatory_value, feerate_per_kw, &mut nondust_htlcs, channel_parameters, &self.onchain_tx_handler.secp_ctx)
3615+
CommitmentTransaction::new(commitment_number, their_per_commitment_point,
3616+
to_broadcaster_value, to_countersignatory_value, feerate_per_kw, nondust_htlcs, channel_parameters, &self.onchain_tx_handler.secp_ctx)
36173617
}
36183618

36193619
fn counterparty_commitment_txs_from_update(&self, update: &ChannelMonitorUpdate) -> Vec<CommitmentTransaction> {
@@ -3629,7 +3629,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36293629
to_countersignatory_value_sat: Some(to_countersignatory_value) } => {
36303630

36313631
let nondust_htlcs = htlc_outputs.iter().filter_map(|(htlc, _)| {
3632-
htlc.transaction_output_index.map(|_| (htlc.clone(), None))
3632+
htlc.transaction_output_index.map(|_| htlc).cloned()
36333633
}).collect::<Vec<_>>();
36343634

36353635
let commitment_tx = self.build_counterparty_commitment_tx(commitment_number,
@@ -5620,21 +5620,21 @@ mod tests {
56205620
{
56215621
let mut res = Vec::new();
56225622
for (idx, preimage) in $preimages_slice.iter().enumerate() {
5623-
res.push((HTLCOutputInCommitment {
5623+
res.push(HTLCOutputInCommitment {
56245624
offered: true,
56255625
amount_msat: 0,
56265626
cltv_expiry: 0,
56275627
payment_hash: preimage.1.clone(),
56285628
transaction_output_index: Some(idx as u32),
5629-
}, ()));
5629+
});
56305630
}
56315631
res
56325632
}
56335633
}
56345634
}
56355635
macro_rules! preimages_slice_to_htlc_outputs {
56365636
($preimages_slice: expr) => {
5637-
preimages_slice_to_htlcs!($preimages_slice).into_iter().map(|(htlc, _)| (htlc, None)).collect()
5637+
preimages_slice_to_htlcs!($preimages_slice).into_iter().map(|htlc| (htlc, None)).collect()
56385638
}
56395639
}
56405640
let dummy_sig = crate::crypto::utils::sign(&secp_ctx,
@@ -5690,15 +5690,17 @@ mod tests {
56905690
let best_block = BestBlock::from_network(Network::Testnet);
56915691
let monitor = ChannelMonitor::new(
56925692
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5693-
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5693+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, Vec::new()),
56945694
best_block, dummy_key, channel_id,
56955695
);
56965696

5697-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
5698-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5697+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
5698+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, nondust_htlcs);
5699+
// These htlcs now have their output indices assigned
5700+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
56995701

57005702
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5701-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), Some(dummy_source.clone()))).collect());
5703+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), Some(dummy_source.clone()))).collect());
57025704
monitor.provide_latest_counterparty_commitment_tx(Txid::from_byte_array(Sha256::hash(b"1").to_byte_array()),
57035705
preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
57045706
monitor.provide_latest_counterparty_commitment_tx(Txid::from_byte_array(Sha256::hash(b"2").to_byte_array()),
@@ -5733,21 +5735,25 @@ mod tests {
57335735

57345736
// Now update holder commitment tx info, pruning only element 18 as we still care about the
57355737
// previous commitment tx's preimages too
5736-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..5]);
5737-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5738+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..5]);
5739+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, nondust_htlcs);
5740+
// These htlcs now have their output indices assigned
5741+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
57385742
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5739-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), Some(dummy_source.clone()))).collect());
5743+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), Some(dummy_source.clone()))).collect());
57405744
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
57415745
monitor.provide_secret(281474976710653, secret.clone()).unwrap();
57425746
assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 12);
57435747
test_preimages_exist!(&preimages[0..10], monitor);
57445748
test_preimages_exist!(&preimages[18..20], monitor);
57455749

57465750
// But if we do it again, we'll prune 5-10
5747-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..3]);
5748-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5749-
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx,
5750-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), Some(dummy_source.clone()))).collect());
5751+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..3]);
5752+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, nondust_htlcs);
5753+
// These htlcs now have their output indices assigned
5754+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
5755+
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5756+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), Some(dummy_source.clone()))).collect());
57515757
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
57525758
monitor.provide_secret(281474976710652, secret.clone()).unwrap();
57535759
assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 5);
@@ -5942,7 +5948,7 @@ mod tests {
59425948
let best_block = BestBlock::from_network(Network::Testnet);
59435949
let monitor = ChannelMonitor::new(
59445950
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5945-
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5951+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, Vec::new()),
59465952
best_block, dummy_key, channel_id,
59475953
);
59485954

lightning/src/chain/onchaintx.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1296,22 +1296,21 @@ mod tests {
12961296

12971297
// Create an OnchainTxHandler for a commitment containing HTLCs with CLTV expiries of 0, 1,
12981298
// and 2 blocks.
1299-
let mut htlcs = Vec::new();
1299+
let mut nondust_htlcs = Vec::new();
13001300
for i in 0..3 {
13011301
let preimage = PaymentPreimage([i; 32]);
13021302
let hash = PaymentHash(Sha256::hash(&preimage.0[..]).to_byte_array());
1303-
htlcs.push((
1303+
nondust_htlcs.push(
13041304
HTLCOutputInCommitment {
13051305
offered: true,
13061306
amount_msat: 10000,
13071307
cltv_expiry: i as u32,
13081308
payment_hash: hash,
13091309
transaction_output_index: Some(i as u32),
1310-
},
1311-
(),
1312-
));
1310+
}
1311+
);
13131312
}
1314-
let holder_commit = HolderCommitmentTransaction::dummy(1000000, &mut htlcs);
1313+
let holder_commit = HolderCommitmentTransaction::dummy(1000000, nondust_htlcs);
13151314
let destination_script = ScriptBuf::new();
13161315
let mut tx_handler = OnchainTxHandler::new(
13171316
1000000,

lightning/src/chain/package.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ mod tests {
17121712
payment_hash: PaymentHash::from(preimage),
17131713
transaction_output_index: None,
17141714
};
1715-
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut vec![(htlc.clone(), ())]);
1715+
let commitment_tx = HolderCommitmentTransaction::dummy(0, vec![htlc.clone()]);
17161716
let trusted_tx = commitment_tx.trust();
17171717
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build(
17181718
HTLCDescriptor {
@@ -1746,7 +1746,7 @@ mod tests {
17461746
payment_hash: PaymentHash::from(PaymentPreimage([2;32])),
17471747
transaction_output_index: None,
17481748
};
1749-
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut vec![(htlc.clone(), ())]);
1749+
let commitment_tx = HolderCommitmentTransaction::dummy(0, vec![htlc.clone()]);
17501750
let trusted_tx = commitment_tx.trust();
17511751
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build(
17521752
HTLCDescriptor {
@@ -1770,7 +1770,7 @@ mod tests {
17701770

17711771
macro_rules! dumb_funding_output {
17721772
() => {{
1773-
let commitment_tx = HolderCommitmentTransaction::dummy(0, &mut Vec::new());
1773+
let commitment_tx = HolderCommitmentTransaction::dummy(0, Vec::new());
17741774
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
17751775
channel_parameters.channel_type_features = ChannelTypeFeatures::only_static_remote_key();
17761776
PackageSolvingData::HolderFundingOutput(HolderFundingOutput::build(

0 commit comments

Comments
 (0)