# projectpai/paicoin architecture

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

- Interactive diagram: https://gitdiagram.com/projectpai/paicoin
- Repository: https://github.com/projectpai/paicoin

## Overview

##### Project Type and Purpose
- This is a full-stack, open-source blockchain node and wallet application (PAI Coin Core) written primarily in C++.
- It implements a hybrid PoW/PoS consensus engine, peer-to-peer networking, transaction and script validation, UTXO storage (via LevelDB), and both CLI/RPC and Qt GUI front-ends for end users.

##### Main Architectural Components
a. P2P Network Layer
- Peer discovery & connection management (src/net.cpp/net.h)
- Message parsing/serialization (protocol.h, netmessagemaker.h)
- Network processing & relaying (src/net_processing.cpp/net_processing.h)
b. Consensus & Validation Engine
- Block and transaction verification (src/consensus, src/validation.cpp)
- PoW and PoS hybrid consensus (src/pow.cpp, src/stake/)
- Mempool management (src/txmempool.cpp)
- Script interpreter (src/script/)
c. Storage Layer
- UTXO set and block index (src/coins.cpp/coins.h, src/txdb.cpp)
- Underlying database (LevelDB in src/leveldb/)
- Block/chain parameters and config (src/chainparams.cpp)
d. Wallet & Key Management
- Wallet DB and key store (src/wallet/db.cpp, src/wallet/keystore.cpp)
- RPC wallet interface (src/wallet/rpcwallet.cpp)
- Fee estimation & coin selection (src/wallet/fees.cpp, src/policy/)
- Staking and ticket management (src/wallet/auto-voter, src/wallet/ticket-buyer)
e. RPC & HTTP Server
- JSON-RPC over HTTP(S) (src/httpserver.cpp, src/httprpc.cpp)
- UniValue JSON library (src/univalue/)
- REST endpoints (src/rest.cpp)
- ZeroMQ notifications (src/zmq/)
f. Front-End Clients
- CLI tools (paicoind, paicoin-cli, paicoin-tx)
- Qt GUI wallet (src/qt/…)
g. Auxiliary Modules
- Crypto primitives (src/crypto/, secp256k1/)
- Utilities & configuration (src/util*, src/init.cpp)
- Testing harness (src/test/, test/functional/)

##### Relationships & Data Flow
- Incoming P2P messages → Network Layer → Net Processing → Validation Engine → Storage Layer
- Transactions submitted via CLI/RPC/Qt → RPC Server → Validation → Mempool → P2P Broadcast
- Mining module → pulls transactions from Mempool → builds block template → PoW miner
- Staking module → selects tickets from wallet → signs & submits via P2P
- Wallet GUI/CLI → RPC client → RPC Server (could be local IPC)
- ZeroMQ/REST subscribers → receive notifications on consensus events (new block, new TX)

##### Architectural Patterns & Principles
- Layered architecture: Network, Consensus/Validation, Storage, API, UI
- Modular design: clear separation of P2P code, consensus code, wallet code, GUI code
- Plugin-like optional components: ZeroMQ, REST, LevelDB, secp256k1 can be enabled/disabled at build
- Threaded concurrency: networking, validation, scheduler (src/scheduler.cpp), wallet IO run in separate threads

## Diagram (Mermaid)

