# Multi-chain EVMs

Guillotine's multi-chain framework (`src/chains/`, published as `guillotine.module("chains")` via `src/chains_package.zig`) lets you run non-Ethereum EVM networks with exact chain consensus rules and comptime-specialized `EvmConfig` profiles—zero runtime branching and zero unused L1 machinery.

## Importing the `chains` module

In a downstream Zig project's `build.zig`:

```zig
const guillotine = b.dependency("guillotine", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("evm", guillotine.module("evm"));
exe.root_module.addImport("chains", guillotine.module("chains"));
exe.root_module.addImport("voltaire", guillotine.module("voltaire"));
```

`evm.chains` exposes pure comptime consensus modules (`chain_spec`, `registry`, `tx_types`, `polygon`). The `chains` package module additionally exports `evm_profiles` (`buildEvmConfig`, `EvmFor`), `host_common.ChainHost`, and concrete chain hosts such as `PolygonHost`.

## Core building blocks

| Component | File | Role |
| --- | --- | --- |
| **`ChainSpec` & `ChainId`** | `src/chains/chain_spec.zig`, `registry.zig` | Comptime chain descriptors (`ChainFamily`, `BaseFeeSink`, `GasAccountingModel`, EIP overrides, bytecode/initcode limits, disabled precompiles) and EIP-155 chain ID registry. |
| **7 comptime `EvmProfiles`** | `src/chains/evm_profiles.zig` | `portable`, `optimized`, `keccak_cached`, `jit_balanced` (Ski-Rental), `jit_aggressive`, `tier1_only`, and `no_fusion`. |
| **`ChainHost`** | `src/chains/host_common.zig` | Reusable real-EVM transaction host with O(1) `Database` snapshots, `BaseFeeSink` (`.burn`, `.collector`, `.sponsored_zero`), EIP-2930/7702 intrinsic gas & authorization lists, EIP-7623 calldata floors, EIP-7825 gas caps, EIP-161 pruning, and zero-gas system calls. |
| **Core EVM hooks** | `src/evm_config.zig`, `src/frame/frame_config.zig` | `EvmConfig.disabled_precompiles` (zero overhead when empty) and `FrameConfig.max_bytecode_size` ceiling raised to **256 KiB** (supporting Polygon 32 KiB and Monad 128 KiB), wired into EIP-170 and EIP-3860 enforcement. |

## Reference chain: Polygon PoS (`137` / `80002`)

Polygon PoS (`src/chains/polygon/`) is Guillotine's reference multi-chain implementation, pinned directly to [`0xPolygon/bor`](https://github.com/0xPolygon/bor) across **18 Bor hardforks** (`London` through `Salvador`) for Mainnet (`137`) and Amoy (`80002`):

| Rule | Ethereum L1 | Polygon PoS (Bor) |
| --- | --- | --- |
| EIP-1559 base fee sink | Permanently burned | Credited to `BurntContract` (`0x70bc...` London → `0x7A8e...` Agra → `0x3ef5...` Lisovo) |
| EIP-1559 denominator | `8` | `8` (London) → `16` (Delhi `38,189,056`) → `64` (Bhilai `73,440,256`, PIP-58) |
| EIP-1559 gas target | `50%` | `50%` → `65%` (Dandeli `81,424,000`, PIP-75) |
| Base fee change bound | Exact formula | Exact pre-Lisovo; `±5%` (min `1 wei`) bound post-Lisovo (`83,756,500`, PIP-79) |
| Max deployed code size | `24,576` B (24 KiB) | `32,768` B (32 KiB) post-Ahmedabad (`62,278,656`) |
| `P256VERIFY` (`0x100`) | Osaka (`6900` gas) | Napoli (`54,876,000`, `3450` gas RIP-7212) → Lisovo (`83,756,500`, `6900` gas EIP-7951) |
| `EIP-4844` / KZG (`0x0A`) | Cancun+ | Disabled via `disabled_precompiles` on Napoli..Bhilai and LisovoPro+ (skips KZG setup) |
| L1 beacon / deposits / withdrawals | Enabled | Compiled out (`enable_beacon_roots = false`, etc.) |
| Default coinbase (Rio+) | Block producer | `0x7Ee41D8A25641000661B1EF5E6AE8A00400466B0` (Rio `77,414,656`, VEBloP) |
| StateSync system tx | N/A | Zero-gas call from `SYSTEM_ADDRESS` (`0xffff...fffe`) to `StateReceiver` (`0x1001`) |

## Testing, fuzzing, and benchmarks

Every chain implementation enforces 0-tolerance wei, gas, fee-sink, and storage parity across all 7 comptime `EvmProfiles` and against tuned peer EVMs:

```bash
# Unit & real-EVM host tests (src/chains/ + src/chains_package.zig)
zig build test-chains

# Leak-checked Polygon PoS fuzz targets (test/fuzz/chains/polygon_fuzz.zig)
zig build test-fuzz -Dtest-filter='Polygon'
zig build fuzz-campaign -Doptimize=ReleaseSafe -- --target=chain_polygon --iters=5000 --threads=4

# ReleaseFast 7-profile sweep (7/7 0-tolerance parity)
zig build bench-chain-polygon
zig build bench-chains

# Head-to-head C-ABI comparator vs Polygon-tuned revm (up to 8.27x on Apple M3 Max, 6/6 parity)
zig build bench-chain-polygon-bridge -Dcpu=native
RUSTFLAGS="-C target-cpu=native" cargo run --release --manifest-path bench/chains/polygon/compare/Cargo.toml
```

For the full top-20 EVM chain specification matrix and contributor playbook for adding new chains, see [`docs/dev/CHAINS-PATTERN.md`](https://github.com/evmts/guillotine/blob/main/docs/dev/CHAINS-PATTERN.md) and [`bench/chains/README.md`](https://github.com/evmts/guillotine/blob/main/bench/chains/README.md).
