mining: add getMemoryLoad() and track template non-mempool memory footprint

bitcoin/bitcoin: PR #33922

5 suggestions

Optional improvements

Link

[suggestion] · Same-wtxid readmission hides a retained allocation

The acknowledged wtxid approximation excludes a template-retained allocation when an equal transaction is readmitted as a different object, even after mempool churn stops.

Location: src/node/block_template_manager.cpp:122 (head).

Details and suggested fix

Templates retain the mempool entry's CTransactionRef. After eviction and readmission as a different object, exists(wtxid) returns true although the mempool no longer shares the allocation retained by the templates. Consider excluding only when m_mempool.get(tx_ref->GetWitnessHash()) == tx_ref. This uses the existing short-lock accessor and preserves pointer deduplication and the non-atomic snapshot behavior; it adds a shared-pointer increment and decrement rather than another lookup or lock.

The unit test already constructs different-object readmission: CreateValidMempoolTransaction() submits its own allocation, while the outer MakeTransactionRef() creates the test's separate tx1. Its re-entry assertion therefore encodes the underestimate. Update that expectation if switching to identity and add a same-object re-entry case using the template's reference. If retaining the approximation, label the existing case explicitly as such.


Link

[suggestion] · Batch template cleanup does quadratic registry work

With the manager alive, sequentially releasing N retained templates makes N(N+1)/2 weak-pointer lock attempts under m_mutex, delaying registration and memory-load snapshots; the practical cost has not been measured.

Location: src/node/block_template_manager.cpp:71 (head).

Details and suggested fix

UnregisterTemplate() scans every registry entry, and each BlockTemplateImpl destructor calls it while the manager remains alive. Releasing a retained batch through destroy() or disconnect cleanup therefore repeats the scan for each template, even for transaction-free templates. Consider an address-keyed non-owning registry such as std::unordered_map<const CBlockTemplate*, std::weak_ptr<const CBlockTemplate>>. It gives expected O(1) removal while preserving weak ownership and the linear snapshot. Every normal destructor already unregisters, so expired-entry pruning need not run on every release.


Link

[suggestion] · The public memory-load description exceeds the accounting scope

getMemoryLoad() documents templates' own data and excludes only the dummy coinbase, but the estimate counts four vector allocations and excludes every coinbase, including a retained client submission.

Location: src/node/block_template_manager.cpp:102 (head).

Details and suggested fix

The overhead sum omits the CBlockTemplate allocation and shared-pointer control block, plus m_coinbase_tx storage such as required_outputs, which receives the witness-commitment output. The unconditional drop(1) also skips the client's coinbase after submitSolution() installs it, including when submission fails and the template remains alive. Avoiding that unsynchronized read is justified, but the public wording should describe the narrower estimate. Specify that overhead covers the transaction-reference, fee, sigops and package-feerate vectors, plus referenced non-coinbase transactions outside the mempool, and that all coinbase transactions are excluded.


Link

[suggestion] · Non-Debug unit tests do not enforce the registry invariant

In non-Debug, non-fuzz unit-test builds, omitting template unregistration would leave the numeric assertions and SanityCheck() calls passing.

Location: src/node/block_template_manager.cpp:135 (head).

Details and suggested fix

SanityCheck() uses Assume(!weak.expired()), whose runtime failure check depends on G_ABORT_ON_FAILED_ASSUME. Meanwhile, memory-load snapshots skip expired entries, so the numeric checks after template release would not detect missing unregistration. The invariant is enforced in Debug builds, but not ordinary Release or RelWithDebInfo unit-test runs. Since this helper currently serves only tests, use Assert here to enforce the intended check in every unit-test configuration.


Link

[suggestion] · The overhead assertions reuse the value under test

Removing any fee, sigops or package-feerate vector contribution would leave the new unit test passing because it measures and reuses its own overhead baselines.

Location: src/test/miner_tests.cpp:1038 (head).

Details and suggested fix

Each *_overhead value comes from usage() and is only required to be positive. The later equalities reuse those values, so block.vtx alone can satisfy the overhead checks while the independent transaction deltas still match. Add a focused manager-level assertion comparing a registered CBlockTemplate's overhead with memusage::DynamicUsage of its actual four vectors. Using the original vectors' capacities avoids allocator-specific constants and preserves the existing overlap and lifetime checks.


Coverage details