Standardize validation var identifier on

This commit is contained in:
Noah Vesely
2018-03-28 18:55:08 -06:00
committed by Romain Ruetschi
parent 840f5cce81
commit c437775169

View File

@ -33,16 +33,20 @@ pub(crate) fn validate_shares<S: IsShare>(shares: &Vec<S>) -> Result<(u8, usize)
let mut ids = Vec::with_capacity(shares_count); let mut ids = Vec::with_capacity(shares_count);
let mut k_compatibility_sets = HashMap::new(); let mut k_compatibility_sets = HashMap::new();
let mut data_len_compatibility_sets = HashMap::new(); let mut slen_compatibility_sets = HashMap::new();
for share in shares { for share in shares {
let (id, threshold, data_len) = (share.get_id(), share.get_threshold(), share.get_data().len()); let (id, threshold, slen) = (
share.get_id(),
share.get_threshold(),
share.get_data().len(),
);
if id < 1 { if id < 1 {
bail!(ErrorKind::ShareParsingInvalidShareId(id)) bail!(ErrorKind::ShareParsingInvalidShareId(id))
} else if threshold < 2 { } else if threshold < 2 {
bail!(ErrorKind::ShareParsingInvalidShareThreshold(threshold, id)) bail!(ErrorKind::ShareParsingInvalidShareThreshold(threshold, id))
} else if data_len < 1 { } else if slen < 1 {
bail!(ErrorKind::ShareParsingErrorEmptyShare(id)) bail!(ErrorKind::ShareParsingErrorEmptyShare(id))
} }
@ -56,11 +60,11 @@ pub(crate) fn validate_shares<S: IsShare>(shares: &Vec<S>) -> Result<(u8, usize)
bail!(ErrorKind::DuplicateShareId(id)); bail!(ErrorKind::DuplicateShareId(id));
} }
data_len_compatibility_sets slen_compatibility_sets
.entry(data_len) .entry(slen)
.or_insert_with(HashSet::new); .or_insert_with(HashSet::new);
let data_len_set = data_len_compatibility_sets.get_mut(&data_len).unwrap(); let slen_set = slen_compatibility_sets.get_mut(&slen).unwrap();
data_len_set.insert(id); slen_set.insert(id);
ids.push(id); ids.push(id);
} }
@ -90,14 +94,14 @@ pub(crate) fn validate_shares<S: IsShare>(shares: &Vec<S>) -> Result<(u8, usize)
} }
// Validate share length consistency // Validate share length consistency
let data_len_sets = data_len_compatibility_sets.keys().count(); let slen_sets = slen_compatibility_sets.keys().count();
match data_len_sets { match slen_sets {
1 => {} // All shares have the same `data` field len 1 => {} // All shares have the same `data` field len
_ => { _ => {
bail! { bail! {
ErrorKind::IncompatibleDataLengths( ErrorKind::IncompatibleDataLengths(
data_len_compatibility_sets slen_compatibility_sets
.values() .values()
.map(|x| x.to_owned()) .map(|x| x.to_owned())
.collect(), .collect(),
@ -106,10 +110,10 @@ pub(crate) fn validate_shares<S: IsShare>(shares: &Vec<S>) -> Result<(u8, usize)
} }
} }
// It is safe to unwrap because data_len_sets == 1 // It is safe to unwrap because slen_sets == 1
let slen = data_len_compatibility_sets.keys().last().unwrap().to_owned(); let slen = slen_compatibility_sets.keys().last().unwrap().to_owned();
Ok((threshold, data_len)) Ok((threshold, slen))
} }
pub(crate) fn validate_share_count(threshold: u8, shares_count: u8) -> Result<(u8, u8)> { pub(crate) fn validate_share_count(threshold: u8, shares_count: u8) -> Result<(u8, u8)> {