Integer conversion

bitcoin/bitcoin: PR #36879

1 suggestion

Optional improvements

Link

[suggestion] · The first commit leaves deterministic undeclared

Source inspection shows that the first commit cannot compile, making it unusable for build-based bisection; the second commit repairs it, so the final tree is unaffected. No build was run.

Location: src/addrdb.cpp:199 at 82ed2c6bc79c (commit) (commit).

Details and suggested fix

Commit 82ed2c6bc79c429be1ea844eca287a43b308d9c4 changes the clamp but also deletes bool deterministic = HasTestOption(args, "addrman");. LoadAddrman still uses that name in the initial AddrMan construction and both database-recovery paths, with no declaration in scope. Commit a425e872084102933cbed8a15efeb089dc47f338 restores the declaration and blank line and removes the inline comment; it contains no independent change. Squash the second commit into the first so the series presents the conversion fix without an unbuildable intermediate revision. This leaves the final code unchanged: clamp in int64_t before narrowing to int32_t, preserving the deterministic test option and recovery paths.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Addrman was designed to bound address storage and resist a localized attacker filling the address table with attacker-controlled nodes.

The changed setting controls diagnostics for this address-management structure, not a consensus-validation rule. At the pinned head, AddrMan maintains new/tried buckets, address indexes, reference counts, and selection metadata; CheckAddrman verifies their internal relationships. The adversarial purpose explains why preserving the structure's invariants matters, without implying that consistency checks themselves prevent Sybil attacks.

Source 1

Consistency checks became a hidden runtime option because compile-time-only checks were not enabled in CI and were believed to receive little or no execution.

Historical PR #20233 introduced -checkaddrman so checks could be activated without rebuilding. The pinned LoadAddrman reads that option and passes its ratio into both the initial AddrMan constructor and replacement instances created during address-database initialization. The integer conversion sits at this diagnostic configuration boundary.

Source 1

Sampling was an explicit tradeoff between exercising invariants and keeping tests practical, rather than a requirement to inspect every operation.

In #20233, the author reported addrman unit-test times of 0.65 seconds without checks, 0.71 seconds at a ratio of 100, and 7.75 seconds at a ratio of 1, describing 1% checking as increased coverage with limited runtime impact. At both pinned base and head, Check() returns immediately for zero and otherwise samples with probability 1/n at each check invocation. Several public operations invoke it before and after their work, so the setting is not a strict periodic operation counter.

Source 1 · Source 2

An enabled consistency-check failure was deliberately changed from logging and continuing to asserting, to expose internal bugs prominently and match other consistency-checking components.

The #20233 description explicitly compares the failure behavior with CTxMemPool and TxRequestTracker, and historical discussion states that verbose failures make bugs less likely to be missed. Both pinned versions retain logging followed by assert(false) when CheckAddrman reports an error. Thus the ratio governs exposure to an intentionally terminating diagnostic, not merely additional logging.

Source 1 · Source 2

Later proposals to enable these checks automatically in debug builds were closed in favor of runtime opt-in because expensive diagnostics can be unsuitable for particular workloads.

In #27300, running checks at ratio 1 was reported to saturate a core; a later revision proposed ratio 1000 with little observed overhead on the author's non-listening node. In #24709, the objection was that expensive checks should be selected individually rather than enabled indiscriminately. The proposal's author explicitly accepted runtime configuration and closed #27300. This explains the continued importance of the user-controlled ratio and the pinned default of zero, without treating those historical measurements as present-day performance guarantees.

Source 1 · Source 2 · Source 3 · Source 4

Ralph's inferences

The affected integer boundary is best understood as translating an operator's diagnostic-intensity choice into Addrman's internal representation, rather than enforcing a network-protocol limit.

GetIntArg returns int64_t, whereas the AddrMan constructor accepts an int32_t consistency-check ratio. The base uses std::clamp<int32_t>; the head clamps in int64_t to the same stated interval, 0 through 1,000,000, and then explicitly converts to int32_t. Downstream sampling, default disabling, and assertion behavior are unchanged. Historical sources support the diagnostic purpose of this value, but do not establish a protocol rationale for its exact numeric bound or storage width.

Source 1 · Source 2 · Source 3