区块链学习之Fabric的环境搭建-手动搭建

借鉴博客

深蓝居

步骤的话也是基于上一篇从第7步开始换成下面的具体步骤。

1. 1生成cryptogen

cd ~/go/src/github.com/hyperledger/fabric
make cryptogen
dell@dell-OptiPlex-3050:~/go/src/github.com/hyperledger/fabric$ make cryptogen
build/bin/cryptogen
CGO_CFLAGS=" " GOBIN=/home/dell/go/src/github.com/hyperledger/fabric/build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/tools/cryptogen/metadata.Version=1.0.0" github.com/hyperledger/fabric/common/tools/cryptogen
# github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11
vendor/github.com/miekg/pkcs11/pkcs11.go:29:18: fatal error: ltdl.h: No such file or directory
compilation terminated.
Makefile:227: recipe for target 'build/bin/cryptogen' failed
make: *** [build/bin/cryptogen] Error 2

如果出现上述问题,是缺少文件

apt-get install libtool libltdl-dev

1.2 配置crypto-config.yaml

搭建环境时此处是默认值就行。

1.3 生成公私钥和证书,这些命令一般不会出现问题

cd examples/e2e_cli/
../../build/bin/cryptogen generate --config=./crypto-config.yaml

2.1 编译生成configtxgen(不清楚是干嘛的)

cd ~/go/src/github.com/hyperledger/fabric

make configtxgen

2.2 配置configtx.yaml

搭建环境时默认值就行,后续的生产环境估计会来修改。

2.3 生成创世区块,配置CHannel区块,以及锚节点更新。

cd examples/e2e_cli/

../../build/bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

../../build/bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

../../build/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

../../build/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

2.4 文件在e2e目录中的channel-artifacts中,存在4个文件,如果是前一篇的话此处的文件是fabric帮我们生成的。(猜测)。

3.1 配置orderer

base/docker-compose-base.yaml 文件中,搭建环境时默认值。

3.2 配置peer

base/docker-compose-base.yaml和peer-base.yaml 文件中,默认值。

3.3 配置CLI

docker-compose-cli.yaml文件中,需要注释掉自动运行的 command那一行(在前面加 #)

 command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT' 

4. 启动Fabric环境(此处出现较多问题)

4.1 将Fabric Docker环境配置放在docker-compose-cli.yaml后面,主要是让后台运行。

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

docker ps 可以看见运行的情况。正常情况是orderer和peer都有运行,还有一个tools也运行(如果它没运行后续继续不下去)

436f6f0d70e4        hyperledger/fabric-tools              "/bin/bash"              28 minutes ago      Up 28 minutes                                                                                   cli
afb11e8d7cf6        hyperledger/fabric-orderer            "orderer"                29 minutes ago      Up 28 minutes       0.0.0.0:7050->7050/tcp                                                      orderer.example.com
e7f3ea4b3520        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 29 minutes       0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
6e4c40b8feae        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 28 minutes       0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp      peer0.org2.example.com
04db32690344        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 28 minutes       0.0.0.0:7051-7053->7051-7053/tcp                                            peer0.org1.example.com
fa1ac260a3e3        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 29 minutes       0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp      peer1.org1.example.com

4.2 进入cli容器。

docker exec -it cli bash

4.3 创建Channel

ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA

这边是出问题最多的地方,我大概列一下我碰到的:

4.3.1

2018-07-26 06:13:15.796 UTC [grpc] Printf -> DEBU 003 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: Error while dialing dial tcp: lookup orderer.example.com on 127.0.0.11:53: no such host"; Reconnecting to {orderer.example.com:7050 }
Error: Error connecting due to  rpc error: code = Unavailable desc = grpc: the connection is unavailable

这个问题主要是你orderer没有正常启动导致找不到,就是上面的docker ps看的时候要确保orderer在启动行列。

4.3.2

Failed to dial orderer.example.com:7050: connection error: desc = "transport: authentication handshake failed: x509: certificate signed by unknown authority (possibly because of \"x509: ECDSA verification failure\" while trying to verify candidate authority certificate \"tlsca.example.com\")"; please retry.
Error: Error connecting due to  rpc error: code = Internal desc = connection error: desc = "transport: authentication handshake failed: x509: certificate signed by unknown authority (possibly because of \"x509: ECDSA verification failure\" while trying to verify candidate authority certificate \"tlsca.example.com\")"

这个主要是脏数据问题,我部署的时候因为出现了很多次失败,所以最终重新来过。

4.3.3

Error: Got unexpected status: BAD_REQUEST

主要是channel重名导致的问题,把docker关闭掉重新来过就行。

docker rm -f $(docker ps -aq)

4.3.4

fabric 报Error: Error endorsing query: rpc error: code = Unknown desc = Error executing chaincode: Timeout expired while starting chaincode

docker rmi -f $(docker images |grep "dev-" |awk '{print $3}')

4.4 将peer加入到Channel,如果上面一切顺利,那么这边也就没什么问题了。(此处默认链接的是peer0.org1)

peer channel join -b mychannel.block

4.5 加入peer1.org1

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 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp 
CORE_PEER_ADDRESS=peer1.org1.example.com:7051

peer channel join -b mychannel.block

4.6 加入peer0.org2

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 
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:7051

peer channel join -b mychannel.block

4.7 加入peer1.org2

CORE_PEER_LOCALMSPID="Org2MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp 
CORE_PEER_ADDRESS=peer1.org2.example.com:7051

peer channel join -b mychannel.block

4.8 更新Org1的锚节点peer0.org1

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 
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

peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA

4.8 更新Org2的锚节点peer0.org2

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 
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:7051

peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA

5. ChainCode运行

5.1 切换到peer0.org1

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 
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

5.2 安装ChainCode

peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

5.3 实例化链上代码(将来的SDK也是通过这个对象来操作链)

peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR      ('Org1MSP.member','Org2MSP.member')"

5.4 查询

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
返回结果:Query Result: 100

5.5 转账

peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'

6. 在另一个节点查询交易,给peer0.org2安装ChainCode

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 
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:7051

peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

6.1 查询

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Query Result: 90

当我们将两个节点都安装上ChainCode时,就启动了两个Docker镜像。

dell@dell-OptiPlex-3050:~$ docker ps
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                                                       NAMES
0bda162b8c01        dev-peer0.org2.example.com-mycc-1.0   "chaincode -peer.add…"   19 minutes ago      Up 19 minutes                                                                                   dev-peer0.org2.example.com-mycc-1.0
7a724b3060c3        dev-peer0.org1.example.com-mycc-1.0   "chaincode -peer.add…"   22 minutes ago      Up 22 minutes                                                                                   dev-peer0.org1.example.com-mycc-1.0
436f6f0d70e4        hyperledger/fabric-tools              "/bin/bash"              28 minutes ago      Up 28 minutes                                                                                   cli
afb11e8d7cf6        hyperledger/fabric-orderer            "orderer"                29 minutes ago      Up 28 minutes       0.0.0.0:7050->7050/tcp                                                      orderer.example.com
e7f3ea4b3520        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 29 minutes       0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
6e4c40b8feae        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 28 minutes       0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp      peer0.org2.example.com
04db32690344        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 28 minutes       0.0.0.0:7051-7053->7051-7053/tcp                                            peer0.org1.example.com
fa1ac260a3e3        hyperledger/fabric-peer               "peer node start"        29 minutes ago      Up 29 minutes       0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp      peer1.org1.example.com

你可能感兴趣的:(区块链)