Hyperledger Fabric v2.1 Installation Guide — Prerequisites & Bring up the Test Network
In this story, we will install Hyperledger Fabric v2.1 on the Linux machine. Everything may be hard when starting the new topic, so I try to collect all you may need in this story.
Let’s start from install the prerequisites which are clearly mentioned in here before Hyperledger Fabric installation;
- Install Git: Install the latest version of Git with the command below.
sudo apt-get install git
- Install cURL: Install the latest version of cURL with the command below.
sudo apt-get install curl
- Install wget: Install the latest version of wget with the command below. It will be needed for downloading Fabric binaries.
sudo apt-get install wget
- Install Docker and Docker Compose: Install the latest version of docker with the command below. Then you add your current user in docker group.
sudo apt-get -y install docker-compose
sudo usermod -aG docker ${USER}===> logout and login are needed after adding docker group, check everything is ok with the command belowid -nG
- Install Go: Install and extract the 1.14.x version of go with the commands below. Then we carry go folders to under /usr/local directory.
wget https://golang.org/dl/go1.14.2.linux-amd64.tar.gz
sudo tar -xvf go1.14.2.linux-amd64.tar.gzsudo chown -R root:root ./go
sudo mv go /usr/local
We need to add our go path to the$PATH
variable but what we add always be wiped when user login. To avoid executing the command at every time we log in, we open the .profile file which is located as $HOME/.profile write the export command to the end of .profile file.
sudo gedit $HOME/.profile
export PATH=$PATH:/usr/local/go/bin
- Install Node.js & NPM: Download and install the Node.js and NPM with the commands below. We can install version 8 (8.9.4 and higher are supported) or version 10 (10.15.3 and higher are supported).
curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
sudo apt-get install nodejs
- Install Python: Install version 2.7 of Python with the commands below. Also, the appropriate version is installing with node.js installation.
sudo apt-get install python
That’s all, we finally installed all we need and we will continue with the installation of the Fabric samples, binaries, and docker images.
Install Samples, Binaries and Docker Images
Now, we will install the Fabric samples, binaries, and docker images with the command below.
curl -sSL http://bit.ly/2ysbOFE | bash -s
To install spesific release, we can pass the version identifiers to curl command such as the following rows.
curl -sSL http://bit.ly/2ysbOFE | bash -s -- <fabric_version> <fabric-ca_version> <thirdparty_version>curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.1 1.4.1 0.4.15
The fabric-samples/bin folder keeps all of the platform-specific binaries you will need to set up your network and place them into the cloned repo and it looks like the image below after the curl command.
We can check the images with the command below.
docker images
Test the Hyperledger Fabric Network
Since we have successfully installed everything we need, we can deploy and test the Hyperledger Fabric Network by using the scripts.
- Start the network: We go to the fabric-samples/test-network directory and start the network by using the script.
cd fabric-samples/test-network
./network.sh up=> we need root authorization if $USER is not in Docker group:
sudo ./network.sh up
In here, we may faced with the warning that is “Local fabric binaries and docker images are out of sync. This may cause problems” and the error that is “Fabric Docker image version of 1.4.6 does not match the versions supported by the test network.” looks like the following image.
To solve this problem, we firstly remove docker images and pull just images without cloning the repo and the downloading the binaries by executing the commands below. If everything is ok, we will have to see all the hyperledger docker images like below.
docker rmi $(docker images | grep hyperledger)
curl -sSL https://bit.ly/2ysbOFE | bash -s -- -s -b-
- Create a channel: We can use the
createChannel
subcommand to create a channel betweenOrg1
andOrg2
join their peers to the channel.
./network.sh createChannel
If we don’t use
-c
flag, it creates the channel with default namemychannel
so we can change the channel name with use the-c
flag.
./network.sh createChannel -c <channel_name>
If the command was successful, we can see the message below.
- Deploy the chaincode: We can use the
deployCC
subcommand to deploy the chaincode.
sudo ./network.sh deployCC -l javascript
The deployCC
subcommand will install the fabcar
chaincode on peer0.org1.example.com
and peer0.org2.example.com
and then deploy the chaincode on the channel specified using the channel flag. Since we created a channel with the default name mychannel
, we no need any modifications.
By default, The script installs the Go version of the fabcar chaincode. As you see above, I used the language flag,
-l
, to install the javascript versions of the chaincode. The all Fabcar chaincode that wrote in different languages can be found in thechaincode
folder of thefabric-samples
directory.
With Hyperledger Fabric 2.0, the chaincode lifecycle has changed and deployCC
subcommand provides chaincode to be installed, deployed, and invoked properly to that new lifecycle. In this story, I don’t mention it in detail. If you want to know more detail about this new lifecycle, you can look out this article.
Since invoking the initLedger
function, the chaincode puts an initial list of cars on the ledger. As you see the code below, the initLedger
function in fabcar
chaincode and cars JSON array was filled. Then each one put to the ledger with await putState
method.
If the chaincode is installed, deployed, and invoked properly you should see the following list of cars which is same above printed in your logs:
- Interacting with the network: Now we will use the peer CLI, which allows us to invoke deployed smart contracts, update channels, or install and deploy new smart contracts from the CLI. Here, we just focus to interact with our network. To add our binaries that we installed Install Samples, Binaries, and Docker Images section, we use the following commands.
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
After it, we can query the ledger from our CLI. The following command is used to query. It accepts function name which we want to query as the first argument so we can add parameters as an argument if the method we want to query needs it. We get the car information whose number is CAR9 by queryCar
function.
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryCar", "CAR9"]}'
Since we want to change an asset, we invoke the chaincode. When a network member wants to transfer or change an asset on the ledger, the chaincodes are invoked. With the following command, we change the owner of the car whose number is CAR9 by invoking the changeCarOwner
function.
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"changeCarOwner","Args":["CAR9","Dave"]}'
If the command has been executed successfully, you see Chaincode invoke successful. result: status:200
message.
After changing on an asset, we will check it on other peers by adding our binaries as we have done to Org1
peer and execute the following commands to define variables.
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
After it, we can query the ledger from our CLI as Org2
peer. The following command is used to query and we will see that the owner of the CAR9 was changed.
We have to down our network when we are finished using it by the following command.
./network.sh down
That’s it, we have;
- installed Prerequisites
- installed Samples, Binaries, and Docker Images
- brought up the test network
- created a channel
- deployed the chaincode
- interacted the network
in this story.
v