Skip to content

Connectblock checkonly - #20

Closed
optout21 wants to merge 912 commits into
masterfrom
connectblock-checkonly
Closed

optout21 wants to merge 912 commits into
masterfrom
connectblock-checkonly

Conversation

@optout21

Copy link
Copy Markdown
Owner

This is a small code-hygiene change to ConnectBlock method.

Problem: In the "check-only" mode of ConnectBlock the mutability of the CBlockIndex parameter is inconsistent with the semantics. In the default operation, ConnectBlock changes the provided CBlockIndex. However, it has a special "check-only" mode, where the provided chain is not changed. This mode is used from only one call site (TestBlockValidity), and is signaled by the fJustCheck = true flag.

Solution: Provide two versions of ConnectBlock: one with mutable chain and one "check-only" with const chain. There is no need for the fJustCheck flag. Both reuse the same internal implementation.

Additional info:

MarcoFalke and others added 30 commits August 5, 2026 11:22
The rule is named misc-anonymous-namespace-in-header in clang-tidy-22,
but use fuchsia-header-anon-namespaces for compat with clang-tidy-21.
aeca061 rpc: reject null for optional parameters (Ruslan Kasheparov)

Pull request description:

  Treat explicitly passed `null` as missing for optional RPC parameters that are required in certain contexts.

ACKs for top commit:
  achow101:
    ACK aeca061
  maflcko:
    review ACK aeca061 🥚
  sedited:
    ACK aeca061

Tree-SHA512: 60f146085fd20e532ba3cbefdb76d430938168621706a20b2b62a34318499fd72a8c934b08f690f9b72d19ed26581094517a0986586f95bc4b23fa8743b24d11
c9cedeb coins: group private cache helpers (Lőrinc)

Pull request description:

  **Problem:** `CCoinsViewCache::ReallocateCache()` is public even though only `Flush()` uses it.
  The private helper declarations are also split between the start and end of the class.

  **Fix:** Move `ReallocateCache()` and `FetchCoin()` into the existing private section.

ACKs for top commit:
  optout21:
    reACK c9cedeb
  sedited:
    ACK c9cedeb

Tree-SHA512: d06062daa9976d767375cd272b3c4ddd838e83fc8b3ac57f6b89a4d8d8837277211786cdd66a794b1a84a39cf6b38f2b9a01699d16e0a648d27b1b93cd0f33b5
This is standard C++20, and may allow a compiler to optimize a bit more.
3ac8b80 test: test the result order of a multiple import request is correct (Pol Espinasa)
e4732bf test: test invalid or missing timestamp throws importdescriptors (Pol Espinasa)
07fb58b test: Test a locked wallet rejects an empty importdescriptors request (Pol Espinasa)

Pull request description:

  In addition to bitcoin#35179 (already merged) this adds more missing test coverage that was detected while rebasing bitcoin#34861.

  The three tests added checks:
  - Locked wallet throws because of being locked if giving an empty importdescriptors request.
  - Invalid or missing timestamp throws as a top level RPC error and not a per-item error.
  - The order of the requests and the response is the same, even if failing or succeeding.

ACKs for top commit:
  nebula-21:
    ACK 3ac8b80
  Bicaru20:
    re-ACK 3ac8b80
  brunoerg:
    reACK 3ac8b80

Tree-SHA512: b6ba9e16bbdbefcab2529f49f9aab0ae8885bd2d381c6eec36ae442dea1aa2361e6fb339ab5bc2c51c3bef6216d8d939db53e57ec577f05fe54c07fc46f8f255
…namespace in header

fa93132 refactor: Enable misc-definitions-in-headers (MarcoFalke)
fa5ca87 refactor: Enable clang-tidy rule to reject anon namespace in header (MarcoFalke)
fafe504 refactor: Use C++20 std::identity over IntIdentity (MarcoFalke)

Pull request description:

  This avoids issues like bitcoin#35737 from passing CI in the first place.

  Also, the rule can help to avoid ODR issues, according to https://clang.llvm.org/extra/clang-tidy/checks/fuchsia/header-anon-namespaces.html

  This follows https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#sf21-dont-use-an-unnamed-anonymous-namespace-in-a-header

ACKs for top commit:
  sedited:
    ACK fa93132

Tree-SHA512: c1a9d573aed9f295a9fb1931e96e37a9c39e2339392af79d0a3441ebd29282812c2614eab13a01e0d508802f66b73e8f2387ffcbb7dd28676b4fa43f81f9a680
policy_estimator_io deliberately reuses a CBlockPolicyEstimator because
constructing one for every fuzz input severely reduces throughput.
However, Read() returns true for an incompatible old fee estimates file
without replacing the estimator state. The target then calls Write()
with state loaded by a previous input, making coverage depend on corpus
order.

Return false for incompatible files so the target skips Write() when no
state was loaded. This keeps the estimator reuse optimization instead of
resetting the expensive object before every fuzz input.

For the in-tree production caller, incompatible files remain non-fatal
and the estimator still starts from its default state. Read() now
reports failure, so startup emits one additional non-fatal warning.
Node startup and estimator state are unchanged, as is RPC behavior.

Co-authored-by: maflcko <[email protected]>
There is already a non-standard and internally used Compare() function,
and an standard operator<().

