Installing Hyperledger Fabric v1.3 on Ubuntu 18.04 — Part III (Updated on 2018–11–15)
Now, that we have run our first sample Hyperledger Fabric Network using All-in-one script in the previous article, we are going to rip that apart and write our custom commands from scratch to understand how it works and what is the correct way of writing the commands.
For this tutorial, we’ll need the images running, so, we need to first edit the docker-compose-cli.yaml, in examples/e2e_cli directory, file to ensure that all-in-one script is not run when starting the images. So, scroll down to the part where the CLI image is configured and change the “command” line to:
command: /bin/bash
So, now use the following command, from the examples/e2e_cli directory:
./network_setup.sh up
Now, that we have our network running, we want the execute the following queries in sequence:
- Create a new Channel
- Join Peers on that channel
- Install Chaincode
- Instantiate Chaincode
- Invoke Chaincode
- Query Chaincode
So, we’ll go through all these steps, one by one.
Create a new Channel
So, to run our network, HF v1.3 uses the concept of Channel, which logically differentiates your blockchain from other party’s blockchain. So, to create a new channel, we need to login into the CLI image and fire commands from there.
To login into the CLI image, use the following command:
docker exec -it cli bash
You should see an output similar to the one shown below:
Now, we need to create a new channel, so the let us first set some environment variables, so that we don’t need to edit them again and again. This tutorial focuses on creating the transactions on a single peer, but the network is multiple peer. To keep the simplicity of the tutorial, we use only peer0 to fire the queries from. Other peers can be used, just by altering some environment variables and TCerts.
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pemORDERER_SYSCHAN_ID=e2e-orderer-syschanCORE_PEER_LOCALMSPID="Org1MSP"CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crtCORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspCORE_PEER_ADDRESS=peer0.org1.example.com:7051CHANNEL_NAME=mychannel
Here is the screenshot of all the variables, we have set:
Now, let us get a brief understanding of what every variable means:
- CORE_PEER_MSPCONFIGPATH = This variable takes into account the path where it can find the peer’s certificates for signing the transactions.
- CORE_PEER_ADDRESS = This defines the IP Address of the Peer we are contacting and firing the transaction on.
- CORE_PEER_LOCALMSPID = This variable takes into account the Organisation name, it belongs to.
- CORE_PEER_TLS_ROOTCERT_FILE = This variable is the path to the CA file of the peer, which is used in mutual TLS with the Orderer.
- ORDERER_CA = This path is for the CA Certificate of the Orderer, which is required when creating channel and joining peers, as only Orderer has the authority to create the channel.
Now, that we have a basic understanding of how everything works, let’s get started and create our first channel:
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile $ORDERER_CA
From the command, it is clear that “-o” is used to define the orderer’s IP Address we are trying to connect to. “-c” defines the channel name we are giving to our new channel. “-f” is the genesis file that we will need to create our genesis block, which contains data about the network, organizations and affiliations. “ — tls true — cafile <filename>” is for enabling the TLS and sending the transaction, as our network we just started above is running on Mutual TLS and will require a CA Certs file.
After the successful execution of the above command, you should a similar to below:
Now, that we have created our channel, we are good to go and join our peers on the channel. Let’s move to the next section then.
Join Peers on the Channel
Once, we create a new channel, Orderer sends us a new .block file, which is the genesis block of the network. We will need this .block file to tell the peers where are we joining.
peer channel join -b mychannel.block
After successful joining, you should have an output similar to mine:
Please take into account that we are only joining 1 Peer to the network, so that to attain simplicity in the transactions. You can always go ahead and add more peers by changing the variables we just created above.
Now that we have a successful join, we now move onto the next section.
Install Chaincode on the Peers
Now, that we have joined our peer to the network, we need to install the chaincode too. Chaincode is basically the business logic of the application, that we are trying to run on the network. Some might even call it as Smart Contracts.
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/example02/cmd
Let us understand what we are trying to achieve here. We are naming our chaincode as “mycc”, so that we don’t need to refer to our chaincode via the path always. “-v” tells the network that this is my version of the chaincode, accept it. “-p” describes the path to the actual chaincode directory.
Here is the output upon successful execution:
Now, that we have our chaincode installed, let us create some entities and assign some value to it. As this is our first transaction related to the Chaincode, we have to use the Instantiate command, which is explained the next section.
Instantiate Chaincode
Now, lets create some new entities and assign values to them. The chaincode we just installed above is about transferring value from one user to another.
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
Now, let us understand what each flag means. We already about the “-o”, “ — tls true — cafile $ORDERER_CA”. Now let us look into the others. “-C” defines the channel name, “-c” defines the argument we are passing to the chaincode. “-P” is the endorsement policy, which defines how many people should endorse before a transaction becomes a valid transaction. Here, we are writing that a member of Org1 OR Org2 can sign it and only then it should become a valid transaction.
Now, as we are only using 1 peer to fire all the transactions, if we want to use another peers, we need to join them to the channel and install the chaincode onto them, separately. But, we don’t need to fire the instantiate chaincode command again and that is taken care of by the network, once you install the chaincode.
So, having done with it, let’s move onto invoking some transactions onto the network.
Invoke a Transaction
Now, that we have created two entities, a and b, we will want to transfer some amount between to create a transaction. So, for this tutorial, we are going to transfer 10 units from a to b. The command looks like the following:
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}'
We already know all the command line tags, so we move onto the screenshot of the final result, to let you verify of the correct result.
The highlighted part shows that the transaction went through and we have successfully transferred 10 units from a to b. Now, we will query the chaincode to see if we can fetch the new values of a and b.
Query Chaincode
Now, that we have invoked the chaincode for the transfer of 10 units from a to b, we want to check whether our transaction really went through or not. Also, we want to fetch the new values of a and b.
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","b"]}'
Here is the screen shot to verify the result:
So, I did fire a query request for both the variables to check that the 10 unit subtracted from a and did get added to b.
So, with this, we are at the end of the tutorial.
In this, we tried to run the network one step at a time and tried to explain what each term means to have a better understanding and knowledge for creating our own Chaincode. Please do write in the comment section below for any queries you might have.
Last Updated: 2018–11–15