Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add little command for easy access to JSON-RPC interface #180

Merged
merged 3 commits into from
Nov 12, 2021

Conversation

lithp
Copy link
Contributor

@lithp lithp commented Nov 9, 2021

This is a much less ambitious version of #169 and replaces it.

Example output:

$ cargo run -p trin-cli -- discv5_routingTableInfo
Attempting RPC. endpoint=discv5_routingTableInfo file=/tmp/trin-jsonrpc.ipc
{
  "id": 0,
  "jsonrpc": "2.0",
  "result": {
    "buckets": [
      [
        "0xa133..79e2",
        "enr:-IS4QBD940ObAwu3B4K1ddW15ohedlJW96z04vmJ3W9nKwsOOsQVsN_OZUTLH6zYJrq7GknkHQQ1r24zlSBr6aT6MngBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQNPYMlwKY62uH1WcO38VPTHHHD1CSHPJblkwxvbiwKXW4N1ZHCCEdg",
        "Connected"
      ]
    ],
    "localKey": "0x1c74..82db"
  }
}

(the first line is emitted to stderr, so the output of this command can be piped into jq)

@lithp lithp requested review from carver and njgheorghita November 9, 2021 22:53
Copy link
Collaborator

@carver carver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, LGTM after tests pass!

Comment on lines 35 to 54
let mut client = match TrinClient::from_ipc(&ipc) {
Ok(client) => client,
Err(err) => {
eprintln!("Could not connect. err={:?}", err);
return;
}
};

let req = client.build_request(endpoint.as_str());
let resp = client.make_request(req);

match resp {
Err(error) => {
eprintln!("error: {}", error);
}
Ok(value) => {
// unwrap: this value is safe to serialize, it was just deserialized!
println!("{}", serde_json::to_string_pretty(&value).unwrap());
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like it, it looks like this could shrink a bit by returning a result from main (with a bit of error tweaking):

Suggested change
let mut client = match TrinClient::from_ipc(&ipc) {
Ok(client) => client,
Err(err) => {
eprintln!("Could not connect. err={:?}", err);
return;
}
};
let req = client.build_request(endpoint.as_str());
let resp = client.make_request(req);
match resp {
Err(error) => {
eprintln!("error: {}", error);
}
Ok(value) => {
// unwrap: this value is safe to serialize, it was just deserialized!
println!("{}", serde_json::to_string_pretty(&value).unwrap());
}
}
let mut client = TrinClient::from_ipc(&ipc)?;
let req = client.build_request(endpoint.as_str());
let resp = client.make_request(req);
let value = resp?;
// unwrap: this value is safe to serialize, it was just deserialized!
println!("{}", serde_json::to_string_pretty(&value).unwrap());

Or, in this kind of one-shot CLI tool, it doesn't bother me to .unwrap() either (as is already used elsewhere in the module).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, just pushed a much shorter version


fn build_request<'a>(method: &'a str, request_id: u64) -> jsonrpc::Request<'a> {
jsonrpc::Request {
method: method,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy said:

Suggested change
method: method,
method,

Comment on lines +108 to +109
// gave it exclusive ownership that would require us to open a new connection for every
// command we wanted to send! By making a clone (or, by trying to) we can have our cake
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's only important with an interactive REPL-type tool, rather than this CLI one-shot, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, definitely. I can drop it if you would prefer. This was originally added because I had grand plans of turning this into a client we could use in other parts of the codebase which want to make JSON-RPC calls, such as in the peer tester. You're right that it's unnecessary for current usage but I still think it's useful to have that long-term goal for TrinClient.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm happy to keep it. Maybe just add that little bit of context to the comment.

Copy link
Collaborator

@njgheorghita njgheorghita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool! It'd be nice if you could add a README.md with a couple basic usage examples. On a broader note, I typically use a python script / ipython console to hit the rpc endpoints. The ipython repl is pretty solid for just hitting endpoints ad nauseam. Just curious, how do you see this crate evolving? To play devils advocate just a bit, would distributing a python script be simpler?

@lithp lithp merged commit a87dc4f into ethereum:master Nov 12, 2021
@lithp lithp deleted the lithp/add-cli-simple branch November 12, 2021 20:02
@lithp
Copy link
Contributor Author

lithp commented Nov 12, 2021

Looks cool! It'd be nice if you could add a README.md with a couple basic usage examples. On a broader note, I typically use a python script / ipython console to hit the rpc endpoints. The ipython repl is pretty solid for just hitting endpoints ad nauseam. Just curious, how do you see this crate evolving? To play devils advocate just a bit, would distributing a python script be simpler?

I really wish that distributing a python script was easier, but the second that script uses a library from outside the standard library then it becomes very difficult for anyone who is not a python developer to use, by bundling simple tooling with trin we can make it easy for anyone who can install trin to install the associated tooling.

Unfortunately I will not be around to see it out, but my plan had a couple different prongs:

  1. This command is already useful, I can hit ctrl-r from any shell and inspect my trin node's routing table. My next step was going to be adding some JSON-RPC endpoints which allow manually inserting data into the state network, or manually triggering specific TALKREQ commands, having all of these in a little cli seems like it provides some nice debugging UX!
  2. Most of the code in this crate is not specific to trin-cli, it's useful anywhere we want to be a JSON-RPC client. This can be pulled out and moved into trin-core and used in other parts of the codebase.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants