0. IP与机器配比说明
主机A | 主机B | |
---|---|---|
节点 | Peer0 | Peer1 |
主机名 | 001 | 002 |
IP | 192.168.1.149 | 192.168.1.189 |
启动节点 | ca、orderer、peer0、couchdb、cli | peer1、couchdb、cli |
1. 说明与前期准备
1.1 说明
主要实现的功能是构建一个Fabric网络,其特点为,一个Orderer,一个Org(peer0和peer1)
其中:
- 主机A上运行orderer、ca、peer0,couchDB、cli
- 主机B上运行peer1、couchDB、cli
1.2 前期准备
为主机添加hosts映射
- 主机A(Peer0)
修改/etc/hosts,添加orderer和peer1节点的映射
192.168.1.149 orderer.example.com
192.168.1.189 peer1.org1.example.com
- 主机B(Peer1)
修改/etc/hosts,添加orderer和peer0节点的映射
192.168.1.149 orderer.example.com
192.168.1.149 peer0.org1.example.com
2. 修改配置
配置文件都在basic-network文件夹下
2.1 crypto-config.yaml
- 修改org中的Peer数
Template:
Count: 3 # 原来是1,需要添加几个Peer就改称几
# Start: 5
# Hostname: {{.Prefix}}{{.Index}} # default
- 运行脚本生成配置文件
# ./generate.sh
运行后会生成两个文件夹config、crypto-config
- 将config、crypto-config复制到主机A、B
(注意:这里可能会出现复制权限的问题,可以通过nautilus进行复制)
2.2 docker-compose.yml
- 主机A(peer0)
- ca服务配置中,FABRIC_CA_SERVER_CA_KEYFILE变量中,xxx_sk文件名要和新生成的/opt/gopath/src/github.com/hyperledger/fabric-samples/basic-network/crypto-config/peerOrganizations/org1.example.com/ca/9554549f657a3f46fc1a3cb4c0c28315499023e9c1bf9709418cbb28f3e26c49_sk下的文件链接一致,否则CA容器会启动失败
...
ca.example.com:
image: hyperledger/fabric-ca
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca.example.com
- FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/9554549f657a3f46fc1a3cb4c0c28315499023e9c1bf9709418cbb28f3e26c49_sk
...
- peer服务配置中,需要添加hosts文件映射
peer0.org1.example.com:
...
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- /etc/hosts:/etc/hosts # 添加内容在这里
depends_on:
# - orderer.example.com # 注释掉orderer节点依赖
- couchdb
networks:
- basic
...
- cli中添加对peer0节点的依赖
cli:
...
depends_on:
# - orderer.example.com
- peer0.org1.example.com
# - couchdb
...
- 主机B(peer1)
复制原始的docker-compose.yml文件,粘贴为docker-compose2.yml去除orderer和ca服务配置,并对peer和cli做修改
- peer
修改节点名称为peer0为peer1
volumes里添加hosts映射
依赖中去除orderer
peer1.org1.example.com:
container_name: peer1.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer1.org1.example.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
# # the following setting starts chaincode containers on the same
# # bridge network as the peers
# # https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
# The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
# provide the credentials for ledger to connect to CouchDB. The username and password must
# match the username and password set for the associated CouchDB.
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: peer node start
# command: peer node start --peer-chaincodedev=true
ports:
- 7051:7051
- 7053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- /etc/hosts:/etc/hosts
depends_on:
# - orderer.example.com
- couchdb
networks:
- basic
- cli
修改peer0为peer1
添加peer1节点依赖
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=info
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
- CORE_CHAINCODE_KEEPALIVE=10
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../chaincode/:/opt/gopath/src/github.com/
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
networks:
- basic
depends_on:
# - orderer.example.com
- peer1.org1.example.com
# - couchdb
2.3 修改脚本
- 主机A(peer0)
修改start.sh脚本,添加从容器中将信道文件导出到/tmp/目录下
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error, print all commands.
set -ev
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
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
# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}
# Create the channel
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
#复制信道文件
docker cp peer0.org1.example.com:/opt/gopath/src/github.com/hyperledger/fabric/mychannel.block /tmp/
# Join peer0.org1.example.com to the channel.
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
- 主机B(peer1)
复制原始的start.sh文件,粘贴为start2.sh文件,清空内容并写入如下:
set -ev
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
docker-compose -f docker-compose2.yml down
docker-compose -f docker-compose2.yml up -d peer1.org1.example.com couchdb
# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}
#复制信道文件
docker cp /tmp/mychannel.block peer1.org1.example.com:/opt/gopath/src/github.com/hyperledger/fabric/
# Join peer0.org1.example.com to the channel.
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer1.org1.example.com peer channel join -b mychannel.block
3. 启动网络
3.1 主机A(peer0)
- 在fabcar目录下, 新建startp3.sh脚本,写入
#!/bin/bash
#
# 没有链码实例化,用于多机分布运行
# Exit on first error
set -e
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
LANGUAGE=${1:-"golang"}
CC_SRC_PATH=github.com/fabcar/go
if [ "$LANGUAGE" = "node" -o "$LANGUAGE" = "NODE" ]; then
CC_SRC_PATH=/opt/gopath/src/github.com/fabcar/node
fi
# clean the keystore
# rm -rf ./hfc-key-store
# launch network; copy channel and join peer to channel
cd ../basic-network
./start.sh
# Now launch the CLI container in order to install, instantiate chaincode
docker-compose -f ./docker-compose.yml up -d cli
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 "$CC_SRC_PATH" -l "$LANGUAGE"
#sleep 10
printf "\nTotal setup execution time : $(($(date +%s) - starttime)) secs ...\n\n\n"
- 执行脚本
./startp3.sh
输出信息
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# ./startp3.sh
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
docker-compose -f docker-compose.yml down
Removing network net_basic
docker-compose -f docker-compose.yml up -d ca.example.com orderer.example.com peer0.org1.example.com couchdb
Creating network "net_basic" with the default driver
Creating couchdb ...
Creating ca.example.com ...
Creating orderer.example.com ...
Creating ca.example.com
Creating couchdb
Creating couchdb ... done
Creating peer0.org1.example.com ...
Creating peer0.org1.example.com ... done
# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}
# Create the channel
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
2018-08-04 11:10:24.416 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-04 11:10:24.429 UTC [channelCmd] InitCmdFactory -> INFO 002 Endorser and orderer connections initialized
2018-08-04 11:10:24.637 UTC [main] main -> INFO 003 Exiting.....
#复制信道文件
docker cp peer0.org1.example.com:/opt/gopath/src/github.com/hyperledger/fabric/mychannel.block /tmp/
# Join peer0.org1.example.com to the channel.
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
2018-08-04 11:10:25.032 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-04 11:10:25.477 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2018-08-04 11:10:25.477 UTC [main] main -> INFO 003 Exiting.....
couchdb is up-to-date
peer0.org1.example.com is up-to-date
Creating cli ...
Creating cli ... done
2018-08-04 11:10:28.657 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-08-04 11:10:28.657 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-08-04 11:10:28.657 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-08-04 11:10:28.657 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-08-04 11:10:28.657 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-08-04 11:10:28.676 UTC [golang-platform] getCodeFromFS -> DEBU 006 getCodeFromFS github.com/fabcar/go
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 007 Discarding GOROOT package bytes
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 008 Discarding GOROOT package crypto/md5
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 009 Discarding GOROOT package crypto/rand
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00a Discarding GOROOT package crypto/sha1
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00b Discarding GOROOT package encoding/binary
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00c Discarding GOROOT package encoding/hex
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00d Discarding GOROOT package encoding/json
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00e Discarding GOROOT package fmt
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 00f Discarding provided package github.com/hyperledger/fabric/core/chaincode/shim
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 010 Discarding provided package github.com/hyperledger/fabric/protos/peer
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 011 Discarding GOROOT package hash
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 012 Discarding GOROOT package io
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 013 Discarding GOROOT package net
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 014 Discarding GOROOT package os
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 015 Discarding GOROOT package strconv
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 016 Discarding GOROOT package sync
2018-08-04 11:10:28.749 UTC [golang-platform] func1 -> DEBU 017 Discarding GOROOT package time
2018-08-04 11:10:28.749 UTC [golang-platform] GetDeploymentPayload -> DEBU 018 done
2018-08-04 11:10:28.749 UTC [container] WriteFileToPackage -> DEBU 019 Writing file to tarball: src/github.com/fabcar/go/base.go
2018-08-04 11:10:28.750 UTC [container] WriteFileToPackage -> DEBU 01a Writing file to tarball: src/github.com/fabcar/go/codec.go
2018-08-04 11:10:28.750 UTC [container] WriteFileToPackage -> DEBU 01b Writing file to tarball: src/github.com/fabcar/go/fabcar.go
2018-08-04 11:10:28.751 UTC [container] WriteFileToPackage -> DEBU 01c Writing file to tarball: src/github.com/fabcar/go/generator.go
2018-08-04 11:10:28.751 UTC [container] WriteFileToPackage -> DEBU 01d Writing file to tarball: src/github.com/fabcar/go/uuid.go
2018-08-04 11:10:28.751 UTC [msp/identity] Sign -> DEBU 01e Sign: plaintext: 0AA0070A5C08031A0C08A49396DB0510...FEF9FF030000FFFFEEBE8AEB00EE0000
2018-08-04 11:10:28.751 UTC [msp/identity] Sign -> DEBU 01f Sign: digest: 28C319A973E2056DAA5B32666021EA5FC97098CF3B32B4A21EA230CDD21093FF
2018-08-04 11:10:28.940 UTC [chaincodeCmd] install -> DEBU 020 Installed remotely response:<status:200 payload:"OK" >
2018-08-04 11:10:28.940 UTC [main] main -> INFO 021 Exiting.....
Total setup execution time : 24 secs ...
- 查看容器
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4adad31d18a hyperledger/fabric-tools "/bin/bash" About a minute ago Up About a minute cli
7f8f7e055c56 hyperledger/fabric-peer "peer node start" About a minute ago Up About a minute 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
020338bccb30 hyperledger/fabric-couchdb "tini -- /docker-ent…" About a minute ago Up About a minute 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
bc78a01dfd54 hyperledger/fabric-ca "sh -c 'fabric-ca-se…" About a minute ago Up About a minute 0.0.0.0:7054->7054/tcp ca.example.com
b7f3a9b5c044 hyperledger/fabric-orderer "orderer" About a minute ago Up About a minute 0.0.0.0:7050->7050/tcp orderer.example.com
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
- 复制的信道文件发送到主机B的/tmp/目录下,可使用scp命令
scp /tmp/mychannel.block njust@192.168.1.189:/tmp/
scp [file] [user@ip:path]
3.2 主机B(peer1)
- 在fabcar目录下, 新建startp3.sh脚本,写入
#!/bin/bash
#
# 没有链码实例化,用于多机分布运行
# Exit on first error
set -e
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
LANGUAGE=${1:-"golang"}
CC_SRC_PATH=github.com/fabcar/go
if [ "$LANGUAGE" = "node" -o "$LANGUAGE" = "NODE" ]; then
CC_SRC_PATH=/opt/gopath/src/github.com/fabcar/node
fi
# clean the keystore
# rm -rf ./hfc-key-store
# launch network; copy channel and join peer to channel
cd ../basic-network
./start2.sh
# Now launch the CLI container in order to install, instantiate chaincode
docker-compose -f ./docker-compose2.yml up -d cli
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 "$CC_SRC_PATH" -l "$LANGUAGE"
# 链码实例化是针对信道
#sleep 10
printf "\nTotal setup execution time : $(($(date +%s) - starttime)) secs ...\n\n\n"
- 启动
./startp3.sh
输出信息
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# ./startp3.sh
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
docker-compose -f docker-compose2.yml down
Removing network net_basic
docker-compose -f docker-compose2.yml up -d peer1.org1.example.com couchdb
Creating network "net_basic" with the default driver
Creating couchdb ... done
Creating peer1.org1.example.com ... done
# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
export FABRIC_START_TIMEOUT=10
#echo ${FABRIC_START_TIMEOUT}
sleep ${FABRIC_START_TIMEOUT}
#复制信道文件
docker cp /tmp/mychannel.block peer1.org1.example.com:/opt/gopath/src/github.com/hyperledger/fabric/
# Join peer0.org1.example.com to the channel.
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer1.org1.example.com peer channel join -b mychannel.block
2018-08-04 11:15:13.772 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-04 11:15:14.210 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2018-08-04 11:15:14.210 UTC [main] main -> INFO 003 Exiting.....
couchdb is up-to-date
peer1.org1.example.com is up-to-date
Creating cli ... done
2018-08-04 11:15:17.698 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-04 11:15:17.698 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-04 11:15:17.913 UTC [main] main -> INFO 003 Exiting.....
Total setup execution time : 21 secs ...
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
- 查看容器docker ps -a
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4604cf199a48 hyperledger/fabric-tools "/bin/bash" 58 seconds ago Up 54 seconds cli
43268ee8c8f1 hyperledger/fabric-peer "peer node start" About a minute ago Up About a minute 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer1.org1.example.com
2660b48140fc hyperledger/fabric-couchdb "tini -- /docker-ent…" About a minute ago Up About a minute 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
3.3 实例化链玛容器
注: 链玛只需实例化一次,这里
- 在主机A(peer0) 上进行实例化
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 -l "golang" -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
输出信息:
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# 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 -l "golang" -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
2018-08-04 11:19:28.464 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-08-04 11:19:28.464 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-08-04 11:19:28.464 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-08-04 11:19:28.465 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-08-04 11:19:28.465 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-08-04 11:19:28.465 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AAB070A6708031A0C08C09796DB0510...324D53500A04657363630A0476736363
2018-08-04 11:19:28.465 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 392A62AFEA5325BD0E53FC7184058FD8C6FF5AEC332FBA665958B35791BAAC16
2018-08-04 11:19:41.291 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0AAB070A6708031A0C08C09796DB0510...B2F76D336D22A9D7378494C8CC8115C0
2018-08-04 11:19:41.291 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 63F3EF2B630C421311D557F5AEB4BBC0BAF4DE61C9799D476BF03B9BC92A024C
2018-08-04 11:19:41.292 UTC [main] main -> INFO 00a Exiting.....
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
- 查看主机A(peer0)容器docker ps -a
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
609e898a2163 dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba "chaincode -peer.add…" About a minute ago Up About a minute dev-peer0.org1.example.com-fabcar-1.0
b4adad31d18a hyperledger/fabric-tools "/bin/bash" 10 minutes ago Up 10 minutes cli
7f8f7e055c56 hyperledger/fabric-peer "peer node start" 10 minutes ago Up 10 minutes 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
020338bccb30 hyperledger/fabric-couchdb "tini -- /docker-ent…" 11 minutes ago Up 10 minutes 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
bc78a01dfd54 hyperledger/fabric-ca "sh -c 'fabric-ca-se…" 11 minutes ago Up 10 minutes 0.0.0.0:7054->7054/tcp ca.example.com
b7f3a9b5c044 hyperledger/fabric-orderer "orderer" 11 minutes ago Up 11 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
出现了实例化后的链玛容器
- 查看主机A(peer0)容器docker ps -a
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4604cf199a48 hyperledger/fabric-tools "/bin/bash" 7 minutes ago Up 7 minutes cli
43268ee8c8f1 hyperledger/fabric-peer "peer node start" 7 minutes ago Up 7 minutes 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer1.org1.example.com
2660b48140fc hyperledger/fabric-couchdb "tini -- /docker-ent…" 7 minutes ago Up 7 minutes 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
发现未出现链玛容器,不过不必担心,后面使用peer1进行交互时会自动构造链玛容器
4. 测试
4.1 测试思路
在主机B(peer1)上执行invoke操作,主机A(peer0)上查询结果
4.2 测试步骤
- 在peer1上执行invoke put A 9
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 '{"function":"put","Args":["A","9"]}'
结果
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# 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 '{"function":"put","Args":["A","9"]}'
2018-08-04 11:26:44.811 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-04 11:26:44.811 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-04 11:26:58.884 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 003 Chaincode invoke successful. result: status:200
2018-08-04 11:26:58.884 UTC [main] main -> INFO 004 Exiting.....
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
看到Chaincode invoke successful. result: status:200,无错误。
此时再次查看peer1上的容器docker ps -a
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5971e6d76f0 dev-peer1.org1.example.com-fabcar-1.0-791f4e9c59e5c5ae44b5e63c754aaf47cfa4757490019879665238e39ab45594 "chaincode -peer.add…" 3 minutes ago Up 3 minutes dev-peer1.org1.example.com-fabcar-1.0
4604cf199a48 hyperledger/fabric-tools "/bin/bash" 15 minutes ago Up 15 minutes cli
43268ee8c8f1 hyperledger/fabric-peer "peer node start" 15 minutes ago Up 15 minutes 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer1.org1.example.com
2660b48140fc hyperledger/fabric-couchdb "tini -- /docker-ent…" 15 minutes ago Up 15 minutes 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
root@002:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
发现已经完成了自动构建链玛容器的操作
- 在peer0上执行query A
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 query -C mychannel -n fabcar -c '{"function":"get","Args":["A"]}'
结果
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar# 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 query -C mychannel -n fabcar -c '{"function":"get","Args":["A"]}'
2018-08-04 11:31:50.352 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-08-04 11:31:50.352 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-08-04 11:31:50.352 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-08-04 11:31:50.352 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-08-04 11:31:50.352 UTC [chaincodeCmd] getChaincodeSpec -> DEBU 005 java chaincode disabled
2018-08-04 11:31:50.352 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AAD070A6908031A0C08A69D96DB0510...6661626361721A080A036765740A0141
2018-08-04 11:31:50.352 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: 9692CA5206F9EB14955BBADA83BAFBAFC194D8B0C065B92F25033B824C0AE99C
Query Result: 9
2018-08-04 11:31:50.356 UTC [main] main -> INFO 008 Exiting.....
root@001:/opt/gopath/src/github.com/hyperledger/fabric-samples/fabcar#
看到了刚刚在peer1上的invoke put A 9
验证成功!
5. 总结
起初在进行实验,遇到的问题是,单peer可以正常工作(执行了实例化的peer节点),但是另一个节点无法自动构造和使用链玛函数,提示:
Error: endorsement failure during query. response: status:500 message:"make sure the chaincode fabcar has been successfully instantiated and try again: getccdata mychannel/fabcar responded with error: could not find chaincode with name 'fabcar'"
翻查资料,最后看到某Fabric集群网络搭建中,服务中
- peer节点配置添加了关于hosts映射
- cli对peer节点配置依赖的方法
此时猜测是否是在网络启动时,脚本逻辑无问题,但是由于没有将peer主机与IP进行映射,使得在peer加入信道以及信道检测peer加入的过程中,无法正常执行。
随后按照思路,实验成功,但其中原理还有待挖掘...