2018-03-04 00:21:40 -08:00
|
|
|
//extern crate serde_json;
|
2018-02-28 09:07:54 -08:00
|
|
|
extern crate silk;
|
|
|
|
|
2018-03-04 00:21:40 -08:00
|
|
|
//use std::io::stdin;
|
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
fn main() {
|
2018-02-28 13:16:50 -08:00
|
|
|
use silk::accountant_stub::AccountantStub;
|
2018-03-03 20:15:42 -08:00
|
|
|
use silk::accountant_skel::AccountantSkel;
|
|
|
|
use silk::accountant::Accountant;
|
|
|
|
use silk::event::{generate_keypair, get_pubkey, sign_transaction_data};
|
2018-03-03 23:13:40 -08:00
|
|
|
use silk::genesis::Genesis;
|
2018-03-01 11:23:27 -08:00
|
|
|
use std::time::Instant;
|
|
|
|
use std::net::UdpSocket;
|
2018-03-03 20:15:42 -08:00
|
|
|
use std::thread::{sleep, spawn};
|
|
|
|
use std::time::Duration;
|
2018-02-28 13:16:50 -08:00
|
|
|
|
|
|
|
let addr = "127.0.0.1:8000";
|
2018-03-01 11:23:27 -08:00
|
|
|
let send_addr = "127.0.0.1:8001";
|
2018-03-03 20:15:42 -08:00
|
|
|
|
|
|
|
let txs = 200;
|
2018-03-04 00:21:40 -08:00
|
|
|
|
2018-03-03 23:13:40 -08:00
|
|
|
let gen = Genesis::new(txs, vec![]);
|
|
|
|
let alice_keypair = generate_keypair();
|
2018-03-04 00:21:40 -08:00
|
|
|
//let gen: Genesis = serde_json::from_reader(stdin()).unwrap();
|
|
|
|
//let alice_keypair = gen.get_keypair();
|
|
|
|
|
2018-03-03 23:13:40 -08:00
|
|
|
let acc = Accountant::new(&gen, None);
|
2018-03-03 20:15:42 -08:00
|
|
|
spawn(move || AccountantSkel::new(acc).serve(addr).unwrap());
|
|
|
|
sleep(Duration::from_millis(30));
|
2018-02-28 09:07:54 -08:00
|
|
|
|
2018-03-03 20:15:42 -08:00
|
|
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
|
|
|
let acc = AccountantStub::new(addr, socket);
|
|
|
|
let alice_pubkey = get_pubkey(&alice_keypair);
|
2018-03-03 09:23:31 -08:00
|
|
|
let one = 1;
|
|
|
|
println!("Signing transactions...");
|
|
|
|
let now = Instant::now();
|
2018-03-03 10:45:21 -08:00
|
|
|
let sigs: Vec<(_, _)> = (0..txs)
|
|
|
|
.map(|_| {
|
|
|
|
let rando_keypair = generate_keypair();
|
|
|
|
let rando_pubkey = get_pubkey(&rando_keypair);
|
|
|
|
let sig = sign_transaction_data(&one, &alice_keypair, &rando_pubkey);
|
|
|
|
(rando_pubkey, sig)
|
|
|
|
})
|
|
|
|
.collect();
|
2018-03-03 09:23:31 -08:00
|
|
|
let duration = now.elapsed();
|
|
|
|
let ns = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64;
|
|
|
|
let bsps = txs as f64 / ns as f64;
|
|
|
|
let nsps = ns as f64 / txs as f64;
|
|
|
|
println!(
|
2018-03-03 10:52:46 -08:00
|
|
|
"Done. {} thousand signatures per second, {}us per signature",
|
|
|
|
bsps * 1_000_000_f64,
|
|
|
|
nsps / 1_000_f64
|
2018-03-03 09:23:31 -08:00
|
|
|
);
|
|
|
|
|
2018-03-03 10:59:34 -08:00
|
|
|
println!("Verify signatures...");
|
|
|
|
use silk::event::{verify_event, Event};
|
|
|
|
let now = Instant::now();
|
|
|
|
for &(k, s) in &sigs {
|
|
|
|
let e = Event::Transaction {
|
2018-03-03 19:41:05 -08:00
|
|
|
from: alice_pubkey,
|
2018-03-03 10:59:34 -08:00
|
|
|
to: k,
|
|
|
|
data: one,
|
|
|
|
sig: s,
|
|
|
|
};
|
|
|
|
assert!(verify_event(&e));
|
|
|
|
}
|
|
|
|
let duration = now.elapsed();
|
|
|
|
let ns = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64;
|
|
|
|
let bsvps = txs as f64 / ns as f64;
|
|
|
|
let nspsv = ns as f64 / txs as f64;
|
|
|
|
println!(
|
|
|
|
"Done. {} thousand signature verifications per second, {}us per signature verification",
|
|
|
|
bsvps * 1_000_000_f64,
|
|
|
|
nspsv / 1_000_f64
|
|
|
|
);
|
|
|
|
|
2018-02-28 18:33:28 -08:00
|
|
|
println!("Transferring 1 unit {} times...", txs);
|
2018-03-01 11:23:27 -08:00
|
|
|
let now = Instant::now();
|
2018-03-03 20:15:42 -08:00
|
|
|
let mut sig = Default::default();
|
2018-03-03 09:23:31 -08:00
|
|
|
for (k, s) in sigs {
|
|
|
|
acc.transfer_signed(alice_pubkey, k, one, s).unwrap();
|
|
|
|
sig = s;
|
2018-02-28 18:33:28 -08:00
|
|
|
}
|
2018-03-01 11:23:27 -08:00
|
|
|
println!("Waiting for last transaction to be confirmed...",);
|
|
|
|
acc.wait_on_signature(&sig).unwrap();
|
2018-02-28 09:07:54 -08:00
|
|
|
|
2018-03-01 11:23:27 -08:00
|
|
|
let duration = now.elapsed();
|
|
|
|
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);
|
2018-03-01 13:07:23 -08:00
|
|
|
let val = acc.get_balance(&alice_pubkey).unwrap();
|
|
|
|
println!("Alice's Final Balance {}", val);
|
|
|
|
assert_eq!(val, 0);
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|