This entire tutorial is the second part of the installation of Hyperledger Fabric v1.1. In the previous article, we installed all the dependencies required for us to make the Fabric environment run. In this part of the tutorial, we aim to launch our own example network and learn to fire some transactions and query the Blockchain network to retrieve the values.

So, at first, Hyperledger Fabric Project has provided a lot of detailed examples and documentation on its site. But the one we’ll be following is the e2e_cli code example in examples directory.

To get started, we first need to clone the Fabric Project on your machine. So, go ahead and fire the command below:

git clone https://github.com/hyperledger/fabric.git

After cloning the project to a safe location, make sure the directory structure is similar to one described below:

<word directory>/src/github.com/hyperledger/fabric

Please refer to the below image for further explanation:

 

I already have the project cloned, so my output will differ from yours. But, at the end, you should have fabric project in your current directory. Now, that we have the project cloned into our work directory, we need some functions to be build to ensure our smooth journey towards the execution environment. So, we need to build functions, like:

configtxgen
cryptogen

“configtxgen” takes in configtx.yaml file and builds your genesis block for the channel and contains metadata associated with the channel, which is broadcasted to the Orderer. So, this is like the block 0 of the network, which is needed to make sure that our chaincode is deployed on top of it. “Cryptogen” is used to generate the certificates according to the organizational structure of the organizations participating in the blockchain.

To enable the building of the tools, we need to set the GOPATH variable in the ~/.bashrc or ~/.bash_profile file of the user logged into. This is to enable fabric to locate the source code to enable building of the commands. For that, we add the following line to the ~/.bashrc or ~/.bash_profile file:

export GOPATH=~/Documents/Blockchain

GOPATH will always be your work directory. So, if you have a different Work Directory, then please state that as your GOPATH. Now, we are ready to build the tool. To build the tool, we write (from the root directory of the fabric project):

make configtxgen cryptogen

So, if all goes well, we will have the following output:

 

If there is some error regarding a library, ‘ltdl.h’, fire the following command and it should probably be fixed:

sudo apt install libtool libltdl-dev

