Skip to content

Commit

Permalink
fix: custom ws url would crash rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
anoushk1234 committed Apr 20, 2023
1 parent 3044975 commit e4c72a6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
55 changes: 42 additions & 13 deletions tinydancer/src/rpc_wrapper/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
get_endpoint,
rpc_wrapper::{
block_store::{BlockInformation, BlockStore},
configs::{IsBlockHashValidConfig, SendTransactionConfig},
Expand All @@ -8,13 +9,18 @@ use crate::{
workers::{BlockListener, Cleaner, TxSender, WireTransaction},
},
sampler::{get_serialized, pull_and_verify_shreds, SHRED_CF},
tinydancer::Cluster,
ConfigSchema,
};
use colored::Colorize;
use hyper::Method;
use reqwest::header;
use serde::{self, Deserialize, Serialize};
use solana_client::rpc_response::RpcApiVersion;
use std::{
fs,
ops::{Deref, Sub},
path::Path,
str::FromStr,
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -260,13 +266,24 @@ impl LiteRpcServer for LiteBridge {
) = self.block_store.get_latest_block(commitment_config).await;

info!("glb {blockhash} {slot} {block_height}");

let sampled = pull_and_verify_shreds(
slot as usize,
String::from("http://0.0.0.0:8899"),
10 as usize,
)
.await;
let mut rpc_url = String::from("http://0.0.0.0:8899");
let home_path = std::env::var("HOME").unwrap();
let is_existing = home_path.clone() + "/.config/tinydancer/config.json";
let path = Path::new(&is_existing);
if path.exists() {
let file = fs::File::open(home_path.clone() + "/.config/tinydancer/config.json")
.expect("Error reading config in bridge");
let config: ConfigSchema = serde_json::from_reader(file).unwrap();
rpc_url = get_endpoint(config.cluster);
} else {
println!(
"{} {}",
"Initialise a config first using:".to_string().yellow(),
"tinydancer set config".to_string().green()
);
}
let sampled =
pull_and_verify_shreds(slot as usize, String::from(rpc_url), 10 as usize).await;

Ok(LiteResponse {
context: LiteRpcResponseContext {
Expand Down Expand Up @@ -345,12 +362,24 @@ impl LiteRpcServer for LiteBridge {
.get_latest_block_info(CommitmentConfig::finalized())
.await
.slot;
let sampled = pull_and_verify_shreds(
slot as usize,
String::from("http://0.0.0.0:8899"),
10 as usize,
)
.await;
let mut rpc_url = String::from("http://0.0.0.0:8899");
let home_path = std::env::var("HOME").unwrap();
let is_existing = home_path.clone() + "/.config/tinydancer/config.json";
let path = Path::new(&is_existing);
if path.exists() {
let file = fs::File::open(home_path.clone() + "/.config/tinydancer/config.json")
.expect("Error reading config in bridge");
let config: ConfigSchema = serde_json::from_reader(file).unwrap();
rpc_url = get_endpoint(config.cluster);
} else {
println!(
"{} {}",
"Initialise a config first using:".to_string().yellow(),
"tinydancer set config".to_string().green()
);
}
let sampled =
pull_and_verify_shreds(slot as usize, String::from(rpc_url), 10 as usize).await;
Ok(LiteResponse {
context: LiteRpcResponseContext {
slot,
Expand Down
28 changes: 6 additions & 22 deletions tinydancer/src/rpc_wrapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub mod tpu_manager;
pub mod workers;
// pub mod cli;
pub mod block_store;
use crate::convert_to_websocket;
use crate::rpc_wrapper::bridge::LiteBridge;
use crate::tinydancer::{ClientService, Cluster};
use crate::tinydancer::{endpoint, ClientService, Cluster};
use anyhow::bail;
use async_trait::async_trait;
use clap::Parser;
Expand Down Expand Up @@ -93,34 +94,17 @@ impl ClientService<TransactionServiceConfig> for TransactionService {
type ServiceError = tokio::io::Error;
fn new(config: TransactionServiceConfig) -> Self {
let transaction_handle = tokio::spawn(async {
// let Args {
// rpc_addr,
// ws_addr,
// tx_batch_size,
// lite_rpc_ws_addr,
// lite_rpc_http_addr,
// tx_batch_interval_ms,
// clean_interval_ms,
// fanout_size,
// identity_keypair,
// } = Args::parse();

dotenv().ok();
let rpc_client = RpcClient::new(DEFAULT_RPC_ADDR.to_string());
let rpc_url = endpoint(config.cluster);

//let identity = get_identity_keypair(&identity_keypair).await;
// let blockhash = rpc_client.get_latest_blockhash().await.unwrap();
let payer = Keypair::new();
let airdrop_sign = rpc_client
.request_airdrop(&payer.try_pubkey().unwrap(), 2000000000)
.await?;
info!("AIRDROP CONFIRMED:{}", airdrop_sign);

let tx_batch_interval_ms = Duration::from_millis(DEFAULT_TX_BATCH_INTERVAL_MS);
let clean_interval_ms = Duration::from_millis(DEFAULT_CLEAN_INTERVAL_MS);

let light_bridge = LiteBridge::new(
String::from(DEFAULT_RPC_ADDR),
String::from(DEFAULT_WS_ADDR),
String::from(rpc_url.clone()),
String::from(convert_to_websocket!(rpc_url)),
DEFAULT_FANOUT_SIZE,
payer,
config.db_instance,
Expand Down
6 changes: 3 additions & 3 deletions tinydancer/src/tinydancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use anyhow::anyhow;
use async_trait::async_trait;
use futures::{future::join_all, TryFutureExt};
use rand::seq::index::sample;
use serde::{Deserialize, Serialize};
use tiny_logger::logs::info;
// use log::info;
// use log4rs;
Expand Down Expand Up @@ -129,8 +130,7 @@ impl TinyDancer {
}
}

#[allow(dead_code)]
#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Cluster {
Mainnet,
Devnet,
Expand All @@ -144,7 +144,7 @@ pub fn endpoint(cluster: Cluster) -> String {
Cluster::Mainnet => String::from("https://api.mainnet-beta.solana.com"),
Cluster::Devnet => String::from("https://api.devnet.solana.com"),
Cluster::Localnet => String::from("http://0.0.0.0:8899"),
Cluster::Custom(cluster) => cluster,
Cluster::Custom(url) => url,
}
}
pub enum ClientStatus {
Expand Down

0 comments on commit e4c72a6

Please sign in to comment.