-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Mining: Avoid copying template CBlocks #32547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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/32547. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. 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. |
7b1a1f1
to
39a3b5b
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. |
Concept ACK on adding a This breaks the multiprocess build somehow, cc @ryanofsky, e.g.
|
I think goals of this PR should be achievable without too much work, but unfortunately the implementation as of 39a3b5b isn't really compatible with IPC so a different approach needs to be taken. There are two problems currently:
I also don't know motivation for TemplateToJSON and BlockCreateOptions changes in this PR:
All these things together suggest to me that the Mining interface should probably be exposing the CBlockTemplate struct directly and not hiding it behind so many accessor methods. This should allow dropping methods, simplifying the interface, and also making it more efficient by avoiding copies when it is called locally and no IPC is involved. The simplest way to do this would be to add new two accessor methods to virtual const CBlockTemplate* getTemplate() { return nullptr; }
virtual CBlockTemplate getTemplateData() = 0; which could be implemented in const CBlockTemplate* getTemplate() override { return m_block_template.get(); }
CBlockTemplate getTemplateData() override { return *m_block_template; } Then if the mining interface is being called from inside the local process, This should work, but would require clients to take call An alternate approach that would avoid unnecessary copies and be simpler for clients but require a little more complexity in the IPC implementation would be to expose an virtual const CBlockTemplate& getTemplate() = 0; with a custom implementation that caches the CBlockTemplate internally in the IPC client class so the references it returns will be valid and the data will be freed when the client is destroyed. There are a few examples of this pattern in #10102, but implementing this would requires using some more complicated c++ features like template specialization so would probably recommend trying the simpler approach first. |
The original reason for this was so that a Stratum v2 IPC sidecar can fetch the minimal info it needs for a NewTemplate message: basically the version field, some parts of the coinbase and its merkle path. It can do this without fetching the full block. Later on it turned out that this interface is also very useful for submitting a solution without (again) having to send a full block over the wire. Ditto for an ergonomic way to wait for fees to rise or a new tip to arrive (with multiple clients). If we assume that Stratum v2 sidecar is running on the same machine as the node, which seems pretty likely, the original optimisation might be overkill and it might as well get the full template. However it's also easier for the sidecar implementation if it doesn't have to hold on to these templates. An approach where the sidecar calls The only tricky bits are (since #31283 the sidecar doesn't use |
re: #32547 (comment)
Sorry, I did not write that very clearly. I am not suggesting dropping the This would just be a minor change to avoid unnecessary copies, not a broader rethinking of the interface. |
I agree it could just be an addition to the interface, though it also fits well if we want to prune it a bit. |
The constness of methods is a property of the interface. It is a way to say calling it will not change the object, and thereby allows using const references. |
Can be turned into draft while the CI is red? |
Yes, I understand the intent of marking Marking the methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistent use of const correctness and references //
nice improvement for clarity and performance!
Are you still working on this? |
Refactoring to avoid unnecessary copies/complexity, at least in the mainnet paths.
Also abstracts out a new
TemplateToJSON
function to keep track of variable scoping better.