This migrates the headers presync parameters (commitment_period and redownload_buffer_size) from being precomputed chainparams to being computed at runtime during startup. This has as advantages:
- Less work for maintainers that need to occasionally run the current
contrib/devtools/headerssync-params.pyscript. - Less room for mistakes in that process.
- Automatically adapts based on time (as opposed to needing to predict the lifetime of the software).
To achieve this, the parameter search algorithm is ported to C++, and significant optimizations are applied to it step-by-step. The combination brings the runtime from minutes to ~3 ms. As an additional advantage, the result is now actually always optimal (within what can be determined using machine precision); the old code relied on (very reasonable) heuristics to guide the search.
To make the changes reviewable without understanding the details of the algorithmic and mathematical optimizations, the PR starts with a few refactors to bring the existing Python code in a state where it can be tested. A set of test vectors is then introduced (in Python), and those vectors are ported to, and remain valid, in all the further C++ versions too.
Disclaimer: this was written with significant assistance from Claude Opus 4.8 and Fable 5, which came up with the C++ port, the algorithmic and mathematical optimizations, and their implementation and testing. The code comments and commit messages are almost entirely written by me for clarity, but also to convince myself I understood all the changes.