Skip to content

Commit 6e9a84f

Browse files
authored
Merge pull request #1 from anoushk1234/master
error handling + minor cleanup + rpc script
2 parents 0bc9244 + aaea068 commit 6e9a84f

File tree

3 files changed

+60
-51
lines changed

3 files changed

+60
-51
lines changed

rpc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
ssh [email protected] "cd diet-rpc-validator && ./v"
4+
5+
ssh -f -T -N -L 8899:0.0.0.0:8899 [email protected]
6+
7+
ssh -f -T -N -L 8900:0.0.0.0:8900 [email protected]

tinydancer/src/main.rs

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
#![allow(dead_code)]
3030
#![feature(mutex_unlock)]
3131
mod tinydancer;
32+
use crossterm::style::Stylize;
33+
use reqwest::header::{ACCEPT, CONTENT_TYPE};
34+
use sampler::{pull_and_verify_shreds, ArchiveConfig};
35+
use serde::{Deserialize, Serialize};
36+
use serde_json::Value;
37+
use spinoff::{spinners, Color, Spinner};
3238
use std::{
3339
f32::consts::E,
3440
fs::{self, File, OpenOptions},
@@ -37,23 +43,17 @@ use std::{
3743
thread::sleep,
3844
time::Duration,
3945
};
40-
41-
use crossterm::style::Stylize;
42-
use sampler::{pull_and_verify_shreds, ArchiveConfig};
43-
use serde::{Deserialize, Serialize};
44-
use serde_json::Value;
45-
use spinoff::{spinners, Color, Spinner};
4646
use tinydancer::{endpoint, Cluster, TinyDancer, TinyDancerConfig};
4747
mod macros;
4848
use colored::Colorize;
4949
mod rpc_wrapper;
5050
mod sampler;
5151
mod ui;
5252

53-
use tracing::{info};
54-
use tracing_subscriber;
55-
use anyhow::{Result, anyhow};
53+
use anyhow::{anyhow, Result};
5654
use clap::{ArgGroup, Parser, Subcommand, *};
55+
use tracing::info;
56+
use tracing_subscriber;
5757

5858
#[derive(Parser, Debug)]
5959
#[clap(author, version, about, long_about = None)]
@@ -93,19 +93,20 @@ pub enum Commands {
9393
},
9494
/// Stream the client logs to your terminal
9595
Logs {
96-
#[clap(long, required = false, default_value = "client.log")]
96+
#[clap(long, required = false, default_value = "/tmp/client.log")]
9797
log_path: String,
9898
},
9999
/// Edit your client config
100100
#[clap(subcommand)]
101101
Config(ConfigSubcommands),
102+
// Get the latest slot
102103
Slot,
103104
}
104105

