Hyperledger Fabric (five) to manually build Fabric Network V2.0 (based on first-network case)

If you do not have a complete environment Hyperledger Fabric V2.0, please refer to: Hyperledger Fabric (a) V2.0 environment to build the foundation . After have a basic environment, you can deploy the work of Fabric network. You can refer to the official website of the script file documents on the case of first-network parts using the supplied Quick Start block chain network. But in order to build a custom Fabric network, you need to understand the specific operation performed by the script.

Fabric way to script the deployment of the network is as follows (enable CAs, use couchdb):

cd Ⅰ fabric-samples / first-network # into the fabric-samples / first-network directory

Ⅱ ./byfn.sh down # If you have already been deployed, the destruction of the existing network; if the first deployment, omit this step

Ⅲ ./byfn.sh up -a -n -s couchdb # parameter -a start the Certification Authority, -n not deploy abstore chain code (the default script to deploy the chain code), - s database rather than using couchdb goleveldb, on the For more help using the script can be viewed by ./byfn.sh -h

Equivalent manner as manual deployment Fabric network (enabled CAs, used couchdb):

Ⅰ generate a certificate and a key for the network entity component configured to generate channel Union, the network starts Fabric

cd fabric-samples / first-network # into the fabric-samples / first-network directory

① using cryptogen generate a certificate and a key, placed under directory crypto-config

cryptogen generate generateCerts --config =. / crypto-config.yaml # byfn.sh script () function call after cryptogen tool, also called ccp-generate.sh script in the same directory as the file generated CCP Org1 and Org2 ( SDK for post-use), in this first ignored

② use configtxgen tool assembly configured to generate 4 (genesis.block, channel.tx, Org1MSPanchors.tx, Org2MSPanchors.tx), placed under the directory channel-artifacts

export FABRIC_CFG_PATH = $ PWD # configtxgen told where to find the file configtx.yaml

configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block # generation system channel creation block genesis.block

export CHANNEL_NAME = mychannel # by the environment variable specifying the channel name

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $ CHANNEL_NAME # Create a channel configuration transaction channel.tx

Transaction Org1MSPanchors.tx anchor nodes configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $ CHANNEL_NAME -asOrg Org1MSP # Org1 create an update on the organization channel

Transaction Org2MSPanchors.tx Org2 anchor nodes configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $ CHANNEL_NAME -asOrg Org2MSP # Creating an update on the organization channel

The results are as follows:

③ start using docker compose container based yaml profiles, the establishment of Fabric network (using CAs and couchdb)

export BYFN_CA1_PRIVATE_KEY = $ (cd crypto-config / peerOrganizations / org1.example.com / ca && ls * _sk) # CA specifies each organization in the form of private environment variables, yaml profile CA node uses the variable

export BYFN_CA2_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org2.example.com/ca && ls *_sk)

docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml -f docker-compose-couch.yaml up -d # -d parameter is not displayed for real-time log, the log to see the flow, it is necessary to open another subsequent command terminal performs

docker ps # to view basic information about the container has started, including a cli, four peer, four couchdb, five order, two ca

Ⅱ create a channel, the peer node joins the channel, and updates the anchor node tissue

docker exec -it cli bash # to enter the CLI (command line) container

Configure the environment variables, such that the container as a CLI peer0.org1.example.com Run node, subsequent operations require a lot of constantly switching the set environment variables to a different peer node to perform related operations, node peer0.org1.example.com the environment variable as follows:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp

CORE_PEER_ADDRESS=peer0.org1.example.com:7051

CORE_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.crt

For ease of viewing, this node is given first while peer0.org2.example.com environment variables:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:9051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

① send transactions to the order channel configuration node to create a channel

export CHANNEL_NAME = mychannel # specify a channel name to the environment variable form, must be all lowercase

peer channel create -o orderer.example.com:7050 -c $ CHANNEL_NAME -f ./channel-artifacts/channel.tx --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 # If the channel is successfully created, will receive the channel from the order of creation block nodes, named to the channel name

② switching node peer environment variables, command repeatedly, the node is added to the channel

peer channel join -b $ CHANNEL_NAME.block $ CHANNEL_NAME.block # command, depending on the channel name change

③ switching node peer environment variables, repeatedly executing the following command, which specifies the current anchor node for each channel organizations, each node having at least one anchor

peer channel update -o orderer.example.com:7050 -c $ CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --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 # organization designated anchor node

 So far ./byfn.sh all the work up -a -n -s couchdb are performed by manual operation to achieve, if removed parameter -n, the script will continue to execute the deployment and testing abstore chain code, the same can purely manual deployment and testing to complete any chain code.

Ⅲ chain code package, installation, the definition, testing deployment submit the calling process is as follows (herein abstore example, the chain code can be used in their preparation):

① 打包链码(以java语言为例,node可通过--lang node指定,go语言稍有不同,请参考官网文档)。脚本中命令格式为peer lifecycle chaincode package mycc.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label mycc_${VERSION}),mycc为链码名称(可自定义),参数--lang指定链码使用的编程语言,--label指定链码标签(通常采用 名称_版本 的格式),--path指定链码的存储路径,docker-compose-cli.yaml文件中有卷的映射关系./../chaincode/:/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode,因此主机的fabric-samples/chaincode目录对应着peer容器的/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode目录

peer lifecycle chaincode package mycc.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode/abstore/java/ --lang java --label mycc_1   #一定要根据自己链码的实际存储位置,修改 --path的值,目录对应关系见上述

② 切换peer节点的环境变量,多次执行以下命令,在不同节点安装链码

peer lifecycle chaincode install mycc.tar.gz   #注意链码包名称mycc.tar.gz与前一条命令保持一致,该命令执行期间很容易报错Error: failed to endorse chaincode install: rpc error: code = Unavailable desc = transport is closing,原因未知。解决方案:稍等两分钟,再次执行命令(高频率重复执行命令将引起其它错误),一般第二次就会成功

③ 查询节点上已安装的链码,将链码ID设置为环境变量(后续命令会用到)

peer lifecycle chaincode queryinstalled   #可看到已安装链码包的ID以及标签

CC_PACKAGE_ID=mycc_1:3a8c52d70c36313cfebbaf09d8616e7a6318ababa01c7cbe40603c373bcfe173   #这个ID是动态的,不要直接复制此命令

④ 切换peer节点的环境变量,多次执行以下命令,所有组织就链码定义达成一致(只需要每个组织的某个peer执行一次)

peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name mycc --version 1.0 --init-required --package-id $CC_PACKAGE_ID --sequence 1 --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   #官方脚本中--version和--sequence的值一致,不太清楚两个值的差别

⑤  检查组织是否就链码定义已经达成一致

peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_NAME --name mycc --version 1.0 --init-required --sequence 1 --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 --output json   #查看各组织对链码定义的批准状况

⑥  将链码定义提交到通道(任何组织的某个节点执行一次即可)

peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID $CHANNEL_NAME --name mycc --version 1.0 --sequence 1 --init-required --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 --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt   

⑦ 查询链码定义

peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name mycc

⑧ 调用链码的初始化函数

peer chaincode invoke -o orderer.example.com:7050 --isInit --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 $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["Init","a","100","b","100"]}' --waitForEvent   #-c指定调用链码函数的参数,请根据自己的链码进行修改(第一个参数为函数名)

⑨ 调用链码的查询(query)函数

peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'   #-c指定调用链码函数的参数,请根据自己的链码进行修改(第一个参数为函数名)

⑩ 调用链码的invoke函数,a向b转账十元,该事务提交后,再次查询a的值将为90

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 $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' --waitForEvent   #-c指定调用链码函数的参数,请根据自己的链码进行修改(第一个参数为函数名)

 So far, Fabric network has been deployed manually. For members of the organization or self-defined number of nodes in the network, it is necessary to modify the actual needs and according to the specific configtx.yaml yaml startup configuration file docker container. About first-network use cases Introduction to the configuration file, refer to Hyperledger Fabric (d) the main configuration file first-network cases and the main script .

Guess you like

Origin www.cnblogs.com/always-kaixuan/p/12416157.html