Skip to content

Commit

Permalink
Merge pull request #2 from PoCInnovation/infos_command
Browse files Browse the repository at this point in the history
Infos command
  • Loading branch information
alexandreTimal authored Feb 1, 2025
2 parents 020b996 + bb1982d commit 4bce8c7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ anyhow = "1.0.95"
clap = { version = "4", features = ["derive", "env"] }
reqwest = { version = "0.12.12", features = ["json"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
tokio = { version = "1.43.0", features = ["full"] }
6 changes: 6 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{Parser, Subcommand, ValueHint};

use crate::wallet::WalletSubcommands;
use crate::infos::InfosSubcommands;

#[derive(Parser)]
#[command(version)]
Expand All @@ -21,4 +22,9 @@ pub enum KermitSubcommand {
#[command(subcommand)]
command: WalletSubcommands,
},
#[command(visible_alias = "i")]
Infos {
#[command(subcommand)]
command: InfosSubcommands,
}
}
121 changes: 121 additions & 0 deletions src/infos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use crate::utils::{get, post};
use anyhow::{Ok, Result};
use clap::Parser;
use serde_json::{json, Value};

/// CLI arguments for `kermit infos`.
#[derive(Parser)]
pub enum InfosSubcommands {
/// Get info about that node.
#[command(visible_alias = "n")]
Node,

/// Get version about that node.
#[command(visible_alias = "v")]
Version,

/// Get key params about your blockchain.
#[command(visible_alias = "cp")]
ChainParams,

/// Get info about your own clique.
#[command(visible_alias = "sc")]
SelfClique,

/// Get infos about the inter cliques.
#[command(visible_alias = "icpi")]
InterCliquePeerInfo,

/// Get discovered neighbors.
#[command(visible_alias = "dn")]
DiscoveredNeighbors,

/// Get the misbehaviors of peers.
#[command(visible_alias = "m")]
Misbehaviors,

/// Ban/Unban given peers.
#[command(visible_alias = "mbu")]
MisbehaviorsBanUnban { r#type: String, peers: Vec<String> },

/// Get the unreachable brokers.
#[command(visible_alias = "ub")]
UnreachableBrokers,

/// Set brokers to be unreachable/reachable.
#[command(visible_alias = "ds")]
Discovery { r#type: String, peers: Vec<String> },

/// Get history average hashrate on the given time interval.
#[command(visible_alias = "hh")]
HistoryHashrate {
from_ts: i64,
to_ts: Option<i64>,
},

/// Get average hashrate from now - timespan(millis) to now.
#[command(visible_alias = "chr")]
CurrentHashrate {
timespan: Option<i64>,
},

/// Get the average difficulty of the latest blocks from all shards.
#[command(visible_alias = "cd")]
CurrentDifficulty,
}

impl InfosSubcommands {
pub async fn run(self, url: &str) -> Result<()> {
let value: Value = match self {
Self::Node => get(url, "/infos/node").await?,
Self::Version => get(url, "/infos/version").await?,
Self::ChainParams => get(url, "/infos/chain-params").await?,
Self::SelfClique => get(url, "/infos/self-clique").await?,
Self::InterCliquePeerInfo => get(url, "/infos/inter-clique-peer-info").await?,
Self::DiscoveredNeighbors => get(url, "/infos/discovered-neighbors").await?,
Self::Misbehaviors => get(url, "/infos/misbehaviors").await?,
Self::MisbehaviorsBanUnban { r#type, peers } => {
post(
url,
"/infos/misbehaviors",
json!({
"type": r#type,
"peers": peers
}),
)
.await?
},
Self::UnreachableBrokers => get(url, "/infos/unreachable").await?,
Self::Discovery { r#type, peers } => {
post(
url,
"/infos/misbehaviors",
json!({
"type": r#type,
"peers": peers
}),
)
.await?
},
Self::HistoryHashrate { from_ts, to_ts } => {
let mut endpoint = format!("/infos/history-hashrate?fromTs={}", from_ts);
if let Some(to_ts) = to_ts {
endpoint.push_str(&format!("&toTs={}", to_ts));
}
get(url, &endpoint).await?
},
Self::CurrentHashrate { timespan } => {
let mut endpoint = "/infos/current-hashrate".to_string();
if let Some(timespan) = timespan {
endpoint.push_str(&format!("?timespan={}", timespan));
}
get(url, &endpoint).await?
},
Self::CurrentDifficulty => get(url, "/infos/current-difficulty").await?,
};

serde_json::to_writer_pretty(std::io::stdout(), &value)?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod args;
mod utils;
mod wallet;
mod infos;

use anyhow::Result;
use args::{Kermit, KermitSubcommand};
Expand All @@ -19,6 +20,7 @@ async fn run() -> Result<()> {

match kermit.cmd {
KermitSubcommand::Wallet { command } => command.run().await?,
KermitSubcommand::Infos { command } => command.run(&kermit.url).await?,
}

Ok(())
Expand Down

0 comments on commit 4bce8c7

Please sign in to comment.