Implement to_data for block and transaction views.#1905
Merged
Conversation
evoskuil
reviewed
Jul 7, 2026
|
|
||
| // Strip witness: the header and transaction count are unaffected. | ||
| sink.write_bytes({ buffer_->data(), std::next(buffer_->data(), | ||
| header::serialized_size()) }); |
Member
There was a problem hiding this comment.
Use
void write_bytes(const uint8_t* data, size_t size) NOEXCEPT override;| // Witness can be stripped but never added (mirrors chain::transaction). | ||
| if (witness && is_segregated()) | ||
| { | ||
| sink.write_bytes({ tx_ptr_, std::next(tx_ptr_, size_) }); |
Member
There was a problem hiding this comment.
Use
void write_bytes(const uint8_t* data, size_t size) NOEXCEPT override;| // One segregated (witness) transaction and one legacy (non-witness) | ||
| // transaction in a single block, so the per-transaction witness strip is | ||
| // exercised both ways within one to_data(false) call. | ||
| const block mixed |
| uint32_t version() const NOEXCEPT; | ||
| uint32_t locktime() const NOEXCEPT; | ||
| size_t serialized_size(bool witness) const NOEXCEPT; | ||
| void to_data(writer& sink, bool witness) const NOEXCEPT; |
Member
There was a problem hiding this comment.
Move up to own section and add complete complement of overloads (see block_view and all chain:: objects):
/// Serialization.
data_chunk to_data(bool witness) const NOEXCEPT;
void to_data(std::ostream& stream, bool witness) const NOEXCEPT;
void to_data(writer& sink, bool witness) const NOEXCEPT;| sink.write_bytes({ tx_ptr_, std::next(tx_ptr_, version_size) }); | ||
| sink.write_bytes({ body, body_end }); | ||
| sink.write_bytes({ std::next(tx_ptr_, size_ - locktime_size), | ||
| std::next(tx_ptr_, size_) }); |
Member
There was a problem hiding this comment.
Same here... if you are going to do pointer and size math, use the pointer/size overload, avoiding the unnecessary intermediate data_slice.
block_view::to_data(writer&, bool) was an unimplemented stub, so a block_view could report its serialized size but not emit its bytes; transaction_view had no to_data at all. Implement transaction_view::to_data to write the view, stripping the witness when requested: witness can be downgraded but never added (mirroring chain::transaction), so a witness-true call on a segregated view writes the held bytes, and otherwise the marker and flag sentinels and the witness data are dropped while version, inputs, outputs and locktime are written unchanged. Implement block_view::to_data as the twin of serialized_size: the witnessed form is the original buffer, otherwise write the header and transaction count and strip each transaction view. Add round-trip tests asserting each view serializes identically to the equivalent chain::block or chain::transaction for witnessed, non-witness and stripped-source cases, including a multi-transaction block.
…tion_view serialization section, block2c mock, overload test.
echennells
force-pushed
the
block-view-to-data
branch
from
July 9, 2026 05:18
ef1f00e to
681552b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
block_view::to_data(writer&, bool)was an unimplemented stub (it asserted "not implemented"), so ablock_viewcould report its serialized size but not emit its bytes;transaction_viewhad noto_dataat all. This implements both, with witness stripping.Witness can be downgraded but never added, mirroring
chain::transaction: awitness = truecall on a witnessed view writes the held bytes; otherwise the witness is stripped on write.block_view::to_datamirrorsserialized_size: the witnessed form is the original buffer (a single write), otherwise it writes the header, the transaction count, and each transaction view stripped.Round-trip tests assert the view serializes identically to the equivalent
chain::blockfor witnessed, non-witness and stripped-source blocks. Full system test suite green.