Hyperledger Fabric 1.0 Learning and Building (3) --- Running Test e2e-Fabric

3.1 、 Problem description of running fabric-samples
The problem can be solved 6.1 . The first step of the platform-specific binary configuration . You can choose to continue reading the instructions, or wait until you refer to Section 6.1 and then read the instructions in reverse, which will be redirected to this step in 6.1.
Under normal circumstances, we will refer to the official website to complete the first Network test , in this online document let us download a fabric-samples , the download address is on github , we need to download it to a local fabric-samples-release folder, rename it fabric-samples and upload it later Go to the opt/gopath/src directory.
The command executed according to the official website prompt cannot run the first-network project. The demo needs to download Platform-specific Binaries (specific binary files) first, according to For the description in the official documentation , you need to execute the following command first:
curl -sSL https: //goo.gl/iX9dek | bash
The above command will download the automated deployment script, as well as the platform-specific binaries cryptogen, configtxgen, configtxlator, and peer, and put them in the bin directory of the above repository.
Usually, the above command cannot be downloaded. Even if it can be downloaded, the speed is extremely slow. Therefore, we download the relevant binary files offline to perform the above operations.
The offline download address is not given in the execution command given on the official website, and there is no offline deployment instructions on the official website. We can find the download address in the fabric source code downloaded in 1.4.2, at /opt/gopath/src/github. There is a bootstrap-1.0.0.sh script file in the com/hyperledger/fabric/scripts directory. In fact, if the external network access conditions are favorable, you can directly run the script to download all the required fabric image files and official designation. required binaries.
Open bootstrap-1.0.0.sh (currently the latest version is 1.0.0, whichever is actual), find the output line for "Downloading platform binaries", and what we see so far is "echo "===> Downloading platform binaries" ”, check the download address it points to, you can get an official website provided Offline download URL , according to the context meaning and the version information we are currently using, we can get the final offline download file address , and download the file to the local.
According to the introduction on the official website, after decompression, you will get a bin folder, and decompress it to the /opt/gopath/src/github.com/hyperledger/fabric-samples directory to run projects such as first-network.
 
3.2. Run the e2e_cli project
Go to the /opt/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli directory, the file structure is as shown below:
network_setup.sh是一件测试脚本,该脚本启动5个docker容器,其中4个容器运行peer节点和1个容器运行orderer节点,它组成一个Fabric集群。另外还有一个cli容器用于执行创建channel、加入channel、安装和执行chaincode等操作。测试用的chaincode定义了两个变量,在实例化的时候会给这两个变量赋予了初始值,并通过invoke操作可以使两个变量的值发生变化。
通过以下命令执行测试:
bash network_setup. sh up
接下来会有许多的调试信息,具体可参考e2e_cli目录下的script/script.sh文件,当终端出现以下信息时,说明测试通过,所有部件正常工作:
如果遇到报错情况,首先检查自己的时间是不是和北京时间一样,如果不一样改为一样再次运行。
这个命令可以在本机启动4+1的Fabric网络并且进行测试,跑Example02这个ChainCode。我们可以看到每一步的操作,最后确认单机没有问题。确认我们的镜像和脚本都是正常的,我们就可以关闭Fabric网络,继续我们的多机Fabric网络设置工作。关闭Fabric命令:
bash network_setup. sh down
当然为了测试单机是否可以正常使用,暂时先不用关闭,等到部署多机的时候在关闭。
3.3、e2e_cliLinux内核bug(如果执行3.2出现问题,可以参考此步骤,否则略过)
该Fabric网络集群测试环境在Linux内核低版本上可能会出现问题,这是旧版内核的bug,比如在执行bash network_setup.sh up的时候会出现如下错误:
即执行chaincode初始化的时候报错,导致集群单机无法启动。
解决方案,使用最新版稳定版的docker(笔者写文档时的最新稳定版为:17.11.0-ce),将Linux内核升级到最新版,笔者原先使用的Linux内核是3.10,出现了上述问题,目前笔者使用的内核版本是4.12.5-1.e17,能够完美运行Fabric集群环境。
3.4、测试单机的Fabric
其实我们在前面3.2运行./network_setup.sh up的时候系统已经运行了一个Example02的ChainCode测试,部署上去的ChainCodeName是mycc,所以接下来我们要测试的话不能再初始化并部署同样名字的ChainCode了,我们可以使用自己另外命名的名字,比如alancc。
3.4.1在CLI中测试Example02
首先我们需要登录到CLI这个容器中,才能执行Fabric的CLI命令。
docker exec -it cli bash
如果成功进入,我们会切换到该容器的root用户下,得到如下的命令行目录:
运行以下命令可以查询a账户的余额:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
可以看到余额是90:
然后,我们试一试把a账户的余额再转20元给b账户,运行命令:
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 mycc -c '{"Args":["invoke","a","b","20"]}'
运行结果为:
现在转账完毕, 我们试一试再查询一下a账户的余额,没问题的话,应该是只剩下70了。我们看看实际情况:
果然,一切正常。最后我们要关闭Fabric网络,首先需要运行exit命令退出cli容器。关闭Fabric的命令与启动类似,命令为:bash network_setup.sh down
 
下面测试为新增一个自己的chaincode(可以不用进行测试,这里为了更精确的进行测试)
与0.6Fabric不同的是,在1.0中,链上代码是需要经过Install和Instantiate两步的。下面我们首先安装Example02,并指定一个名字,比如我们这里就用alancc:
peer chaincode install -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02/ -n alancc -v 1.0
运行后可以看到提示运行成功,返回200状态:
接下来是Instantiate,也就是初始化实例,设置a账户有100元,b账户有200元。
peer chaincode instantiate -n alancc -v 1.0 -c '{"Args": ["init", "A", "100", "B", "100"]}' -C alanc
 
peer chaincode instantiate -o orderer.example.com:7050 -C alanc -n alancc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR('Org1MSP.member','Org2MSP.member')"
 

Guess you like

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