Step-by-step搭建fabric 1.3.0 单organization的SOLO模式instance实例。
- 下载fabric 1.3.0 二进制包
#!/bin/bash
export VERSION=1.3.0
export ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
echo ${ARCH}-${VERSION}/hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
echo "===> Downloading platform binaries"
if [ ! -d fabric ]; then mkdir fabric; fi
curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/hyperledger-fabric-${ARCH}-${VERSION}.tar.gz | tar xz -C fabric
#install dep
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
执行完之后,在当前目录下包含fabric的二进制文件:
$ tree fabric
fabric
|-- bin
| |-- configtxgen
| |-- configtxlator
| |-- cryptogen
| |-- discover
| |-- get-docker-images.sh
| |-- idemixgen
| |-- orderer
| `-- peer
`-- config
|-- configtx.yaml
|-- core.yaml
`-- orderer.yaml
- 下载所有的images
$ fabric/bin/get-docker-images.sh
执行完之后会包含如下本地images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-tools amd64-1.3.0 c056cd9890e7 3 months ago 1.5GB
hyperledger/fabric-ccenv amd64-1.3.0 953124d80237 3 months ago 1.38GB
hyperledger/fabric-orderer amd64-1.3.0 f430f581b46b 3 months ago 145MB
hyperledger/fabric-peer amd64-1.3.0 f3ea63abddaa 3 months ago 151MB
hyperledger/fabric-zookeeper amd64-0.4.13 e62e0af39193 3 months ago 1.39GB
hyperledger/fabric-kafka amd64-0.4.13 4121ea662c47 3 months ago 1.4GB
hyperledger/fabric-couchdb amd64-0.4.13 1d3266e01e64 3 months ago 1.45GB
hyperledger/fabric-baseos amd64-0.4.13 f0fe49196c40 3 months ago 124MB
- 创建一个env文件
#!/bin/bash
cat > ./.env <
- 利用工具cryptogen生成证书文件
4.1 编辑fabric/config/crypto-config.yaml
$ cat fabric/config/crypto-config.yaml
PeerOrgs:
- Name: SampleOrg
Domain: sampleorg.example.com
Template:
Count: 5
Users:
Count: 5
Specs:
- Hostname: orderer
4.2 生成证书
#!/bin/bash
CRYPTOGEN=fabric/bin/cryptogen
${CRYPTOGEN} generate \
--config=fabric/config/crypto-config.yaml \
--output=gen/crypto-config
结果是在目录gen/crypto-config下面生成一系列证书。
- 生成orderer的genesisblock
5.1 修改fabric/config/configtx.yaml
$ diff -wuN fabric/config/configtx.yaml
@@ -235,7 +235,7 @@
# participation in ordering.
# NOTE: In the solo case, this should be a one-item list.
Addresses:
- - 127.0.0.1:7050
+ - orderer.sampleorg.example.com:7050
# Batch Timeout: The amount of time to wait before creating a batch.
BatchTimeout: 2s
@@ -450,6 +450,7 @@
Admins:
Type: Signature
Rule: "OR('SampleOrg.member')"
+ Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
两个地方的改动:
- 修改orderer的真实地址
- 添加Consortium指为SampleDevModeSolo Profile
5.2 生成genesis block
#!/bin/bash
CONFIGTXGEN=fabric/bin/configtxgen
mkdir -p gen/channel-artifacts
ln -snf $(pwd)/gen/crypto-config/peerOrganizations/sampleorg.example.com/msp \
fabric/config/
${CONFIGTXGEN} \
-profile SampleDevModeSolo \
-configPath fabric/config \
-outputBlock gen/channel-artifacts/genesis.block
结果就是生成一个genesisblock文件gen/channel-artifacts/genesis.block
- 创建channel.tx文件
#!/bin/bash
CHANNEL=mychannel
CONFIGTXGEN=fabric/bin/configtxgen
mkdir -p gen/channel-artifacts
${CONFIGTXGEN} \
-profile SampleDevModeSolo \
-configPath fabric/config \
-outputCreateChannelTx gen/channel-artifacts/${CHANNEL}.tx \
-channelID ${CHANNEL}
结果是生成channel文件:gen/channel-artifacts/${CHANNEL}.tx
- 启动docker-compose
7.1 配置docker-compose.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
networks:
byfn:
services:
orderer.sampleorg.example.com:
container_name: orderer.sampleorg.example.com
image: hyperledger/fabric-orderer:amd64-1.3.0
environment:
# - ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/etc/hyperledger/fabric/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=SampleOrg
- ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/fabric/msp
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/etc/hyperledger/fabric/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/etc/hyperledger/fabric/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/etc/hyperledger/fabric/tls/ca.crt]
# working_dir: /etc/hyperledger/fabric
# command: orderer
volumes:
- ./gen/channel-artifacts/genesis.block:/etc/hyperledger/fabric/orderer.genesis.block
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/orderer.sampleorg.example.com/msp:/etc/hyperledger/fabric/msp
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/orderer.sampleorg.example.com/tls/:/etc/hyperledger/fabric/tls
# ports:
# - 7050:7050
networks:
- byfn
peer0.sampleorg.example.com:
container_name: peer0.sampleorg.example.com
image: hyperledger/fabric-peer:amd64-1.3.0
environment:
- CORE_VM_ENDPOINT=unix:///var/run/docker.sock
# 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}_byfn
# - CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
- CORE_PEER_ID=peer0.sampleorg.example.com
- CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.sampleorg.example.com:7051
- CORE_PEER_LOCALMSPID=SampleOrg
# working_dir: /etc/hyperledger/fabric
# command: peer node start
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/peer0.sampleorg.example.com/msp:/etc/hyperledger/fabric/msp
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/peer0.sampleorg.example.com/tls:/etc/hyperledger/fabric/tls
# ports:
# - 7051:7051
# - 7073:7053
networks:
- byfn
peer1.sampleorg.example.com:
container_name: peer1.sampleorg.example.com
image: hyperledger/fabric-peer:amd64-1.3.0
environment:
- CORE_VM_ENDPOINT=unix:///var/run/docker.sock
# 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}_byfn
# - CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
- CORE_PEER_ID=peer1.sampleorg.example.com
- CORE_PEER_ADDRESS=peer1.sampleorg.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.sampleorg.example.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.sampleorg.example.com:7051
- CORE_PEER_LOCALMSPID=SampleOrg
# working_dir: /etc/hyperledger/fabric
# command: peer node start
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/peer1.sampleorg.example.com/msp:/etc/hyperledger/fabric/msp
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/peers/peer1.sampleorg.example.com/tls:/etc/hyperledger/fabric/tls
# ports:
# - 7061:7051
# - 7063:7053
networks:
- byfn
cli.sampleorg.example.com:
container_name: cli.sampleorg.example.com
image: hyperledger/fabric-tools:amd64-1.3.0
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
working_dir: /etc/hyperledger/fabric
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./fabric/config:/etc/hyperledger/fabric
- ./fabric/bin/discover:/usr/local/bin/discover
- ./gen/channel-artifacts/mychannel.tx:/etc/hyperledger/fabric/mychannel.tx
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/users/[email protected]/msp:/etc/hyperledger/fabric/msp
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/users/[email protected]/tls:/etc/hyperledger/fabric/tls
- ./gen/crypto-config/peerOrganizations/sampleorg.example.com/users/[email protected]:/etc/hyperledger/fabric/[email protected]
- ./chaincode:/opt/gopath/src/chaincode
depends_on:
- orderer.sampleorg.example.com
- peer0.sampleorg.example.com
- peer1.sampleorg.example.com
networks:
- byfn
7.2 启动docker-compose
$ docker-compose up -d
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------
cli.sampleorg.example.com /bin/bash Up
orderer.sampleorg.example.com orderer Up 7050/tcp
peer0.sampleorg.example.com peer node start Up
peer1.sampleorg.example.com peer node start Up
总共有4个containers启动。
- 访问fabric网络
下面的访问fabric网络就是fabric提供的基本的操作;注意所有的操作都是从CLI容器里面发起的命令。
8.1 创建channel
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/[email protected]/msp cli.sampleorg.example.com \
peer channel create \
-o orderer.sampleorg.example.com:7050 \
-c mychannel \
-f /etc/hyperledger/fabric/mychannel.tx \
--outputBlock /etc/hyperledger/fabric/mychannel.block \
--tls --cafile /etc/hyperledger/fabric/tls/ca.crt
8.2 peer加入channel
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/[email protected]/msp \
-e CORE_PEER_TLS_ENABLED=true \
-e CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt \
-e CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key \
-e CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt \
cli.sampleorg.example.com peer channel join \
-b /etc/hyperledger/fabric/mychannel.block
8.3 安装chaincode
链码chaincode请先下载,然后会映射到CLI容器里面;例如:
$ tree chaincode -L 2
chaincode
|-- chaintool
| `-- example02
|-- go
| |-- chaincode_example01
| |-- chaincode_example02
| |-- chaincode_example03
| |-- chaincode_example04
| |-- chaincode_example05
| `-- marbles02
`-- java
|-- Example
|-- SimpleSample
|-- chaincode_example02
`-- chaincode_example04
在下面的例子中,我们只使用了go/chaincode_example02一个例子。
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/[email protected]/msp \
-e CORE_PEER_TLS_ENABLED=true \
-e CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt \
-e CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key \
-e CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt \
cli.sampleorg.example.com peer chaincode install \
-n mychaincode \
-v 1.0 \
-p chaincode/go/chaincode_example02
8.4 实例化(instantiate) chaincode
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/[email protected]/msp \
-e CORE_PEER_TLS_ENABLED=true \
-e CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt \
-e CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key \
-e CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt \
cli.sampleorg.example.com peer chaincode instantiate \
-o orderer.sampleorg.example.com:7050 \
-C mychannel \
-n mychaincode \
-v 1.0 \
-c '{"Args":["init","a","1000","b","2000"]}' \
--tls --cafile /etc/hyperledger/fabric/tls/ca.crt
8.5 发起调用transaction
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp \
-e CORE_PEER_TLS_ENABLED=true \
-e CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt \
-e CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key \
-e CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt \
cli.sampleorg.example.com peer chaincode invoke \
-o orderer.sampleorg.example.com:7050 \
-C mychannel \
-n mychaincode \
-c '{"Args":["invoke", "a", "b", "1"]}' \
--tls --cafile /etc/hyperledger/fabric/tls/ca.crt
8.6 发起查询
$ docker exec \
-e CORE_PEER_ID=peer0.sampleorg.example.com \
-e CORE_PEER_ADDRESS=peer0.sampleorg.example.com:7051 \
-e CORE_PEER_LOCALMSPID=SampleOrg \
-e CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp \
-e CORE_PEER_TLS_ENABLED=true \
-e CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt \
-e CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key \
-e CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt \
cli.sampleorg.example.com peer chaincode query \
-C mychannel \
-n mychaincode \
-c '{"Args":["query","a"]}'
8.7 discover服务
$ cat discover.yaml
version: 0
tlsconfig:
certpath: ""
keypath: ""
peercacertpath: /etc/hyperledger/fabric/[email protected]/msp/tlscacerts/tlsca.sampleorg.example.com-cert.pem
timeout: 0s
signerconfig:
mspid: SampleOrg
identitypath: /etc/hyperledger/fabric/[email protected]/msp/signcerts/[email protected]
keypath: /etc/hyperledger/fabric/[email protected]/msp/keystore/7bceb9c74fe332a2ed3bc8661fe79b169dde61fd5705df7b51edca9b0a6d53ed_sk
$ docker exec cli.sampleorg.example.com \
discover \
--configFile discover.yaml peers \
--server peer0.sampleorg.example.com:7051 \
--channel mychannel
注意discover.yaml这个文件需要映射到CLI容易里面的路径。