Also, there is already transaction_identifier::operator<=>().

It seems more consistent to remove the internal Compare() and have a
single standard C++20 <=> operator.
The getblock RPC used a boolean verbose argument before v0.15. Use the
legacy named argument so the helper can synchronize blocks from v0.14.x
nodes while remaining compatible with current nodes.
Test migratewallet on v0.14.3 non-HD and single-chain HD wallets in both
unencrypted and encrypted configurations.

Verify balances, transaction history, address ownership, descriptor
structure, encryption enforcement, backup creation, and the absence of
rescans or unexpected auxiliary wallets.

Run the test with an ASCII-only temporary directory in the Windows
cross-build job because the v0.14.3 binary cannot handle the Unicode
runner path.
NetBSD 11.0, the latest release, ships GCC 12.5.0 as the base
system compiler, which meets the minimum version requirement in
`doc/dependencies.md`.
On NetBSD, `pkgconf` is necessary to find `capnproto`. For example, see
bitcoin-core/libmultiprocess#325.
REST error responses normally go through RESTERR(), which sets
Cache-Control: no-store. Some errors are returned directly by the
generic HTTP dispatcher and bypass RESTERR(), including unmatched
paths, queue exhaustion, handler exceptions, and shutdown rejection.

Use a shared reply helper to set no-store on dispatcher-generated
error responses. This avoids caching transient errors and keeps the
behavior consistent across REST and RPC requests.

Test the work-queue rejection path by checking that its HTTP 503
response includes the header.
There is no reason we should process conflicting
advertisements for transactions, as they cannot be both
accepted into our mempool.

Avoid processing these and doing spurious work.
Found by Sjors in bitcoin#35896 (comment)

This should also fix GCC warnings such as bitcoin#35896 (comment)
This is unused since cluster-mempool. See the commit that forgot to
remove it:

```sh
$ git show 216e693 -U99999 | grep newFeeRate
     CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
-    if (const auto err_string{PaysMoreThanConflicts(ws.m_iters_conflicting, newFeeRate, hash)}) {
```
…ementChecks

fa9a9a8 refactor: Remove unused newFeeRate var in ReplacementChecks (MarcoFalke)

Pull request description:

  This is unused since cluster-mempool, so it seems confusing to keep around. See the commit that forgot to remove it:

  ```sh
  $ git show 216e693 -U99999 | grep newFeeRate
       CFeeRate newFeeRate(ws.m_modified_fees, ws.m_vsize);
  -    if (const auto err_string{PaysMoreThanConflicts(ws.m_iters_conflicting, newFeeRate, hash)}) {
  ```

ACKs for top commit:
  sedited:
    ACK fa9a9a8

Tree-SHA512: f8804109c59dfe37da754cb1924cc16abbc792fbb3619249a1edbc005f66fd88357c4810926625a375415c5be4ff056d68af3029f8075590252289c1d753a2fd
These have been unused since the following commits:
 - c61d3f0
 - e12ff8a
These have been unused since the following commits:
 - c320cdd
 - e9c5aeb
 - 216e693
Like authproxy.py, so that tests can work without having to think whether the cli was used or not.
achow101 and others added 28 commits August 19, 2026 10:47
cf36df0 Wallet: Check crypter return values (benthecarman)
b76afff Wallet: Use unsigned KDF iteration count (benthecarman)

Pull request description:

  CMasterKey::nDeriveIterations values are deserialized from wallet files
  as unsigned 32-bit integers, but key derivation narrowed the count to a
  signed int. A count above INT_MAX became negative in the conversion, and
  the derivation loop counter then overflowed, which is undefined
  behavior.

  Keep the count unsigned through the derivation path to match the
  serialized type, and add tests for zero and normal counts.

  Also check key-derivation calibration failures and validate calculated
  iteration counts before conversion. Keep the output master key unchanged
  until derivation and encryption succeed, and mark fallible crypter
  methods as [[nodiscard]].

ACKs for top commit:
  l0rinc:
    code review ACK cf36df0
  achow101:
    ACK cf36df0

Tree-SHA512: 95d5db2655fef8ca499af7da0f0258b4bee90975468286cb88e424257c4c5bf36407d2b2816b538e2ac3227e2a3a75c8d775211c65fe3ec6d12189da0b05fba1
6d387af psbt: remove write-only global xpub tracking set (Thomas)
3b7051c test: check combinepsbt with conflicting global xpub origins (Thomas)
7c632c0 psbt: avoid duplicate global xpub keys when merging (Thomas)