```mermaid
graph TB
    %% Network Layer
    subgraph "Network Layer"
        P2P["P2P Network"]:::network
    end

    %% Consensus & Validation
    subgraph "Consensus & Validation"
        Consensus["Consensus Engine"]:::compute
        Mempool["Mempool"]:::compute
        Script["Script Interpreter"]:::compute
    end

    %% Storage Layer
    subgraph "Storage Layer"
        UTXO["UTXO Set & Block Index"]:::storage
        DB["LevelDB"]:::storage
    end

    %% Wallet Module
    subgraph "Wallet Module"
        WalletDB["Wallet DB & Keystore"]:::wallet
        RPCWallet["RPC Wallet Interface"]:::wallet
        Fees["Fee Estimation & Coin Selection"]:::wallet
        Staking["Staking & Ticket Management"]:::wallet
    end

    %% RPC & HTTP Server
    subgraph "RPC & HTTP Server"
        RPCServer["RPC / HTTP Server"]:::api
        REST["REST Endpoints"]:::api
        ZMQ["ZeroMQ Notifications"]:::api
    end

    %% Front-End Clients
    subgraph "Front-End Clients"
        CLI["CLI Tools"]:::ui
        GUI["Qt GUI Wallet"]:::ui
    end

    %% Auxiliary Modules
    subgraph "Auxiliary Modules"
        Crypto["Crypto Primitives"]:::aux
        Utils["Utilities & Config"]:::aux
        Scheduler["Scheduler"]:::aux
        Test["Testing Harness"]:::aux
        Deployment["Deployment Tools (Docker, Contrib)"]:::aux
    end

    %% Data Flows
    P2P -->|“messages”| Consensus
    CLI -->|“RPC calls”| RPCServer
    GUI -->|“RPC calls”| RPCServer
    RPCServer -->|“validate TX/block”| Consensus
    Consensus -->|“update UTXO”| UTXO
    Consensus -->|“publish events”| ZMQ
    Consensus -->|“serve data”| REST
    Mempool -->|“tx pool”| Consensus
    WalletDB <-->|“read/write”| UTXO
    WalletDB <-->|“balance & sign”| Consensus
    Staking -->|“submit tickets”| P2P

    %% Click Events
    click P2P "https://github.com/projectpai/paicoin/blob/master/src/net.h"
    click Consensus "https://github.com/projectpai/paicoin/tree/master/src/consensus/"
    click Mempool "https://github.com/projectpai/paicoin/blob/master/src/txmempool.cpp"
    click Script "https://github.com/projectpai/paicoin/tree/master/src/script/"
    click UTXO "https://github.com/projectpai/paicoin/blob/master/src/coins.cpp"
    click DB "https://github.com/projectpai/paicoin/tree/master/src/leveldb/"
    click WalletDB "https://github.com/projectpai/paicoin/blob/master/src/wallet/db.cpp"
    click RPCWallet "https://github.com/projectpai/paicoin/blob/master/src/wallet/rpcwallet.cpp"
    click Fees "https://github.com/projectpai/paicoin/blob/master/src/wallet/fees.cpp"
    click Staking "https://github.com/projectpai/paicoin/tree/master/src/wallet/auto-voter/"
    click RPCServer "https://github.com/projectpai/paicoin/blob/master/src/httpserver.cpp"
    click REST "https://github.com/projectpai/paicoin/blob/master/src/rest.cpp"
    click ZMQ "https://github.com/projectpai/paicoin/tree/master/src/zmq/"
    click CLI "https://github.com/projectpai/paicoin/blob/master/src/paicoin-cli.cpp"
    click GUI "https://github.com/projectpai/paicoin/tree/master/src/qt/"
    click Crypto "https://github.com/projectpai/paicoin/tree/master/src/crypto/"
    click Utils "https://github.com/projectpai/paicoin/blob/master/src/util.cpp"
    click Scheduler "https://github.com/projectpai/paicoin/blob/master/src/scheduler.cpp"
    click Test "https://github.com/projectpai/paicoin/tree/master/src/test/"

    %% Styles
    classDef network fill:#AEDFF7,stroke:#009,stroke-width:1px;
    classDef compute fill:#FDE9A9,stroke:#996600,stroke-width:1px;
    classDef storage fill:#CDEAC0,stroke:#2E7D32,stroke-width:1px;
    classDef wallet fill:#F8BBD0,stroke:#C2185B,stroke-width:1px;
    classDef api fill:#D1C4E9,stroke:#512DA8,stroke-width:1px,stroke-dasharray:5 5;
    classDef ui fill:#FFE0B2,stroke:#E65100,stroke-width:1px;
    classDef aux fill:#E0E0E0,stroke:#424242,stroke-width:1px,stroke-dasharray:5 5;
```
