|
12 | 12 |
|
13 | 13 | use core::fmt; |
14 | 14 |
|
| 15 | +#[cfg(feature = "arbitrary")] |
| 16 | +use arbitrary::{Arbitrary, Unstructured}; |
15 | 17 | use hashes::sha256d; |
16 | 18 | use internals::{compact_size, write_err, ToU64}; |
17 | 19 | use io::{BufRead, Write}; |
@@ -1141,6 +1143,38 @@ mod sealed { |
1141 | 1143 | impl Sealed for super::Version {} |
1142 | 1144 | } |
1143 | 1145 |
|
| 1146 | +#[cfg(feature = "arbitrary")] |
| 1147 | +impl<'a> Arbitrary<'a> for InputWeightPrediction { |
| 1148 | + fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> { |
| 1149 | + // limit script size to 4Mwu block size. |
| 1150 | + let max_block = Weight::MAX_BLOCK.to_wu() as usize; |
| 1151 | + let input_script_len = u.int_in_range(0..=max_block)?; |
| 1152 | + let remaining = max_block - input_script_len; |
| 1153 | + |
| 1154 | + // create witness data if there is remaining space. |
| 1155 | + let mut witness_length = u.int_in_range(0..=remaining)?; |
| 1156 | + let mut witness_element_lengths = Vec::new(); |
| 1157 | + |
| 1158 | + // build vec of random witness element lengths. |
| 1159 | + while witness_length > 0 { |
| 1160 | + let elem = u.int_in_range(1..=witness_length)?; |
| 1161 | + witness_element_lengths.push(elem); |
| 1162 | + witness_length -= elem; |
| 1163 | + } |
| 1164 | + |
| 1165 | + match u.int_in_range(0..=7)? { |
| 1166 | + 0 => Ok(InputWeightPrediction::P2WPKH_MAX), |
| 1167 | + 1 => Ok(InputWeightPrediction::NESTED_P2WPKH_MAX), |
| 1168 | + 2 => Ok(InputWeightPrediction::P2PKH_COMPRESSED_MAX), |
| 1169 | + 3 => Ok(InputWeightPrediction::P2PKH_UNCOMPRESSED_MAX), |
| 1170 | + 4 => Ok(InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH), |
| 1171 | + 5 => Ok(InputWeightPrediction::P2TR_KEY_NON_DEFAULT_SIGHASH), |
| 1172 | + 6 => Ok(InputWeightPrediction::new(input_script_len, witness_element_lengths)), |
| 1173 | + _ => Ok(InputWeightPrediction::from_slice(input_script_len, &witness_element_lengths)), |
| 1174 | + } |
| 1175 | + } |
| 1176 | +} |
| 1177 | + |
1144 | 1178 | #[cfg(test)] |
1145 | 1179 | mod tests { |
1146 | 1180 | use hex::FromHex; |
|
0 commit comments