Pull request description:

  Global xpubs are stored in a map of key origin to set of xpubs, while the serialization writes one record per xpub, keyed by the xpub. `Merge` unions the map origin-by-origin, so when the combined PSBTs provide different key origins for the same xpub, the result serializes the same `PSBT_GLOBAL_XPUB` key twice. BIP 174 declares PSBTs with duplicate keys invalid and the deserializer rejects them, so `combinepsbt` returns a PSBT that no RPC can parse again. This affects all releases since the merge loop was added in bitcoin#17034 (v23.0).

  <details><summary>Reproduction on master</summary>

  The PSBTs share the unsigned transaction and xpub, and differ only in the master fingerprint of the global xpub record (`00000000` vs `11111111`):

  ```
  $ A=cHNidP8BADwCAAAAAaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqAAAAAAD/////AQAAAAAAAAAAAAAAAABPAQQ1h88AAAAAAAAAAACHPf+BwC9SViP9H+UWfqw6VaBJ3j0xS7Qu4if/7TfVCAM5o2ATMBWX2u9B++WToCzFE9C1VSfsLfEFDi6P9JyFwgQAAAAAAAAA
  $ B=cHNidP8BADwCAAAAAaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqAAAAAAD/////AQAAAAAAAAAAAAAAAABPAQQ1h88AAAAAAAAAAACHPf+BwC9SViP9H+UWfqw6VaBJ3j0xS7Qu4if/7TfVCAM5o2ATMBWX2u9B++WToCzFE9C1VSfsLfEFDi6P9JyFwgQRERERAAAA
  $ bitcoin-cli -regtest decodepsbt "$(bitcoin-cli -regtest combinepsbt "[\"$A\",\"$B\"]")"
  error code: -22
  error message:
  TX decode failed Duplicate Key, global key "01043587cf00...9c85c2" already provided: iostream error
  ```

  </details>

  Deduplicate by xpub when merging, keeping the origin that is already present: BIP 174 lets the Combiner "pick arbitrarily when conflicts occur", and conflicting unknown and proprietary records are already resolved the same way. The logic is shared between `combinepsbt` and `joinpsbts` through a new `MergeGlobalXPubs` helper. The second commit adds a test that fails on master with the error above, and the last commit removes the `global_xpubs` tracking set in `Unserialize`, write-only since the generic duplicate key check introduced in bitcoin#21283 (1e2d146) replaced the explicit one.

  Note: the xpub loop in `joinpsbts` currently has no observable effect, since the collected xpubs never reach the returned PSBT. My bitcoin#35516 fixes that, so this PR should land first: on its own, bitcoin#35516 would make the same duplicate key issue reachable through `joinpsbts`, while with the shared helper in place it never becomes reachable. I will rebase bitcoin#35516 on top afterwards.

ACKs for top commit:
  Bicaru20:
    tACK 6d387af.
  achow101:
    ACK 6d387af
  winterrdog:
    tACK 6d387af

Tree-SHA512: e2a9e02617eeec22a9240d7cf9386ee880a5f3639b143df7de4d8ea3e7b808f8c123f0b0410ff4a22e9a564bd86b2335a5c4aa3b2281af47d111484a6f1fd108
fa0fe21 test: [refactor] Properly use BOOST_CHECK_EXCEPTION (MarcoFalke)

Pull request description:

  The exception checking in unit tests is partly verbose, fragile, inconsistent and thus confusing.

  Fix all those issues by using `BOOST_CHECK_EXCEPTION` consistently:

  * The test code is less bloated and follows a standard pattern; Extra state and dead code like `exceptionThrown = false;` or `BOOST_CHECK(0)` can be removed.
  * The checks are more strict, because they use `HasReason{...}` or a similar predicate.

ACKs for top commit:
  l0rinc:
    ACK fa0fe21
  janb84:
    ACK fa0fe21
  jonatack:
    Light ACK fa0fe21

Tree-SHA512: f3a9abfe02988299f6d2604643feb630e078c2177287899d4fcc191802536d41d09e6365d81d0184bf3533583987969cb1b85ab3e112e7364fdb5b85a3405cb7
Steps to reproduce the diff:
```
cmake --preset dev-mode
cmake --build build_dev_mode --target translate
```
… to weak_ptr

15e5c35 doc: Correct comments after HTTPRequest::m_client was changed from shared to weak pointer (Hodlinator)

Pull request description:

  There were lingering comments from when `HTTPRequest::m_client` was a `shared_ptr`.
  Prompted by bitcoin#36007 (comment).

  Follow-up to bitcoin#36007.

ACKs for top commit:
  winterrdog:
    ACK 15e5c35

Tree-SHA512: 021427258e46d2a6a19f5304167c957084b174a4f1fd0e223605f9b06ee771225d023e5f546db9045e1bfdfd5558f933e54d9ee76e5393949612628be9815bc4
…Line on empty command

fef99e6 qt: fix out-of-bounds read in RPCParseCommandLine on empty command (sayed nabhan)

Pull request description:

  When a console line has no command name (it starts with `)`, or is `()`, `(`, or `,`), RPCParseCommandLine reaches the command-execution branch while the current argument frame is still empty, so `stack.back()[0]` reads out of bounds and the argument list built from `stack.back().begin() + 1` to `end()` is an invalid iterator range (throws std::length_error in practice, UBSan flags the null-pointer reference otherwise).

  The `(` branch already guards the frame with `stack.back().size() > 0`, so I add the same check to the `)`/newline branch and the empty frame is skipped. To be clear, `(` alone isn't safe on master either: it fails via the `\n` branch, not via the `(` branch itself (the state there isn't `STATE_ARGUMENT`), and `,` alone fails the same way.

  Since there's no command to run in any of these cases, the parser now returns `false` so the console reports an invalid command line, consistent with other fully-invalid input like a bare `'` or `"`, rather than silently ignoring it.

  Regression cases added to rpcNestedTests for `)`, `()`, `(` and `,` (all abort on master without the guard), plus `getblockchaininfo)` which stays tolerated.

