From 8559a49e03be2b8b3ea4215227ae003e822a663c Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 27 May 2025 18:58:02 -0500 Subject: [PATCH] Do not bound Arbitrary parameters passed to InputWeightPrediction Now that InputWeightPrediction can no longer overflow due to extreme values, there is no longer need to bound the Arbitrary parameters passed. --- bitcoin/src/blockdata/transaction.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 690996ece..2eecfed62 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -1165,22 +1165,6 @@ mod sealed { #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for InputWeightPrediction { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { - // limit script size to 4Mwu block size. - let max_block = Weight::MAX_BLOCK.to_wu() as usize; - let input_script_len = u.int_in_range(0..=max_block)?; - let remaining = max_block - input_script_len; - - // create witness data if there is remaining space. - let mut witness_length = u.int_in_range(0..=remaining)?; - let mut witness_element_lengths = Vec::new(); - - // build vec of random witness element lengths. - while witness_length > 0 { - let elem = u.int_in_range(1..=witness_length)?; - witness_element_lengths.push(elem); - witness_length -= elem; - } - match u.int_in_range(0..=7)? { 0 => Ok(InputWeightPrediction::P2WPKH_MAX), 1 => Ok(InputWeightPrediction::NESTED_P2WPKH_MAX), @@ -1188,8 +1172,16 @@ impl<'a> Arbitrary<'a> for InputWeightPrediction { 3 => Ok(InputWeightPrediction::P2PKH_UNCOMPRESSED_MAX), 4 => Ok(InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH), 5 => Ok(InputWeightPrediction::P2TR_KEY_NON_DEFAULT_SIGHASH), - 6 => Ok(InputWeightPrediction::new(input_script_len, witness_element_lengths)), - _ => Ok(InputWeightPrediction::from_slice(input_script_len, &witness_element_lengths)), + 6 => { + let input_script_len = usize::arbitrary(u)?; + let witness_element_lengths: Vec = Vec::arbitrary(u)?; + Ok(InputWeightPrediction::new(input_script_len, witness_element_lengths)) + } + _ => { + let input_script_len = usize::arbitrary(u)?; + let witness_element_lengths: Vec = Vec::arbitrary(u)?; + Ok(InputWeightPrediction::from_slice(input_script_len, &witness_element_lengths)) + } } } }