WIP: Create an async corepc-client (adding async support to jsonrpc)#505
WIP: Create an async corepc-client (adding async support to jsonrpc)#505jamillambert wants to merge 11 commits intorust-bitcoin:masterfrom
Conversation
d5524e1 to
9a77aa2
Compare
6981d8f to
6cd8bb2
Compare
jsonrpc/src/client_async.rs
Outdated
| /// Creates a new client with the given transport. | ||
| pub fn with_transport<T: Transport>(transport: T) -> Client { | ||
| Client { transport: Box::new(transport), nonce: atomic::AtomicUsize::new(1) } | ||
| pub fn with_transport<T: AsyncTransport>(transport: T) -> AsyncClient { |
There was a problem hiding this comment.
My suggestion would be to not rename these types. That way the diff is smaller. I'd rename client.rs to client_sync.rs. You'll need to remove the public re-exports from lib.rs and that maybe fix up some paths to always use client_sync:: or client_async:: as appropriate.
| # A transport that uses `bitreq` as the async HTTP client. | ||
| bitreq_http_async = [ "base64", "bitreq", "bitreq/async" ] | ||
| # An async JSON-RPC client implementation. | ||
| client_async = [] |
There was a problem hiding this comment.
Maybe this feature should enable bitreq_http_async?
jsonrpc/Cargo.toml
Outdated
| @@ -22,6 +22,10 @@ default = [ "simple_http", "simple_tcp" ] | |||
| simple_http = [ "base64" ] | |||
| # A transport that uses `bitreq` as the HTTP client. | |||
| bitreq_http = [ "base64", "bitreq" ] | |||
There was a problem hiding this comment.
Maybe this should be re-name to bitreq_http_sync?
|
@apoelstra whats your take man? Would you be willing to have |
In preparation for adding the new async client rename the existing sync client, associated feature and fuzz target to have the suffic _sync.
6cd8bb2 to
87dfe15
Compare
|
Holla at me if/when you would like some review mate. |
In preparation for adding an async client remove the reexports of the sync Client and Transport. Fix the affected imports and Debug impl.
Code copy only to make it easier in the next patch to see what the changes are for async.
Remove code related to fuzzing from the new async files. These can be added back later once the async version is working.
In preparation for adding bitreq_http_async feature to jsonrpc move the sync version to the client-sync feature so it is not always on with jsonrpc.
Create a new folder for the upcoming async client and copy in the existing client_sync code. Code copy only to make the next patch easier to review.
Edit the copy of the sync client created in the previous commit to be async. Update the readme and cargo.toml files. Add only small set of RPCs.
Add some vibe coded tests for the async client to check that everything is functional. Tests are for development purposes only to catch simple errors like feature gates etc. and have not been reviewed.
Add a JSONRPC_VERSION constant to remove magic number in code. Add the same version check to batch responses.
eb3b27b to
1c174a6
Compare
Remove the version folders with RPC macros. Rewrite the async client to have the base methods in the main module removing all the macroization. Create a new module for the bdk client that has the required RPCs in it that all return the non-version specific model types. Rewrite the tests to use the bdk client and use a similar structure to the sync tests.
1c174a6 to
7a79b87
Compare
|
Rewrote the async client:
@tcharding thoughts? in particular the error handling of converting to the modelled type. Returning a modelled type vs the inner field e.g. |
| async fn bdk_server_version(&self) -> Result<usize> { | ||
| let info: serde_json::Value = self.call("getnetworkinfo", &[]).await?; | ||
| let version = info | ||
| .get("version") | ||
| .and_then(serde_json::Value::as_u64) | ||
| .ok_or(Error::UnexpectedStructure)?; | ||
| usize::try_from(version).map_err(|_| Error::UnexpectedStructure) |
There was a problem hiding this comment.
Unfortunately we cannot do this. This is one of the main reasons behind certain design choices in this repo. While it seems sane I heard but cannot remember right now that there may be reasons why a node operator disables the getnetworkinfo method and it shouldn't be relied upon. Sorry for the poor explanation, it was a while ago now and I just committed to memory that we can't rely on it.
I'll comment further near where this is called.
| &self, | ||
| hash: &BlockHash, | ||
| ) -> Result<GetBlockHeaderVerbose> { | ||
| if self.bdk_server_version().await? >= VERSION_WITH_TARGET_FIELD { |
There was a problem hiding this comment.
Another solution is to do an RPC call to v29 and if the call fails (because the json is mismatched) then try to do a v25 call. There are other solutions I've thought of but this is the one I like most.
This problem is an example of how which versions of Core the client supports effects the logic and how projects might want to do things differently. For example a project that is only hitting a Core node that it controls would not need to do this dance.
There was a problem hiding this comment.
I think for this case where there are only two possible versions where one has an extra field trying v29 and if it fails try v25 should work fine.
| pub async fn get_block(&self, hash: &BlockHash) -> Result<GetBlockVerboseZero> { | ||
| let json: crate::types::v25::GetBlockVerboseZero = | ||
| self.call("getblock", &[into_json(hash)?, into_json(0)?]).await?; | ||
| json.into_model().map_err(|e| Error::Returned(e.to_string())) |
There was a problem hiding this comment.
We don't need to map here. Plain old Ok(json.into_mode()?) works.
After writing a massive post I realised this solution is not going to work (see bottom for explanation). I think we should inline the How about throwing this at the top: //! Async JSON-RPC client designed explicitly to support BDK.
//!
//! ## Project decisions
//!
//! * Support Core versions 25 to 30.
Troublesome, commented already on the code.
Another solution is to have an error type for each function. Then it can have a variant (assuming its an enum) for the exact error returned by
Agreed that a wrapper type is not useful to returen (eg
Nice.
I don't think separation by project module is not going to work because the modules will conflict with each other. And if we feature gate the features will not be additive. We could try some macro stuff but I think it defeats the purpose which is to write a simple clean client that others can fork if they need to. |
Not a merge candidate. Proof of concept only.
This is PR option 1 of 2, both add an async corepc-client but in a slightly different ways. This version adds
asyncsupport tojsonrpc, the other adds the required part of jsonrpc into corepc-client (#506).The patches are structured to make it easier to see what was changed from sync to async. First the sync version is copied and renamed, then in a separate patch it is changed to async.