ACKs for top commit:
  hebasto:
    ACK fef99e6, tested on Ubuntu 26.04.

Tree-SHA512: 15822a0525402878483d5b2d0fe7b9e27916e514c1a8ad4a697f11b1e5cfe33f5fcd1f089efefb976bcf0d84cdf5166a58a070593924c2d4c2e1f8224d06590f
5a431c9 qt: Update `src/qt/locale/bitcoin_en.ts` translation source file (Hennadii Stepanov)

Pull request description:

  This PR follows our [Release Process](https://github.com/bitcoin/bitcoin/blob/4df077d7cd32c71646a85a2464a58c322a0cee11/doc/release-process.md).

  It is required for the translation string freeze, as bitcoin-core/gui#957 introduced a new translatable string after the soft translation string freeze.

  Steps to reproduce the diff:
  ```console
  cmake --preset dev-mode
  cmake --build build_dev_mode --target translate
  ```

ACKs for top commit:
  polespinasa:
    ACK 5a431c9

Tree-SHA512: 89ee1721c5091fa0deb11f9c014515846b8af781c03d3aaed2ff757e475927d5e9bd5a16a496de0f5269e6371f2ad27d5065ef656c9376793332a05aa707272b
Add MemPoolFeeRateEstimator, which calls Bitcoin Core's block
assembler with the mempool and chainstate to build a block template and
use its chunk fee rates for fee rate estimation.

Add CalculateMaxWeightPercentiles to return the 50th and 75th
percentile chunk feerates by cumulative block weight. If sparse,
EstimateFeeRate uses the higher of the minimum relay fee rate and the
current mempool minimum fee rate.

The 50th percentile is returned as the conservative estimate, and the
75th percentile as the economical estimate.

Wire MemPoolFeeRateEstimator into FeeRateEstimatorManager and add
FeeRateEstimatorType::MEMPOOL_POLICY for result attribution.

Add unit tests for the mempool fee rate estimator and fee estimator
string conversions, plus fuzz coverage for the string conversions.

Co-authored-by: willcl-ark <[email protected]>
Cache previous mempool fee rate estimates.

Cached estimates are tagged with the chain tip they were computed on
(the template's hashPrevBlock). They are only served while they are
not stale and the chain tip has not changed. This avoids generating
block templates too often.

The estimator lock is not held while building a block template, so
concurrent callers may duplicate estimation work; the tip tag keeps
stale results out of the cache.

Co-authored-by: willcl-ark <[email protected]>
fa8762d build: ci/doc win64-cross build via nix (MarcoFalke)
fafe720 doc: Clarify that cygwin/msys2 are not tested/supported (MarcoFalke)

Pull request description:

  Release cross-builds to win64 are done in guix. There are also docs to use Debian/Ubuntu for those cross-builds and this approach is used in CI. However, there are many problems:

  * The CI is intended to mirror the guix build, but often it is not possible to find the major versions used for mingw and GCC in the guix build in the `apt` packages for an LTS distro.
  * Users on older distro releases may lack released bugfixes, such as mingw-w64/mingw-w64@8e06daa in mingw 13  (e.g. Debian Trixie with mingw 12, https://packages.debian.org/trixie/mingw-w64-x86-64-dev).
  * When using the UCRT variant of the build, this uncovers bugs such as https://bugs.launchpad.net/ubuntu/+source/mingw-w64/+bug/2106420 or https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1121403.

  So add a way to use nix to do the cross build. This allows to closer mimic the guix build.

  Also, clarify that cygwin/msys2 are not tested/supported.

ACKs for top commit:
  willcl-ark:
    reACK fa8762d
  hebasto:
    re-ACK fa8762d.

Tree-SHA512: de94f8bc4bb6ed9a75352cd909aa227c5954e336bf9b14969d7412b5cedfbe6cd6b2d8b476d5b1b0bcfc93bdb32fc229015855082350c8c507189e34b9b3ef3f
Integrate MemPoolFeeRateEstimator into FeeRateEstimatorManager.
When both estimators succeed, select the lower of the block policy
and mempool estimates.

When either estimator fails, return its error instead of falling back
to the block policy estimate: if the mempool estimator cannot produce
an estimate, the combined estimate fails.
Callers that want a block-policy-only estimate can request it explicitly
via fee_rate_estimator option.

estimatesmartfee now emits the estimator field only for successful
manager-selected estimates.

Add a test that ensures estimatesmartfee returns the mempool fee rate
estimate when it is lower than the block policy estimate, and can request
the mempool policy estimator explicitly

Two wallet functional tests also need adjusting. When the mempool is
too sparse to fill its percentile buckets, MemPoolFeeRateEstimator
returns a relayable floor of max(min relay fee, mempool min fee), so in
regtest getFeeRateEstimate now returns the min relay fee where the
wallet previously had no estimate and fell back to a higher rate:

- wallet_taproot.py: the cleanup sendall used automatic fee estimation.
  GetMinimumFeeRate previously fell back to the wallet fallback fee
  (fallbackfee, 20 sat/vB in the test framework); it now uses the min
  relay fee floor. At that lower feerate the wallet's underestimate of
  the taproot script-path witness size drops the effective feerate
  below min relay, so the transaction is rejected. Pin fee_rate=20 to
  match the framework fallbackfee.

- wallet_bumpfee.py: GetDiscardRate() previously fell back to the
  wallet discard rate (-discardfee); it now takes the minimum of that
  and the estimate, so the min relay fee floor collapses the discard
  rate down to the dust relay feerate. The lower discard rate reduces
  the cost of change, so the ~614 sat leftover change in
  test_dust_to_fee is now retained instead of being dropped to fee.
  Rework the test to leave a sub-dust (20/270 sat) change that is
  dropped regardless of the discard rate.

Co-authored-by: willcl-ark <[email protected]>
Return the removed mempool transaction info from
CTxMemPool::removeForBlock instead of dispatching the
MempoolTransactionsRemovedForBlock notification from the mempool.

Emit it from ConnectTip after mempool removal and before BlockConnected,
passing the connected block, the removed mempool transactions, and the
block height to the callback.

Because the signal now originates from ConnectTip, where the IBD state is
known, gate it on !IsInitialBlockDownload(): the notification is no longer
fired for blocks connected during initial block download or reindex, while
the mempool removal in removeForBlock still runs unconditionally. This keeps
fee rate estimators from recording blocks connected before the node is
synced.
Gate the mempool fee rate estimator on a coverage check: recent
connected blocks must be well represented by transactions removed from
our mempool.

Track per-block weight for the last MEMPOOL_HEALTH_WINDOW_BLOCKS blocks.
AddMinedBlockStats drops stats at or above a connected block's height
before appending it, and resets the window on a forward height gap so
tracked heights stay consecutive.

Only apply the coverage ratio once the window holds at least one block
of transactions; below that activity is too low for the ratio to be
meaningful, so treat the mempool as healthy.

Replace a boolean health check with a MempoolHealth enum so
EstimateFeeRate() can report whether estimation is unavailable because
too few recent blocks have been tracked (INSUFFICIENT_DATA) or because
recent blocks poorly represent the mempool (LOW_COVERAGE).
Add a verbosity option to the existing estimatesmartfee options object.
The default verbosity remains 1.

When verbosity is at least 2 include mempool_health_statistics in the response.
The array reports the mined blocks tracked by the mempool fee rate estimator in
most-recent-first order, with each entry containing:

- block_height
- block_weight: total non-coinbase transaction weight in the block
- mempool_txs_weight: weight of transactions removed from our mempool
  for that block

Expose these stats through the fee rate estimator manager so RPC users
can inspect the block coverage data used by the mempool health check.
Move block policy fee estimates from fee_estimates.dat to
fees/block_policy_estimates.dat.

On startup, migrate the legacy file to the new path when only the legacy
file exists. If both files exist, keep the new file and remove the
legacy file.

Rename the block policy estimator args source files to the generic
estimator_args.{cpp,h} names and rename FeeestPath to
BlockPolicyFeeEstPath while the path helper is moved into the shared fee
estimator argument code.
Persist MemPoolFeeRateEstimator's recent mined-block statistics
to fees/mempool_policy_estimator.dat and reload them at startup.

Without this, the mempool estimator starts cold after each restart
and treats the mempool as unhealthy until MEMPOOL_HEALTH_WINDOW_BLOCKS
blocks have been observed, causing the default combined estimatesmartfee
request to return a mempool fee rate estimator error.

Files with more stats than MEMPOOL_HEALTH_WINDOW_BLOCKS,
non-consecutive block heights, or a final block that does not match the
active chain tip are rejected on read, preserving the invariant that
loaded stats describe the current chain.

Add MempoolPolicyEstimatorPath(), pass the path through
FeeRateEstimatorManager, and flush both block-policy
and mempool-policy estimator files on interval and shutdown.
Boost.Test registers a `SIGSTKSZ` alternate signal stack as each test binary starts, before any tests run.
On musl, this can be smaller than Linux's hardware-dependent minimum, causing `sigaltstack()` to fail with `ENOMEM` during setup.

Disable Boost.Test's alternate stack in both test entry points.
Signal handlers continue to use the regular process stack. Boost.Test can no longer report stack overflows.

Fixes bitcoin#36026

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
ef501a6 consensus: document merkle mutation root invariant (Lőrinc)

Pull request description:

  **Problem:** `ComputeMerkleRoot`'s optional mutation flag and the reasoning behind its per-level check are undocumented, and the behavior is only exercised indirectly by merkle_test through random duplications and old-vs-new comparisons, so a refactor could silently change it, as the discussions in bitcoin#22046 and bitcoin#28430 illustrate.

  **Fix:** Document the flag on the function declaration, explain inside the inner loop why the mutation check runs at every tree level even after a duplicate is found, and add direct API coverage for the CVE-2012-2459 construction.

  **Coverage check:** Both `merkle_test` and the new `merkle_test_mutated_return_value` would fail under a refactor that stops the outer reduction once mutation is detected, e.g.:
  <details><summary>Hypothetical regression</summary>

  ```patch
  diff --git a/src/consensus/merkle.cpp b/src/consensus/merkle.cpp
  index dfa23cf..40bc3f8efa 100644
  --- a/src/consensus/merkle.cpp
  +++ b/src/consensus/merkle.cpp
  @@ -59,6 +59,7 @@ uint256 ComputeMerkleRoot(std::vector<uint256> hashes, bool* mutated) {
                   if (hashes[pos] == hashes[pos + 1]) mutation = true;
               }
           }
  +        if (mutation) break;
           if (hashes.size() & 1) {
               hashes.push_back(hashes.back());
           }
  ```
  </details>

  Fixes bitcoin#28457

ACKs for top commit:
  optout21:
    reACK ef501a6
  achow101:
    ACK ef501a6
  w0xlt:
    reACK ef501a6
  hodlinator:
    ACK ef501a6

Tree-SHA512: 5a54eed071079a0a37333d5ba7c2d8eb81ae318ee4c84e15e3c050198daea6282453d4f7727b75f7dab90696b3d9bc946b8b33e6456a7f190b2297c15aca390c
…util::Expected<void, PSBTError> and remove PSBTError:Ok

6cca38e refactor: remove unused PSBTError::Ok (kevkevinpal)
3660678 refactor: SignPSBTInput now uses util:Expected (kevkevinpal)

Pull request description:

  ### Description
  This is a follow-up to bitcoin#31622 (comment) and bitcoin#31622 (comment)

  ### What this changes
  - Updates `SignPSBTInput` to return `util::Expected<void, PSBTError>`
  - Removes `PSBTError:Ok` from the `PSBTError` Enum

ACKs for top commit:
  achow101:
    ACK 6cca38e
  rkrux:
    lgtm ACK 6cca38e
  jeanpablojp:
    tACK 6cca38e

Tree-SHA512: c83b2e7e440aff01becc788e36a732756085f507d447eaa16dd1276d0d0b54070d0d8a51727d553d214043935072586111a175f34338a8d1886a6d5b251de7e4
Replace separate member assignments with construction, using brace
initialization so the size_t to CAmount conversions have to be explicit.
Use uint32_t for the loop index feeding COutPoint::n, which avoids
conversion entirely.
…e expression

950bdb7 bench: Construct CTxOut and COutPoint in a single expression (Alexander Wiederin)

Pull request description:

  Replaces field-by-field mutation of `CTxOut` and `COutPoint` in two bench files with brace initialisation, which requires the `size_t` conversions to be made explicit.

  Noticed while looking at bitcoin#35994, where switching the proposed fix-it to `{}` surfaces implicit narrowing conversions like these.

  The constructed values are unchanged.

  *Note: Only the sites where a conversion is involved are included in this PR; the remaining field-by-field construction in `bench/` would be covered by bitcoin#35994's follow-ups.*

ACKs for top commit:
  l0rinc:
    code review ACK 950bdb7
  maflcko:
    review ACK 950bdb7 🐯

Tree-SHA512: c664d41eeeb9241d381e5fa27689f18d9445a942055734463978498f60d0f2c495a0d7a58131493bb63267371bb6fe2d45ac124d910c919c8905322948d17a23
…reduce overestimation

7f9c4e2 doc: add release notes (ismaelsadeeq)
e18d392 test: add mempool estimator i/o fuzz test (ismaelsadeeq)
970f020 fees: persist mempool policy estimator data (ismaelsadeeq)
7dcb379 fees: move fee_estimates.dat into fees directory (ismaelsadeeq)
0db2b69 rpc: add verbosity option to estimatesmartfee options (ismaelsadeeq)
06bb657 fees: gate mempool estimates on recent block coverage (ismaelsadeeq)
cfe585d validation: emit block mempool removal signal from ConnectTip (ismaelsadeeq)
0d88558 fees: return mempool estimates when it's lower than block policy (ismaelsadeeq)
693b135 fees: add caching to MemPoolFeeRateEstimator (ismaelsadeeq)
c9bb3df fees: add MemPoolFeeRateEstimator class (ismaelsadeeq)
9cacf67 rpc: add fee_rate_estimator option to estimatesmartfee (ismaelsadeeq)
ba6c61b fees: add FeeRateEstimatorManager class (ismaelsadeeq)
2cb6b83 fees: add EstimateFeeRate and MaximumTarget to CBlockPolicyEstimator (ismaelsadeeq)
5adb2ab refactor: test block policy estimator directly (ismaelsadeeq)
9c8309a test: rename policy estimator tests to block policy estimator tests (ismaelsadeeq)
e3d5ef1 fees: move StringForBlockPolicyEstimateReason to block policy estimator (ismaelsadeeq)
74245c2 fees: split wallet and estimator fee reasons (ismaelsadeeq)

Pull request description:

  This PR is another attempt to fix bitcoin#27995 using a better approach.

  For background and motivation, see bitcoin#27995 and the discussion in the Delving Bitcoin post [Mempool Based Fee Estimation on Bitcoin Core](https://delvingbitcoin.org/t/mempool-based-fee-estimation-on-bitcoin-core/703).

  This PR is currently limited to using the mempool only to lower what is recommended by the Block Policy Estimator.
  Accurate and safe fee estimation using the mempool is challenging. There are open questions about how to prevent mempool games that are theoretically possible for miners [(a variant of the Finney attack)](https://delvingbitcoin.org/t/mempool-based-fee-estimation-on-bitcoin-core/703/6).

  This is one reason this PR uses the mempool only to lower the Block Policy Estimator result. The Block Policy Estimator itself is not gameable in this way, so the combined estimate is not susceptible to this attack increasing the returned feerate.

  The underlying assumption is that, with the current tools and work done to make RBF and CPFP feasible and reliable (TRUC transaction relay, ephemeral anchors, cluster size 2 package RBF), underestimation is safer than overestimation. We now assume it is relatively easy to fee-bump later if a transaction does not confirm, whereas once a fee is overestimated there is no way to recover from that.

  Another open question when using the mempool for fee estimation is how to account for incoming transaction inflow.
  [Bitcoin Augur](https://github.com/block/bitcoin-augur) does this by using past inflow plus a constant expected inflow to predict future inflow. I find this unconvincing for fee estimation and potentially prone to more overestimation, as past conditions are not always representative of the future. See my [review of the Augur fee rate estimator and open questions](block/bitcoin-augur#3).

  This PR uses a much simpler approach based on current user behavior, similar to the widely used method employed by mempool.space: looking at the top block of the mempool and selecting a percentile feerate depending on whether the user is economical or conservative.

  Empirical data from both myself and Clara Shikhelman shows that the 75th percentile feerate for economical users and the 50th percentile feerate for conservative users provide positive confirmation guarantees, hence this is what is used in this PR.

  Parallel research by Rene Pickhardt and his student suggests that using the [average fee per byte of the block template performs well](https://delvingbitcoin.org/t/mempool-based-fee-estimation-on-bitcoin-core/703/12).

  All of these are constants that can be adjusted. There is parallel work exploring these constants and running benchmarks across fee estimators to find a sweet spot.

  See also work in LND, the [LND Budget Sweeper](https://delvingbitcoin.org/t/lnds-deadline-aware-budget-sweeper/1512), which applies this idea successfully. Their approach is to estimate fees initially with bitcoind, then increment gradually as the confirmation deadline approaches, using a fixed fee budget.

  Historical data indicates that this PR's approach can [reduce overestimation quite significantly (~29%)](https://delvingbitcoin.org/t/mempool-based-fee-estimation-on-bitcoin-core/703/8).

  This is particularly useful in scenarios where the Block Policy Estimator recommends a high feerate while the mempool is empty.

  <img width="1800" height="1090" alt="56f3ba26c0184521c42bb82ec9d8c9f2224d4f8e" src="https://github.com/user-attachments/assets/c035c40c-8ece-42a7-b290-d29f1ac9bf4d" />

  As seen in the image above, there is only one remaining unfixed case: when there is a sudden inflow of transactions and the feerate rises, the Block Policy Estimator takes time to reflect this. In that case, users will continue to see a low feerate estimate until it slowly updates. From the historical data linked above, [this occurs about ~26% of the time](https://delvingbitcoin.org/t/mempool-based-fee-estimation-on-bitcoin-core/703/8).

  Overall, we observe a **73% success rate with 0% overestimation, and 26% underestimation** with this approach.

  See https://bitcoincorefeerate.com/stats for recent running stats that have almost identical data.

  This PR also includes refactors that enable this work. Rather than splitting the PR and implementing changes incrementally, I opted for an end-to-end implementation:

  ### 1. Refactors

  * Split the mixed fee reason enum into separate wallet and block policy concepts. The wallet now has a `FeeReason` enum for why the wallet selected a fee rate (`FEE_RATE_ESTIMATOR`, `MEMPOOL_MIN`, `USER_SPECIFIED`, `FALLBACK`, `REQUIRED`), while the Block Policy Estimator uses `BlockPolicyEstimateReason` for its internal threshold details.
  * Move `StringForBlockPolicyEstimateReason` to the Block Policy Estimator code, keeping the estimator-specific strings with the estimator.
  * Move detailed Block Policy Estimator logging out of wallet transaction creation and into the estimator path. Wallet transaction creation now logs the selected fee and wallet fee reason instead of leaking estimator internals.
  * Keep the wallet RPC `fee_reason` field name for compatibility, but update its meaning to report the wallet fee reason instead of the Block Policy Estimator's internal threshold reason.
  * Rename policy estimator tests and files to block-policy-specific names where appropriate.
  * Update Block Policy Estimator unit tests to be independent of the mempool and validation interface.

  ### 2. Introduce Mempool-Based Fee Estimator and Fee Estimator Manager

  * Introduce `FeeRateEstimation` and `FeeRateEstimationError` as common estimator result types, avoiding new out-parameters for fee estimation results.
  * Add `FeeRateEstimatorType` to identify the estimator that produced a result.
  * Add `FeeRateEstimatorManager`, responsible for owning the Block Policy Estimator and Mempool Fee Rate Estimator.
  * Update the node context to store a `std::unique_ptr` to `FeeRateEstimatorManager` instead of `CBlockPolicyEstimator`.
  * Update `CBlockPolicyEstimator` to no longer subscribe directly to the validation interface; instead, `FeeRateEstimatorManager` subscribes and forwards relevant notifications.
  * Add a mempool fee estimator that generates a block template when called, calculates a percentile feerate, and returns the 75th percentile for economical mode or the 50th percentile for conservative mode.
  * When the selected estimate is below the node's fee floor, `estimatesmartfee` still returns at least the max of `mempoolminfee` and `minrelaytxfee`.
  * Add caching to the mempool estimator so new estimates are generated at most every 7 seconds while the chain tip is unchanged, assuming enough [transactions have propagated](https://bitcoin.stackexchange.com/questions/125776/how-long-does-it-take-for-a-transaction-to-propagate-through-the-network/125777#125777) to make a meaningful difference.
    This heuristic will likely be replaced by requesting block templates via the general-purpose block template cache proposed here: bitcoin#33389
  * Update `MempoolTransactionsRemovedForBlock` to receive the connected block as well as the transactions removed from the mempool.
  * Track the weight of block transactions and mempool transactions removed due to block connection after each block connection.
    This data is tracked for the last 6 mined blocks. A mempool feerate estimate is returned only when the ratio of mempool transaction weight removed due to block connection to block transaction weight is greater than 75% across the tracked window. This heuristic provides rough confidence that the node's mempool matches that of the majority of the hashrate. The 75% threshold is arbitrary and can be adjusted.

  There is a caveat when transactions in the local mempool are consistently not mined by the network, as described in bitcoin#27995 (e.g. due to filtering).
  Accounting for these transactions during fee estimation is not necessary, as they should be evicted from the mempool itself (see bitcoin#33510). Handling this again within fee estimation would be redundant.

  * Persist statistics for the 6 most recent mined blocks to `fees/mempool_policy_estimator.dat` during periodic flushes and shutdown, so this data is available after restarts.
  * Move Block Policy Estimator data from `fee_estimates.dat` to `fees/block_policy_estimates.dat`, migrating the legacy file during startup when needed.
  * Add `fee_rate_estimator` to the `estimatesmartfee` options object. Supported values are `"none"` (default combined behavior), `"block_policy"` (use only the Block Policy Estimator), and `"mempool_policy"` (use only the Mempool Fee Rate Estimator). Unknown values are treated as `"none"`.
  * Add `verbosity` to the `estimatesmartfee` options object. With `verbosity >= 2`, the RPC returns recent mempool health statistics.
  * Expose the selected fee rate estimator in `estimatesmartfee` results when `fee_rate_estimator` is `"none"` and the estimate succeeds.
  * Add unit, functional, and fuzz test coverage for the new estimator behavior, persistence, RPC options, and estimator I/O.

  <details>
  <summary>see example output</summary>

  ```bash
  bitcoin-cli estimatesmartfee 1 economical '{"verbosity": 2, "fee_rate_estimator": "none"}'
  ```

  ```json
  {
    "feerate": 0.00002133,
    "estimator": "mempool_policy",
    "blocks": 2,
    "mempool_health_statistics": [
      {
        "block_height": 927953,
        "block_weight": 3991729,
        "mempool_txs_weight": 3942409
      }
    ]
  }
  ```

  </details>

ACKs for top commit:
  willcl-ark:
    reACK 7f9c4e2
  jsarenik:
    Approach ACK 7f9c4e2

Tree-SHA512: c35b423eea0eb34524cf5ad07822c0ab8d53e2ab78965b58c8738044c61c77352184822360ed077a51bfbf83d0226d221e988f7156b1948023707c7e1fb31495
9eba3aa test: avoid undersized Boost.Test signal stacks (Lőrinc)

Pull request description:

  **Problem:** Boost.Test can fail while an Alpine CI test binary is starting, before any tests run.
  The required signal stack size depends on the runner's CPU features, while musl provides a fixed size.

  **Fix:** Use the [regular process stack](https://www.boost.org/doc/libs/latest/libs/test/doc/html/boost_test/utf_reference/link_references/config_disable_alt_stack.html) for Boost.Test signal handling in both unit-test binaries.

  Fixes bitcoin#36026

ACKs for top commit:
  maflcko:
    lgtm ACK 9eba3aa

Tree-SHA512: 177a31ab325f4ebce0ad7e4679b171cd5a28787595bf0e117fc66731b8bf9157c4f5bef74cc6bb7651021bafb616fe5e9a25709abf30b7689d10ddd0fbd484cb
dd669f4 util: set os-level thread names on Windows (ViniciusCestarii)

Pull request description:

  Update SetThreadName to set os-level thread names on Windows too.

  This is useful for debugging-ergonomics on Windows. Threads currently show up unnamed in debuggers, crash dumps on Windows and mismatch what's documented under https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#threads.

  Tested with the mingw cross build running on Windows 11, print from WinDbg:

  <img width="713" height="631" alt="image" src="https://github.com/user-attachments/assets/05e03383-c9b1-4e6b-91f3-9088b2fc7e90" />

ACKs for top commit:
  l0rinc:
    code review ACK dd669f4
  hebasto:
    ACK dd669f4, tested Guix-built `bitcoind.exe` on Windows 11 Pro using WinDbg:
  winterrdog:
    utACK dd669f4

Tree-SHA512: 3632584f5f0612414a53ad6d868b9f832e8e7f1fad19f212f292172a4a6516f6e0055ec6ac8fbb71b22acdd003d09c9a5f97c0c15a137e1ad242580f997aca6f
New `TestConnectBlock` method for the check-only variant of `ConnectBlock`,
with const `CBlockIndex` parameter. The no-longer-needed `fJustCheck` flag
is dropped.
@optout21 optout21 closed this Aug 24, 2026
@optout21

Copy link
Copy Markdown
Owner Author

-> #21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.