105106
#[derive(Debug, Subcommand)]
106107
pub enum ConfigSubcommands {
107108
Set {
108-
#[clap(long, required = false, default_value = "client.log")]
109+
#[clap(long, required = false, default_value = "/tmp/client.log")]
109110
log_path: String,
110111
/// The cluster you want to run the client on (Mainnet, Localnet,Devnet, <custom-url>)
111112
#[clap(long, short, required = false, default_value = "Localnet")]
@@ -114,14 +115,13 @@ pub enum ConfigSubcommands {
114115
Get,
115116
}
116117

117-
pub fn get_config_file() -> Result<ConfigSchema> {
118+
pub fn get_config_file() -> Result<ConfigSchema> {
118119
let home_path = std::env::var("HOME")?;
119-
let path = home_path + "/.config/tinydancer/config.json";
120+
let path = home_path + "/.config/tinydancer/config.json";
120121
let config_str = std::fs::read_to_string(path)?;
121122
Ok(serde_json::from_str::<ConfigSchema>(&config_str)?)
122123
}
123124

124-
// ~/.config/
125125
#[tokio::main]
126126
async fn main() -> Result<()> {
127127
let args = Args::parse();
@@ -142,7 +142,8 @@ async fn main() -> Result<()> {
142142
shred_archive_duration,
143143
tui_monitor,
144144
} => {
145-
let config_file = get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
145+
let config_file =
146+
get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
146147
let config = TinyDancerConfig {
147148
enable_ui_service,
148149
rpc_endpoint: get_cluster(config_file.cluster),
@@ -161,16 +162,39 @@ async fn main() -> Result<()> {
161162
}
162163

163164
Commands::Slot => {
164-
let config_file = get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
165-
let slot_res = send_rpc_call!(
166-
get_endpoint(config_file.cluster),
167-
serde_json::json!({"jsonrpc":"2.0","id":1, "method":"getSlot"}).to_string()
168-
);
169-
let slot = serde_json::from_str::<GetSlotResponse>(slot_res.as_str());
165+
let config_file =
166+
get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
167+
let slot_res = {
168+
let req_client = reqwest::Client::new();
169+
let res = req_client
170+
.post(get_endpoint(config_file.cluster))
171+
.body(
172+
serde_json::json!({"jsonrpc":"2.0","id":1, "method":"getSlot"}).to_string(),
173+
)
174+
.header(CONTENT_TYPE, "application/json")
175+
.header(ACCEPT, "application/json")
176+
.send()
177+
.await;
178+
179+
res
180+
};
181+
182+
match slot_res {
183+
Ok(get_slot_response) => {
184+
let slot_text = get_slot_response.text().await.map_err(|e| {
185+
anyhow!("Failed to get slot due to error: {}", e.to_string())
186+
})?;
170187

171-
match slot {
172-
Ok(slot) => {
173-
println!("Slot: {}", slot.result.to_string().green(),);
188+
let slot = serde_json::from_str::<GetSlotResponse>(&slot_text.as_str());
189+
190+
match slot {
191+
Ok(slot) => {
192+
println!("Slot: {}", slot.result.to_string().green(),);
193+
}
194+
Err(e) => {
195+
println!("Failed to get slot due to error: {}", e.to_string().red());
196+
}
197+
}
174198
}
175199
Err(e) => {
176200
println!("Failed to get slot,due to error: {}", e.to_string().red());
@@ -222,24 +246,21 @@ async fn main() -> Result<()> {
222246
}
223247
sleep(Duration::from_secs(1));
224248

225-
let config_file = get_config_file();
249+
let config_file = get_config_file();
226250
match config_file {
227251
Ok(mut config_file) => {
228252
// overwrite
229253
config_file.log_path = log_path;
230254
config_file.cluster = cluster;
231-
std::fs::write(
232-
config_path,
233-
serde_json::to_string_pretty(&config_file)?,
234-
)?;
255+
std::fs::write(config_path, serde_json::to_string_pretty(&config_file)?)?;
235256
}
236257
Err(_) => {
237258
// initialize
238259
std::fs::write(
239260
config_path,
240261
serde_json::to_string_pretty(&serde_json::json!({
241262
"cluster":"Localnet",
242-
"logPath":"client.log"
263+
"logPath":"/tmp/client.log"
243264
}))?,
244265
)?;
245266
}
@@ -253,7 +274,8 @@ async fn main() -> Result<()> {
253274
Color::Green,
254275
);
255276

256-
let config_file = get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
277+
let config_file =
278+
get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
257279
let is_verified = pull_and_verify_shreds(slot, get_endpoint(config_file.cluster)).await;
258280

259281
if is_verified {

tinydancer/src/tinydancer.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,6 @@ use std::io;
5858
use std::io::ErrorKind;
5959
use std::path::PathBuf;
6060

61-
// use tiny_logger::logs::info;
62-
pub fn get_project_root() -> io::Result<PathBuf> {
63-
let path = env::current_dir()?;
64-
let path_ancestors = path.as_path().ancestors();
65-
66-
for p in path_ancestors {
67-
let has_cargo = read_dir(p)?.any(|p| p.unwrap().file_name() == *"Cargo.lock");
68-
if has_cargo {
69-
let mut path = PathBuf::from(p);
70-
// path.push("log");
71-
path.push("client.log");
72-
return Ok(path);
73-
}
74-
}
75-
Err(io::Error::new(
76-
ErrorKind::NotFound,
77-
"Ran out of places to find Cargo.toml",
78-
))
79-
}
80-
8161
impl TinyDancer {
8262
pub async fn start(config: TinyDancerConfig) -> Result<()> {
8363
let status = ClientStatus::Initializing(String::from("Starting Up Tinydancer"));

0 commit comments

Comments
 (0)