# ava-labs/avalanchego architecture

> Architecture diagram of the GitHub repository ava-labs/avalanchego, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ava-labs/avalanchego
- Repository: https://github.com/ava-labs/avalanchego

## Overview

##### High-Level Components
First, organize the main architectural components into these categories:

a) Core Network Layer:
- Network validators and consensus mechanisms
- Connection handling and peer management

b) Chain Management:
- Three main chains (P-Chain, C-Chain, X-Chain)
- VM management system (/vms directory)
- Chain registry and coordination components

c) API Layer:
- Various APIs (/api directory)
- Admin, health, info, and keystore interfaces
- HTTP server components

d) Database Layer:
- Database abstractions (/database directory)
- MerkleDB implementation (/x/merkledb)
- Versioning and state management

##### Key Relationships and Flows

a) External Communication:
- Incoming API requests
- P2P network communication
- Consensus message flows

b) Internal Processing:
- Flow from API to chain processing
- Consensus to database operations
- VM execution paths

##### Specific Components to Highlight

a) Consensus Layer:
- Snow* family protocols implementation
- Validator management
- Block processing pipeline

b) Virtual Machine System:
- VM registry and management
- Chain VM interfaces
- Custom VM support (rpcchainvm)

##### Technical Details

Annotations for:
- Protocol ports (9650, 9651)
- Database backends (LevelDB/Pebble)
- Supported VM types
- API endpoint groups

##### Implementation Notes

Create callouts for:
- Bootstrapping process
- State synchronization
- Blockchain validation
- Cross-chain atomic transactions

**┌─────────────────────────────────────┐**
- External Layer           │
- (API Endpoints, P2P Connections)   │
- ┤
- Processing Layer            │
- (Chain Management, Consensus)      │
- ┤
- Execution Layer             │
- (VMs, State Management)          │
- ┤
- Persistence Layer           │
- (Database, State Storage)        │
- ┘

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define styles
    classDef external fill:#e1f5fe,stroke:#01579b
    classDef processing fill:#e8f5e9,stroke:#1b5e20
    classDef execution fill:#fff3e0,stroke:#e65100
    classDef persistence fill:#fce4ec,stroke:#880e4f
    classDef api fill:#fff59d,stroke:#f57f17
    classDef consensus fill:#ffcdd2,stroke:#b71c1c
    classDef vm fill:#e0f2f1,stroke:#004d40

    %% External Layer
    subgraph ExternalLayer["External Layer"]
        API["API Gateway"]
        P2P["P2P Network"]
    end

    %% API Layer
    subgraph APILayer["API Layer"]
        AdminAPI["Admin API"]:::api
        HealthAPI["Health API"]:::api
        InfoAPI["Info API"]:::api
        KeystoreAPI["Keystore API"]:::api
        HTTPServer["HTTP Server"]:::api
    end

    %% Processing Layer
    subgraph ProcessingLayer["Processing Layer"]
        ChainMgmt["Chain Management"]
        ConsensusEngine["Consensus Engine"]:::consensus
        ValidatorMgmt["Validator Management"]:::consensus
    end

    %% Chain Layer
    subgraph ChainLayer["Chain Layer"]
        PChain["P-Chain"]
        CChain["C-Chain"]
        XChain["X-Chain"]
    end

    %% Execution Layer
    subgraph ExecutionLayer["Execution Layer"]
        VMRegistry["VM Registry"]:::vm
        RPCChainVM["RPC Chain VM"]:::vm
        VMInterfaces["VM Interfaces"]:::vm
    end

    %% Persistence Layer
    subgraph PersistenceLayer["Persistence Layer"]
        DB["Database Interface"]
        MerkleDB["MerkleDB"]
        StateDB["State Management"]
    end

    %% Relationships
    API --> HTTPServer
    P2P --> ChainMgmt
    HTTPServer --> |Routes| AdminAPI & HealthAPI & InfoAPI & KeystoreAPI
    ChainMgmt --> |Manages| PChain & CChain & XChain
    ChainMgmt --> ConsensusEngine
    ConsensusEngine --> ValidatorMgmt
    PChain & CChain & XChain --> VMRegistry
    VMRegistry --> RPCChainVM
    VMRegistry --> VMInterfaces
    ConsensusEngine --> StateDB
    StateDB --> DB
    DB --> MerkleDB

    %% Click events
    click AdminAPI "https://github.com/ava-labs/avalanchego/tree/master/api/admin/"
    click HealthAPI "https://github.com/ava-labs/avalanchego/tree/master/api/health/"
    click InfoAPI "https://github.com/ava-labs/avalanchego/tree/master/api/info/"
    click KeystoreAPI "https://github.com/ava-labs/avalanchego/tree/master/api/keystore/"
    click HTTPServer "https://github.com/ava-labs/avalanchego/tree/master/api/server/"
    click ChainMgmt "https://github.com/ava-labs/avalanchego/tree/master/chains/"
    click PChain "https://github.com/ava-labs/avalanchego/tree/master/vms/platformvm/"
    click XChain "https://github.com/ava-labs/avalanchego/tree/master/vms/avm/"
    click VMRegistry "https://github.com/ava-labs/avalanchego/tree/master/vms/registry/"
    click RPCChainVM "https://github.com/ava-labs/avalanchego/tree/master/vms/rpcchainvm/"
    click VMInterfaces "https://github.com/ava-labs/avalanchego/blob/master/snow/engine/common/vm.go"
    click DB "https://github.com/ava-labs/avalanchego/tree/master/database/"
    click MerkleDB "https://github.com/ava-labs/avalanchego/tree/master/x/merkledb/"
    click StateDB "https://github.com/ava-labs/avalanchego/tree/master/database/versiondb/"
    click ConsensusEngine "https://github.com/ava-labs/avalanchego/tree/master/snow/consensus/snowman/"
    click ValidatorMgmt "https://github.com/ava-labs/avalanchego/tree/master/snow/validators/"

    %% Legend
    subgraph Legend["Legend"]
        L1["API Component"]:::api
        L2["Consensus Component"]:::consensus
        L3["VM Component"]:::vm
        L4["External Interface"]:::external
        L5["Processing Component"]:::processing
        L6["Execution Component"]:::execution
        L7["Persistence Component"]:::persistence
    end
```
