Skip to content

primitives: encapsulate CTransaction fields - #212

Draft
l0rinc wants to merge 8 commits into
masterfrom
l0rinc/encapsulate-ctransaction-fields
Draft

l0rinc wants to merge 8 commits into
masterfrom
l0rinc/encapsulate-ctransaction-fields

Conversation

@l0rinc

@l0rinc l0rinc commented Jul 6, 2026 •

Copy link
Copy Markdown
Owner

Problem: CTransaction exposes vin, vout, version, and nLockTime as public fields, so callers depend on the layout of the immutable transaction object.
bitcoin#35569 solves this with GetInputs() / GetOutputs() style (non-reusable) observers that migrate everything (including CMutableTransaction) all at once.

Fix: This stack first routes generic transaction reads through small tx_detail overloads, so CTransaction and CMutableTransaction no longer need matching public member names.
It then adds a parameterized bitcoin-field-observers tidy helper and uses four one-field scripted-diff commits to rename CTransaction storage to m_..., add protocol-name observers (vin(), vout(), version(), and nLockTime()), and rewrite external reads mechanically.
The final commit moves the renamed const storage private and updates comments.
CMutableTransaction remains the mutable construction type with public mutable fields.

@l0rinc
l0rinc force-pushed the l0rinc/encapsulate-ctransaction-fields branch 7 times, most recently from 719dcf1 to a3bfcf3 Compare July 7, 2026 20:20
l0rinc and others added 3 commits July 8, 2026 13:46
Route generic transaction code through `tx_detail` overload helpers before changing any `CTransaction` fields.
The helpers delegate to the public field on `CMutableTransaction` and to the matching field or observer on `CTransaction`.

This removes the incidental dependency on both transaction types sharing public member names, so later commits can leave `CMutableTransaction` unchanged while migrating only the immutable transaction side.
At this point the `CTransaction` overload bodies still contain direct field access, which lets the later field-observer scripted diffs update those overloads mechanically.

Co-authored-by: Daniel Pfeifer <[email protected]>
Spell policy and RPC transaction-version constants as `uint32_t` before `CTransaction::version` stops being public.
These values are protocol version numbers, so the field migration commits do not need to carry this cleanup.

Co-authored-by: Daniel Pfeifer <[email protected]>
Add a parameterized `bitcoin-tidy` check that rewrites reads of a configured qualified field to the observer call derived from a configured observer declaration, renames the configured storage field with the `m_` prefix, and inserts the observer declaration after the renamed field declaration.

The helper is option-driven rather than tied to `CTransaction` because the following scripted diffs run it once per `CTransaction` field.
When no field-observer options are provided, the check registers no matchers so regular `clang-tidy` runs can load it through `bitcoin-*` without requiring scripted-diff configuration.
Partial or malformed check options produce `clang-tidy` configuration diagnostics instead of silently registering no matchers.
Keep the bitcoin-tidy self-test scoped to `bitcoin-nontrivial-threadlocal`, because this check is option-driven and only runs when a scripted diff provides its field and observer options.

Install the lint-environment build, ccache, and clang-tidy packages needed by scripted diffs that run this helper, and raise the lint timeout so CI can host those verifier runs.
The verify scripts still perform their own configure/build steps, so the lint checker does not need custom setup logic.

Co-authored-by: Daniel Pfeifer <[email protected]>
@l0rinc
l0rinc force-pushed the l0rinc/encapsulate-ctransaction-fields branch from a3bfcf3 to 632286a Compare July 8, 2026 20:47
@l0rinc l0rinc closed this Jul 8, 2026
@l0rinc l0rinc reopened this Jul 8, 2026
l0rinc and others added 5 commits July 8, 2026 14:03
Run `bitcoin-field-observers` for `::CTransaction::nLockTime` so external reads use the observer, the storage field is renamed with the `m_` prefix, and the observer is added.
`CMutableTransaction::nLockTime` stays public.

-BEGIN VERIFY SCRIPT-
export CC=clang
export CXX=clang++
rm -rf /tmp/bitcoin-field-observers-build /tmp/bitcoin-field-observers-tidy
cmake -B /tmp/bitcoin-field-observers-build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_GUI=ON -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON -DENABLE_IPC=OFF -DWITH_QRENCODE=OFF
cmake --build /tmp/bitcoin-field-observers-build --parallel "$(nproc)" && cmake -B /tmp/bitcoin-field-observers-tidy -S contrib/devtools/bitcoin-tidy && cmake --build /tmp/bitcoin-field-observers-tidy --parallel "$(nproc)"
run-clang-tidy -j "$(nproc)" -p=/tmp/bitcoin-field-observers-build -load=/tmp/bitcoin-field-observers-tidy/libbitcoin-tidy.so -checks='-*,bitcoin-field-observers' \
  -config='{
    CheckOptions: {
      bitcoin-field-observers.ReplacementMainFile: "src/primitives/transaction.cpp",
      bitcoin-field-observers.QualifiedFieldName: "::CTransaction::nLockTime",
      bitcoin-field-observers.AccessorDeclaration: "uint32_t nLockTime() const { return m_nLockTime; }"
    }
  }' -header-filter='.*' -fix
-END VERIFY SCRIPT-

Co-authored-by: Daniel Pfeifer <[email protected]>
Run `bitcoin-field-observers` for `::CTransaction::version` so external reads use the observer, the storage field is renamed with the `m_` prefix, and the observer is added.
`CMutableTransaction::version` stays public.

-BEGIN VERIFY SCRIPT-
export CC=clang
export CXX=clang++
rm -rf /tmp/bitcoin-field-observers-build /tmp/bitcoin-field-observers-tidy
cmake -B /tmp/bitcoin-field-observers-build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_GUI=ON -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON -DENABLE_IPC=OFF -DWITH_QRENCODE=OFF
cmake --build /tmp/bitcoin-field-observers-build --parallel "$(nproc)" && cmake -B /tmp/bitcoin-field-observers-tidy -S contrib/devtools/bitcoin-tidy && cmake --build /tmp/bitcoin-field-observers-tidy --parallel "$(nproc)"
run-clang-tidy -j "$(nproc)" -p=/tmp/bitcoin-field-observers-build -load=/tmp/bitcoin-field-observers-tidy/libbitcoin-tidy.so -checks='-*,bitcoin-field-observers' \
  -config='{
    CheckOptions: {
      bitcoin-field-observers.ReplacementMainFile: "src/primitives/transaction.cpp",
      bitcoin-field-observers.QualifiedFieldName: "::CTransaction::version",
      bitcoin-field-observers.AccessorDeclaration: "uint32_t version() const { return m_version; }"
    }
  }' -header-filter='.*' -fix
-END VERIFY SCRIPT-

Co-authored-by: Daniel Pfeifer <[email protected]>
Run `bitcoin-field-observers` for `::CTransaction::vout` so external reads use the observer, the storage field is renamed with the `m_` prefix, and the observer is added.
`CMutableTransaction::vout` stays public.

-BEGIN VERIFY SCRIPT-
export CC=clang
export CXX=clang++
rm -rf /tmp/bitcoin-field-observers-build /tmp/bitcoin-field-observers-tidy
cmake -B /tmp/bitcoin-field-observers-build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_GUI=ON -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON -DENABLE_IPC=OFF -DWITH_QRENCODE=OFF
cmake --build /tmp/bitcoin-field-observers-build --parallel "$(nproc)" && cmake -B /tmp/bitcoin-field-observers-tidy -S contrib/devtools/bitcoin-tidy && cmake --build /tmp/bitcoin-field-observers-tidy --parallel "$(nproc)"
run-clang-tidy -j "$(nproc)" -p=/tmp/bitcoin-field-observers-build -load=/tmp/bitcoin-field-observers-tidy/libbitcoin-tidy.so -checks='-*,bitcoin-field-observers' \
  -config='{
    CheckOptions: {
      bitcoin-field-observers.ReplacementMainFile: "src/primitives/transaction.cpp",
      bitcoin-field-observers.QualifiedFieldName: "::CTransaction::vout",
      bitcoin-field-observers.AccessorDeclaration: "const std::vector<CTxOut>& vout() const LIFETIMEBOUND { return m_vout; }"
    }
  }' -header-filter='.*' -fix
-END VERIFY SCRIPT-

Co-authored-by: Daniel Pfeifer <[email protected]>
Run `bitcoin-field-observers` for `::CTransaction::vin` so external reads use the observer, the storage field is renamed with the `m_` prefix, and the observer is added.
`CMutableTransaction::vin` stays public.

-BEGIN VERIFY SCRIPT-
export CC=clang
export CXX=clang++
rm -rf /tmp/bitcoin-field-observers-build /tmp/bitcoin-field-observers-tidy
cmake -B /tmp/bitcoin-field-observers-build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_GUI=ON -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON -DENABLE_IPC=OFF -DWITH_QRENCODE=OFF
cmake --build /tmp/bitcoin-field-observers-build --parallel "$(nproc)" && cmake -B /tmp/bitcoin-field-observers-tidy -S contrib/devtools/bitcoin-tidy && cmake --build /tmp/bitcoin-field-observers-tidy --parallel "$(nproc)"
run-clang-tidy -j "$(nproc)" -p=/tmp/bitcoin-field-observers-build -load=/tmp/bitcoin-field-observers-tidy/libbitcoin-tidy.so -checks='-*,bitcoin-field-observers' \
  -config='{
    CheckOptions: {
      bitcoin-field-observers.ReplacementMainFile: "src/primitives/transaction.cpp",
      bitcoin-field-observers.QualifiedFieldName: "::CTransaction::vin",
      bitcoin-field-observers.AccessorDeclaration: "const std::vector<CTxIn>& vin() const LIFETIMEBOUND { return m_vin; }"
    }
  }' -header-filter='.*' -fix
-END VERIFY SCRIPT-

Co-authored-by: Daniel Pfeifer <[email protected]>
Move the renamed `CTransaction` storage members behind the class boundary once all external reads have been migrated.

The previous commits keep the transaction data `const` and add only read-only observers.
This commit preserves that shape: it changes visibility and declaration placement, declares copy construction explicitly, deletes copy assignment, and leaves `CMutableTransaction` as the mutable construction type.
The declared copy constructor keeps `CTransaction` without an implicit move constructor while still allowing containers to copy from rvalues when required.

It also updates comments that still referenced the old public member spelling.

Co-authored-by: Daniel Pfeifer <[email protected]>
@l0rinc
l0rinc force-pushed the l0rinc/encapsulate-ctransaction-fields branch from 632286a to f926510 Compare July 8, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant