2018-03-05 15:34:15 -07:00
|
|
|
extern crate serde_json;
|
2018-02-28 10:07:54 -07:00
|
|
|
extern crate silk;
|
|
|
|
|
2018-03-04 07:28:51 -07:00
|
|
|
use silk::accountant_stub::AccountantStub;
|
2018-03-06 12:26:39 -07:00
|
|
|
use silk::event::Event;
|
2018-03-06 12:48:26 -07:00
|
|
|
use silk::signature::{generate_keypair, get_pubkey};
|
|
|
|
use silk::transaction::{sign_transaction_data, Transaction};
|
2018-03-04 07:28:51 -07:00
|
|
|
use silk::genesis::Genesis;
|
|
|
|
use std::time::Instant;
|
|
|
|
use std::net::UdpSocket;
|
2018-03-05 15:34:15 -07:00
|
|
|
use std::io::stdin;
|
2018-03-04 01:21:40 -07:00
|
|
|
|
2018-02-28 10:07:54 -07:00
|
|
|
fn main() {
|
2018-02-28 14:16:50 -07:00
|
|
|
let addr = "127.0.0.1:8000";
|
2018-03-01 12:23:27 -07:00
|
|
|
let send_addr = "127.0.0.1:8001";
|
2018-03-03 21:15:42 -07:00
|
|
|
|
2018-03-05 15:34:15 -07:00
|
|
|
let gen: Genesis = serde_json::from_reader(stdin()).unwrap();
|
|
|
|
let alice_keypair = gen.get_keypair();
|
|
|
|
let alice_pubkey = gen.get_pubkey();
|
2018-02-28 10:07:54 -07:00
|
|
|
|
2018-03-03 21:15:42 -07:00
|
|
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
2018-03-05 11:11:00 -07:00
|
|
|
let mut acc = AccountantStub::new(addr, socket);
|
2018-03-05 12:48:09 -07:00
|
|
|
let last_id = acc.get_last_id().unwrap();
|
2018-03-05 15:34:15 -07:00
|
|
|
|
|
|
|
let txs = acc.get_balance(&alice_pubkey).unwrap().unwrap();
|
|
|
|
println!("Alice's Initial Balance {}", txs);
|
|
|
|
|
2018-03-03 10:23:31 -07:00
|
|
|
let one = 1;
|
|
|
|
println!("Signing transactions...");
|
|
|
|
let now = Instant::now();
|
2018-03-03 11:45:21 -07:00
|
|
|
let sigs: Vec<(_, _)> = (0..txs)
|
|
|
|
.map(|_| {
|
|
|
|
let rando_keypair = generate_keypair();
|
|
|
|
let rando_pubkey = get_pubkey(&rando_keypair);
|
2018-03-05 12:48:09 -07:00
|
|
|
let sig = sign_transaction_data(&one, &alice_keypair, &rando_pubkey, &last_id);
|
2018-03-03 11:45:21 -07:00
|
|
|
(rando_pubkey, sig)
|
|
|
|
})
|
|
|
|
.collect();
|
2018-03-03 10:23:31 -07: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 11:52:46 -07:00
|
|
|
"Done. {} thousand signatures per second, {}us per signature",
|
|
|
|
bsps * 1_000_000_f64,
|
|
|
|
nsps / 1_000_f64
|
2018-03-03 10:23:31 -07:00
|
|
|
);
|
|
|
|
|
2018-03-03 11:59:34 -07:00
|
|
|
println!("Verify signatures...");
|
|
|
|
let now = Instant::now();
|
|
|
|
for &(k, s) in &sigs {
|
2018-03-06 11:54:45 -07:00
|
|
|
let e = Event::Transaction(Transaction {
|
2018-03-03 20:41:05 -07:00
|
|
|
from: alice_pubkey,
|
2018-03-03 11:59:34 -07:00
|
|
|
to: k,
|
2018-03-06 14:37:08 -07:00
|
|
|
asset: one,
|
2018-03-05 12:48:09 -07:00
|
|
|
last_id,
|
2018-03-03 11:59:34 -07:00
|
|
|
sig: s,
|
2018-03-06 10:49:40 -07:00
|
|
|
});
|
2018-03-06 12:26:39 -07:00
|
|
|
assert!(e.verify());
|
2018-03-03 11:59:34 -07:00
|
|
|
}
|
|
|
|
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 19:33:28 -07:00
|
|
|
println!("Transferring 1 unit {} times...", txs);
|
2018-03-01 12:23:27 -07:00
|
|
|
let now = Instant::now();
|
2018-03-03 21:15:42 -07:00
|
|
|
let mut sig = Default::default();
|
2018-03-03 10:23:31 -07:00
|
|
|
for (k, s) in sigs {
|
2018-03-05 12:48:09 -07:00
|
|
|
acc.transfer_signed(alice_pubkey, k, one, last_id, s)
|
|
|
|
.unwrap();
|
2018-03-03 10:23:31 -07:00
|
|
|
sig = s;
|
2018-02-28 19:33:28 -07:00
|
|
|
}
|
2018-03-01 12:23:27 -07:00
|
|
|
println!("Waiting for last transaction to be confirmed...",);
|
|
|
|
acc.wait_on_signature(&sig).unwrap();
|
2018-02-28 10:07:54 -07:00
|
|
|
|
2018-03-01 12:23:27 -07: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-05 15:34:15 -07:00
|
|
|
let val = acc.get_balance(&alice_pubkey).unwrap().unwrap();
|
2018-03-01 14:07:23 -07:00
|
|
|
println!("Alice's Final Balance {}", val);
|
|
|
|
assert_eq!(val, 0);
|
2018-02-28 10:07:54 -07:00
|
|
|
}
|