# fantom-foundation/sonic architecture

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

- Interactive diagram: https://gitdiagram.com/fantom-foundation/sonic
- Repository: https://github.com/fantom-foundation/sonic

## Overview

Below are guidelines tailored to this project:

##### Main Components
- Command Line Tool (sonictool): Found in ‘cmd/sonictool’, this component is used for managing genesis creation, configuration dump, and validator key operations.
- Network & Consensus Layer: The gossip package (and related sub-packages like eventcheck, inter, and protocols directories) implements the peer-to-peer messaging and consensus functions. This includes the handling of events, block propagation, and Lachesis-specific algorithms.
- Ethereum Virtual Machine (EVM) Layer: Components such as ‘evmcore’, ‘ethapi’, and parts of ‘ftmclient’ implement EVM functionalities to make the chain EVM-compatible and provide API access.
- Data Persistence & Database Utilities: The topicsdb directory along with numerous utilities (in utils, config, and dbutil folders) establish persistent storage for blockchain data, state, and logs.
- Configuration & Validator Keys: The config package (with default configuration, parameter management, etc.) and the valkeystore package manage node configuration and validator key management (including signing and encrypting keys).

##### Relationships and Interactions
- Sonicd as the central node runtime that interacts with both the consensus (gossip and protocols) and the EVM core modules.
- Data flow between the network layer (gossip) and the core blockchain processing components. Indicate that gossip distributes events/transactions to be processed by the node’s consensus and state machine.
- EVM components (evmcore, ethapi) to the smart contract execution and transaction processing parts, highlighting how they interface with both the state management and networking layers.
- Storage subsystems (topicsdb and utilities for database management) as backing components where blockchain state, receipts, and historical data are stored.
- Configuration and logging modules (config, logger) as supporting infrastructure ensuring operational stability and debugging.

##### Important Architectural Patterns
- Modular Design: The separation in directories (cmd, config, gossip, evmcore, etc.) indicates a modular architecture where each package plays a distinct role.
- Layered Architecture: Clearly distinguish between the client’s CLI/tools (sonictool and sonicd startup), network and consensus layer (gossip and protocols), application (EVM processing), and storage.
- Adaptability & Extensibility: The existence of multiple adapters in the utils folder (for different database interfaces or EVM storage) shows an emphasis on decoupling and flexibility.
- Cross-Platform/Conditional Compilation: The presence of OS-specific implementations (e.g., diskusage for OpenBSD and Windows, memory packages) highlights attention to cross-platform support.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI & Configuration Layer
    subgraph "CLI & Configuration"
        sonictool["Command Line Tool (sonictool)"]:::cli
        config["config"]:::config
        valkeystore["valkeystore"]:::config
    end

    %% Node Runtime Layer
    subgraph "Node Runtime"
        sonicd["Node Runtime (sonicd)"]:::node
    end

    %% Consensus & P2P Communication Layer
    subgraph "Consensus & P2P Communication"
        gossip["gossip"]:::consensus
        eventcheck["eventcheck"]:::consensus
        inter["inter"]:::consensus
    end

    %% EVM & Smart Contract Execution Layer
    subgraph "EVM & Smart Contract Execution"
        evmcore["evmcore"]:::evm
        ethapi["ethapi"]:::evm
        ftmclient["ftmclient"]:::evm
    end

    %% Data Persistence Layer
    subgraph "Data Persistence"
        topicsdb["topicsdb"]:::db
        dbutil["dbutil"]:::db
        iodb["iodb"]:::db
    end

    %% Inter-layer Connections
    sonictool -->|"init/config"| config
    sonictool -->|"init/keys"| valkeystore
    config -->|"provide-config"| sonicd
    valkeystore -->|"provide-keys"| sonicd

    gossip -->|"consensus-events"| sonicd
    eventcheck -->|"verify-events"| sonicd
    inter -->|"peer-msgs"| sonicd

    sonicd -->|"trigger-evm"| evmcore
    gossip -->|"deliver-tx"| evmcore

    evmcore -->|"api-call"| ethapi
    evmcore -->|"client-call"| ftmclient

    sonicd -->|"persist-state"| topicsdb
    evmcore -->|"update-state"| topicsdb

    topicsdb -->|"db-utils"| dbutil
    dbutil -->|"chain-storage"| iodb

    %% Class Definitions for Color Coding
    classDef cli fill:#cfe2f3,stroke:#6c8ebf,stroke-width:2px;
    classDef node fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef consensus fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef evm fill:#d0e0e3,stroke:#1c7c7d,stroke-width:2px;
    classDef db fill:#fce5cd,stroke:#e69138,stroke-width:2px;
    classDef config fill:#ead1dc,stroke:#b85497,stroke-width:2px;

    class sonictool cli;
    class config config;
    class valkeystore config;
    class sonicd node;
    class gossip consensus;
    class eventcheck consensus;
    class inter consensus;
    class evmcore evm;
    class ethapi evm;
    class ftmclient evm;
    class topicsdb db;
    class dbutil db;
    class iodb db;

    %% Click Events for Component Mapping
    click sonicd "https://github.com/fantom-foundation/sonic/tree/main/cmd/sonicd"
    click sonictool "https://github.com/fantom-foundation/sonic/tree/main/cmd/sonictool"
    click gossip "https://github.com/fantom-foundation/sonic/tree/main/gossip"
    click eventcheck "https://github.com/fantom-foundation/sonic/tree/main/eventcheck"
    click inter "https://github.com/fantom-foundation/sonic/tree/main/inter"
    click evmcore "https://github.com/fantom-foundation/sonic/tree/main/evmcore"
    click ethapi "https://github.com/fantom-foundation/sonic/tree/main/ethapi"
    click ftmclient "https://github.com/fantom-foundation/sonic/tree/main/ftmclient"
    click topicsdb "https://github.com/fantom-foundation/sonic/tree/main/topicsdb"
    click dbutil "https://github.com/fantom-foundation/sonic/tree/main/utils/dbutil"
    click iodb "https://github.com/fantom-foundation/sonic/tree/main/utils/iodb"
    click config "https://github.com/fantom-foundation/sonic/tree/main/config"
    click valkeystore "https://github.com/fantom-foundation/sonic/tree/main/valkeystore"
```
