From 3831816a732872e06bc62197b166ce4c1c661ed2 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 8 Mar 2022 17:09:19 +1100 Subject: [PATCH] Move test helper function Move helper function to above the test that uses it. Refactor only, no logic changes. --- src/blockdata/transaction.rs | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 6fc26205..7c4f1a4e 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -1082,25 +1082,6 @@ mod tests { serde_round_trip!(tx); } - fn run_test_sighash(tx: &str, script: &str, input_index: usize, hash_type: i32, expected_result: &str) { - let tx: Transaction = deserialize(&Vec::from_hex(tx).unwrap()[..]).unwrap(); - let script = Script::from(Vec::from_hex(script).unwrap()); - let mut raw_expected = Vec::from_hex(expected_result).unwrap(); - raw_expected.reverse(); - let expected_result = SigHash::from_slice(&raw_expected[..]).unwrap(); - - let actual_result = if raw_expected[0] % 2 == 0 { - // tx.signature_hash and cache.legacy_signature_hash are the same, this if helps to test - // both the codepaths without repeating the test code - tx.signature_hash(input_index, &script, hash_type as u32) - } else { - let cache = SigHashCache::new(&tx); - cache.legacy_signature_hash(input_index, &script, hash_type as u32).unwrap() - }; - - assert_eq!(actual_result, expected_result); - } - // Test decoding transaction `4be105f158ea44aec57bf12c5817d073a712ab131df6f37786872cfc70734188` // from testnet, which is the first BIP144-encoded transaction I encountered. #[test] @@ -1155,6 +1136,25 @@ mod tests { assert_eq!(EcdsaSigHashType::from_u32_standard(nonstandard_hashtype), Err(NonStandardSigHashType(0x04))); } + fn run_test_sighash(tx: &str, script: &str, input_index: usize, hash_type: i32, expected_result: &str) { + let tx: Transaction = deserialize(&Vec::from_hex(tx).unwrap()[..]).unwrap(); + let script = Script::from(Vec::from_hex(script).unwrap()); + let mut raw_expected = Vec::from_hex(expected_result).unwrap(); + raw_expected.reverse(); + let expected_result = SigHash::from_slice(&raw_expected[..]).unwrap(); + + let actual_result = if raw_expected[0] % 2 == 0 { + // tx.signature_hash and cache.legacy_signature_hash are the same, this if helps to test + // both the codepaths without repeating the test code + tx.signature_hash(input_index, &script, hash_type as u32) + } else { + let cache = SigHashCache::new(&tx); + cache.legacy_signature_hash(input_index, &script, hash_type as u32).unwrap() + }; + + assert_eq!(actual_result, expected_result); + } + // These test vectors were stolen from libbtc, which is Copyright 2014 Jonas Schnelli MIT // They were transformed by replacing {...} with run_test_sighash(...), then the ones containing // OP_CODESEPARATOR in their pubkeys were removed