If still the problem pertains, maybe you want to check out the stackoverflow and jira for the Hyperledger Project, link (https://jira.hyperledger.org)

So, considering all goes well, we now have our tools, configtxgen and cryptogen, in build/bin directory, relative to Fabric’s root directory.

Now, we move to the examples/e2e_cli directory, for some real blockchain deployment and transactions stuff. So, to use the configtxgen tool and generate the genesis block and configurations for the channel, we use the following script, which by-default creates the files.

 

This is the output one should receive after a successful execution of the command:

./generateArtifacts.sh

Now, that we have the genesis block, we need containers to run our blockchain network. Hyperledger Fabric suggests that one should use Docker containers for these kinds of work, so that it is isolated from the host tasks and enables customization and tweaking. So, to download all the required images, run the command from the fabric root directory:

make docker

This will download all the required images and when you check the installed images using:

docker images

You should see a similar output as below:

 

Now, that we have the images with us, we will now start our network and run the all-in-one script to test everything is working fine or not. In our ecosystem, we have 1 Orderer, 2 Peers from 2 Organisations each and 1 CLI to access the logs and fire custom transactions.

./network_setup.sh up <channel-id>

If you provide a channel-id argument, it will create the channel with the specific channel-id or else keep “mychannel” as the default channel-id name.

If everything goes well, then, upon a successful execution, you should see the following output:

 

Now, that we have run the automated tests, we know that now we can create our own network and fire transactions from that too. Stay tuned for the next article, depicting, how to manually do the steps, we just did with the network_setup.sh file.

To bring down the network, fire the command:

./network_up.sh down

The above command deletes all the extra images that were created to ensure the channel and chaincode data access.

Last Updated: 2018–07–09


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 commented lines as shown in the picture below:

 

So, now use the following command, from the examples/e2e_cli directory:

docker-compose -f docker-compose-cli.yaml up

Upon successful execution, you should have an output similar to mine:

 

Now, that we have our network running, we want the execute the following queries in sequence:

  1. Create a new Channel
  2. Join Peers on that channel
  3. Install Chaincode
  4. Instantiate Chaincode
  5. Invoke Chaincode
  6. Query Chaincode

So, we’ll go through all these steps, one by one.

Create a new Channel

So, to run our network, HF v1.0, uses the concept of Channel, which logically differentiates your chaincode from other parties chaincode. So, to create a new channel, we need to login into the CLI image and fire commands from that only.

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.

CORE_PEER_MSPCONFIGPATH = /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig
CORE_PEER_ADDRESS = peer:7051
CORE_PEER_LOCALMSPID = “Org0MSP”
CORE_PEER_TLS_ROOTCERT_FILE = /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig/cacerts/peerOrg0.pem
ORDERER_CA = /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem

Here is the screen shot of all the variables, we have set:

 

Now, let us get a brief understanding of what every variable means:

  1. CORE_PEER_MSPCONFIGPATH = This variable takes into account the path where it can find the peer’s certificates for signing the transactions.
  2. CORE_PEER_ADDRESS = This defines the IP Address of the Peer we are contacting and firing the transaction on.
  3. CORE_PEER_LOCALMSPID = This variable takes into account the Organisation name, it belongs to.
  4. 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.
  5. 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 work, let’s get started and create our first channel:

peer channel create -o orderer0:7050 -c mychannel -f crypto/orderer/channel.tx — tls true — 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/chaincode_example02

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 orderer0:7050 — tls true — cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c ‘{“Args”:[“init”,”a”,”100",”b”,”200"]}’ -P “OR (‘Org0MSP.member’)”

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 Org0 should sign it and only then it should become a valid transaction.

After creating the transaction, the output should similar to mine:

 

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 orderer0:7050 — tls true — cafile $ORDERER_CA -C mychannel -n mycc -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 mychannel -n mycc -c ‘{“Args”:[“query”,”a”]}’ 
peer chaincode query -C mychannel -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 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.

If you have any doubts or errors, please email hyperledger@mlgblockchain.com and we shall get back to you as soon as possible, with a solution.

Installing Hyperledger Fabric v1.1 on Ubuntu 16.04 — Part II &  Part III的更多相关文章

  1. Installing Hyperledger Fabric v1.1 on Ubuntu 16.04 — Part I

    There is an entire library of Blockchain APIs which you can select according to the needs that suffi ...

  2. Ubuntu下搭建Hyperledger Fabric v1.0环境

      多次尝试才正常启动了Fabric,如遇到各种莫名错误,请参考如下一步步严格安装,特别用户权限需要注意. 一.安装Ubuntu16 虚拟机或双系统,虚拟机有VirtualBox或者VMware,Ub ...

  3. Hyperledger Fabric(v1.1.0)编译时遇到的问题

    Hyperledger Fabric(v1.1.0)编译时遇到的问题 0. 编译过程的坑 编译时,按照如下顺序编译 make release,编译源码生成二进制文件 make docker,生成一系列 ...

  4. 解决Ubuntu 16.04 上Android Studio2.3上面运行APP时提示DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs的问题

    本人工作环境:Ubuntu 16.04 LTS + Android Studio 2.3 AVD启动之后,运行APP,报错提示: DELETE_FAILED_INTERNAL_ERROR Error ...

  5. 三、主流区块链技术特点及Hyperledger Fabric V1.0版本特点

    一.Hyperledger fabric V1.0 架构 1.逻辑架构: 2.区块链网络 3.运行时架构 二.架构总结 1.架构要点 分拆Peer的功能,将Blockchain的数据维护和共识服务进行 ...

  6. Installing Moses on Ubuntu 16.04

    Installing Moses on Ubuntu 16.04 The process of installation To install requirements sudo apt-get in ...

  7. 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1

    摘要: 全球开源区块链领域影响最为广泛的Hyperledger Fabric日前宣布了1.1版本的正式发布,带来了一系列丰富的新功能以及在安全性.性能与扩展性等方面的显著提升.阿里云容器服务区块链解决 ...

  8. Hyperledger Fabric(v1.2.0)代码分析1——channel创建

    Hyperledger Fabric(v1.2.0)代码分析1--channel创建 0. e2e_cli Hyperledger Fabric提供了一个e2e的例子,该例中创建了一个基础的区块链网络 ...

  9. 003-主流区块链技术特点及Hyperledger Fabric V1.0版本特点

    一.Hyperledger fabric V1.0 架构 1.逻辑架构: 2.区块链网络 3.运行时架构 二.架构总结 1.架构要点 分拆Peer的功能,将Blockchain的数据维护和共识服务进行 ...

随机推荐

  1. Codeforces 336D Dima and Trap Graph 并查集

    Dima and Trap Graph 枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check #include<bits/stdc++.h> #defi ...

  2. valgrind 内存调试工具

    一.valgrind 是运行在linux系统下的内存调试工具,支持很多对象:memcheck.addrcheck.cachegrind.Massif.helgrind.Callgrind等.使用val ...

  3. canvas版《俄罗斯方块》

    试玩(没有考虑兼容低版本浏览器): See the Pen Canvas俄罗斯方块 by 王美建 (@wangmeijian) on CodePen. ************************ ...

  4. odoo导入功能二开

    原来有的导入功能相信很多小伙伴对其功能不是很满意,不过没关系,我们可以二开啊,把它的功能改造成你想要的样子,接下来让我们看看怎么办吧 例如我想把员工导入功能中添加上用户同步注册功能 首先,我要找到原模 ...

  5. hihoCoder.1457.后缀自动机四 重复旋律7(广义后缀自动机)

    题目链接 假设我们知道一个节点表示的子串的和sum,表示的串的个数cnt,那么它会给向数字x转移的节点p贡献 \(sum\times 10+c\times cnt\) 的和. 建广义SAM,按拓扑序正 ...

  6. bzoj4289 Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...

  7. springmvc学习总结(一) -- 从零搭建,基础入门

    1.新建maven项目 参考mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)第一部分,修改配置 2.修改pom.xml 文件 <proj ...

  8. Webpack 性能优化 (一)(使用别名做重定向)

    前言 Webpack 是 OneAPM 前端技术栈中非常重要的一部分.它非常好用,假设你还不了解它,建议你阅读这篇Webpack 入门指迷,在 OneAPM 我们用它完毕静态资源打包.ES6 代码的转 ...

  9. 【Go命令教程】11. go vet 与 go tool vet

    命令 go vet 是一个 用于检查 Go 语言源码中静态错误的简单工具.与大多数 Go 命令一样,go vet 命令可以接受 -n 标记和 -x 标记.-n 标记用于只打印流程中执行的命令而不真正执 ...

  10. Parallel Programming--perfbook

    https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html