Move signature duplicate detection into the historian
This commit is contained in:
@ -11,7 +11,6 @@ use std::collections::HashMap;
|
|||||||
pub struct Accountant {
|
pub struct Accountant {
|
||||||
pub historian: Historian<u64>,
|
pub historian: Historian<u64>,
|
||||||
pub balances: HashMap<PublicKey, u64>,
|
pub balances: HashMap<PublicKey, u64>,
|
||||||
pub signatures: HashMap<Signature, bool>,
|
|
||||||
pub end_hash: Sha256Hash,
|
pub end_hash: Sha256Hash,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,19 +20,13 @@ impl Accountant {
|
|||||||
Accountant {
|
Accountant {
|
||||||
historian: hist,
|
historian: hist,
|
||||||
balances: HashMap::new(),
|
balances: HashMap::new(),
|
||||||
signatures: HashMap::new(),
|
|
||||||
end_hash: *start_hash,
|
end_hash: *start_hash,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_event(self: &mut Self, event: &Event<u64>) {
|
pub fn process_event(self: &mut Self, event: &Event<u64>) {
|
||||||
match *event {
|
match *event {
|
||||||
Event::Claim { key, data, sig } => {
|
Event::Claim { key, data, .. } => {
|
||||||
if self.signatures.contains_key(&sig) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.signatures.insert(sig, true);
|
|
||||||
|
|
||||||
if self.balances.contains_key(&key) {
|
if self.balances.contains_key(&key) {
|
||||||
if let Some(x) = self.balances.get_mut(&key) {
|
if let Some(x) = self.balances.get_mut(&key) {
|
||||||
*x += data;
|
*x += data;
|
||||||
@ -42,16 +35,7 @@ impl Accountant {
|
|||||||
self.balances.insert(key, data);
|
self.balances.insert(key, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Transaction {
|
Event::Transaction { from, to, data, .. } => {
|
||||||
from,
|
|
||||||
to,
|
|
||||||
data,
|
|
||||||
sig,
|
|
||||||
} => {
|
|
||||||
if self.signatures.contains_key(&sig) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.signatures.insert(sig, true);
|
|
||||||
if let Some(x) = self.balances.get_mut(&from) {
|
if let Some(x) = self.balances.get_mut(&from) {
|
||||||
*x -= data;
|
*x -= data;
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,10 @@
|
|||||||
//! The resulting stream of entries represents ordered events in time.
|
//! The resulting stream of entries represents ordered events in time.
|
||||||
|
|
||||||
use std::thread::JoinHandle;
|
use std::thread::JoinHandle;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::sync::mpsc::{Receiver, SyncSender};
|
use std::sync::mpsc::{Receiver, SyncSender};
|
||||||
use std::time::{Duration, SystemTime};
|
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 serde::Serialize;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ fn log_event<T: Serialize + Clone + Debug>(
|
|||||||
fn log_events<T: Serialize + Clone + Debug>(
|
fn log_events<T: Serialize + Clone + Debug>(
|
||||||
receiver: &Receiver<Event<T>>,
|
receiver: &Receiver<Event<T>>,
|
||||||
sender: &SyncSender<Entry<T>>,
|
sender: &SyncSender<Entry<T>>,
|
||||||
|
signatures: &mut HashMap<Signature, bool>,
|
||||||
num_hashes: &mut u64,
|
num_hashes: &mut u64,
|
||||||
end_hash: &mut Sha256Hash,
|
end_hash: &mut Sha256Hash,
|
||||||
epoch: SystemTime,
|
epoch: SystemTime,
|
||||||
@ -63,6 +65,12 @@ fn log_events<T: Serialize + Clone + Debug>(
|
|||||||
match receiver.try_recv() {
|
match receiver.try_recv() {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
if verify_event(&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)?;
|
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 end_hash = start_hash;
|
||||||
let mut num_hashes = 0;
|
let mut num_hashes = 0;
|
||||||
let mut num_ticks = 0;
|
let mut num_ticks = 0;
|
||||||
|
let mut signatures = HashMap::new();
|
||||||
let epoch = SystemTime::now();
|
let epoch = SystemTime::now();
|
||||||
loop {
|
loop {
|
||||||
if let Err(err) = log_events(
|
if let Err(err) = log_events(
|
||||||
&receiver,
|
&receiver,
|
||||||
&sender,
|
&sender,
|
||||||
|
&mut signatures,
|
||||||
&mut num_hashes,
|
&mut num_hashes,
|
||||||
&mut end_hash,
|
&mut end_hash,
|
||||||
epoch,
|
epoch,
|
||||||
|
18
src/log.rs
18
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.
|
/// 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();
|
let mut hash_data = end_hash.to_vec();
|
||||||
hash_data.push(ty);
|
|
||||||
hash_data.extend_from_slice(val);
|
hash_data.extend_from_slice(val);
|
||||||
hash(&hash_data)
|
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 {
|
match *event {
|
||||||
Event::Tick => *end_hash,
|
Event::Tick => None,
|
||||||
Event::Claim { sig, .. } => extend_and_hash(end_hash, 2, &sig),
|
Event::Claim { sig, .. } => Some(sig),
|
||||||
Event::Transaction { sig, .. } => extend_and_hash(end_hash, 3, &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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user