Skip to content

Commit b9d4a0c

Browse files
committed
make WEIGHT{_REVOKED,}_{OFFERED,RECEIVED}_HTLC functions with opt_anchors parameter
1 parent 249ed0e commit b9d4a0c

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

lightning/src/chain/channelmonitor.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20412041
tx.output[transaction_output_index as usize].value != htlc.amount_msat / 1000 {
20422042
return (claimable_outpoints, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
20432043
}
2044-
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone());
2044+
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), self.onchain_tx_handler.channel_transaction_parameters.opt_anchors.is_some());
20452045
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, true, height);
20462046
claimable_outpoints.push(justice_package);
20472047
}
@@ -3307,7 +3307,7 @@ mod tests {
33073307
use ::{check_added_monitors, check_closed_broadcast, check_closed_event, check_spends, get_local_commitment_txn, get_monitor, get_route_and_payment_hash, unwrap_send_err};
33083308
use chain::{BestBlock, Confirm};
33093309
use chain::channelmonitor::ChannelMonitor;
3310-
use chain::package::{WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC, WEIGHT_REVOKED_OUTPUT};
3310+
use chain::package::{weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT};
33113311
use chain::transaction::OutPoint;
33123312
use chain::keysinterface::InMemorySigner;
33133313
use ln::{PaymentPreimage, PaymentHash};
@@ -3569,7 +3569,7 @@ mod tests {
35693569
macro_rules! sign_input {
35703570
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr, $opt_anchors: expr) => {
35713571
let htlc = HTLCOutputInCommitment {
3572-
offered: if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_OFFERED_HTLC { true } else { false },
3572+
offered: if *$weight == weight_revoked_offered_htlc($opt_anchors) || *$weight == weight_offered_htlc($opt_anchors) { true } else { false },
35733573
amount_msat: 0,
35743574
cltv_expiry: 2 << 16,
35753575
payment_hash: PaymentHash([1; 32]),
@@ -3584,9 +3584,9 @@ mod tests {
35843584
sum_actual_sigs += $sighash_parts.access_witness($idx)[0].len();
35853585
if *$weight == WEIGHT_REVOKED_OUTPUT {
35863586
$sighash_parts.access_witness($idx).push(vec!(1));
3587-
} else if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_REVOKED_RECEIVED_HTLC {
3587+
} else if *$weight == weight_revoked_offered_htlc($opt_anchors) || *$weight == weight_revoked_received_htlc($opt_anchors) {
35883588
$sighash_parts.access_witness($idx).push(pubkey.clone().serialize().to_vec());
3589-
} else if *$weight == WEIGHT_RECEIVED_HTLC {
3589+
} else if *$weight == weight_received_htlc($opt_anchors) {
35903590
$sighash_parts.access_witness($idx).push(vec![0]);
35913591
} else {
35923592
$sighash_parts.access_witness($idx).push(PaymentPreimage([1; 32]).0.to_vec());
@@ -3623,7 +3623,7 @@ mod tests {
36233623
value: 0,
36243624
});
36253625
let base_weight = claim_tx.get_weight();
3626-
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_OFFERED_HTLC, WEIGHT_REVOKED_RECEIVED_HTLC];
3626+
let inputs_weight = vec![WEIGHT_REVOKED_OUTPUT, weight_revoked_offered_htlc($opt_anchors), weight_revoked_offered_htlc($opt_anchors), weight_revoked_received_htlc($opt_anchors)];
36273627
let mut inputs_total_weight = 2; // count segwit flags
36283628
{
36293629
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
@@ -3661,7 +3661,7 @@ mod tests {
36613661
value: 0,
36623662
});
36633663
let base_weight = claim_tx.get_weight();
3664-
let inputs_weight = vec![WEIGHT_OFFERED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC, WEIGHT_RECEIVED_HTLC];
3664+
let inputs_weight = vec![weight_offered_htlc($opt_anchors), weight_received_htlc($opt_anchors), weight_received_htlc($opt_anchors), weight_received_htlc($opt_anchors)];
36653665
let mut inputs_total_weight = 2; // count segwit flags
36663666
{
36673667
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);

lightning/src/chain/onchaintx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
387387
// didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
388388
let new_timer = Some(cached_request.get_height_timer(cur_height));
389389
if cached_request.is_malleable() {
390-
let predicted_weight = cached_request.package_weight(&self.destination_script);
390+
let predicted_weight = cached_request.package_weight(&self.destination_script, self.channel_transaction_parameters.opt_anchors.is_some());
391391
if let Some((output_value, new_feerate)) =
392392
cached_request.compute_package_output(predicted_weight, self.destination_script.dust_value().as_sat(), fee_estimator, logger) {
393393
assert!(new_feerate != 0);

lightning/src/chain/package.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,34 @@ use core::ops::Deref;
4040
const MAX_ALLOC_SIZE: usize = 64*1024;
4141

4242

43-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
44-
pub(crate) const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
45-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
46-
pub(crate) const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139;
47-
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
48-
pub(crate) const WEIGHT_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133;
49-
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
50-
pub(crate) const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
43+
pub(crate) fn weight_revoked_offered_htlc(opt_anchors: bool) -> u64 {
44+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
45+
const WEIGHT_REVOKED_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133;
46+
const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3;
47+
if opt_anchors { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
48+
}
49+
50+
pub(crate) fn weight_revoked_received_htlc(opt_anchors: bool) -> u64 {
51+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
52+
const WEIGHT_REVOKED_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139;
53+
const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3;
54+
if opt_anchors { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
55+
}
56+
57+
pub(crate) fn weight_offered_htlc(opt_anchors: bool) -> u64 {
58+
// number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
59+
const WEIGHT_OFFERED_HTLC: u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133;
60+
const WEIGHT_OFFERED_HTLC_ANCHORS: u64 = WEIGHT_OFFERED_HTLC + 3;
61+
if opt_anchors { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
62+
}
63+
64+
pub(crate) fn weight_received_htlc(opt_anchors: bool) -> u64 {
65+
// number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
66+
const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
67+
const WEIGHT_RECEIVED_HTLC_ANCHORS: u64 = WEIGHT_RECEIVED_HTLC + 3;
68+
if opt_anchors { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
69+
}
70+
5171
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
5272
pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77;
5373

@@ -118,8 +138,8 @@ pub(crate) struct RevokedHTLCOutput {
118138
}
119139

120140
impl RevokedHTLCOutput {
121-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment) -> Self {
122-
let weight = if htlc.offered { WEIGHT_REVOKED_OFFERED_HTLC } else { WEIGHT_REVOKED_RECEIVED_HTLC };
141+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, opt_anchors: bool) -> Self {
142+
let weight = if htlc.offered { weight_revoked_offered_htlc(opt_anchors) } else { weight_revoked_received_htlc(opt_anchors) };
123143
RevokedHTLCOutput {
124144
per_commitment_point,
125145
counterparty_delayed_payment_base_key,
@@ -292,12 +312,12 @@ impl PackageSolvingData {
292312
};
293313
amt
294314
}
295-
fn weight(&self) -> usize {
315+
fn weight(&self, opt_anchors: bool) -> usize {
296316
let weight = match self {
297317
PackageSolvingData::RevokedOutput(ref outp) => { outp.weight as usize },
298318
PackageSolvingData::RevokedHTLCOutput(ref outp) => { outp.weight as usize },
299-
PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { WEIGHT_OFFERED_HTLC as usize },
300-
PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { WEIGHT_RECEIVED_HTLC as usize },
319+
PackageSolvingData::CounterpartyOfferedHTLCOutput(..) => { weight_offered_htlc(opt_anchors) as usize },
320+
PackageSolvingData::CounterpartyReceivedHTLCOutput(..) => { weight_received_htlc(opt_anchors) as usize },
301321
// Note: Currently, weights of holder outputs spending witnesses aren't used
302322
// as we can't malleate spending package to increase their feerate. This
303323
// should change with the remaining anchor output patchset.
@@ -568,13 +588,13 @@ impl PackageTemplate {
568588
self.inputs.iter().map(|(_, outp)| outp.absolute_tx_timelock(self.height_original))
569589
.max().expect("There must always be at least one output to spend in a PackageTemplate")
570590
}
571-
pub(crate) fn package_weight(&self, destination_script: &Script) -> usize {
591+
pub(crate) fn package_weight(&self, destination_script: &Script, opt_anchors: bool) -> usize {
572592
let mut inputs_weight = 0;
573593
let mut witnesses_weight = 2; // count segwit flags
574594
for (_, outp) in self.inputs.iter() {
575595
// previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
576596
inputs_weight += 41 * WITNESS_SCALE_FACTOR;
577-
witnesses_weight += outp.weight();
597+
witnesses_weight += outp.weight(opt_anchors);
578598
}
579599
// version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
580600
let transaction_weight = 10 * WITNESS_SCALE_FACTOR;
@@ -1027,6 +1047,8 @@ mod tests {
10271047
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
10281048
// (nVersion (4) + nLocktime (4) + count_tx_in (1) + prevout (36) + sequence (4) + script_length (1) + count_tx_out (1) + value (8) + var_int (1)) * WITNESS_SCALE_FACTOR
10291049
// + witness marker (2) + WEIGHT_REVOKED_OUTPUT
1030-
assert_eq!(package.package_weight(&Script::new()), (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2 + WEIGHT_REVOKED_OUTPUT as usize);
1050+
for opt_anchors in &[false, true] {
1051+
assert_eq!(package.package_weight(&Script::new(), *opt_anchors), (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2 + WEIGHT_REVOKED_OUTPUT as usize);
1052+
}
10311053
}
10321054
}

0 commit comments

Comments
 (0)