目标
实践一个简化版本的 fabric 1.1 网络,在一台服务器中部署完成,并执行 chaincode。
包括:
- 1个orderer 节点
- 2个组织,每个组织1个peer节点
- chaincode 使用 fabric-release-1.1 中的 examples/chaincode/go/chaincode_example02
实践本示例最好先把 fabric 基础跑通,可以参考之前的文章:
HyperLedger Fabric 1.1 End-2-End 示例
思路
- 生成基础文件,如权限、创始块相关文件
- 部署 orderer 服务
- 部署一个 peer 服务,在这个 peer 上安装 chaincode,执行测试操作,这时,一个最小化的 fabric 网络就完成了
- 部署其他 peer 服务,加入到现有网络中,并在其中执行 chaincode 进行测试
步骤说明
- 写配置文件 生成权限文件、创始块
- 写orderer配置文件 启动
- 写org1的peer配置文件 启动
- 安装执行 chaincode
- 写org2的peer配置文件 启动 执行chaincode
- org2的peer中执行chaincode
详细步骤
(1)写配置文件 生成权限文件、创始块
创建测试项目目录 fabrictest,并把 chaincode_example02 拷贝到 fabrictest/chaincode/go
目录下。
写配置文件 fabrictest/crypto-config.yaml,内容:
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com
Template:
Count: 1
Users:
Count: 1
- Name: Org2
Domain: org2.example.com
Template:
Count: 1
Users:
Count: 1
执行命令生成安全文件:
cryptogen generate --config=./crypto-config.yaml
# 返回信息
org1.example.com
org2.example.com
会自动生成一个文件夹 crypto-config,里面是各个组织的安全文件,现在项目的目录结构如下:
├── chaincode
│ └── go
│ └── chaincode_example02
│ ├── chaincode_example02.go
│ └── chaincode_example02_test.go
├── crypto-config.yaml
├── crypto-config
│ ├── ordererOrganizations
│ │ └── example.com
│ │ ├── ca
│ │ ├── msp
│ │ ├── orderers
│ │ ├── tlsca
│ │ └── users
│ └── peerOrganizations
│ ├── org1.example.com
│ │ ├── ca
│ │ ├── msp
│ │ ├── peers
│ │ ├── tlsca
│ │ └── users
│ └── org2.example.com
│ ├── ca
│ ├── msp
│ ├── peers
│ ├── tlsca
│ └── users
编写文件 fabrictest/configtx.yaml,内容:
Profiles:
TwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
AnchorPeers:
- Host: peer0.org2.example.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
执行命令,生成创始块和频道认证文件:
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
目录结构:
├── chaincode
│ └── go
├── channel-artifacts
│ ├── genesis.block
│ └── mychannel.tx
├── configtx.yaml
├── crypto-config
│ ├── ordererOrganizations
│ └── peerOrganizations
└── crypto-config.yaml
(2)写orderer配置文件 启动
编写 docker-orderer.yaml,内容:
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer
environment:
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabrictest_default
- ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_LISTENPORT=7050
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- ORDERER_GENERAL_TLS_ENABLED=false
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
networks:
default:
aliases:
- aberic
ports:
- 7050:7050
启动:
docker-compose -f docker-orderer.yaml up -d
检查:
docker ps --format 'table {{.Names}}\t{{.Ports}}\t{{.Status}}'
NAMES PORTS STATUS
orderer.example.com 0.0.0.0:7050->7050/tcp Up 57 seconds
(3)写org1的peer配置文件 启动
编写 docker-peer01.yaml,内容:
version: '2'
services:
couchdb:
container_name: couchdb
image: hyperledger/fabric-couchdb
ports:
- "5984:5984"
ca:
container_name: ca
image: hyperledger/fabric-ca
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca
- FABRIC_CA_SERVER_TLS_ENABLED=false
- FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/ab7dca5e5f6b1cc24c2023764c5b34d1f78d8614d2a11e74178d2d5509bd3be8_sk
ports:
- "7054:7054"
command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/ab7dca5e5f6b1cc24c2023764c5b34d1f78d8614d2a11e74178d2d5509bd3be8_sk -b admin:adminpw -d'
volumes:
- ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
peer0.org1.example.com:
container_name: peer0.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_NETWORKID=fabrictest
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabrictest
- CORE_LOGGING_LEVEL=DEBUG
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabrictest_default
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=false
- CORE_PEER_TLS_ENABLED=false
- 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
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 7051:7051
- 7052:7052
- 7053:7053
depends_on:
- couchdb
networks:
default:
aliases:
- fabrictest
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=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
- 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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/fabrictest/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org1.example.com
注意,ca 服务中在 fabric-ca-server-config 后面有一个很长的文件名,以 _sk 结尾,这个需要根据自己的情况修改,到自己的 crypto-config/peerOrganizations/org1.example.com/ca/ 文件夹下查看实际的文件名,修改上面的配置文件,共有两处。
启动:
docker-compose -f docker-peer01.yaml up -d
检查:
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'
NAMES IMAGE STATUS
cli hyperledger/fabric-tools Up About a minute
peer0.org1.example.com hyperledger/fabric-peer Up About a minute
couchdb hyperledger/fabric-couchdb Up About a minute
ca hyperledger/fabric-ca Up About a minute
orderer.example.com hyperledger/fabric-orderer Up 12 minutes
(4)安装执行 chaincode
进入 cli 容器:
docker exec -it cli bash
容器内执行命令:
# 创建channel
peer channel create -o orderer.example.com:7050 -c mychannel -t 50 -f ./channel-artifacts/mychannel.tx
# 加入channel
peer channel join -b mychannel.block
# 安装 chaincode
peer chaincode install -n mychannel -p github.com/hyperledger/fabric/fabrictest/chaincode/go/chaincode_example02 -v 1.0
# 实例化
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mychannel -c '{"Args":["init","A","10","B","10"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" -v 1.0
# 查询
peer chaincode query -C mychannel -n mychannel -c '{"Args":["query", "A"]}'
# 结果为 Query Result: 10
peer chaincode query -C mychannel -n mychannel -c '{"Args":["query", "B"]}'
# 转移资产
peer chaincode invoke -C mychannel -n mychannel -c '{"Args":["invoke", "A", "B", "1"]}'
# 查询验证
peer chaincode query -C mychannel -n mychannel -c '{"Args":["query", "A"]}'
# 结果为 Query Result: 9
(5)写org2的peer配置文件 启动 执行chaincode
编写 docker-peer02.yaml,内容:
version: '2'
services:
peer0.org2.example.com:
container_name: peer0.org2.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_NETWORKID=fabrictest
- CORE_PEER_ADDRESS=peer0.org2.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabrictest
- CORE_LOGGING_LEVEL=DEBUG
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabrictest_default
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=false
- CORE_PEER_TLS_ENABLED=false
- 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
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 8051:7051
- 8052:7052
- 8053:7053
networks:
default:
aliases:
- fabrictest
启动:
docker-compose -f docker-peer02.yaml up -d
检查:
docker ps --format 'table {{.Names}}\t{{.Ports}}'
NAMES PORTS
peer0.org2.example.com 0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp
fabrictest-peer0.org1.example.com-mychannel-1.0
cli
peer0.org1.example.com 0.0.0.0:7051-7053->7051-7053/tcp
couchdb 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp
ca 0.0.0.0:7054->7054/tcp
orderer.example.com 0.0.0.0:7050->7050/tcp
加入 channel :
docker exec \
-e "CORE_PEER_ID=peer0.org2.example.com" \
-e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" \
-e "CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052" \
-e "CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051" \
-e "CORE_PEER_LOCALMSPID=Org2MSP" \
-e "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" \
-e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp" \
cli \
peer channel join -b mychannel.block
此命令中设置了多个环境变量,这是因为 cli 容器是属于 peer01 的,为了让其变为 peer02 就需要通过修改环境变量来实现。
安装chain code:
docker exec \
-e "CORE_PEER_ID=peer0.org2.example.com" \
-e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" \
-e "CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052" \
-e "CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051" \
-e "CORE_PEER_LOCALMSPID=Org2MSP" \
-e "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" \
-e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp" \
cli \
peer chaincode install -n mychannel -p github.com/hyperledger/fabric/fabrictest/chaincode/go/chaincode_example02 -v 1.0
查询测试:
docker exec \
-e "CORE_PEER_ID=peer0.org2.example.com" \
-e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" \
-e "CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052" \
-e "CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051" \
-e "CORE_PEER_LOCALMSPID=Org2MSP" \
-e "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" \
-e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp" \
cli \
peer chaincode query -C mychannel -n mychannel -c '{"Args":["query", "A"]}'
查询结果:
Query Result: 9
和第一个peer中一致。