mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 14:05:38 +00:00
kernel: add btck_witness_stack_to_bytes #36911
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36911
Loading…
Reference in a new issue
No description provided.
Delete branch "kernel-witness-stack-to-bytes"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I agree the O(n²) cost of reading a whole witness with
btck_witness_stack_get_item_atneeds 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_bytesfunctions we already have in the API.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.
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:
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.
🔵 Suggestion · Writer fragmentation and partial-failure semantics are undocumented
Like the existing transaction and block serializers,
btck_witness_stack_to_bytesleaves callers without guidance on accumulating fragments or handling-1after 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 reportspermalink
Yuvicc, thanks for putting this up. It looks like it comes down to where the items are decoded.
btck_witness_stack_get_itemsdecodes them in the kernel and returns pointers that are valid for the duration of the existence of the witness stack andbtck_witness_stack_to_bytesreturns 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.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_bytesfunctions.I'll leave this open for now so other reviewers can weigh in on which approach they'd prefer for the API.
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.
Closing for now.
I ran some numbers that might be relevant to whether a
to_bytesis 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:
get_item_atcall per item: 2.53 msto_bytescall 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_byteson the input (btck_transaction_input_witness_to_bytes) since that witness stack no longer holds aCScriptWitness, so it can still reuseCScriptWitness::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 ato_bytesfor bindings that would rather parse the format themselves than cross the language boundary for every item.