kernel: add btck_witness_stack_to_bytes #36911

Closed
yuvicc wants to merge 2 commits from kernel-witness-stack-to-bytes into master AGit
yuvicc commented 2026-11-19 05:13:29 +00:00 (Migrated from github.com)

I agree the O(n²) cost of reading a whole witness with btck_witness_stack_get_item_at needs fixing. I'm less sure a new callback type is the best way to do it which #36909 is doing. Instead we could expose the witness's serialized bytes and let the bindings or caller decide how to decode them, this also follows the *_to_bytes functions we already have in the API.

I agree the O(n²) cost of reading a whole witness with `btck_witness_stack_get_item_at` needs fixing. I'm less sure a new callback type is the best way to do it which #36909 is doing. Instead we could expose the witness's serialized bytes and let the bindings or caller decide how to decode them, this also follows the `*_to_bytes` functions we already have in the API.
DrahtBot commented 2026-11-19 05:13:38 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Approach NACK stickies-v

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36909 (kernel: add btck_witness_stack_get_items by pzafonte)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36911 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36911.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36911#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Approach NACK | [stickies-v](https://github.com/bitcoin/bitcoin/pull/36911#issuecomment-7165076305) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36909](https://github.com/bitcoin/bitcoin/pull/36909) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36909.svg"></sub> (kernel: add btck_witness_stack_get_items by pzafonte) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Writer fragmentation and partial-failure semantics are undocumented

Like the existing transaction and block serializers, btck_witness_stack_to_bytes leaves callers without guidance on accumulating fragments or handling -1 after partial output.

Location: src/kernel/bitcoinkernel.h:1889 (head).

🔵 Suggestion · Witness serialization tests miss writer rejection

The witness serialization tests cover only successful writes, so reporting success after writer rejection would go undetected even though the current implementation correctly returns -1.

Location: src/test/kernel/test_kernel.cpp:588 (head).

Review coverage was incomplete.


