Skip to content

Commit

Permalink
addressed some PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Aug 31, 2023
1 parent e1feed9 commit e17ae06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn fetch_chain_info(
transport: &HttpTransport,
base_url: &str,
) -> Result<ChainInfo, DrandClientError> {
let url = format!("{}/info", base_url);
let url = format!("{base_url}/info");
match transport.fetch(&url) {
Err(_) => Err(DrandClientError::NotResponding),
Ok(body) => serde_json::from_str(&body).map_err(|e| {
Expand All @@ -51,9 +51,10 @@ impl<'a, T: Transport> DrandClient<'a, T> {

pub fn randomness(&self, round_number: u64) -> Result<Beacon, DrandClientError> {
if round_number == 0 {
return Err(InvalidRound);
Err(InvalidRound)
} else {
self.fetch_beacon_tag(&format!("{}", round_number))
}
self.fetch_beacon_tag(&format!("{}", round_number))
}

fn fetch_beacon_tag(&self, tag: &str) -> Result<Beacon, DrandClientError> {
Expand Down Expand Up @@ -111,7 +112,7 @@ mod test {
let client = new_http_client(chained_url)?;
let randomness = client.latest_randomness()?;
assert!(randomness.round_number > 0);
return Ok(());
Ok(())
}

#[test]
Expand All @@ -120,7 +121,7 @@ mod test {
let client = new_http_client(unchained_url)?;
let randomness = client.latest_randomness()?;
assert!(randomness.round_number > 0);
return Ok(());
Ok(())
}

#[test]
Expand All @@ -130,7 +131,7 @@ mod test {
let result = client.randomness(0);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InvalidRound);
return Ok(());
Ok(())
}

#[test]
Expand Down
15 changes: 9 additions & 6 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ fn chained_beacon_message(beacon: &Beacon) -> Result<Vec<u8>, VerificationError>
if beacon.previous_signature.is_empty() {
Err(VerificationError::ChainedBeaconNeedsPreviousSignature)
} else {
// surely there's a better way to concat two slices
let mut message = Vec::new();
message.extend_from_slice(beacon.previous_signature.as_slice());
message.extend_from_slice(&beacon.round_number.to_be_bytes());
let message: Vec<u8> = beacon
.previous_signature
.clone()
.into_iter()
.chain(beacon.round_number.to_be_bytes().into_iter())
.collect();

Ok(Sha256::digest(message.as_slice()).to_vec())
}
}
Expand Down Expand Up @@ -596,15 +599,15 @@ mod test {
}

fn dehexify(s: &str) -> Vec<u8> {
return hex::decode(s).unwrap().to_vec();
hex::decode(s).unwrap().to_vec()
}

fn assert_error(actual: Result<(), VerificationError>, expected: VerificationError) {
match actual {
Ok(_) => panic!("expected error but got success"),
Err(e) => {
if e != expected {
panic!("expected {:?} but got {:?}", expected, e);
panic!("expected {expected:?} but got {e:?}");
}
}
}
Expand Down

0 comments on commit e17ae06

Please sign in to comment.