From c935fdb12fd5e0f9f5995422934b661f206e0db3 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 1 Mar 2018 17:44:10 -0700 Subject: [PATCH] Move signature duplicate detection into the historian --- src/accountant.rs | 20 ++------------------ src/historian.rs | 12 +++++++++++- src/log.rs | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/accountant.rs b/src/accountant.rs index ab2e3adc8f..627f214bad 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -11,7 +11,6 @@ use std::collections::HashMap; pub struct Accountant { pub historian: Historian, pub balances: HashMap, - pub signatures: HashMap, pub end_hash: Sha256Hash, } @@ -21,19 +20,13 @@ impl Accountant { Accountant { historian: hist, balances: HashMap::new(), - signatures: HashMap::new(), end_hash: *start_hash, } } pub fn process_event(self: &mut Self, event: &Event) { match *event { - Event::Claim { key, data, sig } => { - if self.signatures.contains_key(&sig) { - return; - } - self.signatures.insert(sig, true); - + Event::Claim { key, data, .. } => { if self.balances.contains_key(&key) { if let Some(x) = self.balances.get_mut(&key) { *x += data; @@ -42,16 +35,7 @@ impl Accountant { self.balances.insert(key, data); } } - Event::Transaction { - from, - to, - data, - sig, - } => { - if self.signatures.contains_key(&sig) { - return; - } - self.signatures.insert(sig, true); + Event::Transaction { from, to, data, .. } => { if let Some(x) = self.balances.get_mut(&from) { *x -= data; } diff --git a/src/historian.rs b/src/historian.rs index 0226b6ac69..1a0441451c 100644 --- a/src/historian.rs +++ b/src/historian.rs @@ -6,9 +6,10 @@ //! The resulting stream of entries represents ordered events in time. use std::thread::JoinHandle; +use std::collections::HashMap; use std::sync::mpsc::{Receiver, SyncSender}; use std::time::{Duration, SystemTime}; -use log::{hash, hash_event, verify_event, Entry, Event, Sha256Hash}; +use log::{get_signature, hash, hash_event, verify_event, Entry, Event, Sha256Hash, Signature}; use serde::Serialize; use std::fmt::Debug; @@ -45,6 +46,7 @@ fn log_event( fn log_events( receiver: &Receiver>, sender: &SyncSender>, + signatures: &mut HashMap, num_hashes: &mut u64, end_hash: &mut Sha256Hash, epoch: SystemTime, @@ -63,6 +65,12 @@ fn log_events( match receiver.try_recv() { Ok(event) => { if verify_event(&event) { + if let Some(sig) = get_signature(&event) { + if signatures.contains_key(&sig) { + continue; + } + signatures.insert(sig, true); + } log_event(sender, num_hashes, end_hash, event)?; } } @@ -94,11 +102,13 @@ pub fn create_logger( let mut end_hash = start_hash; let mut num_hashes = 0; let mut num_ticks = 0; + let mut signatures = HashMap::new(); let epoch = SystemTime::now(); loop { if let Err(err) = log_events( &receiver, &sender, + &mut signatures, &mut num_hashes, &mut end_hash, epoch, diff --git a/src/log.rs b/src/log.rs index 99d1eff75c..04a3b24823 100644 --- a/src/log.rs +++ b/src/log.rs @@ -101,18 +101,24 @@ pub fn hash(val: &[u8]) -> Sha256Hash { } /// Return the hash of the given hash extended with the given value. -pub fn extend_and_hash(end_hash: &Sha256Hash, ty: u8, val: &[u8]) -> Sha256Hash { +pub fn extend_and_hash(end_hash: &Sha256Hash, val: &[u8]) -> Sha256Hash { let mut hash_data = end_hash.to_vec(); - hash_data.push(ty); hash_data.extend_from_slice(val); hash(&hash_data) } -pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash { +pub fn get_signature(event: &Event) -> Option { match *event { - Event::Tick => *end_hash, - Event::Claim { sig, .. } => extend_and_hash(end_hash, 2, &sig), - Event::Transaction { sig, .. } => extend_and_hash(end_hash, 3, &sig), + Event::Tick => None, + Event::Claim { sig, .. } => Some(sig), + Event::Transaction { sig, .. } => Some(sig), + } +} + +pub fn hash_event(end_hash: &Sha256Hash, event: &Event) -> Sha256Hash { + match get_signature(event) { + None => *end_hash, + Some(sig) => extend_and_hash(end_hash, &sig), } }