2929#![ allow( dead_code) ]
3030#![ feature( mutex_unlock) ]
3131mod 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 } ;
3238use 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 } ;
4646use tinydancer:: { endpoint, Cluster , TinyDancer , TinyDancerConfig } ;
4747mod macros;
4848use colored:: Colorize ;
4949mod rpc_wrapper;
5050mod sampler;
5151mod ui;
5252
53- use tracing:: { info} ;
54- use tracing_subscriber;
55- use anyhow:: { Result , anyhow} ;
53+ use anyhow:: { anyhow, Result } ;
5654use 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 ) ]
106107pub 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]
126126async 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 {
0 commit comments