# chainsafe/lodestar architecture

> Architecture diagram of the GitHub repository chainsafe/lodestar, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chainsafe/lodestar
- Repository: https://github.com/chainsafe/lodestar

## Overview

##### Major Components
- Core Client (Beacon Node): Represented by the “@lodestar/beacon-node” package. This component is responsible for running the beacon chain client, including processing blocks, managing state, handling fork choice, and interacting with the network.
- Communication/API Layer: The “@lodestar/api” package exposes REST endpoints while “@lodestar/reqresp” and “@lodestar/light-client” handle protocol-level messaging (request/response, light client sync, etc.).
- CLI and User Interface: The main CLI tool is in “@chainsafe/lodestar” (also under packages/cli). It ties together and provides command-line access to the underlying services.
- State Transition & Consensus: The “@lodestar/state-transition” package covers all the logic for state transition and processing blocks (deposits, attestation processing, slashing, fork-choice integration, etc.). It forms the core engine for checking consensus rules.
- Supporting Modules: Several packages provide supporting functionality like:
- Configuration: “@lodestar/config” (for consensus parameters and config management)
- Database/Persistence: “@lodestar/db” (for reading/writing persistent consensus data)
- Logging: “@lodestar/logger” (for a unified logging interface)
- Consensus Parameters & Types: “@lodestar/params”, “@lodestar/types” (to shout about Ethereum-specific types, SSZ, chain constants, etc.)
- Fork Choice: “@lodestar/fork-choice” (with its fork-choice algorithms and proto-array implementations)
- Proving & Verification: “@lodestar/prover” (for verifying execution payloads and JSON-RPC calls)
- Utilities: “@lodestar/utils” (miscellaneous functions used across the project)
- Validator functionality: “@lodestar/validator” (for client side validator tools, slashing protection, duties, external signer integration)
- Other specialized modules such as “@lodestar/flare”, which handles non-standard actions or triggering events.

- The beacon node (the main execution engine) will have internal dependencies on:
- The state-transition module (processing block transitions and protocol rules),
- The fork-choice module (ensuring chain selection is correct),
- The database module for state persistence,
- The network modules (reqresp and light-client) for peer-to-peer communications.
- Configuration and parameters modules providing settings/information to multiple components (for example, passing consensus specs to state transition, database, and API).
- Logging and monitoring systems (embedded within modules such as “@lodestar/logger”) and highlight that metrics (possibly exposed by the metrics server in the beacon node package) flow from core operations to external dashboards (Grafana or Prometheus, as indicated by the docker-compose and dashboards directories).
- For network communication, show the “reqresp” layer encapsulating requests/responses between nodes; these exchange data with the beacon node and interact with external Ethereum networks (e.g., via ETH1 bridges in certain modules).

##### Architectural Patterns and Design Principles
- Separation of concerns: Each package is responsible for a distinct aspect of Ethereum consensus (e.g., state transitions, configuration, network communication, logging).
- That the system is extensible—new modules or updates to a module (for example, updated state-transition logic, fork-choice protocol, or new network communication strategies) can be developed relatively independently.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface Layer"
        CLI["@chainsafe/lodestar (CLI)"]:::ui
    end

    %% Core Execution Layer
    subgraph "Core Execution Layer"
        BN["@lodestar/beacon-node (Beacon Node)"]:::core
        ST["@lodestar/state-transition (State Transition)"]:::core
        FC["@lodestar/fork-choice (Fork Choice)"]:::core
        VAL["@lodestar/validator (Validator)"]:::core
    end

    %% Data & Persistence Layer
    subgraph "Data & Persistence Layer"
        DB["@lodestar/db (Database)"]:::data
        CONF["@lodestar/config (Configuration)"]:::data
        CP["@lodestar/params (Consensus Params)"]:::data
        TYP["@lodestar/types (Types)"]:::data
    end

    %% External Interfaces
    subgraph "External Interfaces"
        API["@lodestar/api (API Layer)"]:::external
        REQ["@lodestar/reqresp (ReqResp)"]:::external
        LC["@lodestar/light-client (Light Client)"]:::external
    end

    %% Supporting Modules
    subgraph "Supporting Modules"
        LOG["@lodestar/logger (Logging)"]:::support
        PROV["@lodestar/prover (Proving & Verification)"]:::support
        UTIL["@lodestar/utils (Utilities)"]:::support
        FLR["@lodestar/flare (Flare)"]:::support
    end

    %% Relationships
    CLI -->|"initiate"| BN

    BN -->|"process_block"| ST
    BN -->|"fork_choice"| FC
    BN -->|"validator_calls"| VAL
    BN -->|"persist_state"| DB
    BN -->|"reqresp_comm"| REQ
    BN -->|"light_sync"| LC
    BN -->|"api_access"| API
    BN -->|"log_events"| LOG
    BN -->|"verify_payloads"| PROV
    BN -->|"util_functions"| UTIL
    BN -->|"specialized_logic"| FLR

    ST -->|"log_events"| LOG
    FC -->|"log_events"| LOG
    VAL -->|"log_events"| LOG

    ST -->|"uses_config"| CONF
    FC -->|"uses_config"| CONF
    VAL -->|"uses_config"| CONF
    API -->|"uses_config"| CONF

    CP -->|"provide_params"| ST
    CP -->|"provide_params"| FC
    CP -->|"provide_params"| DB

    TYP -->|"provide_types"| ST
    TYP -->|"provide_types"| FC
    TYP -->|"provide_types"| DB

    CONF -.->|"config_data"| BN
    CONF -.->|"config_data"| API

    %% Styles
    classDef ui fill:#ccf,stroke:#333,stroke-width:2px;
    classDef core fill:#cfc,stroke:#333,stroke-width:2px;
    classDef data fill:#ffc,stroke:#333,stroke-width:2px;
    classDef external fill:#fcc,stroke:#333,stroke-width:2px;
    classDef support fill:#eee,stroke:#333,stroke-width:2px;

    %% Click Events
    click BN "https://github.com/chainsafe/lodestar/tree/unstable/packages/beacon-node"
    click API "https://github.com/chainsafe/lodestar/tree/unstable/packages/api"
    click CLI "https://github.com/chainsafe/lodestar/tree/unstable/packages/cli"
    click ST "https://github.com/chainsafe/lodestar/tree/unstable/packages/state-transition"
    click CONF "https://github.com/chainsafe/lodestar/tree/unstable/packages/config"
    click DB "https://github.com/chainsafe/lodestar/tree/unstable/packages/db"
    click LOG "https://github.com/chainsafe/lodestar/tree/unstable/packages/logger"
    click CP "https://github.com/chainsafe/lodestar/tree/unstable/packages/params"
    click TYP "https://github.com/chainsafe/lodestar/tree/unstable/packages/types"
    click FC "https://github.com/chainsafe/lodestar/tree/unstable/packages/fork-choice"
    click PROV "https://github.com/chainsafe/lodestar/tree/unstable/packages/prover"
    click UTIL "https://github.com/chainsafe/lodestar/tree/unstable/packages/utils"
    click VAL "https://github.com/chainsafe/lodestar/tree/unstable/packages/validator"
    click FLR "https://github.com/chainsafe/lodestar/tree/unstable/packages/flare"
    click REQ "https://github.com/chainsafe/lodestar/tree/unstable/packages/reqresp"
    click LC "https://github.com/chainsafe/lodestar/tree/unstable/packages/light-client"
```
