Skip to content

Commit f21d419

Browse files
committed
clear out tokio/jsonrpsee; insert jsonrpc
1 parent b87d36f commit f21d419

File tree

10 files changed

+98
-823
lines changed

10 files changed

+98
-823
lines changed

Cargo.lock

Lines changed: 5 additions & 669 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2021"
99
anyhow = "1.0.81"
1010
base64 = "0.22.0"
1111
clap = { version = "4.5.4", features = ["derive"] }
12-
jsonrpsee = { version = "0.22.3", features = ["http-client", "client"] }
12+
jsonrpc = "0.17.0"
1313
jsonschema = "0.17.1"
1414
petgraph = "0.6.4"
1515
petgraph-graphml = "3.0.0"
@@ -19,7 +19,6 @@ rand = "0.8.5"
1919
rust-ini = "0.21.0"
2020
serde = { version = "1.0.197", features = ["derive"] }
2121
serde_json = "1.0.115"
22-
tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
2322
xmltree = { version = "0.10.3", features = ["indexmap"] }
2423

2524
[[bin]]

src/rust-cli/debug.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use anyhow::Context;
2-
use jsonrpsee::core::params::ObjectParams;
1+
use serde_json::{json, Value};
32
use std::path::PathBuf;
43

54
use clap::Subcommand;
@@ -15,18 +14,11 @@ pub enum DebugCommand {
1514
},
1615
}
1716

