contrib: Add external RPC code generator with unit tests #32140

pull EniRox001 wants to merge 6 commits into bitcoin:master from EniRox001:codegen changing 10 files +516 −0
  1. EniRox001 commented at 7:16 pm on March 25, 2025: none

    Description

    This pull request introduces an external tool for generating RPC client code based on the Bitcoin Core RPC schema. The tool is implemented in Python using Jinja2 templates and can generate code for both C++ and Python clients. In addition, unit tests have been added to verify that the generator produces the expected output given a sample schema.

    Changes Included

    • contrib/codegen/generate_client.py
      A script that retrieves the latest RPC schema (via bitcoin-cli) and renders language- specific templates.
    • contrib/codegen/templates/
      • cpp_client.jinja2 – Template for generating a C++ RPC client header.
      • python_client.jinja2 – Template for generating a Python RPC client.
    • contrib/codegen/tests/test_generate_client.py
      Unit tests to ensure the code generator works as expected for both C++ and Python output.

    Rationale

    This tool provides a convenient way for developers to automatically update external client libraries when the RPC schema changes. It improves maintainability by reducing manual effort and potential errors. As a non-core developer utility, it is placed under the contrib/ directory to keep it separate from the core runtime.

    Testing and Verification

    • The generated client code for both C++ and Python has been compiled (for C++) and run in a test harness.
    • Unit tests verify that, given a sample schema, the output contains the expected method declarations and syntax.
    • The tool automatically retrieves the latest schema using the bitcoin-cli schema RPC.

    Please see the accompanying README in contrib/codegen/ for detailed usage instructions.

  2. Add `schema` RPC 9d90e50d19
  3. Add external code generator for RPC clients
    Introduce generate_client.py to generate client code from the Bitcoin
    Core RPC schema. This tool enables developers to easily update and
    maintain client libraries in various languages by automatically
    reflecting changes in the RPC interface.
    02c68c3db1
  4. Add Python client template using Jinja2
    Include the python_client.jinja2 template in the code generator. This
    template defines the structure for the generated Python RPC client and
    integrates with generate_client.py to reflect RPC schema changes
    automatically.
    7a4108c30e
  5. Add C++ client test harness and template
    Introduce main.cpp as a test program to verify the generated C++
    RPC client template. This commit adds a minimal harness that instantiates
    BitcoinRPCClient, calls getblockchaininfo, and prints the result. The
    template and test program help ensure that our C++ client code integrates
    correctly with libcurl and JsonCpp, aiding further development and testing.
    3be8d37446
  6. Add tests for generate_client.py and Jinja templates
    Introduce unit tests for the generate_client.py script to verify
    correct code generation for both C++ and Python clients. These tests
    ensure that the script produces the expected output based on a sample
    schema, enhancing the reliability of the code generation process.
    aaba9b27d4
  7. DrahtBot commented at 7:16 pm on March 25, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32140.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  8. DrahtBot added the label Scripts and tools on Mar 25, 2025
  9. DrahtBot added the label CI failed on Mar 25, 2025
  10. DrahtBot commented at 7:29 pm on March 25, 2025: contributor

    🚧 At least one of the CI tasks failed. Debug: https://github.com/bitcoin/bitcoin/runs/39395957729

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  11. laanwj commented at 4:05 am on March 26, 2025: member
    /schema.json probably wasn’t intended to be in this PR.
  12. Remove schema.json file
    Remove the static schema.json file since the generator now retrieves the
    latest schema dynamically via bitcoin-cli. This change prevents stale
    data and reduces repository clutter.
    44fbc9d83d
  13. EniRox001 commented at 11:04 am on March 26, 2025: none

    /schema.json probably wasn’t intended to be in this PR.

    Yes, redundant now that latest schema is retrieved each time, pushed the change


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-03-28 15:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me