2023-02-23 02:02:57 -08:00
|
|
|
use {
|
2023-03-14 05:39:19 -07:00
|
|
|
log::info,
|
2023-02-23 02:02:57 -08:00
|
|
|
serde_json,
|
2023-03-13 14:14:17 -07:00
|
|
|
mango_simulation::{
|
2023-02-23 02:02:57 -08:00
|
|
|
cli,
|
|
|
|
confirmation_strategies::confirmations_by_blocks,
|
2023-03-06 02:05:25 -08:00
|
|
|
crank,
|
2023-02-23 02:02:57 -08:00
|
|
|
helpers::{
|
|
|
|
get_latest_blockhash, get_mango_market_perps_cache, start_blockhash_polling_service,
|
2023-03-14 05:39:19 -07:00
|
|
|
to_sdk_pk,
|
2023-02-23 02:02:57 -08:00
|
|
|
},
|
|
|
|
keeper::start_keepers,
|
|
|
|
mango::{AccountKeys, MangoConfig},
|
|
|
|
market_markers::start_market_making_threads,
|
2023-03-14 05:39:19 -07:00
|
|
|
result_writer::initialize_result_writers,
|
|
|
|
states::PerpMarketCache,
|
|
|
|
stats::MangoSimulationStats,
|
2023-03-10 04:38:11 -08:00
|
|
|
websocket_source::KeeperConfig,
|
2023-02-15 08:08:55 -08:00
|
|
|
},
|
2023-02-23 02:02:57 -08:00
|
|
|
solana_client::{
|
2023-03-14 05:39:19 -07:00
|
|
|
connection_cache::ConnectionCache, nonblocking::rpc_client::RpcClient as NbRpcClient,
|
|
|
|
rpc_client::RpcClient, tpu_client::TpuClient,
|
2022-09-02 07:26:44 -07:00
|
|
|
},
|
2023-02-24 06:12:14 -08:00
|
|
|
solana_program::pubkey::Pubkey,
|
2023-02-23 02:02:57 -08:00
|
|
|
solana_sdk::commitment_config::CommitmentConfig,
|
|
|
|
std::{
|
|
|
|
fs,
|
|
|
|
net::{IpAddr, Ipv4Addr},
|
2023-02-24 06:12:14 -08:00
|
|
|
str::FromStr,
|
2023-03-14 05:39:19 -07:00
|
|
|
sync::atomic::{AtomicBool, AtomicU64, Ordering},
|
|
|
|
sync::Arc,
|
2023-03-09 07:20:49 -08:00
|
|
|
time::Duration,
|
|
|
|
},
|
2023-03-14 05:39:19 -07:00
|
|
|
tokio::{sync::RwLock, task::JoinHandle},
|
2022-09-02 07:26:44 -07:00
|
|
|
};
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
|
|
|
|
pub async fn main() -> anyhow::Result<()> {
|
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,
|
2023-02-21 02:04:02 -08:00
|
|
|
priority_fees_proba,
|
2023-02-23 02:02:57 -08:00
|
|
|
keeper_authority,
|
2023-02-24 06:48:08 -08:00
|
|
|
number_of_markers_per_mm,
|
2022-09-02 07:26:44 -07:00
|
|
|
..
|
|
|
|
} = &cli_config;
|
2023-02-24 06:48:08 -08:00
|
|
|
let number_of_markers_per_mm = *number_of_markers_per_mm;
|
2022-09-02 07:26:44 -07:00
|
|
|
|
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
|
|
|
|
2023-02-21 00:49:20 -08:00
|
|
|
info!(
|
|
|
|
"Connecting to the cluster {}, {}",
|
|
|
|
json_rpc_url, websocket_url
|
|
|
|
);
|
2022-09-02 07:26:44 -07:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2023-02-21 00:49:20 -08:00
|
|
|
let rpc_client = Arc::new(RpcClient::new_with_commitment(
|
|
|
|
json_rpc_url.to_string(),
|
|
|
|
CommitmentConfig::confirmed(),
|
|
|
|
));
|
2022-09-02 07:26:44 -07:00
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let nb_rpc_client = Arc::new(NbRpcClient::new_with_commitment(
|
|
|
|
json_rpc_url.to_string(),
|
|
|
|
CommitmentConfig::confirmed(),
|
|
|
|
));
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2023-02-21 00:49:20 -08:00
|
|
|
let tpu_client = Arc::new(
|
|
|
|
TpuClient::new_with_connection_cache(
|
|
|
|
rpc_client.clone(),
|
|
|
|
&websocket_url,
|
|
|
|
solana_client::tpu_client::TpuClientConfig::default(),
|
|
|
|
quic_connection_cache.unwrap(),
|
2023-02-21 02:04:02 -08:00
|
|
|
)
|
2023-02-21 00:49:20 -08:00
|
|
|
.unwrap(),
|
|
|
|
);
|
2022-09-02 07:26:44 -07:00
|
|
|
|
|
|
|
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()
|
2023-03-14 05:39:19 -07:00
|
|
|
* number_of_markers_per_mm as usize
|
2022-09-02 07:26:44 -07:00
|
|
|
* quotes_per_second.clone() as usize,
|
|
|
|
duration
|
|
|
|
);
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let nb_users = account_keys_parsed.len();
|
|
|
|
|
|
|
|
let mango_sim_stats = MangoSimulationStats::new(
|
|
|
|
nb_users,
|
|
|
|
*quotes_per_second as usize,
|
|
|
|
number_of_markers_per_mm as usize,
|
|
|
|
duration.as_secs() as usize,
|
|
|
|
);
|
|
|
|
|
2022-09-02 07:26:44 -07:00
|
|
|
// 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));
|
2023-03-14 05:39:19 -07:00
|
|
|
let latest_blockhash = get_latest_blockhash(&rpc_client.clone()).await;
|
|
|
|
let blockhash = Arc::new(RwLock::new(latest_blockhash));
|
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(),
|
|
|
|
);
|
2023-03-10 04:38:11 -08:00
|
|
|
let mango_program_pk = Pubkey::from_str(mango_group_config.mango_program_id.as_str()).unwrap();
|
2023-02-15 08:08:55 -08:00
|
|
|
let perp_market_caches: Vec<PerpMarketCache> =
|
2023-03-10 04:38:11 -08:00
|
|
|
get_mango_market_perps_cache(rpc_client.clone(), mango_group_config, &mango_program_pk);
|
2023-02-15 08:08:55 -08:00
|
|
|
|
2023-02-24 06:48:08 -08:00
|
|
|
let quote_root_bank =
|
|
|
|
Pubkey::from_str(mango_group_config.tokens.last().unwrap().root_key.as_str()).unwrap();
|
|
|
|
let quote_node_banks = mango_group_config
|
|
|
|
.tokens
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
2023-02-24 06:12:14 -08:00
|
|
|
.node_keys
|
|
|
|
.iter()
|
|
|
|
.map(|x| Pubkey::from_str(x.as_str()).unwrap())
|
|
|
|
.collect();
|
2023-02-23 02:02:57 -08:00
|
|
|
// start keeper if keeper authority is present
|
|
|
|
let keepers_jl = if let Some(keeper_authority) = keeper_authority {
|
|
|
|
let jl = start_keepers(
|
|
|
|
exit_signal.clone(),
|
|
|
|
tpu_client.clone(),
|
|
|
|
perp_market_caches.clone(),
|
|
|
|
blockhash.clone(),
|
|
|
|
keeper_authority,
|
2023-02-24 06:12:14 -08:00
|
|
|
quote_root_bank,
|
|
|
|
quote_node_banks,
|
2023-02-23 02:02:57 -08:00
|
|
|
);
|
|
|
|
Some(jl)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let (tx_record_sx, tx_record_rx) = tokio::sync::mpsc::unbounded_channel();
|
2023-02-23 05:41:12 -08:00
|
|
|
let from_slot = current_slot.load(Ordering::Relaxed);
|
2023-03-10 04:38:11 -08:00
|
|
|
let keeper_config = KeeperConfig {
|
|
|
|
program_id: to_sdk_pk(&mango_program_pk),
|
|
|
|
rpc_url: json_rpc_url.clone(),
|
|
|
|
websocket_url: websocket_url.clone(),
|
|
|
|
};
|
2023-02-15 08:08:55 -08:00
|
|
|
|
2023-03-06 00:37:42 -08:00
|
|
|
crank::start(
|
2023-03-10 04:38:11 -08:00
|
|
|
keeper_config,
|
2023-03-06 00:37:42 -08:00
|
|
|
exit_signal.clone(),
|
|
|
|
blockhash.clone(),
|
2023-03-06 02:05:25 -08:00
|
|
|
tpu_client.clone(),
|
2023-03-06 00:37:42 -08:00
|
|
|
mango_group_config,
|
2023-03-09 07:20:49 -08:00
|
|
|
id,
|
2023-03-06 00:37:42 -08:00
|
|
|
);
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let warmup_duration = Duration::from_secs(10);
|
|
|
|
info!("waiting for keepers to warmup for {warmup_duration:?}");
|
|
|
|
tokio::time::sleep(warmup_duration).await;
|
|
|
|
|
|
|
|
let mut tasks: Vec<JoinHandle<()>> = start_market_making_threads(
|
2023-02-15 08:08:55 -08:00
|
|
|
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(),
|
2023-02-21 00:49:20 -08:00
|
|
|
tpu_client.clone(),
|
2023-02-15 08:08:55 -08:00
|
|
|
&duration,
|
|
|
|
*quotes_per_second,
|
2023-02-21 02:04:02 -08:00
|
|
|
*priority_fees_proba,
|
2023-02-24 06:48:08 -08:00
|
|
|
number_of_markers_per_mm,
|
2023-02-15 08:08:55 -08:00
|
|
|
);
|
2023-03-06 00:37:42 -08:00
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
info!("Number of MM threads {}", tasks.len());
|
|
|
|
drop(tx_record_sx);
|
2023-03-03 08:52:33 -08:00
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
tasks.push(blockhash_thread);
|
|
|
|
if let Some(keepers_jl) = keepers_jl {
|
|
|
|
tasks.push(keepers_jl);
|
2022-09-02 07:26:44 -07:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let (tx_status_sx, tx_status_rx) = async_channel::unbounded();
|
|
|
|
let (block_status_sx, block_status_rx) = async_channel::unbounded();
|
2022-09-02 07:26:44 -07:00
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let mut writers_jh = initialize_result_writers(
|
|
|
|
transaction_save_file,
|
|
|
|
block_data_save_file,
|
|
|
|
tx_status_rx.clone(),
|
|
|
|
block_status_rx.clone(),
|
|
|
|
);
|
|
|
|
tasks.append(&mut writers_jh);
|
|
|
|
|
|
|
|
let stats_handle =
|
|
|
|
mango_sim_stats.update_from_tx_status_stream(tx_status_rx.clone(), exit_signal.clone());
|
|
|
|
tasks.push(stats_handle);
|
|
|
|
|
|
|
|
let mut confirmation_threads = confirmations_by_blocks(
|
|
|
|
nb_rpc_client,
|
|
|
|
tx_record_rx,
|
|
|
|
tx_status_sx,
|
|
|
|
block_status_sx,
|
|
|
|
from_slot,
|
|
|
|
);
|
|
|
|
tasks.append(&mut confirmation_threads);
|
2023-02-23 02:02:57 -08:00
|
|
|
|
2023-03-14 05:39:19 -07:00
|
|
|
let nb_tasks = tasks.len();
|
|
|
|
for i in 0..nb_tasks {
|
|
|
|
println!("waiting for {}", i);
|
|
|
|
let task = tasks.remove(0);
|
|
|
|
let _ = task.await;
|
2023-02-23 02:02:57 -08:00
|
|
|
}
|
2023-03-14 05:39:19 -07:00
|
|
|
mango_sim_stats.report("Mango simulation stats");
|
|
|
|
Ok(())
|
2022-09-02 07:26:44 -07:00
|
|
|
}
|