2023-02-15 08:08:55 -08:00
|
|
|
use log::{error, info};
|
2022-09-02 07:26:44 -07:00
|
|
|
use serde_json;
|
|
|
|
use solana_bench_mango::{
|
|
|
|
cli,
|
2023-02-15 08:08:55 -08:00
|
|
|
confirmation_strategies::confirmations_by_blocks,
|
|
|
|
helpers::{
|
|
|
|
get_latest_blockhash, get_mango_market_perps_cache, start_blockhash_polling_service,
|
|
|
|
write_block_data_into_csv, write_transaction_data_into_csv,
|
|
|
|
},
|
2022-09-02 07:26:44 -07:00
|
|
|
mango::{AccountKeys, MangoConfig},
|
2023-02-15 08:08:55 -08:00
|
|
|
market_markers::start_market_making_threads,
|
|
|
|
rotating_queue::RotatingQueue,
|
2023-03-06 00:37:42 -08:00
|
|
|
states::{BlockData, PerpMarketCache, TransactionConfirmRecord, TransactionSendRecord}, crank, account_write_filter,
|
2022-09-02 07:26:44 -07:00
|
|
|
};
|
|
|
|
use solana_client::{
|
2023-02-17 04:01:18 -08:00
|
|
|
rpc_client::RpcClient, tpu_client::TpuClient, connection_cache::ConnectionCache,
|
2022-09-02 07:26:44 -07:00
|
|
|
};
|
2023-02-17 04:01:18 -08:00
|
|
|
use solana_quic_client::{QuicPool, QuicConnectionManager, QuicConfig};
|
2023-02-15 08:08:55 -08:00
|
|
|
use solana_sdk::commitment_config::CommitmentConfig;
|
2022-11-22 14:09:46 -08:00
|
|
|
|
2022-09-02 07:26:44 -07:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
sync::{
|
2022-09-08 12:52:18 -07:00
|
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
2022-09-02 07:26:44 -07:00
|
|
|
Arc, RwLock,
|
|
|
|
},
|
2023-02-17 04:01:18 -08:00
|
|
|
thread::{Builder, JoinHandle}, net::{IpAddr, Ipv4Addr},
|
2022-09-02 07:26:44 -07:00
|
|
|
};
|
|
|
|
|
2023-03-06 00:37:42 -08:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-09-02 07:26:44 -07:00
|
|
|
solana_logger::setup_with_default("solana=info");
|
|
|
|
solana_metrics::set_panic_hook("bench-mango", /*version:*/ None);
|
|
|
|
|
|
|
|
let matches = cli::build_args(solana_version::version!()).get_matches();
|
|
|
|
let cli_config = cli::extract_args(&matches);
|
|
|
|
|
|
|
|
let cli::Config {
|
|
|
|
json_rpc_url,
|
|
|
|
websocket_url,
|
|
|
|
id,
|
|
|
|
account_keys,
|
|
|
|
mango_keys,
|
|
|
|
duration,
|
|
|
|
quotes_per_second,
|
|
|
|
transaction_save_file,
|
2022-09-14 06:48:33 -07:00
|
|
|
block_data_save_file,
|
2022-10-01 05:51:46 -07:00
|
|
|
mango_cluster,
|
2022-11-14 09:46:04 -08:00
|
|
|
txs_batch_size,
|
2022-09-02 07:26:44 -07:00
|
|
|
..
|
|
|
|
} = &cli_config;
|
|
|
|
|
2022-09-08 12:52:18 -07:00
|
|
|
let transaction_save_file = transaction_save_file.clone();
|
2022-09-14 06:48:33 -07:00
|
|
|
let block_data_save_file = block_data_save_file.clone();
|
2022-09-08 12:52:18 -07:00
|
|
|
|
2022-09-02 07:26:44 -07:00
|
|
|
info!("Connecting to the cluster");
|
|
|
|
|
|
|
|
let account_keys_json = fs::read_to_string(account_keys).expect("unable to read accounts file");
|
|
|
|
let account_keys_parsed: Vec<AccountKeys> =
|
|
|
|
serde_json::from_str(&account_keys_json).expect("accounts JSON was not well-formatted");
|
|
|
|
|
|
|
|
let mango_keys_json = fs::read_to_string(mango_keys).expect("unable to read mango keys file");
|
|
|
|
let mango_keys_parsed: MangoConfig =
|
|
|
|
serde_json::from_str(&mango_keys_json).expect("mango JSON was not well-formatted");
|
|
|
|
|
2022-10-01 05:51:46 -07:00
|
|
|
let mango_group_id = mango_cluster;
|
2022-09-02 07:26:44 -07:00
|
|
|
let mango_group_config = mango_keys_parsed
|
|
|
|
.groups
|
|
|
|
.iter()
|
2022-10-01 05:51:46 -07:00
|
|
|
.find(|g| g.name == *mango_group_id)
|
2022-09-02 07:26:44 -07:00
|
|
|
.unwrap();
|
|
|
|
|
2022-11-02 07:39:53 -07:00
|
|
|
let number_of_tpu_clients: usize = 1;
|
2022-09-08 12:52:18 -07:00
|
|
|
let rpc_clients = RotatingQueue::<Arc<RpcClient>>::new(number_of_tpu_clients, || {
|
|
|
|
Arc::new(RpcClient::new_with_commitment(
|
|
|
|
json_rpc_url.to_string(),
|
|
|
|
CommitmentConfig::confirmed(),
|
|
|
|
))
|
|
|
|
});
|
2022-09-02 07:26:44 -07:00
|
|
|
|
2023-02-17 04:01:18 -08:00
|
|
|
let connection_cache = ConnectionCache::new_with_client_options(
|
|
|
|
4,
|
|
|
|
None,
|
|
|
|
Some((id, IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let quic_connection_cache = if let ConnectionCache::Quic(connection_cache) = connection_cache {
|
|
|
|
Some(connection_cache)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let tpu_client_pool = Arc::new(RotatingQueue::<Arc<TpuClient<QuicPool, QuicConnectionManager, QuicConfig>>>::new(
|
2022-09-02 07:26:44 -07:00
|
|
|
number_of_tpu_clients,
|
|
|
|
|| {
|
2023-02-17 04:01:18 -08:00
|
|
|
let quic_connection_cache = quic_connection_cache.clone();
|
2022-09-02 07:26:44 -07:00
|
|
|
Arc::new(
|
|
|
|
TpuClient::new_with_connection_cache(
|
2022-09-07 11:58:07 -07:00
|
|
|
rpc_clients.get().clone(),
|
2022-09-02 07:26:44 -07:00
|
|
|
&websocket_url,
|
|
|
|
solana_client::tpu_client::TpuClientConfig::default(),
|
2023-02-17 04:01:18 -08:00
|
|
|
quic_connection_cache.unwrap(),
|
2022-09-02 07:26:44 -07:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"accounts:{:?} markets:{:?} quotes_per_second:{:?} expected_tps:{:?} duration:{:?}",
|
|
|
|
account_keys_parsed.len(),
|
|
|
|
mango_group_config.perp_markets.len(),
|
|
|
|
quotes_per_second,
|
|
|
|
account_keys_parsed.len()
|
|
|
|
* mango_group_config.perp_markets.len()
|
|
|
|
* quotes_per_second.clone() as usize,
|
|
|
|
duration
|
|
|
|
);
|
|
|
|
|
|
|
|
// continuosly fetch blockhash
|
|
|
|
let rpc_client = Arc::new(RpcClient::new_with_commitment(
|
|
|
|
json_rpc_url.to_string(),
|
|
|
|
CommitmentConfig::confirmed(),
|
|
|
|
));
|
|
|
|
let exit_signal = Arc::new(AtomicBool::new(false));
|
|
|
|
let blockhash = Arc::new(RwLock::new(get_latest_blockhash(&rpc_client.clone())));
|
2022-09-04 10:27:30 -07:00
|
|
|
let current_slot = Arc::new(AtomicU64::new(0));
|
2023-02-15 08:08:55 -08:00
|
|
|
let blockhash_thread = start_blockhash_polling_service(
|
|
|
|
exit_signal.clone(),
|
|
|
|
blockhash.clone(),
|
|
|
|
current_slot.clone(),
|
|
|
|
rpc_client.clone(),
|
|
|
|
);
|
2022-09-02 07:26:44 -07:00
|
|
|
|
2023-02-15 08:08:55 -08:00
|
|
|
let perp_market_caches: Vec<PerpMarketCache> =
|
2023-03-06 00:37:42 -08:00
|
|
|
get_mango_market_perps_cache(rpc_client.clone(), mango_group_config);
|
2023-02-15 08:08:55 -08:00
|
|
|
|
|
|
|
let (tx_record_sx, tx_record_rx) = crossbeam_channel::unbounded();
|
|
|
|
|
2023-03-06 00:37:42 -08:00
|
|
|
crank::start(
|
|
|
|
tx_record_sx.clone(),
|
|
|
|
exit_signal.clone(),
|
|
|
|
blockhash.clone(),
|
|
|
|
current_slot.clone(),
|
|
|
|
tpu_client_pool.clone(),
|
|
|
|
mango_group_config,
|
|
|
|
id
|
|
|
|
);
|
|
|
|
|
2023-02-15 08:08:55 -08:00
|
|
|
let mm_threads: Vec<JoinHandle<()>> = start_market_making_threads(
|
|
|
|
account_keys_parsed.clone(),
|
|
|
|
perp_market_caches.clone(),
|
2023-03-06 00:37:42 -08:00
|
|
|
tx_record_sx.clone(),
|
2023-02-15 08:08:55 -08:00
|
|
|
exit_signal.clone(),
|
|
|
|
blockhash.clone(),
|
|
|
|
current_slot.clone(),
|
|
|
|
tpu_client_pool.clone(),
|
|
|
|
&duration,
|
|
|
|
*quotes_per_second,
|
|
|
|
*txs_batch_size,
|
|
|
|
);
|
2023-03-06 00:37:42 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-02 07:26:44 -07:00
|
|
|
let duration = duration.clone();
|
|
|
|
let quotes_per_second = quotes_per_second.clone();
|
|
|
|
let account_keys_parsed = account_keys_parsed.clone();
|
|
|
|
let tx_confirm_records: Arc<RwLock<Vec<TransactionConfirmRecord>>> =
|
|
|
|
Arc::new(RwLock::new(Vec::new()));
|
|
|
|
let tx_timeout_records: Arc<RwLock<Vec<TransactionSendRecord>>> =
|
|
|
|
Arc::new(RwLock::new(Vec::new()));
|
|
|
|
|
2022-09-08 12:52:18 -07:00
|
|
|
let tx_block_data = Arc::new(RwLock::new(Vec::<BlockData>::new()));
|
|
|
|
|
2022-09-02 07:26:44 -07:00
|
|
|
let confirmation_thread = Builder::new()
|
|
|
|
.name("solana-client-sender".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
let recv_limit = account_keys_parsed.len()
|
2022-09-08 12:52:18 -07:00
|
|
|
* perp_market_caches.len()
|
|
|
|
* duration.as_secs() as usize
|
|
|
|
* quotes_per_second as usize;
|
2022-09-02 07:26:44 -07:00
|
|
|
|
2022-09-07 11:58:07 -07:00
|
|
|
//confirmation_by_querying_rpc(recv_limit, rpc_client.clone(), &tx_record_rx, tx_confirm_records.clone(), tx_timeout_records.clone());
|
2022-09-08 12:52:18 -07:00
|
|
|
confirmations_by_blocks(
|
|
|
|
rpc_clients,
|
|
|
|
¤t_slot,
|
|
|
|
recv_limit,
|
|
|
|
tx_record_rx,
|
|
|
|
tx_confirm_records.clone(),
|
|
|
|
tx_timeout_records.clone(),
|
|
|
|
tx_block_data.clone(),
|
|
|
|
);
|
2022-09-02 07:26:44 -07:00
|
|
|
|
|
|
|
let confirmed: Vec<TransactionConfirmRecord> = {
|
|
|
|
let lock = tx_confirm_records.write().unwrap();
|
|
|
|
(*lock).clone()
|
|
|
|
};
|
|
|
|
let total_signed = account_keys_parsed.len()
|
|
|
|
* perp_market_caches.len()
|
|
|
|
* duration.as_secs() as usize
|
|
|
|
* quotes_per_second as usize;
|
|
|
|
info!(
|
|
|
|
"confirmed {} signatures of {} rate {}%",
|
|
|
|
confirmed.len(),
|
|
|
|
total_signed,
|
|
|
|
(confirmed.len() * 100) / total_signed
|
|
|
|
);
|
2022-09-09 02:19:40 -07:00
|
|
|
let error_count = confirmed.iter().filter(|tx| !tx.error.is_empty()).count();
|
2022-09-02 07:26:44 -07:00
|
|
|
info!(
|
|
|
|
"errors counted {} rate {}%",
|
|
|
|
error_count,
|
|
|
|
(error_count as usize * 100) / total_signed
|
|
|
|
);
|
|
|
|
let timeouts: Vec<TransactionSendRecord> = {
|
|
|
|
let timeouts = tx_timeout_records.clone();
|
|
|
|
let lock = timeouts.write().unwrap();
|
|
|
|
(*lock).clone()
|
|
|
|
};
|
|
|
|
info!(
|
|
|
|
"timeouts counted {} rate {}%",
|
|
|
|
timeouts.len(),
|
|
|
|
(timeouts.len() * 100) / total_signed
|
|
|
|
);
|
|
|
|
|
2023-02-15 08:08:55 -08:00
|
|
|
// let mut confirmation_times = confirmed
|
|
|
|
// .iter()
|
|
|
|
// .map(|r| {
|
|
|
|
// r.confirmed_at
|
|
|
|
// .signed_duration_since(r.sent_at)
|
|
|
|
// .num_milliseconds()
|
|
|
|
// })
|
|
|
|
// .collect::<Vec<_>>();
|
|
|
|
// confirmation_times.sort();
|
|
|
|
// info!(
|
|
|
|
// "confirmation times min={} max={} median={}",
|
|
|
|
// confirmation_times.first().unwrap(),
|
|
|
|
// confirmation_times.last().unwrap(),
|
|
|
|
// confirmation_times[confirmation_times.len() / 2]
|
|
|
|
// );
|
2022-09-08 12:52:18 -07:00
|
|
|
|
|
|
|
write_transaction_data_into_csv(
|
|
|
|
transaction_save_file,
|
|
|
|
tx_confirm_records,
|
|
|
|
tx_timeout_records,
|
|
|
|
);
|
2022-09-14 06:48:33 -07:00
|
|
|
|
2022-11-02 07:39:26 -07:00
|
|
|
write_block_data_into_csv(block_data_save_file, tx_block_data);
|
2022-09-02 07:26:44 -07:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for t in mm_threads {
|
|
|
|
if let Err(err) = t.join() {
|
|
|
|
error!("mm join failed with: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("joined all mm_threads");
|
|
|
|
|
|
|
|
if let Err(err) = confirmation_thread.join() {
|
|
|
|
error!("confirmation join fialed with: {:?}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("joined confirmation thread");
|
|
|
|
|
|
|
|
exit_signal.store(true, Ordering::Relaxed);
|
|
|
|
|
|
|
|
if let Err(err) = blockhash_thread.join() {
|
|
|
|
error!("blockhash join failed with: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|