Skip to content

Commit fd57c35

Browse files
committed
Remove generic and mutable references 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 6f80f99 commit fd57c35

File tree

5 files changed

+132
-105
lines changed

5 files changed

+132
-105
lines changed

lightning/src/chain/channelmonitor.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -3483,15 +3483,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34833483
to_broadcaster_value,
34843484
to_countersignatory_value,
34853485
)| {
3486-
let htlc_outputs = vec![];
3486+
let nondust_htlcs = vec![];
34873487

34883488
let commitment_tx = self.build_counterparty_commitment_tx(
34893489
INITIAL_COMMITMENT_NUMBER,
34903490
&their_per_commitment_point,
34913491
to_broadcaster_value,
34923492
to_countersignatory_value,
34933493
feerate_per_kw,
3494-
htlc_outputs,
3494+
&nondust_htlcs,
34953495
);
34963496
// Take the opportunity to populate this recently introduced field
34973497
self.initial_counterparty_commitment_tx = Some(commitment_tx.clone());
@@ -3504,11 +3504,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35043504
fn build_counterparty_commitment_tx(
35053505
&self, commitment_number: u64, their_per_commitment_point: &PublicKey,
35063506
to_broadcaster_value: u64, to_countersignatory_value: u64, feerate_per_kw: u32,
3507-
mut nondust_htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>
3507+
nondust_htlcs: &Vec<HTLCOutputInCommitment>
35083508
) -> CommitmentTransaction {
35093509
let channel_parameters = &self.funding.channel_parameters.as_counterparty_broadcastable();
3510-
CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number, their_per_commitment_point,
3511-
to_broadcaster_value, to_countersignatory_value, feerate_per_kw, &mut nondust_htlcs, channel_parameters, &self.onchain_tx_handler.secp_ctx)
3510+
CommitmentTransaction::new(commitment_number, their_per_commitment_point,
3511+
to_broadcaster_value, to_countersignatory_value, feerate_per_kw, nondust_htlcs, channel_parameters, &self.onchain_tx_handler.secp_ctx)
35123512
}
35133513

35143514
fn counterparty_commitment_txs_from_update(&self, update: &ChannelMonitorUpdate) -> Vec<CommitmentTransaction> {
@@ -3524,12 +3524,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35243524
to_countersignatory_value_sat: Some(to_countersignatory_value) } => {
35253525

35263526
let nondust_htlcs = htlc_outputs.iter().filter_map(|(htlc, _)| {
3527-
htlc.transaction_output_index.map(|_| (htlc.clone(), None))
3527+
htlc.transaction_output_index.map(|_| htlc).cloned()
35283528
}).collect::<Vec<_>>();
35293529

35303530
let commitment_tx = self.build_counterparty_commitment_tx(commitment_number,
35313531
&their_per_commitment_point, to_broadcaster_value,
3532-
to_countersignatory_value, feerate_per_kw, nondust_htlcs);
3532+
to_countersignatory_value, feerate_per_kw, &nondust_htlcs);
35333533

35343534
debug_assert_eq!(commitment_tx.trust().txid(), commitment_txid);
35353535

@@ -5423,21 +5423,21 @@ mod tests {
54235423
{
54245424
let mut res = Vec::new();
54255425
for (idx, preimage) in $preimages_slice.iter().enumerate() {
5426-
res.push((HTLCOutputInCommitment {
5426+
res.push(HTLCOutputInCommitment {
54275427
offered: true,
54285428
amount_msat: 0,
54295429
cltv_expiry: 0,
54305430
payment_hash: preimage.1.clone(),
54315431
transaction_output_index: Some(idx as u32),
5432-
}, ()));
5432+
});
54335433
}
54345434
res
54355435
}
54365436
}
54375437
}
54385438
macro_rules! preimages_slice_to_htlc_outputs {
54395439
($preimages_slice: expr) => {
5440-
preimages_slice_to_htlcs!($preimages_slice).into_iter().map(|(htlc, _)| (htlc, None)).collect()
5440+
preimages_slice_to_htlcs!($preimages_slice).into_iter().map(|htlc| (htlc, None)).collect()
54415441
}
54425442
}
54435443
let dummy_sig = crate::crypto::utils::sign(&secp_ctx,
@@ -5493,15 +5493,17 @@ mod tests {
54935493
let best_block = BestBlock::from_network(Network::Testnet);
54945494
let monitor = ChannelMonitor::new(
54955495
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5496-
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5496+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &Vec::new()),
54975497
best_block, dummy_key, channel_id,
54985498
);
54995499

5500-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
5501-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5500+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..10]);
5501+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &nondust_htlcs);
5502+
// These htlcs now have their output indices assigned
5503+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
55025504

