2018-08-22 07:57:07 -07:00
|
|
|
#![feature(test)]
|
2018-07-10 19:33:16 -07:00
|
|
|
extern crate bincode;
|
2018-09-26 05:52:13 -07:00
|
|
|
extern crate rand;
|
2018-07-10 19:33:16 -07:00
|
|
|
extern crate rayon;
|
|
|
|
extern crate solana;
|
2018-09-24 10:20:58 -07:00
|
|
|
extern crate solana_program_interface;
|
2018-08-22 07:57:07 -07:00
|
|
|
extern crate test;
|
2018-07-10 19:33:16 -07:00
|
|
|
|
2018-09-26 05:52:13 -07:00
|
|
|
use rand::{thread_rng, Rng};
|
2018-07-10 19:33:16 -07:00
|
|
|
use rayon::prelude::*;
|
2018-10-17 16:28:26 -07:00
|
|
|
use solana::bank::{Bank, MAX_ENTRY_IDS};
|
2018-09-26 05:52:13 -07:00
|
|
|
use solana::banking_stage::{BankingStage, NUM_THREADS};
|
2018-09-24 10:40:42 -07:00
|
|
|
use solana::entry::Entry;
|
2018-10-17 16:28:26 -07:00
|
|
|
use solana::hash::hash;
|
2018-07-10 19:33:16 -07:00
|
|
|
use solana::mint::Mint;
|
2018-09-24 10:20:58 -07:00
|
|
|
use solana::packet::to_packets_chunked;
|
2018-09-26 16:55:36 -07:00
|
|
|
use solana::signature::{KeypairUtil, Signature};
|
2018-09-26 17:21:43 -07:00
|
|
|
use solana::system_transaction::SystemTransaction;
|
2018-07-10 19:33:16 -07:00
|
|
|
use solana::transaction::Transaction;
|
2018-09-24 10:20:58 -07:00
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-07-10 19:33:16 -07:00
|
|
|
use std::iter;
|
|
|
|
use std::sync::mpsc::{channel, Receiver};
|
|
|
|
use std::sync::Arc;
|
2018-09-24 10:40:42 -07:00
|
|
|
use std::time::Duration;
|
2018-08-22 07:57:07 -07:00
|
|
|
use test::Bencher;
|
2018-07-10 19:33:16 -07:00
|
|
|
|
2018-09-24 10:40:42 -07:00
|
|
|
fn check_txs(receiver: &Receiver<Vec<Entry>>, ref_tx_count: usize) {
|
2018-07-10 19:33:16 -07:00
|
|
|
let mut total = 0;
|
2018-08-12 10:04:21 -07:00
|
|
|
loop {
|
2018-09-24 10:40:42 -07:00
|
|
|
let entries = receiver.recv_timeout(Duration::new(1, 0));
|
|
|
|
if let Ok(entries) = entries {
|
|
|
|
for entry in &entries {
|
|
|
|
total += entry.transactions.len();
|
2018-08-07 10:29:57 -07:00
|
|
|
}
|
2018-07-10 19:33:16 -07:00
|
|
|
} else {
|
2018-09-24 10:40:42 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if total >= ref_tx_count {
|
|
|
|
break;
|
2018-07-10 19:33:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(total, ref_tx_count);
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:57:07 -07:00
|
|
|
#[bench]
|
2018-07-10 19:33:16 -07:00
|
|
|
fn bench_banking_stage_multi_accounts(bencher: &mut Bencher) {
|
2018-09-26 05:52:13 -07:00
|
|
|
let txes = 1000 * NUM_THREADS;
|
2018-07-10 19:33:16 -07:00
|
|
|
let mint_total = 1_000_000_000_000;
|
|
|
|
let mint = Mint::new(mint_total);
|
|
|
|
|
|
|
|
let (verified_sender, verified_receiver) = channel();
|
2018-09-26 05:52:13 -07:00
|
|
|
let bank = Arc::new(Bank::new(&mint));
|
2018-09-26 17:21:43 -07:00
|
|
|
let dummy = Transaction::system_move(
|
|
|
|
&mint.keypair(),
|
|
|
|
mint.keypair().pubkey(),
|
|
|
|
1,
|
|
|
|
mint.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
2018-09-26 05:52:13 -07:00
|
|
|
let transactions: Vec<_> = (0..txes)
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|_| {
|
|
|
|
let mut new = dummy.clone();
|
|
|
|
let from: Vec<u8> = (0..64).map(|_| thread_rng().gen()).collect();
|
|
|
|
let to: Vec<u8> = (0..64).map(|_| thread_rng().gen()).collect();
|
|
|
|
let sig: Vec<u8> = (0..64).map(|_| thread_rng().gen()).collect();
|
2018-09-28 16:16:35 -07:00
|
|
|
new.account_keys[0] = Pubkey::new(&from[0..32]);
|
|
|
|
new.account_keys[1] = Pubkey::new(&to[0..32]);
|
2018-09-26 05:52:13 -07:00
|
|
|
new.signature = Signature::new(&sig[0..64]);
|
|
|
|
new
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect();
|
2018-09-26 05:52:13 -07:00
|
|
|
// fund all the accounts
|
|
|
|
transactions.iter().for_each(|tx| {
|
2018-09-26 17:21:43 -07:00
|
|
|
let fund = Transaction::system_move(
|
2018-09-26 05:52:13 -07:00
|
|
|
&mint.keypair(),
|
2018-09-28 16:16:35 -07:00
|
|
|
tx.account_keys[0],
|
2018-09-26 05:52:13 -07:00
|
|
|
mint_total / txes as i64,
|
|
|
|
mint.last_id(),
|
2018-09-26 17:21:43 -07:00
|
|
|
0,
|
2018-09-26 05:52:13 -07:00
|
|
|
);
|
|
|
|
assert!(bank.process_transaction(&fund).is_ok());
|
2018-07-10 19:33:16 -07:00
|
|
|
});
|
2018-09-26 05:52:13 -07:00
|
|
|
//sanity check, make sure all the transactions can execute sequentially
|
|
|
|
transactions.iter().for_each(|tx| {
|
|
|
|
let res = bank.process_transaction(&tx);
|
|
|
|
assert!(res.is_ok(), "sanity test transactions");
|
|
|
|
});
|
|
|
|
bank.clear_signatures();
|
|
|
|
//sanity check, make sure all the transactions can execute in parallel
|
|
|
|
let res = bank.process_transactions(&transactions);
|
|
|
|
for r in res {
|
|
|
|
assert!(r.is_ok(), "sanity parallel execution");
|
2018-07-10 19:33:16 -07:00
|
|
|
}
|
2018-09-26 05:52:13 -07:00
|
|
|
bank.clear_signatures();
|
2018-09-24 10:20:58 -07:00
|
|
|
let verified: Vec<_> = to_packets_chunked(&transactions.clone(), 192)
|
2018-09-26 05:52:13 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
2018-09-24 10:20:58 -07:00
|
|
|
let len = x.read().unwrap().packets.len();
|
2018-09-26 05:52:13 -07:00
|
|
|
(x, iter::repeat(1).take(len).collect())
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect();
|
2018-10-12 00:39:10 -07:00
|
|
|
let (_stage, signal_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
|
|
|
Default::default(),
|
|
|
|
&mint.last_id(),
|
2018-10-18 22:57:48 -07:00
|
|
|
0,
|
|
|
|
None,
|
2018-10-12 00:39:10 -07:00
|
|
|
);
|
2018-10-17 16:28:26 -07:00
|
|
|
|
|
|
|
let mut id = mint.last_id();
|
|
|
|
for _ in 0..MAX_ENTRY_IDS {
|
|
|
|
id = hash(&id.as_ref());
|
|
|
|
bank.register_entry_id(&id);
|
|
|
|
}
|
|
|
|
|
2018-07-10 19:33:16 -07:00
|
|
|
bencher.iter(move || {
|
2018-10-17 16:28:26 -07:00
|
|
|
// make sure the tx last id is still registered
|
|
|
|
if bank.count_valid_ids(&[mint.last_id()]).len() == 0 {
|
|
|
|
bank.register_entry_id(&mint.last_id());
|
|
|
|
}
|
2018-09-26 05:52:13 -07:00
|
|
|
for v in verified.chunks(verified.len() / NUM_THREADS) {
|
|
|
|
verified_sender.send(v.to_vec()).unwrap();
|
|
|
|
}
|
|
|
|
check_txs(&signal_receiver, txes);
|
|
|
|
bank.clear_signatures();
|
2018-07-10 19:33:16 -07:00
|
|
|
});
|
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_banking_stage_multi_programs(bencher: &mut Bencher) {
|
|
|
|
let progs = 5;
|
|
|
|
let txes = 1000 * NUM_THREADS;
|
|
|
|
let mint_total = 1_000_000_000_000;
|
|
|
|
let mint = Mint::new(mint_total);
|
|
|
|
|
|
|
|
let (verified_sender, verified_receiver) = channel();
|
|
|
|
let bank = Arc::new(Bank::new(&mint));
|
|
|
|
let dummy = Transaction::system_move(
|
|
|
|
&mint.keypair(),
|
|
|
|
mint.keypair().pubkey(),
|
|
|
|
1,
|
|
|
|
mint.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
let transactions: Vec<_> = (0..txes)
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|_| {
|
|
|
|
let mut new = dummy.clone();
|
|
|
|
let from: Vec<u8> = (0..32).map(|_| thread_rng().gen()).collect();
|
|
|
|
let sig: Vec<u8> = (0..64).map(|_| thread_rng().gen()).collect();
|
|
|
|
let to: Vec<u8> = (0..32).map(|_| thread_rng().gen()).collect();
|
|
|
|
new.account_keys[0] = Pubkey::new(&from[0..32]);
|
|
|
|
new.account_keys[1] = Pubkey::new(&to[0..32]);
|
|
|
|
let prog = new.instructions[0].clone();
|
|
|
|
for i in 1..progs {
|
|
|
|
//generate programs that spend to random keys
|
|
|
|
let to: Vec<u8> = (0..32).map(|_| thread_rng().gen()).collect();
|
|
|
|
let to_key = Pubkey::new(&to[0..32]);
|
|
|
|
new.account_keys.push(to_key);
|
|
|
|
assert_eq!(new.account_keys.len(), i + 2);
|
|
|
|
new.instructions.push(prog.clone());
|
|
|
|
assert_eq!(new.instructions.len(), i + 1);
|
|
|
|
new.instructions[i].accounts[1] = 1 + i as u8;
|
|
|
|
assert_eq!(new.key(i, 1), Some(&to_key));
|
|
|
|
assert_eq!(
|
|
|
|
new.account_keys[new.instructions[i].accounts[1] as usize],
|
|
|
|
to_key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert_eq!(new.instructions.len(), progs);
|
|
|
|
new.signature = Signature::new(&sig[0..64]);
|
|
|
|
new
|
|
|
|
}).collect();
|
|
|
|
transactions.iter().for_each(|tx| {
|
|
|
|
let fund = Transaction::system_move(
|
|
|
|
&mint.keypair(),
|
|
|
|
tx.account_keys[0],
|
|
|
|
mint_total / txes as i64,
|
|
|
|
mint.last_id(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
assert!(bank.process_transaction(&fund).is_ok());
|
|
|
|
});
|
|
|
|
//sanity check, make sure all the transactions can execute sequentially
|
|
|
|
transactions.iter().for_each(|tx| {
|
|
|
|
let res = bank.process_transaction(&tx);
|
|
|
|
assert!(res.is_ok(), "sanity test transactions");
|
|
|
|
});
|
|
|
|
bank.clear_signatures();
|
|
|
|
//sanity check, make sure all the transactions can execute in parallel
|
|
|
|
let res = bank.process_transactions(&transactions);
|
|
|
|
for r in res {
|
|
|
|
assert!(r.is_ok(), "sanity parallel execution");
|
|
|
|
}
|
|
|
|
bank.clear_signatures();
|
|
|
|
let verified: Vec<_> = to_packets_chunked(&transactions.clone(), 96)
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
let len = x.read().unwrap().packets.len();
|
|
|
|
(x, iter::repeat(1).take(len).collect())
|
|
|
|
}).collect();
|
2018-10-12 00:39:10 -07:00
|
|
|
let (_stage, signal_receiver) = BankingStage::new(
|
|
|
|
&bank,
|
|
|
|
verified_receiver,
|
|
|
|
Default::default(),
|
|
|
|
&mint.last_id(),
|
2018-10-18 22:57:48 -07:00
|
|
|
0,
|
|
|
|
None,
|
2018-10-12 00:39:10 -07:00
|
|
|
);
|
2018-10-17 16:28:26 -07:00
|
|
|
|
|
|
|
let mut id = mint.last_id();
|
|
|
|
for _ in 0..MAX_ENTRY_IDS {
|
|
|
|
id = hash(&id.as_ref());
|
|
|
|
bank.register_entry_id(&id);
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:16:35 -07:00
|
|
|
bencher.iter(move || {
|
2018-10-17 16:28:26 -07:00
|
|
|
// make sure the transactions are still valid
|
|
|
|
if bank.count_valid_ids(&[mint.last_id()]).len() == 0 {
|
|
|
|
bank.register_entry_id(&mint.last_id());
|
|
|
|
}
|
2018-09-28 16:16:35 -07:00
|
|
|
for v in verified.chunks(verified.len() / NUM_THREADS) {
|
|
|
|
verified_sender.send(v.to_vec()).unwrap();
|
|
|
|
}
|
|
|
|
check_txs(&signal_receiver, txes);
|
|
|
|
bank.clear_signatures();
|
|
|
|
});
|
|
|
|
}
|