Review report

core: PR #35558

4 minor findings · 5 suggestions

Earlier concerns neither republished nor resolved: 7 minor findings · 2 suggestions. See the Run archive for their assessments.

Findings

Link

[minor] · Queued-byte estimates omit data on FreeBSD, OpenBSD, and Windows

These platforms can undercount queued bytes, causing the prefill heuristic to select a message that crosses an additional TCP window.

Location: src/util/sock.cpp:121 (head).

Details and suggested fix

GetOSBytesQueued() is documented to return unsent plus unacknowledged bytes, which WindowBytesTotalAndAvailable() adds to the application-queue size. The FreeBSD branch uses ioctl(TIOCOUTQ), a tty request; FreeBSD documents FIONWRITE for a socket send queue, as used by the NetBSD branch. A mailing-list report says TIOCOUTQ on a socket can fail with EOPNOTSUPP. If it fails, the call logs an error and returns 0, as if the queue were empty. On OpenBSD, tcpi_snd_nxt - tcpi_snd_una, and on Windows, BytesInFlight, count sent but unacknowledged bytes, not data accepted by the kernel but still unsent. SocketSendData() has already removed that data from the application queue measured by GetSendQueueSize(), so it is counted nowhere. The result in all three cases is an understated backlog, which can make SendCompactBlock() choose a prefilled message even when it crosses a window boundary that the plain message would not. Use FIONWRITE on FreeBSD. Where unsent kernel bytes cannot be measured, return a failure distinct from 0 and skip prefilling. The FreeBSD runtime behavior is based on documentation and a mailing-list report, not this checkout.


Link

[minor] · The optimistic reconstruction path drops prefill candidates

When optimistic reconstruction succeeds, the node announces the block with only the coinbase prefilled, omitting transactions it had to obtain separately.

Location: src/net_processing.cpp:3882 (head).

Details and suggested fix

ProcessCompactBlockTxns() saves PrefillCandidates() before calling ProcessBlock(), but the CMPCTBLOCK handler has a second reconstruction path. That path is used when the in-flight download limit for the block is reached or the announcing peer has reached its in-transit limit. It fills a local tempBlock and, on success, passes the block to ProcessBlock() without saving tempBlock.PrefillCandidates(). When the block extends the tip, NewPoWValidBlock() finds no candidate set for its hash and builds only the coinbase-only message. Transactions obtained from the extra pool or received as non-mempool prefills are therefore not prefilled for this node's high-bandwidth peers. Save the candidates on this path before ProcessBlock(), ideally through a helper shared with ProcessCompactBlockTxns(). NewPoWValidBlock() uses candidates only for a block whose hash matches a block that passed CheckBlock(), so saving them early is as safe here as on the existing path.


Link

[minor] · The kernel and application queues are sampled at different times

A send between the queue reads is counted in neither sample, understating the backlog and occasionally selecting a prefilled message that crosses an extra window.

Location: src/net.cpp:4264 (head).

Details and suggested fix

WindowBytesTotalAndAvailable() reads the kernel queue under m_sock_mutex, releases that lock, and then reads the application queue through GetSendQueueSize() under cs_vSend. SocketSendData() holds cs_vSend; if it moves bytes from vSendMsg and the transport into the kernel between the reads, those bytes appear in neither sample. The backlog is then understated, so the prefilled message can be chosen even though it crosses one more window than the plain message. For example, with a 10,000-byte window and 6,000 bytes moved in the gap, both a 3,000-byte and a 7,000-byte message appear to fit, but only the first does. This requires a non-empty send queue and a send during the short gap, so the choice is occasional. Take cs_vSend first and then m_sock_mutex, the order used by SocketSendData(), and read both queues while holding them.


Link

[minor] · Compact-block nonces draw from the global RNG on worker threads

Under fuzzing, worker scheduling can change the order of global RNG draws, so the same input can take different paths; production nonces remain random.

Location: src/net_processing.cpp:2272 (head).

Details and suggested fix

NewPoWValidBlock() now calls FastRandomContext().rand64() inside the std::async worker threads. A default FastRandomContext seeds itself from GetRandHash() on first use, so each worker draws from the process-wide RNG at a scheduler-dependent time. The base code drew the nonce on the calling thread. Fuzz targets seed that RNG with SeedRandomStateForTest(SeedRand::ZEROS) for reproducibility. Targets that reach NewPoWValidBlock(), such as process_message after JumpOutOfIbd(), also draw from the same RNG on the main thread, for example through salted hashers and MaybeSendPing(). Worker draws can occur before or after those draws, changing the main thread's values between runs of the same input. The futures are waited on only when a high-bandwidth peer is served, so a worker can still be running while validation continues. Draw both nonces on the calling thread, from m_rng or a local FastRandomContext, before starting the tasks, and capture them by value. This preserves asynchronous serialization. No specific input that diverges between runs was constructed.


Optional improvements

Link

[suggestion] · The TCP window check measures the local proxy connection, not the peer path

For proxied peers, the measured window does not represent the path to the peer, so the check cannot reliably limit prefill size; the selection frequency is unconfirmed.

