Setting Up the Development Environment

Prerequisites
  • Operating System: Linux (Ubuntu) or macOS (Windows is possible but more complex).
  • Docker: Containerization platform.
  • Docker Compose: Tool for defining and running multi-container Docker applications.
  • Go: Programming language required for Fabric chaincode development.
  • Node.js: For developing client applications.

I) Installation

Install Docker:

sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER

Install Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Install Go:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Install Node.js:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

II) Downloading and Setting Up Hyperledger Fabric

Download the Fabric Binaries and Docker Images:

curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.3.0 1.5.0

Add Fabric binaries to PATH:

export PATH=$PATH:$HOME/fabric-samples/bin

III) Creating a Hyperledger Fabric Network

Navigate to the fabric-samples directory:

cd fabric-samples/test-network

Start the Network:

./network.sh up createChannel -c mychannel -ca

Deploy a Sample Chaincode:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

IV) Developing Chaincode

Create a New Chaincode Directory:

mkdir -p chaincode/mychaincode/go
cd chaincode/mychaincode/go

Write Chaincode in Go:

// main.go
package main

import (
    "github.com/hyperledger/fabric-contract-api-go/contractapi"
)

type SmartContract struct {
    contractapi.Contract
}

func main() {
    chaincode, err := contractapi.NewChaincode(new(SmartContract))
    if err != nil {
panic(err.Error())
    }
    if err := chaincode.Start(); err != nil {
panic(err.Error())
    }
}

Build the Chaincode:

GO111MODULE=on go mod vendor

V) Deploying Custom Chaincode

Package the Chaincode:

peer lifecycle chaincode package mychaincode.tar.gz
--path ./chaincode/mychaincode/go
--lang golang
--label mychaincode_1

Install the Chaincode on Peers:

peer lifecycle chaincode install mychaincode.tar.gz

Approve the Chaincode Definition:

peer lifecycle chaincode approveformyorg
-o localhost:7050
--ordererTLSHostnameOverride orderer.example.com
--channelID mychannel
--name mychaincode
--version 1.0
--sequence 1
--tls
--cafile $ORDERER_CA

Commit the Chaincode Definition:

peer lifecycle chaincode commit
-o localhost:7050
--ordererTLSHostnameOverride orderer.example.com
--channelID mychannel
--name mychaincode
--version 1.0
--sequence 1
--tls
--cafile $ORDERER_CA

VI) Developing Client Application

Navigate to Application Directory:

cd fabric-samples/asset-transfer-basic/application-javascript

Install Dependencies:

npm install

Run the Application:

node app.js

VII) Interacting with the Network

Invoke Chaincode:

peer chaincode invoke
-o localhost:7050
--ordererTLSHostnameOverride orderer.example.com
--tls
--cafile $ORDERER_CA
-C mychannel
-n mychaincode
-c '{"function":"CreateAsset","Args":["asset1","blue","5","Tom","1000"]}'

Query Chaincode:

peer chaincode query
-C mychannel
-n mychaincode
-c '{"Args":["ReadAsset","asset1"]}'

VIII) Shutting Down the Network

Stop the Network:

./network.sh down

Summary

You have now set up a Hyperledger Fabric application by:

  1. Setting up the development environment.
  2. Downloading and configuring Hyperledger Fabric.
  3. Creating and starting a network.
  4. Developing and deploying chaincode.
  5. Developing a client application.
  6. Interacting with the blockchain network.
  7. Shutting down the network when done.

By following these steps, you can establish a complete Hyperledger Fabric application suitable for development and testing purposes.