Skip to content

refactor(toolkit): give each ledger version its own copy of the builders - #2075

Open
ozgb wants to merge 2 commits into
ozgb-split-ledger-helpersfrom
ozgb-split-toolkit-builders
Open

refactor(toolkit): give each ledger version its own copy of the builders#2075
ozgb wants to merge 2 commits into
ozgb-split-ledger-helpersfrom
ozgb-split-toolkit-builders

Conversation

@ozgb

@ozgb ozgb commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Overview

Last of three PRs closing #1768: directories compiled twice via Rust's
#[path = "…"] attribute, once bound to the ledger-8 crates and once to the
ledger-9 crates, so that super:: resolves differently in each instantiation.
A reader of such a file cannot tell which ledger crate an alias refers to, and
an edit meant for one generation silently applies to both. #2059 fixed
ledger/src/, the previous PR in the stack fixed ledger/helpers/src/, and
this one fixes the two remaining occurrences in the toolkit.

Both used the same trick:

#[path = "common"]
#[allow(clippy::duplicate_mod)]
pub mod inner {
    pub use midnight_node_ledger_helpers::ledger_8 as ledger_helpers_local;
    pub mod batch_single_tx; mod batches;pub mod type_convert;
}
pub use inner::*;

so builders/common/ (16 files, 3328 lines) and commands/fork/common/
(8 files, 374 lines) were each compiled twice. Each version now has its own
directory — builders/{ledger_8,ledger_9}/ and
commands/fork/{ledger_8,ledger_9}/ — and each file names its own version with
one use midnight_node_ledger_helpers::ledger_N as ledger_helpers_local; line,
so bodies stay byte-identical between the two copies.

util/toolkit/src/commands/common/ is a different, single-compiled module and
is left alone.

This is not a move toward a generic <L: Ledger> builder. rustc already
emitted both instantiations; this only makes the existing duplication visible so
diff -r can police it. In particular type_convert.rs's identity conversions
on the v9 side stay exactly as they are.

Notes for review:

  • The inner wrapper module disappears. It is referenced nowhere outside these
    four files, so every external path (builders::ledger_8::SingleTxBuilder,
    fork::ledger_9::show_wallet, …) and the whole CLI surface are unchanged.
  • impl_encoded_zswap_conversions! stays in builders/mod.rs. Its comment
    needed one phrase updated, but the reason still holds: ledger_storage
    aliases to ledger_storage_ledger_8 in both versions, so some types are
    shared and duplicate impls would still conflict with E0119.
  • remote_prover.rs (impl …ledger_8::ProofProvider) is outside the duplicated
    tree and is untouched.

With this merged, grep -rn '#\[path' --include='*.rs' over the workspace
returns nothing and no directory is compiled more than once — #1768 is done.

🗹 TODO before merging

  • Ready
  • Merges after the ledger-helpers PR it is based on.

📌 Submission Checklist

  • All commits are signed off (git commit -s) for the DCO
  • Changes are backward-compatible (or flagged if breaking)
  • Pull request description explains why the change is needed
  • Self-reviewed the diff
  • I have included a change file, or skipped for this reason:
  • If the changes introduce a new feature, I have bumped the node minor version
  • Update documentation (if relevant)
  • Updated AGENTS.md if build commands, architecture, or workflows changed
  • No new todos introduced

🧪 Testing Evidence

$ cargo test -p midnight-node-toolkit
test result: ok. 135 passed; 0 failed; 1 ignored

(The Docker-based hardfork_e2e integration test fails locally against a stale
pre-#2059 node image — expected header tag 'midnight:ledger-state[v18]:', got '…[v13]:' from the container's own genesis load, before any toolkit code runs.
CI builds that image from the branch.)

The layout check the split buys — the two copies must be identical apart from
the version name:

$ diff -r builders/ledger_8 builders/ledger_9      # normalised: s/ledger_[89]/ledger_N/
DIFFERS: mod.rs                                     # the per-version serialize_tx
$ diff -r commands/fork/ledger_8 commands/fork/ledger_9
(identical)

And the surface is unchanged:

$ cargo run -p midnight-node-toolkit -- --help      # same commands as before
$ grep -rn '#\[path' --include='*.rs' ledger util node pallets primitives runtime   # no hits
$ grep -rn 'duplicate_mod' --include='*.rs' .                                       # no hits

Also run: cargo fmt --all --check, cargo clippy -p midnight-node-toolkit --all-targets -- -D warnings, cargo check --workspace --all-targets,
cargo build --release.

  • Additional tests are provided (if possible)

🔱 Fork Strategy

  • N/A — toolkit only, no runtime or client behaviour change.

Links

@ozgb
ozgb requested a review from a team as a code owner August 28, 2026 11:25
@ozgb ozgb added the bot:ai-assisted Authored or substantially edited by an AI agent label Aug 28, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ad796549f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -1,6 +1,7 @@
use super::ledger_helpers_local::{self, DefaultDB, FinalizedTransaction, mn_ledger_serialize};
use crate::commands::contract_address::{ContractAddressBoth, ContractAddressError};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the required license headers to new Rust files

Add LICENSE_HEADER.txt to this newly introduced source file and the other headerless additions: seven command files under each ledger version, plus contract_custom.rs, register_dust_address.rs, and transactions.rs under each builders version. All 20 are added as new .rs paths in this commit but begin directly with imports, contrary to the repository requirement for every new source file.

AGENTS.md reference: AGENTS.md:L239-L241

Useful? React with 👍 / 👎.

@ozgb
ozgb force-pushed the ozgb-split-toolkit-builders branch from 30b23fd to 86f1a8f Compare September 1, 2026 12:12
@datadog-official

This comment has been minimized.

ozgb added 2 commits September 1, 2026 13:49
`tx_generator/builder/builders/common/**` and `commands/fork/common/**` were
each compiled twice via a `#[path = "common"] pub mod inner { … }` trick, once
bound to the ledger-8 helpers and once to the ledger-9 helpers, with
`ledger_helpers_local` resolving differently in each instantiation. A reader of
a file under `common/` could not tell which ledger version it was looking at,
and an edit meant for one version silently applied to both. This finishes the
job #2059 and the ledger-helpers split started.

Each version now has its own directory — `builders/{ledger_8,ledger_9}/` and
`commands/fork/{ledger_8,ledger_9}/` — and each file names its own version with
a `use midnight_node_ledger_helpers::ledger_N as ledger_helpers_local;` line, so
the two copies stay byte-identical apart from that one word and `diff -r` can
police them. The only genuine divergence left is `builders/*/mod.rs`, which
carries the per-version `serialize_tx`.

The `inner` wrapper module is gone (nothing referenced it), so every external
path — `builders::ledger_8::SingleTxBuilder`, `fork::ledger_9::show_wallet`, … —
is unchanged, as is the CLI surface.
`impl_encoded_zswap_conversions!` stays in `builders/mod.rs`: `ledger_storage`
still aliases to `ledger_storage_ledger_8` in both versions, so duplicating the
impls per version would still hit E0119.

With this, `grep -r '#\[path' --include='*.rs'` over the workspace returns
nothing, and no directory is compiled more than once.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Oscar Bailey <79094698+ozgb@users.noreply.github.com>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Oscar Bailey <79094698+ozgb@users.noreply.github.com>
@ozgb
ozgb force-pushed the ozgb-split-toolkit-builders branch from 86f1a8f to 9f11c7b Compare September 1, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:ai-assisted Authored or substantially edited by an AI agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove shared common modules to isolate ledger-version behavior

1 participant