Comment predict_weight

This commit is contained in:
yancy 2023-04-11 20:22:18 +02:00
parent 36500b4451
commit dff757d7db
1 changed files with 16 additions and 0 deletions

View File

@ -1216,6 +1216,13 @@ where
I: IntoIterator<Item = InputWeightPrediction>, I: IntoIterator<Item = InputWeightPrediction>,
O: IntoIterator<Item = usize>, O: IntoIterator<Item = usize>,
{ {
// This fold() does three things:
// 1) Counts the inputs and returns the sum as `input_count`.
// 2) Sums all of the input weights and returns the sum as `partial_input_weight`
// For every input: script_size * 4 + witness_size
// Since script_size is non-witness data, it gets a 4x multiplier.
// 3) Counts the number of inputs that have a witness data and returns the count as
// `num_inputs_with_witnesses`.
let (input_count, partial_input_weight, inputs_with_witnesses) = inputs.into_iter().fold( let (input_count, partial_input_weight, inputs_with_witnesses) = inputs.into_iter().fold(
(0, 0, 0), (0, 0, 0),
|(count, partial_input_weight, inputs_with_witnesses), prediction| { |(count, partial_input_weight, inputs_with_witnesses), prediction| {
@ -1226,6 +1233,11 @@ where
) )
}, },
); );
// This fold() does two things:
// 1) Counts the outputs and returns the sum as `output_count`.
// 2) Sums the output script sizes and returns the sum as `output_scripts_size`.
// script_len + the length of a VarInt struct that stores the value of script_len
let (output_count, output_scripts_size) = output_script_lens.into_iter().fold( let (output_count, output_scripts_size) = output_script_lens.into_iter().fold(
(0, 0), (0, 0),
|(output_count, total_scripts_size), script_len| { |(output_count, total_scripts_size), script_len| {
@ -1249,7 +1261,11 @@ const fn predict_weight_internal(
output_count: usize, output_count: usize,
output_scripts_size: usize, output_scripts_size: usize,
) -> Weight { ) -> Weight {
// Lengths of txid, index and sequence: (32, 4, 4).
// Multiply the lengths by 4 since the fields are all non-witness fields.
let input_weight = partial_input_weight + input_count * 4 * (32 + 4 + 4); let input_weight = partial_input_weight + input_count * 4 * (32 + 4 + 4);
// The value field of a TxOut is 8 bytes.
let output_size = 8 * output_count + output_scripts_size; let output_size = 8 * output_count + output_scripts_size;
let non_input_size = let non_input_size =
// version: // version: