485: Update assert_weight test to account for a range up to 8 bytes. r=bonomat a=bonomat

Fixes #482 

Weights fluctuate because of the length of the signatures. Valid ecdsa signatures can have 68, 69, 70, 71, or 72 bytes. Since most of our transactions have 2 signatures the weight can be up to 8 bytes less than the static weight (4 bytes per signature). 
Since it is really hard to get these short signatures (<1 in 100), I also include the transaction in the assert message which will help for debugging purposes. 

Source: https://medium.com/coinmonks/on-bitcoin-transaction-sizes-97e31bc9d816

Co-authored-by: Philipp Hoenisch <philipp@hoenisch.at>
pull/483/head
bors[bot] 3 years ago committed by GitHub
commit af2938a39a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -393,40 +393,26 @@ mod tests {
.unwrap(); .unwrap();
let refund_transaction = bob_state6.signed_refund_transaction().unwrap(); let refund_transaction = bob_state6.signed_refund_transaction().unwrap();
assert_weight( assert_weight(redeem_transaction, TxRedeem::weight(), "TxRedeem");
redeem_transaction.get_weight(), assert_weight(cancel_transaction, TxCancel::weight(), "TxCancel");
TxRedeem::weight(), assert_weight(punish_transaction, TxPunish::weight(), "TxPunish");
"TxRedeem", assert_weight(refund_transaction, TxRefund::weight(), "TxRefund");
);
assert_weight(
cancel_transaction.get_weight(),
TxCancel::weight(),
"TxCancel",
);
assert_weight(
punish_transaction.get_weight(),
TxPunish::weight(),
"TxPunish",
);
assert_weight(
refund_transaction.get_weight(),
TxRefund::weight(),
"TxRefund",
);
} }
// Weights fluctuate -+1 wu because of the length of the signatures. // Weights fluctuate because of the length of the signatures. Valid ecdsa
// Some of our transactions have 2 signatures and hence the weight can fluctuate // signatures can have 68, 69, 70, 71, or 72 bytes. Since most of our
// +-2 // transactions have 2 signatures the weight can be up to 8 bytes less than
fn assert_weight(is_weight: usize, expected_weight: usize, tx_name: &str) { // the static weight (4 bytes per signature).
fn assert_weight(transaction: Transaction, expected_weight: usize, tx_name: &str) {
let is_weight = transaction.get_weight();
assert!( assert!(
is_weight + 1 == expected_weight expected_weight - is_weight <= 8,
|| is_weight + 2 == expected_weight "{} to have weight {}, but was {}. Transaction: {:#?}",
|| is_weight == expected_weight,
"{} to have weight {}, but was {}",
tx_name, tx_name,
expected_weight, expected_weight,
is_weight is_weight,
transaction
) )
} }
} }

Loading…
Cancel
Save