Skip to content

Commit

Permalink
clear out tokio/jsonrpsee; insert jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
mplsgrant committed Apr 8, 2024
1 parent b87d36f commit f21d419
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 823 deletions.
674 changes: 5 additions & 669 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
anyhow = "1.0.81"
base64 = "0.22.0"
clap = { version = "4.5.4", features = ["derive"] }
jsonrpsee = { version = "0.22.3", features = ["http-client", "client"] }
jsonrpc = "0.17.0"
jsonschema = "0.17.1"
petgraph = "0.6.4"
petgraph-graphml = "3.0.0"
Expand All @@ -19,7 +19,6 @@ rand = "0.8.5"
rust-ini = "0.21.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
xmltree = { version = "0.10.3", features = ["indexmap"] }

[[bin]]
Expand Down
16 changes: 4 additions & 12 deletions src/rust-cli/debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Context;
use jsonrpsee::core::params::ObjectParams;
use serde_json::{json, Value};
use std::path::PathBuf;

use clap::Subcommand;
Expand All @@ -15,18 +14,11 @@ pub enum DebugCommand {
},
}

pub async fn handle_debug_command(
command: &DebugCommand,
mut rpc_params: ObjectParams,
) -> anyhow::Result<()> {
pub fn handle_debug_command(command: &DebugCommand, mut rpc_params: Value) -> anyhow::Result<()> {
match command {
DebugCommand::GenerateCompose { graph_file_path } => {
rpc_params
.insert("graph_file", graph_file_path.to_str())
.context("Adding graph_file_path to rpc params")?;
let data = make_rpc_call("generate_compose", rpc_params)
.await
.context("Calling generate_compose RPC")?;
rpc_params["graph_file"] = json!(graph_file_path.to_str());
let data = make_rpc_call("generate_compose", &rpc_params);
println!("Docker-compose file generated: {:?}", data);
}
}
Expand Down
74 changes: 26 additions & 48 deletions src/rust-cli/general.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::rpc_call::make_rpc_call;
use anyhow::Context;
use jsonrpsee::core::params::ObjectParams;
use serde_json::{json, Value};

use crate::util::pretty_print_value;

Expand All @@ -9,81 +9,59 @@ pub enum NodeType {
BitcoinCli,
}

pub async fn handle_rpc_commands(
pub fn handle_rpc_commands(
node_type: NodeType,
node_index: &u64,
method: &String,
rpc_params: &Option<Vec<String>>,
mut params: ObjectParams,
mut params: Value,
) -> anyhow::Result<()> {
params
.insert("node", node_index)
.context("add node_index param")?;
params
.insert("method", method)
.context("add method param")?;
params["node"] = json!(node_index);
params["method"] = json!(method);
if let Some(p) = rpc_params {
params.insert("params", p).context("add rpc params")?;
params["params"] = json!(p);
}
let data = match node_type {
NodeType::LnCli => make_rpc_call("tank_lncli", params)
.await
.context("make RPC call lncli")?,
NodeType::BitcoinCli => make_rpc_call("tank_bcli", params)
.await
.context("make RPC call bitcoin-cli")?,
NodeType::LnCli => make_rpc_call("tank_lncli", &params).context("make RPC call lncli")?,
NodeType::BitcoinCli => {
make_rpc_call("tank_bcli", &params).context("make RPC call bitcoin-cli")?
}
};
pretty_print_value(&data).context("Pretty print the result")?;
Ok(())
}

pub async fn handle_debug_log_command(node: &u64, mut params: ObjectParams) -> anyhow::Result<()> {
params
.insert("node", node)
.context("add node_index param")?;
let data = make_rpc_call("tank_debug_log", params)
.await
.context("make RPC call tank_debug_log")?;
pub fn handle_debug_log_command(node: &u64, mut params: Value) -> anyhow::Result<()> {
params["node"] = json!(node);
let data = make_rpc_call("tank_debug_log", &params).context("make RPC call tank_debug_log")?;
pretty_print_value(&data).context("pretty print result")?;
Ok(())
}

pub async fn handle_messages_command(
pub fn handle_messages_command(
node_a: &u64,
node_b: &u64,
mut params: ObjectParams,
mut params: Value,
) -> anyhow::Result<()> {
params
.insert("node_a", node_a)
.context("add node_b param")?;
params
.insert("node_b", node_b)
.context("add node_b param")?;
let data = make_rpc_call("tank_messages", params)
.await
.context("Failed to make RPC call tank_messages")?;
params["node_a"] = json!(node_a);
params["node_b"] = json!(node_b);
let data =
make_rpc_call("tank_messages", &params).context("Failed to make RPC call tank_messages")?;
pretty_print_value(&data).context("pretty print result")?;
Ok(())
}

pub async fn handle_grep_logs_command(
pattern: &String,
mut params: ObjectParams,
) -> anyhow::Result<()> {
params
.insert("pattern", pattern)
.context("add pattern param")?;
let data = make_rpc_call("logs_grep", params)
.await
.context("Failed to make RPC call tank_messages")?;
pub fn handle_grep_logs_command(pattern: &String, mut params: Value) -> anyhow::Result<()> {
params["pattern"] = json!(pattern);
let data =
make_rpc_call("logs_grep", &params).context("Failed to make RPC call tank_messages")?;
pretty_print_value(&data).context("pretty print result")?;
Ok(())
}

pub async fn handle_stop_command(params: ObjectParams) -> anyhow::Result<()> {
let data = make_rpc_call("server_stop", params)
.await
.context("Failed to make RPC call server_stop")?;
pub fn handle_stop_command(params: Value) -> anyhow::Result<()> {
let data =
make_rpc_call("server_stop", &params).context("Failed to make RPC call server_stop")?;
pretty_print_value(&data).context("pretty print result")?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/rust-cli/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn handle_create_command(
Ok(())
}

pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()> {
pub fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()> {
match command {
GraphCommand::Create {
number,
Expand Down
2 changes: 1 addition & 1 deletion src/rust-cli/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn build_image(
}
}

pub async fn handle_image_command(command: &ImageCommand) -> anyhow::Result<()> {
pub fn handle_image_command(command: &ImageCommand) -> anyhow::Result<()> {
match command {
ImageCommand::Build {
repo,
Expand Down
40 changes: 20 additions & 20 deletions src/rust-cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Context;
use clap::{Parser, Subcommand};
use debug::handle_debug_command;
use network::handle_network_command;
use serde_json::json;

mod debug;
mod general;
Expand All @@ -9,11 +11,11 @@ mod network;
mod rpc_call;
mod scenarios;
mod util;
use crate::debug::{handle_debug_command, DebugCommand};
use crate::debug::DebugCommand;
use crate::general::*;
use crate::graph::{handle_graph_command, GraphCommand};
use crate::image::{handle_image_command, ImageCommand};
use crate::network::{handle_network_command, NetworkCommand};
use crate::network::NetworkCommand;
use crate::scenarios::{handle_scenario_command, ScenarioCommand};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -94,67 +96,65 @@ enum Commands {
Stop {},
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let mut rpc_params = jsonrpsee::core::params::ObjectParams::new();

let mut rpc_params = json!({});
if let Some(network_value) = &cli.network {
rpc_params
.insert("network", network_value)
.context("Adding --network to rpc_params")?;
rpc_params["network"] = json!(network_value);
}

match &cli.command {
Some(Commands::Debug { command }) => {
if let Some(command) = command {
handle_debug_command(command, rpc_params).await?;
handle_debug_command(command, rpc_params)?;
}
}
Some(Commands::Graph { command }) => {
if let Some(command) = command {
handle_graph_command(command).await?;
handle_graph_command(command)?;
}
}
Some(Commands::Network { command }) => {
if let Some(command) = command {
handle_network_command(command, rpc_params).await?;
handle_network_command(command, rpc_params)?;
}
}
Some(Commands::Scenarios { command }) => {
if let Some(command) = command {
handle_scenario_command(command, rpc_params).await?;
handle_scenario_command(command, rpc_params)?;
}
}
Some(Commands::Rpc {
node,
method,
params,
}) => {
handle_rpc_commands(NodeType::BitcoinCli, node, method, params, rpc_params).await?;
handle_rpc_commands(NodeType::BitcoinCli, node, method, params, rpc_params)?;
}
Some(Commands::LnCli {
node,
method,
params,
}) => {
handle_rpc_commands(NodeType::LnCli, node, method, params, rpc_params).await?;
handle_rpc_commands(NodeType::LnCli, node, method, params, rpc_params)?;
}
Some(Commands::DebugLog { node }) => {
handle_debug_log_command(node, rpc_params).await?;
handle_debug_log_command(node, rpc_params)?;
}
Some(Commands::Image { command }) => {
if let Some(command) = command {
handle_image_command(command).await?;
handle_image_command(command)?;
}
}
Some(Commands::Messages { node_a, node_b }) => {
handle_messages_command(node_a, node_b, rpc_params).await?;
handle_messages_command(node_a, node_b, rpc_params)?;
}
Some(Commands::GrepLogs { pattern }) => {
handle_grep_logs_command(pattern, rpc_params).await?;
handle_grep_logs_command(pattern, rpc_params)?;
}
Some(Commands::Stop {}) => {
handle_stop_command(rpc_params).await?;
handle_stop_command(rpc_params)?;
}
None => println!("No command provided"),
}
Expand Down
18 changes: 5 additions & 13 deletions src/rust-cli/network.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use anyhow::{bail, Context};
use base64::{engine::general_purpose, Engine as _};
use clap::Subcommand;
use jsonrpsee::core::params::ObjectParams;
use prettytable::{cell, Row, Table};
use serde_json::Value;
use serde_json::{json, Value};
use std::path::PathBuf;

use crate::rpc_call::make_rpc_call;
Expand Down Expand Up @@ -118,20 +117,13 @@ fn handle_network_start_response(data: serde_json::Value) -> anyhow::Result<()>
Ok(())
}

pub async fn handle_network_command(
command: &NetworkCommand,
mut params: ObjectParams,
) -> anyhow::Result<()> {
pub fn handle_network_command(command: &NetworkCommand, mut params: Value) -> anyhow::Result<()> {
let (request, params) = match command {
NetworkCommand::Start { graph_file, force } => {
let b64_graph =
graph_file_to_b64(graph_file).context("Reading graph file to base 64")?;
params
.insert("graph_file", b64_graph)
.context("Add base64 graph file to params")?;
params
.insert("force", *force)
.context("Add force bool to params")?;
params["graph_file"] = json!(b64_graph);
params["force"] = json!(force);
("network_from_file", params)
}
NetworkCommand::Up {} => ("network_up", params),
Expand All @@ -142,7 +134,7 @@ pub async fn handle_network_command(
NetworkCommand::Export {} => ("network_export", params),
};

let data = make_rpc_call(request, params).await?;
let data = make_rpc_call(request, &params)?;
match request {
"network_status" => {
handle_network_status_response(data).context("Handling network status response")?
Expand Down
21 changes: 12 additions & 9 deletions src/rust-cli/rpc_call.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder};
use anyhow::Context;
use jsonrpc::{simple_http::SimpleHttpTransport, Client};

use serde_json::Value;
use serde_json::{value::to_raw_value, Value};

pub async fn make_rpc_call(
request: &str,
params: jsonrpsee::core::params::ObjectParams,
) -> anyhow::Result<serde_json::Value> {
pub fn make_rpc_call(request: &str, params: &Value) -> anyhow::Result<serde_json::Value> {
let params = to_raw_value(&params)?;
let url = "http://127.0.0.1:9276/api";
let client = HttpClientBuilder::default().build(url)?;
let response = client.request::<Value, _>(request, params).await?;
Ok(response)
let t = SimpleHttpTransport::builder().url(url)?.build();
let client = Client::with_transport(t);
let req = client.build_request(request, Some(&params));
let response = client.send_request(req)?;
response
.result()
.with_context(|| format!("RPC call failed: {}", request))
}
Loading

0 comments on commit f21d419

Please sign in to comment.