# derek2403/teetee architecture

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

- Interactive diagram: https://gitdiagram.com/derek2403/teetee
- Repository: https://github.com/derek2403/teetee

## Overview

##### Main Components
- Frontend Application:
- Represented by Next.js pages (e.g., pages/index.js, pages/chat.js, pages/ContributionPool.js) and UI components (found in the components folder).
- Hooks that manage blockchain calls and on-chain operations (e.g., useAIChat, useDepositToPool, etc.).
- Backend API:
- Captured in the pages/api folder (endpoints like /api/host-model.js, /api/models.js, /api/tee-nodes.js, etc.) which handle requests, model verification, and act as gatekeepers between the frontend and other layers.
- Dockerized TEE Nodes:
- The “TeeDockerFiles” directory houses Docker configurations and code for the two applications (app1 and app2) that handle distinct parts of the LLM inference process.
- Blockchain & Smart Contract Layer:
- Although the smart contract source code might not be directly in the file tree, the README clearly explains that on-chain interactions (profit sharing, token transactions, node registration, and attestations) are a core part of the system.
- Base Sepolia smart contract as an external blockchain component that interacts with both the frontend (for token buying, wallet interactions) and backend APIs.
- External Services:
- Phala Network as the TEE hosting platform provider (and on-chain attestation proofs) and any related external URLs (e.g., Phala Cloud, blockchain explorers).
- Additional Supporting Components:
- Tools like Ethers.js for blockchain interactions, Docker for containerization, and smart contract calls provide the integration glue.

##### Relationships and Data Flows
- The backend API then interfaces with the blockchain by invoking Ethers.js functions and calling smart contract methods for registration and profit sharing.
- Within the TEE layer:
- TEE Node 1 (from TeeDockerFiles/app1) receives user requests (via a user-facing endpoint such as /generate at port 5002) and processes input.
- It then forwards processed tensor data over to TEE Node 2 (from TeeDockerFiles/app2, typically at port 5001) for final output generation.
- On-chain attestation paths:
- That both TEE nodes generate attestation reports which are sent back through TEE 1 to the user, and these reports are also verified against the smart contract.
- For the self-hosting user flow:
- For token-based usage:
- Purchase of tokens (using wallet integrations such as RainbowKit) that are then used to generate queries via the API.

##### Architectural Patterns and Technologies
- Separation of Concerns:
- Clearly segregate frontend, backend, TEE container logic, and blockchain components.
- Microservices / Containerization:
- Use Docker container symbols for TEE nodes, showing that each service is isolated.
- Decentralized and Fault-Tolerant Design:
- As indicated in the README, highlight the dual flows (normal user vs. self-hosting) and represent the fail-over paths (e.g., multiple TEE nodes in future horizontal scalability).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global nodes
    User("User"):::user

    %% Frontend Layer
    subgraph "Frontend (Next.js UI)"
        FP["Next.js Pages"]:::frontend
        FC["UI Components"]:::frontend
        FH["Hooks"]:::frontend
        CP["Contribution Pool Page"]:::frontend
    end

    %% Backend API Layer
    subgraph "Backend API"
        BA["API Endpoints"]:::backend
    end

    %% TEE Layer (Docker Containers)
    subgraph "TEE Layer (Docker Containers)"
        T1["TEE Node 1 (App1)"]:::tee
        T2["TEE Node 2 (App2)"]:::tee
    end

    %% Blockchain & Smart Contract Layer
    BSC["Blockchain & Smart Contract"]:::blockchain

    %% External Services & Tools
    subgraph "External Services & Tools"
        ES["Phala Network"]:::external
        UT["Utils Tools"]:::external
    end

    %% Data Flow Connections
    User -->|"HTTP/REST"| FP
    FP -->|"API_call"| BA
    BA -->|"Forward_request"| T1
    T1 -->|"Process_and_forward"| T2
    T2 -->|"GenerateAttestation_Response"| T1
    T1 -->|"Return_response"| BA
    BA -->|"Response"| FP
    FP -->|"DisplayOutput"| User

    %% Blockchain interactions
    BA -->|"SmartContractCalls"| BSC
    FP -->|"WalletInteractions"| BSC
    CP -->|"RegisterTEENode"| BSC

    %% External services and integration
    BA -->|"EthersJS_Interaction"| UT
    UT -->|"SmartContractInteraction"| BSC
    T1 -->|"AttestationReport"| ES
    T2 -->|"AttestationReport"| ES
    ES -->|"VerifyAttestation"| BSC

    %% Click Events
    click FP "https://github.com/derek2403/teetee/blob/main/pages/index.js"
    click FC "https://github.com/derek2403/teetee/tree/main/components"
    click FH "https://github.com/derek2403/teetee/blob/main/hooks/useAIChat.js"
    click CP "https://github.com/derek2403/teetee/blob/main/pages/ContributionPool.js"
    click BA "https://github.com/derek2403/teetee/tree/main/pages/api"
    click T1 "https://github.com/derek2403/teetee/tree/main/TeeDockerFiles/src/app1"
    click T2 "https://github.com/derek2403/teetee/tree/main/TeeDockerFiles/src/app2"
    click BSC "https://github.com/derek2403/teetee/tree/main/smartcontract"
    click UT "https://github.com/derek2403/teetee/tree/main/utils"

    %% Styles
    classDef frontend fill:#CCF,stroke:#000,stroke-width:1px;
    classDef backend fill:#CFC,stroke:#000,stroke-width:1px;
    classDef tee fill:#F99,stroke:#000,stroke-width:1px;
    classDef blockchain fill:#F9C,stroke:#000,stroke-width:1px,stroke-dasharray: 5 5;
    classDef external fill:#C9F,stroke:#000,stroke-width:1px;
    classDef user fill:#FFC,stroke:#000,stroke-width:1px;
```
