refactor: store positions as a 64 bits integer in flatfiles #36669

Manually merged
darosior merged 2 commits from 2610_64bit_blockstorage into master 2026-10-26 17:00:27 +00:00 AGit
darosior commented 2026-10-26 16:01:04 +00:00 (Migrated from github.com)

That field is the last 32-bit in flat files management that is only bounded indirectly by callers' usage (unlike the file number for instance). In the spirit of the ongoing effort to avoid unnecessarily using 32 bits integers or platform specific types (for instance #24214, #33724, #34109, #35097, #35616), i was scanning for fields it could be prudent to promote to prevent any possible future misuse. This one is a good candidate because it does not change on-disk representation, since we serialize positions as varints.

That field is the last 32-bit in flat files management that is only bounded indirectly by callers' usage (unlike the file number for instance). In the spirit of the ongoing effort to avoid unnecessarily using 32 bits integers or platform specific types (for instance #24214, #33724, #34109, #35097, #35616), i was scanning for fields it could be prudent to promote to prevent any possible future misuse. This one is a good candidate because it does not change on-disk representation, since we serialize positions as varints.
DrahtBot commented 2026-10-26 16:01:16 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK sedited, hebasto, instagibbs, l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36669 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36669.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36669#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/36669#pullrequestreview-5655445494), [hebasto](https://github.com/bitcoin/bitcoin/pull/36669#pullrequestreview-5655530519), [instagibbs](https://github.com/bitcoin/bitcoin/pull/36669#pullrequestreview-5655567584), [l0rinc](https://github.com/bitcoin/bitcoin/pull/36669#pullrequestreview-5655794995) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited (Migrated from github.com) approved these changes 2026-10-26 16:28:50 +00:00
sedited (Migrated from github.com) left a comment

tACK 48092a9042

Also ran a few extra tests locally to stresss this a bit.

tACK 48092a9042d8fcfd6bb7ae5911a1d53b8746d546 Also ran a few extra tests locally to stresss this a bit.
hebasto (Migrated from github.com) approved these changes 2026-10-26 16:34:34 +00:00
hebasto (Migrated from github.com) left a comment

ACK 48092a9042, I have reviewed the code and it looks OK.

ACK 48092a9042d8fcfd6bb7ae5911a1d53b8746d546, I have reviewed the code and it looks OK.
instagibbs (Migrated from github.com) approved these changes 2026-10-26 16:36:36 +00:00
instagibbs (Migrated from github.com) left a comment

ACK 48092a9042

reproduced on linux only

ACK 48092a9042d8fcfd6bb7ae5911a1d53b8746d546 reproduced on linux only
First-time contributor

Base: 2186232955fa70f47c584fb4973e5d849dcfe0f1
Head: 48092a9042d8fcfd6bb7ae5911a1d53b8746d546

Findings

Minor

Use large-file positioning APIs on 32-bit Linux (src/util/fs_helpers.h:60, head)

On 32-bit Linux, long remains 32 bits. Although the build enables 64-bit off_t, this branch uses fseek and ftell, whose offset and result types are long. Positions above LONG_MAX therefore cannot be reliably sought or reported. Use POSIX large-file positioning APIs such as fseeko and ftello with 64-bit off_t. No current production caller producing an offset above LONG_MAX was identified; ordinary block files have a 128 MiB cap, so production impact is unconfirmed.

Suggestions

Test seeking above UINT32_MAX (src/flatfile.cpp:49, head)

The new path seeks to a flat-file position, but flatfile_open uses positions derived from short strings and the allocation tests use offsets below 200. Add a seek-only test above UINT32_MAX and check that AutoFile::tell() returns the requested position. This exercises the widened seek/tell path without writing a multi-gigabyte file.

Review coverage was incomplete.