Reviewed: 97d464fec3c4 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3f89681de11a6729a084d29fd96adfa42e3f319bf8f5c8ad91552cdeb231adb4:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:d55f55eda79a98ddaad7f7bec9faca8beb097b99 --> <!-- ralph-head:97d464fec3c4fd9d177b72bc665f8e44143dde89 --> <!-- ralph-case:1b02de4647875ace3d5617ff099d84b1608befb7c482384a28bdd3be8dedf2ae --> **🔵 Suggestion · Writer fragmentation and partial-failure semantics are undocumented** Like the existing transaction and block serializers, `btck_witness_stack_to_bytes` leaves callers without guidance on accumulating fragments or handling `-1` after partial output. Location: [`src/kernel/bitcoinkernel.h:1889`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/97d464fec3c4fd9d177b72bc665f8e44143dde89/src/kernel/bitcoinkernel.h#L1889>) (head). **🔵 Suggestion · Witness serialization tests miss writer rejection** The witness serialization tests cover only successful writes, so reporting success after writer rejection would go undetected even though the current implementation correctly returns `-1`. Location: [`src/test/kernel/test_kernel.cpp:588`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/97d464fec3c4fd9d177b72bc665f8e44143dde89/src/test/kernel/test_kernel.cpp#L588>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`97d464fec3c4`](<https://git.fish.foo/bitcoin/bitcoin/commit/97d464fec3c4fd9d177b72bc665f8e44143dde89>) · [Review report](<https://review.fish.foo/report/36911-97d464fec3c4fd9d177b72bc665f8e44143dde89-2832-1.html>) · [Previous reports](<https://review.fish.foo/traces/36911/>) [permalink](<https://review.fish.foo/report/36911-97d464fec3c4fd9d177b72bc665f8e44143dde89-2832-1.html>)
pzafonte commented 2026-11-19 18:58:25 +00:00 (Migrated from github.com)

Yuvicc, thanks for putting this up. It looks like it comes down to where the items are decoded. btck_witness_stack_get_items decodes them in the kernel and returns pointers that are valid for the duration of the existence of the witness stack and btck_witness_stack_to_bytes returns the serialization and each binding would decode the items themselves.

What I'm trying to accomplish is to make an efficient form of the design that the API already has (btck_witness_stack_get_item_at) and to decode once in the kernel so that consumers don't need to reimplement consensus parsing.

Yuvicc, thanks for putting this up. It looks like it comes down to where the items are decoded. `btck_witness_stack_get_items` decodes them in the kernel and returns pointers that are valid for the duration of the existence of the witness stack and `btck_witness_stack_to_bytes` returns the serialization and each binding would decode the items themselves. What I'm trying to accomplish is to make an efficient form of the design that the API already has (`btck_witness_stack_get_item_at`) and to decode once in the kernel so that consumers don't need to reimplement consensus parsing.
yuvicc commented 2026-11-20 05:33:47 +00:00 (Migrated from github.com)

It looks like it comes down to where the items are decoded. btck_witness_stack_get_items decodes them in the kernel and returns pointers that are valid for the duration of the existence of the witness stack and btck_witness_stack_to_bytes returns the serialization and each binding would decode the items themselves.

Thanks, that's a fair summary. With the switch to btck_witness_stack_get_items, there's no callback anymore, which addresses most of my original concern. I'd say the trade-off comes down to:

  • *_get_items: items are decoded once in the kernel, so consumers don't parse anything, and the returned pointers stay valid as long as the witness stack does. It's tied to the kernel's own representation of the witness.
  • *_to_bytes: the witness comes out as a single blob that can be hashed, stored or forwarded as is, and it follows the existing *_to_bytes functions.

I'll leave this open for now so other reviewers can weigh in on which approach they'd prefer for the API.

> It looks like it comes down to where the items are decoded. `btck_witness_stack_get_items` decodes them in the kernel and returns pointers that are valid for the duration of the existence of the witness stack and `btck_witness_stack_to_bytes` returns the serialization and each binding would decode the items themselves. Thanks, that's a fair summary. With the switch to `btck_witness_stack_get_items`, there's no callback anymore, which addresses most of my original concern. I'd say the trade-off comes down to: - `*_get_items`: items are decoded once in the kernel, so consumers don't parse anything, and the returned pointers stay valid as long as the witness stack does. It's tied to the kernel's own representation of the witness. - `*_to_bytes`: the witness comes out as a single blob that can be hashed, stored or forwarded as is, and it follows the existing `*_to_bytes` functions. I'll leave this open for now so other reviewers can weigh in on which approach they'd prefer for the API.
stickies-v commented 2026-11-25 14:18:31 +00:00 (Migrated from github.com)

Approach NACK, I think we can ~keep the current interface and let the implementation handle this as per https://github.com/bitcoin/bitcoin/pull/36909#discussion_r4696210491.

Also, the complexity is still O(n) on master currently, so this should at the very least be drafted until/when #36895 or equivalent is merged.

Approach NACK, I think we can ~keep the current interface and let the implementation handle this as per https://github.com/bitcoin/bitcoin/pull/36909#discussion_r4696210491. Also, the complexity is still O(n) on master currently, so this should at the very least be drafted until/when #36895 or equivalent is merged.
yuvicc commented 2026-11-25 15:33:52 +00:00 (Migrated from github.com)

Closing for now.

Closing for now.
stringintech commented 2026-11-30 15:00:25 +00:00 (Migrated from github.com)

I ran some numbers that might be relevant to whether a to_bytes is worth having in addition to (not instead of) the per-item API from #36909.

Since #36909 discussion is now leaning towards the witness stack copying the items into its own buffer (diff), I benchmarked that version from the Go bindings on mainnet block 840000 (3765 inputs, 6826 witness items), reading every witness item of every input:

  • creating that witness stack for each input and reading it with one get_item_at call per item: 2.53 ms
  • one to_bytes call per input, building a witness stack with its own item index in Go, and reading items from that: 1.25 ms (about 50% less)

The saving comes from crossing between Go and C once per input instead of once per item. I put the to_bytes on the input (btck_transaction_input_witness_to_bytes) since that witness stack no longer holds a CScriptWitness, so it can still reuse CScriptWitness::Serialize. So I'm wondering if it makes sense to offer both: the per-item API as the default, so consumers don't have to parse anything, and a to_bytes for bindings that would rather parse the format themselves than cross the language boundary for every item.

I ran some numbers that might be relevant to whether a `to_bytes` is worth having in addition to (not instead of) the per-item API from #36909. Since #36909 discussion is now leaning towards the witness stack copying the items into its own buffer ([diff](https://github.com/bitcoin/bitcoin/pull/36909#discussion_r4739086014)), I benchmarked that version from the Go bindings on mainnet block 840000 (3765 inputs, 6826 witness items), reading every witness item of every input: - creating that witness stack for each input and reading it with one `get_item_at` call per item: 2.53 ms - one `to_bytes` call per input, building a witness stack with its own item index in Go, and reading items from that: 1.25 ms (about 50% less) The saving comes from **crossing between Go and C once per input instead of once per item**. I put the `to_bytes` on the input (`btck_transaction_input_witness_to_bytes`) since that witness stack no longer holds a `CScriptWitness`, so it can still reuse `CScriptWitness::Serialize`. So I'm wondering if it makes sense to offer both: the per-item API as the default, so consumers don't have to parse anything, and a `to_bytes` for bindings that would rather parse the format themselves than cross the language boundary for every item.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36911
No description provided.