Move signature duplicate detection into the historian

This commit is contained in:
Greg Fitzgerald 2018-03-01 17:44:10 -07:00
parent 9e16937914
commit c935fdb12f
3 changed files with 25 additions and 25 deletions

View File

@ -11,7 +11,6 @@ use std::collections::HashMap;
pub struct Accountant {
pub historian: Historian<u64>,
pub balances: HashMap<PublicKey, u64>,
pub signatures: HashMap<Signature, bool>,
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<u64>) {
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;
}

View File

@ -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<T: Serialize + Clone + Debug>(
fn log_events<T: Serialize + Clone + Debug>(
receiver: &Receiver<Event<T>>,
sender: &SyncSender<Entry<T>>,
signatures: &mut HashMap<Signature, bool>,
num_hashes: &mut u64,
end_hash: &mut Sha256Hash,
epoch: SystemTime,
@ -63,6 +65,12 @@ fn log_events<T: Serialize + Clone + Debug>(
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<T: 'static + Serialize + Clone + Debug + Send>(
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,

View File

@ -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<T>(end_hash: &Sha256Hash, event: &Event<T>) -> Sha256Hash {
pub fn get_signature<T>(event: &Event<T>) -> Option<Signature> {
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<T>(end_hash: &Sha256Hash, event: &Event<T>) -> Sha256Hash {
match get_signature(event) {
None => *end_hash,
Some(sig) => extend_and_hash(end_hash, &sig),
}
}