mega-system-contracts 1.7.0

System contracts for the MegaETH EVM
Documentation
//! Self-consistency checks for the pre-generated bytecode constants.
//!
//! The build script (`build.rs`) attests `src/generated/` against the Solidity source and the
//! versioned artifacts — but only when those inputs are available (i.e. a repository build with
//! Foundry). When building from a published crate, the source, scripts, and artifacts are excluded
//! from the package, so that attestation is skipped.
//!
//! These tests run via `cargo test` in *any* context, including a published crate, and pin the
//! one invariant that must hold regardless of build provenance: for every publicly exported
//! `*_CODE` / `*_CODE_HASH` pair, `keccak256(CODE) == CODE_HASH`. This catches an out-of-sync or
//! tampered generated file even when the build-time source-attestation cannot run.

use mega_system_contracts::alloy_primitives::keccak256;

/// Asserts that `keccak256(<CODE>) == <CODE_HASH>` for each `(code, code_hash)` pair, naming the
/// constant in any failure message.
macro_rules! assert_code_hashes {
    ( $( ($code:path, $code_hash:path) ),+ $(,)? ) => {{
        $(
            assert_eq!(
                keccak256($code.as_ref()),
                $code_hash,
                concat!(
                    "self-consistency: keccak256(",
                    stringify!($code),
                    ") != ",
                    stringify!($code_hash)
                )
            );
        )+
    }};
}

#[test]
fn test_oracle_constants_are_self_consistent() {
    use mega_system_contracts::oracle::*;
    assert_code_hashes!(
        (LATEST_CODE, LATEST_CODE_HASH),
        (V1_0_0_CODE, V1_0_0_CODE_HASH),
        (V1_1_0_CODE, V1_1_0_CODE_HASH),
        (V2_0_0_CODE, V2_0_0_CODE_HASH),
    );
}

#[test]
fn test_keyless_deploy_constants_are_self_consistent() {
    use mega_system_contracts::keyless_deploy::*;
    assert_code_hashes!((LATEST_CODE, LATEST_CODE_HASH), (V1_0_0_CODE, V1_0_0_CODE_HASH));
}

#[test]
fn test_access_control_constants_are_self_consistent() {
    use mega_system_contracts::access_control::*;
    assert_code_hashes!((LATEST_CODE, LATEST_CODE_HASH), (V1_0_0_CODE, V1_0_0_CODE_HASH));
}

#[test]
fn test_limit_control_constants_are_self_consistent() {
    use mega_system_contracts::limit_control::*;
    assert_code_hashes!((LATEST_CODE, LATEST_CODE_HASH), (V1_0_0_CODE, V1_0_0_CODE_HASH));
}

#[test]
fn test_sequencer_registry_constants_are_self_consistent() {
    use mega_system_contracts::sequencer_registry::*;
    assert_code_hashes!((LATEST_CODE, LATEST_CODE_HASH), (V1_0_0_CODE, V1_0_0_CODE_HASH));
}