# forta-network/forta-node architecture

> Architecture diagram of the GitHub repository forta-network/forta-node, 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/forta-network/forta-node
- Repository: https://github.com/forta-network/forta-node

## Overview

##### Main Components
- CLI & Orchestration Layer: Represented mainly by the cmd folder (e.g., cmd/cli.go, cmd/run.go, etc.) and the main.go file. This is the entry point from which administrators initialize (“forta init”) and run (“forta run”) the node. It interacts with the underlying service manager.
- Container Management: The project uses Docker extensively (multiple Dockerfiles, docker-compose configurations, and related scripts). This component acts as the runtime environment for the different services and agents.
- Service Modules: These are implemented in the services folder. They include specialized components such as:
- Detection and scanning (services/scanner)
- API gateway and JSON-RPC communication (services/json-rpc, services/public-api)
- JWT-based authentication (services/jwt-provider)
- Supervisory components (services/supervisor) that monitor and manage agent logs and health
- Miscellaneous supporting services like storage management (services/storage), update mechanisms (services/updater), and metric collections (services/components/metrics, services/components/prometheus)
- Detection Agents and Bots: Although deployed as Dockerized containers, the agents are developed in various parts of the project (see directories like tests/e2e/agents and clients/agentgrpc). They represent the detection bots that monitor blockchain events.
- External Integrations and Clients: Various client packages (e.g., clients/alertapi, clients/docker, clients/ipfsclient, clients/redis) provide interfaces to external systems (such as alert platforms, container runtimes, IPFS, and Redis) and are used across different services.
- Continuous Integration/Deployment & Build Tools: The .github directory, Makefile, and release scripts indicate robust build, test, and deployment pipelines; include these components as ancillary but important parts of the system that ensure automation and consistency.

##### Relationships and Interactions
- The CLI (user interface) triggers container orchestration actions using Docker. In turn, it deploys various services that work independently but coordinate through inter-service APIs (e.g., via JSON-RPC or public API endpoints).
- The service modules interact with detection agents running in containers—supervisors track the agents, while messaging and alert components handle result propagation.
- Build and release pipelines (represented in the .github workflows and release directories) are part of the development environment and ensure that container images (CLI and service containers) are built and pushed appropriately.
- Client libraries encapsulate calls to external services (like IPFS or Redis) while also abstracting internal inter-service communications.

