headersync: do parameter search at runtime #35642

Open
sipa wants to merge 5 commits from 202607_headerssync_cpp into master AGit
sipa commented 2026-07-02 15:06:59 +00:00 (Migrated from github.com)

This migrates the headers presync parameters (commitment_period and redownload_buffer_size) from being precomputed chainparams to being computed at runtime whenever a headers sync starts. This has as advantages:

  • Less work for maintainers that need to occasionally run the current contrib/devtools/headerssync-params.py script.
  • 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 implemented in C++. It is simpler, because it does not attempt to model the fact that the attacker is forced to submit their headers in batches of 2000 if they want to maintain the attack. The old Python code did that, but actually got it (slightly) wrong by missing the fact that attackers are capable of strategically deciding to stop the attack in the middle of a batch (found by darosior).

Unit tests and fuzz tests are added, and at the end the old Python code is deleted.

This migrates the headers presync parameters (`commitment_period` and `redownload_buffer_size`) from being precomputed chainparams to being computed at runtime whenever a headers sync starts. This has as advantages: * Less work for maintainers that need to occasionally run the current `contrib/devtools/headerssync-params.py` script. * 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 implemented in C++. It is simpler, because it does not attempt to model the fact that the attacker is forced to submit their headers in batches of 2000 if they want to maintain the attack. The old Python code did that, but actually got it (slightly) wrong by missing the fact that attackers are capable of strategically deciding to stop the attack in the middle of a batch (found by darosior). Unit tests and fuzz tests are added, and at the end the old Python code is deleted.
DrahtBot commented 2026-07-02 15:07:07 +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 l0rinc, fametrano
Concept ACK sedited, fjahr, hodlinator
Approach ACK ajtowns
Stale ACK darosior

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36844 (iwyu: Fix warnings in more modules in src/ and treat them as errors by hebasto)
  • #36543 (validation: check the most recent AssumeUTXO commitment during IBD by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--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/35642 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35642.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35642#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 | [l0rinc](https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-6324747295), [fametrano](https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-6394267241) | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-5069107232), [fjahr](https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-5367998461), [hodlinator](https://github.com/bitcoin/bitcoin/pull/35642#pullrequestreview-5379510928) | | Approach ACK | [ajtowns](https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-5205781986) | | Stale ACK | [darosior](https://github.com/bitcoin/bitcoin/pull/35642#pullrequestreview-5368176826) | 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. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36844](https://github.com/bitcoin/bitcoin/pull/36844) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36844.svg"></sub> (iwyu: Fix warnings in more modules in `src/` and treat them as errors by hebasto) * [#36543](https://github.com/bitcoin/bitcoin/pull/36543) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36543.svg"></sub> (validation: check the most recent AssumeUTXO commitment during IBD by l0rinc) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
ajtowns (Migrated from github.com) reviewed 2026-07-02 15:51:50 +00:00
ajtowns (Migrated from github.com) commented 2026-07-02 15:51:50 +00:00

Is there any anonymity breaking via NodeClock::now() influencing these parameters? (I think maybe not, but might be worth quantizing or similar to guarantee it?)

Is there any anonymity breaking via `NodeClock::now()` influencing these parameters? (I think maybe not, but might be worth quantizing or similar to guarantee it?)
sipa (Migrated from github.com) reviewed 2026-07-02 16:18:20 +00:00
sipa (Migrated from github.com) commented 2026-07-02 16:18:20 +00:00

Good point, I had not considered that.

The choice of the commitment period should not be observable (besides a very minor time leak perhaps). However, the redownload buffer is observable in that an attacker can probably disconnect mid-redownload, and see how much you accepted afterwards.

I'll run some numbers to see how quickly these parameters change and suggest some quantization or noise.

Good point, I had not considered that. The choice of the commitment period should not be observable (besides a very minor time leak perhaps). However, the redownload buffer is observable in that an attacker can probably disconnect mid-redownload, and see how much you accepted afterwards. I'll run some numbers to see how quickly these parameters change and suggest some quantization or noise.
sipa (Migrated from github.com) reviewed 2026-07-02 17:00:51 +00:00
sipa (Migrated from github.com) commented 2026-07-02 17:00:51 +00:00

It looks like the output parameters only change roughly every 22 days. I don't think quantizing further gains us much, especially as the attack only applies during IBD, so peers generally already know you just started.

It looks like the output parameters only change roughly every 22 days. I don't think quantizing further gains us much, especially as the attack only applies during IBD, so peers generally already know you just started.
DrahtBot commented 2026-07-09 14:33:41 +00:00 (Migrated from github.com)

Could turn into draft while CI is red?

Could turn into draft while CI is red?
sipa commented 2026-07-09 20:50:51 +00:00 (Migrated from github.com)

Somehow moc doesn't like the changes to net_processing.h this makes, but only in the sanitizer build?

Somehow `moc` doesn't like the changes to `net_processing.h` this makes, but only in the sanitizer build?
sedited commented 2026-07-24 10:58:44 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-07-25 12:10:07 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
ajtowns commented 2026-08-06 14:08:43 +00:00 (Migrated from github.com)

Approach ACK ; needs rebase if wanted for 32.0 I guess?

Approach ACK ; needs rebase if wanted for 32.0 I guess?
darosior commented 2026-08-06 16:23:41 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
sipa commented 2026-08-06 17:46:04 +00:00 (Migrated from github.com)

Rebased.

Rebased.
shuv-amp commented 2026-08-12 16:11:20 +00:00 (Migrated from github.com)

next_height here is int, while m_current_height is int64_t and ValidateAndStoreRedownloadedHeader uses int64_t for the same step. For the long timewarp chains you mention, the presync height can exceed INT_MAX and overflow. Not a practical concern, but it should probably be int64_t to match the redownload path?

`next_height` here is `int`, while `m_current_height` is `int64_t` and `ValidateAndStoreRedownloadedHeader` uses `int64_t` for the same step. For the long timewarp chains you mention, the presync height can exceed `INT_MAX` and overflow. Not a practical concern, but it should probably be `int64_t` to match the redownload path?
sipa commented 2026-08-14 17:36:46 +00:00 (Migrated from github.com)

@shuv-amp That's mostly independent of this PR, but added a commit to address that. I have also made a small simplification in the search algorithm.

Ready for more review.

@shuv-amp That's mostly independent of this PR, but added a commit to address that. I have also made a small simplification in the search algorithm. Ready for more review.
shuv-amp commented 2026-08-14 19:30:43 +00:00 (Migrated from github.com)

ComputeHeadersSyncParams() doesn't terminate for a small minchainwork_headers
combined with a short timespan:

ComputeHeadersSyncParams(std::chrono::seconds{0}, 30228);  // hangs
ComputeHeadersSyncParams(std::chrono::seconds{0}, 30229);  // period=2 bufsize=40

30229 is the boundary; below it the region extends out to timespans around 1e4 s.

In attack_rate(), the left side of future_limit * prob < 1.0e-16 * rate can't be
negative, and the right side is exactly 0 once rate is below ~2.5e-308, so that
break stops being reachable. The limit check is the only other exit and Step 1's
Newton call doesn't pass one, so there it has none at all.

The Python this replaces has the same shape
(HEADER_BATCH_COUNT * prob < 1.0e-16 * rate * len(align_choices)) and doesn't
terminate at those arguments either, so it isn't new, it just matters more now that
it runs at startup.

Non-strict is enough, since prob decreases to exactly 0 and takes the left side
with it:

-    if (future_limit * prob < 1.0e-16 * rate) break;
+    if (future_limit * prob <= 1.0e-16 * rate) break;

The 50 test vectors still reproduce with that, and 196/196 other inputs I compared
are unchanged.

Nothing in chainparams is close: testnet4 has the lowest minchainwork_height at
123613, and reaches an exponent of 531 against the 1075 where prob underflows. It
is the region a new chain starts in though, and 0 hangs a different way
(attack_headers is 0, so period is NaN and accept_forged_headers > 0.0 never
holds), so a guard on the input might be worth having too.

`ComputeHeadersSyncParams()` doesn't terminate for a small `minchainwork_headers` combined with a short timespan: ```c++ ComputeHeadersSyncParams(std::chrono::seconds{0}, 30228); // hangs ComputeHeadersSyncParams(std::chrono::seconds{0}, 30229); // period=2 bufsize=40 ``` 30229 is the boundary; below it the region extends out to timespans around 1e4 s. In `attack_rate()`, the left side of `future_limit * prob < 1.0e-16 * rate` can't be negative, and the right side is exactly 0 once `rate` is below ~2.5e-308, so that break stops being reachable. The `limit` check is the only other exit and Step 1's Newton call doesn't pass one, so there it has none at all. The Python this replaces has the same shape (`HEADER_BATCH_COUNT * prob < 1.0e-16 * rate * len(align_choices)`) and doesn't terminate at those arguments either, so it isn't new, it just matters more now that it runs at startup. Non-strict is enough, since `prob` decreases to exactly 0 and takes the left side with it: ```c++ - if (future_limit * prob < 1.0e-16 * rate) break; + if (future_limit * prob <= 1.0e-16 * rate) break; ``` The 50 test vectors still reproduce with that, and 196/196 other inputs I compared are unchanged. Nothing in chainparams is close: testnet4 has the lowest `minchainwork_height` at 123613, and reaches an exponent of 531 against the 1075 where `prob` underflows. It is the region a new chain starts in though, and 0 hangs a different way (`attack_headers` is 0, so `period` is NaN and `accept_forged_headers > 0.0` never holds), so a guard on the input might be worth having too.
sipa commented 2026-08-19 18:55:03 +00:00 (Migrated from github.com)

@shuv-amp Fixed, and added some extra Assume()s in the code.

@shuv-amp Fixed, and added some extra `Assume()`s in the code.
sipa commented 2026-08-20 14:53:17 +00:00 (Migrated from github.com)

I made a further simplification to the algorithm, and also added a fuzz test that exercises the code far beyond its intended parameter range, so catch meaningless results, crashes, or infinite loops. The fuzz tests are relatively slow (for extreme values it's just above 1/second, through normal running closer to 50/second), but it's also not a particularly large or complicated space to explore.

I made a further simplification to the algorithm, and also added a fuzz test that exercises the code far beyond its intended parameter range, so catch meaningless results, crashes, or infinite loops. The fuzz tests are relatively slow (for extreme values it's just above 1/second, through normal running closer to 50/second), but it's also not a particularly large or complicated space to explore.
fjahr commented 2026-08-21 09:19:53 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-08-31 16:53:39 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
darosior commented 2026-09-04 21:15:24 +00:00 (Migrated from github.com)

The HeadersSyncParams definition can now be moved from chainparams to the headersync module.

The `HeadersSyncParams` definition can now be moved from chainparams to the headersync module.
darosior (Migrated from github.com) reviewed 2026-09-14 21:18:09 +00:00
darosior (Migrated from github.com) left a comment

Submitting my comments so far, because i think attack_rate will need to be revised.

Submitting my comments so far, because i think `attack_rate` will need to be revised.
darosior (Migrated from github.com) commented 2026-09-14 21:16:48 +00:00

I think this routine underestimates the rate, because an incomplete batch sent by an attacker may be detected, but will still lead to processing as many headers from the buffer.

I think this routine underestimates the rate, because an incomplete batch sent by an attacker may be detected, but will still lead to processing as many headers from the buffer.
Lines 418-420
darosior (Migrated from github.com) commented 2026-09-09 19:10:44 +00:00

nit: feel free to ignore, just mentioning what gave me pause when i was reading from scratch.

That last sentence in the comment threw me off a little bit. future_limit is a number of headers, weighted by the exponentially-decreasing contribution each batch makes. The last sentence could be read as it representing a single batch, while really it represents all batches, so the check later can be essentially "if the contribution of the entire tail of headers at the current probability is no more than a rounding error, we are done".

nit: feel free to ignore, just mentioning what gave me pause when i was reading from scratch. That last sentence in the comment threw me off a little bit. `future_limit` is a number of headers, weighted by the exponentially-decreasing contribution each batch makes. The last sentence could be read as it representing a single batch, while really it represents *all* batches, so the check later can be essentially "if the contribution of the entire tail of headers at the current probability is no more than a rounding error, we are done".
Lines 435-439
darosior (Migrated from github.com) commented 2026-09-09 15:16:13 +00:00

nit: forged_headers counts the number of headers deviating from the honest chain up to that point (i.e. that batch and previous ones), whereas accept_forged_headers counts the additional number of headers accepted as the result of that batch. Took me a minute, and i think this could be clarified in the comment:

                /** The forged headers received so far, up to and including this batch. */
                const int64_t forged_headers = (batch + 1) * HEADER_BATCH_COUNT - honest;
                /** The number of forged headers that would be accepted as a result of this batch,
                 *  if the commitments in this batch and all earlier batches verify correctly. */
                const double accept_forged_headers = std::clamp<double>(forged_headers - bufsize, 0.0, HEADER_BATCH_COUNT);
nit: `forged_headers` counts the number of headers deviating from the honest chain up to that point (i.e. that batch and previous ones), whereas `accept_forged_headers` counts the additional number of headers accepted as the result of that batch. Took me a minute, and i think this could be clarified in the comment: ```suggestion /** The forged headers received so far, up to and including this batch. */ const int64_t forged_headers = (batch + 1) * HEADER_BATCH_COUNT - honest; /** The number of forged headers that would be accepted as a result of this batch, * if the commitments in this batch and all earlier batches verify correctly. */ const double accept_forged_headers = std::clamp<double>(forged_headers - bufsize, 0.0, HEADER_BATCH_COUNT); ```
Lines 529-530
darosior (Migrated from github.com) commented 2026-09-09 19:39:00 +00:00

Why is it not always true?

Why is it not always true?
Lines 541-547
darosior (Migrated from github.com) commented 2026-09-14 19:57:29 +00:00

Why is this square root "indirection" necessary? Using gamma = bufsize / period (intuitively "bits of security") and memory_factor = (max_headers - mcw_headers) / COMPACT_HEADER_SIZE directly seem clearer to me, and equivalent?

Why is this square root "indirection" necessary? Using `gamma = bufsize / period` (intuitively "bits of security") and `memory_factor = (max_headers - mcw_headers) / COMPACT_HEADER_SIZE` directly seem clearer to me, and equivalent?
sipa commented 2026-09-14 21:33:48 +00:00 (Migrated from github.com)

Converted to draft. @darosior found that the current version (and the original Python it's based on) did not take one attack scenario into account (using a final headers batch of size less than 2000). Fixing that is possible, but makes the code even more complicated, and results in parameters that increase per-peer memory by ~2.2 KiB.

I'm considering instead going for a much simpler approach, that drops the assumption that all headers arrive in batches of 2000 altogether. That'd be much simpler and faster, and perhaps more obviously correct, but increase per-peer memory by ~14.7 KiB. I think that's acceptable compared to existing memory usage.

Converted to draft. @darosior found that the current version (and the original Python it's based on) did not take one attack scenario into account (using a final headers batch of size less than 2000). Fixing that is possible, but makes the code even more complicated, and results in parameters that increase per-peer memory by ~2.2 KiB. I'm considering instead going for a much simpler approach, that drops the assumption that all headers arrive in batches of 2000 altogether. That'd be much simpler and faster, and perhaps more obviously correct, but increase per-peer memory by ~14.7 KiB. I think that's acceptable compared to existing memory usage.
sipa commented 2026-09-15 18:19:19 +00:00 (Migrated from github.com)

Rewritten to take that approach. The commit history is simpler now too, as it doesn't attempt to show equivalence between the old Python script and the new code anymore.

Rewritten to take that approach. The commit history is simpler now too, as it doesn't attempt to show equivalence between the old Python script and the new code anymore.
DrahtBot commented 2026-09-15 19:18:11 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task fuzzer,address,undefined,integer: https://github.com/bitcoin/bitcoin/actions/runs/35006193047/job/104506428335
LLM reason (✨ experimental): Fuzz tests failed due to an UndefinedBehaviorSanitizer float-cast-overflow (inf cast to int) in ComputeHeadersSyncParamsInner (headersync.cpp:530).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `fuzzer,address,undefined,integer`: https://github.com/bitcoin/bitcoin/actions/runs/35006193047/job/104506428335</sub> <sub>LLM reason (✨ experimental): Fuzz tests failed due to an UndefinedBehaviorSanitizer float-cast-overflow (inf cast to `int`) in `ComputeHeadersSyncParamsInner` (`headersync.cpp:530`).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
darosior (Migrated from github.com) approved these changes 2026-09-18 18:36:32 +00:00
darosior (Migrated from github.com) left a comment

ACK 526db3911d

I was not familiar with the existing script, so i reviewed the new calculation from scratch. The latest approach is a lot easier to grasp. I think this slightly looser upper bound is the right tradeoff.

ACK 526db3911d400b793e5715c0f989f1d3b66ac12e I was not familiar with the existing script, so i reviewed the new calculation from scratch. The latest approach is a lot easier to grasp. I think this slightly looser upper bound is the right tradeoff.
Lines 573-590
darosior (Migrated from github.com) commented 2026-09-18 16:18:23 +00:00

Is Step 2 really useful anymore with the new approach? It made sense when Step 1 was a Newton-Raphson search that stopped at some threshold, to use as a basis for a more accurate search. But now Step 1 is an exact (according to the model) derivation.

I asked Claude to check if Step 2 had any material impact in the new version. It claims that over 20,168 fuzz domain cases Step 2 picks offset 0 in 99.32% of cases. Furthermore, when it does pick a different offset it only saves about 0.0039% memory on realistic inputs.

Is Step 2 really useful anymore with the new approach? It made sense when Step 1 was a Newton-Raphson search that stopped at some threshold, to use as a basis for a more accurate search. But now Step 1 is an exact (according to the model) derivation. I asked Claude to check if Step 2 had any material impact in the new version. It claims that over 20,168 fuzz domain cases Step 2 picks offset 0 in 99.32% of cases. Furthermore, when it does pick a different offset it only saves about 0.0039% memory on realistic inputs.
sipa (Migrated from github.com) reviewed 2026-09-18 20:44:41 +00:00
Lines 573-590
sipa (Migrated from github.com) commented 2026-09-18 20:44:41 +00:00

I've changed it to only checking the floor() and ceil() of the continuous optimal period. Experimentally, the only examples where neither of those wins occur with extremely low security levels (they all involve bufsize < period).

I've changed it to only checking the `floor()` and `ceil()` of the continuous optimal period. Experimentally, the only examples where neither of those wins occur with extremely low security levels (they all involve `bufsize` < `period`).
darosior (Migrated from github.com) approved these changes 2026-09-18 21:22:08 +00:00
darosior (Migrated from github.com) left a comment

re-ACK 0e473d1752

re-ACK 0e473d175224746c13678db5106f9d4ab1086b9d
Lines 566-567
darosior (Migrated from github.com) commented 2026-09-18 21:21:51 +00:00

Neat.

Neat.
DrahtBot commented 2026-09-24 01:14:46 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
First-time contributor

Earlier concerns remain unresolved; see the full report for their current assessments.

🟠 Minor · Avoid overflow when scaling negative timespans

headerssync.cpp:595 evaluates 6 * timespan.count() before the clamp, so a seconds count below INT64_MIN/6 overflows. NodeClock::now() asserts positive time and the fuzz target stays within ±(2^34-1), so no in-tree caller reaches it.

Location: src/headerssync.cpp:595 (head).

🟠 Minor · Use one clock sample for parameter sizing and the commitment cap

net_processing.cpp:2146 sizes the period from one Now() read, while headerssync.cpp:44 reads NodeClock::now() again for m_max_commitments. A host clock step between the two reads raises the commitment cap. The window is tiny and no clock-step producer is known.

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

🔵 Suggestion · The production-parameter wrapper lacks direct unit coverage

Unit tests do not pin the constants or clamp in ComputeHeadersSyncParams, so a change could alter headers-sync parameters across networks without failing a test.

Location: src/test/headerssync_tests.cpp:236 (head).

🔵 Suggestion · The optimizer fuzz target does not check the attack-rate budget

The fuzz target checks only result shape, so a redownload buffer that exceeds the attack-rate budget outside the 50 unit vectors would pass its assertions.

Location: src/test/fuzz/headerssync.cpp:171 (head).

🔵 Suggestion · The attack budget uses in-memory header size instead of wire size

The value is correct today, but an in-memory-only addition to CBlockHeader would silently loosen the attack budget.

Location: src/headerssync.cpp:579 (head).

🔵 Suggestion · Initialize the minimum-chain-work height member

consensus/params.h:136 declares int minchainwork_height; without a default initializer, so a default-constructed Consensus::Params leaves it indeterminate. Several other int members of the struct follow the same existing convention.

Location: src/consensus/params.h:133 (head).


Reviewed: 0316d0dc9ec6 · Full review report · Previous reports

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:6f232c871bca65facadc248ad7ecd634f87b0b5f6936bc30593af890589888f1:0cfbbc826fdd30e42f35c9a5b7d389c0902089c94f2400654bb32123e69823f6 --> <!-- ralph-base:6ed5aad06111177c39f9b9f07a52634476380ae1 --> <!-- ralph-head:0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee --> <!-- ralph-case:3aef0ad6873990361f8c9753432f0436ad877cf99d2bf6a34c5fdda676f37942 --> Earlier concerns remain unresolved; see the full report for their current assessments. **🟠 Minor · Avoid overflow when scaling negative timespans** headerssync.cpp:595 evaluates `6 * timespan.count()` before the clamp, so a seconds count below INT64_MIN/6 overflows. NodeClock::now() asserts positive time and the fuzz target stays within ±(2^34-1), so no in-tree caller reaches it. Location: [`src/headerssync.cpp:595`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/headerssync.cpp#L595>) (head). **🟠 Minor · Use one clock sample for parameter sizing and the commitment cap** net_processing.cpp:2146 sizes the period from one Now<NodeSeconds>() read, while headerssync.cpp:44 reads NodeClock::now() again for m_max_commitments. A host clock step between the two reads raises the commitment cap. The window is tiny and no clock-step producer is known. Location: [`src/net_processing.cpp:2144`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/net_processing.cpp#L2144>) (head). **🔵 Suggestion · The production-parameter wrapper lacks direct unit coverage** Unit tests do not pin the constants or clamp in `ComputeHeadersSyncParams`, so a change could alter headers-sync parameters across networks without failing a test. Location: [`src/test/headerssync_tests.cpp:236`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/test/headerssync_tests.cpp#L236>) (head). **🔵 Suggestion · The optimizer fuzz target does not check the attack-rate budget** The fuzz target checks only result shape, so a redownload buffer that exceeds the attack-rate budget outside the 50 unit vectors would pass its assertions. Location: [`src/test/fuzz/headerssync.cpp:171`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/test/fuzz/headerssync.cpp#L171>) (head). **🔵 Suggestion · The attack budget uses in-memory header size instead of wire size** The value is correct today, but an in-memory-only addition to `CBlockHeader` would silently loosen the attack budget. Location: [`src/headerssync.cpp:579`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/headerssync.cpp#L579>) (head). **🔵 Suggestion · Initialize the minimum-chain-work height member** consensus/params.h:136 declares `int minchainwork_height;` without a default initializer, so a default-constructed Consensus::Params leaves it indeterminate. Several other int members of the struct follow the same existing convention. Location: [`src/consensus/params.h:133`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee/src/consensus/params.h#L133>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`0316d0dc9ec6`](<https://git.fish.foo/bitcoin/bitcoin/commit/0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee>) · [Full review report](<https://review.fish.foo/traces/35642-0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee-1694-10.html>) · [Previous reports](<https://review.fish.foo/traces/35642/>)
sipa commented 2026-09-30 13:47:43 +00:00 (Migrated from github.com)

Rebased, should be ready for review.

Rebased, should be ready for review.
darosior (Migrated from github.com) approved these changes 2026-09-30 15:09:12 +00:00
darosior (Migrated from github.com) left a comment

re-ACK 9f7bcfe874

re-ACK 9f7bcfe8747
sipa (Migrated from github.com) reviewed 2026-10-07 20:14:13 +00:00
Lines 529-530
sipa (Migrated from github.com) commented 2026-10-07 20:14:13 +00:00

In practice, it is always true. Outside of that (fuzz tests) it could be untrue, so it's good to document under what conditions the function's behavior is well-defined.

In practice, it is always true. Outside of that (fuzz tests) it could be untrue, so it's good to document under what conditions the function's behavior is well-defined.
l0rinc (Migrated from github.com) requested changes 2026-10-08 08:14:43 +00:00
l0rinc (Migrated from github.com) left a comment

Concept ACK on calculating the parameters at startup from elapsed time since genesis, instead of predicting the release's lifetime.

I found the calculation difficult to review, and the lengthy derivations interspersed with the code made it harder to follow. I left a few simplification suggestions and a question about whether a small memory tradeoff could eliminate the searches, and whether the unbounded floating-point loop is safe (and whether we can eliminate it completely).

Concept ACK on calculating the parameters at startup from elapsed time since genesis, instead of predicting the release's lifetime. I found the calculation difficult to review, and the lengthy derivations interspersed with the code made it harder to follow. I left a few simplification suggestions and a question about whether a small memory tradeoff could eliminate the searches, and whether the unbounded floating-point loop is safe (and whether we can eliminate it completely).
l0rinc (Migrated from github.com) commented 2026-10-08 07:31:43 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Since this assertion protects the test vectors (and since ComputeHeadersSyncParamsInner is already very complicated), I’d keep it in the test.

Move the test-vector size assertion
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 1b5ec9bc43..be9b3d8b1a 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -394,9 +394,6 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
 {
     /** Headers in the redownload buffer are stored without prevhash, as a CompressedHeader. [bits] */
     constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8};
-    // The unit tests would need to be regenerated if this constant were to change, so
-    // static_assert to give a more targeted error message.
-    static_assert(COMPACT_HEADER_SIZE == 384);
 
     // The chain has at least one header.
     Assume(minchainwork_headers > 0);
diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp
index a093bf6287..b5b13da883 100644
--- a/src/test/headerssync_tests.cpp
+++ b/src/test/headerssync_tests.cpp
@@ -31,6 +31,8 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests)
 
 BOOST_AUTO_TEST_CASE(compute_headers_sync_params)
 {
+    constexpr int64_t COMPACT_HEADER_SIZE{384};
+    static_assert(sizeof(CompressedHeader) * 8 == COMPACT_HEADER_SIZE, "Regenerate the optimizer test vectors if CompressedHeader changes size");
     const struct {
         int64_t max_headers;
         int64_t minchainwork_headers;
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Since this assertion protects the test vectors (and since `ComputeHeadersSyncParamsInner` is already very complicated), I’d keep it in the test. <details> <summary>Move the test-vector size assertion</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 1b5ec9bc43..be9b3d8b1a 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -394,9 +394,6 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min { /** Headers in the redownload buffer are stored without prevhash, as a CompressedHeader. [bits] */ constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8}; - // The unit tests would need to be regenerated if this constant were to change, so - // static_assert to give a more targeted error message. - static_assert(COMPACT_HEADER_SIZE == 384); // The chain has at least one header. Assume(minchainwork_headers > 0); diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp index a093bf6287..b5b13da883 100644 --- a/src/test/headerssync_tests.cpp +++ b/src/test/headerssync_tests.cpp @@ -31,6 +31,8 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests) BOOST_AUTO_TEST_CASE(compute_headers_sync_params) { + constexpr int64_t COMPACT_HEADER_SIZE{384}; + static_assert(sizeof(CompressedHeader) * 8 == COMPACT_HEADER_SIZE, "Regenerate the optimizer test vectors if CompressedHeader changes size"); const struct { int64_t max_headers; int64_t minchainwork_headers; ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 03:55:12 +00:00

9f7bcfe contrib: remove headerssync-params.py:

Following up on https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-5297347941: is there a reason to use Assume (rather than Assert) for these preconditions? Shouldn't these abort in production? It seems to me that zero inputs could theoretically still lead to division by zero or an infinity-to-integer conversion.

> [9f7bcfe](https://github.com/bitcoin/bitcoin/pull/35642/changes/9f7bcfe874729d8b20c46dd61d9dab8a0e5ea3fa) _contrib: remove headerssync-params.py_: Following up on https://github.com/bitcoin/bitcoin/pull/35642#issuecomment-5297347941: is there a reason to use `Assume` (rather than `Assert`) for these preconditions? Shouldn't these abort in production? It seems to me that zero inputs could theoretically still lead to division by zero or an infinity-to-integer conversion.
l0rinc (Migrated from github.com) commented 2026-10-08 07:59:40 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

I wonder whether we need to find the optimum this precisely. If we restricted the buffer to whole commitment periods, we could calculate its size directly, and a logarithmic estimate might replace the period search too. The earlier experiments suggest a small memory increase could let us remove both searches. Do you think that tradeoff is worth exploring?

Whole-period calculation and earlier measurements

AI summary:

For whole-period buffers (M = 0), the attack rate reduces to 2^-K * (1.5*P - 0.5). Choosing at least one check and K = ceil(log2(1.5*P / attack_headers)) meets the budget without searching for the buffer. A corrected logarithmic estimate for the period also removed the other search in the earlier experiments. Across the existing 50 vectors, that used about 1.1% more memory on average, with a maximum increase of 3.9%. Those are observed differences, not a bound for every input. The original exact outputs would remain the comparison baseline.

> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: I wonder whether we need to find the optimum this precisely. If we restricted the buffer to whole commitment periods, we could calculate its size directly, and a logarithmic estimate might replace the period search too. The earlier experiments suggest a small memory increase could let us remove both searches. Do you think that tradeoff is worth exploring? <details> <summary>Whole-period calculation and earlier measurements</summary> AI summary: > For whole-period buffers (`M = 0`), the attack rate reduces to `2^-K * (1.5*P - 0.5)`. Choosing at least one check and `K = ceil(log2(1.5*P / attack_headers))` meets the budget without searching for the buffer. A corrected logarithmic estimate for the period also removed the other search in the earlier experiments. Across the existing 50 vectors, that used about 1.1% more memory on average, with a maximum increase of 3.9%. Those are observed differences, not a bound for every input. The original exact outputs would remain the comparison baseline. </details>
l0rinc (Migrated from github.com) commented 2026-10-08 07:27:37 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Unless I'm missing something, this could be simplified to modulo, see https://godbolt.org/z/53PfMqxnj.
If we cannot use that, could you add a same-line comment explaining why?

Use the remainder operator
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 06964703a4..1b5ec9bc43 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -448,7 +448,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
      */
     auto attack_rate = [](int64_t period, int64_t bufsize) noexcept -> double {
         auto k = bufsize / period;
-        auto m = bufsize - k * period;
+        auto m = bufsize % period;
         return std::ldexp((period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / period, -int(k + 2));
     };
 
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Unless I'm missing something, this could be simplified to modulo, see https://godbolt.org/z/53PfMqxnj. If we cannot use that, could you add a same-line comment explaining why? <details> <summary>Use the remainder operator</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 06964703a4..1b5ec9bc43 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -448,7 +448,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min */ auto attack_rate = [](int64_t period, int64_t bufsize) noexcept -> double { auto k = bufsize / period; - auto m = bufsize - k * period; + auto m = bufsize % period; return std::ldexp((period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / period, -int(k + 2)); }; ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 07:56:56 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Nit: I find the preceding 2^-K * ... form easier to follow. Could we separate the expected headers from the probability of passing the first K checks here?

Separate the probability from the expected headers
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index bb1701cebd..7a083848c0 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -448,7 +448,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
     auto attack_rate = [](int64_t period, int64_t bufsize) noexcept -> double {
         auto k = bufsize / period;
         auto m = bufsize % period;
-        return std::ldexp((period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / period, -int(k + 2));
+        // Expected accepted headers, given the first k checks pass
+        const double expected_headers = (period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / (4.0 * period);
+        return std::ldexp(expected_headers, -int(k));
     };
 
     /** The peak per-peer memory a (period,bufsize) configuration needs. */
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Nit: I find the preceding `2^-K * ...` form easier to follow. Could we separate the expected headers from the probability of passing the first `K` checks here? <details> <summary>Separate the probability from the expected headers</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index bb1701cebd..7a083848c0 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -448,7 +448,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min auto attack_rate = [](int64_t period, int64_t bufsize) noexcept -> double { auto k = bufsize / period; auto m = bufsize % period; - return std::ldexp((period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / period, -int(k + 2)); + // Expected accepted headers, given the first k checks pass + const double expected_headers = (period * (6.0 * period - 4.0 * m - 2.0) + m * (m + 1.0)) / (4.0 * period); + return std::ldexp(expected_headers, -int(k)); }; /** The peak per-peer memory a (period,bufsize) configuration needs. */ ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 06:22:38 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Since fail_buf = 2 * succ_buf + 1 throughout the growth loop, we can grow just fail_buf internally and derive succ_buf at the end.

Track one bound while growing the search range
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index b89468b174..06964703a4 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -466,14 +466,12 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
 
     /** Smallest bufsize whose attack rate against (period, bufsize) is below attack_headers. */
     auto find_bufsize = [&](int64_t period) noexcept -> int64_t {
-        int64_t succ_buf{0};
         int64_t fail_buf{1};
         // First double iteratively until an upper bound for failure is found.
         while (attack_rate(period, fail_buf) >= attack_headers) {
-            const int64_t next_fail = 3 * fail_buf - 2 * succ_buf;
-            succ_buf = fail_buf;
-            fail_buf = next_fail;
+            fail_buf = 2 * fail_buf + 1;
         }
+        int64_t succ_buf{fail_buf / 2};
         // Then perform a bisection search to narrow it down.
         while (fail_buf > succ_buf + 1) {
             const int64_t try_buf = (succ_buf + fail_buf) / 2;
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Since `fail_buf = 2 * succ_buf + 1` throughout the growth loop, we can grow just `fail_buf` internally and derive `succ_buf` at the end. <details> <summary>Track one bound while growing the search range</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index b89468b174..06964703a4 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -466,14 +466,12 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min /** Smallest bufsize whose attack rate against (period, bufsize) is below attack_headers. */ auto find_bufsize = [&](int64_t period) noexcept -> int64_t { - int64_t succ_buf{0}; int64_t fail_buf{1}; // First double iteratively until an upper bound for failure is found. while (attack_rate(period, fail_buf) >= attack_headers) { - const int64_t next_fail = 3 * fail_buf - 2 * succ_buf; - succ_buf = fail_buf; - fail_buf = next_fail; + fail_buf = 2 * fail_buf + 1; } + int64_t succ_buf{fail_buf / 2}; // Then perform a bisection search to narrow it down. while (fail_buf > succ_buf + 1) { const int64_t try_buf = (succ_buf + fail_buf) / 2; ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 08:01:39 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Nit: The attack budget stays fixed during this search, so we could calculate its logarithm once in the capture.

Calculate the fixed logarithm once
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 7a083848c0..1f8485e0a7 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -525,8 +525,8 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         //
         //   M^2 - (4*P - 1)*M + 6*P^2 - 2*P - attack_headers * P * 2^(K+2) = 0.
         //
-        auto cont_bufsize = [&](double period) noexcept -> double {
-            const double k = std::floor(std::log2(1.5 * period - 0.5) - std::log2(attack_headers));
+        auto cont_bufsize = [attack_headers, log_attack_headers = std::log2(attack_headers)](double period) noexcept -> double {
+            const double k = std::floor(std::log2(1.5 * period - 0.5) - log_attack_headers);
             const double m = 0.5 * (4.0 * period - 1.0 - std::sqrt(std::ldexp(attack_headers * period, int(k + 4)) - 8.0 * period * period + 1.0));
             return k * period + m;
         };
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Nit: The attack budget stays fixed during this search, so we could calculate its logarithm once in the capture. <details> <summary>Calculate the fixed logarithm once</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 7a083848c0..1f8485e0a7 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -525,8 +525,8 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // // M^2 - (4*P - 1)*M + 6*P^2 - 2*P - attack_headers * P * 2^(K+2) = 0. // - auto cont_bufsize = [&](double period) noexcept -> double { - const double k = std::floor(std::log2(1.5 * period - 0.5) - std::log2(attack_headers)); + auto cont_bufsize = [attack_headers, log_attack_headers = std::log2(attack_headers)](double period) noexcept -> double { + const double k = std::floor(std::log2(1.5 * period - 0.5) - log_attack_headers); const double m = 0.5 * (4.0 * period - 1.0 - std::sqrt(std::ldexp(attack_headers * period, int(k + 4)) - 8.0 * period * period + 1.0)); return k * period + m; }; ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 06:04:20 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Could we cap this loop? I was surprised we need a binary search here (considered a few alternatives but don't have a fully working version) and wondered about NaN risk, but that does actually make this condition break.
Digging further with AI pointed to x87 excess precision: a midpoint can compare between the endpoints, then round back to one when stored, leaving the loop with no progress. GCC describes the relevant behavior.

Bound the existing bisection loop

The saved GCC 12.2/i686 runs timed out on the 50 existing vectors. The capped runs matched the native outputs. I inspected those results but have not rerun the compiler matrix.

diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index e218f99d3d..5c09d516fe 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -542,7 +542,7 @@ std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int
         const double target = double(max_headers - minchainwork_headers) / COMPACT_HEADER_SIZE;
         double lo = 1.0;
         double hi = minchainwork_headers;
-        while (true) {
+        for (int iter{0}; iter < 128; ++iter) {
             const double mid = 0.5 * (lo + hi);
             if (!(lo < mid && mid < hi)) break;
             if (mid * cont_bufsize(mid) < target) {
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Could we cap this loop? I was surprised we need a binary search here (considered a few alternatives but don't have a fully working version) and wondered about `NaN` risk, but that does actually make this condition break. Digging further with AI pointed to x87 excess precision: a midpoint can compare between the endpoints, then round back to one when stored, leaving the loop with no progress. [GCC describes the relevant behavior](https://gcc.gnu.org/gcc-13/porting_to.html#excess-precision). <details> <summary>Bound the existing bisection loop</summary> The saved GCC 12.2/i686 runs timed out on the 50 existing vectors. The capped runs matched the native outputs. I inspected those results but have not rerun the compiler matrix. ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index e218f99d3d..5c09d516fe 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -542,7 +542,7 @@ std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int const double target = double(max_headers - minchainwork_headers) / COMPACT_HEADER_SIZE; double lo = 1.0; double hi = minchainwork_headers; - while (true) { + for (int iter{0}; iter < 128; ++iter) { const double mid = 0.5 * (lo + hi); if (!(lo < mid && mid < hi)) break; if (mid * cont_bufsize(mid) < target) { ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 06:18:59 +00:00

014aff1 net_processing: compute headers sync parameters at runtime:

Since we already have a HeadersSyncParams type could we move it and use it here as well (as @darosior also hinted I think)?

Return the existing named parameter type
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 5c09d516fe..b89468b174 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -390,7 +390,7 @@ CBlockLocator HeadersSyncState::NextHeadersRequestLocator() const
 //    alternate chain. This trade-off is ignored here, as it results in at most a small constant
 //    factor in attack rate.
 
-std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers)
+HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers)
 {
     /** Headers in the redownload buffer are stored without prevhash, as a CompressedHeader. [bits] */
     constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8};
@@ -568,12 +568,12 @@ std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int
         make_config(std::floor(cont_period)),
         make_config(std::ceil(cont_period)));
 
-    return {size_t(best_period), size_t(best_bufsize)};
+    return {.commitment_period = size_t(best_period), .redownload_buffer_size = size_t(best_bufsize)};
 }
 
 // Derive max_headers (the longest a valid chain could be by now) and the attack_headers budget from
 // the chain's age and minimum-chain-work header count, then optimize.
-std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers)
+HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers)
 {
     /** Expected block interval. [seconds] */
     constexpr double BLOCK_INTERVAL{600.0};
diff --git a/src/headerssync.h b/src/headerssync.h
index 5827db43f6..0dd1d0e3fe 100644
--- a/src/headerssync.h
+++ b/src/headerssync.h
@@ -15,9 +15,9 @@
 #include <util/hasher.h>
 
 #include <chrono>
+#include <cstddef>
 #include <deque>
 #include <stdexcept>
-#include <utility>
 #include <vector>
 
 // A compressed CBlockHeader, which leaves out the prevhash
@@ -56,6 +56,14 @@ struct CompressedHeader {
     };
 };
 
+//! Configuration for headers sync memory usage.
+struct HeadersSyncParams {
+    //! Distance in blocks between header commitments.
+    size_t commitment_period{0};
+    //! Minimum number of validated headers to accumulate in the redownload
+    //! buffer before feeding them into the permanent block index.
+    size_t redownload_buffer_size{0};
+};
 
 /** Find a near-optimal (period, bufsize) headers synchronization configuration for:
  *
@@ -63,11 +71,8 @@ struct CompressedHeader {
  * - A specified number of headers in the minchainwork chain.
  *
  * See the constants in the implementation that control what attack success rate is acceptable.
- *
- * Returns {commitment period, redownload buffer size}.
  */
-std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers);
-
+HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers);
 
 /** Find a near-optimal (period, bufsize) headers synchronization configuration for:
  *
@@ -79,8 +84,7 @@ std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan
  * This is the internal part of ComputeHeadersSyncParams that performs the actual optimization.
  * Test-only.
  */
-std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers);
-
+HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers);
 
 /** HeadersSyncState:
  *
diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h
index e9761f70ae..61c734b0a4 100644
--- a/src/kernel/chainparams.h
+++ b/src/kernel/chainparams.h
@@ -14,7 +14,6 @@
 #include <util/hash_type.h>
 #include <util/vector.h>
 
-#include <cstddef>
 #include <cstdint>
 #include <memory>
 #include <optional>
@@ -60,15 +59,6 @@ struct ChainTxData {
     double dTxRate;   //!< estimated number of transactions per second after that timestamp
 };
 
-//! Configuration for headers sync memory usage.
-struct HeadersSyncParams {
-    //! Distance in blocks between header commitments.
-    size_t commitment_period{0};
-    //! Minimum number of validated headers to accumulate in the redownload
-    //! buffer before feeding them into the permanent block index.
-    size_t redownload_buffer_size{0};
-};
-
 /**
  * CChainParams defines various tweakable parameters of a given instance of the
  * Bitcoin system.
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 5524e16b4f..ea800ba9a3 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -2141,13 +2141,13 @@ std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrm
 static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams,
                                                const std::optional<std::pair<size_t, size_t>>& precomputed)
 {
-    const auto [period, bufsize] = precomputed ? *precomputed : [&] {
+    const auto params{precomputed ? HeadersSyncParams{.commitment_period = precomputed->first, .redownload_buffer_size = precomputed->second} : [&] {
         const auto genesis_time{NodeSeconds{std::chrono::seconds{chainparams.GenesisBlock().nTime}}};
         const auto timespan{std::chrono::duration_cast<std::chrono::seconds>(NodeClock::now() - genesis_time)};
         return ComputeHeadersSyncParams(timespan, chainparams.GetConsensus().minchainwork_height);
-    }();
-    LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u\n", period, bufsize);
-    return HeadersSyncParams{.commitment_period = period, .redownload_buffer_size = bufsize};
+    }()};
+    LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u", params.commitment_period, params.redownload_buffer_size);
+    return params;
 }
 
 PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
> [014aff1](https://github.com/bitcoin/bitcoin/pull/35642/changes/014aff1b813a31a9a8d0185fe01f641581088e0a) _net_processing: compute headers sync parameters at runtime_: Since we already have a `HeadersSyncParams` type could we move it and use it here as well (as @darosior also hinted I think)? <details> <summary>Return the existing named parameter type</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 5c09d516fe..b89468b174 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -390,7 +390,7 @@ CBlockLocator HeadersSyncState::NextHeadersRequestLocator() const // alternate chain. This trade-off is ignored here, as it results in at most a small constant // factor in attack rate. -std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers) +HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers) { /** Headers in the redownload buffer are stored without prevhash, as a CompressedHeader. [bits] */ constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8}; @@ -568,12 +568,12 @@ std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int make_config(std::floor(cont_period)), make_config(std::ceil(cont_period))); - return {size_t(best_period), size_t(best_bufsize)}; + return {.commitment_period = size_t(best_period), .redownload_buffer_size = size_t(best_bufsize)}; } // Derive max_headers (the longest a valid chain could be by now) and the attack_headers budget from // the chain's age and minimum-chain-work header count, then optimize. -std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers) +HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers) { /** Expected block interval. [seconds] */ constexpr double BLOCK_INTERVAL{600.0}; diff --git a/src/headerssync.h b/src/headerssync.h index 5827db43f6..0dd1d0e3fe 100644 --- a/src/headerssync.h +++ b/src/headerssync.h @@ -15,9 +15,9 @@ #include <util/hasher.h> #include <chrono> +#include <cstddef> #include <deque> #include <stdexcept> -#include <utility> #include <vector> // A compressed CBlockHeader, which leaves out the prevhash @@ -56,6 +56,14 @@ struct CompressedHeader { }; }; +//! Configuration for headers sync memory usage. +struct HeadersSyncParams { + //! Distance in blocks between header commitments. + size_t commitment_period{0}; + //! Minimum number of validated headers to accumulate in the redownload + //! buffer before feeding them into the permanent block index. + size_t redownload_buffer_size{0}; +}; /** Find a near-optimal (period, bufsize) headers synchronization configuration for: * @@ -63,11 +71,8 @@ struct CompressedHeader { * - A specified number of headers in the minchainwork chain. * * See the constants in the implementation that control what attack success rate is acceptable. - * - * Returns {commitment period, redownload buffer size}. */ -std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers); - +HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_headers); /** Find a near-optimal (period, bufsize) headers synchronization configuration for: * @@ -79,8 +84,7 @@ std::pair<size_t, size_t> ComputeHeadersSyncParams(std::chrono::seconds timespan * This is the internal part of ComputeHeadersSyncParams that performs the actual optimization. * Test-only. */ -std::pair<size_t, size_t> ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers); - +HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t minchainwork_headers, double attack_headers); /** HeadersSyncState: * diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h index e9761f70ae..61c734b0a4 100644 --- a/src/kernel/chainparams.h +++ b/src/kernel/chainparams.h @@ -14,7 +14,6 @@ #include <util/hash_type.h> #include <util/vector.h> -#include <cstddef> #include <cstdint> #include <memory> #include <optional> @@ -60,15 +59,6 @@ struct ChainTxData { double dTxRate; //!< estimated number of transactions per second after that timestamp }; -//! Configuration for headers sync memory usage. -struct HeadersSyncParams { - //! Distance in blocks between header commitments. - size_t commitment_period{0}; - //! Minimum number of validated headers to accumulate in the redownload - //! buffer before feeding them into the permanent block index. - size_t redownload_buffer_size{0}; -}; - /** * CChainParams defines various tweakable parameters of a given instance of the * Bitcoin system. diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 5524e16b4f..ea800ba9a3 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2141,13 +2141,13 @@ std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrm static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams, const std::optional<std::pair<size_t, size_t>>& precomputed) { - const auto [period, bufsize] = precomputed ? *precomputed : [&] { + const auto params{precomputed ? HeadersSyncParams{.commitment_period = precomputed->first, .redownload_buffer_size = precomputed->second} : [&] { const auto genesis_time{NodeSeconds{std::chrono::seconds{chainparams.GenesisBlock().nTime}}}; const auto timespan{std::chrono::duration_cast<std::chrono::seconds>(NodeClock::now() - genesis_time)}; return ComputeHeadersSyncParams(timespan, chainparams.GetConsensus().minchainwork_height); - }(); - LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u\n", period, bufsize); - return HeadersSyncParams{.commitment_period = period, .redownload_buffer_size = bufsize}; + }()}; + LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u", params.commitment_period, params.redownload_buffer_size); + return params; } PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman, ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 06:20:51 +00:00

014aff1 net_processing: compute headers sync parameters at runtime:

With the rewrite down to about 3 µs per call and fixed clocks already in these fuzz targets, is the override still worth keeping?

Use the existing fixed fuzz clocks

(Based on previous local suggestions, so this patch may not apply cleanly)

diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index ea800ba9a3..d107c5fbc8 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -2135,17 +2135,11 @@ std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrm
     return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, warnings, opts);
 }
 
-//! Compute the headers sync DoS-protection parameters for the given chain (unless precomputed
-//! ones are provided), using its age (genesis to now) and the height at which its minimum chain
-//! work was taken, and log the result.
-static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams,
-                                               const std::optional<std::pair<size_t, size_t>>& precomputed)
+//! Compute the headers sync DoS-protection parameters for the given chain, using its age (genesis
+//! to now) and the height at which its minimum chain work was taken, and log the result.
+static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams)
 {
-    const auto params{precomputed ? HeadersSyncParams{.commitment_period = precomputed->first, .redownload_buffer_size = precomputed->second} : [&] {
-        const auto genesis_time{NodeSeconds{std::chrono::seconds{chainparams.GenesisBlock().nTime}}};
-        const auto timespan{std::chrono::duration_cast<std::chrono::seconds>(NodeClock::now() - genesis_time)};
-        return ComputeHeadersSyncParams(timespan, chainparams.GetConsensus().minchainwork_height);
-    }()};
+    const auto params{ComputeHeadersSyncParams(Now<NodeSeconds>() - chainparams.GenesisBlock().Time(), chainparams.GetConsensus().minchainwork_height)};
     LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u", params.commitment_period, params.redownload_buffer_size);
     return params;
 }
@@ -2156,7 +2150,7 @@ PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
     : m_rng{opts.deterministic_rng},
       m_fee_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}, m_rng},
       m_chainparams(chainman.GetParams()),
-      m_headers_sync_params{MakeHeadersSyncParams(m_chainparams, opts.headers_sync_params)},
+      m_headers_sync_params{MakeHeadersSyncParams(m_chainparams)},
       m_connman(connman),
       m_addrman(addrman),
       m_banman(banman),
diff --git a/src/net_processing.h b/src/net_processing.h
index 3a9ecbab63..edaf48418f 100644
--- a/src/net_processing.h
+++ b/src/net_processing.h
@@ -18,12 +18,10 @@
 
 #include <atomic>
 #include <chrono>
-#include <cstddef>
 #include <cstdint>
 #include <memory>
 #include <optional>
 #include <string>
-#include <utility>
 #include <vector>
 
 class AddrMan;
@@ -112,9 +110,6 @@ public:
         bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
         //! Maximum per-second rate for sending transaction inventory to peers.
         unsigned int tx_send_rate{DEFAULT_TX_SEND_RATE};
-        //! Precomputed headers-sync parameters, as {commitment period, redownload buffer size};
-        //! when unset, they are computed at construction time (this is a test-only option).
-        std::optional<std::pair<size_t, size_t>> headers_sync_params{std::nullopt};
     };
 
     static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
