mining: add getMemoryLoad() and track template non-mempool memory footprint#33922
mining: add getMemoryLoad() and track template non-mempool memory footprint#33922Sjors wants to merge 5 commits intobitcoin:masterfrom
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33922. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
I haven't benchmarked this yet on mainnet, so I'm not sure if checking every (unique) transaction for mempool presence is unacceptably expensive. If people prefer, I could also add a way for the |
21ad8c1 to
f22413f
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
|
||
| TxTemplateMap& tx_refs{*Assert(m_tx_template_refs)}; | ||
| // Don't track the dummy coinbase, because it can be modified in-place | ||
| // by submitSolution() |
There was a problem hiding this comment.
b9306b79b8f5667a2679236af8792bb1c36db817: in addition, we might be wiping the dummy coinbase from the template later: Sjors#106
f22413f to
3b77529
Compare
There was a problem hiding this comment.
Concept ACK
I think it would be better if we have internal memory management for the mining interface IPC, since we hold on to the block templates.
I would suggest the following approach:
- Add memory budget for the mining interface.
- Introduce a tracking list of recently built block templates and total memory usage.
- Add templates to the list and increment the memory usage after every
createnewblockorwaitnextreturn. - Whenever the memory budget is exhausted, we should release templates in FIFO order.
I think since we create a new template after a time interval elapses even if fees increase and that interval is usually enough for the client to receive and distribute the template to miners, this mechanism should be safe as the miners have long switch to most recent template when the budget elapsed because of the time interval being used in between returns of waitnext.
Mining interface clients should also handle their own memory internally.
Currently, I don’t see much use for the exposed getMemoryLoad method. In my opinion, we should not rely on the IPC client to manage our memory.
It seems counter intuitive, but from a memory management perspective IPC clients are treated no different than our own code. And if we started FIFO deleting templates that are used by our own code, we'd crash. So I think FIFO deletion should be a last resort (not implemented here). There's another reason why we should give clients an opportunity to gracefully release templates in whatever order they prefer. Maybe there's 100 downstream ASIC's, one of which is very slow at loading templates, so it's only given a new template when the tip changes, not when there's a fee change. In that scenario you have a specific template that the client wants to "defend" at all cost. In practice I'm hoping none of this matters and we can pick and recommend defaults that make it unlikely to get close to a memory limit, other than during some weird token launch. |
IMHO I think we should separate that, and treat clients differently from our own code, because they are different codebases and separate applications with their own memory.
I see your point but I don’t think that’s a realistic scenario, and I think we shouldn’t design software to be one-size-fits-all.
Delegating template eviction responsibility to the client can put us in a situation where they handle it poorly and cause us to OOM (but I guess your argument is that we rather take that chance than being in a situation where we make miners potentially lose on rewards). |
Note that it's already the clients responsibility, that's inherent to how multiprocess works. In the scenario where they handle it poorly, we can use FIFO deletion. All
We currently don't track whether any given
Afaik that means revalidating the block from scratch, removing one advantage the |
3b77529 to
24592b7
Compare
|
I restructured the implementation and commits a bit. The It's also less code churn because I don't have to touch the It also made it easier to move This in turn let me split out a separate commit that introduces the actual I added some comments to point out that we don't hold a |
24592b7 to
03dcfae
Compare
|
One caveat is that Expanded the PR description. |
03dcfae to
ac1e97a
Compare
587a18e to
e8aaada
Compare
|
The workaround is still needed. We can drop it here if #35073 lands. |
IWYU incorrectly suggested <variant> for std::hash in bitcoin#33922. On Ubuntu with include-what-you-use 0.26 based clang version 22.1.1, it incorrectly suggested <string_view>. On macOS with IWYU 0.26 based on clang version 22.1.2, it correctly suggests <functional>. Fix this by adding the mapping.
IWYU incorrectly suggested <variant> for std::hash in bitcoin#33922. On Ubuntu with include-what-you-use 0.26 based clang version 22.1.1, it incorrectly suggested <string_view>. On macOS with IWYU 0.26 based on clang version 22.1.2, it correctly suggests <functional>. Fix this by adding the mapping.
Build Bitcoin Core from the bitcoin/bitcoin#33922 branch for the SRI integration test so the node IPC interface matches the getMemoryLoad() coverage added in this branch. Assisted-by: OpenAI GPT-5-Codex
Build Bitcoin Core from the bitcoin/bitcoin#33922 branch for the SRI integration test so the node IPC interface matches the getMemoryLoad() coverage added in this branch. Assisted-by: OpenAI GPT-5-Codex
Build Bitcoin Core from the bitcoin/bitcoin#33922 branch for the SRI integration test so the node IPC interface matches the getMemoryLoad() coverage added in this branch. Assisted-by: OpenAI GPT-5-Codex
Build Bitcoin Core from the bitcoin/bitcoin#33922 branch for the SRI integration test so the node IPC interface matches the getMemoryLoad() coverage added in this branch. Assisted-by: OpenAI GPT-5-Codex
Build Bitcoin Core from the bitcoin/bitcoin#33922 branch for the SRI integration test so the node IPC interface matches the getMemoryLoad() coverage added in this branch. Assisted-by: OpenAI GPT-5-Codex
bf49247 to
4673836
Compare
ceb5b70 to
e23a8f3
Compare
e23a8f3 to
79a74da
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
79a74da to
b89942d
Compare
|
Waiting for CI to pass. I'm probably going to move 92fd54b and b89942d in a separate PR. Need to address this too: #33922 (comment) |
|
The safety commit for |
vasild
left a comment
There was a problem hiding this comment.
ACK d37e0fc
Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
ACK d37e0fc16c235ff511bd9485ef505f0c5615d9e4
-----BEGIN PGP SIGNATURE-----
iQQzBAEBCAAdFiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmnvNGMACgkQVN8G9ktV
y79uSyAAmswvwjxbv/vFTrf0BERXNHfG0lEVrvURRQavQzajUq0Q0dSiUpBlklGY
Ec+zt5x6G9vSGkYeo7ZFyjV7uwhLMMKlOGxqmnrKUT4PWHYjiItpnsJPEE8iK7hc
M2Xpq7OGNiWfc+ZT81KIYYPo6v+dRBKBh+dHwdgJ/5X7nFSMru7OV0+W4uk4wfu1
xayuZ5A1jWqM/vqYt9jEtmEaPhyVma61CxiMCGoxOIuhsuKGx49gZb+AQi3zuyD0
GKViOOA+A9krE0Ntr8MZeoXRw/+loj+yDp49s/l9LJ6rG8C3o4CKDMjzTcGSbWp8
sc1dN0wohyMwOrBToEidkC+G4eN1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pgp.org

Implements a way to track the memory footprint of all non-mempool transactions that are still being referenced by block templates, see discussion in #33899. It does not impose a limit.
IPC clients can query this footprint (total, across all clients) using the
getMemoryLoad()IPC method. Its client-side usage is demonstrated here:Additionally, the functional test in
interface_ipc.pyis expanded to demonstrate how template memory management works: templates are not released until the client drops references to them, or calls the template destroy method, or disconnects. The destroy method is called automatically by clients using libmultiprocess, as sv2-tp does. In the Python tests it also happens when references are destroyed or go out of scope.The PR starts with preparation refactor commits:
interface_ipc.pysodestroy()calls happen in an order that's useful to later demonstrate memory managementstd::unique_ptr<BlockTemplate> block_templatefrom astaticdefined inrpc/mining.cpptoNodeContext. This prevents a crash when we switch to a non-trivial destructor later (which usesm_node).Then the main commits:
template_tx_refstoNodeContextto track how many templates contain any given transaction. This map is updated by theBlockTemplateconstructor and destructor.GetTemplateMemoryUsage()which loops over this map and sums up the memory footprint for transactions outside the mempoolgetMemoryLoad()and add test coverage