# benjamin-wilson/public-pool architecture

> Architecture diagram of the GitHub repository benjamin-wilson/public-pool, 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/benjamin-wilson/public-pool
- Repository: https://github.com/benjamin-wilson/public-pool

## Overview

##### Project Type and Purpose
- Type: A backend server application specifically for Bitcoin mining pool operations.
- Purpose: Facilitating stratum mining operations by receiving mining jobs, handling client requests, interacting with Bitcoin’s RPC interface, managing notifications and external shares, and interfacing with a web UI (public-pool-ui) for administration.

##### Main Components (as discerned from the file structure)
a. Application Core
- NestJS Application: This is the central component hosted in the “src” directory. It aggregates:
- Controllers (in src/controllers): Handle incoming HTTP/API and mining-related requests.
- Services (in src/services): Business logic components (e.g., bitcoin-rpc.service, stratum-v1.service, notification.service) that interact with other parts of the system.
- ORM Modules (in src/ORM): These modules (address-settings, blocks, client, external-shares, etc.) handle data persistence and represent key domain entities.
- Models (in src/models): Define core data structures such as mining jobs, Stratum messages, RPC definitions, and validators.
b. External Bitcoin Node Integration
- Bitcoin RPC Service: Communicates with a Bitcoin full node (via RPC endpoints as highlighted in README and bitcoin config files in full-setup) to retrieve block templates and mining information.
c. Stratum Mining Layer
- Stratum Service & Jobs: Manages the mining protocol (Stratum V1) supported by models and services, facilitating communication with mining clients.
d. Notification/Integration Modules
- Integrations with messaging platforms (Telegram, Discord) for notifications and alerts.
e. Deployment Infrastructure
- Docker & Docker Compose files and scripts (Dockerfile, docker-compose.yml, full-setup folder): These define containerization, deployment, and environment configuration.
- PM2 and process management: Instructions in README for running the production build.

##### Relationships and Data Flow
- Clients (mining hardware/software) communicate with the NestJS server via the Stratum protocol. Their requests are handled by dedicated controllers and routed to the appropriate services.
- The services call the underlying ORM modules to persist and retrieve data (e.g., block and client statistics, external shares).
- The bitcoin RPC service is used by the business layer to interact with the Bitcoin node, obtaining necessary blockchain data.
- Notification services trigger external communication (Telegram, Discord) based on key events.
- The web interface (public-pool-ui) operates separately but integrates with this server for administrative actions.

##### Architectural Patterns and Design Principles
- Modular Structure: Organized into distinct modules (controllers, services, ORM entities) that encapsulate functionality.
- Separation of Concerns: Clear division between API handling, business logic, and data persistence.
- Containerized Deployment: Docker (and Docker Compose) is used to isolate the running environment, with CI/CD pipelines configured via GitHub workflows.
- Extensibility: The design supports additional modules (patches, additional configuration, etc.) for future expansions (like adding new external integrations).

##### Final Advice
- Representing the essential data flows and interactions between sub-systems.
- Leverage a layered approach, placing infrastructure components (deployment and containerization) separately from application logic.
- Avoid over-simplification; break out each major module (e.g., the diversity within ORM and Services) if it adds clarity to understanding the overall architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actors
    subgraph "External Actors"
        MC("Mining Clients"):::actor
        UI("Public-Pool UI"):::actor
    end

    %% Application Core
    subgraph "Application Core"
        A("NestJS Application Core"):::core
        B("HTTP/API Controllers"):::core
        C("Business Logic Services"):::core
        D("Data Persistence (ORM Modules)"):::core
        E("Data Models & Structures"):::core
    end

    %% Stratum Mining Services
    subgraph "Stratum Mining Services"
        F("Stratum V1 Service"):::core
        G("Stratum V1 Jobs"):::core
    end

    %% External Integrations
    subgraph "External Integrations"
        H("Bitcoin RPC Integration"):::integration
        I("External Notifications"):::integration
        I1("Notification Service"):::integration
        I2("Discord Service"):::integration
        I3("Telegram Service"):::integration
    end

    %% Deployment & CI/CD
    subgraph "Deployment & Containerization"
        J("Deployment & Containerization"):::deployment
        J1("Dockerfile"):::deployment
        J2("docker-compose.yml"):::deployment
        J3("full-setup"):::deployment
    end
    K("CI/CD Workflow"):::deployment

    %% Connections between External Actors and Application Core
    UI -->|"adminOperations"| B
    MC -->|"miningProtocol"| F

    %% Internal connections within Application Core
    A --> B
    B --> C
    C --> D
    C --> E

    %% Connections from Stratum Mining Services to Business Logic
    F -->|"processJob"| C
    G -->|"queueJob"| C

    %% Connections from Business Logic to External Integrations
    C -->|"fetchBlockData"| H
    C -->|"sendNotifications"| I

    %% Breakdown of External Notifications
    I -->|"via"| I1
    I -->|"via"| I2
    I -->|"via"| I3

    %% Deployment connections
    A -->|"packagedIn"| J
    A -->|"managedBy"| K

    %% Class Definitions
    classDef core fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef integration fill:#f8d7da,stroke:#000,stroke-width:2px;
    classDef deployment fill:#d1e7dd,stroke:#000,stroke-width:2px;
    classDef actor fill:#fff3b0,stroke:#000,stroke-width:2px;

    %% Click Events
    click A "https://github.com/benjamin-wilson/public-pool/tree/master/src/"
    click B "https://github.com/benjamin-wilson/public-pool/tree/master/src/controllers"
    click C "https://github.com/benjamin-wilson/public-pool/tree/master/src/services"
    click D "https://github.com/benjamin-wilson/public-pool/tree/master/src/ORM"
    click E "https://github.com/benjamin-wilson/public-pool/tree/master/src/models"
    click H "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/bitcoin-rpc.service.ts"
    click F "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/stratum-v1.service.ts"
    click G "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/stratum-v1-jobs.service.ts"
    click I1 "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/notification.service.ts"
    click I2 "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/discord.service.ts"
    click I3 "https://github.com/benjamin-wilson/public-pool/blob/master/src/services/telegram.service.ts"
    click J1 "https://github.com/benjamin-wilson/public-pool/tree/master/Dockerfile"
    click J2 "https://github.com/benjamin-wilson/public-pool/blob/master/docker-compose.yml"
    click J3 "https://github.com/benjamin-wilson/public-pool/tree/master/full-setup"
    click K "https://github.com/benjamin-wilson/public-pool/tree/master/.github/workflows"
```
