# wefantasy/fabriclearn architecture

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

- Interactive diagram: https://gitdiagram.com/wefantasy/fabriclearn
- Repository: https://github.com/wefantasy/fabriclearn

## Overview

##### Project Type and Purpose
- This is an educational, full-stack Hyperledger Fabric learning project (“FabricLearn”) that incrementally demonstrates how to deploy and operate a permissioned blockchain network. It packages each stage of the network lifecycle (from test-network to multi-host deployment, dynamic org/peer updates, CouchDB integration, SDK/Gateway usage, complex contracts) into self-contained directories with Docker Compose, configuration, scripts, chaincode, and client code.

##### File-Structure Analysis
- Top-level directories (`0_TestNetworkExplain`, `1_3Org2Peer1Orderer1TLS`, …, `7_CouchDBAndComplexContract`) each encapsulate one “stage” of network topology and feature set.
- Within each stage:
- `compose/` → Docker Compose YAMLs to launch CAs, orderers, peers, CouchDB (when used).
- `config/` → Fabric configtx and core/orderer configuration files.
- `envpeer*` → CLI environment variable scripts for each peer.
- `*.sh` scripts (`0_Restart.sh`, `1_RegisterUser.sh`, …) orchestrate the network lifecycle.
- Chaincode directories (`asset-transfer-basic-go`, `project_contract`, etc.) hold smart contracts and their unit tests.
- Later stages add `contract-gateway` and `contract-sdk` Go applications as clients.

##### Key Components
a. Certificate Authorities (CAs)
- A root TLS-CA (e.g., `council.fantasy.com`) issuing TLS certs for all nodes.
- Org-specific CAs (`soft.fantasy.com`, `hard.fantasy.com`, `web.fantasy.com`, `orderer.fantasy.com`) issuing identity certs (MSP) for peers, orderers, admins, users.
b. Ordering Service
- Solo or Raft cluster of orderer nodes (often managed by the “council” org).
c. Peer Nodes
- One peer per org (`soft`, `hard`, `web`), each with:
- MSP (identity materials)
- TLS materials
- CouchDB state database (in stages that require rich queries)
d. Chaincode Containers
- Deployed and instantiated by peers, executing business logic and persisting state.
e. Client-side Components
- CLI containers/scripts (Bash + `peer` CLI) that invoke lifecycle commands.
- Go Gateway application (`contract-gateway`) and Go SDK application (`contract-sdk`) illustrating programmatic access.
f. Supporting Artifacts
- Configuration transactions (`configtx.yaml`, channel genesis blocks).
- Docker networks facilitating inter-container communication.

##### Relationships and Interactions
1. CA → Issues TLS certs and identity certs to Orderer and Peer nodes, Admins, Users.
2. CLI / Client App → Enrolls with CA to obtain certificates → Uses `peer` CLI or SDK/Gateway to:
a. Submit channel creation and join requests to Orderer.
b. Install and instantiate chaincode on Peers.
c. Submit endorsement proposals to Peers → Peers endorse → Client collects endorsements → Broadcasts transaction to Orderer → Orderer orders blocks.
d. Peers validate and commit blocks, update CouchDB (if enabled).
3. Docker Compose services connect via defined Docker networks.
4. Scripts sequence the above operations in each stage.

##### Architectural Patterns and Design Principles
- Micro-service style: each network node (CA, Peer, Orderer, CouchDB) runs in its own container with clear responsibilities.
- Infrastructure-as-code: all network topology and lifecycle steps are codified in Bash scripts and YAML.
- Separation of concerns:
- Certificates (CAs) vs. ledger services (Peers/Orderers) vs. state database (CouchDB) vs. client apps.
- Network configuration (configtx, core/orderer yaml) separated from runtime data (MSP, ledger).
- Incremental complexity: tutorials progress from a basic test network to multi-host, dynamic org addition/removal, complex contracts, and SDK usage.

##### Final Recommendations
- Incrementally annotate additional features per tutorial stage.
- Every component from the file tree (compose files, config, chaincode, clients) is represented or noted in callouts.
- This level of detail will ensure the principal software engineer sees exactly how each directory, script, and container maps to the running Fabric network.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Council Org"
        CouncilTLSCA["TLS CA"]:::caGreen
        CouncilCA["CA (council)"]:::caGreen
        OrdererCluster["Orderer Cluster"]:::ordererBlue
    end

    subgraph "Soft Org"
        SoftCA["CA (soft)"]:::caGreen
        PeerSoft["Peer0.soft"]:::peerOrange
        CCSoft["Chaincode Container"]:::ccPurple
    end

    subgraph "Hard Org"
        HardCA["CA (hard)"]:::caGreen
        PeerHard["Peer0.hard"]:::peerOrange
    end

    subgraph "Web Org"
        WebCA["CA (web)"]:::caGreen
        PeerWeb["Peer0.web"]:::peerOrange
        CouchDBWeb["CouchDB"]:::dbGrey
    end

    CLI["CLI Scripts"]:::clientYellow
    Gateway["Go Gateway App"]:::clientYellow
    SDK["Go SDK App"]:::clientYellow

    %% Flows
    CouncilTLSCA -->|"issues TLS certs"| OrdererCluster
    CouncilTLSCA -->|"issues TLS certs"| PeerSoft
    CouncilTLSCA -->|"issues TLS certs"| PeerHard
    CouncilTLSCA -->|"issues TLS certs"| PeerWeb

    CouncilCA -->|"issues MSP identities"| OrdererCluster
    SoftCA -->|"issues MSP identities"| PeerSoft
    HardCA -->|"issues MSP identities"| PeerHard
    WebCA -->|"issues MSP identities"| PeerWeb

    CLI -->|"enroll & invoke"| PeerSoft
    CLI -->|"enroll & invoke"| PeerHard
    CLI -->|"enroll & invoke"| PeerWeb
    CLI -->|"broadcast tx"| OrdererCluster

    PeerSoft -->|"endorse"| CCSoft
    PeerHard -->|"endorse"| CCSoft
    PeerWeb -->|"endorse"| CCSoft

    PeerWeb -->|"read/write state"| CouchDBWeb

    Gateway -->|"SDK calls"| PeerSoft
    SDK -->|"SDK calls"| PeerSoft

    %% Click Events
    click CouncilTLSCA "https://github.com/wefantasy/fabriclearn/tree/main/5_FabricNetworkByMultiHost/orgs/council.ifantasy.net/ca/crypto"
    click CouncilCA "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/orgs/council.ifantasy.net/ca/crypto/fabric-ca-server-config.yaml"
    click SoftCA "https://github.com/wefantasy/fabriclearn/tree/main/5_FabricNetworkByMultiHost/orgs/soft.ifantasy.net/ca/crypto"
    click HardCA "https://github.com/wefantasy/fabriclearn/tree/main/5_FabricNetworkByMultiHost/orgs/hard.ifantasy.net/ca/crypto"
    click WebCA "https://github.com/wefantasy/fabriclearn/tree/main/5_FabricNetworkByMultiHost/orgs/web.ifantasy.net/ca/crypto"
    click OrdererCluster "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/compose/docker-compose.yaml"
    click OrdererCluster "https://github.com/wefantasy/fabriclearn/blob/main/4-2_RunOrdererByCouncil/compose/docker-compose.yaml"
    click OrdererCluster "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/config/orderer.yaml"
    click PeerSoft "https://github.com/wefantasy/fabriclearn/tree/main/1_3Org2Peer1Orderer1TLS/envpeer1soft"
    click PeerWeb "https://github.com/wefantasy/fabriclearn/tree/main/1_3Org2Peer1Orderer1TLS/envpeer1web"
    click PeerHard "https://github.com/wefantasy/fabriclearn/tree/main/2_FabricNetworkUpdate/envpeer1hard"
    click PeerSoft "https://github.com/wefantasy/fabriclearn/tree/main/2_FabricNetworkUpdate/envpeer1soft"
    click PeerWeb "https://github.com/wefantasy/fabriclearn/tree/main/2_FabricNetworkUpdate/envpeer1web"
    click PeerSoft "https://github.com/wefantasy/fabriclearn/tree/main/2_FabricNetworkUpdate/envpeer2soft"
    click PeerSoft "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/config/core.yaml"
    click CouchDBWeb "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/compose/docker-compose.yaml"
    click CouchDBWeb "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/config/core.yaml"
    click CCSoft "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/asset-transfer-basic/chaincode-go/chaincode/smartcontract.go"
    click CCSoft "https://github.com/wefantasy/fabriclearn/blob/main/0_TestNetworkExplain/test-network/asset-transfer-basic-go/chaincode/smartcontract.go"
    click CCSoft "https://github.com/wefantasy/fabriclearn/tree/main/1_3Org2Peer1Orderer1TLS/asset-transfer-basic/chaincode-go/chaincode/mocks"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/0_TestNetworkExplain/test-network/1_RunDefaultNetwork.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/0_Restart.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/1_RegisterUser.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/2_EnrollUser.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/3_Configtxgen.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/4_TestChaincode.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/0_Restart.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/1_RegisterUser.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/2_EnrollUser.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/3_Configtxgen.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/4_JoinChannel_hostA.sh"
    click CLI "https://github.com/wefantasy/fabriclearn/blob/main/5_FabricNetworkByMultiHost/5_TestChaincode_hostA.sh"
    click Gateway "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/contract-gateway/app.go"
    click Gateway "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/contract-gateway/connect.go"
    click SDK "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/contract-sdk/app.go"
    click SDK "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/contract-sdk/connection-soft.yaml"
    click SDK "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/contract-sdk/wallet/appUser.id"
    click ProjectContract "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/contract/project.go"
    click ProjectMain "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/main.go"
    click ProjectModel "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/model/base.go"
    click ProjectModel "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/model/project.go"
    click ProjectModel "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/model/user.go"
    click ProjectContract "https://github.com/wefantasy/fabriclearn/blob/main/7_CouchDBAndComplexContract/project_contract/tools/contract.go"
    click ConfigTX "https://github.com/wefantasy/fabriclearn/blob/main/2_FabricNetworkUpdate/config/configtx.yaml"
    click ConfigTX "https://github.com/wefantasy/fabriclearn/blob/main/6_ContractGatewayAndSDK/config/configtx.yaml"
    click ConfigMSP "https://github.com/wefantasy/fabriclearn/blob/main/1_3Org2Peer1Orderer1TLS/config/config-msp.yaml"

    %% Styles
    classDef caGreen fill:#b8e986,stroke:#4caf50,color:#000,shape:hexagon
    classDef ordererBlue fill:#add8e6,stroke:#2196f3,color:#000
    classDef peerOrange fill:#ffcc99,stroke:#fb8c00,color:#000
    classDef dbGrey fill:#d3d3d3,stroke:#757575,color:#000,shape:cylinder
    classDef ccPurple fill:#e1bee7,stroke:#9c27b0,color:#000
    classDef clientYellow fill:#fff59d,stroke:#fdd835,color:#000,shape:parallelogram
```
