ubuntu18.04 Fabric1.1 双主机、双Peer节点、单Org环境搭建

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(peer0peer1)
其中:

  • 主机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)
  1. 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
...
  1. 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
    ...
  1. 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做修改
  1. 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
  1. 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)

  1. 在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"
  1. 执行脚本
./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 ...
  1. 查看容器
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# 
  1. 复制的信道文件发送到主机B的/tmp/目录下,可使用scp命令
scp /tmp/mychannel.block njust@192.168.1.189:/tmp/

scp [file] [user@ip:path]

3.2 主机B(peer1)

  1. 在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"
  1. 启动
./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# 
  1. 查看容器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 实例化链玛容器

注: 链玛只需实例化一次,这里

  1. 主机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#
  1. 查看主机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# 

出现了实例化后的链玛容器

  1. 查看主机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 测试步骤

  1. 在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# 

发现已经完成了自动构建链玛容器的操作

  1. 在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加入的过程中,无法正常执行。

随后按照思路,实验成功,但其中原理还有待挖掘...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容