# shugo-labs/tensorprox architecture

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

- Interactive diagram: https://gitdiagram.com/shugo-labs/tensorprox
- Repository: https://github.com/shugo-labs/tensorprox

## Overview

##### Project Type and Purpose
- Tensorprox is an open-source, full-stack distributed DDoS mitigation system built on the Bittensor network.
- It simulates attacks (via synthetic traffic generators), evaluates “miner” nodes running a high-performance packet filter (“Moat”), and rewards them via a validator-driven scoring mechanism.
- Key use-cases: deploy a validator node to challenge miners, or deploy a miner node to protect a target server (“King”) behind an AF_XDP-based firewall.

##### File Structure Analysis
Top-level directories:
- assets/        – user-facing docs for starting validator/miner and architecture illustrations
- model/         – pickled ML artifacts for traffic classification (decision_tree.pkl, imputer, scaler)
- neurons/       – CLI entrypoints for running miner.py and validator.py
- storage/       – persistent state (state.npz)
- tensorprox/    – core Python package
– base/        • abstract classes & protocols (Miner, Validator, Neuron, Dendrite, LoopRunner)
– core/        • implementations of round management, tunnel setup scripts (gre_setup.py), traffic generators, immutable scripts
– rewards/     • scoring and reward-setting logic
– utils/       • configuration loader, logging, SSH sync, timing, UID generation
– global_vars.py, settings.py
Other files:
- .env*, requirements.txt, trafficgen_machines.csv – configuration

##### Key Insights from README
- Four actor types: Validators, Miners, Traffic Generators (tgens), King
- GRE/IPIP multi-level tunneling for overlay network (10.0.0.0/8) on top of physical 10.0.0.0/24
- Round orchestration: immutable scripts + round_manager coordinate traffic patterns and metric collection
- Security: whitelist-agent enforces least privilege over SSH
- Reward function: 4 weighted metrics (accuracy, efficiency, throughput, latency) implemented in tensorprox/rewards

##### Main Components
A. Physical and Overlay Network
- Physical LAN: nodes with 10.0.0.0/24 addresses
- GRE tunnels between tgens ↔ Moat ↔ King
- IPIP overlay for end-to-end addressing (e.g., 10.200.77.x and 10.0.0.1)

B. Logical Services
1. Traffic Generator Service (tensorprox/core/traffic_generator.py)
2. Miner/Moat Service (neurons/miner.py + tensorprox/base/miner.py + core scripts)
3. Validator Service (neurons/validator.py + tensorprox/base/validator.py + round_manager)
4. Whitelist-Agent (tensorprox/core/immutable/restrict.sh + protocol enforcement)
5. Scoring Engine (tensorprox/rewards/)

C. Data Stores
- storage/state.npz for per-round state
- Local pickled ML model under model/ for packet classification

D. Bittensor Network Integration
- Configuration and key distribution via tensorprox/utils/config.py
- Global variables and settings

##### Component Interactions and Data Flow
1. Validator triggers a new round via round_manager
2. Traffic generators produce benign + attack packets (GRE, IPIP) → Moat
3. Moat processes packets using AF_XDP ML model → forwards or drops
4. King node receives filtered traffic
5. Metrics (benign delivered, attack mitigated, latency) are sent back to validator
6. Scoring engine computes reward, updates storage/state.npz
7. Validator enforces security via whitelist-agent for all remote scripts

##### Architectural Patterns and Design Principles
- Modular “Neuron” abstraction: miners and validators inherit from base Neuron class
- Immutable, repeatable script flows for each validation round (in core/immutable)
- Separation of concerns: transport (GRE/IPIP), compute (Moat/ML model), orchestration (round_manager), security (whitelist-agent)
- Plug-and-play extensibility: add new traffic patterns or scoring metrics under core/immutable or rewards/

