Skip to content

fix: harden system-contract bytecode and storage-layout verification#316

Open
RealiCZ wants to merge 3 commits into
mainfrom
cz/fix/v12-build-verification
Open

fix: harden system-contract bytecode and storage-layout verification#316
RealiCZ wants to merge 3 commits into
mainfrom
cz/fix/v12-build-verification

Conversation

@RealiCZ

@RealiCZ RealiCZ commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hardens the build-time verification of the embedded system-contract bytecode and storage-layout constants, and corrects a misleading doc comment. No runtime or consensus behavior changes — the diff is confined to build.rs, tests, a dev-dependency, and documentation.

  • Attest the *-latest.json bytecode alias. Versioned artifacts are self-hash-validated when collected, but the *-latest.json alias was loaded and trusted without any check. It is now verified to (a) be internally consistent (keccak256(deployed_bytecode) == code_hash) and (b) match one of the attested versioned artifacts on both version and code hash. The generated LATEST_CODE aliases V{version}_CODE, so binding version+hash prevents a latest alias whose bytecode is one version but whose version field says another from pointing at the wrong versioned constant.
  • Track generated constants for rebuilds. build.rs now emits cargo::rerun-if-changed for each src/generated/*.rs, so editing a generated constant re-runs verification instead of compiling unverified.
  • Don't silently skip attestation in a published crate. When the Solidity source / Foundry / scripts/ are unavailable (e.g. building from a published crate), the build emits a cargo::warning making the skipped source-attestation explicit rather than silently trusting the checked-in constants.
  • Self-consistency test (runs everywhere). A new test asserts keccak256(CODE) == CODE_HASH for every exported bytecode constant. Unlike the build-time source-attestation, it runs via cargo test in any context — including a published crate — catching an out-of-sync or tampered generated file.
  • Storage-layout guard for SequencerRegistry. The hand-maintained Rust storage-slot constants had no automated check against the Solidity layout (the existing Solidity test is tautological). A new test asserts each constant matches forge inspect SequencerRegistry storageLayout, catching a field reorder or a missed constant update. It skips gracefully when Foundry or the Solidity source aren't present.
  • Doc fix. Corrected the is_enabled doc comment, which described the version-gating direction backwards (the current spec enables every version at or below it).

Test plan

  • New tests/generated_self_consistency.rskeccak256(CODE) == CODE_HASH for all exported constants across the 5 contracts.
  • New tests/sequencer_registry_storage_layout.rs — Rust slot constants vs forge inspect storage layout, with a field-count guard.
  • New test functions use the repository's test_ naming convention.
  • cargo test -p mega-system-contracts and the full mega-evm suite pass; cargo sort --check, cargo fmt --check, and RUSTFLAGS="-D warnings" cargo clippy are clean.

…gating doc comment

- Attest the latest-bytecode alias at build time: verify its own keccak256(bytecode)==code_hash and that it matches a versioned artifact on both version and code hash (the generated LATEST_CODE aliases V{version}_CODE, so matching on hash alone could point the alias at the wrong version).
- Track the generated constant files with cargo::rerun-if-changed so editing one re-runs build-time verification.
- Emit a build warning when source attestation cannot run (no Foundry/Solidity source, e.g. a published crate) instead of silently trusting the generated constants; add a test asserting keccak256(code)==code_hash for every generated constant, which runs in any context.
- Add a test asserting the SequencerRegistry Rust storage-slot constants match Foundry's reported storage layout; it skips gracefully when Foundry or the Solidity source/config are unavailable (forge-less env or packaged crate).
- Order the Cargo.toml dependency sections so the sort check passes.
- Correct an inverted doc comment on the spec version-gating check so it matches the code (a version at or below the current spec is enabled).
@RealiCZ RealiCZ added spec:unchanged No change to any `mega-evm`'s behavior api:unchanged No change to the public interface or API dependencies Pull requests that update a dependency file rust Pull requests that update rust code agent Generated by AI agents comp:core Changes to the `mega-evm` core crate comp:misc Changes to the miscellaneous part of this repo spec:stable Touches stable spec code — must not change behavior labels Jun 16, 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: 433334e71a

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/system-contracts/tests/generated_self_consistency.rs Outdated
@claude

claude Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review: LGTM with one minor note

The PR is well-structured and closes a real gap: the *-latest.json alias was trusted without verification, and the generated constants had no runtime self-consistency check in non-repo builds. The chain of trust (versioned artifacts self-hash → latest alias cross-checked on both version + code hash → generated constants verified) is sound, and the explanation in each comment is clear.

Specific things done well:

  • attest_latest_alias checks both self-consistency and version+hash binding, with the comment explaining why hash-only is insufficient.
  • The rerun-if-changed for generated files is a correct addition — editing a constant would otherwise skip re-verification.
  • The published-crate warning makes the skipped attestation explicit rather than silent.
  • The field-count guard in the storage-layout test catches added/removed fields even for the dynamic-array history slots that have no Rust constant.
  • cfg_attr(not(test), warn(unused_crate_dependencies)) is the right fix for the dev-dependency lint.
  • Doc fix in spec.rs is accurate — the old comment had the direction backwards.

One small note left inline.

Comment thread crates/system-contracts/tests/sequencer_registry_storage_layout.rs Outdated
@claude

claude Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LGTM. Prior feedback addressed; no new issues in the latest push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent Generated by AI agents api:unchanged No change to the public interface or API comp:core Changes to the `mega-evm` core crate comp:misc Changes to the miscellaneous part of this repo dependencies Pull requests that update a dependency file rust Pull requests that update rust code spec:stable Touches stable spec code — must not change behavior spec:unchanged No change to any `mega-evm`'s behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants