Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discount: fix weight calculation #218

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ impl Transaction {
}

/// Get the "discount weight" of this transaction; this is the weight minus the output witnesses and minus the
/// differences between asset and nonce commitments from their explicit values.
/// differences between asset and nonce commitments from their explicit values (weighted as part of the base transaction).
pub fn discount_weight(&self) -> usize {
let mut weight = self.scaled_size(4);

Expand All @@ -941,10 +941,10 @@ impl Transaction {
let witness_weight = VarInt(sp_len as u64).size() + sp_len + VarInt(rp_len as u64).size() + rp_len;
weight -= witness_weight.saturating_sub(2); // explicit transactions have 1 byte for each empty proof
if out.value.is_confidential() {
weight -= 33 - 9;
weight -= (33 - 9) * 4;
}
if out.nonce.is_confidential() {
weight -= 33 - 1;
weight -= (33 - 1) * 4;
}
}

Expand Down Expand Up @@ -2444,16 +2444,16 @@ mod tests {
assert_eq!(tx.output.len(), 2);
assert_eq!(tx.weight(), 5330);
assert_eq!(tx.vsize(), 1333);
assert_eq!(tx.discount_weight(), 1031);
assert_eq!(tx.discount_vsize(), 258);
assert_eq!(tx.discount_weight(), 863);
assert_eq!(tx.discount_vsize(), 216);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/1in3out_tx.hex"));
assert_eq!(tx.input.len(), 1);
assert_eq!(tx.output.len(), 3);
assert_eq!(tx.weight(), 10107);
assert_eq!(tx.vsize(), 2527);
assert_eq!(tx.discount_weight(), 1509);
assert_eq!(tx.discount_vsize(), 378);
assert_eq!(tx.discount_weight(), 1173);
assert_eq!(tx.discount_vsize(), 294);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in3out_exp.hex"));
assert_eq!(tx.input.len(), 2);
Expand All @@ -2468,47 +2468,47 @@ mod tests {
assert_eq!(tx.output.len(), 3);
assert_eq!(tx.weight(), 10300);
assert_eq!(tx.vsize(), 2575);
assert_eq!(tx.discount_weight(), 1638);
assert_eq!(tx.discount_vsize(), 410);
assert_eq!(tx.discount_weight(), 1302);
assert_eq!(tx.discount_vsize(), 326);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in3out_tx2.hex"));
assert_eq!(tx.input.len(), 2);
assert_eq!(tx.output.len(), 3);
assert_eq!(tx.weight(), 10536);
assert_eq!(tx.vsize(), 2634);
assert_eq!(tx.discount_weight(), 1874);
assert_eq!(tx.discount_vsize(), 469);
assert_eq!(tx.discount_weight(), 1538);
assert_eq!(tx.discount_vsize(), 385);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/3in3out_tx.hex"));
assert_eq!(tx.input.len(), 3);
assert_eq!(tx.output.len(), 3);
assert_eq!(tx.weight(), 10922);
assert_eq!(tx.vsize(), 2731);
assert_eq!(tx.discount_weight(), 2196);
assert_eq!(tx.discount_vsize(), 549);
assert_eq!(tx.discount_weight(), 1860);
assert_eq!(tx.discount_vsize(), 465);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/4in3out_tx.hex"));
assert_eq!(tx.input.len(), 4);
assert_eq!(tx.output.len(), 3);
assert_eq!(tx.weight(), 11192);
assert_eq!(tx.vsize(), 2798);
assert_eq!(tx.discount_weight(), 2466);
assert_eq!(tx.discount_vsize(), 617);
assert_eq!(tx.discount_weight(), 2130);
assert_eq!(tx.discount_vsize(), 533);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in4out_tx.hex"));
assert_eq!(tx.input.len(), 2);
assert_eq!(tx.output.len(), 4);
assert_eq!(tx.weight(), 15261);
assert_eq!(tx.vsize(), 3816);
assert_eq!(tx.discount_weight(), 2268);
assert_eq!(tx.discount_vsize(), 567);
assert_eq!(tx.discount_weight(), 1764);
assert_eq!(tx.discount_vsize(), 441);

let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in5out_tx.hex"));
assert_eq!(tx.input.len(), 2);
assert_eq!(tx.output.len(), 5);
assert_eq!(tx.weight(), 20030);
assert_eq!(tx.vsize(), 5008);
assert_eq!(tx.discount_weight(), 2706);
assert_eq!(tx.discount_vsize(), 677);
assert_eq!(tx.discount_weight(), 2034);
assert_eq!(tx.discount_vsize(), 509);
}
}
Loading