Skip to content

Add coverage for operations on primitive types and introduce JSON schemas#14

Merged
stringintech merged 7 commits into
mainfrom
jsonschema
May 28, 2026
Merged

Add coverage for operations on primitive types and introduce JSON schemas#14
stringintech merged 7 commits into
mainfrom
jsonschema

Conversation

@stringintech

@stringintech stringintech commented May 14, 2026

Copy link
Copy Markdown
Owner

Details included in commit descriptions.
Tested pre-release v0.0.4-alpha.1 in stringintech/go-bitcoinkernel#12 (comment).


Thank you @janb84 for your earlier work on JSON schemas in #10. I worked on the structure a bit to enforce stricter test suite validation (according to my earlier #11 (comment)) and making it easier to integrate the schemas for runtime handler response validation and method spec generation.

Also change values to better titles and adapt runner to use both title and filename

@stringintech stringintech left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Left some notes/questions that I would appreciate the reviewers input on.

Comment thread testdata/block_header.json Outdated
Comment thread docs/schemas/btck_script_pubkey_create.response.json
Comment thread testdata/block_hash.json

@stickies-v stickies-v left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK. Having coverage for operations on primitive types significantly reduces the amount of unit tests each binding needs to implement, when they use this project.

Didn't review the json spec in too much detail, but format seems sensible. Always nice to have things more structured, just slightly worried too much verbosity slows down development velocity, but i suppose with AI tooling that's not really a concern anymore.

Updated stickies-v/py-bitcoinkernel#42 without significant issues (except the >80 bytes header one, which I think should be skipped).

Comment thread docs/methods-spec.md
Comment thread testdata/block_header.json Outdated
Comment thread docs/schemas/btck_script_pubkey_create.response.json
@stringintech stringintech force-pushed the jsonschema branch 3 times, most recently from b8427ae to cf0e26f Compare May 16, 2026 14:47
@stringintech stringintech linked an issue May 16, 2026 that may be closed by this pull request
@stringintech

Copy link
Copy Markdown
Owner Author

Thank you for the review @stickies-v!
New pre-release which addresses your comments and #15 can be tested using v0.0.4-alpha.2 (compare).

@stickies-v

Copy link
Copy Markdown
Contributor

LGTM. I was on the fence about the JSON schemas initially, but I think the fact that it better allows ensuring that the documentation matches the tests you ship is pretty cool, and worth the complexity.

Didn't review in detail, but tests are green now in stickies-v/py-bitcoinkernel#42 without having to make any awkward changes.

@stringintech

stringintech commented May 18, 2026

Copy link
Copy Markdown
Owner Author

Force pushed to address #14 (comment) and drop the last commit (cf0e26f) in order to address #15 in a separate PR. Tested pre-release v0.0.4-alpha.3 in stringintech/go-bitcoinkernel#12 (comment) - shouldn't need testing if you already tested with alpha.2.

New suites added for operations on block, block hash, block header, script pubkey, transaction, transaction input, transaction output and txid.

Chain suite refactored to return ref for `btck_block_tree_entry_get_block_hash` test cases and call `btck_block_hash_to_bytes` for asserting its value.

Existing suites refactored to call destroy as soon as objects (refs) no longer needed and use `"expected_response": {}` where both `result` and `error` are expected to be null.
@stringintech stringintech force-pushed the jsonschema branch 2 times, most recently from c3dbed0 to eccebaf Compare May 27, 2026 10:12

@janb84 janb84 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK eccebaf

Looks good, doesn't need much to get working on BitcoinKernel.NET.

Of course I like the addition of schemas and schema validaton.
The generated methods-spec.md i think will be a helpful reference.

Solid improvements!

Comment thread docs/schemas/shared.json
Comment thread docs/schemas/shared.json
Comment thread runner/runner.go
@stringintech

Copy link
Copy Markdown
Owner Author

Force pushed (compare) to add the missing btck_block_header_to_bytes request and response schemas along with a serialization round-trip test case in block_header.json. The response schemas for btck_block_to_bytes, btck_transaction_to_bytes, and btck_script_pubkey_to_bytes now include a GenericErrorResponse variant to more accurately reflect the C API, even though the error path is not exercised in the test suites (and cannot be exercised easily). Ref param descriptions across all schemas no longer mention the creating function - that pattern was dropped because refs can come from multiple sources (create, copy, etc.).

Tested pre-release v0.0.4-alpha.pr14.4 in stringintech/go-bitcoinkernel#12 (comment).

stringintech and others added 5 commits May 27, 2026 14:50
Adds suite-schema.json which is used to validate each test suite in testdata dir against. Adds a separate schema file per method which is referenced by the root schema file suite-schema.json.

Response schema files are also introduced separately (and referenced by their parent method schema file). This allows the runner to later only embed the response schemas for handler response validation.