##### Architectural Patterns and Design Principles
- Modularity: The project is organized into multiple small components (e.g., services/components, clients, store, config) that interact through well-defined interfaces.
- Containerization: With Docker at its core, each service and agent is isolated into its own container for scalability and ease of deployment.
- Microservices/Lite Service Decomposition: While not strictly split into separate microservices, the diverse services (scanner, jwt-provider, supervisor, etc.) resemble a microservices architecture within a container ecosystem.
- Clear Separation of Concerns: Each module (CLI, service orchestration, blockchain detection, logging, monitoring) is implemented in its own area, easing maintenance and future expansion.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% CLI & Orchestration Layer
    subgraph "CLI & Orchestration Layer"
        CLI["Forta Node CLI"]:::orchestration
        MAIN["Main Entry (main.go)"]:::orchestration
    end

    %% Container Management Layer
    subgraph "Container Management"
        DF["Dockerfile CLI"]:::container
        DC["docker-compose Configuration"]:::container
        DPS["Build & Push Scripts"]:::container
    end

    %% Service Modules Layer
    subgraph "Service Modules"
        SCN["Scanner Service"]:::service
        JRPC["JSON-RPC Service"]:::service
        PUB["Public API Service"]:::service
        JWT["JWT Provider Service"]:::service
        SUP["Supervisor Service"]:::service
        STG["Storage Service"]:::service
        UPD["Updater Service"]:::service
        MET["Metrics Component"]:::service
        PROM["Prometheus Monitoring"]:::service
    end

    %% Detection Agents Layer
    subgraph "Detection Agents"
        DDA["Dockerized Detection Agents"]:::agent
        AGC["gRPC Agent Client"]:::agent
    end

    %% External Systems Layer
    subgraph "External Systems"
        ALERT["Alert API & Messaging"]:::external
        DCLIENT["Docker Client"]:::external
        IPFS["IPFS Client"]:::external
        REDIS["Redis Client"]:::external
        BCNET["Blockchain Network"]:::external
    end

    %% CI/CD & Build Tools Layer
    subgraph "CI/CD & Build Tools"
        CICD["CI/CD Pipelines"]:::cicd
        BUILD["Build Automation"]:::cicd
        REL["Release Scripts"]:::cicd
    end

    %% Relationships

    %% CLI triggers Container Management
    CLI -->|"commands"| DF
    CLI -->|"commands"| DC
    CLI -->|"triggers"| DPS
    MAIN -->|"initiates"| DPS

    %% CI/CD builds container config and services
    CICD -->|"builds"| DF
    CICD -->|"builds"| PUB
    BUILD --> CICD
    REL --> CICD

    %% Container Management deploys Services
    DPS -->|"deploys"| SUP

    %% Container Management interacts with External Docker Client
    DC -->|"uses"| DCLIENT

    %% Services interactions
    SUP -->|"monitors"| DDA
    AGC -->|"reports to"| ALERT
    SCN -->|"detects anomalies"| ALERT
    PUB -->|"routes requests"| ALERT
    JRPC -->|"queries"| BCNET
    STG -->|"stores data"| IPFS
    STG -->|"caches data"| REDIS
    MET -->|"feeds metrics to"| PROM

    %% Services interact with Detection Agents
    SUP -->|"manages"| DDA

    %% External interactions from CI/CD
    %% (CI/CD pipeline builds and deploys containerized services)

    %% Detection Agents reporting
    AGC -->|"reports findings"| ALERT

    %% Classes
    classDef orchestration fill:#B3CDE0,stroke:#000,stroke-width:2px;
    classDef container fill:#CDEBA7,stroke:#000,stroke-width:2px;
    classDef service fill:#F9E79F,stroke:#000,stroke-width:2px;
    classDef agent fill:#F7C6C7,stroke:#000,stroke-width:2px;
    classDef external fill:#D5F5E3,stroke:#000,stroke-width:2px;
    classDef cicd fill:#D6EAF8,stroke:#000,stroke-width:2px;

    %% Click Events
    click CLI "https://github.com/forta-network/forta-node/tree/master/cmd"
    click MAIN "https://github.com/forta-network/forta-node/blob/master/main.go"
    click DF "https://github.com/forta-network/forta-node/blob/master/Dockerfile.cli"
    click DC "https://github.com/forta-network/forta-node/blob/master/docker-compose/standalone/docker-compose.yml"
    click DPS "https://github.com/forta-network/forta-node/blob/master/scripts/docker-build-push.sh"
    click SCN "https://github.com/forta-network/forta-node/tree/master/services/scanner"
    click JRPC "https://github.com/forta-network/forta-node/tree/master/services/json-rpc"
    click PUB "https://github.com/forta-network/forta-node/tree/master/services/public-api"
    click JWT "https://github.com/forta-network/forta-node/tree/master/services/jwt-provider"
    click SUP "https://github.com/forta-network/forta-node/tree/master/services/supervisor"
    click STG "https://github.com/forta-network/forta-node/tree/master/services/storage"
    click UPD "https://github.com/forta-network/forta-node/tree/master/services/updater"
    click MET "https://github.com/forta-network/forta-node/tree/master/services/components/metrics"
    click PROM "https://github.com/forta-network/forta-node/tree/master/services/components/prometheus"
    click DDA "https://github.com/forta-network/forta-node/tree/master/tests/e2e/agents"
    click AGC "https://github.com/forta-network/forta-node/tree/master/clients/agentgrpc"
    click ALERT "https://github.com/forta-network/forta-node/tree/master/clients/alertapi"
    click DCLIENT "https://github.com/forta-network/forta-node/tree/master/clients/docker"
    click IPFS "https://github.com/forta-network/forta-node/tree/master/clients/ipfsclient"
    click REDIS "https://github.com/forta-network/forta-node/tree/master/clients/redis"
    click CICD "https://github.com/forta-network/forta-node/blob/master/.github"
    click BUILD "https://github.com/forta-network/forta-node/tree/master/Makefile"
    click REL "https://github.com/forta-network/forta-node/blob/master/scripts/build-for-release.sh"
```
