Hyperledger Discovery Tour II: Begin the First Program

This article will show how to use the blockchain network through the Hyperledger Fabric network. The most common level is that applications on the blockchain network allow users to query the ledger, obtain specific records contained in the ledger, or update the ledger, such as adding record information to the ledger.

    In this example, using the Node.js SDK to interact with the blockchain consists of the following three steps:

  1. Start a test Hyperledger Fabric blockchain network. We need some basic components in the blockchain network to be able to query and update the ledger. These components include:

    (1) A peer node

    (2) A certificate verification node - this is the backbone and foundation of the entire network

    (3) A CLI container to execute some commands with permissions



  2. Learn about smart contracts and parameters in the examples. The smart contract in the example contains functions that interact with the ledger in different ways. For example, we can read data globally, and we can also query data at a more fine-grained level.

  3. Develop the application so that the application can perform query and update operations. We will provide two example applications, one for querying the ledger and the other for updating the ledger. The application will interact with the blockchain network through the SDK API, and finally call these functions.



After reading this article, you will know how the application uses the Fabric SDK (using the Node.js SDK in this example) to programmatically integrate the contract to interact with the ledger in the blockchain network.


Next, start your Hyperledger journey.


Start a blockchain network


We assume that you have completed all the tasks of setting up the environment , and the environment of the whole Hyperledger is completely normal. The normal Hyperledger environment interface is as follows:

0?wx_fmt=png


After logging in to the system, open a command line and enter:

cd fabric-samples/fabcar
ls

The fabcar directory is as follows:

0?wx_fmt=png

This is the directory structure of the entire fabric-samples. You need to understand this directory structure because fabcar will execute commands in other directories:

0?wx_fmt=png


startFabric.sh Now, start the blockchain network by executing  this script

./startFabric.sh

Note that in this script, a series of operations will be performed, mainly:


  • Start a peer node, ordering node, certificate verification node, and CLI container

  • Create a channel and add peer nodes to the channel

  • Install the smart contract (aka chaincode) into the peer's file system, and initialize the chaincode on the channel; 

  • Call initLedger the function to populate this channel's ledger with 10 different car data.


Let's start analyzing the script line by line. 
#!/bin/bash
# Exit on first error, the function of the set command is to display the existing shell variables in the system and set the new variable value of the shell variable. When using set to change shell properties, the symbols "+" and "-" are used to turn on and off the specified mode, respectively. -e means that if the value returned by the command is not equal to 0, exit the shell immediately.

set -e
# Set the time
starttime=$(date +%s)

# Prepare the certificate directory
if [ ! -d ~/.hfc-key-store/ ]; then
mkdir ~/.hfc-key-store/
fi #This
is Copy the certificate information to the corresponding directory. This step is the key to establishing a CA.
cp $PWD/creds/* ~/.hfc-key-store/
# Enter the basic-network directory, open the blockchain network, create a channel, and put the node Join
in./start.sh


Let's take a look at what work is done in this start.sh.


set -ev #First 
stop the network started before
docker-compose -f docker-compose.yml down #In
this line, enter ca, orderer, peer0 and a container stored in couchdb, start the network
docker-compose -f docker -compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb

Through Docker, the blockchain network is started. The current topology of the network is roughly as follows:

0?wx_fmt=png


The next step is to install and start chaincode through the CLI

#Start cli container 
docker-compose -f ./docker-compose.yml up -d cli #Create
channel, specify to create mychannel channel on peer0, and pass in the target orderer node
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger /configtx/channel.tx

0?wx_fmt=png

#将peer0加入mychannel
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block

0?wx_fmt=png

Now that the blockchain network is built, return to the previous command line:

# Install smart contracts via Cli 
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/ [email protected]/msp" cli peer chaincode install -n fabcar -v 1.0 -p github.com/fabcar #Through


cli, initialize the smart contract
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt /gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n fabcar -v 1.0 -c '
{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')" #Through


Cli, perform an invoke call, Add 10 car information in blockchain network
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar -c '

The command line output after executing the invoke operation:

0?wx_fmt=png


As can be seen from the command line, 10 car information has been stored in the blockchain.


Blockchain application analysis


First, at the command line enter:

#docker ps

You can see how many docker instances there are currently:

0?wx_fmt=png


For the specific information and usage of these docker images, follow-up articles will analyze them.


Application Analysis


Schematic diagram of the interaction between the application and the Fabric network:

0?wx_fmt=png


As mentioned earlier, this example contains an active chaincode container, and a ledger preloaded with information about 10 cars. In addition, there is a sample Javascript code in this example:

    -query.js

Can be used to query car information in the ledger.

    Before going any further to see how the app works, we first need to install an SDK node module to get our sample app working. In the fabcar directory, enter the following command:

    

#sudo npm install


After the installation is successful, enter in the command line:


#sudo node query.js


The information of these ten cars will be output in the console:

0?wx_fmt=png


Look, the first car, CAR0, is Toyota's Prius, and its owner is Tomoko. If you see this line, it means that the query can be executed~!

    Today, let’s analyze it here. As for how this query is executed, where is the chaincode stored, please push it in the next issue!


0?wx_fmt=jpeg

0?wx_fmt=jpeg


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392577&siteId=291194637