Skip to content

Commit 2d87938

Browse files
committed
Add unit test coverage for package::weight_{offered,received}_htlc
1 parent a938df6 commit 2d87938

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

lightning/src/chain/package.rs

+42-7
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ fn feerate_bump<F: Deref, L: Deref>(predicted_weight: usize, input_amounts: u64,
842842

843843
#[cfg(test)]
844844
mod tests {
845-
use chain::package::{CounterpartyReceivedHTLCOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedOutput, WEIGHT_REVOKED_OUTPUT};
845+
use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc};
846846
use chain::Txid;
847847
use ln::chan_utils::HTLCOutputInCommitment;
848848
use ln::{PaymentPreimage, PaymentHash};
@@ -878,6 +878,19 @@ mod tests {
878878
}
879879
}
880880

881+
macro_rules! dumb_counterparty_offered_output {
882+
($secp_ctx: expr, $amt: expr) => {
883+
{
884+
let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
885+
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
886+
let hash = PaymentHash([1; 32]);
887+
let preimage = PaymentPreimage([2;32]);
888+
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
889+
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc))
890+
}
891+
}
892+
}
893+
881894
macro_rules! dumb_htlc_output {
882895
() => {
883896
{
@@ -1042,13 +1055,35 @@ mod tests {
10421055
fn test_package_weight() {
10431056
let txid = Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap();
10441057
let secp_ctx = Secp256k1::new();
1045-
let revk_outp = dumb_revk_output!(secp_ctx);
10461058

1047-
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1048-
// (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
1049-
// + witness marker (2) + WEIGHT_REVOKED_OUTPUT
1050-
for &opt_anchors in [false, true].iter() {
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);
1059+
{
1060+
let revk_outp = dumb_revk_output!(secp_ctx);
1061+
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1062+
// (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
1063+
// + witness marker (2) + WEIGHT_REVOKED_OUTPUT
1064+
for &opt_anchors in [false, true].iter() {
1065+
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);
1066+
}
1067+
}
1068+
1069+
{
1070+
let counterparty_outp = dumb_counterparty_output!(secp_ctx, 1_000_000);
1071+
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1072+
// (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
1073+
// + witness marker (2) + weight_received_htlc(opt_anchors)
1074+
for &opt_anchors in [false, true].iter() {
1075+
assert_eq!(package.package_weight(&Script::new(), opt_anchors), (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2 + weight_received_htlc(opt_anchors) as usize);
1076+
}
1077+
}
1078+
1079+
{
1080+
let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000);
1081+
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1082+
// (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
1083+
// + witness marker (2) + weight_offered_htlc(opt_anchors)
1084+
for &opt_anchors in [false, true].iter() {
1085+
assert_eq!(package.package_weight(&Script::new(), opt_anchors), (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2 + weight_offered_htlc(opt_anchors) as usize);
1086+
}
10521087
}
10531088
}
10541089
}

0 commit comments

Comments
 (0)