18-
pub async fn handle_debug_command(
19-
command: &DebugCommand,
20-
mut rpc_params: ObjectParams,
21-
) -> anyhow::Result<()> {
17+
pub fn handle_debug_command(command: &DebugCommand, mut rpc_params: Value) -> anyhow::Result<()> {
2218
match command {
2319
DebugCommand::GenerateCompose { graph_file_path } => {
24-
rpc_params
25-
.insert("graph_file", graph_file_path.to_str())
26-
.context("Adding graph_file_path to rpc params")?;
27-
let data = make_rpc_call("generate_compose", rpc_params)
28-
.await
29-
.context("Calling generate_compose RPC")?;
20+
rpc_params["graph_file"] = json!(graph_file_path.to_str());
21+
let data = make_rpc_call("generate_compose", &rpc_params);
3022
println!("Docker-compose file generated: {:?}", data);
3123
}
3224
}

src/rust-cli/general.rs

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::rpc_call::make_rpc_call;
22
use anyhow::Context;
3-
use jsonrpsee::core::params::ObjectParams;
3+
use serde_json::{json, Value};
44

55
use crate::util::pretty_print_value;
66

@@ -9,81 +9,59 @@ pub enum NodeType {
99
BitcoinCli,
1010
}
1111

12-
pub async fn handle_rpc_commands(
12+
pub fn handle_rpc_commands(
1313
node_type: NodeType,
1414
node_index: &u64,
1515
method: &String,
1616
rpc_params: &Option<Vec<String>>,
17-
mut params: ObjectParams,
17+
mut params: Value,
1818
) -> anyhow::Result<()> {
19-
params
20-
.insert("node", node_index)
21-
.context("add node_index param")?;
22-
params
23-
.insert("method", method)
24-
.context("add method param")?;
19+
params["node"] = json!(node_index);
20+
params["method"] = json!(method);
2521
if let Some(p) = rpc_params {
26-
params.insert("params", p).context("add rpc params")?;
22+
params["params"] = json!(p);
2723
}
2824
let data = match node_type {
29-
NodeType::LnCli => make_rpc_call("tank_lncli", params)
30-
.await
31-
.context("make RPC call lncli")?,
32-
NodeType::BitcoinCli => make_rpc_call("tank_bcli", params)
33-
.await
34-
.context("make RPC call bitcoin-cli")?,
25+
NodeType::LnCli => make_rpc_call("tank_lncli", &params).context("make RPC call lncli")?,
26+
NodeType::BitcoinCli => {
27+
make_rpc_call("tank_bcli", &params).context("make RPC call bitcoin-cli")?
28+
}
3529
};
3630
pretty_print_value(&data).context("Pretty print the result")?;
3731
Ok(())
3832
}
3933

40-
pub async fn handle_debug_log_command(node: &u64, mut params: ObjectParams) -> anyhow::Result<()> {
41-
params
42-
.insert("node", node)
43-
.context("add node_index param")?;
44-
let data = make_rpc_call("tank_debug_log", params)
45-
.await
46-
.context("make RPC call tank_debug_log")?;
34+
pub fn handle_debug_log_command(node: &u64, mut params: Value) -> anyhow::Result<()> {
35+
params["node"] = json!(node);
36+
let data = make_rpc_call("tank_debug_log", &params).context("make RPC call tank_debug_log")?;
4737
pretty_print_value(&data).context("pretty print result")?;
4838
Ok(())
4939
}
5040

51-
pub async fn handle_messages_command(
41+
pub fn handle_messages_command(
5242
node_a: &u64,
5343
node_b: &u64,
54-
mut params: ObjectParams,
44+
mut params: Value,
5545
) -> anyhow::Result<()> {
56-
params
57-
.insert("node_a", node_a)
58-
.context("add node_b param")?;
59-
params
60-
.insert("node_b", node_b)
61-
.context("add node_b param")?;
62-
let data = make_rpc_call("tank_messages", params)
63-
.await
64-
.context("Failed to make RPC call tank_messages")?;
46+
params["node_a"] = json!(node_a);
47+
params["node_b"] = json!(node_b);
48+
let data =
49+
make_rpc_call("tank_messages", &params).context("Failed to make RPC call tank_messages")?;
6550
pretty_print_value(&data).context("pretty print result")?;
6651
Ok(())
6752
}
6853

69-
pub async fn handle_grep_logs_command(
70-
pattern: &String,
71-
mut params: ObjectParams,
72-
) -> anyhow::Result<()> {
73-
params
74-
.insert("pattern", pattern)
75-
.context("add pattern param")?;
76-
let data = make_rpc_call("logs_grep", params)
77-
.await
78-
.context("Failed to make RPC call tank_messages")?;
54+
pub fn handle_grep_logs_command(pattern: &String, mut params: Value) -> anyhow::Result<()> {
55+
params["pattern"] = json!(pattern);
56+
let data =
57+
make_rpc_call("logs_grep", &params).context("Failed to make RPC call tank_messages")?;
7958
pretty_print_value(&data).context("pretty print result")?;
8059
Ok(())
8160
}
8261

83-
pub async fn handle_stop_command(params: ObjectParams) -> anyhow::Result<()> {
84-
let data = make_rpc_call("server_stop", params)
85-
.await
86-
.context("Failed to make RPC call server_stop")?;
62+
pub fn handle_stop_command(params: Value) -> anyhow::Result<()> {
63+
let data =
64+
make_rpc_call("server_stop", &params).context("Failed to make RPC call server_stop")?;
8765
pretty_print_value(&data).context("pretty print result")?;
8866
Ok(())
8967
}

src/rust-cli/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn handle_create_command(
252252
Ok(())
253253
}
254254

255-
pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()> {
255+
pub fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()> {
256256
match command {
257257
GraphCommand::Create {
258258
number,

src/rust-cli/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn build_image(
145145
}
146146
}
147147

148-
pub async fn handle_image_command(command: &ImageCommand) -> anyhow::Result<()> {
148+
pub fn handle_image_command(command: &ImageCommand) -> anyhow::Result<()> {
149149
match command {
150150
ImageCommand::Build {
151151
repo,

src/rust-cli/main.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use anyhow::Context;
21
use clap::{Parser, Subcommand};
2+
use debug::handle_debug_command;
3+
use network::handle_network_command;
4+
use serde_json::json;
35

46
mod debug;
57
mod general;
@@ -9,11 +11,11 @@ mod network;
911
mod rpc_call;
1012
mod scenarios;
1113
mod util;
12-
use crate::debug::{handle_debug_command, DebugCommand};
14+
use crate::debug::DebugCommand;
1315
use crate::general::*;
1416
use crate::graph::{handle_graph_command, GraphCommand};
1517
use crate::image::{handle_image_command, ImageCommand};
16-
use crate::network::{handle_network_command, NetworkCommand};
18+
use crate::network::NetworkCommand;
1719
use crate::scenarios::{handle_scenario_command, ScenarioCommand};
1820

1921
#[derive(Parser, Debug)]
@@ -94,67 +96,65 @@ enum Commands {
9496
Stop {},
9597
}
9698

97-
#[tokio::main]
98-
async fn main() -> anyhow::Result<()> {
99+
fn main() -> anyhow::Result<()> {
99100
let cli = Cli::parse();
100-
let mut rpc_params = jsonrpsee::core::params::ObjectParams::new();
101+
102+
let mut rpc_params = json!({});
101103
if let Some(network_value) = &cli.network {
102-
rpc_params
103-
.insert("network", network_value)
104-
.context("Adding --network to rpc_params")?;
104+
rpc_params["network"] = json!(network_value);
105105
}
106106

107107
match &cli.command {
108108
Some(Commands::Debug { command }) => {
109109
if let Some(command) = command {
110-
handle_debug_command(command, rpc_params).await?;
110+
handle_debug_command(command, rpc_params)?;
111111
}
112112
}
113113
Some(Commands::Graph { command }) => {
114114
if let Some(command) = command {
115-
handle_graph_command(command).await?;
115+
handle_graph_command(command)?;
116116
}
117117
}
118118
Some(Commands::Network { command }) => {
119119
if let Some(command) = command {
120-
handle_network_command(command, rpc_params).await?;
120+
handle_network_command(command, rpc_params)?;
121121
}
122122
}
123123
Some(Commands::Scenarios { command }) => {
124124
if let Some(command) = command {
125-
handle_scenario_command(command, rpc_params).await?;
125+
handle_scenario_command(command, rpc_params)?;
126126
}
127127
}
128128
Some(Commands::Rpc {
129129
node,
130130
method,
131131
params,
132132
}) => {
133-
handle_rpc_commands(NodeType::BitcoinCli, node, method, params, rpc_params).await?;
133+
handle_rpc_commands(NodeType::BitcoinCli, node, method, params, rpc_params)?;
134134
}
135135
Some(Commands::LnCli {
136136
node,
137137
method,
138138
params,
139139
}) => {
140-
handle_rpc_commands(NodeType::LnCli, node, method, params, rpc_params).await?;
140+
handle_rpc_commands(NodeType::LnCli, node, method, params, rpc_params)?;
141141
}
142142
Some(Commands::DebugLog { node }) => {
143-
handle_debug_log_command(node, rpc_params).await?;
143+
handle_debug_log_command(node, rpc_params)?;
144144
}
145145
Some(Commands::Image { command }) => {
146146
if let Some(command) = command {
147-
handle_image_command(command).await?;
147+
handle_image_command(command)?;
148148
}
149149
}
150150
Some(Commands::Messages { node_a, node_b }) => {
151-
handle_messages_command(node_a, node_b, rpc_params).await?;
151+
handle_messages_command(node_a, node_b, rpc_params)?;
152152
}
153153
Some(Commands::GrepLogs { pattern }) => {
154-
handle_grep_logs_command(pattern, rpc_params).await?;
154+
handle_grep_logs_command(pattern, rpc_params)?;
155155
}
156156
Some(Commands::Stop {}) => {
157-
handle_stop_command(rpc_params).await?;
157+
handle_stop_command(rpc_params)?;
158158
}
159159
None => println!("No command provided"),
160160
}

src/rust-cli/network.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use anyhow::{bail, Context};
22
use base64::{engine::general_purpose, Engine as _};
33
use clap::Subcommand;
4-
use jsonrpsee::core::params::ObjectParams;
54
use prettytable::{cell, Row, Table};
6-
use serde_json::Value;
5+
use serde_json::{json, Value};
76
use std::path::PathBuf;
87

98
use crate::rpc_call::make_rpc_call;
@@ -118,20 +117,13 @@ fn handle_network_start_response(data: serde_json::Value) -> anyhow::Result<()>
118117
Ok(())
119118
}
120119

121-
pub async fn handle_network_command(
122-
command: &NetworkCommand,
123-
mut params: ObjectParams,
124-
) -> anyhow::Result<()> {
120+
pub fn handle_network_command(command: &NetworkCommand, mut params: Value) -> anyhow::Result<()> {
125121
let (request, params) = match command {
126122
NetworkCommand::Start { graph_file, force } => {
127123
let b64_graph =
128124
graph_file_to_b64(graph_file).context("Reading graph file to base 64")?;
129-
params
130-
.insert("graph_file", b64_graph)
131-
.context("Add base64 graph file to params")?;
132-
params
133-
.insert("force", *force)
134-
.context("Add force bool to params")?;
125+
params["graph_file"] = json!(b64_graph);
126+
params["force"] = json!(force);
135127
("network_from_file", params)
136128
}
137129
NetworkCommand::Up {} => ("network_up", params),
@@ -142,7 +134,7 @@ pub async fn handle_network_command(
142134
NetworkCommand::Export {} => ("network_export", params),
143135
};
144136

145-
let data = make_rpc_call(request, params).await?;
137+
let data = make_rpc_call(request, &params)?;
146138
match request {
147139
"network_status" => {
148140
handle_network_status_response(data).context("Handling network status response")?

src/rust-cli/rpc_call.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder};
1+
use anyhow::Context;
2+
use jsonrpc::{simple_http::SimpleHttpTransport, Client};
23

3-
use serde_json::Value;
4+
use serde_json::{value::to_raw_value, Value};
45

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

0 commit comments

Comments
 (0)