Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
7b0104b to
0b01ce0
Compare
A getdata for MSG_BLOCK or MSG_CMPCT_BLOCK sent the stored record without checking that it was the block asked for. A record holding another well-formed block went out under the requested hash. Core v31.1 reads these items with ReadBlock(..., inv.hash). It refuses a header that misses its proof of work, on signet a block whose solution does not verify, and a header that hashes to another hash. This node asks the same, in that order. Off signet, only the 80 header octets are parsed. A MSG_WITNESS_BLOCK is not checked: Core reads it with ReadRawBlock, which checks no hash. bitcoin/bitcoin#36548, at its head 0b01ce05e1, checks the header's work and hash before it strips a MSG_BLOCK, reads a signet MSG_BLOCK with ReadBlock, and serves a MSG_WITNESS_BLOCK as read. A refused read logs "Cannot load block from disk" as an error and drops the peer, as Core does. A record that does not parse gets the same answer; it raised and kept the peer. Tests pin the order: a forged signet record under another hash is refused for its solution, and a forged signet record missing its work for its work. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: Ferdinando Ametrano <[email protected]> Signed-off-by: pmazzocchi <[email protected]>
|
tACK 0b01ce0 I checked that the new Each of seven mutants of 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 |
4ddf35f to
d36943b
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
fametrano
left a comment
There was a problem hiding this comment.
tACK d36943b
The PR's own changes are the ones I tested on 0b01ce0: the range-diff only moves three bench includes into the earlier bench commits and places the new tests before master's new blockmanager_tests case, 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, ReadBlockWithoutWitness matches ReadBlock plus TX_NO_WITNESS for 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_BLOCK equals 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_BLOCK equals getblock. blockmanager_tests, p2p_getdata, p2p_segwit and p2p_compactblocks pass. I did not re-run the mutants or the timings. Scripts and output: https://gist.github.com/fametrano/740bc1f88f3d5967023c920824bafd70/29865a5e048c740b1ac50fbc311e2f1607bfaa3d
d36943b to
5208112
Compare
|
Rebased + applied some minor Fable 5.2 simplification suggestions. |
085c2eb to
28a0c6a
Compare
optout21
left a comment
There was a problem hiding this comment.
Concept ACK (28a0c6a)
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.
| full = bytes.fromhex(node.getblock(blockhash, 0)) | ||
| stripped = from_binary(CBlock, full).serialize(with_witness=False) | ||
| assert_greater_than(len(full), len(stripped)) | ||
| MESSAGEMAP[b"block"] = RawBlockMessage |
There was a problem hiding this comment.
be13ee8 test: cover block serving serialization:
This overwrite is not restored. Can later tests be distracted by this?
| if (const auto block_data{m_chainman.m_blockman.ReadRawBlock(block_pos)}) { | ||
| } else if (const bool strip_witness{inv.IsMsgBlk() && !m_chainparams.GetConsensus().signet_blocks}; | ||
| inv.IsMsgWitnessBlk() || strip_witness) { | ||
| // Serve MSG_WITNESS_BLOCK directly from on-disk serialization. |
There was a problem hiding this comment.
28a0c6a net: serve
MSG_BLOCKfrom serialized data:
Nit: This also handles MSG_BLOCK
| inv.IsMsgWitnessBlk() || strip_witness) { | ||
| // Serve MSG_WITNESS_BLOCK directly from on-disk serialization. | ||
| // Use ReadBlock below for MSG_BLOCK on Signet to check the block solution. | ||
| const auto& blockman{m_chainman.m_blockman}; |
There was a problem hiding this comment.
28a0c6a net: serve
MSG_BLOCKfrom serialized data:
Nit: 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))
df99e2d to
8a827ef
Compare
|
Thanks for the review, @optout21! |
| if (!CheckProofOfWork(hash, header.nBits, GetConsensus())) { | ||
| LogError("Errors in block header at %s while reading block", pos.ToString()); | ||
| return util::Unexpected{ReadRawError::IO}; | ||
| } | ||
| if (hash != expected_hash) { | ||
| LogError("GetHash() doesn't match index at %s while reading block (%s != %s)", pos.ToString(), hash.ToString(), expected_hash.ToString()); | ||
| return util::Unexpected{ReadRawError::IO}; | ||
| } |
There was a problem hiding this comment.
1f1d86c node: read non-witness block payloads:
Nit: These checks are verbatim in ReadBlock as well, a common helper could reduce code duplication (it's fine to have CheckSignetBlockSolution in as well, as that's if'ed).
There was a problem hiding this comment.
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.
| const auto hash{header.GetHash()}; | ||
| if (!CheckProofOfWork(hash, header.nBits, GetConsensus())) { | ||
| LogError("Errors in block header at %s while reading block", pos.ToString()); | ||
| return util::Unexpected{ReadRawError::IO}; |
There was a problem hiding this comment.
1f1d86c node: read non-witness block payloads:
Nit: Invalid data read from disk is arguably not an IO error, but data inconsistency. Could use a specialized error here.
There was a problem hiding this comment.
InvalidData may 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.
Check exact `MSG_BLOCK` and `MSG_WITNESS_BLOCK` payloads before and after recent-block cache eviction. Request an older Signet block from disk and check that serving it rejects corruption of its stored solution.
Measure disk reads followed by `TX_NO_WITNESS` message serialization before replacing the full block reader. Measurements from the original branch at `369db2528b` before bitcoin#36895. Witness-free: 1.31 ms/block Witness-bearing: 3.47 ms/block
Check proof of work and the expected block hash before walking the serialized transactions. Compact the retained bytes in the owned buffer so serving can skip witnesses without constructing transactions or computing their cached hashes. Switch the non-witness serving benchmark to the new reader. Compare the result with ordinary decoding followed by non-witness serialization in unit and fuzz tests.
Use `ReadBlockWithoutWitness` for disk-backed `MSG_BLOCK` requests. Keep Signet, cached, witness, compact, and filtered block requests on their existing paths.
Reject Signet in ReadBlockWithoutWitness so callers get an error in release builds. They need ReadBlock to verify the block solution. optout21 pointed out that “Assume does nothing in release mode.” bitcoin#36548 (comment)
8a827ef to
64378eb
Compare
|
Rebased and made |

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