Check for duplicate signatures
TODO: have client add recent hash to each message
This commit is contained in:
parent
c9cc4b4369
commit
570e71f050
|
@ -11,6 +11,7 @@ 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,
|
||||
}
|
||||
|
||||
|
@ -20,13 +21,19 @@ 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, .. } => {
|
||||
Event::Claim { key, data, sig } => {
|
||||
if self.signatures.contains_key(&sig) {
|
||||
return;
|
||||
}
|
||||
self.signatures.insert(sig, true);
|
||||
|
||||
if self.balances.contains_key(&key) {
|
||||
if let Some(x) = self.balances.get_mut(&key) {
|
||||
*x += data;
|
||||
|
@ -35,7 +42,16 @@ impl Accountant {
|
|||
self.balances.insert(key, data);
|
||||
}
|
||||
}
|
||||
Event::Transaction { from, to, data, .. } => {
|
||||
Event::Transaction {
|
||||
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) {
|
||||
*x -= data;
|
||||
}
|
||||
|
|
|
@ -11,9 +11,7 @@ fn main() {
|
|||
let socket = UdpSocket::bind(send_addr).unwrap();
|
||||
let mut acc = AccountantStub::new(addr, socket);
|
||||
let alice_keypair = generate_keypair();
|
||||
let bob_keypair = generate_keypair();
|
||||
let alice_pubkey = get_pubkey(&alice_keypair);
|
||||
let bob_pubkey = get_pubkey(&bob_keypair);
|
||||
let txs = 10_000;
|
||||
println!("Depositing {} units in Alice's account...", txs);
|
||||
let sig = acc.deposit(txs, &alice_keypair).unwrap();
|
||||
|
@ -24,11 +22,10 @@ fn main() {
|
|||
println!("Transferring 1 unit {} times...", txs);
|
||||
let now = Instant::now();
|
||||
let mut sig = sig;
|
||||
for i in 0..txs {
|
||||
for _ in 0..txs {
|
||||
let bob_keypair = generate_keypair();
|
||||
let bob_pubkey = get_pubkey(&bob_keypair);
|
||||
sig = acc.transfer(1, &alice_keypair, bob_pubkey).unwrap();
|
||||
if i % 200 == 1 {
|
||||
acc.wait_on_signature(&sig).unwrap();
|
||||
}
|
||||
}
|
||||
println!("Waiting for last transaction to be confirmed...",);
|
||||
acc.wait_on_signature(&sig).unwrap();
|
||||
|
@ -37,13 +34,7 @@ fn main() {
|
|||
let ns = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64;
|
||||
let tps = (txs * 1_000_000_000) as f64 / ns as f64;
|
||||
println!("Done. {} tps!", tps);
|
||||
println!(
|
||||
"Alice's Final Balance {}",
|
||||
acc.get_balance(&alice_pubkey).unwrap()
|
||||
);
|
||||
|
||||
println!(
|
||||
"Bob's Final Balance {}",
|
||||
acc.get_balance(&bob_pubkey).unwrap()
|
||||
);
|
||||
let val = acc.get_balance(&alice_pubkey).unwrap();
|
||||
println!("Alice's Final Balance {}", val);
|
||||
assert_eq!(val, 0);
|
||||
}
|
||||
|
|
|
@ -116,8 +116,8 @@ pub fn create_logger<T: 'static + Serialize + Clone + Debug + Send>(
|
|||
impl<T: 'static + Serialize + Clone + Debug + Send> Historian<T> {
|
||||
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
|
||||
use std::sync::mpsc::sync_channel;
|
||||
let (sender, event_receiver) = sync_channel(12000);
|
||||
let (entry_sender, receiver) = sync_channel(12000);
|
||||
let (sender, event_receiver) = sync_channel(1000);
|
||||
let (entry_sender, receiver) = sync_channel(1000);
|
||||
let thread_hdl = create_logger(*start_hash, ms_per_tick, event_receiver, entry_sender);
|
||||
Historian {
|
||||
sender,
|
||||
|
|
Loading…
Reference in New Issue