mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 15:11:38 +00:00
net: serve MSG_BLOCK by stripping witnesses in place #36548
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!36548
Loading…
Reference in a new issue
No description provided.
Delete branch "l0rinc/msg-block-serving"
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?
Problem: To send a
MSG_BLOCKreply from disk, a node currently decodes the block and serializes it again without witnesses.For
MSG_WITNESS_BLOCK, it already sends the stored bytes directly.Serving
MSG_BLOCKefficiently also matters for the proposed witnessless initial block download. #36476Fix: For
MSG_BLOCKreplies from disk on non-Signet chains, read the block into an owned byte buffer and check the header’s proof of work and expected hash.Walk the serialized transactions directly and strip their witnesses in place.
Signet still uses
ReadBlockto verify the Signet solution.Header checks now run before transaction parsing, so if both the header and transaction data are corrupt, the node may log a different error.
Performance:
block413567The synthetic fixture adds a 512-byte witness item and an empty item to every input.
Related work: This extends the path that sends witness blocks directly from disk. #13151
Earlier work identified unnecessary transaction hashing when serving historical blocks. #13011 discussion · #13098
The implementation also builds on bulk block reads. #31551
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 · The Signet restriction on
ReadBlockWithoutWitnessduplicates reader selectionReadBlockWithoutWitnessmakesProcessGetBlockDatarepeat the Signet restriction and reports unsupported use asReadRawError::IO, although no current caller reaches that case.Location:
src/node/blockstorage.cpp:1099(head).Reviewed:
516378803669· Review report · Previous reportspermalink
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/38833479928/job/116757258223LLM reason (✨ experimental): CI failed because IWYU modified includes in
src/bench/readwriteblock.cpp, and the formatting check detected uncommitted changes.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
tACK
0b01ce05e1I checked that the new
MSG_BLOCKreply is byte for byte the old one. On 214 blocks, the head'sReadBlockWithoutWitnessmatches itsReadBlockplusTX_NO_WITNESS, that old path matches the base's, and btclib, a library I maintain, agrees. The blocks are mainnet 481824, 709632, 774628, 920000 and the bench's 413567, plus 209 regtest blocks with segwit v0 and v1 spends, 1,000 witness items, empty stacks and items, a 3,990,000-byte annex, 300 inputs, 316 outputs, 260 transactions, or no witness. Over P2P, a head node and a base node give the sameMSG_BLOCKandMSG_WITNESS_BLOCKpayloads for all 209: 208 from disk, and the two successive tips from the recent-block cache.Each of seven mutants of
StripBlockWitnessandReadBlockWithoutWitnessfailsblockmanager_tests. They range from keeping the marker or the witnesses to skipping the proof-of-work or hash check. The newp2p_getdatacase also passes against a base node, so it guards the payload format rather than the new path.Building the reply got 7.2x to 9.0x faster than on the base for those five mainnet blocks and the two largest regtest blocks (median of three runs, Release, Apple M5). Peak heap fell from 2.9 to 2.0 MB for 481824 and from 7.9 to 4.0 MB for 774628. I measured with a Boost test that writes each block with
WriteBlockand reads it back both ways, and a functional test running a head and a base node; I can share both as a gist.🐙 This pull request conflicts with the target branch and needs rebase.
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/41212933561/job/124571513411LLM reason (✨ experimental): CI failed because IWYU modified
src/bench/readwriteblock.cppto add missing includes, and the subsequent diff check detected uncommitted changes.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
tACK
d36943b49cThe PR's own changes are the ones I tested on
0b01ce05: the range-diff only moves three bench includes into the earlier bench commits and places the new tests before master's newblockmanager_testscase, and the net diff is unchanged line for line. The new base widens block file positions to 64 bits, which the new read path goes through, so I re-ran the byte-for-byte check rather than carry the old result over.On this head,
ReadBlockWithoutWitnessmatchesReadBlockplusTX_NO_WITNESSfor the same five mainnet blocks and for 263 new regtest blocks, and btclib agrees. The regtest blocks include taproot annexes of up to 3,990,001 bytes, witness stacks of 1,001 items, 260 transactions, 300 inputs and 316 outputs, and three blocks with no witness. Over P2P,MSG_BLOCKequals the test framework's non-witness serialization for all 263 blocks read from disk and for 22 of them while they were the tip.MSG_WITNESS_BLOCKequalsgetblock.blockmanager_tests,p2p_getdata,p2p_segwitandp2p_compactblockspass. I did not re-run the mutants or the timings. Scripts and output: https://gist.github.com/fametrano/740bc1f88f3d5967023c920824bafd70/29865a5e048c740b1ac50fbc311e2f1607bfaa3dRebased + applied some minor Fable 5.2 simplification suggestions.
🐙 This pull request conflicts with the target branch and needs rebase.
reACK
5208112736Since
d36943b4, the rebase and the simplifications l0rinc applied leave the non-test changes identical line for line; only the unit tests, the functional test and the bench changed. The new base widens the block file sizes and constants to 64 bits, with the same values.I re-ran the earlier checks on this head with the same scripts (https://gist.github.com/fametrano/740bc1f88f3d5967023c920824bafd70/29865a5e048c740b1ac50fbc311e2f1607bfaa3d).
ReadBlockWithoutWitnessstill matchesReadBlockplusTX_NO_WITNESSfor the five mainnet and 263 regtest blocks,MSG_BLOCKstill matches the non-witness serialization for all 263 regtest blocks, andblockmanager_tests,p2p_getdata,p2p_segwitandp2p_compactblockspass.🐙 This pull request conflicts with the target branch and needs rebase.
Concept ACK (
28a0c6a4ec)Reviewed by reimplementing and comparing solutions.
Comments:
StripBlockWitnessis pure serialization logic with no storage dependency, but it sits innode::insideblockstorage. Putting it next to the block and transaction serialization code would make it reusable (for example byRESTor a future compact-block path), and its tests wouldn't need node code.The reason to exclude Signet could be explained in more detail.
Signet isn't covered. No test exercises the
MSG_BLOCK-on-Signet branch, which still usesReadBlock. It's cheap to leave untested since it's the old code path, but if the special case stays, a test would stop it from being lost in a later cleanup.Is
ReadBlockWithoutWitnessmore similar toReadBlockorReadRawBlock? It's a combinations; this could be explained.Possible follow-up:
CSerializedNetMsg::datais astd::vector<unsigned char>, soNetMsg::Makecopies the 1–4 MB payload once more. If the message could take ownership of the buffer, that copy would go away for both the witness and non-witness paths.Nit: This also handles
MSG_BLOCKNit: as this alias is introduced, it could also be used 4 lines below, within the same code block.
(
return m_chainman.m_blockman.IsBlockPruned(*pindex))This overwrite is not restored. Can later tests be distracted by this?
Thanks for the review, @optout21!
I rebased and moved the witness-stripping code and tests next to block serialization and made the P2P test restore
MESSAGEMAP. I also added a test that checks that we serve a valid Signet block from disk and refuse it if its solution is damaged.ACK
8a827efb36Thanks for addressing my review comments. Reviewed again, only non-blocking nits found.
Nit: This can happen on incorrect usage, so maybe a proper error in this case would be more appropriate.
Assumedoes nothing in release mode.Nit: Invalid data read from disk is arguably not an
IOerror, but data inconsistency. Could use a specialized error here.Nit: These checks are verbatim in
ReadBlockas well, a common helper could reduce code duplication (it's fine to haveCheckSignetBlockSolutionin as well, as that's if'ed).Sharing that sequence would require passing the decoded block into the helper, I tried it, didn't like the results - you can suggest a version you like if you think we should do it here.
InvalidDatamay be more precise, but the logs already distinguish the failures and the P2P caller handles them the same way - the diff seems simpler as-is.Rebased and made
ReadBlockWithoutWitnessreturn an error on Signet in all builds, thanks @optout21It looks like the fixup is missing for the last fixup commit.
I've reviewed it, LGTM, thanks for reacting. Happy to re-ack after fixup.
That's how I usually review locally, forgot to fold back the fixup, thanks, rebased and folded it back, no other change was done in latest push.
reACK
5163788036(
git range-diff 8a827ef...5163788)