diff --git a/src/test/fuzz/cmpctblock.cpp b/src/test/fuzz/cmpctblock.cpp
index 4f1e0df805..fea6229790 100644
--- a/src/test/fuzz/cmpctblock.cpp
+++ b/src/test/fuzz/cmpctblock.cpp
@@ -24,7 +24,6 @@
 #include <test/fuzz/FuzzedDataProvider.h>
 #include <test/fuzz/fuzz.h>
 #include <test/fuzz/util.h>
-#include <test/fuzz/util/headerssync.h>
 #include <test/fuzz/util/net.h>
 #include <test/util/net.h>
 #include <test/util/random.h>
@@ -141,7 +140,6 @@ FUZZ_TARGET(cmpctblock, .init = initialize_cmpctblock)
                                      mempool, *setup->m_node.warnings,
                                      PeerManager::Options{
                                          .deterministic_rng = true,
-                                         .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS,
                                      });
     connman.SetMsgProc(peerman.get());
 
diff --git a/src/test/fuzz/p2p_handshake.cpp b/src/test/fuzz/p2p_handshake.cpp
index 8d1c1b2373..0ab3e0c248 100644
--- a/src/test/fuzz/p2p_handshake.cpp
+++ b/src/test/fuzz/p2p_handshake.cpp
@@ -10,7 +10,6 @@
 #include <test/fuzz/FuzzedDataProvider.h>
 #include <test/fuzz/fuzz.h>
 #include <test/fuzz/util.h>
-#include <test/fuzz/util/headerssync.h>
 #include <test/fuzz/util/net.h>
 #include <test/util/net.h>
 #include <test/util/setup_common.h>
@@ -58,7 +57,6 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
                                      PeerManager::Options{
                                          .reconcile_txs = true,
                                          .deterministic_rng = true,
-                                         .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS,
                                      });
     connman.SetMsgProc(node.peerman.get());
     connman.SetAddrman(*node.addrman);
diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp
index e5320155a8..a63f8a0a6a 100644
--- a/src/test/fuzz/process_message.cpp
+++ b/src/test/fuzz/process_message.cpp
@@ -14,7 +14,6 @@
 #include <test/fuzz/FuzzedDataProvider.h>
 #include <test/fuzz/fuzz.h>
 #include <test/fuzz/util.h>
-#include <test/fuzz/util/headerssync.h>
 #include <test/fuzz/util/net.h>
 #include <test/util/net.h>
 #include <test/util/random.h>
@@ -91,7 +90,6 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
                                      PeerManager::Options{
                                          .reconcile_txs = true,
                                          .deterministic_rng = true,
-                                         .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS,
                                      });
 
     connman.SetMsgProc(node.peerman.get());
diff --git a/src/test/fuzz/process_messages.cpp b/src/test/fuzz/process_messages.cpp
index 4ede3ce674..f4a93df8dc 100644
--- a/src/test/fuzz/process_messages.cpp
+++ b/src/test/fuzz/process_messages.cpp
@@ -14,7 +14,6 @@
 #include <test/fuzz/FuzzedDataProvider.h>
 #include <test/fuzz/fuzz.h>
 #include <test/fuzz/util.h>
-#include <test/fuzz/util/headerssync.h>
 #include <test/fuzz/util/net.h>
 #include <test/util/net.h>
 #include <test/util/random.h>
@@ -83,7 +82,6 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
                                      PeerManager::Options{
                                          .reconcile_txs = true,
                                          .deterministic_rng = true,
-                                         .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS,
                                      });
     connman.SetMsgProc(node.peerman.get());
     connman.SetAddrman(*node.addrman);
