Skip to content

refactor: move block-file I/O helpers into node/blockstorage#3073

Open
PrestackI wants to merge 6 commits into
gridcoin-community:developmentfrom
PrestackI:blockstorage/extract-file-io
Open

refactor: move block-file I/O helpers into node/blockstorage#3073
PrestackI wants to merge 6 commits into
gridcoin-community:developmentfrom
PrestackI:blockstorage/extract-file-io

Conversation

@PrestackI

Copy link
Copy Markdown
Contributor

What

Moves the block-file I/O helpers out of the ~1200-line src/main.h / ~3500-line src/main.cpp monolith into the existing src/node/blockstorage.{h,cpp} translation unit — the natural home, since WriteBlockToDisk / ReadBlockFromDisk already live there and already call these functions.

Moved from main.cpp into node/blockstorage.cpp:

  • CheckDiskSpace
  • OpenBlockFile
  • AppendBlockFile
  • LoadExternalBlockFile
  • BlockFilePath (file-local static helper)

Their two file-local statics move alongside their sole users: nMinDiskSpaceCheckDiskSpace, nCurrentBlockFileAppendBlockFile.

The declarations are removed from main.h; the four public functions are now declared in node/blockstorage.h. main.h gains #include "node/blockstorage.h", so the ~79 files that include main.h and call these helpers keep resolving transitively with no consumer edits (validation.cpp, init.cpp, gridcoin/upgrade.cpp, gridcoin/staking/kernel.cpp, gridcoin/voting/result.cpp).

No functional change — pure code move. The only textual change to the moved bodies: blockstorage.cpp has no using namespace std, so the two unqualified string locals are qualified to std::string.

Why

Continues the incremental main.h/main.cpp decomposition tracked in #3030 (Workstream A), after A1 (block primitives, #3060) and A2 (txmempool, #3072). Block-file I/O is a self-contained, isolated slice that belongs with the rest of block storage.

Stacked on #3072#3060. Until those merge, the diff here also includes their commits; it narrows to just the block-file-I/O move once they land.

Diffstat

main.cpp −155, main.h −11; node/blockstorage.cpp +165, node/blockstorage.h +10. No CMakeLists.txt change (blockstorage.cpp already builds in gridcoin_util).

Testing

Move-only diff (git diff -M shows pure relocation, no logic change). Local lint green: lint-whitespace, lint-include-guards, lint-circular-dependencies (the new main.h → node/blockstorage.h edge introduces no cycle). Full CI matrix to confirm on the PR.

Lift the pure block/index value types (CBlockHeader, CBlock, CBlockIndex,
CDiskBlockIndex, CBlockLocator, BlockHasher, the GRC::MRCFees/MintSummary
reports, LOCKTIME_THRESHOLD, FutureDrift, GetTargetSpacing) out of the
~1200-line main.h god-header into a new, main.h-free primitives/block.h,
mirroring Bitcoin Core's primitives/block.h.

The eight inline methods that reference chain-state globals (pindexBest,
mapBlockIndex, pindexGenesisBlock, fUseFastIndex, the genesis hashes) are
relocated out-of-line into primitives/block.cpp so the header stays free of
main.h. Their EXCLUSIVE_LOCKS_REQUIRED(cs_main) annotations remain on the
in-class declarations.

consensus/merkle.h and consensus/tx_verify.h now include primitives/block.h
instead of main.h, removing the layering inversion where low-level consensus
headers dragged in the entire top-of-stack include graph. tx_verify.cpp gains
a direct main.h include for the chain-state globals it still uses. main.h keeps
functional change.
A quote-include "validation.h" from src/consensus/tx_verify.h resolves to the
sibling src/consensus/validation.h (which has no MapPrevTx), not src/validation.h.
The original "main.h" include avoided this because there is no src/consensus/main.h.
Use angle brackets so the include resolves against -I src to the top-level
validation.h that defines MapPrevTx, matching the existing convention in
test/script_p2sh_tests.cpp.
validation.h uses CTxIndex by value in the MapPrevTx typedef and the
std::map<uint256, CTxIndex> parameters, but only ever compiled because main.h
includes index/txindex.h (line 13) before validation.h (line 24). Now that
consensus/tx_verify.h pulls validation.h directly, CTxIndex was undeclared,
which also broke overload resolution for the CTxIndex ReadTxFromDisk overload
at tx_verify.cpp:186. Include index/txindex.h directly (after index/disktxpos.h,
since CTxIndex embeds CDiskTxPos), mirroring main.h's existing include order.
Move CTxMemPool, the MemPoolRemovalReason enum, and the global
`mempool` instance out of the main.h/main.cpp god-header into a
dedicated translation unit src/txmempool.{h,cpp}.

This is a pure code move with no behavior change. main.h now
includes txmempool.h, so all existing consumers (including
wallet code using MemPoolRemovalReason) resolve unchanged. It also
serves as Phase 0 of the mempool modernization work (gridcoin-community#3029).

Register txmempool.cpp in the gridcoin_util CMake target.
Continues the incremental main.h/main.cpp decomposition (gridcoin-community#3030). Moves the
block-file I/O helpers out of the main.{h,cpp} monolith into the existing
node/blockstorage.{h,cpp} translation unit, alongside WriteBlockToDisk /
ReadBlockFromDisk which already live there and already call them:

  CheckDiskSpace, OpenBlockFile, AppendBlockFile, LoadExternalBlockFile
  (+ the file-local BlockFilePath helper)

Their two file-local statics move with them (each has a single user):
nMinDiskSpace -> CheckDiskSpace, nCurrentBlockFile -> AppendBlockFile.

main.h keeps the consumers working by #include "node/blockstorage.h", so the
~79 includers that call these functions resolve transitively with no edits.
The declarations are removed from main.h.

Pure code move, no behavior change. Two incidental fixes that the move exposes:
- blockstorage.cpp has no `using namespace std`, so the two unqualified
  `string` locals in the moved bodies are qualified to `std::string`.
- blockstorage.cpp now sees its own header (via main.h), so the redundant
  default arguments on the two ReadBlockFromDisk *definitions* are dropped --
  default args already live on the blockstorage.h declarations, and giving
  them in both places is ill-formed (-fpermissive). Behavior is unchanged.
@PrestackI PrestackI force-pushed the blockstorage/extract-file-io branch from b869d0a to 7c6a476 Compare June 16, 2026 23:56
When a ctest case fails in the distro/native build path, ctest buffers the
test's stdout into build/Testing/Temporary/LastTest.log and prints nothing
to the console, so functional-test flakes (e.g. rpc_multisig.py) show only
"1/3 Test #1: functional_tests ...***Failed" with no failing script name,
traceback, or combined node logs. test_runner.py already runs with
--ci --quiet --combinedlogslen=4000, so the detail exists — it's just
swallowed.

Add --output-on-failure to all four ctest invocations (native, depends,
win64, macos), mirroring what the dedicated functional job already does
(cmake_functional.yml). The flag is additive: no change on success, full
captured output on failure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant