# chandanneralgi/decentralized-file-sharing-system architecture

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

- Interactive diagram: https://gitdiagram.com/chandanneralgi/decentralized-file-sharing-system
- Repository: https://github.com/chandanneralgi/decentralized-file-sharing-system

## Overview

##### Main Components
- Two Node Instances: From the file tree you see “client_server_1” and “main_server”, each representing an independent node running a similar server with a blockchain module. Label them (e.g., “Node A” and “Node B”).
- Blockchain Module: Each node contains a blockchain.py file; this module is responsible for handling block creation, including block index, timestamp, proof, previous hash, and file sharing data (sender, receiver, file hash).
- Server Component: Both directories include a server.py file, which likely manages HTTP endpoints (for file upload/download) and peer-to-peer connections via socket programming.
- Encryption & Constants: The my_constants.py file in each node defines key settings, and the code uses AES for encryption and SHA-256 for hashing.
- User Interface (UI): The static and templates folders in each node show the existence of front-end assets (HTML, CSS, JS) for connecting to the blockchain, uploading files, and downloading files.
- External IPFS Service: Though not a local directory item, the README explains integration with IPFS for storing the actual files so that the blockchain only holds the file’s hash.
- P2P Networking: The peer-to-peer communication (via socket programming) is critical to maintaining synchronized blockchain state across nodes, as explained in the “Creating the Peer to Peer Network” section of the README.

##### Relationships and Interactions
- Each node’s server handles incoming HTTP requests from a user interface that serves upload/download pages.
- When a file is uploaded, the following happens:
- The file is encrypted with AES using a file key.
- It is sent to IPFS for decentralized storage.
- The blockchain module creates a new block that includes the file’s IPFS hash along with sender/receiver data.
- Nodes communicate via sockets to update and share the current state of the blockchain across the network (the P2P network).
- For file download, a valid file key decrypts the file retrieved from IPFS using its hash stored on the blockchain.

##### Important Architectural Patterns and Design Principles
- Decentralization: The system uses peer-to-peer networking to maintain a decentralized and replicated blockchain among nodes.
- Layered Architecture: Separate layers for presentation (UI templates & static assets), application logic (server.py and blockchain.py), and external services (IPFS).
- Security by Design: Incorporation of SHA-256 and AES encryption demonstrates a commitment to security (e.g., linking a block’s integrity and ensuring file privacy).
- Loose Coupling: The blockchain module and IPFS integration are decoupled from each node’s server logic, providing modular segments that can work or be scaled independently.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    UserBrowser["User Browser"]:::user
    IPFS["IPFS Storage Service"]:::external

    %% Node A Subgraph
    subgraph "Node A (client_server_1)"
        subgraph "UI Layer"
            NodeA_UI_Templates["HTML/CSS/JS Templates"]:::ui
            NodeA_UI_Static["Static Assets"]:::ui
        end
        NodeA_Server["Server Component"]:::server
        NodeA_Encryption["Encryption & Constants"]:::security
        NodeA_Blockchain["Blockchain Module"]:::blockchain
    end

    %% Node B Subgraph
    subgraph "Node B (main_server)"
        subgraph "UI Layer"
            NodeB_UI_Templates["HTML/CSS/JS Templates"]:::ui
            NodeB_UI_Static["Static Assets"]:::ui
        end
        NodeB_Server["Server Component"]:::server
        NodeB_Encryption["Encryption & Constants"]:::security
        NodeB_Blockchain["Blockchain Module"]:::blockchain
    end

    %% Data Flows from User Browser to Nodes
    UserBrowser -->|"access"| NodeA_UI_Templates
    UserBrowser -->|"access"| NodeB_UI_Templates

    %% Internal flow in Node A
    NodeA_UI_Templates -->|"render_request"| NodeA_Server
    NodeA_UI_Static --- NodeA_Server
    NodeA_Server -->|"initiates_encryption"| NodeA_Encryption
    NodeA_Encryption -->|"encrypt&upload"| IPFS
    IPFS -->|"return_hash"| NodeA_Server
    NodeA_Server -->|"create_block"| NodeA_Blockchain

    %% P2P Communication between Node A and Node B Blockchain Modules
    NodeA_Blockchain <-->|"P2P_Sync"| NodeB_Blockchain

    %% Styles
    classDef user fill:#f9f,stroke:#333,stroke-width:2px;
    classDef server fill:#bbf,stroke:#333,stroke-width:2px;
    classDef blockchain fill:#afa,stroke:#333,stroke-width:2px;
    classDef security fill:#faa,stroke:#333,stroke-width:2px;
    classDef ui fill:#ffd,stroke:#333,stroke-width:2px;
    classDef external fill:#ccf,stroke:#333,stroke-width:2px;

    %% Click Events for Node A
    click NodeA_Server "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/client_server_1/server.py"
    click NodeA_Blockchain "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/client_server_1/blockchain.py"
    click NodeA_Encryption "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/client_server_1/my_constants.py"
    click NodeA_UI_Templates "https://github.com/chandanneralgi/decentralized-file-sharing-system/tree/main/client_server_1/templates"
    click NodeA_UI_Static "https://github.com/chandanneralgi/decentralized-file-sharing-system/tree/main/client_server_1/static"

    %% Click Events for Node B
    click NodeB_Server "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/main_server/server.py"
    click NodeB_Blockchain "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/main_server/blockchain.py"
    click NodeB_Encryption "https://github.com/chandanneralgi/decentralized-file-sharing-system/blob/main/main_server/my_constants.py"
    click NodeB_UI_Templates "https://github.com/chandanneralgi/decentralized-file-sharing-system/tree/main/main_server/templates"
    click NodeB_UI_Static "https://github.com/chandanneralgi/decentralized-file-sharing-system/tree/main/main_server/static"
```