55035505
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5504-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect());
5506+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), None)).collect());
55055507
monitor.provide_latest_counterparty_commitment_tx(Txid::from_byte_array(Sha256::hash(b"1").to_byte_array()),
55065508
preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
55075509
monitor.provide_latest_counterparty_commitment_tx(Txid::from_byte_array(Sha256::hash(b"2").to_byte_array()),
@@ -5536,21 +5538,25 @@ mod tests {
55365538

55375539
// Now update holder commitment tx info, pruning only element 18 as we still care about the
55385540
// previous commitment tx's preimages too
5539-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..5]);
5540-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5541+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..5]);
5542+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &nondust_htlcs);
5543+
// These htlcs now have their output indices assigned
5544+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
55415545
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5542-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect());
5546+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), None)).collect());
55435547
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
55445548
monitor.provide_secret(281474976710653, secret.clone()).unwrap();
55455549
assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 12);
55465550
test_preimages_exist!(&preimages[0..10], monitor);
55475551
test_preimages_exist!(&preimages[18..20], monitor);
55485552

55495553
// But if we do it again, we'll prune 5-10
5550-
let mut htlcs = preimages_slice_to_htlcs!(preimages[0..3]);
5551-
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &mut htlcs);
5552-
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx,
5553-
htlcs.into_iter().map(|(htlc, _)| (htlc, Some(dummy_sig), None)).collect());
5554+
let nondust_htlcs = preimages_slice_to_htlcs!(preimages[0..3]);
5555+
let dummy_commitment_tx = HolderCommitmentTransaction::dummy(0, &nondust_htlcs);
5556+
// These htlcs now have their output indices assigned
5557+
let nondust_htlcs = dummy_commitment_tx.nondust_htlcs();
5558+
monitor.provide_latest_holder_commitment_tx(dummy_commitment_tx.clone(),
5559+
nondust_htlcs.into_iter().map(|htlc| (htlc.clone(), Some(dummy_sig), None)).collect());
55545560
secret[0..32].clone_from_slice(&<Vec<u8>>::from_hex("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
55555561
monitor.provide_secret(281474976710652, secret.clone()).unwrap();
55565562
assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 5);
@@ -5745,7 +5751,7 @@ mod tests {
57455751
let best_block = BestBlock::from_network(Network::Testnet);
57465752
let monitor = ChannelMonitor::new(
57475753
Secp256k1::new(), keys, Some(shutdown_script.into_inner()), 0, &ScriptBuf::new(),
5748-
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &mut Vec::new()),
5754+
&channel_parameters, true, 0, HolderCommitmentTransaction::dummy(0, &Vec::new()),
57495755
best_block, dummy_key, channel_id,
57505756
);
57515757

lightning/src/chain/onchaintx.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1359,29 +1359,28 @@ mod tests {
13591359

13601360
// Create an OnchainTxHandler for a commitment containing HTLCs with CLTV expiries of 0, 1,
13611361
// and 2 blocks.
1362-
let mut htlcs = Vec::new();
1362+
let mut nondust_htlcs = Vec::new();
13631363
for i in 0..3 {
13641364
let preimage = PaymentPreimage([i; 32]);
13651365
let hash = PaymentHash(Sha256::hash(&preimage.0[..]).to_byte_array());
1366-
htlcs.push((
1366+
nondust_htlcs.push(
13671367
HTLCOutputInCommitment {
13681368
offered: true,
13691369
amount_msat: 10000,
13701370
cltv_expiry: i as u32,
13711371
payment_hash: hash,
13721372
transaction_output_index: Some(i as u32),
1373-
},
1374-
(),
1375-
));
1373+
}
1374+
);
13761375
}
1377-
let holder_commit = HolderCommitmentTransaction::dummy(1000000, &mut htlcs);
1376+
let holder_commit = HolderCommitmentTransaction::dummy(1000000, &nondust_htlcs);
13781377
let mut tx_handler = OnchainTxHandler::new(
13791378
1000000,
13801379
[0; 32],
13811380
ScriptBuf::new(),
13821381
signer,
13831382
chan_params,
1384-
holder_commit,
1383+
holder_commit.clone(),
13851384
secp_ctx,
13861385
);
13871386

@@ -1400,7 +1399,7 @@ mod tests {
14001399
// Request claiming of each HTLC on the holder's commitment, with current block height 1.
14011400
let holder_commit_txid = tx_handler.get_unsigned_holder_commitment_tx().compute_txid();
14021401
let mut requests = Vec::new();
1403-
for (htlc, _) in htlcs {
1402+
for htlc in holder_commit.nondust_htlcs() {
14041403
requests.push(PackageTemplate::build_package(
14051404
holder_commit_txid,
14061405
htlc.transaction_output_index.unwrap(),

0 commit comments

Comments
 (0)