diff --git a/src/test/fuzz/util/headerssync.h b/src/test/fuzz/util/headerssync.h
deleted file mode 100644
index abfea0ab68..0000000000
--- a/src/test/fuzz/util/headerssync.h
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright (c) The Bitcoin Core developers
-// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#ifndef BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H
-#define BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H
-
-#include <cstddef>
-#include <utility>
-
-/** Fixed headers-sync parameters for fuzz targets that construct a PeerManager per iteration,
- *  avoiding both recomputing them every time and any dependence on the (mocked) clock at
- *  construction time, as {commitment period, redownload buffer size}. The values are the regtest
- *  constants that were committed in chainparams before the parameters became runtime-computed,
- *  but any sane values would do. */
-inline constexpr std::pair<size_t, size_t> FUZZ_HEADERS_SYNC_PARAMS{275, 7017};
-
-#endif // BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H
> [014aff1](https://github.com/bitcoin/bitcoin/pull/35642/changes/014aff1b813a31a9a8d0185fe01f641581088e0a) _net_processing: compute headers sync parameters at runtime_: With the rewrite down to about 3 µs per call and fixed clocks already in these fuzz targets, is the override still worth keeping? <details> <summary>Use the existing fixed fuzz clocks</summary> (Based on previous local suggestions, so this patch may not apply cleanly) ```diff diff --git a/src/net_processing.cpp b/src/net_processing.cpp index ea800ba9a3..d107c5fbc8 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2135,17 +2135,11 @@ std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrm return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, warnings, opts); } -//! Compute the headers sync DoS-protection parameters for the given chain (unless precomputed -//! ones are provided), using its age (genesis to now) and the height at which its minimum chain -//! work was taken, and log the result. -static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams, - const std::optional<std::pair<size_t, size_t>>& precomputed) +//! Compute the headers sync DoS-protection parameters for the given chain, using its age (genesis +//! to now) and the height at which its minimum chain work was taken, and log the result. +static HeadersSyncParams MakeHeadersSyncParams(const CChainParams& chainparams) { - const auto params{precomputed ? HeadersSyncParams{.commitment_period = precomputed->first, .redownload_buffer_size = precomputed->second} : [&] { - const auto genesis_time{NodeSeconds{std::chrono::seconds{chainparams.GenesisBlock().nTime}}}; - const auto timespan{std::chrono::duration_cast<std::chrono::seconds>(NodeClock::now() - genesis_time)}; - return ComputeHeadersSyncParams(timespan, chainparams.GetConsensus().minchainwork_height); - }()}; + const auto params{ComputeHeadersSyncParams(Now<NodeSeconds>() - chainparams.GenesisBlock().Time(), chainparams.GetConsensus().minchainwork_height)}; LogInfo("Headers sync parameters: commitment period=%u, redownload buffer size=%u", params.commitment_period, params.redownload_buffer_size); return params; } @@ -2156,7 +2150,7 @@ PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman, : m_rng{opts.deterministic_rng}, m_fee_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}, m_rng}, m_chainparams(chainman.GetParams()), - m_headers_sync_params{MakeHeadersSyncParams(m_chainparams, opts.headers_sync_params)}, + m_headers_sync_params{MakeHeadersSyncParams(m_chainparams)}, m_connman(connman), m_addrman(addrman), m_banman(banman), diff --git a/src/net_processing.h b/src/net_processing.h index 3a9ecbab63..edaf48418f 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -18,12 +18,10 @@ #include <atomic> #include <chrono> -#include <cstddef> #include <cstdint> #include <memory> #include <optional> #include <string> -#include <utility> #include <vector> class AddrMan; @@ -112,9 +110,6 @@ public: bool private_broadcast{DEFAULT_PRIVATE_BROADCAST}; //! Maximum per-second rate for sending transaction inventory to peers. unsigned int tx_send_rate{DEFAULT_TX_SEND_RATE}; - //! Precomputed headers-sync parameters, as {commitment period, redownload buffer size}; - //! when unset, they are computed at construction time (this is a test-only option). - std::optional<std::pair<size_t, size_t>> headers_sync_params{std::nullopt}; }; static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman, diff --git a/src/test/fuzz/cmpctblock.cpp b/src/test/fuzz/cmpctblock.cpp index 4f1e0df805..fea6229790 100644 --- a/src/test/fuzz/cmpctblock.cpp +++ b/src/test/fuzz/cmpctblock.cpp @@ -24,7 +24,6 @@ #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> -#include <test/fuzz/util/headerssync.h> #include <test/fuzz/util/net.h> #include <test/util/net.h> #include <test/util/random.h> @@ -141,7 +140,6 @@ FUZZ_TARGET(cmpctblock, .init = initialize_cmpctblock) mempool, *setup->m_node.warnings, PeerManager::Options{ .deterministic_rng = true, - .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS, }); connman.SetMsgProc(peerman.get()); diff --git a/src/test/fuzz/p2p_handshake.cpp b/src/test/fuzz/p2p_handshake.cpp index 8d1c1b2373..0ab3e0c248 100644 --- a/src/test/fuzz/p2p_handshake.cpp +++ b/src/test/fuzz/p2p_handshake.cpp @@ -10,7 +10,6 @@ #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> -#include <test/fuzz/util/headerssync.h> #include <test/fuzz/util/net.h> #include <test/util/net.h> #include <test/util/setup_common.h> @@ -58,7 +57,6 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize) PeerManager::Options{ .reconcile_txs = true, .deterministic_rng = true, - .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS, }); connman.SetMsgProc(node.peerman.get()); connman.SetAddrman(*node.addrman); diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp index e5320155a8..a63f8a0a6a 100644 --- a/src/test/fuzz/process_message.cpp +++ b/src/test/fuzz/process_message.cpp @@ -14,7 +14,6 @@ #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> -#include <test/fuzz/util/headerssync.h> #include <test/fuzz/util/net.h> #include <test/util/net.h> #include <test/util/random.h> @@ -91,7 +90,6 @@ FUZZ_TARGET(process_message, .init = initialize_process_message) PeerManager::Options{ .reconcile_txs = true, .deterministic_rng = true, - .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS, }); connman.SetMsgProc(node.peerman.get()); diff --git a/src/test/fuzz/process_messages.cpp b/src/test/fuzz/process_messages.cpp index 4ede3ce674..f4a93df8dc 100644 --- a/src/test/fuzz/process_messages.cpp +++ b/src/test/fuzz/process_messages.cpp @@ -14,7 +14,6 @@ #include <test/fuzz/FuzzedDataProvider.h> #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> -#include <test/fuzz/util/headerssync.h> #include <test/fuzz/util/net.h> #include <test/util/net.h> #include <test/util/random.h> @@ -83,7 +82,6 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages) PeerManager::Options{ .reconcile_txs = true, .deterministic_rng = true, - .headers_sync_params = FUZZ_HEADERS_SYNC_PARAMS, }); connman.SetMsgProc(node.peerman.get()); connman.SetAddrman(*node.addrman); diff --git a/src/test/fuzz/util/headerssync.h b/src/test/fuzz/util/headerssync.h deleted file mode 100644 index abfea0ab68..0000000000 --- a/src/test/fuzz/util/headerssync.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H -#define BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H - -#include <cstddef> -#include <utility> - -/** Fixed headers-sync parameters for fuzz targets that construct a PeerManager per iteration, - * avoiding both recomputing them every time and any dependence on the (mocked) clock at - * construction time, as {commitment period, redownload buffer size}. The values are the regtest - * constants that were committed in chainparams before the parameters became runtime-computed, - * but any sane values would do. */ -inline constexpr std::pair<size_t, size_t> FUZZ_HEADERS_SYNC_PARAMS{275, 7017}; - -#endif // BITCOIN_TEST_FUZZ_UTIL_HEADERSSYNC_H ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-08 07:29:24 +00:00

b819b05 headerssync: add computation of optimal sync parameters:

Nit: this only tests ComputeHeadersSyncParamsInner (which doesn’t need the node setup), could we drop the fixture?

Drop the unnecessary test fixture
diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp
index 4a3000cdb2..a093bf6287 100644
--- a/src/test/headerssync_tests.cpp
+++ b/src/test/headerssync_tests.cpp
@@ -4,13 +4,11 @@
 
 #include <headerssync.h>
 
-#include <test/util/setup_common.h>
-
 #include <boost/test/unit_test.hpp>
 
 #include <cstdint>
 
-BOOST_FIXTURE_TEST_SUITE(headerssync_tests, BasicTestingSetup)
+BOOST_AUTO_TEST_SUITE(headerssync_tests)
 
 // Test vectors checking that ComputeHeadersSyncParamsInner() finds the optimal (period, bufsize)
 // configuration, each a tuple containing:
> [b819b05](https://github.com/bitcoin/bitcoin/pull/35642/changes/b819b057075bd682fab763be76a73e15123e3826) _headerssync: add computation of optimal sync parameters_: Nit: this only tests `ComputeHeadersSyncParamsInner` (which doesn’t need the node setup), could we drop the fixture? <details> <summary>Drop the unnecessary test fixture</summary> ```diff diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp index 4a3000cdb2..a093bf6287 100644 --- a/src/test/headerssync_tests.cpp +++ b/src/test/headerssync_tests.cpp @@ -4,13 +4,11 @@ #include <headerssync.h> -#include <test/util/setup_common.h> - #include <boost/test/unit_test.hpp> #include <cstdint> -BOOST_FIXTURE_TEST_SUITE(headerssync_tests, BasicTestingSetup) +BOOST_AUTO_TEST_SUITE(headerssync_tests) // Test vectors checking that ComputeHeadersSyncParamsInner() finds the optimal (period, bufsize) // configuration, each a tuple containing: ``` </details>
sipa (Migrated from github.com) reviewed 2026-10-08 18:54:31 +00:00
sipa (Migrated from github.com) commented 2026-10-08 18:54:39 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-08 18:57:18 +00:00

I didn't take this, as it risks a disconnect between the two (e.g. a new ExtraCompressedHeader is introduced but only one of the two locations is updated to use it). Could be resolved by exporting the actually used COMPACT_HEADER_SIZE, and asserting on that in the test, but I feel that's overkill.

I didn't take this, as it risks a disconnect between the two (e.g. a new `ExtraCompressedHeader` is introduced but only one of the two locations is updated to use it). Could be resolved by exporting the actually used `COMPACT_HEADER_SIZE`, and asserting on that in the test, but I feel that's overkill.
sipa (Migrated from github.com) commented 2026-10-08 19:00:12 +00:00

It's true that the need for exactness is less relevant than in earlier iterations of this PR. But I still like the fact that it actually finds the optimal solution (within rounding precision) to a well-defined (but weakened) variant of the real problem, so a more naive search will find the exact same solutions.

To demonstrate that, I added a commit that does a more naive search on random inputs.

It's true that the need for exactness is less relevant than in earlier iterations of this PR. But I still like the fact that it actually finds *the* optimal solution (within rounding precision) to a well-defined (but weakened) variant of the real problem, so a more naive search will find the exact same solutions. To demonstrate that, I added a commit that does a more naive search on random inputs.
sipa (Migrated from github.com) commented 2026-10-08 19:03:53 +00:00

Done.

The reason for not doing this was my (folklore, perhaps) aversion due to division and modulus operators which are very slow instructions (as far as CPU instructions go; they used to be ~50s slower than multiplication IIRC).

Modern compilers don't need this, as will turn the computation of both a division and a modulus into a single instruction anyway, so I took your change.

Done. The reason for not doing this was my (folklore, perhaps) aversion due to division and modulus operators which are very slow instructions (as far as CPU instructions go; they used to be ~50s slower than multiplication IIRC). Modern compilers don't need this, as will turn the computation of both a division and a modulus into a single instruction anyway, so I took your change.
sipa (Migrated from github.com) commented 2026-10-08 19:04:02 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-08 19:04:12 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-08 19:04:17 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-08 19:05:11 +00:00

Nice catch. Fixed, but with a much better alternative: do the bisection over integer bufsize values, as those are the only ones we care about anyway. Avoids floating-point shenanigans like these, and is faster too.

Nice catch. Fixed, but with a much better alternative: do the bisection over integer `bufsize` values, as those are the only ones we care about anyway. Avoids floating-point shenanigans like these, and is faster too.
sipa (Migrated from github.com) commented 2026-10-08 19:06:25 +00:00

Done, in a later commit.

The reason for this approach (still kept in the first commit) is that doing it while such parameters are still defined in chainparams would need a cyclic dependency. With the later commit that removes them from chainparams, it's possible to move the HeadersSyncParams to headerssync.h.

Done, in a later commit. The reason for this approach (still kept in the first commit) is that doing it while such parameters are still defined in chainparams would need a cyclic dependency. With the later commit that removes them from chainparams, it's possible to move the `HeadersSyncParams` to headerssync.h.
sipa (Migrated from github.com) commented 2026-10-08 19:06:42 +00:00

Done, and attributed.

Done, and attributed.
sipa (Migrated from github.com) commented 2026-10-08 18:54:31 +00:00

Done.

Done.
l0rinc (Migrated from github.com) approved these changes 2026-10-09 08:31:27 +00:00
l0rinc (Migrated from github.com) left a comment

Only had time for a quick pass and left a few questions. I'll review more carefully later.

Only had time for a quick pass and left a few questions. I'll review more carefully later.
l0rinc (Migrated from github.com) commented 2026-10-09 06:27:59 +00:00

Since this is the optimum for our simplified model of the attack surface, how much confidence do we have that the extra precision translates into a better tradeoff in practice?
Honest question, I'm still working on fully understanding it.
Could a slightly less memory-efficient result within that model be worth a simpler calculation, while preserving the same security bound?

I won't block either way, but the added complexity makes it harder for me to ACK confidently.

Since this is the optimum for our simplified model of the attack surface, how much confidence do we have that the extra precision translates into a better tradeoff in practice? Honest question, I'm still working on fully understanding it. Could a slightly less memory-efficient result within that model be worth a simpler calculation, while preserving the same security bound? I won't block either way, but the added complexity makes it harder for me to ACK confidently.
l0rinc (Migrated from github.com) commented 2026-10-09 08:23:35 +00:00

e5c9dba headerssync: add computation of optimal sync parameters:

Nit: With the integer search, a zero height would give Step 2 invalid clamp bounds, so I would update this explanation. The earlier "real-valued" wording also helped me see that this formula allows fractional inputs.

Refresh the comments after the search rewrite
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 4bdc6f8ff0..40b4a05057 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -448,8 +448,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
     // static_assert to give a more targeted error message.
     static_assert(COMPACT_HEADER_SIZE == 384);
 
-    // The chain has at least one header. Otherwise Step 1 below returns a zero period, which
-    // Step 2 then divides by.
+    // Step 2 requires a nonempty range of legal periods [1, minchainwork_headers]
     Assert(minchainwork_headers > 0);
     // Some small attacker success rate is inevitable. A non-positive one cannot be reached, which
     // would make Step 1 below compute with infinities, and the search for bufsize not terminate.
@@ -516,7 +515,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         //
         // Both of these turn into exact equalities in the continuous relaxation:
         // (1) becomes HeadersSyncAttackRate(period, bufsize) = attack_headers, because there is no need
-        //     overshoot.
+        //     to overshoot.
         // (2) becomes mem_timewarp(period, bufsize) = mem_mainchain(period, bufsize), because
         //     timewarp memory goes down with increasing period, and mainchain memory goes up with
         //     increasing period (with bufsize set to keep the attack rate constant). The maximum
@@ -525,7 +524,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         //     practically always true.
         //
         // For the continuous relaxation, the expression from HeadersSyncAttackRate is used with
-        // period and bufsize (and thus M in [0, P)). As a function of bufsize it is continuous and
+        // real-valued period and bufsize (and thus M in [0, P)). As a function of bufsize it is continuous and
         // strictly decreasing, and for the bufsize values sharing a given K it takes values in
         // (2^-K * (3*P - 1) / 4, 2^-K * (3*P - 1) / 2]. Thus (1) can be solved for bufsize
         // directly: K is the unique integer for which attack_headers falls in that range, after
> [e5c9dba](https://github.com/bitcoin/bitcoin/pull/35642/changes/e5c9dba365881706424023b222a1f7e5bdba0c10) _headerssync: add computation of optimal sync parameters_: Nit: With the integer search, a zero height would give Step 2 invalid clamp bounds, so I would update this explanation. The earlier "real-valued" wording also helped me see that this formula allows fractional inputs. <details> <summary>Refresh the comments after the search rewrite</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 4bdc6f8ff0..40b4a05057 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -448,8 +448,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // static_assert to give a more targeted error message. static_assert(COMPACT_HEADER_SIZE == 384); - // The chain has at least one header. Otherwise Step 1 below returns a zero period, which - // Step 2 then divides by. + // Step 2 requires a nonempty range of legal periods [1, minchainwork_headers] Assert(minchainwork_headers > 0); // Some small attacker success rate is inevitable. A non-positive one cannot be reached, which // would make Step 1 below compute with infinities, and the search for bufsize not terminate. @@ -516,7 +515,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // // Both of these turn into exact equalities in the continuous relaxation: // (1) becomes HeadersSyncAttackRate(period, bufsize) = attack_headers, because there is no need - // overshoot. + // to overshoot. // (2) becomes mem_timewarp(period, bufsize) = mem_mainchain(period, bufsize), because // timewarp memory goes down with increasing period, and mainchain memory goes up with // increasing period (with bufsize set to keep the attack rate constant). The maximum @@ -525,7 +524,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // practically always true. // // For the continuous relaxation, the expression from HeadersSyncAttackRate is used with - // period and bufsize (and thus M in [0, P)). As a function of bufsize it is continuous and + // real-valued period and bufsize (and thus M in [0, P)). As a function of bufsize it is continuous and // strictly decreasing, and for the bufsize values sharing a given K it takes values in // (2^-K * (3*P - 1) / 4, 2^-K * (3*P - 1) / 2]. Thus (1) can be solved for bufsize // directly: K is the unique integer for which attack_headers falls in that range, after ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-09 06:20:28 +00:00

Thanks, please resolve

Thanks, please resolve
l0rinc (Migrated from github.com) commented 2026-10-09 06:18:34 +00:00

e5c9dba headerssync: add computation of optimal sync parameters:

Nit: Now that both searches use integer bounds, we could use std::midpoint in both places.

Use the standard midpoint helper
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 19b9b517f4..4bdc6f8ff0 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -13,6 +13,7 @@
 #include <algorithm>
 #include <cmath>
 #include <cstdint>
+#include <numeric>
 #include <tuple>
 #include <utility>
 
@@ -481,7 +482,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         int64_t succ_buf = fail_buf / 2;
         // Then perform a bisection search to narrow it down.
         while (fail_buf > succ_buf + 1) {
-            const int64_t try_buf = (succ_buf + fail_buf) / 2;
+            const int64_t try_buf = std::midpoint(succ_buf, fail_buf);
             if (HeadersSyncAttackRate(period, try_buf) >= attack_headers) {
                 succ_buf = try_buf;
             } else {
@@ -554,7 +555,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         int64_t lo = 0;
         int64_t hi = minchainwork_headers + 1;
         while (hi - lo > 1) {
-            const int64_t mid = lo + (hi - lo) / 2;
+            const int64_t mid = std::midpoint(lo, hi);
             if (mid * cont_bufsize(mid) < target) {
                 lo = mid;
             } else {
> [e5c9dba](https://github.com/bitcoin/bitcoin/pull/35642/changes/e5c9dba365881706424023b222a1f7e5bdba0c10) _headerssync: add computation of optimal sync parameters_: Nit: Now that both searches use integer bounds, we could use `std::midpoint` in both places. <details> <summary>Use the standard midpoint helper</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 19b9b517f4..4bdc6f8ff0 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -13,6 +13,7 @@ #include <algorithm> #include <cmath> #include <cstdint> +#include <numeric> #include <tuple> #include <utility> @@ -481,7 +482,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min int64_t succ_buf = fail_buf / 2; // Then perform a bisection search to narrow it down. while (fail_buf > succ_buf + 1) { - const int64_t try_buf = (succ_buf + fail_buf) / 2; + const int64_t try_buf = std::midpoint(succ_buf, fail_buf); if (HeadersSyncAttackRate(period, try_buf) >= attack_headers) { succ_buf = try_buf; } else { @@ -554,7 +555,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min int64_t lo = 0; int64_t hi = minchainwork_headers + 1; while (hi - lo > 1) { - const int64_t mid = lo + (hi - lo) / 2; + const int64_t mid = std::midpoint(lo, hi); if (mid * cont_bufsize(mid) < target) { lo = mid; } else { ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-09 08:28:40 +00:00

e5c9dba headerssync: add computation of optimal sync parameters:

Would it make sense to encapsulate the bounds on legal periods in the search, so Step 2 can use its two candidates directly?

Try the two endpoints of the legal bracket
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index 40b4a05057..01786fa0fc 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -494,9 +494,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
     // The overall strategy to solve our problem (minimizing peak memory usage while staying under
     // the acceptable attack rate), is as follows:
     //
-    // Step 1: assume that period and bufsize can be arbitrary real numbers, and find the largest
-    //         integer period which is still below the optimum of that continuous relaxation.
-    // Step 2: try that period and the next one, and return the one whose memory usage is lowest
+    // Step 1: assume that period and bufsize can be arbitrary real numbers, and find the two
+    //         adjacent legal integer periods around the optimum of that continuous relaxation.
+    // Step 2: try both periods, and return the one whose memory usage is lowest
     //         (breaking ties toward the smallest period).
     //
     // As a function of the period, the memory usage is the maximum of a decreasing (timewarp) and
@@ -506,9 +506,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
     // for absurdly high attack rates, and even then, it only results in memory usage slightly
     // above the optimal.
 
-    // Step 1: find the largest integer period below the optimum of the continuous relaxation of
-    //         the problem (0 if even the smallest legal period exceeds that optimum).
-    const auto period_below = [&]() {
+    // Step 1: find the adjacent legal periods around the optimum of the continuous relaxation of
+    //         the problem (or the two at the end of the legal range it lies beyond).
+    const auto [period_below, period_above] = [&] {
         // Abstractly, our goal is to find (period, bufsize):
         // (1) such that HeadersSyncAttackRate(period, bufsize) <= attack_headers
         // (2) such that memory_usage(period, bufsize) is minimal
@@ -545,14 +545,14 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
         //
         // whose left-hand side increases with period. The periods below the continuous optimum are
         // therefore exactly those whose left-hand side is still below the right-hand side, so the
-        // largest one can be found by bisection. As only integer periods are ever tried, that
-        // bisection can be performed over the integers, bracketed just outside the legal range so
-        // that an optimum below 1 or above minchainwork_headers yields that endpoint. Note that
+        // nearest legal periods can be found by bisection. As only integer periods are ever tried, that
+        // bisection can be performed over the integers within the legal range. If the optimum lies
+        // outside that range, return the two periods nearest that endpoint instead. Note that
         // the interval shrinks by construction on every iteration, independent of how accurately
         // the condition is evaluated.
         const auto target = double(max_headers - minchainwork_headers) / COMPACT_HEADER_SIZE;
-        int64_t lo = 0;
-        int64_t hi = minchainwork_headers + 1;
+        int64_t lo = 1;
+        int64_t hi = minchainwork_headers;
         while (hi - lo > 1) {
             const int64_t mid = std::midpoint(lo, hi);
             if (mid * cont_bufsize(mid) < target) {
@@ -561,15 +561,12 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
                 hi = mid;
             }
         }
-        return lo;
+        return std::pair{lo, hi};
     }();
 
-    // Step 2: try period_below and the period above it, and pick the one which, together with its
-    //         corresponding bufsize, results in the lowest memory usage. Both candidates are
-    //         clamped into the legal range, which matters only when the continuous optimum lies
-    //         outside of it.
-    const auto make_config = [&](int64_t candidate) noexcept {
-        const auto period = std::clamp<int64_t>(candidate, 1, minchainwork_headers);
+    // Step 2: try period_below and period_above, and pick the one which, together with its
+    //         corresponding bufsize, results in the lowest memory usage.
+    const auto make_config = [&](int64_t period) noexcept {
         const auto bufsize = find_bufsize(period);
         return std::tuple{memory_usage(period, bufsize), period, bufsize};
     };
@@ -577,7 +574,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min
     // low ones the smallest period.
     const auto [_memory, best_period, best_bufsize] = std::min(
         make_config(period_below),
-        make_config(period_below + 1));
+        make_config(period_above));
 
     return {.commitment_period = size_t(best_period), .redownload_buffer_size = size_t(best_bufsize)};
 }
> [e5c9dba](https://github.com/bitcoin/bitcoin/pull/35642/changes/e5c9dba365881706424023b222a1f7e5bdba0c10) _headerssync: add computation of optimal sync parameters_: Would it make sense to encapsulate the bounds on legal periods in the search, so Step 2 can use its two candidates directly? <details> <summary>Try the two endpoints of the legal bracket</summary> ```diff diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 40b4a05057..01786fa0fc 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -494,9 +494,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // The overall strategy to solve our problem (minimizing peak memory usage while staying under // the acceptable attack rate), is as follows: // - // Step 1: assume that period and bufsize can be arbitrary real numbers, and find the largest - // integer period which is still below the optimum of that continuous relaxation. - // Step 2: try that period and the next one, and return the one whose memory usage is lowest + // Step 1: assume that period and bufsize can be arbitrary real numbers, and find the two + // adjacent legal integer periods around the optimum of that continuous relaxation. + // Step 2: try both periods, and return the one whose memory usage is lowest // (breaking ties toward the smallest period). // // As a function of the period, the memory usage is the maximum of a decreasing (timewarp) and @@ -506,9 +506,9 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // for absurdly high attack rates, and even then, it only results in memory usage slightly // above the optimal. - // Step 1: find the largest integer period below the optimum of the continuous relaxation of - // the problem (0 if even the smallest legal period exceeds that optimum). - const auto period_below = [&]() { + // Step 1: find the adjacent legal periods around the optimum of the continuous relaxation of + // the problem (or the two at the end of the legal range it lies beyond). + const auto [period_below, period_above] = [&] { // Abstractly, our goal is to find (period, bufsize): // (1) such that HeadersSyncAttackRate(period, bufsize) <= attack_headers // (2) such that memory_usage(period, bufsize) is minimal @@ -545,14 +545,14 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // // whose left-hand side increases with period. The periods below the continuous optimum are // therefore exactly those whose left-hand side is still below the right-hand side, so the - // largest one can be found by bisection. As only integer periods are ever tried, that - // bisection can be performed over the integers, bracketed just outside the legal range so - // that an optimum below 1 or above minchainwork_headers yields that endpoint. Note that + // nearest legal periods can be found by bisection. As only integer periods are ever tried, that + // bisection can be performed over the integers within the legal range. If the optimum lies + // outside that range, return the two periods nearest that endpoint instead. Note that // the interval shrinks by construction on every iteration, independent of how accurately // the condition is evaluated. const auto target = double(max_headers - minchainwork_headers) / COMPACT_HEADER_SIZE; - int64_t lo = 0; - int64_t hi = minchainwork_headers + 1; + int64_t lo = 1; + int64_t hi = minchainwork_headers; while (hi - lo > 1) { const int64_t mid = std::midpoint(lo, hi); if (mid * cont_bufsize(mid) < target) { @@ -561,15 +561,12 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min hi = mid; } } - return lo; + return std::pair{lo, hi}; }(); - // Step 2: try period_below and the period above it, and pick the one which, together with its - // corresponding bufsize, results in the lowest memory usage. Both candidates are - // clamped into the legal range, which matters only when the continuous optimum lies - // outside of it. - const auto make_config = [&](int64_t candidate) noexcept { - const auto period = std::clamp<int64_t>(candidate, 1, minchainwork_headers); + // Step 2: try period_below and period_above, and pick the one which, together with its + // corresponding bufsize, results in the lowest memory usage. + const auto make_config = [&](int64_t period) noexcept { const auto bufsize = find_bufsize(period); return std::tuple{memory_usage(period, bufsize), period, bufsize}; }; @@ -577,7 +574,7 @@ HeadersSyncParams ComputeHeadersSyncParamsInner(int64_t max_headers, int64_t min // low ones the smallest period. const auto [_memory, best_period, best_bufsize] = std::min( make_config(period_below), - make_config(period_below + 1)); + make_config(period_above)); return {.commitment_period = size_t(best_period), .redownload_buffer_size = size_t(best_bufsize)}; } ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-09 06:16:03 +00:00

e5c9dba headerssync: add computation of optimal sync parameters:

Nit: The caller uses the current time, even when the local tip is still genesis. I’d describe this as genesis-to-now.

Match the age description to its caller
diff --git a/src/headerssync.h b/src/headerssync.h
index 74516120db..d3448d722b 100644
--- a/src/headerssync.h
+++ b/src/headerssync.h
@@ -68,7 +68,7 @@ struct HeadersSyncParams {
 
 /** Find a near-optimal (period, bufsize) headers synchronization configuration for:
  *
- * - A specified timespan between genesis and the tip of the chain.
+ * - A specified timespan between genesis and now.
  * - A specified number of headers in the minchainwork chain.
  *
  * See the constants in the implementation that control what attack success rate is acceptable.
> [e5c9dba](https://github.com/bitcoin/bitcoin/pull/35642/changes/e5c9dba365881706424023b222a1f7e5bdba0c10) _headerssync: add computation of optimal sync parameters_: Nit: The caller uses the current time, even when the local tip is still genesis. I’d describe this as genesis-to-now. <details> <summary>Match the age description to its caller</summary> ```diff diff --git a/src/headerssync.h b/src/headerssync.h index 74516120db..d3448d722b 100644 --- a/src/headerssync.h +++ b/src/headerssync.h @@ -68,7 +68,7 @@ struct HeadersSyncParams { /** Find a near-optimal (period, bufsize) headers synchronization configuration for: * - * - A specified timespan between genesis and the tip of the chain. + * - A specified timespan between genesis and now. * - A specified number of headers in the minchainwork chain. * * See the constants in the implementation that control what attack success rate is acceptable. ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-09 08:28:18 +00:00

9451c93 test: verify the sync parameters by brute force:

Nit: I would find the failing period more useful than a failure count here. Initializing the best memory to the maximum also avoids treating the first iteration differently.

Report the period at a boundary failure
diff --git a/src/net_processing.h b/src/net_processing.h
index 6f946bd98a..ec17e8f60a 100644
--- a/src/net_processing.h
+++ b/src/net_processing.h
@@ -23,7 +23,6 @@
 #include <memory>
 #include <optional>
 #include <string>
-#include <utility>
 #include <vector>
 
 class AddrMan;
diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp
index 78aaae7cfb..42237afa17 100644
--- a/src/test/headerssync_tests.cpp
+++ b/src/test/headerssync_tests.cpp
@@ -12,6 +12,7 @@
 #include <chrono>
 #include <cmath>
 #include <cstdint>
+#include <limits>
 
 BOOST_AUTO_TEST_SUITE(headerssync_tests)
 
@@ -130,20 +131,18 @@ static void CheckOptimalByBruteForce(int64_t max_headers, int64_t minchainwork_h
     constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8};
     int64_t best_period{0};
     int64_t best_bufsize{0};
-    int64_t best_mem{0};
+    int64_t best_mem{std::numeric_limits<int64_t>::max()};
     int64_t bufsize{1};
-    int64_t boundary_failures{0};
     for (int64_t period{1}; period <= minchainwork_headers; ++period) {
         while (HeadersSyncAttackRate(period, bufsize) >= attack_headers) ++bufsize;
-        if (bufsize > 1 && HeadersSyncAttackRate(period, bufsize - 1) < attack_headers) ++boundary_failures;
+        BOOST_CHECK_MESSAGE(HeadersSyncAttackRate(period, bufsize - 1) >= attack_headers, "period=" << period);
         const int64_t mem{std::max(max_headers / period, minchainwork_headers / period + bufsize * COMPACT_HEADER_SIZE)};
-        if (best_period == 0 || mem < best_mem) {
+        if (mem < best_mem) {
             best_period = period;
             best_bufsize = bufsize;
             best_mem = mem;
         }
     }
-    BOOST_CHECK_EQUAL(boundary_failures, 0);
     BOOST_CHECK_EQUAL(expected_period, best_period);
     BOOST_CHECK_EQUAL(expected_bufsize, best_bufsize);
 }
> [9451c93](https://github.com/bitcoin/bitcoin/pull/35642/changes/9451c93ff38435f2ce100af786b9221b0c565c5c) _test: verify the sync parameters by brute force_: Nit: I would find the failing period more useful than a failure count here. Initializing the best memory to the maximum also avoids treating the first iteration differently. <details> <summary>Report the period at a boundary failure</summary> ```diff diff --git a/src/net_processing.h b/src/net_processing.h index 6f946bd98a..ec17e8f60a 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -23,7 +23,6 @@ #include <memory> #include <optional> #include <string> -#include <utility> #include <vector> class AddrMan; diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp index 78aaae7cfb..42237afa17 100644 --- a/src/test/headerssync_tests.cpp +++ b/src/test/headerssync_tests.cpp @@ -12,6 +12,7 @@ #include <chrono> #include <cmath> #include <cstdint> +#include <limits> BOOST_AUTO_TEST_SUITE(headerssync_tests) @@ -130,20 +131,18 @@ static void CheckOptimalByBruteForce(int64_t max_headers, int64_t minchainwork_h constexpr int64_t COMPACT_HEADER_SIZE{sizeof(CompressedHeader) * 8}; int64_t best_period{0}; int64_t best_bufsize{0}; - int64_t best_mem{0}; + int64_t best_mem{std::numeric_limits<int64_t>::max()}; int64_t bufsize{1}; - int64_t boundary_failures{0}; for (int64_t period{1}; period <= minchainwork_headers; ++period) { while (HeadersSyncAttackRate(period, bufsize) >= attack_headers) ++bufsize; - if (bufsize > 1 && HeadersSyncAttackRate(period, bufsize - 1) < attack_headers) ++boundary_failures; + BOOST_CHECK_MESSAGE(HeadersSyncAttackRate(period, bufsize - 1) >= attack_headers, "period=" << period); const int64_t mem{std::max(max_headers / period, minchainwork_headers / period + bufsize * COMPACT_HEADER_SIZE)}; - if (best_period == 0 || mem < best_mem) { + if (mem < best_mem) { best_period = period; best_bufsize = bufsize; best_mem = mem; } } - BOOST_CHECK_EQUAL(boundary_failures, 0); BOOST_CHECK_EQUAL(expected_period, best_period); BOOST_CHECK_EQUAL(expected_bufsize, best_bufsize); } ``` </details>
sipa (Migrated from github.com) reviewed 2026-10-09 17:08:48 +00:00
sipa (Migrated from github.com) commented 2026-10-09 17:14:49 +00:00

I forgot where, but you commented somewhere about simplifying to remove the need to search for bufsize.

It turns out that no simplification is needed, since we already have a closed-form function that finds the optimal bufsize when relaxed to real numbers (formerly cont_bufsize, now renamed to real_bufsize). The integer version doesn't need bisection, it can just round real_bufsize up to an integer.

This still leaves a bisection search for the periods. That one is harder to get rid of. It's possible to simplify the attack_rate formula a bit more so the result can be found using a closed form expression with the Lambert W function, and evaluating that needs a loop still too. Simplifying it even further so that the solution can be written explicitly using just elementary functions would lose a great deal of accuracy.

I forgot where, but you commented somewhere about simplifying to remove the need to search for bufsize. It turns out that no simplification is needed, since we already have a closed-form function that finds the optimal bufsize when relaxed to real numbers (formerly `cont_bufsize`, now renamed to `real_bufsize`). The integer version doesn't need bisection, it can just round `real_bufsize` up to an integer. This still leaves a bisection search for the periods. That one is harder to get rid of. It's possible to simplify the `attack_rate` formula a bit more so the result can be found using a closed form expression with the [Lambert W](https://en.wikipedia.org/wiki/Lambert_W_function) function, and evaluating that needs a loop still too. Simplifying it even further so that the solution can be written explicitly using just elementary functions would lose a great deal of accuracy.
sipa (Migrated from github.com) commented 2026-10-09 17:16:04 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-09 17:16:17 +00:00

Done. Cool, I didn't know about std::midpoint.

Done. Cool, I didn't know about `std::midpoint`.
sipa (Migrated from github.com) commented 2026-10-09 17:16:27 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-09 17:16:33 +00:00

Done.

Done.
sipa (Migrated from github.com) commented 2026-10-09 17:08:48 +00:00

I have dropped this commit, because attack_rate is now gone entirely (see further). I could bring it back, with a test-only copy of the old attack_rate code if desirable, but it seems overkill now.

I have dropped this commit, because `attack_rate` is now gone entirely (see further). I could bring it back, with a test-only copy of the old `attack_rate` code if desirable, but it seems overkill now.
l0rinc (Migrated from github.com) approved these changes 2026-10-10 14:18:46 +00:00
l0rinc (Migrated from github.com) left a comment

Light ACK 02d1084eba

I'd appreciate another pair of eyes on the math.
The clock correction case still seems worth addressing here or in a follow-up, but isn't blocking for me, it's not systemic.

Light ACK 02d1084eba30de70c45eeaee77312fd417f79bd8 I'd appreciate another pair of eyes on the math. The clock correction case still seems worth addressing here or in a follow-up, but isn't blocking for me, it's not systemic.
l0rinc (Migrated from github.com) commented 2026-10-10 01:49:05 +00:00

Thanks, please resolve

Thanks, please resolve
l0rinc (Migrated from github.com) commented 2026-10-10 13:43:01 +00:00

02d1084 contrib: remove headerssync-params.py:

Similarly to #35208, could an incorrect startup clock leave us with an inflated memory allowance after the clock is corrected? Fresh nodes seem to skip the clock check against the stored chain tip for some reason: github.com/bitcoin/bitcoin@02d1084eba/src/node/chainstate.cpp (L249-L255)

According to my clanker, starting with a clock near the Unix epoch and correcting it before headers sync could leave the per-peer commitment allowance about 50x higher than intended - could we defer the estimate after the clock checks?

> [02d1084](https://github.com/bitcoin/bitcoin/pull/35642/changes/02d1084eba30de70c45eeaee77312fd417f79bd8) _contrib: remove headerssync-params.py_: Similarly to #35208, could an incorrect startup clock leave us with an inflated memory allowance after the clock is corrected? Fresh nodes seem to skip the clock check against the stored chain tip for some reason: https://github.com/bitcoin/bitcoin/blob/02d1084eba30de70c45eeaee77312fd417f79bd8/src/node/chainstate.cpp#L249-L255 According to my clanker, starting with a clock near the Unix epoch and correcting it before headers sync could leave the per-peer commitment allowance about 50x higher than intended - could we defer the estimate after the clock checks?
l0rinc (Migrated from github.com) commented 2026-10-10 13:42:27 +00:00

02d1084 contrib: remove headerssync-params.py:

Nit: A few commit message and PR details my clankers found, just resolve if irrelevant:

  • The first commit message still describes finding the largest period below the optimum. It could describe the two candidates within the legal range instead, including when the optimum lies outside it.
  • The fuzz commit says "any representable timespan" and "the entire range of legal inputs", but the targets use bounded ranges. "A broad range of inputs" and "the portable timespan range" seem more accurate.
  • In the PR description, "batches of up to 2000" would leave room for the shorter final batch mentioned in the next sentence.
  • The fuzz comment says "roughly twice", but these bounds are closer to three times the current values. "Above" would avoid pinning that explanation to a particular ratio.
> [02d1084](https://github.com/bitcoin/bitcoin/pull/35642/changes/02d1084eba30de70c45eeaee77312fd417f79bd8) _contrib: remove headerssync-params.py_: Nit: A few commit message and PR details my clankers found, just resolve if irrelevant: - The first commit message still describes finding the largest period below the optimum. It could describe the two candidates within the legal range instead, including when the optimum lies outside it. - The fuzz commit says "any representable timespan" and "the entire range of legal inputs", but the targets use bounded ranges. "A broad range of inputs" and "the portable timespan range" seem more accurate. - In the PR description, "batches of up to 2000" would leave room for the shorter final batch mentioned in the next sentence. - The fuzz comment says "roughly twice", but these bounds are closer to three times the current values. "Above" would avoid pinning that explanation to a particular ratio.
hodlinator (Migrated from github.com) reviewed 2026-10-13 12:18:14 +00:00
hodlinator (Migrated from github.com) left a comment

Human linter comments for 02d1084eba

Will see if I find time to dig into the math.

Concept ACK!

Human linter comments for 02d1084eba30de70c45eeaee77312fd417f79bd8 Will see if I find time to dig into the math. Concept ACK!
hodlinator (Migrated from github.com) commented 2026-10-01 12:56:59 +00:00

nanonit: If you touch again, could const and brace-initialize to further tighten the screws (mutation+anti-narrowing verification).

    const int64_t next_height{m_current_height + 1};
nanonit: If you touch again, could const and brace-initialize to further tighten the screws (mutation+anti-narrowing verification). ```suggestion const int64_t next_height{m_current_height + 1}; ```
Lines 356-380
hodlinator (Migrated from github.com) commented 2026-10-13 11:23:52 +00:00

This first section is somewhat duplicating the header:
github.com/bitcoin/bitcoin@4bacf21a13/src/headerssync.h (L59-L100)

Could we refer to that instead, boil this down further, or merge them?

This first section is somewhat duplicating the header: https://github.com/bitcoin/bitcoin/blob/4bacf21a13c2ed25ef9362ca38f26bf0a67d22c9/src/headerssync.h#L59-L100 Could we refer to that instead, boil this down further, or merge them?
Lines 400-402
hodlinator (Migrated from github.com) commented 2026-10-01 17:28:34 +00:00

nit: Might as well use static_assert message, and specify "case records".

    static_assert(COMPACT_HEADER_SIZE == 384,
        "Unit test case records will need to be regenerated if this constant were to change.");
nit: Might as well use `static_assert` message, and specify "case records". ```suggestion static_assert(COMPACT_HEADER_SIZE == 384, "Unit test case records will need to be regenerated if this constant were to change."); ```
hodlinator (Migrated from github.com) commented 2026-10-01 17:42:55 +00:00

remark: "commitment phase" is clearer than "presync phase". Wish it was used everywhere in older code.

remark: "commitment phase" is clearer than "presync phase". Wish it was used everywhere in older code.
hodlinator (Migrated from github.com) commented 2026-10-01 18:11:04 +00:00

nit: This lambda could be declared inside make_config to decrease cognitive complexity.

nit: This lambda could be declared inside `make_config` to decrease cognitive complexity.
hodlinator (Migrated from github.com) commented 2026-10-13 12:04:51 +00:00

Feels clearer explicitly capture here and for make_config:

    const auto int_bufsize = [real_bufsize](int64_t period) noexcept {
Feels clearer explicitly capture here and for `make_config`: ```suggestion const auto int_bufsize = [real_bufsize](int64_t period) noexcept { ```
hodlinator (Migrated from github.com) commented 2026-10-01 13:22:30 +00:00

Rather than magic numbers, could we use sizeof(CBlockHeader) or compute it somehow, or improve the doc-string?
80 is the value of sizeof(CBlockHeader) but the 81 constant appears in headerssync-params.py on master. I would think it typically serializes down to something smaller due to compact integers, but then there is overhead for the network magic, message type etc?

Rather than magic numbers, could we use `sizeof(CBlockHeader)` or compute it somehow, or improve the doc-string? 80 is the value of `sizeof(CBlockHeader)` but the 81 constant appears in headerssync-params.py on master. I would think it typically serializes down to something smaller due to compact integers, but then there is overhead for the network magic, message type etc?
hodlinator (Migrated from github.com) commented 2026-10-01 13:09:28 +00:00

nanonit: It is used internally in production, as stated in the line above.

 * Only exposed for tests.
nanonit: It is used internally in production, as stated in the line above. ```suggestion * Only exposed for tests. ```
hodlinator (Migrated from github.com) commented 2026-10-13 11:41:15 +00:00

Would change it to:

    // Well over double what the optimizer computes for mainnet at the time of writing, hardcoded so that the covered
Would change it to: ```suggestion // Well over double what the optimizer computes for mainnet at the time of writing, hardcoded so that the covered ```
hodlinator (Migrated from github.com) commented 2026-10-13 11:57:31 +00:00

While the comment is quite descriptive, I feel that it is sufficiently advanced that including the code for generating and filtering would be helpful for the next human needing update the record values. Maybe you consider the comment sufficient for LLMs and therefore not worth including dead code which harms code coverage stats?

While the comment is quite descriptive, I feel that it is sufficiently advanced that including the code for generating and filtering would be helpful for the next human needing update the record values. Maybe you consider the comment sufficient for LLMs and therefore not worth including dead code which harms code coverage stats?
sipa (Migrated from github.com) reviewed 2026-10-20 09:39:08 +00:00
sipa (Migrated from github.com) commented 2026-10-20 09:39:08 +00:00

Historically, CBlockHeader was not a separate type, and headers were relayed as CBlock-with-0-transactions. That's where the value 81 comes from, and it is exact: there is just a trailing 0x00 byte at the end for the number of transactions.

The overhead from the P2P transport (length/magic/command/checksum in V1, length/header/auth in V2) doesn't apply here because those are paid only once (and a single headers message can carry 2000 of them).

Historically, `CBlockHeader` was not a separate type, and headers were relayed as `CBlock`-with-0-transactions. That's where the value 81 comes from, and it is exact: there is just a trailing 0x00 byte at the end for the number of transactions. The overhead from the P2P transport (length/magic/command/checksum in V1, length/header/auth in V2) doesn't apply here because those are paid only once (and a single `headers` message can carry 2000 of them).
hodlinator (Migrated from github.com) reviewed 2026-10-20 12:19:41 +00:00
hodlinator (Migrated from github.com) commented 2026-10-20 12:19:41 +00:00

Ah, I see the tx count is ignored here at the end as you say:

github.com/bitcoin/bitcoin@4bacf21a13/src/net_processing.cpp (L5072-L5083)

And none of the integers in CBlockHeader are serialized in compact form. So it is indeed exact.

Still think it would be more helpful to have:

    // + 1 for the always-zero tx count
    constexpr int64_t NET_HEADER_SIZE{(sizeof(CBlockHeader) + 1) * 8};
Ah, I see the tx count is ignored here at the end as you say: https://github.com/bitcoin/bitcoin/blob/4bacf21a13c2ed25ef9362ca38f26bf0a67d22c9/src/net_processing.cpp#L5072-L5083 And none of the integers in `CBlockHeader` are serialized in compact form. So it is indeed exact. Still think it would be more helpful to have: ```suggestion // + 1 for the always-zero tx count constexpr int64_t NET_HEADER_SIZE{(sizeof(CBlockHeader) + 1) * 8}; ```
sipa (Migrated from github.com) reviewed 2026-10-20 14:14:18 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:14:18 +00:00

Done.

Done.
sipa (Migrated from github.com) reviewed 2026-10-20 14:14:28 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:14:28 +00:00

Done.

Done.
sipa (Migrated from github.com) reviewed 2026-10-20 14:18:11 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:18:11 +00:00

Reworded.

Reworded.
sipa (Migrated from github.com) reviewed 2026-10-20 14:19:09 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:19:09 +00:00

Nice catch. Changed to instead just compute the parameters at runtime per connection, now that it's super fast. The need to precompute it dated from when it was too slow to repeatedly recompute.

Nice catch. Changed to instead just compute the parameters at runtime per connection, now that it's super fast. The need to precompute it dated from when it was too slow to repeatedly recompute.
sipa (Migrated from github.com) reviewed 2026-10-20 14:19:47 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:19:47 +00:00

Explained in the comment now.

Explained in the comment now.
sipa (Migrated from github.com) reviewed 2026-10-20 14:20:56 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:20:56 +00:00

I don't think that's very helpful.

I don't think that's very helpful.
sipa (Migrated from github.com) reviewed 2026-10-20 14:22:21 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:22:21 +00:00

I've included the code in a comment, so it doesn't affect code coverage stats, but still allows re-running (or examining by an LLM for inspiration for future improvements).

I've included the code in a comment, so it doesn't affect code coverage stats, but still allows re-running (or examining by an LLM for inspiration for future improvements).
sipa (Migrated from github.com) reviewed 2026-10-20 14:22:35 +00:00
sipa (Migrated from github.com) commented 2026-10-20 14:22:35 +00:00

I have made all captures explicit.

I have made all captures explicit.
sipa (Migrated from github.com) reviewed 2026-10-20 14:24:20 +00:00
Lines 356-380
sipa (Migrated from github.com) commented 2026-10-20 14:24:20 +00:00

I've compacted the new text a bit.

I've compacted the new text a bit.
sipa (Migrated from github.com) reviewed 2026-10-20 14:24:47 +00:00
Lines 399-401
sipa (Migrated from github.com) commented 2026-10-20 14:24:47 +00:00

Done.

Done.
l0rinc (Migrated from github.com) approved these changes 2026-10-20 21:14:31 +00:00
l0rinc (Migrated from github.com) left a comment

lightly tested ACK 672e628d96

Since my last review, the parameters are computed whenever a headers sync starts (addressing my clock-correction concern), and the test-vector generation script is now included.
I like the more compact code comments, and I'd still appreciate another pair of eyes to double-check the math.

The fixed test vectors could also be checked against the actual attack-rate formula, and the Python generator could move to contrib/testgen/, but neither suggestion is blocking for me.

lightly tested ACK 672e628d96bf1d1f2890ff666b4d16f37309b61e Since my last review, the parameters are computed whenever a headers sync starts (addressing my clock-correction concern), and the test-vector generation script is now included. I like the more compact code comments, and I'd still appreciate another pair of eyes to double-check the math. The fixed test vectors could also be checked against the actual attack-rate formula, and the Python generator could move to `contrib/testgen/`, but neither suggestion is blocking for me.
l0rinc (Migrated from github.com) commented 2026-10-20 21:06:12 +00:00

dce0241 net_processing: compute headers sync parameters at runtime:

The release-process note and PR description could say that the parameters are computed at runtime whenever a headers sync begins - the current startup wording describes the previous implementation.

Align the wording with the current computation point
diff --git a/doc/release-process.md b/doc/release-process.md
index 29ef13845b..3cf70bddd2 100644
--- a/doc/release-process.md
+++ b/doc/release-process.md
@@ -46,7 +46,7 @@ Release Process
     - On mainnet, the selected value must not be orphaned, so it may be useful to set the height two blocks back from the tip.
     - Testnet should be set with a height some tens of thousands back from the tip, due to reorgs there.
   - `nMinimumChainWork` with the "chainwork" value of RPC `getblockheader` using the same height as that selected for the previous step.
-  - `minchainwork_height` with that same height. It is the number of headers corresponding to `nMinimumChainWork`, and is used to size the headers synchronization DoS-protection parameters (which are computed at node startup).
+  - `minchainwork_height` with that same height. It is the number of headers corresponding to `nMinimumChainWork`, and is used to size the headers synchronization DoS-protection parameters, which are computed at runtime when a headers sync begins.
   - `m_assumeutxo_data` array should be appended to with the values returned by calling `bitcoin-cli -rpcclienttimeout=0 -named dumptxoutset utxo.dat rollback=<height or hash>`
     The same height considerations for `defaultAssumeValid` apply.
 - Clear the release notes and move them to the wiki (see "Write the release notes" below).
> [dce0241](https://github.com/bitcoin/bitcoin/pull/35642/changes/dce02415e160ab395a37e5c65cd22c8109a34e7c) _net_processing: compute headers sync parameters at runtime_: The release-process note and PR description could say that the parameters are computed at runtime whenever a headers sync begins - the current startup wording describes the previous implementation. <details> <summary>Align the wording with the current computation point</summary> ```diff diff --git a/doc/release-process.md b/doc/release-process.md index 29ef13845b..3cf70bddd2 100644 --- a/doc/release-process.md +++ b/doc/release-process.md @@ -46,7 +46,7 @@ Release Process - On mainnet, the selected value must not be orphaned, so it may be useful to set the height two blocks back from the tip. - Testnet should be set with a height some tens of thousands back from the tip, due to reorgs there. - `nMinimumChainWork` with the "chainwork" value of RPC `getblockheader` using the same height as that selected for the previous step. - - `minchainwork_height` with that same height. It is the number of headers corresponding to `nMinimumChainWork`, and is used to size the headers synchronization DoS-protection parameters (which are computed at node startup). + - `minchainwork_height` with that same height. It is the number of headers corresponding to `nMinimumChainWork`, and is used to size the headers synchronization DoS-protection parameters, which are computed at runtime when a headers sync begins. - `m_assumeutxo_data` array should be appended to with the values returned by calling `bitcoin-cli -rpcclienttimeout=0 -named dumptxoutset utxo.dat rollback=<height or hash>` The same height considerations for `defaultAssumeValid` apply. - Clear the release notes and move them to the wiki (see "Write the release notes" below). ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-20 21:06:31 +00:00

1bbf4a1 headerssync: add computation of optimal sync parameters:

I'd find this easier to rerun as a script in contrib/testgen/, with a reference here.
That would also let Python tooling check it and keep the test easier to read.

Move the generator to contrib/testgen
diff --git a/contrib/testgen/README.md b/contrib/testgen/README.md
index 2f0288df16..4190c6d89d 100644
--- a/contrib/testgen/README.md
+++ b/contrib/testgen/README.md
@@ -6,3 +6,7 @@ To use inside a scripted-diff (or just execute directly):
 
     ./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
     ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
+
+To regenerate the headers sync parameter records in `src/test/headerssync_tests.cpp`, run from the repository root:
+
+    pypy3 contrib/testgen/gen_headerssync_test_vectors.py
diff --git a/contrib/testgen/gen_headerssync_test_vectors.py b/contrib/testgen/gen_headerssync_test_vectors.py
new file mode 100644
index 0000000000..e963085307
--- /dev/null
+++ b/contrib/testgen/gen_headerssync_test_vectors.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+# Copyright (c) The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+"""Generate headers sync parameter test vectors by exhaustively searching all periods."""
+
+import math
+import random
+
+# Both must match src/headerssync.cpp.
+COMPACT_HEADER_SIZE = 48 * 8
+LIMIT_FRACTION = (0.2 / 600.0) / (6000000000.0 / (81 * 8))
+# Reject solutions which are only better by this much relatively speaking (floating point precision).
+MARGIN = 1e-9
+
+def memory_usage(max_headers, mch, period, bufsize):
+    return max(max_headers // period, mch // period + bufsize * COMPACT_HEADER_SIZE)
+
+def round_error(value):
+    return abs(value - round(value)) / value
+
+def rate(period, bufsize):
+    """Expected number of forged headers accepted per attack: the closed form of headerssync.cpp."""
+    k, m = divmod(bufsize, period)
+    return 2.0**-k * (6 * period**2 - 4 * period * m + m * m - 2 * period + m) / (4 * period)
+
+def optimum(max_headers, mch, attack_headers):
+    """Exhaustively search for the optimal (period, bufsize), and the runner-up it beats."""
+    best, runner_up, bufsize = None, None, 1
+    for period in range(1, mch + 1):
+        while rate(period, bufsize) >= attack_headers:  # bufsize never decreases with period
+            bufsize += 1
+        candidate = (memory_usage(max_headers, mch, period, bufsize), period, bufsize)
+        if best is None or candidate < best:
+            best, runner_up = candidate, best
+        elif runner_up is None or candidate < runner_up:
+            runner_up = candidate
+    return best, runner_up
+
+def sample():
+    """One record, or None if its inputs are illegal or its optimum is too ambiguous."""
+    # Start with random initial values:
+    # - A timespan, log-normally distributed with mode 12 years and mean 18 years.
+    sigma = math.sqrt(math.log(18.0 / 12.0) / 1.5)
+    timespan = random.lognormvariate(math.log(12.0 * 365.25 * 86400) + sigma**2, sigma)
+    # - minchainwork_headers, exponentially distributed with mean 1000000.
+    mch = random.expovariate(1.0 / 1000000.0)
+    # - attack_headers, log-normally distributed with median LIMIT_FRACTION * mch, sigma 1.5.
+    attack_headers = random.lognormvariate(math.log(LIMIT_FRACTION * mch), 1.5)
+    # Convert to a starting point in (max_headers, minchainwork_headers, attack_headers) space.
+    start = [6.0 * timespan, mch, attack_headers]
+    # Pick a random point on the 3D sphere with radius 1/200 as (logarithm of) walk step factor to explore.
+    direction = [random.gauss(0.0, 1.0) for _ in range(3)]
+    step = [d * 0.005 / math.hypot(*direction) for d in direction]
+    # Pick 100 points, starting from start, each a further factor exp(step) away from the previous one.
+    walk = ([c * math.exp(d * i) for c, d in zip(start, step)] for i in range(100))
+    # Among those 100 points, pick the one whose max_headers and mch are closest to integers.
+    max_headers, mch, attack_headers = min(walk, key=lambda pt: max(round_error(pt[0]), round_error(pt[1])))
+    max_headers, mch = round(max_headers), round(mch)
+    # Reject if outside the inputs the optimizer is defined for.
+    if mch < 1 or max_headers <= 2 * mch:
+        return None
+    best, runner_up = optimum(max_headers, mch, attack_headers)
+    _, period, bufsize = best
+    # Reject if too close to the runner up, to avoid depending on floating point rounding behavior.
+    if not runner_up or runner_up[0] <= best[0] * (1.0 + MARGIN):
+        return None  # another period is just as good
+    if not (rate(period, bufsize) < attack_headers * (1.0 - MARGIN)
+            and rate(period, bufsize - 1) > attack_headers * (1.0 + MARGIN)):
+        return None  # another bufsize is just as good
+    return max_headers, mch, attack_headers, period, bufsize
+
+random.seed(0)
+count = 0
+while count < 50:
+    record = sample()
+    if not record:
+        continue
+    print("        {%d, %d, %r, %d, %d}," % record)
+    count += 1
diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp
index 363df619f9..4c4e7b2b64 100644
--- a/src/test/headerssync_tests.cpp
+++ b/src/test/headerssync_tests.cpp
@@ -16,84 +16,9 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests)
 // - inputs: max_headers, minchainwork_headers, attack_headers
 // - expected outputs: period, bufsize
 //
-// The records below were generated by the following script, which finds the exact optimum for each
-// input through an exhaustive search over all periods, independent of the search strategy the
-// implementation uses. Run under PyPy for faster results.
-//
-// import math
-// import random
-//
-// # Both must match src/headerssync.cpp.
-// COMPACT_HEADER_SIZE = 48 * 8
-// LIMIT_FRACTION = (0.2 / 600.0) / (6000000000.0 / (81 * 8))
-// # Reject solutions which are only better by this much relatively speaking (floating point precision).
-// MARGIN = 1e-9
-//
-// def memory_usage(max_headers, mch, period, bufsize):
-//     return max(max_headers // period, mch // period + bufsize * COMPACT_HEADER_SIZE)
-//
-// def round_error(value):
-//     return abs(value - round(value)) / value
-//
-// def rate(period, bufsize):
-//     """Expected number of forged headers accepted per attack: the closed form of headerssync.cpp."""
-//     k, m = divmod(bufsize, period)
-//     return 2.0**-k * (6 * period**2 - 4 * period * m + m * m - 2 * period + m) / (4 * period)
-//
-// def optimum(max_headers, mch, attack_headers):
-//     """Exhaustively search for the optimal (period, bufsize), and the runner-up it beats."""
-//     best, runner_up, bufsize = None, None, 1
-//     for period in range(1, mch + 1):
-//         while rate(period, bufsize) >= attack_headers:  # bufsize never decreases with period
-//             bufsize += 1
-//         candidate = (memory_usage(max_headers, mch, period, bufsize), period, bufsize)
-//         if best is None or candidate < best:
-//             best, runner_up = candidate, best
-//         elif runner_up is None or candidate < runner_up:
-//             runner_up = candidate
-//     return best, runner_up
-//
-// def sample():
-//     """One record, or None if its inputs are illegal or its optimum is too ambiguous."""
-//     # Start with random initial values:
-//     # - A timespan, log-normally distributed with mode 12 years and mean 18 years.
-//     sigma = math.sqrt(math.log(18.0 / 12.0) / 1.5)
-//     timespan = random.lognormvariate(math.log(12.0 * 365.25 * 86400) + sigma**2, sigma)
-//     # - minchainwork_headers, exponentially distributed with mean 1000000.
-//     mch = random.expovariate(1.0 / 1000000.0)
-//     # - attack_headers, log-normally distributed with median LIMIT_FRACTION * mch, sigma 1.5.
-//     attack_headers = random.lognormvariate(math.log(LIMIT_FRACTION * mch), 1.5)
-//     # Convert to a starting point in (max_headers, minchainwork_headers, attack_headers) space.
-//     start = [6.0 * timespan, mch, attack_headers]
-//     # Pick a random point on the 3D sphere with radius 1/200 as (logarithm of) walk step factor to explore.
-//     direction = [random.gauss(0.0, 1.0) for _ in range(3)]
-//     step = [d * 0.005 / math.hypot(*direction) for d in direction]
-//     # Pick 100 points, starting from start, each a further factor exp(step) away from the previous one.
-//     walk = ([c * math.exp(d * i) for c, d in zip(start, step)] for i in range(100))
-//     # Among those 100 points, pick the one whose max_headers and mch are closest to integers.
-//     max_headers, mch, attack_headers = min(walk, key=lambda pt: max(round_error(pt[0]), round_error(pt[1])))
-//     max_headers, mch = round(max_headers), round(mch)
-//     # Reject if outside the inputs the optimizer is defined for.
-//     if mch < 1 or max_headers <= 2 * mch:
-//         return None
-//     best, runner_up = optimum(max_headers, mch, attack_headers)
-//     _, period, bufsize = best
-//     # Reject if too close to the runner up, to avoid depending on floating point rounding behavior.
-//     if not runner_up or runner_up[0] <= best[0] * (1.0 + MARGIN):
-//         return None  # another period is just as good
-//     if not (rate(period, bufsize) < attack_headers * (1.0 - MARGIN)
-//             and rate(period, bufsize - 1) > attack_headers * (1.0 + MARGIN)):
-//         return None  # another bufsize is just as good
-//     return max_headers, mch, attack_headers, period, bufsize
-//
-// random.seed(0)
-// count = 0
-// while count < 50:
-//     record = sample()
-//     if not record:
-//         continue
-//     print("        {%d, %d, %r, %d, %d}," % record)
-//     count += 1
+// The records below were generated by contrib/testgen/gen_headerssync_test_vectors.py
+// The generator finds the exact optimum through an exhaustive search over all periods, independently of the implementation's search strategy
+// Run from the repository root: pypy3 contrib/testgen/gen_headerssync_test_vectors.py
 
 BOOST_AUTO_TEST_CASE(compute_headers_sync_params)
 {
> [1bbf4a1](https://github.com/bitcoin/bitcoin/pull/35642/changes/1bbf4a1772d3a2b148eb7e0b8e85687146ee9043) _headerssync: add computation of optimal sync parameters_: I'd find this easier to rerun as a script in `contrib/testgen/`, with a reference here. That would also let Python tooling check it and keep the test easier to read. <details> <summary>Move the generator to contrib/testgen</summary> ```diff diff --git a/contrib/testgen/README.md b/contrib/testgen/README.md index 2f0288df16..4190c6d89d 100644 --- a/contrib/testgen/README.md +++ b/contrib/testgen/README.md @@ -6,3 +6,7 @@ To use inside a scripted-diff (or just execute directly): ./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json + +To regenerate the headers sync parameter records in `src/test/headerssync_tests.cpp`, run from the repository root: + + pypy3 contrib/testgen/gen_headerssync_test_vectors.py diff --git a/contrib/testgen/gen_headerssync_test_vectors.py b/contrib/testgen/gen_headerssync_test_vectors.py new file mode 100644 index 0000000000..e963085307 --- /dev/null +++ b/contrib/testgen/gen_headerssync_test_vectors.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# Copyright (c) The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +"""Generate headers sync parameter test vectors by exhaustively searching all periods.""" + +import math +import random + +# Both must match src/headerssync.cpp. +COMPACT_HEADER_SIZE = 48 * 8 +LIMIT_FRACTION = (0.2 / 600.0) / (6000000000.0 / (81 * 8)) +# Reject solutions which are only better by this much relatively speaking (floating point precision). +MARGIN = 1e-9 + +def memory_usage(max_headers, mch, period, bufsize): + return max(max_headers // period, mch // period + bufsize * COMPACT_HEADER_SIZE) + +def round_error(value): + return abs(value - round(value)) / value + +def rate(period, bufsize): + """Expected number of forged headers accepted per attack: the closed form of headerssync.cpp.""" + k, m = divmod(bufsize, period) + return 2.0**-k * (6 * period**2 - 4 * period * m + m * m - 2 * period + m) / (4 * period) + +def optimum(max_headers, mch, attack_headers): + """Exhaustively search for the optimal (period, bufsize), and the runner-up it beats.""" + best, runner_up, bufsize = None, None, 1 + for period in range(1, mch + 1): + while rate(period, bufsize) >= attack_headers: # bufsize never decreases with period + bufsize += 1 + candidate = (memory_usage(max_headers, mch, period, bufsize), period, bufsize) + if best is None or candidate < best: + best, runner_up = candidate, best + elif runner_up is None or candidate < runner_up: + runner_up = candidate + return best, runner_up + +def sample(): + """One record, or None if its inputs are illegal or its optimum is too ambiguous.""" + # Start with random initial values: + # - A timespan, log-normally distributed with mode 12 years and mean 18 years. + sigma = math.sqrt(math.log(18.0 / 12.0) / 1.5) + timespan = random.lognormvariate(math.log(12.0 * 365.25 * 86400) + sigma**2, sigma) + # - minchainwork_headers, exponentially distributed with mean 1000000. + mch = random.expovariate(1.0 / 1000000.0) + # - attack_headers, log-normally distributed with median LIMIT_FRACTION * mch, sigma 1.5. + attack_headers = random.lognormvariate(math.log(LIMIT_FRACTION * mch), 1.5) + # Convert to a starting point in (max_headers, minchainwork_headers, attack_headers) space. + start = [6.0 * timespan, mch, attack_headers] + # Pick a random point on the 3D sphere with radius 1/200 as (logarithm of) walk step factor to explore. + direction = [random.gauss(0.0, 1.0) for _ in range(3)] + step = [d * 0.005 / math.hypot(*direction) for d in direction] + # Pick 100 points, starting from start, each a further factor exp(step) away from the previous one. + walk = ([c * math.exp(d * i) for c, d in zip(start, step)] for i in range(100)) + # Among those 100 points, pick the one whose max_headers and mch are closest to integers. + max_headers, mch, attack_headers = min(walk, key=lambda pt: max(round_error(pt[0]), round_error(pt[1]))) + max_headers, mch = round(max_headers), round(mch) + # Reject if outside the inputs the optimizer is defined for. + if mch < 1 or max_headers <= 2 * mch: + return None + best, runner_up = optimum(max_headers, mch, attack_headers) + _, period, bufsize = best + # Reject if too close to the runner up, to avoid depending on floating point rounding behavior. + if not runner_up or runner_up[0] <= best[0] * (1.0 + MARGIN): + return None # another period is just as good + if not (rate(period, bufsize) < attack_headers * (1.0 - MARGIN) + and rate(period, bufsize - 1) > attack_headers * (1.0 + MARGIN)): + return None # another bufsize is just as good + return max_headers, mch, attack_headers, period, bufsize + +random.seed(0) +count = 0 +while count < 50: + record = sample() + if not record: + continue + print(" {%d, %d, %r, %d, %d}," % record) + count += 1 diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp index 363df619f9..4c4e7b2b64 100644 --- a/src/test/headerssync_tests.cpp +++ b/src/test/headerssync_tests.cpp @@ -16,84 +16,9 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests) // - inputs: max_headers, minchainwork_headers, attack_headers // - expected outputs: period, bufsize // -// The records below were generated by the following script, which finds the exact optimum for each -// input through an exhaustive search over all periods, independent of the search strategy the -// implementation uses. Run under PyPy for faster results. -// -// import math -// import random -// -// # Both must match src/headerssync.cpp. -// COMPACT_HEADER_SIZE = 48 * 8 -// LIMIT_FRACTION = (0.2 / 600.0) / (6000000000.0 / (81 * 8)) -// # Reject solutions which are only better by this much relatively speaking (floating point precision). -// MARGIN = 1e-9 -// -// def memory_usage(max_headers, mch, period, bufsize): -// return max(max_headers // period, mch // period + bufsize * COMPACT_HEADER_SIZE) -// -// def round_error(value): -// return abs(value - round(value)) / value -// -// def rate(period, bufsize): -// """Expected number of forged headers accepted per attack: the closed form of headerssync.cpp.""" -// k, m = divmod(bufsize, period) -// return 2.0**-k * (6 * period**2 - 4 * period * m + m * m - 2 * period + m) / (4 * period) -// -// def optimum(max_headers, mch, attack_headers): -// """Exhaustively search for the optimal (period, bufsize), and the runner-up it beats.""" -// best, runner_up, bufsize = None, None, 1 -// for period in range(1, mch + 1): -// while rate(period, bufsize) >= attack_headers: # bufsize never decreases with period -// bufsize += 1 -// candidate = (memory_usage(max_headers, mch, period, bufsize), period, bufsize) -// if best is None or candidate < best: -// best, runner_up = candidate, best -// elif runner_up is None or candidate < runner_up: -// runner_up = candidate -// return best, runner_up -// -// def sample(): -// """One record, or None if its inputs are illegal or its optimum is too ambiguous.""" -// # Start with random initial values: -// # - A timespan, log-normally distributed with mode 12 years and mean 18 years. -// sigma = math.sqrt(math.log(18.0 / 12.0) / 1.5) -// timespan = random.lognormvariate(math.log(12.0 * 365.25 * 86400) + sigma**2, sigma) -// # - minchainwork_headers, exponentially distributed with mean 1000000. -// mch = random.expovariate(1.0 / 1000000.0) -// # - attack_headers, log-normally distributed with median LIMIT_FRACTION * mch, sigma 1.5. -// attack_headers = random.lognormvariate(math.log(LIMIT_FRACTION * mch), 1.5) -// # Convert to a starting point in (max_headers, minchainwork_headers, attack_headers) space. -// start = [6.0 * timespan, mch, attack_headers] -// # Pick a random point on the 3D sphere with radius 1/200 as (logarithm of) walk step factor to explore. -// direction = [random.gauss(0.0, 1.0) for _ in range(3)] -// step = [d * 0.005 / math.hypot(*direction) for d in direction] -// # Pick 100 points, starting from start, each a further factor exp(step) away from the previous one. -// walk = ([c * math.exp(d * i) for c, d in zip(start, step)] for i in range(100)) -// # Among those 100 points, pick the one whose max_headers and mch are closest to integers. -// max_headers, mch, attack_headers = min(walk, key=lambda pt: max(round_error(pt[0]), round_error(pt[1]))) -// max_headers, mch = round(max_headers), round(mch) -// # Reject if outside the inputs the optimizer is defined for. -// if mch < 1 or max_headers <= 2 * mch: -// return None -// best, runner_up = optimum(max_headers, mch, attack_headers) -// _, period, bufsize = best -// # Reject if too close to the runner up, to avoid depending on floating point rounding behavior. -// if not runner_up or runner_up[0] <= best[0] * (1.0 + MARGIN): -// return None # another period is just as good -// if not (rate(period, bufsize) < attack_headers * (1.0 - MARGIN) -// and rate(period, bufsize - 1) > attack_headers * (1.0 + MARGIN)): -// return None # another bufsize is just as good -// return max_headers, mch, attack_headers, period, bufsize -// -// random.seed(0) -// count = 0 -// while count < 50: -// record = sample() -// if not record: -// continue -// print(" {%d, %d, %r, %d, %d}," % record) -// count += 1 +// The records below were generated by contrib/testgen/gen_headerssync_test_vectors.py +// The generator finds the exact optimum through an exhaustive search over all periods, independently of the implementation's search strategy +// Run from the repository root: pypy3 contrib/testgen/gen_headerssync_test_vectors.py BOOST_AUTO_TEST_CASE(compute_headers_sync_params) { ``` </details>
l0rinc (Migrated from github.com) commented 2026-10-20 21:05:49 +00:00

1bbf4a1 headerssync: add computation of optimal sync parameters:

Could we also validate these expected values against the attack-rate formula?
That would check the test records themselves, alongside checking that the optimizer reproduces them.

Check the buffer using the existing vectors
diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp
index d291c11371..363df619f9 100644
--- a/src/test/headerssync_tests.cpp
+++ b/src/test/headerssync_tests.cpp
@@ -6,6 +6,7 @@
 
 #include <boost/test/unit_test.hpp>
 
+#include <cmath>
 #include <cstdint>
 
 BOOST_AUTO_TEST_SUITE(headerssync_tests)
@@ -96,7 +97,7 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests)
 
 BOOST_AUTO_TEST_CASE(compute_headers_sync_params)
 {
-    const struct {
+    constexpr struct {
         int64_t max_headers;
         int64_t minchainwork_headers;
         double attack_headers;
@@ -154,10 +155,20 @@ BOOST_AUTO_TEST_CASE(compute_headers_sync_params)
         {9516499040, 2241730, 5.612939355277791e-05, 1002, 24723},
         {2762151481, 103252, 1.7805562892829343e-06, 501, 14355},
     };
-    for (const auto& cas : cases) {
-        const auto [period, bufsize] = ComputeHeadersSyncParamsInner(cas.max_headers, cas.minchainwork_headers, cas.attack_headers);
-        BOOST_CHECK_EQUAL(period, cas.expected_period);
-        BOOST_CHECK_EQUAL(bufsize, cas.expected_bufsize);
+    const auto expected_headers{[](int64_t period, int64_t bufsize) {
+        double accepted{0};
+        for (int64_t offset{0}; offset < period; ++offset) {
+            const int64_t checks{(bufsize - offset + period - 1) / period}; // ceil((B-F)/P)
+            accepted += std::ldexp(double(offset + period * checks - bufsize + period), -int(checks));
+        }
+        return accepted / period;
+    }};
+    for (const auto& [max_headers, minchainwork_headers, attack_headers, expected_period, expected_bufsize] : cases) {
+        const auto [period, bufsize]{ComputeHeadersSyncParamsInner(max_headers, minchainwork_headers, attack_headers)};
+        BOOST_CHECK_EQUAL(period, expected_period);
+        BOOST_CHECK_EQUAL(bufsize, expected_bufsize);
+        BOOST_CHECK_LT(expected_headers(expected_period, expected_bufsize), attack_headers);
+        BOOST_CHECK_GE(expected_headers(expected_period, expected_bufsize - 1), attack_headers);
     }
 }
 
> [1bbf4a1](https://github.com/bitcoin/bitcoin/pull/35642/changes/1bbf4a1772d3a2b148eb7e0b8e85687146ee9043) _headerssync: add computation of optimal sync parameters_: Could we also validate these expected values against the attack-rate formula? That would check the test records themselves, alongside checking that the optimizer reproduces them. <details> <summary>Check the buffer using the existing vectors</summary> ```diff diff --git a/src/test/headerssync_tests.cpp b/src/test/headerssync_tests.cpp index d291c11371..363df619f9 100644 --- a/src/test/headerssync_tests.cpp +++ b/src/test/headerssync_tests.cpp @@ -6,6 +6,7 @@ #include <boost/test/unit_test.hpp> +#include <cmath> #include <cstdint> BOOST_AUTO_TEST_SUITE(headerssync_tests) @@ -96,7 +97,7 @@ BOOST_AUTO_TEST_SUITE(headerssync_tests) BOOST_AUTO_TEST_CASE(compute_headers_sync_params) { - const struct { + constexpr struct { int64_t max_headers; int64_t minchainwork_headers; double attack_headers; @@ -154,10 +155,20 @@ BOOST_AUTO_TEST_CASE(compute_headers_sync_params) {9516499040, 2241730, 5.612939355277791e-05, 1002, 24723}, {2762151481, 103252, 1.7805562892829343e-06, 501, 14355}, }; - for (const auto& cas : cases) { - const auto [period, bufsize] = ComputeHeadersSyncParamsInner(cas.max_headers, cas.minchainwork_headers, cas.attack_headers); - BOOST_CHECK_EQUAL(period, cas.expected_period); - BOOST_CHECK_EQUAL(bufsize, cas.expected_bufsize); + const auto expected_headers{[](int64_t period, int64_t bufsize) { + double accepted{0}; + for (int64_t offset{0}; offset < period; ++offset) { + const int64_t checks{(bufsize - offset + period - 1) / period}; // ceil((B-F)/P) + accepted += std::ldexp(double(offset + period * checks - bufsize + period), -int(checks)); + } + return accepted / period; + }}; + for (const auto& [max_headers, minchainwork_headers, attack_headers, expected_period, expected_bufsize] : cases) { + const auto [period, bufsize]{ComputeHeadersSyncParamsInner(max_headers, minchainwork_headers, attack_headers)}; + BOOST_CHECK_EQUAL(period, expected_period); + BOOST_CHECK_EQUAL(bufsize, expected_bufsize); + BOOST_CHECK_LT(expected_headers(expected_period, expected_bufsize), attack_headers); + BOOST_CHECK_GE(expected_headers(expected_period, expected_bufsize - 1), attack_headers); } } ``` </details>
sipa (Migrated from github.com) reviewed 2026-10-21 18:17:53 +00:00
sipa (Migrated from github.com) commented 2026-10-21 18:17:53 +00:00

Done, and went further by implementing more checks to verify optimality of the test vectors.

Done, and went further by implementing more checks to verify optimality of the test vectors.
sipa (Migrated from github.com) reviewed 2026-10-21 18:20:15 +00:00
sipa (Migrated from github.com) commented 2026-10-21 18:20:15 +00:00

I'm not taking this.

The test generator script isn't particularly important, it's just selecting some cases to try perhaps slightly better than purely randomly. There is no requirement that the test vectors match its output, or even that it's reproducible. Burdening the project with maintaining this code is overkill.

If you think it's undesirable to have this code in the comment, I'd rather delete it entirely and replace it with a single sentence explaining what considerations were made when generating the vectors.

I'm not taking this. The test generator script isn't particularly important, it's just selecting some cases to try perhaps slightly better than purely randomly. There is no requirement that the test vectors match its output, or even that it's reproducible. Burdening the project with maintaining this code is overkill. If you think it's undesirable to have this code in the comment, I'd rather delete it entirely and replace it with a single sentence explaining what considerations were made when generating the vectors.
l0rinc (Migrated from github.com) approved these changes 2026-10-21 19:25:01 +00:00
l0rinc (Migrated from github.com) left a comment

Lightly tested diff re-ACK 0457cec22a

Lightly tested diff re-ACK 0457cec22a273eba31e92950bd485bf72c864ab0
l0rinc (Migrated from github.com) commented 2026-10-21 19:05:14 +00:00

Thanks for considering, please resolve the suggestion.

Thanks for considering, please resolve the suggestion.
sipa (Migrated from github.com) reviewed 2026-10-21 19:31:23 +00:00
sipa (Migrated from github.com) commented 2026-10-21 19:31:23 +00:00

Done.

Done.
l0rinc commented 2026-10-21 19:32:12 +00:00 (Migrated from github.com)

reACK 0316d0dc9e

reACK 0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee
sipa (Migrated from github.com) reviewed 2026-10-21 19:41:44 +00:00
sipa (Migrated from github.com) commented 2026-10-21 19:41:44 +00:00

Fair point, but not going to touch this everywhere now.

Fair point, but not going to touch this everywhere now.
hodlinator (Migrated from github.com) reviewed 2026-10-24 09:38:57 +00:00
Lines 356-357
hodlinator (Migrated from github.com) commented 2026-10-23 11:56:16 +00:00

nit: Decreasing the difficulty allows even faster hashing?

//      commitment bits. Such a chain may be very long, by exploiting the timewarp bug to minimize
//      the difficulty. There is however an absolute limit on how long such a chain can be: 6
nit: Decreasing the difficulty allows even faster hashing? ```suggestion // commitment bits. Such a chain may be very long, by exploiting the timewarp bug to minimize // the difficulty. There is however an absolute limit on how long such a chain can be: 6 ```
hodlinator (Migrated from github.com) commented 2026-10-23 12:26:32 +00:00

nit: How about swapping the function definition order for ComputeHeadersSyncParams() & ComputeHeadersSyncParamsInner() to match the order in the header?
In order to attempt understanding the attack_headers argument coming in it's helpful to at least have glimpsed at the body of ComputeHeadersSyncParams().

nit: How about swapping the function definition order for `ComputeHeadersSyncParams()` & `ComputeHeadersSyncParamsInner()` to match the order in the header? In order to attempt understanding the `attack_headers` argument coming in it's helpful to at least have glimpsed at the body of `ComputeHeadersSyncParams()`.
Lines 561-562
hodlinator (Migrated from github.com) commented 2026-10-23 12:09:20 +00:00

nanonit: Could keep to the name in Consensus::Params:

// the chain's age and minimum-chain-work height, then optimize.
HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_height)
nanonit: Could keep to the name in `Consensus::Params`: ```suggestion // the chain's age and minimum-chain-work height, then optimize. HeadersSyncParams ComputeHeadersSyncParams(std::chrono::seconds timespan, int64_t minchainwork_height) ```
hodlinator (Migrated from github.com) commented 2026-10-23 12:27:33 +00:00

nanonit: Helps human eyes:

    constexpr double ATTACK_BANDWIDTH{6'000'000'000.0};
nanonit: Helps human eyes: ```suggestion constexpr double ATTACK_BANDWIDTH{6'000'000'000.0}; ```
hodlinator (Migrated from github.com) commented 2026-10-23 12:57:01 +00:00

Would expect attack_headers => 1, but LIMIT_FRACTION becomes 3.6000000000000005E-11. For a mainchainwork_headers value of 966143 that makes the attack_headers parameter equal 0.00003478....
Going by the above comment, the number of headers sent by attackers that can at most be accepted by a victim is... 0.00003478...?! Does that mean zero attack headers are accepted? Or do we round it up to only 1 attack header?
Would it be possible to better explain this in the comment?

Would expect `attack_headers => 1`, but `LIMIT_FRACTION` becomes `3.6000000000000005E-11`. For a `mainchainwork_headers` value of `966143` that makes the `attack_headers` parameter equal `0.00003478...`. Going by the above comment, the number of headers sent by attackers that can at most be accepted by a victim is... `0.00003478...`?! Does that mean zero attack headers are accepted? Or do we round it up to only 1 attack header? Would it be possible to better explain this in the comment?
Lines 582-593
hodlinator (Migrated from github.com) commented 2026-10-23 12:39:18 +00:00

Why jointly, isn't it more correct that multiple headers sync peers could be sending you these low-difficulty headers in parallel?

    /** How much additional permanent memory usage is an attacking peer allowed to cause in the
     *  victim, expressed as fraction of the normal memory usage due to mainchain growth, for the
     *  duration the attack is sustained. [unitless]. 0.2 means that attackers, while they keep up the
     *  attack, can cause permanent memory usage due to headers storage to grow at 1.2 header per
     *  BLOCK_INTERVAL. */
    constexpr double ATTACK_FRACTION{0.2};
    /** How many bits a header uses in the P2P protocol: a block header, plus the trailing zero
     *  transaction count headers are relayed with. [bits] */
    constexpr int64_t NET_HEADER_SIZE{(sizeof(CBlockHeader) + 1) * 8};
    /** What rate of headers worth of RAM attackers are allowed to cause in the victim. [headers/s] */
    constexpr double LIMIT_HEADERRATE{ATTACK_FRACTION / BLOCK_INTERVAL};
    /** How many headers can an attacking peer send a victim per second. [headers/s] */
Why jointly, isn't it more correct that multiple headers sync peers could be sending you these low-difficulty headers in parallel? ```suggestion /** How much additional permanent memory usage is an attacking peer allowed to cause in the * victim, expressed as fraction of the normal memory usage due to mainchain growth, for the * duration the attack is sustained. [unitless]. 0.2 means that attackers, while they keep up the * attack, can cause permanent memory usage due to headers storage to grow at 1.2 header per * BLOCK_INTERVAL. */ constexpr double ATTACK_FRACTION{0.2}; /** How many bits a header uses in the P2P protocol: a block header, plus the trailing zero * transaction count headers are relayed with. [bits] */ constexpr int64_t NET_HEADER_SIZE{(sizeof(CBlockHeader) + 1) * 8}; /** What rate of headers worth of RAM attackers are allowed to cause in the victim. [headers/s] */ constexpr double LIMIT_HEADERRATE{ATTACK_FRACTION / BLOCK_INTERVAL}; /** How many headers can an attacking peer send a victim per second. [headers/s] */ ```
hodlinator (Migrated from github.com) reviewed 2026-10-24 09:42:15 +00:00
Lines 582-593
hodlinator (Migrated from github.com) commented 2026-10-24 09:42:15 +00:00

(To affect RAM usage the attacker(s) would need unique header chains for each peer but the difficulty could be timewarped to a minimum before forking).

(To affect RAM usage the attacker(s) would need unique header chains for each peer but the difficulty could be timewarped to a minimum before forking).
sipa (Migrated from github.com) reviewed 2026-10-24 12:04:52 +00:00
Lines 356-357
sipa (Migrated from github.com) commented 2026-10-24 12:04:52 +00:00

If they fork off after genesis, indeed, but in this attack scenario (just wasting the victim's per-peer memory using the commitment bit data structure) I believe it is always better to start at genesis. Will address if I retouch.

If they fork off after genesis, indeed, but in this attack scenario (just wasting the victim's per-peer memory using the commitment bit data structure) I believe it is always better to start at genesis. Will address if I retouch.
sipa (Migrated from github.com) reviewed 2026-10-24 12:05:21 +00:00
Lines 561-562
sipa (Migrated from github.com) commented 2026-10-24 12:05:21 +00:00

Will address if I retouch.

Will address if I retouch.
sipa (Migrated from github.com) reviewed 2026-10-24 12:05:32 +00:00
sipa (Migrated from github.com) commented 2026-10-24 12:05:32 +00:00

Will address if I retouch.

Will address if I retouch.
sipa (Migrated from github.com) reviewed 2026-10-24 12:05:43 +00:00
sipa (Migrated from github.com) commented 2026-10-24 12:05:43 +00:00

Will address if I retouch.

Will address if I retouch.
sipa (Migrated from github.com) reviewed 2026-10-24 12:17:34 +00:00
Lines 582-593
sipa (Migrated from github.com) commented 2026-10-24 12:17:34 +00:00

Yes, there may be multiple attackers in parallel. But their joint bandwidth to the victim is what matters, not their individual bandwidth.

The attack here consists of the attacker(s) sending the honest main chain during the commitment phase, and then giving a forged low-difficulty headers chain during the redownload phase. On average, for every attempt at this some tiny amount of these headers will be accepted into the blockchain index. If an someone could perform this attack an infinite number of times, our memory growth would be infinite too.

What saves us is that the attack costs download volume; each attack requires sending the entirety of the main chain's headers during a commitment phase. And we are okay with accepting a tiny amount of forged headers into our index, as long as that growth is sufficiently slow. After all, we're already okay with our blockchain index growing at a rate of ~1 block per 10 minutes. If all an attacker can do is increase that slightly (here, to 1.2 blocks per 10 minutes), then that's fine.

So the reasoning is: we accept that attacks are possible, and will be successful occasionally, but on average they cannot get more than a certain number of headers accepted per mainchain they send us. Since the attacker has a finite amount of bandwidth to us, that translates to a small finite amount of headers accepted per unit of time. As long as that is no more than 0.2 headers per 10 minutes, we are okay.

But what happens when there are multiple attackers, all performing the same attack on us? If each individual attacker was limited to 6 Gbit/s and our parameters are chosen so that a single 6 Gbit/s attacker could get 0.2 headers per 10 minutes accepted, then 1000 attackers doing the same thing in parallel could get 200 headers per 10 minutes accepted which would be totally unacceptable. However, this is not a concern: it's not each individual attacker that is limited to 6 Gbit/s, but all attackers combined. So, the 6 Gbit/s number must represent the joint bandwidth all attackers together have to us, which is indeed limited (our own network connection is a bound, as is the rate at which we can compute hashes).

Yes, there may be multiple attackers in parallel. But their joint bandwidth to the victim is what matters, not their individual bandwidth. The attack here consists of the attacker(s) sending the honest main chain during the commitment phase, and then giving a forged low-difficulty headers chain during the redownload phase. On average, for every attempt at this some tiny amount of these headers will be accepted into the blockchain index. If an someone could perform this attack an infinite number of times, our memory growth would be infinite too. What saves us is that the attack costs download volume; each attack requires sending the entirety of the main chain's headers during a commitment phase. And we are okay with accepting a tiny amount of forged headers into our index, as long as that growth is sufficiently slow. After all, we're already okay with our blockchain index growing at a rate of ~1 block per 10 minutes. If all an attacker can do is increase that slightly (here, to 1.2 blocks per 10 minutes), then that's fine. So the reasoning is: we accept that attacks are possible, and will be successful occasionally, but on average they cannot get more than a certain number of headers accepted per mainchain they send us. Since the attacker has a finite amount of bandwidth to us, that translates to a small finite amount of headers accepted per unit of time. As long as that is no more than 0.2 headers per 10 minutes, we are okay. But what happens when there are multiple attackers, all performing the same attack on us? If each individual attacker was limited to 6 Gbit/s and our parameters are chosen so that a single 6 Gbit/s attacker could get 0.2 headers per 10 minutes accepted, then 1000 attackers doing the same thing in parallel could get 200 headers per 10 minutes accepted which would be totally unacceptable. However, this is not a concern: it's not each individual attacker that is limited to 6 Gbit/s, but all attackers combined. So, the 6 Gbit/s number must represent the joint bandwidth all attackers together have to us, which is indeed limited (our own network connection is a bound, as is the rate at which we can compute hashes).
sipa (Migrated from github.com) reviewed 2026-10-24 12:19:10 +00:00
sipa (Migrated from github.com) commented 2026-10-24 12:19:10 +00:00

It's a fraction of headers accepted per header sent. 1 would be horrible, as it would mean every header sent by the attacker is accepted.

It's a fraction of headers accepted per header sent. 1 would be horrible, as it would mean *every* header sent by the attacker is accepted.
sipa (Migrated from github.com) reviewed 2026-10-24 12:48:25 +00:00
sipa (Migrated from github.com) commented 2026-10-24 12:48:25 +00:00

(disregard my now-deleted earlier comment, I missed you're already multiplying with minchainwork_headers)

The number is correct, in expectation. We permit 0.00003478 headers to be accepted per attack. That is, an attacker that performs the attack 1000000000 times, will on average get no more than 3478 headers accepted.

(disregard my now-deleted earlier comment, I missed you're already multiplying with `minchainwork_headers`) The number is correct, in expectation. We permit 0.00003478 headers to be accepted per attack. That is, an attacker that performs the attack 1000000000 times, will on average get no more than 3478 headers accepted.
fametrano commented 2026-10-25 06:12:42 +00:00 (Migrated from github.com)

tACK 0316d0dc9e

Compiled from this branch, the two new functions return the exact optimum of their model for all five chains, every six hours from 2026-10-25 to 2041. The same holds on a grid of heights from 1 to 5 million and clocks, some before genesis. The chosen bufsize always meets the attack budget. Summing the attack model term by term, in exact arithmetic, gives the closed form for every period up to 16 and bufsize up to 60. I checked this with a Python port and a brute-force search; I can share the scripts as a gist. The new unit tests and p2p_headers_sync_with_minchainwork.py pass, and the parameters its nodes log match the port at each node's clock.

At 2026-10-25 00:00 UTC the parameters, as period/bufsize, are mainnet 597/14697, testnet3 590/13096, testnet4 217/5604, signet 345/8770 and regtest 535/14517. Mainnet needs 689 KiB per peer against 722 KiB for the hard-coded 649/15400. On 2029-04-10, the date that pair was sized for, mainnet needs 14.7 KiB more than the pair, as you estimated. Testnet4 and signet fall much further, from 616/16199 and 629/15885, because each chain now uses its own genesis time, like HeadersSyncState's commitment bound when syncing from genesis. Rerunning the old script with 2009-01-03, mainnet's genesis date, reproduces all four hard-coded pairs.

tACK 0316d0dc9ec6a1abbe84489c44a47a2dd94c79ee Compiled from this branch, the two new functions return the exact optimum of their model for all five chains, every six hours from 2026-10-25 to 2041. The same holds on a grid of heights from 1 to 5 million and clocks, some before genesis. The chosen bufsize always meets the attack budget. Summing the attack model term by term, in exact arithmetic, gives the closed form for every period up to 16 and bufsize up to 60. I checked this with a Python port and a brute-force search; I can share the scripts as a gist. The new unit tests and p2p_headers_sync_with_minchainwork.py pass, and the parameters its nodes log match the port at each node's clock. At 2026-10-25 00:00 UTC the parameters, as period/bufsize, are mainnet 597/14697, testnet3 590/13096, testnet4 217/5604, signet 345/8770 and regtest 535/14517. Mainnet needs 689 KiB per peer against 722 KiB for the hard-coded 649/15400. On 2029-04-10, the date that pair was sized for, mainnet needs 14.7 KiB more than the pair, as you estimated. Testnet4 and signet fall much further, from 616/16199 and 629/15885, because each chain now uses its own genesis time, like HeadersSyncState's commitment bound when syncing from genesis. Rerunning the old script with 2009-01-03, mainnet's genesis date, reproduces all four hard-coded pairs.
DrahtBot commented 2026-11-26 17:43:36 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
hodlinator (Migrated from github.com) reviewed 2026-12-01 11:25:27 +00:00
hodlinator (Migrated from github.com) left a comment

Did some pen & paper math to verify the quadratic derivation step for M. Nice to finally make use of that. :) Haven't taken a stab at the weighted average step and not completely understood the log2 part yet.

Firing off what I have so far FWIW since the review was started >1month ago. Sorry I'm not able to find more time for deep work.

Did some pen & paper math to verify the quadratic derivation step for `M`. Nice to finally make use of that. :) Haven't taken a stab at the weighted average step and not completely understood the log<sub>2</sub> part yet. Firing off what I have so far FWIW since the review was started >1month ago. Sorry I'm not able to find more time for deep work.
Lines 363-367
hodlinator (Migrated from github.com) commented 2026-10-29 09:25:08 +00:00

One might add that since we still require reaching the minchainwork limit, potential attackers able to reach that have a strong incentive to instead mine upon the honest chain.

One might add that since we still require reaching the minchainwork limit, potential attackers able to reach that have a strong incentive to instead mine upon the honest chain.
Lines 432-433
hodlinator (Migrated from github.com) commented 2026-10-29 09:46:23 +00:00

remark: I considered whether it would simplify the math while still giving acceptable accuracy if we instead were always maximally pessimistic and assumed that the attacker is lucky enough to have the first commitment position at P-1. Clanker responded negatively.

remark: I considered whether it would simplify the math while still giving acceptable accuracy if we instead were always maximally pessimistic and assumed that the attacker is lucky enough to have the first commitment position at `P-1`. Clanker responded negatively.
Lines 452-455
hodlinator (Migrated from github.com) commented 2026-10-29 14:17:46 +00:00

Could include M?

     *  with solution for M =
     *
     *      [4*P - 1   +- sqrt(1   - 8*P^2 + 2^(K+4)*P * attack_headers)] / 2
     *    =  2*P - 1/2 +- sqrt(1/4 - 2*P^2 + 2^(K+2)*P * attack_headers)
Could include M? ```suggestion * with solution for M = * * [4*P - 1 +- sqrt(1 - 8*P^2 + 2^(K+4)*P * attack_headers)] / 2 * = 2*P - 1/2 +- sqrt(1/4 - 2*P^2 + 2^(K+2)*P * attack_headers) ```
hodlinator (Migrated from github.com) commented 2026-10-29 14:23:41 +00:00

Clanker helped clarify some undocumented steps for deriving this C++ expression like:

... upper bound for interval I haven't yet understood
=> attack_headers ≤ (2^−K*(3P−1)​) / 2
=> 2^K ≤ (3P−1​) / (2*attack_headers)

and then applying log2 on both sides.

Might be helpful to fill in the missing steps in the preceding comment or just above this line.

Clanker helped clarify some undocumented steps for deriving this C++ expression like: ``` ... upper bound for interval I haven't yet understood => attack_headers ≤ (2^−K*(3P−1)​) / 2 => 2^K ≤ (3P−1​) / (2*attack_headers) ``` and then applying log<sub>2</sub> on both sides. Might be helpful to fill in the missing steps in the preceding comment or just above this line.
Lines 582-593
hodlinator (Migrated from github.com) commented 2026-10-31 09:28:21 +00:00

Thanks for the explanation! I must have glossed over the bandwidth applying to all attackers and become derailed by the single core CPU comment.

Thanks for the explanation! I must have glossed over the bandwidth applying to *all* attackers and become derailed by the single core CPU comment.
Sign in to join this conversation.
No reviewers
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!35642
No description provided.