Fabcar
Fabcar
Create and deploy a blockchain network using Hyperledger Fabric SDK for go Set up and
initialize the channel, install and instantiate chain code, and perform invoke and query on
your blockchain network.
Aim:
To demonstrate how to create and deploy a Hyperledger Fabric blockchain network using
the Hyperledger Fabric for go set up . The goal is to set up the network, initialize the channel, install
and instantiate chaincode, and interact with the blockchain network by invoking transactions and
querying the ledger.
Procedure
1. Prerequisites
Install the required dependencies:
Git - sudo apt install git
cURL - sudo apt install curl
Go - wget https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzvf go1.17.3.linux-amd64.tar.gz
Jq - sudo apt install jq
Docker - sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
or sudo apt install docker*
Docker – compose - sudo apt install docker- compose
sudo chmod +x /usr/local/bin/docker-compose
Check all the dependencies version using a command
<dependency name> --version
2. Install Hyperledger Fabric 2.2
Install Samples, Binaries, and Docker images
Determine a location on your machine where you want to place the fabric-samples
repository and enter that directory in a terminal window.
mkdir fabric
cd fabric
Check the Docker status
sudo systemctl status docker
Add into the docker
sudo usermod -aG docker <batchno> sce251014
sudo groupadd docker or newgrp dockernewgrp docker
Pull the Hyperledger fabric images in Docker
curl -sSL https://bit.ly/2ysbOFE | bash -s -- <fabric_version> <fabric-ca_version>
Example:
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.4 1.5.2
Set up the enviornment Variables – Load the Hyperledger Fabric samples
export PATH=<path to download location>/bin:$PATH or
export PATH=/home/mohanapriya-cse/fabric/fabric-samples/bin:$PATH
(home/mohanapriya-cse/fabric/fabric-samples ) - use your path as per your login
Navigate to the directory where you have installed fabric samples.
cd fabric-samples
cd test-network
./network.sh up
Monitor the Containers
docker ps
Create Channel
./network.sh createChannel
Creates a channel with name "mychannel" or ./network.sh createChannel -c <channel
name>
Install Chaincode
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
Invoke Chaincode – Configure Peer
Type the Command in this file :
#export GOROOT=/usr/local/go
#export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
xport PATH=/home/mohanapriya-cse/fabric/fabric-samples/bin:$PATH
export PATH=$PATH:/home/mohanapriya-cse/fabric/fabric-samples/bin
export FABRIC_CFG_PATH=/home/mohanapriya-cse/fabric/fabric-samples/config
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
Set environment variables for Org1
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export
CORE_PEER_TLS_ROOTCERT_FILE=/home/mohanapriya-cse/fabric/fabric-
samples/test-network/organizations/peerOrganizations/org1.example.com/peers/
peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/home/mohanapriya-cse/fabric/fabric-
samples/test-network/organizations/peerOrganizations/org1.example.com/users/
[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
Run the bash file to
source ~/.bashrc
Start the Peer - Once the environment is set, start the peer using:
peer node start
Once the peer is started successfully, you can verify its status using:
peer node status
Verify That the Peer Is Running
docker ps | grep peer
Invoke Chaincode
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride
orderer.example.com --tls --cafile /home/mohanapriya-cse/fabric/fabric-samples/test-
network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/
msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses
localhost:7051 --tlsRootCertFiles /home/mohanapriya-cse/fabric/fabric-samples/test-
network/organizations/peerOrganizations/org1.example.com/peers/
peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles
/home/mohanapriya-cse/fabric/fabric-samples/test-network/organizations/
peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c
'{"function":"InitLedger","Args":[]}'
Query Chaincode
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
# Read the Asset
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}’
# Transfer the Asset
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride
orderer.example.com --tls --cafile /home/mohanapriya-cse/fabric/fabric-samples/test-
network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/
msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses
localhost:7051 --tlsRootCertFiles /home/mohanapriya-cse/fabric/fabric-samples/test-
network/organizations/peerOrganizations/org1.example.com/peers/
peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles
/home/mohanapriya-cse/fabric/fabric-samples/test-network/organizations/
peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c
'{"function":"TransferAsset","Args":["asset6","priya"]}'
#Read the asset after transactions It has been changed
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}’