This PR is a second attempt at #19594. This PR has two motivations:
- Improve code hygiene by eliminating a global variable,
mapBlocksUnknownParent
- Fix fuzz test OOM when running too long ([see #19594 comment](/bitcoin-bitcoin/19594/#issuecomment-958801638))
A minor added advantage is to release mapBlocksUnknownParent
memory when the reindexing phase is done. The current situation is somewhat similar to a memory leak because this map exists unused for the remaining lifetime of the process. It’s true that this map should be empty of data elements after use, but its internal metadata (indexing structures, etc.) can have non-trivial size because there can be many thousands of simultaneous elements in this map.
This PR helps our efforts to reduce the use of global variables. This variable isn’t just global, it’s hidden inside a function (it looks like a local variable but has the static
attribute).
This global variable exists because the -reindex
processing code calls LoadExternalBlockFile()
multiple times (once for each block file), but that function must preserve some state between calls (the mapBlocksUnknownParent
map). This PR fixes this by allocating this map as a local variable in the caller’s scope and passing it in on each call. When reindexing completes, the map goes out of scope and is deallocated.
I tested this manually by reindexing on mainnet and signet. Also, the existing feature_reindex.py
functional test passes.