From ff82fbf1129f33561f0935155d6b66fb4ee93828 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Sun, 4 Mar 2018 09:52:33 -0700 Subject: [PATCH] Make num_hashes mean the num_hashes since the last ID Before this change, num_hashes meant the number of hashes since the last ID, minus any hashing done on the event data. It made no difference for Tick events, but logged Transaction events with one less hash than actually occurred. --- README.md | 2 +- diagrams/historian.msc | 6 +++--- src/log.rs | 16 ++++++++++------ src/logger.rs | 1 + 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 560f5e75e..efd238c10 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Running the program should produce a log similar to: ```rust Entry { num_hashes: 0, id: [0, ...], event: Tick } -Entry { num_hashes: 2, id: [67, ...], event: Transaction { data: [37, ...] } } +Entry { num_hashes: 3, id: [67, ...], event: Transaction { data: [37, ...] } } Entry { num_hashes: 3, id: [123, ...], event: Tick } ``` diff --git a/diagrams/historian.msc b/diagrams/historian.msc index 0a97b70c6..91e93539d 100644 --- a/diagrams/historian.msc +++ b/diagrams/historian.msc @@ -4,10 +4,10 @@ msc { logger=>historian [ label = "e0 = Entry{id: h0, n: 0, event: Tick}" ] ; logger=>logger [ label = "h1 = hash(h0)" ] ; logger=>logger [ label = "h2 = hash(h1)" ] ; - client=>historian [ label = "Claim(d0)" ] ; - historian=>logger [ label = "Claim(d0)" ] ; + client=>historian [ label = "Transaction(d0)" ] ; + historian=>logger [ label = "Transaction(d0)" ] ; logger=>logger [ label = "h3 = hash(h2 + d0)" ] ; - logger=>historian [ label = "e1 = Entry{id: hash(h3), n: 2, event: Claim(d0)}" ] ; + logger=>historian [ label = "e1 = Entry{id: hash(h3), n: 3, event: Transaction(d0)}" ] ; logger=>logger [ label = "h4 = hash(h3)" ] ; logger=>logger [ label = "h5 = hash(h4)" ] ; logger=>logger [ label = "h6 = hash(h5)" ] ; diff --git a/src/log.rs b/src/log.rs index cc5881b2b..68a437d6b 100644 --- a/src/log.rs +++ b/src/log.rs @@ -56,23 +56,27 @@ pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash { hash(&hash_data) } -/// Creates the hash 'num_hashes' after start_hash, plus an additional hash for any event data. +/// Creates the hash 'num_hashes' after start_hash. If the event contains +/// signature, the final hash will be a hash of both the previous ID and +/// the signature. pub fn next_hash( start_hash: &Sha256Hash, num_hashes: u64, event: &Event, ) -> Sha256Hash { let mut id = *start_hash; - for _ in 0..num_hashes { + let sig = get_signature(event); + let start_index = if sig.is_some() { 1 } else { 0 }; + for _ in start_index..num_hashes { id = hash(&id); } - match get_signature(event) { - None => id, - Some(sig) => extend_and_hash(&id, &sig), + if let Some(sig) = sig { + id = extend_and_hash(&id, &sig); } + id } -/// Creates the next Tick Entry 'num_hashes' after 'start_hash'. +/// Creates the next Entry 'num_hashes' after 'start_hash'. pub fn next_entry( start_hash: &Sha256Hash, num_hashes: u64, diff --git a/src/logger.rs b/src/logger.rs index 627ce75e6..ae54dd182 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -61,6 +61,7 @@ impl Logger { pub fn log_event(&mut self, event: Event) -> Result<(), (Entry, ExitReason)> { if let Some(sig) = get_signature(&event) { self.last_id = extend_and_hash(&self.last_id, &sig); + self.num_hashes += 1; } let entry = Entry { id: self.last_id,