Skip to content

Commit 6909e1c

Browse files
committed
discount: fix calculation to weight amount and nonce by witness scaling factor
1 parent a0cee51 commit 6909e1c

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/transaction.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ impl Transaction {
931931
}
932932

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

@@ -941,10 +941,10 @@ impl Transaction {
941941
let witness_weight = VarInt(sp_len as u64).size() + sp_len + VarInt(rp_len as u64).size() + rp_len;
942942
weight -= witness_weight.saturating_sub(2); // explicit transactions have 1 byte for each empty proof
943943
if out.value.is_confidential() {
944-
weight -= 33 - 9;
944+
weight -= (33 - 9) * 4;
945945
}
946946
if out.nonce.is_confidential() {
947-
weight -= 33 - 1;
947+
weight -= (33 - 1) * 4;
948948
}
949949
}
950950

@@ -2444,16 +2444,16 @@ mod tests {
24442444
assert_eq!(tx.output.len(), 2);
24452445
assert_eq!(tx.weight(), 5330);
24462446
assert_eq!(tx.vsize(), 1333);
2447-
assert_eq!(tx.discount_weight(), 1031);
2448-
assert_eq!(tx.discount_vsize(), 258);
2447+
assert_eq!(tx.discount_weight(), 863);
2448+
assert_eq!(tx.discount_vsize(), 216);
24492449

24502450
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/1in3out_tx.hex"));
24512451
assert_eq!(tx.input.len(), 1);
24522452
assert_eq!(tx.output.len(), 3);
24532453
assert_eq!(tx.weight(), 10107);
24542454
assert_eq!(tx.vsize(), 2527);
2455-
assert_eq!(tx.discount_weight(), 1509);
2456-
assert_eq!(tx.discount_vsize(), 378);
2455+
assert_eq!(tx.discount_weight(), 1173);
2456+
assert_eq!(tx.discount_vsize(), 294);
24572457

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

24742474
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in3out_tx2.hex"));
24752475
assert_eq!(tx.input.len(), 2);
24762476
assert_eq!(tx.output.len(), 3);
24772477
assert_eq!(tx.weight(), 10536);
24782478
assert_eq!(tx.vsize(), 2634);
2479-
assert_eq!(tx.discount_weight(), 1874);
2480-
assert_eq!(tx.discount_vsize(), 469);
2479+
assert_eq!(tx.discount_weight(), 1538);
2480+
assert_eq!(tx.discount_vsize(), 385);
24812481

24822482
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/3in3out_tx.hex"));
24832483
assert_eq!(tx.input.len(), 3);
24842484
assert_eq!(tx.output.len(), 3);
24852485
assert_eq!(tx.weight(), 10922);
24862486
assert_eq!(tx.vsize(), 2731);
2487-
assert_eq!(tx.discount_weight(), 2196);
2488-
assert_eq!(tx.discount_vsize(), 549);
2487+
assert_eq!(tx.discount_weight(), 1860);
2488+
assert_eq!(tx.discount_vsize(), 465);
24892489

24902490
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/4in3out_tx.hex"));
24912491
assert_eq!(tx.input.len(), 4);
24922492
assert_eq!(tx.output.len(), 3);
24932493
assert_eq!(tx.weight(), 11192);
24942494
assert_eq!(tx.vsize(), 2798);
2495-
assert_eq!(tx.discount_weight(), 2466);
2496-
assert_eq!(tx.discount_vsize(), 617);
2495+
assert_eq!(tx.discount_weight(), 2130);
2496+
assert_eq!(tx.discount_vsize(), 533);
24972497

24982498
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in4out_tx.hex"));
24992499
assert_eq!(tx.input.len(), 2);
25002500
assert_eq!(tx.output.len(), 4);
25012501
assert_eq!(tx.weight(), 15261);
25022502
assert_eq!(tx.vsize(), 3816);
2503-
assert_eq!(tx.discount_weight(), 2268);
2504-
assert_eq!(tx.discount_vsize(), 567);
2503+
assert_eq!(tx.discount_weight(), 1764);
2504+
assert_eq!(tx.discount_vsize(), 441);
25052505

25062506
let tx: Transaction = hex_deserialize!(include_str!("../tests/data/2in5out_tx.hex"));
25072507
assert_eq!(tx.input.len(), 2);
25082508
assert_eq!(tx.output.len(), 5);
25092509
assert_eq!(tx.weight(), 20030);
25102510
assert_eq!(tx.vsize(), 5008);
2511-
assert_eq!(tx.discount_weight(), 2706);
2512-
assert_eq!(tx.discount_vsize(), 677);
2511+
assert_eq!(tx.discount_weight(), 2034);
2512+
assert_eq!(tx.discount_vsize(), 509);
25132513
}
25142514
}

0 commit comments

Comments
 (0)