Skip to content

Commit c47d014

Browse files
committed
Add unit test coverage for package::weight_{offered,received}_htlc
1 parent 37001b8 commit c47d014

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

lightning/src/chain/package.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ fn feerate_bump<F: Deref, L: Deref>(predicted_weight: usize, input_amounts: u64,
841841

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

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

1046-
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1047-
// (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
1048-
// + witness marker (2) + WEIGHT_REVOKED_OUTPUT
1049-
for &opt_anchors in [false, true].iter() {
1050-
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);
1058+
// (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 + witness marker (2)
1059+
let weight_sans_output = (4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1) * WITNESS_SCALE_FACTOR + 2;
1060+
1061+
{
1062+
let revk_outp = dumb_revk_output!(secp_ctx);
1063+
let package = PackageTemplate::build_package(txid, 0, revk_outp, 0, true, 100);
1064+
for &opt_anchors in [false, true].iter() {
1065+
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + 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+
for &opt_anchors in [false, true].iter() {
1073+
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_received_htlc(opt_anchors) as usize);
1074+
}
1075+
}
1076+
1077+
{
1078+
let counterparty_outp = dumb_counterparty_offered_output!(secp_ctx, 1_000_000);
1079+
let package = PackageTemplate::build_package(txid, 0, counterparty_outp, 1000, true, 100);
1080+
for &opt_anchors in [false, true].iter() {
1081+
assert_eq!(package.package_weight(&Script::new(), opt_anchors), weight_sans_output + weight_offered_htlc(opt_anchors) as usize);
1082+
}
10511083
}
10521084
}
10531085
}

0 commit comments

Comments
 (0)