Skip to content

Commit e4c72a6

Browse files
committed
fix: custom ws url would crash rpc
1 parent 3044975 commit e4c72a6

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

tinydancer/src/rpc_wrapper/bridge.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
get_endpoint,
23
rpc_wrapper::{
34
block_store::{BlockInformation, BlockStore},
45
configs::{IsBlockHashValidConfig, SendTransactionConfig},
@@ -8,13 +9,18 @@ use crate::{
89
workers::{BlockListener, Cleaner, TxSender, WireTransaction},
910
},
1011
sampler::{get_serialized, pull_and_verify_shreds, SHRED_CF},
12+
tinydancer::Cluster,
13+
ConfigSchema,
1114
};
15+
use colored::Colorize;
1216
use hyper::Method;
1317
use reqwest::header;
1418
use serde::{self, Deserialize, Serialize};
1519
use solana_client::rpc_response::RpcApiVersion;
1620
use std::{
21+
fs,
1722
ops::{Deref, Sub},
23+
path::Path,
1824
str::FromStr,
1925
sync::Arc,
2026
time::Duration,
@@ -260,13 +266,24 @@ impl LiteRpcServer for LiteBridge {
260266
) = self.block_store.get_latest_block(commitment_config).await;
261267

262268
info!("glb {blockhash} {slot} {block_height}");
263-
264-
let sampled = pull_and_verify_shreds(
265-
slot as usize,
266-
String::from("http://0.0.0.0:8899"),
267-
10 as usize,
268-
)
269-
.await;
269+
let mut rpc_url = String::from("http://0.0.0.0:8899");
270+
let home_path = std::env::var("HOME").unwrap();
271+
let is_existing = home_path.clone() + "/.config/tinydancer/config.json";
272+
let path = Path::new(&is_existing);
273+
if path.exists() {
274+
let file = fs::File::open(home_path.clone() + "/.config/tinydancer/config.json")
275+
.expect("Error reading config in bridge");
276+
let config: ConfigSchema = serde_json::from_reader(file).unwrap();
277+
rpc_url = get_endpoint(config.cluster);
278+
} else {
279+
println!(
280+
"{} {}",
281+
"Initialise a config first using:".to_string().yellow(),
282+
"tinydancer set config".to_string().green()
283+
);
284+
}
285+
let sampled =
286+
pull_and_verify_shreds(slot as usize, String::from(rpc_url), 10 as usize).await;
270287

271288
Ok(LiteResponse {
272289
context: LiteRpcResponseContext {
@@ -345,12 +362,24 @@ impl LiteRpcServer for LiteBridge {
345362
.get_latest_block_info(CommitmentConfig::finalized())
346363
.await
347364
.slot;
348-
let sampled = pull_and_verify_shreds(
349-
slot as usize,
350-
String::from("http://0.0.0.0:8899"),
351-
10 as usize,
352-
)
353-
.await;
365+
let mut rpc_url = String::from("http://0.0.0.0:8899");
366+
let home_path = std::env::var("HOME").unwrap();
367+
let is_existing = home_path.clone() + "/.config/tinydancer/config.json";
368+
let path = Path::new(&is_existing);
369+
if path.exists() {
370+
let file = fs::File::open(home_path.clone() + "/.config/tinydancer/config.json")
371+
.expect("Error reading config in bridge");
372+
let config: ConfigSchema = serde_json::from_reader(file).unwrap();
373+
rpc_url = get_endpoint(config.cluster);
374+
} else {
375+
println!(
376+
"{} {}",
377+
"Initialise a config first using:".to_string().yellow(),
378+
"tinydancer set config".to_string().green()
379+
);
380+
}
381+
let sampled =
382+
pull_and_verify_shreds(slot as usize, String::from(rpc_url), 10 as usize).await;
354383
Ok(LiteResponse {
355384
context: LiteRpcResponseContext {
356385
slot,

tinydancer/src/rpc_wrapper/mod.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ pub mod tpu_manager;
1616
pub mod workers;
1717
// pub mod cli;
1818
pub mod block_store;
19+
use crate::convert_to_websocket;
1920
use crate::rpc_wrapper::bridge::LiteBridge;
20-
use crate::tinydancer::{ClientService, Cluster};
21+
use crate::tinydancer::{endpoint, ClientService, Cluster};
2122
use anyhow::bail;
2223
use async_trait::async_trait;
2324
use clap::Parser;
@@ -93,34 +94,17 @@ impl ClientService<TransactionServiceConfig> for TransactionService {
9394
type ServiceError = tokio::io::Error;
9495
fn new(config: TransactionServiceConfig) -> Self {
9596
let transaction_handle = tokio::spawn(async {
96-
// let Args {
97-
// rpc_addr,
98-
// ws_addr,
99-
// tx_batch_size,
100-
// lite_rpc_ws_addr,
101-
// lite_rpc_http_addr,
102-
// tx_batch_interval_ms,
103-
// clean_interval_ms,
104-
// fanout_size,
105-
// identity_keypair,
106-
// } = Args::parse();
107-
10897
dotenv().ok();
109-
let rpc_client = RpcClient::new(DEFAULT_RPC_ADDR.to_string());
98+
let rpc_url = endpoint(config.cluster);
11099

111-
//let identity = get_identity_keypair(&identity_keypair).await;
112-
// let blockhash = rpc_client.get_latest_blockhash().await.unwrap();
113100
let payer = Keypair::new();
114-
let airdrop_sign = rpc_client
115-
.request_airdrop(&payer.try_pubkey().unwrap(), 2000000000)
116-
.await?;
117-
info!("AIRDROP CONFIRMED:{}", airdrop_sign);
101+
118102
let tx_batch_interval_ms = Duration::from_millis(DEFAULT_TX_BATCH_INTERVAL_MS);
119103
let clean_interval_ms = Duration::from_millis(DEFAULT_CLEAN_INTERVAL_MS);
120104

121105
let light_bridge = LiteBridge::new(
122-
String::from(DEFAULT_RPC_ADDR),
123-
String::from(DEFAULT_WS_ADDR),
106+
String::from(rpc_url.clone()),
107+
String::from(convert_to_websocket!(rpc_url)),
124108
DEFAULT_FANOUT_SIZE,
125109
payer,
126110
config.db_instance,

tinydancer/src/tinydancer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use anyhow::anyhow;
1818
use async_trait::async_trait;
1919
use futures::{future::join_all, TryFutureExt};
2020
use rand::seq::index::sample;
21+
use serde::{Deserialize, Serialize};
2122
use tiny_logger::logs::info;
2223
// use log::info;
2324
// use log4rs;
@@ -129,8 +130,7 @@ impl TinyDancer {
129130
}
130131
}
131132

132-
#[allow(dead_code)]
133-
#[derive(Clone)]
133+
#[derive(Clone, Debug, Serialize, Deserialize)]
134134
pub enum Cluster {
135135
Mainnet,
136136
Devnet,
@@ -144,7 +144,7 @@ pub fn endpoint(cluster: Cluster) -> String {
144144
Cluster::Mainnet => String::from("https://api.mainnet-beta.solana.com"),
145145
Cluster::Devnet => String::from("https://api.devnet.solana.com"),
146146
Cluster::Localnet => String::from("http://0.0.0.0:8899"),
147-
Cluster::Custom(cluster) => cluster,
147+
Cluster::Custom(url) => url,
148148
}
149149
}
150150
pub enum ClientStatus {

0 commit comments

Comments
 (0)