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.
This commit is contained in:
Greg Fitzgerald 2018-03-04 09:52:33 -07:00
parent 8503a0a58f
commit ff82fbf112
4 changed files with 15 additions and 10 deletions

View File

@ -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 }
```

View File

@ -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)" ] ;

View File

@ -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<T: Serialize>(
start_hash: &Sha256Hash,
num_hashes: u64,
event: &Event<T>,
) -> 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<T: Serialize>(
start_hash: &Sha256Hash,
num_hashes: u64,

View File

@ -61,6 +61,7 @@ impl<T: Serialize + Clone + Debug> Logger<T> {
pub fn log_event(&mut self, event: Event<T>) -> Result<(), (Entry<T>, 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,