-
Notifications
You must be signed in to change notification settings - Fork 37.8k
refactor: Make explicit CMutableTransaction -> CTransaction conversion. #14156
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
refactor: Make explicit CMutableTransaction -> CTransaction conversion. #14156
Conversation
Concept ACK |
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.
utACK e4c33899e12bde83b92026a2f310333d76d87939
Agree that for these cases an explicit constructor is preferable. utACK e4c3389. |
For most cases the performance should not matter (e.g. unit tests or benches where the conversion is not in a hot path. Also, for the rpc interface or tx-tools, where the overhead of communication dwarfs the speedup of skipping the conversion). For other cases, we might want to accept either a mutable or immuatable tx. See e.g. #13359 or #13309 |
@MarcoFalke I agree with you that in most cases it won't make a difference. But I think having it implicit makes it harder to reason about the cases when it's important. |
I think the only case where it could potentially matter is But agree that it makes sense to mark it explicit, so that the other cases are easily visible. Though, might still want to submit the other prs first that do the cleanup you had planned afterward. This way we avoid having to touch the lines twice. If you wanted to have this merged first, at the very least, you'd have to reorder the two commits, since the first one doesn't compile on its own. |
@MarcoFalke I'll take a loot at I was just thinking, other changes that are just clean-up might make more sense to go as commits in this PR, instead of new PRs, since they wouldn't make that much sense to get merged if this one doesn't get merged in the end. (this doesn't apply to wallet.cpp). I'll mark this PR as WIP meanwhile. |
Fixed commit order. |
Rebased. |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. 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'm afraid this PR doesn't compile when rebase on current |
That's because #14588 introduced an unnecessary implicit conversion from That's precisely the kind of mistake this PR is intended to prevent. Added commit 4f9a39b8720ba8703962341474ff74bc4be5ff48 fixing it. |
src/bench/mempool_eviction.cpp
Outdated
@@ -127,7 +127,7 @@ static void MempoolEviction(benchmark::State& state) | |||
AddTx(tx6_r, 1100LL, pool); | |||
AddTx(tx7_r, 9000LL, pool); | |||
pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); | |||
pool.TrimToSize(GetVirtualTransactionSize(tx1)); | |||
pool.TrimToSize(GetVirtualTransactionSize(CTransaction(tx1))); |
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.
Should probably move out of the hot loop (see comment above) and submit as separate pull request, since it changes the benchmark
src/test/txvalidationcache_tests.cpp
Outdated
|
||
std::vector<CScriptCheck> scriptchecks; | ||
// Make sure this transaction was not cached (ie because the first | ||
// input was valid) | ||
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks)); | ||
BOOST_CHECK(CheckInputs(CTransaction(tx), state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks)); |
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.
Could submit all changes in src/test and src/wallet/test as separate pull request?
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.
Sure. So I understand you'd like both tests and benchmark changes in a separate PR, right?
@MarcoFalke github seems to have got lost with my latest force push, and automatically closed this PR. Should I open another one? |
@lucash-dev That's fine. Just point back to this PR for reference. |
Reincarnated it as #14906 |
There are now two PRs (as per @MarcoFalke's request): #14908 - Removes implicit construction of CTransaction from tests and benchmarks. |
…tion conversion. b301950 Made expicit constructor CTransaction(const CMutableTransaction &tx). (lucash-dev) faf29dd Minimal changes to comply with explicit CMutableTransaction -> CTranaction conversion. (lucash-dev) Pull request description: This PR is re-submission of #14156, which was automatically closed by github (glitch?) Original description: This PR makes explicit the now implicit conversion constructor `CTransaction(const CMutableTransaction&)` in `transaction.h`. Minimal changes were made elsewhere to make the code compilable. I'll follow up with other PRs to address individually refactoring functions that should have a `CMutableTransaction` version, or where a `CTransaction` should be reused. The rationale for this change is: - Conversion constructors should not be explicit unless there's a strong reason for it (in the opinion of, for example, https://google.github.io/styleguide/cppguide.html, and https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ro-conversion. Let me know your take on this). - This particular conversion is very costly -- it implies a serialization plus hash of the transaction. - Even though `CTransaction` and `CMutableTransaction` represent the same data, they have very different use cases and performance properties. - Making it explicit allows for easier reasoning of performance trade-offs. - There has been previous performance issues caused by unneeded use of this implicit conversion. - This PR creates a map for places to look for possible refactoring and performance gains (this benefit still holds if the PR is not merged). Tree-SHA512: 2427462e7211b5ffc7299dae17339d27f8c43266e0895690fda49a83c72751bd2489d4471b3993075a18f3fef25d741243e5010b2f49aeef4a9688b30b6d0631
This PR makes explicit the now implicit conversion constructor
CTransaction(const CMutableTransaction&)
intransaction.h
.Minimal changes were made elsewhere to make the code compilable. I'll follow up with other PRs to address individually refactoring functions that should have a
CMutableTransaction
version, or where aCTransaction
should be reused.The rationale for this change is:
CTransaction
andCMutableTransaction
represent the same data, they have very different use cases and performance properties.