2018-04-24 10:15:08 -07:00
|
|
|
extern crate futures;
|
2018-04-09 11:28:54 -07:00
|
|
|
extern crate getopts;
|
2018-04-21 06:12:57 -07:00
|
|
|
extern crate isatty;
|
2018-03-28 15:51:18 -07:00
|
|
|
extern crate rayon;
|
2018-03-05 14:34:15 -08:00
|
|
|
extern crate serde_json;
|
2018-03-27 15:24:05 -07:00
|
|
|
extern crate solana;
|
2018-02-28 09:07:54 -08:00
|
|
|
|
2018-04-24 10:15:08 -07:00
|
|
|
use futures::Future;
|
2018-04-11 19:24:14 -07:00
|
|
|
use getopts::Options;
|
2018-04-21 06:12:57 -07:00
|
|
|
use isatty::stdin_isatty;
|
2018-04-02 20:15:21 -07:00
|
|
|
use rayon::prelude::*;
|
2018-03-27 15:24:05 -07:00
|
|
|
use solana::accountant_stub::AccountantStub;
|
|
|
|
use solana::mint::Mint;
|
|
|
|
use solana::signature::{KeyPair, KeyPairUtil};
|
|
|
|
use solana::transaction::Transaction;
|
2018-04-11 19:24:14 -07:00
|
|
|
use std::env;
|
2018-04-21 06:12:57 -07:00
|
|
|
use std::io::{stdin, Read};
|
2018-03-28 19:13:10 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-04-19 07:06:19 -07:00
|
|
|
use std::process::exit;
|
2018-03-29 12:41:09 -07:00
|
|
|
use std::thread::sleep;
|
2018-04-02 20:15:21 -07:00
|
|
|
use std::time::{Duration, Instant};
|
2018-03-04 00:21:40 -08:00
|
|
|
|
2018-04-19 10:16:20 -07:00
|
|
|
fn print_usage(program: &str, opts: Options) {
|
|
|
|
let mut brief = format!("Usage: cat <mint.json> | {} [options]\n\n", program);
|
|
|
|
brief += " Solana client demo creates a number of transactions and\n";
|
|
|
|
brief += " sends them to a target node.";
|
|
|
|
brief += " Takes json formatted mint file to stdin.";
|
|
|
|
|
|
|
|
print!("{}", opts.usage(&brief));
|
|
|
|
}
|
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
fn main() {
|
2018-04-09 11:28:54 -07:00
|
|
|
let mut threads = 4usize;
|
|
|
|
let mut addr: String = "127.0.0.1:8000".to_string();
|
|
|
|
let mut send_addr: String = "127.0.0.1:8001".to_string();
|
2018-03-03 20:15:42 -08:00
|
|
|
|
2018-04-09 11:28:54 -07:00
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optopt("s", "", "server address", "host:port");
|
|
|
|
opts.optopt("c", "", "client address", "host:port");
|
|
|
|
opts.optopt("t", "", "number of threads", "4");
|
2018-04-19 10:16:20 -07:00
|
|
|
opts.optflag("h", "help", "print help");
|
2018-04-09 11:28:54 -07:00
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
|
|
|
Ok(m) => m,
|
2018-04-19 07:06:19 -07:00
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-04-09 11:28:54 -07:00
|
|
|
};
|
|
|
|
|
2018-04-19 10:16:20 -07:00
|
|
|
if matches.opt_present("h") {
|
|
|
|
let program = args[0].clone();
|
|
|
|
print_usage(&program, opts);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-09 11:28:54 -07:00
|
|
|
if matches.opt_present("s") {
|
|
|
|
addr = matches.opt_str("s").unwrap();
|
|
|
|
}
|
|
|
|
if matches.opt_present("c") {
|
|
|
|
send_addr = matches.opt_str("c").unwrap();
|
|
|
|
}
|
|
|
|
if matches.opt_present("t") {
|
|
|
|
threads = matches.opt_str("t").unwrap().parse().expect("integer");
|
|
|
|
}
|
2018-04-21 06:12:57 -07:00
|
|
|
|
|
|
|
if stdin_isatty() {
|
|
|
|
eprintln!("nothing found on stdin, expected a json file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buffer = String::new();
|
|
|
|
let num_bytes = stdin().read_to_string(&mut buffer).unwrap();
|
|
|
|
if num_bytes == 0 {
|
|
|
|
eprintln!("empty file on stdin, expected a json file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mint: Mint = serde_json::from_str(&buffer).unwrap_or_else(|e| {
|
2018-04-19 07:55:47 -07:00
|
|
|
eprintln!("failed to parse json: {}", e);
|
|
|
|
exit(1);
|
|
|
|
});
|
2018-03-07 16:08:12 -08:00
|
|
|
let mint_keypair = mint.keypair();
|
|
|
|
let mint_pubkey = mint.pubkey();
|
2018-02-28 09:07:54 -08:00
|
|
|
|
2018-04-09 11:28:54 -07:00
|
|
|
let socket = UdpSocket::bind(&send_addr).unwrap();
|
|
|
|
println!("Stub new");
|
|
|
|
let acc = AccountantStub::new(&addr, socket);
|
|
|
|
println!("Get last id");
|
2018-04-24 10:15:08 -07:00
|
|
|
let last_id = acc.get_last_id().wait().unwrap();
|
2018-03-05 14:34:15 -08:00
|
|
|
|
2018-04-09 11:28:54 -07:00
|
|
|
println!("Get Balance");
|
2018-04-24 10:15:08 -07:00
|
|
|
let mint_balance = acc.get_balance(&mint_pubkey).wait().unwrap();
|
2018-03-28 13:40:58 -07:00
|
|
|
println!("Mint's Initial Balance {}", mint_balance);
|
2018-03-05 14:34:15 -08:00
|
|
|
|
2018-03-03 09:23:31 -08:00
|
|
|
println!("Signing transactions...");
|
2018-04-09 11:28:54 -07:00
|
|
|
let txs = 1_000_000;
|
2018-03-03 09:23:31 -08:00
|
|
|
let now = Instant::now();
|
2018-03-06 15:34:14 -08:00
|
|
|
let transactions: Vec<_> = (0..txs)
|
2018-03-28 15:51:18 -07:00
|
|
|
.into_par_iter()
|
2018-03-03 10:45:21 -08:00
|
|
|
.map(|_| {
|
2018-03-07 14:32:22 -08:00
|
|
|
let rando_pubkey = KeyPair::new().pubkey();
|
2018-03-07 16:08:12 -08:00
|
|
|
Transaction::new(&mint_keypair, rando_pubkey, 1, last_id)
|
2018-03-03 10:45:21 -08:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-03 09:23:31 -08:00
|
|
|
let duration = now.elapsed();
|
2018-04-09 11:28:54 -07:00
|
|
|
let ns = duration.as_secs() * 2_000_000_000 + u64::from(duration.subsec_nanos());
|
2018-04-03 08:55:33 -07:00
|
|
|
let bsps = f64::from(txs) / ns as f64;
|
|
|
|
let nsps = ns as f64 / f64::from(txs);
|
2018-03-03 09:23:31 -08:00
|
|
|
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-04-09 11:28:54 -07:00
|
|
|
println!("Transfering {} transactions in {} batches", txs, threads);
|
2018-03-01 11:23:27 -08:00
|
|
|
let now = Instant::now();
|
2018-04-09 11:28:54 -07:00
|
|
|
let sz = transactions.len() / threads;
|
|
|
|
let chunks: Vec<_> = transactions.chunks(sz).collect();
|
|
|
|
let _: Vec<_> = chunks
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|trs| {
|
|
|
|
println!("Transferring 1 unit {} times...", trs.len());
|
|
|
|
let send_addr = "0.0.0.0:0";
|
|
|
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
|
|
|
let acc = AccountantStub::new(&addr, socket);
|
|
|
|
for tr in trs {
|
|
|
|
acc.transfer_signed(tr.clone()).unwrap();
|
|
|
|
}
|
|
|
|
()
|
|
|
|
})
|
|
|
|
.collect();
|
2018-03-01 11:23:27 -08:00
|
|
|
println!("Waiting for last transaction to be confirmed...",);
|
2018-03-28 15:51:18 -07:00
|
|
|
let mut val = mint_balance;
|
2018-03-29 12:41:09 -07:00
|
|
|
let mut prev = 0;
|
|
|
|
while val != prev {
|
|
|
|
sleep(Duration::from_millis(20));
|
|
|
|
prev = val;
|
2018-04-24 10:15:08 -07:00
|
|
|
val = acc.get_balance(&mint_pubkey).wait().unwrap();
|
2018-03-28 13:40:58 -07:00
|
|
|
}
|
2018-03-29 12:41:09 -07:00
|
|
|
println!("Mint's Final Balance {}", val);
|
|
|
|
let txs = mint_balance - val;
|
|
|
|
println!("Successful transactions {}", txs);
|
2018-02-28 09:07:54 -08:00
|
|
|
|
2018-03-01 11:23:27 -08:00
|
|
|
let duration = now.elapsed();
|
2018-03-22 13:15:29 -07:00
|
|
|
let ns = duration.as_secs() * 1_000_000_000 + u64::from(duration.subsec_nanos());
|
2018-03-01 11:23:27 -08:00
|
|
|
let tps = (txs * 1_000_000_000) as f64 / ns as f64;
|
|
|
|
println!("Done. {} tps!", tps);
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|