##### Essential Elements to Capture
- GRE/IPIP tunnel endpoints with their subnets
- Round lifecycle scripts (initial_setup.sh, challenge.sh, traffic_generator.py, lockdown.sh)
- Reward computation pipeline (tensorprox/rewards/*.py)
- Whitelist-agent enforcement point on remote SSH
- ML model lookup in the Moat

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Network Topology Layer
    subgraph "Network Topology" 
        direction TB
        Physical["Physical LAN\n10.0.0.0/24"]:::net
        Overlay["Overlay Network\n10.0.0.0/8"]:::net
        Physical --> Overlay
        Tgen["Tgen-0\n10.1.3.71"]:::tg
        Moat["Moat Node\n10.1.3.141"]:::miner
        King["King\n10.1.3.23"]:::king
        Overlay --> Tgen
        Tgen -->|"GRE/IPIP traffic"| Moat
        Moat -->|"filtered traffic"| King
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        TrafficGenSvc["Traffic Generator Service"]:::tg
        MinerSvc["Miner/Moat Service"]:::miner
        ValidatorSvc["Validator Controller"]:::validator
        WhitelistAgent["Whitelist-Agent"]:::validator
        subgraph "Scoring Engine"
            direction TB
            Reward["reward.py"]:::validator
            Scoring["scoring.py"]:::validator
            WeightSetter["weight_setter.py"]:::validator
        end
        TrafficGenSvc -->|"produces traffic"| Tgen
        MinerSvc -->|"processes packets"| Moat
        ValidatorSvc -->|triggers round| TrafficGenSvc
        ValidatorSvc -->|orchestrates| MinerSvc
        ValidatorSvc -->|enforces| WhitelistAgent
        King -->|"sends metrics"| ValidatorSvc
        ValidatorSvc -->|computes reward| Scoring
        Scoring --> Reward
        Scoring --> WeightSetter
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        StateStore["state.npz"]:::datastore
        subgraph "ML Model Artifacts"
            direction TB
            DT["decision_tree.pkl"]:::datastore
            Imputer["imputer.pkl"]:::datastore
            Scaler["scaler.pkl"]:::datastore
        end
        Scoring --> StateStore
        Moat --> DT
    end

    %% Bittensor Integration & Utilities
    subgraph "Bittensor & Utils"
        direction TB
        Config["config.py"]:::utils
        Globals["global_vars.py"]:::utils
        Settings["settings.py"]:::utils
    end
    ValidatorSvc --> Config
    ValidatorSvc --> Globals
    ValidatorSvc --> Settings

    %% Click Events
    click TrafficGenSvc "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/core/immutable/traffic_generator.py"
    click MinerSvc "https://github.com/shugo-labs/tensorprox/blob/main/neurons/miner.py"
    click MinerSvc "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/base/miner.py"
    click MinerSvc "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/core/immutable/gre_setup.py"
    click ValidatorSvc "https://github.com/shugo-labs/tensorprox/blob/main/neurons/validator.py"
    click ValidatorSvc "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/base/validator.py"
    click ValidatorSvc "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/core/round_manager.py"
    click WhitelistAgent "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/core/restrict.sh"
    click Reward "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/rewards/reward.py"
    click Scoring "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/rewards/scoring.py"
    click WeightSetter "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/rewards/weight_setter.py"
    click StateStore "https://github.com/shugo-labs/tensorprox/blob/main/storage/state.npz"
    click DT "https://github.com/shugo-labs/tensorprox/blob/main/model/decision_tree.pkl"
    click Imputer "https://github.com/shugo-labs/tensorprox/blob/main/model/imputer.pkl"
    click Scaler "https://github.com/shugo-labs/tensorprox/blob/main/model/scaler.pkl"
    click Config "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/utils/config.py"
    click Globals "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/global_vars.py"
    click Settings "https://github.com/shugo-labs/tensorprox/blob/main/tensorprox/settings.py"

    %% Styles
    classDef tg fill:#add8e6,stroke:#000
    classDef miner fill:#90ee90,stroke:#000
    classDef validator fill:#ff7f7f,stroke:#000
    classDef king fill:#ffa500,stroke:#000
    classDef datastore fill:#d3d3d3,stroke:#000
    classDef utils fill:#f0f0f0,stroke:#000
    classDef net fill:#e0e0ff,stroke:#000
```
