Super Ledger Discovery Tour Two: Start the first program

Source: Internet
Author: User
Tags set time docker ps

This article will show you how to use the blockchain network through the Super Ledger Fabric network. One of the most common aspects is that applications on the blockchain network allow users to query the ledger (ledger), get specific records contained in the ledger, or update the ledger, for example by adding record information to the ledger.

In this example, you interact with the blockchain by using the node. JS SDK, which contains the following three steps:

Start a test with the super Ledger fabric blockchain network. We need to have some basic components in the blockchain network so that we can complete the query and update the ledger operations. These components include:

(1) A peer node

(2) A certificate authentication node-This is the backbone and foundation of the entire network

(3) A CLI container for executing commands with permissions



Learn the 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 the data globally, or we can query more granular levels of data.

This application is developed to enable the application to perform query and update operations. By providing two sample apps, one for querying the ledger and the other for updating the ledger. The application interacts with the blockchain network through the SDK API and eventually calls these functions.



After reading this article, you will use the fabric SDK (in this case, the node. JS SDK) to programmatically federate only contracts and interact with the ledgers in the blockchain network.


Next, start the Super Ledger tour.


start the Blockchain network


We assume that you have completed all the tasks of setting up an environment where the entire super ledger environment is perfectly normal. The normal super ledger environment interface is as follows:


After logging into the system, open the command line and enter:

CD Fabric-samples/fabcar
Ls

The Fabcar directory is as follows:

This is the directory structure of the entire fabric-samples and needs to understand this directory structure because Fabcar executes the commands in the other directories:


Now, start the blockchain network by executing the startfabric.sh script.

./startfabric.sh

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


Start a peer node, ordering node, certificate authentication node, and CLI container

Create a channel and add the peer node to the channel

Install the smart contract (i.e. Chaincode) into peer's file system and initialize the Chaincode on the channel;

Call the Initledger function to populate the channel ledger with 10 different car data.


This script is parsed on a row-by-line basis.
#!/bin/bash
# Exit on first error, the SET command functions primarily to display shell variables that already exist in the system, and to set new variable values for shell variables. When you use set to change the shell attributes, the symbols "+" and "-" are used to turn the specified mode on and off, respectively. -E indicates that if the instruction return value is not equal to 0, exit the shell immediately.


# Set time
starttime=$ (Date +%s)

#准备证书目录
if [!-D ~/.hfc-key-store/]; Then
mkdir ~/.hfc-key-store/
Fi
#这是把证书信息复制到对应目录, this step is the key to building a CA
CP $PWD/creds/* ~/.hfc-key-store/
# Enter the Basic-network directory, open the Blockchain network, create the channel, and add the nodes in
./start.sh


Let's take a look at what has been done in this start.sh.


Set-ev
#先停掉之前启动的网络
Docker-compose-f docker-compose.yml Down

Docker-compose-f docker-compose.yml up-d ca.example.com orderer.example.com peer0.org1.example.com couchdb

Through Docker, the network of blockchain is started, and the topology of the network is as follows:


The next step is to install and start the Chaincode via the CLI.

#启动cli container
Docker-compose-f./docker-compose.yml up-d CLI
#创建channel, specify that the MyChannel channel is created on Peer0 and passed in to the target's orderer node
Docker exec-e "Core_peer_localmspid=org1msp"-E "core_peer_mspconfigpath=/etc/hyperledger/msp/users/ Admin@org1.example.com/msp "peer0.org1.example.com Peer channel Create-o orderer.example.com:7050-c mychannel-f/etc/ Hyperledger/configtx/channel.tx

#将peer0加入mychannel
Docker exec-e "Core_peer_localmspid=org1msp"-E "core_peer_mspconfigpath=/etc/hyperledger/msp/users/ Admin@org1.example.com/msp "peer0.org1.example.com Peer channel Join-b Mychannel.block

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

#通过Cli, install 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/admin@org1.example.com/msp "CLI Peer Chaincode Install -N fabcar-v 1.0-p Github.com/fabcar

#通过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/admin@org1.example.com/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 ')"


#通过Cli, execute an invoke call to add 10 car information to the 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/admin@org1.example.com/msp "CLI Peer Chaincode Invoke- o orderer.example.com:7050-c mychannel-n fabcar-c '

command line output after execution of the invoke operation:


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


Blockchain Application Analysis


First, on the command line, enter:

#docker PS

You can see how many instances of Docker are currently available:


For specific information and usage of these Docker image, subsequent articles are analyzed.


Application Analysis


Application and Fabric network interaction diagram:


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

-query.js

can be used to query the car information in the ledger.

Before you can see how the app works, you'll first need to install an SDK node module so that our sample app works. In the Fabcar directory, enter the following command:

#sudo NPM Install


After the installation is successful, enter it at the command line:


#sudo node Query.js


The information for these 10 vehicles will be output at the console:


Look, the first car, CAR0, is Toyota's Prius, owner is Tomoko, if you see this line, the description can be executed query Oh ~.

Today first analysis here, as to how this query is executed, Chaincode store in what position, the next issue push it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.