Skip to content

Commit

Permalink
feat: handle NoReveals in tally_precondition_clause
Browse files Browse the repository at this point in the history
And add conversion between NoReveals RadError and RadonError
  • Loading branch information
tmpolaczyk committed Jan 10, 2020
1 parent c7d3066 commit 3be6787
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data_structures/src/radon_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub enum RadonErrors {
Overflow = 0x41,
/// Tried to divide by zero.
DivisionByZero = 0x42,
// Other errors
/// Received zero reveals
NoReveals = 0x50,
}

/// Use `RadonErrors::Unknown` as the default value of `RadonErrors`.
Expand Down
1 change: 1 addition & 0 deletions rad/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl ErrorLike for RadError {
Some(error),
vec![CborValue::U8(status_code as u8)],
),
RadError::NoReveals => RadonError::new(RadonErrors::NoReveals, Some(error), vec![]),
other => RadonError::from(other),
}),
result => result.map_err(RadonError::from),
Expand Down
6 changes: 6 additions & 0 deletions rad/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ impl TryFrom<&cbor::value::Value> for RadonTypes {
}
}
}
RadonErrors::NoReveals => {
return Err(RadError::NoReveals);
}
RadonErrors::Unknown => {
return Err(RadError::Unknown);
}
_ => {
log::warn!(
"RadonError not implemented in TryFrom CBOR value"
Expand Down
13 changes: 13 additions & 0 deletions validations/src/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ pub fn evaluate_tally_precondition_clause(
reveals: Vec<RadonReport<RadonTypes>>,
non_error_min: f64,
) -> Result<(Vec<RadonTypes>, Vec<bool>), RadError> {
if reveals.is_empty() {
return Err(RadError::NoReveals);
}

let reveals_len = reveals.len() as f64;

let mut counter = Counter::new(RadonTypes::num_types());
Expand Down Expand Up @@ -1613,4 +1617,13 @@ mod tests {

assert_eq!(out, RadError::default());
}

#[test]
fn test_tally_precondition_clause_no_reveals() {
let v = vec![];

let out = evaluate_tally_precondition_clause(v, 0.51).unwrap_err();

assert_eq!(out, RadError::NoReveals);
}
}

0 comments on commit 3be6787

Please sign in to comment.