Make the Discovery event into a struct instead of a tuple

This commit is contained in:
Greg Fitzgerald 2018-02-24 11:15:03 -07:00
parent 7d9bab9508
commit b8d52cc3e4
4 changed files with 15 additions and 8 deletions

View File

@ -37,7 +37,8 @@ use std::sync::mpsc::SendError;
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> { fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
sleep(Duration::from_millis(15)); sleep(Duration::from_millis(15));
hist.sender.send(Event::Discovery(Sha256Hash::default()))?; let data = Sha256Hash::default();
hist.sender.send(Event::Discovery { data })?;
sleep(Duration::from_millis(10)); sleep(Duration::from_millis(10));
Ok(()) Ok(())
} }
@ -62,7 +63,7 @@ Running the program should produce a log similar to:
```rust ```rust
Entry { num_hashes: 0, end_hash: [0, ...], event: Tick } Entry { num_hashes: 0, end_hash: [0, ...], event: Tick }
Entry { num_hashes: 2, end_hash: [67, ...], event: Discovery(3735928559) } Entry { num_hashes: 2, end_hash: [67, ...], event: Discovery { data: [37, ...] } }
Entry { num_hashes: 3, end_hash: [123, ...], event: Tick } Entry { num_hashes: 3, end_hash: [123, ...], event: Tick }
``` ```

View File

@ -8,7 +8,8 @@ use std::sync::mpsc::SendError;
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> { fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
sleep(Duration::from_millis(15)); sleep(Duration::from_millis(15));
hist.sender.send(Event::Discovery(Sha256Hash::default()))?; let data = Sha256Hash::default();
hist.sender.send(Event::Discovery { data })?;
sleep(Duration::from_millis(10)); sleep(Duration::from_millis(10));
Ok(()) Ok(())
} }

View File

@ -137,7 +137,7 @@ mod tests {
hist.sender.send(Event::Tick).unwrap(); hist.sender.send(Event::Tick).unwrap();
sleep(Duration::new(0, 1_000_000)); sleep(Duration::new(0, 1_000_000));
hist.sender.send(Event::Discovery(zero)).unwrap(); hist.sender.send(Event::Discovery { data: zero }).unwrap();
sleep(Duration::new(0, 1_000_000)); sleep(Duration::new(0, 1_000_000));
hist.sender.send(Event::Tick).unwrap(); hist.sender.send(Event::Tick).unwrap();
@ -171,7 +171,7 @@ mod tests {
let zero = Sha256Hash::default(); let zero = Sha256Hash::default();
let hist = Historian::new(&zero, Some(20)); let hist = Historian::new(&zero, Some(20));
sleep(Duration::from_millis(30)); sleep(Duration::from_millis(30));
hist.sender.send(Event::Discovery(zero)).unwrap(); hist.sender.send(Event::Discovery { data: zero }).unwrap();
sleep(Duration::from_millis(15)); sleep(Duration::from_millis(15));
drop(hist.sender); drop(hist.sender);
assert_eq!( assert_eq!(

View File

@ -35,7 +35,9 @@ pub struct Entry {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Event { pub enum Event {
Tick, Tick,
Discovery(Sha256Hash), Discovery {
data: Sha256Hash,
},
Claim { Claim {
key: PublicKey, key: PublicKey,
data: Sha256Hash, data: Sha256Hash,
@ -97,7 +99,7 @@ pub fn extend_and_hash(end_hash: &Sha256Hash, ty: u8, val: &[u8]) -> Sha256Hash
pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash { pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash {
match *event { match *event {
Event::Tick => *end_hash, Event::Tick => *end_hash,
Event::Discovery(data) => extend_and_hash(end_hash, 1, &data), Event::Discovery { data } => extend_and_hash(end_hash, 1, &data),
Event::Claim { key, data, sig } => { Event::Claim { key, data, sig } => {
let mut event_data = data.to_vec(); let mut event_data = data.to_vec();
event_data.extend_from_slice(&sig); event_data.extend_from_slice(&sig);
@ -218,7 +220,10 @@ mod tests {
// First, verify Discovery events // First, verify Discovery events
let mut end_hash = zero; let mut end_hash = zero;
let events = [Event::Discovery(zero), Event::Discovery(one)]; let events = [
Event::Discovery { data: zero },
Event::Discovery { data: one },
];
let mut entries: Vec<Entry> = events let mut entries: Vec<Entry> = events
.iter() .iter()
.map(|event| { .map(|event| {