A validator command line tool is written in cmd/suite-validate which performs the testdata suites validation against the root schema and depends on a third party library (github.com/santhosh-tekuri/jsonschema/v6).

Co-authored-by: janb84 <githubjanb.drainer976@passmail.net>
Now the runner binary embeds the response schema files and uses the corresponding method response schema for a more strict validation of an incoming response from handler.
Adds a generator in cmd/specgen that deterministically generates a markdown file documenting methods based on method schema files.

Now handle-spec points to the generated document using the generator.
@stringintech

Copy link
Copy Markdown
Owner Author

Thanks for your review @janb84! Marked error as required for GenericErrorRepsonse as suggested in #14 (comment) and in the response schema for the btck_script_pubkey_verify method.
Latest pre-release available at v0.0.4-alpha.pr14.5.

@janb84 janb84 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 025fbce

LGTM !

@stringintech stringintech merged commit 98e416b into main May 28, 2026
4 checks passed
@stringintech stringintech deleted the jsonschema branch May 28, 2026 11:21
sedited added a commit to bitcoin/bitcoin that referenced this pull request Jun 1, 2026
…create` functions

570a627 kernel: assert invalid buffer preconditions in `btck_*_create` functions (stringintech)

Pull request description:

  The kernel API appears to use `nullptr` returns to report failures that callers may reasonably want to recover from: malformed serialized input, object construction failures, chainstate/load failures, and similar runtime conditions.

  The raw create-function buffer checks seem to be a different case. A failure of `ptr == nullptr && len > 0` does not indicate malformed input data or a failure encountered while deserializing or constructing the requested object. Returning `nullptr` for these checks widens the recoverable error surface with cases that are better treated as programmer errors, similar to other asserted preconditions in this API such as invalid indices and impossible enum/flag states.

  This change switches those buffer argument checks from `nullptr` returns to assertions in `btck_transaction_create`, `btck_script_pubkey_create`, `btck_block_create`, `btck_block_header_create`, and `btck_chainstate_manager_options_create`. `btck_block_header_create` additionally asserts the pre-existing documented length contract (must be 80 bytes). These functions still return `nullptr` when the provided bytes cannot be parsed or when object creation fails during processing.

  I ended up looking at this while working on the `kernel-bindings-tests` spec/schema for `btck_script_pubkey_create`, where treating this path as a regular error did not seem like the right contract: stringintech/kernel-bindings-tests#14 (comment).

ACKs for top commit:
  stickies-v:
    ACK 570a627
  janb84:
    ACK 570a627
  w0xlt:
    ACK 570a627
  sedited:
    ACK 570a627

Tree-SHA512: 064d834abe0c27245a144e5290bbeeb510daf9e4d50bb3a8e50bd8a0bf897b3dcf6ad5acfcabf1d8110da120e5e014ee3aea0241c0f181a21c6f3c14dc452ade
rustaceanrob pushed a commit to 2140-dev/bitcoin that referenced this pull request Jun 5, 2026
…btck_*_create` functions

570a627 kernel: assert invalid buffer preconditions in `btck_*_create` functions (stringintech)

Pull request description:

  The kernel API appears to use `nullptr` returns to report failures that callers may reasonably want to recover from: malformed serialized input, object construction failures, chainstate/load failures, and similar runtime conditions.

  The raw create-function buffer checks seem to be a different case. A failure of `ptr == nullptr && len > 0` does not indicate malformed input data or a failure encountered while deserializing or constructing the requested object. Returning `nullptr` for these checks widens the recoverable error surface with cases that are better treated as programmer errors, similar to other asserted preconditions in this API such as invalid indices and impossible enum/flag states.

  This change switches those buffer argument checks from `nullptr` returns to assertions in `btck_transaction_create`, `btck_script_pubkey_create`, `btck_block_create`, `btck_block_header_create`, and `btck_chainstate_manager_options_create`. `btck_block_header_create` additionally asserts the pre-existing documented length contract (must be 80 bytes). These functions still return `nullptr` when the provided bytes cannot be parsed or when object creation fails during processing.

  I ended up looking at this while working on the `kernel-bindings-tests` spec/schema for `btck_script_pubkey_create`, where treating this path as a regular error did not seem like the right contract: stringintech/kernel-bindings-tests#14 (comment).

ACKs for top commit:
  stickies-v:
    ACK 570a627
  janb84:
    ACK 570a627
  w0xlt:
    ACK 570a627
  sedited:
    ACK 570a627

Tree-SHA512: 064d834abe0c27245a144e5290bbeeb510daf9e4d50bb3a8e50bd8a0bf897b3dcf6ad5acfcabf1d8110da120e5e014ee3aea0241c0f181a21c6f3c14dc452ade
(cherry picked from commit 19e4533)
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.

3 participants