2 suggestions
Optional improvements
[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).
Details and suggested fix
WriterStream may invoke the writer repeatedly for the count, item lengths, and payloads. Any nonzero writer result aborts serialization and becomes -1, rather than being forwarded, with no rollback of bytes already delivered. By contrast, btck_witness_stack_get_item_at calls the writer once and returns its result. The C++ collector already appends fragments correctly. Extend this documentation to tell C and binding callers to process fragments in order without depending on chunk boundaries, and to expect -1 with possible partial output on rejection.
[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).
Details and suggested fix
The explicit ToBytes() assertions and the CheckHandle serialization checks all use write_bytes, whose callback rejects data only if vector insertion throws. Add a direct btck_witness_stack_to_bytes(ws.get(), ...) call using the existing four-item witness and a stateful writer that accepts the first callback and rejects the second. Assert that the function returns -1 and makes no further callbacks. This exercises rejection deterministically without relying on allocation failure.