Skip to content

kernel: add btck_witness_stack_to_bytes - #36911

Closed
yuvicc wants to merge 2 commits into
bitcoin:masterfrom
yuvicc:kernel-witness-stack-to-bytes
Closed

yuvicc wants to merge 2 commits into
bitcoin:masterfrom
yuvicc:kernel-witness-stack-to-bytes

Conversation

@yuvicc

@yuvicc yuvicc commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

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.

Expose the consensus serialization of a witness stack, so all of its
items can be read in a single call.
@DrahtBot

DrahtBot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

@pzafonte

Copy link
Copy Markdown
Contributor

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

yuvicc commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

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

stickies-v commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Approach NACK, I think we can ~keep the current interface and let the implementation handle this as per #36909 (comment).

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

yuvicc commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing for now.

@yuvicc yuvicc closed this Nov 25, 2026
@stringintech

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants