Repository navigation
Encapsulation for CTransaction - #35569
purpleKarrot wants to merge 7 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy 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. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-09-24 18:29:12 |
Can you put all the parts relevant to reviewers into the PR description? |
|
🚧 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. |
4541201 to
853db19
Compare
should be The linter fails validating the |
This doesn't seem like a good idea at all to me. If you want an object that can represent different transactions over its lifetime, you should be using Note that the comment ("CTransaction is not actually immutable; deserialization and assignment are implemented, and bypass the constness.") was made incorrect by #8580 in 2016. |
I do not think you can do this, as far as I can tell the linter container image does not contain cmake. Drop the scripted diff validation ? |
853db19 to
de03c01
Compare
Just to make sure there is no misunderstanding here. All builtin types in C++ as well as nearly all types in the standard library have regular semantics. You are saying that is a bad design, right?
So instead, the mempool uses |
de03c01 to
18b0efc
Compare
|
Concept ACK |
No, I said exactly what I intended to say: making
Passing a CTransactionRef around prevents the recipient from modifying it (ignoring const casts), but doesn't prevent the creator from doing so. std::shared_ptr<CTransaction> foo;
vRecv >> TX_WITH_WITNESS(foo);
ProcessTransaction(foo); // add to mempool
foo->version = 42; // mempool copy is automatically updated, how neat! |
|
Concept ACK Moving the data members behind observers, vs having the class layout define the API seems a solid improvement.
Porque no los dos? Turns out, afaict, there is a C++ idiom for this: handle/body idiom with shared immutable representation! What I mean by this is we want to make the exposed transaction type a regular value handle to immutable transaction data1. Concretely, something like this: class CTransaction {
public:
CTransaction(const CTransaction&) = default;
CTransaction(CTransaction&&) noexcept = default;
CTransaction& operator=(const CTransaction&) = default;
CTransaction& operator=(CTransaction&&) noexcept = default;
// the observers added in this PR
...
private:
std::shared_ptr<const TransactionData> m_data;
};
auto a = tx1;
auto b = a;
a = tx2; // b still observes tx1That is different from mutating a shared transaction object: auto p = std::shared_ptr<CTransaction>{...};
ProcessTransaction(p);
*p = tx2; // bad: existing users may observe changed transaction contentsWhile we still use Based on my understanding of the linked blogs, this seems in line with their stated goals. We can get rid of the "class layout is the API," eventually allowing serialisation/deserialisation to move behind an explicit construction/codec boundary as described in the blogs. It also plays nice with the vocabulary type direction: the eventual public transaction type can be regular and idiomatic, while preserving the invariant of once a transaction contents are published/shared, they are immutable. Footnotes |
|
@josibake, exactly. As highlighted in my blog post, the next refactoring step would be moving the
for what benefit? It is pointing to a |
Perform an automated replacement of all direct accesses of `CTransaction`'s data members, each with it's associated observer function. Produce the changeset with the following commands: ```sh export CC="$(which clang)" export CXX="$(which clang++)" cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_GUI=ON -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON cmake -B build/tidy -S contrib/devtools/bitcoin-tidy cmake --build build cmake --build build/tidy run-clang-tidy -p build -load='build/tidy/libbitcoin-tidy.so' -checks='-*,bitcoin-use-observers' -fix ```
Now that `CTransaction`'s data members are no longer accessed outside of member functions, they can be made `private`.
4c7f4ae to
5bffd2c
Compare
|
Pushed to resolve merge conflicts and updated the description. Removed all mentions of |
|
Somewhat related, in #36521 I change the representation of I've experimented with extending this approach to |
|
@sipa, there are hundreds of possibilities of changing the in-memory representation of these types. Keeping But instead of coming up with a "better" representation, I think we should focus on the change that will allow changing the representation independently of all user code. This is the benefit that I see in encapsulation per se. With encapsulation in place, all changes to the representation are internal. This allows us to experiment with different data layouts and gives us an easy way to compare different implementations. Introducing encapsulation is expected to touch all client code. It is a necessary precondition to make future changes to the client code unnecessary. |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
It's not clear to me to what extent getting rid of the distinction between Significant memory usage and performance gains are possible by exploiting packed in-memory representations. That included keeping witnesses in serialized form as my PR does, and experimenting shows that even more is possible by packing everything at a transaction level. When taken far enough, such representations are incompatible with easy and efficient modifications. But modifications are very localized in wallet/signing logic, while most of the codebase only needs immutable transactions. Because of that, I think it's reasonable to keep them separate in representation, and possibly also in interface. I'm aware further abstraction is possible, where we have for example one or multiple "transaction view" objects, which can be backed by independent allocation pool objects (which could be per transaction, or even per block), some of which could be optimized for modification and some aren't. I think that's a step too far, as the gains are minimal beyond a specialized per- |
@sipa, my goal is not getting rid of the distinction, my goal is getting rid of I have shown in #36692 that writing unit tests in a way that does not rely on In #36696 (comment), I show concrete examples how |
I think there are two views, which are both subjectively valid and don't come to an agreement:
|

CTransactioncurrently exposes its data members publicly. This makes the implementation details of the type part of its interface.This PR encapsulates the data members behind public observer functions. The existing users of
CTransactionare migrated to the observers, after which the data members are made private.The refactoring is deliberately split into three steps:
The migration is automated with a new
bitcoin-tidyuse-observerscheck. This both makes the large mechanical change straightforward and provides a reusable tool for similar refactorings.The split into separate commits allows reviewers to review handwritten changes independently of the automated changes. It also simplifies rebasing: if the mechanical migration conflicts, individual files can be reset to their new base versions and the migration tool can be run on them again. This works because the old interface remains available until the final commit, so the reset code is still valid while the mechanical migration is reapplied.
This PR does not change the semantics of
CTransaction. In particular, regular value semantics are intentionally left for a subsequent change.Why Encapsulation? Why
CTransaction?Public data members make the representation of a type part of its API. Once access goes through observers, the representation can change without requiring its users to change as well. Potential changes are using shared immutable transaction state, or even an offset into shared immutable block state.
This establishes a clean boundary between the transaction's data and functionality that operates on it. In particular, it allows serialization/deserialization and other functionality to be separated from the data layout itself in subsequent refactorings.
This work is part of the broader interface-segregation direction described in #35904, but is not a prerequisite for the stateless validation library proposed there. The encapsulation has value independently of that work.
CTransactionis a good type to start this migration with: it is widely used, its interface is relatively simple, and its data members are already effectively immutable. This allows the migration to establish the pattern and tooling without first requiring changes to the semantics of the type.