Location: src/net.cpp:4251 (head).

Details and suggested fix

For outbound connections through -proxy or -onion, ConnectThroughProxy() returns the socket to the SOCKS5 proxy, which ConnectNode() stores as m_sock. I2P peers use the SAM bridge connection, and inbound onion peers arrive on the Tor daemon's local connection. WindowBytesTotalAndAvailable() reads the congestion window, receive window, and queued bytes from that local hop. SendCompactBlock() treats any non-zero window as sufficient to make the comparison. The local hop's window does not describe the Tor or I2P path to the peer, so this check does not limit prefill size on these connections; a large local window can allow the entire candidate set. With a Unix-domain proxy socket, TCP_INFO fails, so peers do not get prefills and each attempt logs an error. Decide and document a policy for connections that are not direct TCP to the peer, such as skipping prefilling or using a fixed conservative byte budget. Loopback window sizes and Tor flow control were not measured, so how often the prefilled message is selected on these connections is unconfirmed.


Link

[suggestion] · Deterministic tests do not cover prefill candidates or window selection

A regression in prefill encoding, candidate tracking, or message selection would not be caught by the current unit tests.

Location: src/net_processing.cpp:2348 (head).

Details and suggested fix

All updated blockencodings_tests cases pass an empty candidate set, and no test calls PrefillCandidates(). windows_used is a local lambda inside SendCompactBlock(), and no test covers the choice between the prefilled and plain messages. Only the cmpctblock fuzz target builds compact blocks with non-empty candidate sets. Consequently, unit tests would not catch regressions in relative-index encoding, candidate tracking for extra-pool hits, non-mempool prefills or requested transactions, or the boundary rule. Add a round-trip test with candidates such as {2, 5} that checks the prefilledtxn indexes and reconstructed block. Assert PrefillCandidates() after ReceiveWithExtraTransactions and after FillBlock() with missing transactions. Move windows_used into a testable helper and cover an exact fit, one byte over, and equal window counts.


Link

[suggestion] · Logging-only prefill bookkeeping runs for every reconstruction

Every prefilled transaction now incurs size computation and map insertion even though that bookkeeping is used only for debug logging; the runtime cost was not measured.

Location: src/blockencodings.cpp:105 (head).

Details and suggested fix

The base code computed ComputeTotalSize(), prefilled_size, and the leftover_prefills map only when CMPCTBLOCK debug logging was enabled. The current code performs this work for every prefilled transaction while holding pool->cs, though leftover_prefills is read only inside the later debug_log block. This work grows with the number of prefills, which the change aims to increase. Keep the GetIter() check and prefill_candidates.insert() unconditional, but guard the size computation, size counters, and leftover_prefills insertion with debug_log.


Link

[suggestion] · Expected TCP_INFO failures produce warning- and error-level logs

Unsupported platforms and proxy connections can emit warning- or error-level lines on every block, making normal operation look faulty.

Location: src/util/sock.h:371 (head).

Details and suggested fix

TCPInfo logs a warning on platforms without an implementation, such as the #else branch for illumos, and logs an error whenever the query fails, including on Unix-domain proxy sockets where TCP_INFO cannot work. GetOSBytesQueued() also logs errors; on FreeBSD, its TIOCOUTQ call is expected to fail every time. WindowBytesTotalAndAvailable() constructs a TCPInfo for every high-bandwidth peer on each block with a prefilled message, so these expected conditions produce repeated warning- and error-level lines. Log these at debug level or record once per socket that the information is unavailable.


Link

[suggestion] · The compact-block getdata path updates pindexBestHeaderSent

Replying to getdata(MSG_CMPCT_BLOCK) now changes pindexBestHeaderSent; after a reorg, this can cause the requesting peer's next announcement to use INV instead of a compact-block announcement.

Location: src/net_processing.cpp:2392 (head).

Details and suggested fix

The change routes the getdata(MSG_CMPCT_BLOCK) reply in ProcessGetBlockData() through SendCompactBlock(), which sets pindexBestHeaderSent; the base reply did not update that field. Usually this is harmless because a peer requesting the block already knows its header. However, m_most_recent_pow_block is replaced only by blocks that extend the current tip, so after a reorg it can hold a block no longer on the active chain. If a peer requests that block, pindexBestHeaderSent moves back to the stale branch. The next announcement to that peer can then fail PeerHasHeader(pindex->pprev) and fall back to INV, unless the peer's own announcements cover it. Only the requesting peer is affected, and the window is narrow. Either document this behavior or have the getdata path skip the update.


Design and approach

The prefill data supports prefilling, but it does not show that TCP-window-aware selection improves propagation over unconditional or fixed-cap prefilling. Measure those approaches against each other for propagation latency and redundant bytes; if the difference is small, use a fixed byte cap to avoid transport introspection and its platform-specific complexity. The reported reconstruction rates compare window-limited prefilling with coinbase-only prefilling and do not measure the window limit's effect. https://github.com/bitcoin/bitcoin/pull/35558

Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.