Skip to content

Commit aaea068

Browse files
committed
error handling + minor cleanup + rpc script
1 parent 2b0bc6a commit aaea068

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();
@@ -143,7 +143,8 @@ async fn main() -> Result<()> {
143143
shred_archive_duration,
144144
tui_monitor,
145145
} => {
146-
let config_file = get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
146+
let config_file =
147+
get_config_file().map_err(|_| anyhow!("tinydancer config not set"))?;
147148
let config = TinyDancerConfig {
148149
enable_ui_service,
149150
rpc_endpoint: get_cluster(config_file.cluster),
@@ -162,16 +163,39 @@ async fn main() -> Result<()> {
162163
}
163164

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

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

226-
let config_file = get_config_file();
250+
let config_file = get_config_file();
227251
match config_file {
228252
Ok(mut config_file) => {
229253
// overwrite
230254
config_file.log_path = log_path;
231255
config_file.cluster = cluster;
232-
std::fs::write(
233-
config_path,
234-
serde_json::to_string_pretty(&config_file)?,
235-
)?;
256+
std::fs::write(config_path, serde_json::to_string_pretty(&config_file)?)?;
236257
}
237258
Err(_) => {
238259
// initialize
239260
std::fs::write(
240261
config_path,
241262
serde_json::to_string_pretty(&serde_json::json!({
242263
"cluster":"Localnet",
243-
"logPath":"client.log"
264+
"logPath":"/tmp/client.log"
244265
}))?,
245266
)?;
246267
}
@@ -254,7 +275,8 @@ async fn main() -> Result<()> {
254275
Color::Green,
255276
);
256277

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

260282
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)