Repository navigation
Description
I've been considering an alternative approach to writing a Bitcoin Core JSON RPC client, namely, exporting the structured API information available within Bitcoin Core as JSON, and using that to auto-generate the API.
I created a branch of Core here which implements a new JSON RPC command, api, which dumps structured API information as JSON.
You can see the output here. It contains types and documentation for all RPC commands.
These could be used to automatically generate Rust code, instead of writing them manually and keeping them up to date between versions of Core. Types are not ideal, for example, addresses have type "string". However, you could write a complementary JSON "patch" file which described what the user facing types should be.
For example, getaddressinfo takes a single argument which is of type string. The patch file could contain instructions for the code generator to expose this as a bitcoin::Address:
{
"getaddressinfo": {
"arguments": {
"address": {
"type": "address",
}
}
}
}The code generator would then load the JSON file dumped by bitcoin-cli api, as well as the patch file, and use them to generate user-facing Rust code which exposed the appropriate types, and performed the appropriate conversions when calling Bitcoin Core.
There are a bunch of advantages to this approach. You would be guaranteed to be up-to-date with Core, and could automatically expose documentation from Core as Rust doc comments, without needing to copy them manually. Since everything is auto-generated, you would likely only need a minimum of tests to be confident that things were working.
Some types would require special considerations, like addresses, fee rates, and amounts (which are exposed by the Core RPC with different units in different places). So, it might be wise to consider the patchfile to be a kind of whitelist, and require entries in the patch file for all commands before allowing to code generator to expose them, to ensure that a human had manually confirmed that no types require special handling.
I don't know if I'm going to keep working on this myself. Since rust-bitcoincore-rpc has been deprecated, I went down a bit of a rabbit hole thinking about how the whole RPC situation could be improved, and this is what I came up with. However, in reality, we'll probably keep using rust-bitcoincore-rpc for the time being, and just hack around the limitations until they become intolerable.
If this approach is fruitful, it would be nice to get the branch which adds the api command merged into Core, so it doesn't require continual rebasing. I've been away from Core development for a while, and have been spoiled rotten by Rust, so I might not be the right person to do that.
Activity
tcharding commentedon Nov 14, 2024
Thanks for the write up man, I didn't totally grok it but I had an immediate question, will your idea achieve what I want to achieve with less work now and less work to maintain it? If so I'm in favour of it. To assist you in possibly answering this perhaps I can outline what it is this repo is trying to achieve and why I think it should be low maintenance going forward. FTR if I never have to look at JSON RPC code again I will be a happy camper, I am working on this crate because I think it needs to exist not because I find it interesting in any way.
Wants and don't wants
rust-bitcoin, and when it does break I want to be able to patch it quickly/easilyThis project is pretty new so I don't know if it will achieve these aims but in regards to each one:
rust-bitcointype conversion code that will be annoying to update but it should be brain dead simple at least.json::modelmoduleIt is bleedingly obvious while working on this repo that a trained monkey could do it because it is so repetative. It is also boring, hence why it is taking me so long.
casey commentedon Nov 14, 2024
I'm actually a little bit unclear about how this crate does 1. Looking at the client code, it looks like each version has a different module containing the types for that version. How does the client code run against different core versions? Does it have to enumerate those versions, like doing
v1::method(),v2::method(), or does it somehow select the right method and models to use dynamically based on what version of Core it's connected to?I think codegen potentially helps with this. Each time a new version of core comes out, you rebase the C++ code which dumps the API to JSON onto the new version. Then you run a Rust codegen script to auto-generate the Rust code which contains Rust structs for all the request and response types for that version.
I think codegen helps with this. You have the type conversion code, for example between a JSON string and a Rust
addressin one place, so if that changes, for example because of a change torust-bitcoin, you only have to change it in one place, and the codegen script will replicate it wherever that type is used in a request or response.This depends on whether or not the code in Core changes enough to make rebasing hard. In the best case, it gets integrated into Core, so you don't need to rebase. In the medium case you have to rebase manually but it's easy, in the worst case there's crazy code churn in the new version of Core, so you have to modify it to rebase it.
I think codegen probably helps, or at least makes that complexity easy to deal with.
A big problem with the codegen approach is that I wrote the code for Bitcoin Core 28. I don't know how much change there's been in the RPC API code, but if there's been a lot, rebasing the the code onto earlier and earlier versions, back to v17, might be very hard. I do wonder if it's worth supporting versions of core which are that old. Is there demand for it? If not, then you could just decided to support v28 going forward, and anyone who wants support for an older version should use the deprecated library.
casey commentedon Nov 14, 2024
I posted the API description PR to Twitter, and someone mentioned it in an issue in Core, so linking here for posterity.
tcharding commentedon Nov 14, 2024
Gawd damn, if I knew that before I started this repo I might not have started.
casey commentedon Nov 14, 2024
😭
maflcko commentedon Nov 15, 2024
I wasn't aware of this repo. I only looked at https://github.com/rust-bitcoin/rust-bitcoincore-rpc, which looked a bit incomplete and stale, according to its own readme: https://github.com/rust-bitcoin/rust-bitcoincore-rpc?tab=readme-ov-file#supported-bitcoin-core-versions
This repo looks a lot more what I think should be the goal. That is, this repo properly versions RPCs for each release and every field has the correct type (only did some spot checking). Short of auto-generation (which no one has implemented yet) this is probably the best approach that I've seen so far.
tcharding commentedon Nov 15, 2024
Mad, that inspires me. Thanks.
tcharding commentedon Nov 15, 2024
The names are a mess now, below I'm going to use the new names because they are shorter but for anyone not following the rename I list them here
bitcoind-json-rpc-regtest->corepc-node(directory isbitcoindbecause the crate was originally called that)bitcoind-json-rpc-client->corepc-clientbitcoind-json-rpc-types->corepc-typesWe test using features on the
integration_testcrate that enable features incorepc-node.corepcp-nodespins up a Bitcoin Core node of the correct version and passes back a handle inBitcoinD.corepc-nodehas features that configure whichClientis exposed fromcorepc-clientin the handle i.e., theClientfield ofBitcoinDis version specific. When one callsbitcoind.client.foo()they are callingclient::client_sync::v17::foo(assumingcorepc-nodewas built with feature0_17_1).I think you are right, as long as we don't manually go in and touch the generated code. All "fixes" would have to go into the code generator.
This one I'm more confident you are correct.
I'm hopeful that you are correct here, I don't know exactly how hard it is to go from a fully fleshed out version to the next in this crate yet because v17 is not fully fleshed out. All other versions are just minimal (basically just what was needed by
miniscript).One idea would be to only codegen Rust std types and leave the
modelstuff done manually. It would be nice to code gen the whole thing of course.Yeah I asked myself this question also, I started at v17 because that is where
bitcoincore-rpcstarted and I wanted to replace it without loss of test coverage.casey commentedon Nov 17, 2024
I started working on a new branch which exports a description of the RPC API as JSON Schema. I don't have a lot of experience with JSON Schema, but it seems pretty widely adopted, and there is at least some amount of tooling for it. At the very worst, it doesn't seem a whole lot more difficult to work with than an ad-hoc JSON description of the API
For each command, there is an
argumentskey which holds a JSON Schema describing the arguments to the command. I haven't started on return types yet, since they're much more complicated.casey commentedon Nov 17, 2024
Does that mean that client code has to statically select which version of bitcoind it wants to talk to with a feature?
tcharding commentedon Nov 17, 2024
I don't quite understand the question. By 'client code' you mean code in the
corepc-clientcrate or code that uses theClent(by way of egbitcoind.client)?casey commentedon Nov 17, 2024
Ah yeah, sorry it's confusing. Yeah, I mean a user of
corepc-client::Client.tcharding commentedon Nov 17, 2024
I'll try to clarify.
The
Client(andcorepc-client) is specifically, and explicitly, only for integration testing. I envisage only two users ofClient:integration_testcrate in this repo (solely exits to verify that thetypescrate is correct).bitcoind(for an example seerust-miniscript/bitcoind-tests).A user uses
Clientby way ofcorepc-nodeand theBitcoinDtype (which I'd like to rename toNode).corepc-nodecontrols, by way of features, which version of Core theClientis for.A user of
Clientshould be able to write arbitrary code e.g.,client.foo(). The return type ofclient.foo()is version specific eg if it returnsFoothen thecorepc-nodeguarantees thatFoois correct for that version of Core. This is tested because when the integration tests in this repo run they run against each version of Core.If other projects (
miniscriptor anyone else) want to extend theClientI plan of having a low bar for merging new methods onClient.Now, my opinion, and background on design assumptions:
For production code, people should write their own client and only use the
corepc-typescrate from this repo. They will be able to have confidence that what ever version of Core they hit the types contained intypeswill be valid and correct but they are version specific. Such crates will have to work out for themselves how they handle hitting different versions of Core. I maintain the position that this decision is application specific egAnd all of these depend on what subset of RPC methods the app calls. Hence my view that its app specific and not right to have a single production client library that everyone can use.
The final piece of the puzzle is the
types::modelmodule. This has types that are:rust-bitcointypes (Amountinstead ofu64)One can convert from an
types::vXY::Footo amodel::Fooby callingbitcoind.client.foo().into_model().I have a good reason for the split between
vXY::Fooandmodel::Foobut I've already written enough text for now.nervana21 commentedon Apr 14, 2025
Hi @casey and @tcharding,
Thanks for your efforts.
I've been working on this problem after trying to get more familiar with the corepc code base. The progress that I've made has convinced me that this idea can work and it's not too far away. It would be nice to just have some external confirmation that I'm not mistaken. I'd like to know if this idea is worthy of continued scarce bitcoin engineering resources.
I used the api.json that was generated here the schema found here. From there, types and clients that are very similar to those found in this repository were automatically generated.
There's much more to do, but here is what I have so far:
bitcoin-rpc-codegen
casey commentedon Apr 14, 2025
@nervana21 Very nice, I'm super excited to see someone take this on!
I didn't check out the code in depth, but a couple of things to consider:
snafu, butthiserroris very similar. I'm more familiar with snafu, and use it myself in my projects.Cargo.toml, instead of having to figure out how to run a code generator as part of their workflow. This is probably something for later though, since who knows what eventual shape things will take.Nice work!
nervana21 commentedon Jun 3, 2025
@casey Thanks for your encouraging words :)
I’ve added
thiserrorand published bitcoin-rpc-midas to crates.io.A burning question I have now — this idea is simply a transformation from a structured api.json plus a patch, and that transformation has now been fully implemented, so shouldn’t anything we build manually from here on be cheaper and safer to express through this generator?
The goal from the outset was to create the conditions under which the manual path stops making sense and I'm hopeful that we've finally reached such a threshold.
I just opened a PR against master that drops a clean QED: one integration test, using only the generated client, runs a full PSBT lifecycle.
I'd love to hear if there's anything that I'm missing or any other thoughts that you may have.
Thanks!
tcharding commentedon Jun 4, 2025
I'm going to close this issue because as it describes an idea but it is unactionable. If someone creates a new RPC crate / repo using this method it will be a direct competitor for this repo and the crates in it.
Code is a liability not an asset. If someone comes up with a better way to solve the problems that this repo aims to solve I'm happy to burn this repo and not have to maintain it. Until then I plan to keep working on it and maintaining it.
satsfy commentedon Feb 20, 2026
Hey @tcharding, I understand the issue has been marked as completed, but I kept working on it because I see has value for corepc. I started developing @casey's Bitcoin Core patch such that it generates OpenRPC standard specs for each version Bitcoin Core from v24 to v30, complete with all fields and even comments, directly from Bitcoin Core source code. Then I created a script that can use the spec to generate Rust source code resembling corepc, and another script that inlines structs from corepc source (all the repo code for, say, v30 gets squashed into a single file). Finally, I created yet another comparison script that compares and finds implementation differences between current source code file against the generated code, which yields a useful log with various potential things to be fixed. It is very easy to use. Could even become a CI feature.
This solution is valuable because it delivers:
Let me know if you like this idea, if you think we can use it as is or how can it be made useful for corepc. Thanks!
Here is the PR draft in my fork: https://github.com/natobritto/corepc/pull/26
Thanks for the work you did @casey! I I'll also show the patch OpenRPC patch based on your code on the Bitcoin Core issue #29912 to continue pressing for this change.
tcharding commentedon Feb 20, 2026
Awesome man, I'll take a look. IIUC your hoping to improve the current
corepc-typesnot totally replace it?satsfy commentedon Feb 20, 2026
Yes, my hope is to improve
corepc-types, not build another new project. Also potentially improvecorepc-client. I understand the client package is intended to be used in integration testing, but maybe a codegen solution with Bitcoin Core specs could help lift it someday to production grade? Anyways, I hope to see corepc succeed.tcharding commentedon Feb 20, 2026
Thanks man, appreciate the effort and support. FYI #506 is introducing an example production client. The current
client_syncis destined (IMNSHO) to remain for testing purposes only.casey commentedon Feb 20, 2026
@natobritto Nice, this is awesome to see!