Review limitations
  • adversarial_deepseek: Stage did not complete (ValueError).
  • tests: Output failed validation: Finding must identify a changed file: 'src/node/blockstorage.cpp'
  • verifier: The concept candidate's assumption that the 128 MiB block-file limit also bounds cumulative undo-file offsets remains unverified. The retrieved code shows separate undo-size accounting, but the full relevant call path was not available to establish its upper bound.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4c492902f6fea4a58817c95cbe5fb719fecdcac9a41632149276919f687c2992:283fe7d72e2d0733dc3ef17f319aaa3c0a25e70562cdebdc5e12bfea8f38de15 --> Base: `2186232955fa70f47c584fb4973e5d849dcfe0f1` Head: `48092a9042d8fcfd6bb7ae5911a1d53b8746d546` ##### Findings **Minor** **Use large-file positioning APIs on 32-bit Linux** (src/util/fs_helpers.h:60, head) On 32-bit Linux, `long` remains 32 bits. Although the build enables 64-bit `off_t`, this branch uses `fseek` and `ftell`, whose offset and result types are `long`. Positions above `LONG_MAX` therefore cannot be reliably sought or reported. Use POSIX large-file positioning APIs such as `fseeko` and `ftello` with 64-bit `off_t`. No current production caller producing an offset above `LONG_MAX` was identified; ordinary block files have a 128 MiB cap, so production impact is unconfirmed. **Suggestions** **Test seeking above `UINT32_MAX`** (src/flatfile.cpp:49, head) The new path seeks to a flat-file position, but `flatfile_open` uses positions derived from short strings and the allocation tests use offsets below 200. Add a seek-only test above `UINT32_MAX` and check that `AutoFile::tell()` returns the requested position. This exercises the widened seek/tell path without writing a multi-gigabyte file. Review coverage was incomplete. <details> <summary>Review limitations</summary> - adversarial_deepseek: Stage did not complete (ValueError). - tests: Output failed validation: Finding must identify a changed file: 'src/node/blockstorage.cpp' - verifier: The concept candidate's assumption that the 128 MiB block-file limit also bounds cumulative undo-file offsets remains unverified. The retrieved code shows separate undo-size accounting, but the full relevant call path was not available to establish its upper bound. </details> [Full review report](<https://review.fish.foo/traces/36669-48092a9042d8fcfd6bb7ae5911a1d53b8746d546-1378-1.html>)
l0rinc (Migrated from github.com) approved these changes 2026-10-26 16:52:46 +00:00
l0rinc (Migrated from github.com) left a comment

tested ACK 48092a9042

About time we got rid of the 32-bit file offsets - checked the CBlockIndex layout with Clang and GCC, the padding nit can be addressed in a follow-up.

tested ACK 48092a9042d8fcfd6bb7ae5911a1d53b8746d546 About time we got rid of the 32-bit file offsets - checked the `CBlockIndex` layout with Clang and GCC, the padding nit can be addressed in a follow-up.
l0rinc (Migrated from github.com) commented 2026-10-26 16:51:37 +00:00

48092a9 flatfile: use 64-bit file positioning syscalls in Windows:

This increases sizeof(CBlockIndex) slightly, we can bring that back in a followup PR by moving nTx after m_chain_tx_count.

Restore the CBlockIndex size
diff --git a/src/chain.h b/src/chain.h
index 830cabe3f6..b2380468b2 100644
--- a/src/chain.h
+++ b/src/chain.h
@@ -117,17 +117,17 @@ public:
     //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
     arith_uint256 nChainWork{};
 
-    //! Number of transactions in this block. This will be nonzero if the block
-    //! reached the VALID_TRANSACTIONS level, and zero otherwise.
-    //! Note: in a potential headers-first mode, this number cannot be relied upon
-    unsigned int nTx{0};
-
     //! (memory only) Number of transactions in the chain up to and including this block.
     //! This value will be non-zero if this block and all previous blocks back
     //! to the genesis block or an assumeutxo snapshot block have reached the
     //! VALID_TRANSACTIONS level.
     uint64_t m_chain_tx_count{0};
 
+    //! Number of transactions in this block. This will be nonzero if the block
+    //! reached the VALID_TRANSACTIONS level, and zero otherwise.
+    //! Note: in a potential headers-first mode, this number cannot be relied upon
+    unsigned int nTx{0};
+
     //! Verification status of this block. See enum BlockStatus
     //!
     //! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot
> [48092a9](https://github.com/bitcoin/bitcoin/pull/36669/changes/48092a9042d8fcfd6bb7ae5911a1d53b8746d546) _flatfile: use 64-bit file positioning syscalls in Windows_: This increases `sizeof(CBlockIndex)` slightly, we can bring that back in a followup PR by moving `nTx` after `m_chain_tx_count`. <details> <summary>Restore the CBlockIndex size</summary> ```diff diff --git a/src/chain.h b/src/chain.h index 830cabe3f6..b2380468b2 100644 --- a/src/chain.h +++ b/src/chain.h @@ -117,17 +117,17 @@ public: //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block arith_uint256 nChainWork{}; - //! Number of transactions in this block. This will be nonzero if the block - //! reached the VALID_TRANSACTIONS level, and zero otherwise. - //! Note: in a potential headers-first mode, this number cannot be relied upon - unsigned int nTx{0}; - //! (memory only) Number of transactions in the chain up to and including this block. //! This value will be non-zero if this block and all previous blocks back //! to the genesis block or an assumeutxo snapshot block have reached the //! VALID_TRANSACTIONS level. uint64_t m_chain_tx_count{0}; + //! Number of transactions in this block. This will be nonzero if the block + //! reached the VALID_TRANSACTIONS level, and zero otherwise. + //! Note: in a potential headers-first mode, this number cannot be relied upon + unsigned int nTx{0}; + //! Verification status of this block. See enum BlockStatus //! //! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot ``` </details>
fanquake commented 2026-10-26 17:04:40 +00:00 (Migrated from github.com)

Backported to 32.x in #36658.

Backported to `32.x` in #36658.
fanquake commented 2026-10-26 17:19:54 +00:00 (Migrated from github.com)

Backported to 31.x in #36661.

Backported to `31.x` in #36661.
fanquake commented 2026-10-26 18:25:39 +00:00 (Migrated from github.com)

Backported to 30.x in #36664.

Backported to `30.x` in #36664.
fanquake commented 2026-10-26 19:42:19 +00:00 (Migrated from github.com)

Backported to 29.x in #36666.

Backported to `29.x` in #36666.
maflcko (Migrated from github.com) reviewed 2026-10-27 10:06:45 +00:00
maflcko (Migrated from github.com) commented 2026-10-27 10:06:45 +00:00
See https://github.com/bitcoin/bitcoin/pull/36501 for a thread on this
maflcko (Migrated from github.com) reviewed 2026-11-04 10:11:12 +00:00
maflcko (Migrated from github.com) left a comment
post-merge lgtm. Only three style nits, which I fixed in https://github.com/bitcoin/bitcoin/pull/36725
maflcko (Migrated from github.com) commented 2026-11-04 10:06:01 +00:00

style-nit: While reviewing I found there is no test coverage at all for varint overflow

style-nit: While reviewing I found there is no test coverage at all for varint overflow
maflcko (Migrated from github.com) commented 2026-11-04 10:09:19 +00:00

style-nit: I still feel uneasy about using u32 for the length here. Obviously this is fine for all callers today, but it seems more consistent and easier to read to just use 64-bits for all file sizes and lengths.

style-nit: I still feel uneasy about using u32 for the length here. Obviously this is fine for all callers today, but it seems more consistent and easier to read to just use 64-bits for all file sizes and lengths.
Lines 208-209
maflcko (Migrated from github.com) commented 2026-11-04 10:07:35 +00:00

unrelated style-nit: After C++11, this should use the 64-bit QuadPart according to the official Windows docs

unrelated style-nit: After C++11, this should use the 64-bit QuadPart according to the official Windows docs
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36669
No description provided.