blockchain-trace-fabric

repository·master·Indexed 19 days ago

https://github.com/steakliu/blockchain-trace-fabric

An agricultural product traceability system built on Hyperledger Fabric 1.2. It provides a full-stack solution including a blockchain network, a PC-side management interface (RuoYi-Vue), and a WeChat Mini Program for end-user traceability. The system tracks products through five organizational nodes: Farmers, Raw Material Suppliers, Manufacturers, Logistics Providers, and Retailers, ensuring data transparency and immutability via Golang smart contracts.

Tokens
11K
Snippets
45
Records
53
Agent score
66%

What's inside blockchain-trace-fabric

  1. Overview of the blockchain-trace-fabric project

    master

    This project is an agricultural product traceability system based on Hyperledger Fabric. It consists of multiple components including a Fabric network, a web application, and mobile applets (mini-programs).

    Core Components:

    • trace: The mobile mini-program (小程序) used for end-user interaction.
    • ruoyi: The PC-side management interface.
    • webapp: The web application component.
    • fabcar: A package related to the system.
    • Fabric Network: The underlying blockchain infrastructure.
  2. Overview of the Fabric-based Agricultural Traceability System

    master

    This project is a blockchain-based traceability system for agricultural products using Hyperledger Fabric. It tracks products through five organizational nodes: Farmers, Raw Material Suppliers, Manufacturers, Logistics Providers, and Retailers. Every step of the process is recorded on the blockchain to ensure transparency and immutability.

    Core Modules

    • blockchain-trace-bcnetwork: The Hyperledger Fabric blockchain network.
    • blockchain-trace-applets: The WeChat Mini Program (H5) client for end-user traceability.
    • blockchain-trace-pc: The PC-side management interface (built with RuoYi-Vue).
    • blockchain-trace-basic-data: The system's basic data backend (built with RuoYi/SpringBoot).

    Technology Stack

    • Blockchain: Fabric 1.2 (Smart Contracts in Golang)
    • Frontend: Vue.js, Element UI, mpvue
    • Backend: SpringBoot, Mybatis, FastDFS, Node.js, Redis, MySQL
    • Environment: Ubuntu 16.04 64-bit (Recommended: 2 Cores, 4GB+ RAM), Docker, Docker-compose
  3. Trace Products via PC or Mini Program

    master

    End-users can verify product authenticity through two main interfaces:

    • PC Traceability Interface: A web-based dashboard for detailed product history.
    • Mini Program (小程序) Traceability: A mobile-optimized interface designed for consumers to scan QR codes, view traceability data, and provide product evaluations.
  4. Understand the Agricultural Traceability System Workflow

    master

    The system provides a complete lifecycle for agricultural product traceability using Hyperledger Fabric. The workflow follows these stages:

    1. Crop Addition: Planning land plots and using IoT devices to transmit soil/crop metrics.
    2. Planting Process Recording: Logging growth environments and stages to ensure transparency.
    3. Logistics Tracking: Simulating and recording the movement of products through the supply chain.
    4. Raw Material Quality Inspection: Involving inspection agencies to verify raw materials before processing.
    5. Production/Manufacturing: Recording product information and processing details (who processed it and when) onto the blockchain.
    6. Retail/Sign-off: Retailers receive products and generate unique traceability IDs/QR codes.
    7. Consumer Traceability: Consumers use a PC interface or a WeChat Mini Program (小程序) to scan QR codes and view the full history.
  5. Install and Run the PC Client (blockchain-trace-pc)

    master

    The PC client is a Vue.js application used for management.

    1. Install Dependencies

    Use the Taobao registry to ensure reliable package downloads:

    npm install --registry=https://registry.npm.taobao.org

    2. Configure Network Connection

    Open main.js and update Vue.prototype.$httpUrl to point to the IP address of the server where your Node.js middleware is running:

    Vue.prototype.$httpUrl = "http://<YOUR_SERVER_IP>:8080/route";

    3. Start the Development Server

    npm run dev
    npm install --registry=https://registry.npm.taobao.org
    # Edit main.js to set Vue.prototype.$httpUrl
    npm run dev
  6. Set up the Hyperledger Fabric Network

    master

    Follow these steps to deploy the blockchain network layer.

    1. Prerequisites

    Ensure the following are installed on your Ubuntu environment:

    • Node.js 12
    • Docker & Docker-compose
    • Redis
    • FastDFS
    • MySQL 8
    • Go language environment

    2. Pull and Tag Docker Images

    Run the following commands to pull the required Fabric 1.2 images and tag them for local use:

    # Pull images
    docker pull hyperledger/fabric-peer:1.2.0 && \
    docker pull hyperledger/fabric-orderer:1.2.0 && \
    docker pull hyperledger/fabric-ca:1.2.0 && \
    docker pull hyperledger/fabric-tools:1.2.0 && \
    docker pull hyperledger/fabric-ccenv:1.2.0 && \
    docker pull hyperledger/fabric-baseimage:0.4.10 && \
    docker pull hyperledger/fabric-baseos:0.4.10 && \
    docker pull hyperledger/fabric-couchdb:0.4.10
    
    # Tag images
    docker tag hyperledger/fabric-peer:1.2.0 hyperledger/fabric-peer && \
    docker tag hyperledger/fabric-orderer:1.2.0 hyperledger/fabric-orderer && \
    docker tag hyperledger/fabric-ca:1.2.0 hyperledger/fabric-ca && \
    docker tag hyperledger/fabric-tools:1.2.0 hyperledger/fabric-tools && \
    docker tag hyperledger/fabric-ccenv:1.2.0 hyperledger/fabric-ccenv && \
    docker tag hyperledger/fabric-baseimage:0.4.10  hyperledger/fabric-baseimage && \
    docker tag hyperledger/fabric-baseos:0.4.10  hyperledger/fabric-baseos && \
    docker tag hyperledger/fabric-couchdb:0.4.10 hyperledger/fabric-couchdb

    3. Initialize the Network

    1. Clone or upload the blockchain-trace-bcnetwork repository to your server.
    2. Navigate to the basic_network directory and run the startup script:
    chmod -R 777 start.sh
    ./start.sh

    4. Initialize Webapp and Node.js Middleware

    1. Navigate to the webapp directory and authorize the startup scripts:
    chmod -R 777 startFarmerCC.sh
    1. Install dependencies:
    npm install

    (Note: Node.js 8 or 12 is recommended to avoid installation issues).

    1. Register users (Admin and Users):
    node enrollAdmin.js
    node registerUser.js

    Troubleshooting: If registerUser.js fails, delete the hfc-key-store directory and try again.

    1. Start the Node.js middleware (connects frontend to Fabric):
    node app.js

    To run the service in the background, use pm2:

    pm install pm2 -g # if not installed
    pm2 start app.js
    pm2 stop app.js
    # Pulling and tagging images
    docker pull hyperledger/fabric-peer:1.2.0 && \
    docker tag hyperledger/fabric-peer:1.2.0 hyperledger/fabric-peer
    
    # Starting the network
    chmod -R 777 start.sh
    ./start.sh
    
    # Starting the middleware
    node app.js
    # or with pm2
    pm2 start app.js
  7. Configure the Basic Data Backend (blockchain-trace-basic-data)

    master

    The blockchain-trace-basic-data module is a SpringBoot project. You must configure the connection strings for Redis, FastDFS, and MySQL before running.

    1. Configure Redis and FastDFS

    Edit application.yml to set your service addresses:

    # redis configuration
    redis:
      host: 127.0.0.1
      port: 6379
      password: 
    
    # FastDFS configuration
    fdfs:
      so-timeout: 1501
      connect-timeout: 601
      thumb-image:
        width: 60
        height: 60
      tracker-list: 127.0.0.1:22122
      address: http://127.0.0.1:8888/

    2. Configure MySQL

    Edit application-druid.yml to set the database connection details:

    # Datasource configuration
    spring:
        datasource:
            type: com.alibaba.druid.pool.DruidDataSource
            driverClassName: com.mysql.cj.jdbc.Driver
            druid:
                master:
                    url: jdbc:mysql://127.0.0.1:3306/blockchain?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                    username: root
                    password: root
    # Example snippet from application.yml
    redis:
      host: 127.0.0.1
      port: 6379
    
    fdfs:
      tracker-list: 127.0.0.1:22122
      address: http://127.0.0.1:8888/
  8. Start the Fabric blockchain network using start.sh

    master

    The start.sh script automates the deployment of a Hyperledger Fabric network using Docker Compose. It performs the following sequence of operations:

    1. Cleanup: Runs docker-compose down to remove existing containers.
    2. Environment Setup: Sets a startup timeout (default 10 seconds) to allow Fabric components to initialize.
    3. Deployment: Runs docker-compose up -d to start the network nodes.
    4. Channel Creation: Creates the tracechannel using a transaction file from ./channel-artifacts/channel.tx.
    5. Peer Onboarding: Joins multiple organization peers (Org1MSP through Org5MSP) to the channel.
    6. Anchor Updates: Updates the channel anchors for each organization using corresponding transaction files in ./channel-artifacts/.

    Note on TLS: While the script contains commented-out logic for TLS-enabled operations, the active execution path uses non-TLS commands for channel creation, joining, and updating.

    #!/bin/bash
    # Run this script from the basic-network directory to initialize the network
    ./start.sh
  9. Enroll an admin user via enrollAdmin.js

    master

    The enrollAdmin.js script is a CLI utility used to enroll an administrative user onto the Hyperledger Fabric network using a Certificate Authority (CA).

    Workflow

    1. Initialize Fabric Client: Sets up a local key-value store and crypto store in the hfc-key-store directory to persist user certificates and keys.
    2. Connect to CA: Connects to the Fabric CA server (defaulting to http://localhost:7054 with CA name ca.trace.com).
    3. Check Persistence: Attempts to load the 'admin' user from the local store. If the user is already enrolled, it loads them from persistence.
    4. Enrollment: If not found locally, it calls the CA server with the enrollmentID and enrollmentSecret to obtain new credentials.
    5. User Creation: Creates a new user object in the Fabric client using the enrollment's private key and certificate, then sets this user as the current context for the client.

    Configuration Details

    • Store Path: ./hfc-key-store (relative to the script).
    • CA URL: http://localhost:7054 (Note: Change to https if TLS is enabled on the CA).
    • CA Name: ca.trace.com.
    • Default Admin Credentials:
      • enrollmentID: admin
      • enrollmentSecret: adminpw
      • mspid: Org1MSP
    node enrollAdmin.js