2023-01-09 22:40:43 -08:00
|
|
|
use std::time::Duration;
|
2022-11-12 05:32:01 -08:00
|
|
|
|
2023-01-31 09:02:50 -08:00
|
|
|
use anyhow::bail;
|
2022-11-30 07:56:41 -08:00
|
|
|
use clap::Parser;
|
2023-02-13 10:58:30 -08:00
|
|
|
use dotenv::dotenv;
|
2022-12-21 05:31:43 -08:00
|
|
|
use lite_rpc::{bridge::LiteBridge, cli::Args};
|
2023-01-31 09:08:10 -08:00
|
|
|
use log::info;
|
2023-02-08 13:18:09 -08:00
|
|
|
use solana_sdk::signature::Keypair;
|
2023-02-13 10:58:30 -08:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
async fn get_identity_keypair(identity_from_cli: &String) -> Keypair {
|
|
|
|
if let Some(identity_env_var) = env::var("IDENTITY").ok() {
|
|
|
|
if let Ok(identity_bytes) = serde_json::from_str::<Vec<u8>>(identity_env_var.as_str()) {
|
|
|
|
Keypair::from_bytes(identity_bytes.as_slice()).unwrap()
|
|
|
|
} else {
|
|
|
|
// must be a file
|
|
|
|
let identity_file = tokio::fs::read_to_string(identity_env_var.as_str())
|
|
|
|
.await
|
|
|
|
.expect("Cannot find the identity file provided");
|
|
|
|
let identity_bytes: Vec<u8> = serde_json::from_str(&identity_file).unwrap();
|
|
|
|
Keypair::from_bytes(identity_bytes.as_slice()).unwrap()
|
|
|
|
}
|
|
|
|
} else if identity_from_cli.is_empty() {
|
|
|
|
Keypair::new()
|
|
|
|
} else {
|
|
|
|
let identity_file = tokio::fs::read_to_string(identity_from_cli.as_str())
|
|
|
|
.await
|
|
|
|
.expect("Cannot find the identity file provided");
|
|
|
|
let identity_bytes: Vec<u8> = serde_json::from_str(&identity_file).unwrap();
|
|
|
|
Keypair::from_bytes(identity_bytes.as_slice()).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 18:35:49 -08:00
|
|
|
|
2023-02-13 23:11:18 -08:00
|
|
|
#[tokio::main(flavor = "multi_thread", worker_threads = 16)]
|
2022-12-16 18:35:49 -08:00
|
|
|
pub async fn main() -> anyhow::Result<()> {
|
2023-01-11 22:27:38 -08:00
|
|
|
tracing_subscriber::fmt::init();
|
2022-12-16 18:35:49 -08:00
|
|
|
|
|
|
|
let Args {
|
|
|
|
rpc_addr,
|
|
|
|
ws_addr,
|
2023-01-05 02:54:00 -08:00
|
|
|
lite_rpc_ws_addr,
|
|
|
|
lite_rpc_http_addr,
|
2023-01-10 06:56:41 -08:00
|
|
|
clean_interval_ms,
|
2023-01-10 07:45:30 -08:00
|
|
|
fanout_size,
|
2023-01-31 05:30:52 -08:00
|
|
|
enable_postgres,
|
2023-02-04 03:45:20 -08:00
|
|
|
prometheus_addr,
|
2023-02-08 13:18:09 -08:00
|
|
|
identity_keypair,
|
2022-12-16 18:35:49 -08:00
|
|
|
} = Args::parse();
|
|
|
|
|
2023-02-13 10:58:30 -08:00
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
let identity = get_identity_keypair(&identity_keypair).await;
|
2023-02-08 13:18:09 -08:00
|
|
|
|
2023-01-10 06:56:41 -08:00
|
|
|
let clean_interval_ms = Duration::from_millis(clean_interval_ms);
|
2023-01-09 22:40:43 -08:00
|
|
|
|
2023-02-08 13:18:09 -08:00
|
|
|
let light_bridge = LiteBridge::new(rpc_addr, ws_addr, fanout_size, identity).await?;
|
2022-12-16 18:35:49 -08:00
|
|
|
|
2023-01-05 02:54:00 -08:00
|
|
|
let services = light_bridge
|
2023-01-09 22:40:43 -08:00
|
|
|
.start_services(
|
|
|
|
lite_rpc_http_addr,
|
|
|
|
lite_rpc_ws_addr,
|
2023-01-10 06:56:41 -08:00
|
|
|
clean_interval_ms,
|
2023-01-31 05:30:52 -08:00
|
|
|
enable_postgres,
|
2023-02-04 03:45:20 -08:00
|
|
|
prometheus_addr,
|
2023-01-09 22:40:43 -08:00
|
|
|
)
|
2023-01-05 02:54:00 -08:00
|
|
|
.await?;
|
2023-01-09 22:40:43 -08:00
|
|
|
|
2022-12-21 05:31:43 -08:00
|
|
|
let services = futures::future::try_join_all(services);
|
2022-12-16 18:35:49 -08:00
|
|
|
|
|
|
|
let ctrl_c_signal = tokio::signal::ctrl_c();
|
|
|
|
|
|
|
|
tokio::select! {
|
2023-01-31 09:02:50 -08:00
|
|
|
_ = services => {
|
2023-01-31 09:08:28 -08:00
|
|
|
bail!("Services quit unexpectedly");
|
2023-01-31 09:02:50 -08:00
|
|
|
}
|
|
|
|
_ = ctrl_c_signal => {
|
2023-01-31 09:08:10 -08:00
|
|
|
info!("Received ctrl+c signal");
|
2023-01-31 09:02:50 -08:00
|
|
|
Ok(())
|
2022-12-16 18:35:49 -08:00
|
|
|
}
|
2022-11-25 01:22:12 -08:00
|
|
|
}
|
2022-12-07 07:05:18 -08:00
|
|
|
}
|