Table of Contents
Hyperledger Fabric samples git repository comes with many blockchain examples that you can deploy on the blockchain. From fairly simple network like basic network that has only one organization and only one peer to a complex structure like first network that has two organizations and four peers in total. As with all these networks fabric samples also comes with pre built chaincodes in Go language, Javascript and Java. You can add your own chaincode to these folders and you can also install your chaincode on byfn.
Build Your first Network ( byfn)
In order to install your chaincode on byfn. First of all lets see how do we run byfn fabric normally and then we will modify it and then run it with the TLS disabled. I am not going to assume that you already have the samples and everything to let me explain it from the very basics. lets download the fabric samples first.
Create a new folder named Blockchain. and then open terminal in that folder.
Run this command in the terminal.
$ git clone https://github.com/hyperledger/fabric-samples.git
After you run this command you will get all the fabric samples. Now they are not yet ready to work. You need to download binaries and docker images of the fabric version which you want or by default let it do it for the latest version.
run this command in the fabric samples directory.
$ ./scripts/bootstrap.sh [version] [ca version] [thirdparty_version]
The version, ca version and thirdpart_version are optional. u can simply run it without them as well.
After all everything is done. You are now ready to run the script for byfn. Go to the first network directory and run the byfn script using this command.
$ ./byfn.sh up
for levedb
or you can run it like
$ ./byfn.sh up -s couchdb
to start your byfn using couchdb.
You will see a lot of console outputs on your screen that is fabric just building you a new orderer, peers, couchdb etc.
Once they are done. your byfn is all ready to run but know that there isn’t any chaincode on byfn network. So what we will do now is install our own chaincode on byfn network.
For the sake of this tutorial lets assume you already have your own chaincode.
Create a folder in the chaincode folder in fabric samples directory and start writing these commands in the terminal. ( make sure your byfn network is up and all the peers are active ).
To install your chain code on on peer0.org0 you need the following command. Don’t forget to change the mychaincode to the name you have given to your chaincode.
$ docker exec cli peer chaincode install -n mychaincode -l golang -p github.com/chaincode/mychaincode_chaincode -v 1.0
To install on other peers you have to mention the peers and the organization on which you want to install your chaincode and also make sure you mention the chaincode which you want to install there.
docker exec -e "CORE_PEER_LOCALMSPID=Org2MSP" -e "CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp" -e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" cli peer mychaincode install -n adverce -p github.com/chaincode/mychaincode_chaincode -v 1.0
Now that your chaincode is successfully installed on all the peers, You have to instantiate the chaincode. Run the following command in terminal.
$ export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
$ docker exec cli peer chaincode instantiate -o ordered.example.com:7050 –tls –cafile $ORDERER_CA -C mychannel -c ‘{“Args”:[]}’ -n mychaincode -v 1.0 -P “OR(‘Org1MSP.member’, ‘Org2MSP.member’)”
Now you are done with installing the chaincode on all of the peers and the byfn network. You are done 🙂 .
Now if you want to store some record in your blockchain then run this command to initiate the chaincode. The addClick is a function that takes in 6 arguments and stores them in a click structure inside our chaincode. You have to define your own struct and a method for it to run this.
docker exec cli peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mychaincode --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["addClick","31","1","1","1","1","NEW"]}'
You will get a chaincode successfully invoked response from the orderer and this ensures that your deployed chaincode is working properly but still if you want to test that the record you inserted should be retrieved as well so you have to call your get method. (You need to have a get query written in your chaincode before you can do this).
docker exec cli peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mychaincode --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["getClick","31"]}'
If you want to build a blockchain based app or dapp, you can contact a Blockchain development company. Send email below.