Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Some ideas for improvement #19

Merged
merged 5 commits into from
Nov 8, 2018
Merged

Some ideas for improvement #19

merged 5 commits into from
Nov 8, 2018

Conversation

dpc
Copy link
Contributor

@dpc dpc commented Nov 4, 2018

While Request and Response might be useful, for a lot of use-cases they are just an implementation detail, that just gets in the way. I was wondering if they could be made non-private altogether, but I guess for some advanced uses, they might be necessary.

So the first patch makes some allocation savings, and second add a convenience function that allows forgetting that Response and Request exist at all. First patch have a longer commit explaining more about what's going on.

@dpc
Copy link
Contributor Author

dpc commented Nov 4, 2018

This is how the user API changes: dpc/rust-bitcoin-rpc@dpc-dev...dpc:dpc-dev-jsonrpc-reorg

@apoelstra apoelstra added this to the 0.11 milestone Nov 5, 2018
@apoelstra
Copy link
Owner

apoelstra commented Nov 6, 2018

I like this, though I wonder if it's possible to go farther. At Blockstream we have a similar wrapper to your do_rpc function, but with a couple more changes:

  1. We also have a do_rpc_hex function which additionally hex-decodes the data and then passes the result to rust-bitcoin's deserialize function. This probably doesn't make sense because it'd put a rust-bitcoin dependency on rust-jsonrpc, but it'd be cool if there was a way to do this. This is useful for RPCs like getblock and getrawtransaction which support dumping raw hex-encoded data and evading json altogether.
  2. We use the RPC getblockchaininfo call to determine whether a bitcoind has started up. When it first turns on it returns the error code -28. After that it will return a valid getblockchaininfo output. This requires using the Request/Response distinction. Do you think there's a way we can make that more ergonomic?

@dpc
Copy link
Contributor Author

dpc commented Nov 6, 2018

  1. Yeah. This is a common pattern in in rust-bitcoin-rpc too. Because it depends on rust-bitcoin I'd just generalize it exactly like you described and put in soon to be a rust-bitcoincore-rpc.

  2. I was actually doing something lamer. I belive this also belongs to rust-bitcoincore-rpc. I would actually make it something higher-level like fn wait_for_node_to_be_ready(&self) -> Result<()> so users don't have to know the details of "error code -28".

Both of these fit with my general plan to make rust-bitcioncore-rpc not just raw pass-through to rust-jsonrpc, but actually contain some conveninance for the user and interoperability with rust-bitcoin.

What can be taken by reference, is taken by reference.
This gives `Request` a lifetime, so it can't derive `Deserialize`.

Because of that, I had to delete a test, but that seems OK, since
it looks like this test is testing `serde_json` crate, and not any
logic in this crate.
@dpc
Copy link
Contributor Author

dpc commented Nov 6, 2018

  1. like this?

src/client.rs Outdated
let request = self.build_request(rpc_name, args);
let response = self.send_request(&request)?;

Ok(response.clone().into_result()?)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this clone required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it isn't! :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gone. Thank you!

@stevenroose
Copy link
Contributor

ACK

src/client.rs Outdated
@@ -114,14 +113,14 @@ impl Client {
}

/// Builds a request
pub fn build_request(&self, name: String, params: Vec<serde_json::Value>) -> Request {
pub fn build_request<'a>(&self, name: &'a str, params: &'a [serde_json::Value]) -> Request<'a> {
Copy link
Owner

Choose a reason for hiding this comment

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

Can you split this into two lifetimes, one for name and one for params?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@apoelstra
Copy link
Owner

ACK

@apoelstra apoelstra merged commit 18862f8 into apoelstra:master Nov 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants