基于solo手动搭建fabric服务
1.准备工作
机构组织 组织标志符 组织ID
Go org_go OrgGoMSP
Cpp org_cpp OrgCppMSP
概念:
msp
Membership service provider (MSP)是一个提供虚拟成员操作的管理框架的组件。
账号
都谁有msp
每个节点都有一个msp账号
每个用户都有msp账号
2.生成Fabric证书
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs: # 排序节点组织信息
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
#上边使用的域名, 在真实的生成环境中需要注册备案, 测试环境, 域名自己随便指定就可以
#根据要求编写好的配置文件, 配置文件名: crypto-config.yaml
- Name: Orderer # 排序节点组织的名字
Domain: htkj.com # 根域名, 排序节点组织的根域名
Specs:
- Hostname: orderer # 访问这台orderer对应的域名为: orderer.htkj.com
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org1
# ---------------------------------------------------------------------------
- Name: OrgGo # 第一个组织的名字, 自己指定
Domain: orggo.htkj.com # 访问第一个组织用到的根域名
EnableNodeOUs: true # 是否支持node.js
Template: # 模板, 根据默认的规则生成2个peer存储数据的节点
Count: 2 # 1. peer0.org1.example.com 2. peer1.org1.example.com
Users: # 创建的普通用户的个数
Count: 3
# ---------------------------------------------------------------------------
# Org2: See "Org1" for full specification
# ---------------------------------------------------------------------------
- Name: OrgCpp
Domain: orgcpp.htkj.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
证书生成命令
$ cryptogen generate --config=crypto-config.yaml
2. 生成创世块
2.1命令介绍
configtxgen --help
# 输出创始块区块文件的路径和名字
`-outputBlock string`
# 指定创建的channel的名字, 如果没指定系统会提供一个默认的名字.
`-channelID string`
# 表示输通道文件路径和名字
`-outputCreateChannelTx string`
# 指定配置文件中的节点
`-profile string`
# 更新channel的配置信息
`-outputAnchorPeersUpdate string`
# 指定所属的组织名称
`-asOrg string`
# 要想执行这个命令, 需要一个配置文件 configtx.yaml
2.2 编写配置文件
# configtx.yaml
---
################################################################################
# #
#Section: Organizations
# #
###############################################################################
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/htkj.com/msp
- &org_go
Name: OrgGoMSP
ID: OrgGoMSP
MSPDir: crypto-config/peerOrganizations/orggo.htkj.com/msp
AnchorPeers:
- Host: peer0.orggo.htkj.com
Port: 7051
- &org_cpp
Name: OrgCppMSP
ID: OrgCppMSP
MSPDir: crypto-config/peerOrganizations/orgcpp.htkj.com/msp
AnchorPeers:
- Host: peer0.orgcpp.htkj.com
Port: 7051
################################################################################
# #
#SECTION: Capabilities
# #
###############################################################################
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_2: true
################################################################################
# #
#SECTION: Application
# #
###############################################################################
Application: &ApplicationDefaults
Organizations:
################################################################################
Orderer: &OrdererDefaults
# Available types are "solo" and "kafka"
OrdererType: solo
Addresses:
- orderer.htkj.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 100
AbsoluteMaxBytes: 32 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
################################################################################
# #
#Profile
# #
###############################################################################
Profiles:
HtkjOrgsOrdererGenesis :
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *org_go
- *org_cpp
HtkjOrgsChannel :
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *org_go
- *org_cpp
Capabilities:
<<: *ApplicationCapabilities
2.3 生成创世块和通道文件
configtxgen -profile HtkjOrgsOrdererGenesis -outputBlock ./genesis.block
- 在当前目录下得到一个文件: genesis.block
- .block 就是区块文件
configtxgen -profile HtkjOrgsChannel -outputCreateChannelTx channel.tx -channelID htkjchannel
- 在当前目录生成通道文件:
2.4 生成锚节点更新文件(可选操作)
# 每个组织都对应一个锚节点的更新文件
# go组织锚节点文件
$ configtxgen -profile HtkjOrgsChannel -outputAnchorPeersUpdate GoMSPanchors.tx -channelID htkjchannel -asOrg OrgGoMSP
# cpp组织锚节点文件
$ configtxgen -profile HtkjOrgsChannel -outputAnchorPeersUpdate CppMSPanchors.tx -channelID htkjchannel -asOrg OrgCppMSP
#这里可能会报错,所以配置文件一定要检查清楚,以免出错.
3 docker-compose 启动前依赖的配置文件编写
3.1 docker-compose 配置文件编写
version: '2'
volumes:
orderer.htkj.com:
peer0.orggo.htkj.com:
peer1.orggo.htkj.com:
peer0.orgcpp.htkj.com:
peer1.orgcpp.htkj.com:
networks:
byfn:
services:
orderer.htkj.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.htkj.com
container_name: orderer.htkj.com
networks:
- byfn
peer0.orggo.htkj.com:
container_name: peer0.orggo.htkj.com
extends:
file: base/docker-compose-base.yaml
service: peer0.orggo.htkj.com
networks:
- byfn
peer1.orggo.htkj.com:
container_name: peer1.orggo.htkj.com
extends:
file: base/docker-compose-base.yaml
service: peer1.orggo.htkj.com
networks:
- byfn
peer0.orgcpp.htkj.com:
container_name: peer0.orgcpp.htkj.com
extends:
file: base/docker-compose-base.yaml
service: peer0.orgcpp.htkj.com
networks:
- byfn
peer1.orgcpp.htkj.com:
container_name: peer1.orgcpp.htkj.com
extends:
file: base/docker-compose-base.yaml
service: peer1.orgcpp.htkj.com
networks:
- byfn
cli:
container_name: cli
image: hyperledger/fabric-tools:latest
tty: true
stdin_open: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
#- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.orggo.htkj.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.htkj.com/users/Admin@orggo.htkj.com/msp
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/chaincode
- ./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:
- orderer.htkj.com
- peer0.orggo.htkj.com
- peer1.orggo.htkj.com
- peer0.orgcpp.htkj.com
- peer1.orgcpp.htkj.com
networks:
- byfn
3.2 docker-compose-base (docker-compose 所依赖的配置文件)
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
orderer.htkj.com:
container_name: orderer.htkj.com
image: hyperledger/fabric-orderer:latest
environment:
- ORDERER_GENERAL_LOGLEVEL=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- 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/htkj.com/orderers/orderer.htkj.com/msp:/var/hyperledger/orderer/msp
- ../crypto-config/ordererOrganizations/htkj.com/orderers/orderer.htkj.com/tls/:/var/hyperledger/orderer/tls
- orderer.htkj.com:/var/hyperledger/production/orderer
# /var/lib/docker/volumes/order.htkj.com
ports:
- 7050:7050
peer0.orggo.htkj.com:
container_name: peer0.orggo.htkj.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.orggo.htkj.com
- CORE_PEER_ADDRESS=peer0.orggo.htkj.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.orggo.htkj.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orggo.htkj.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/tls:/etc/hyperledger/fabric/tls
- peer0.orggo.htkj.com:/var/hyperledger/production
ports:
- 7051:7051
- 7053:7053
peer1.orggo.htkj.com:
container_name: peer1.orggo.htkj.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.orggo.htkj.com
- CORE_PEER_ADDRESS=peer1.orggo.htkj.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orggo.htkj.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.orggo.htkj.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/orggo.htkj.com/peers/peer1.orggo.htkj.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/orggo.htkj.com/peers/peer1.orggo.htkj.com/tls:/etc/hyperledger/fabric/tls
- peer1.orggo.htkj.com:/var/hyperledger/production
ports:
- 8051:7051
- 8053:7053
peer0.orgcpp.htkj.com:
container_name: peer0.orgcpp.htkj.com
extends:
#这里依赖 peer-base.yaml 文件
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.orgcpp.htkj.com
- CORE_PEER_ADDRESS=peer0.orgcpp.htkj.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orgcpp.htkj.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.orgcpp.htkj.com:7051
- CORE_PEER_LOCALMSPID=OrgCppMSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/orgcpp.htkj.com/peers/peer0.orgcpp.htkj.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/orgcpp.htkj.com/peers/peer0.orgcpp.htkj.com/tls:/etc/hyperledger/fabric/tls
- peer0.orgcpp.htkj.com:/var/hyperledger/production
ports:
- 9051:7051
- 9053:7053
peer1.orgcpp.htkj.com:
container_name: peer1.orgcpp.htkj.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.orgcpp.htkj.com
- CORE_PEER_ADDRESS=peer1.orgcpp.htkj.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orgcpp.htkj.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.orgcpp.htkj.com:7051
- CORE_PEER_LOCALMSPID=OrgCppMSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/orgcpp.htkj.com/peers/peer1.orgcpp.htkj.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/orgcpp.htkj.com/peers/peer1.orgcpp.htkj.com/tls:/etc/hyperledger/fabric/tls
- peer1.orgcpp.htkj.com:/var/hyperledger/production
ports:
- 10051:7051
- 10053:7053
3.3 peer-base 文件编写 (docker-comepose-base 所依赖的文件)
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer-base:
image: hyperledger/fabric-peer:latest
environment:
- CORE_VM_ENDPOINT=unix:///host/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=htkj_byfn
- CORE_LOGGING_LEVEL=INFO
#- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=htkj
- CORE_PEER_PROFILE_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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
4 启动docker-compose
CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_byfn -> _byfn
创建的网络叫: htkj_byfn
- byfn: 网络名
- htkj: docker-compose.yaml所在的目录
检测网络是否正常启动了:
# 在docker-compose.yaml 文件目录下执行下边命令
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------
cli /bin/bash Up
orderer.htkj.com orderer Up 0.0.0.0:7050->7050/tcp
peer0.orgcpp.htkj.com peer node start Up 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp
peer0.orggo.htkj.com peer node start Up 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp
peer1.orgcpp.htkj.com peer node start Up 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp
peer1.orggo.htkj.com peer node start Up 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp
#如果 state 为 up 则为启动成功
#如果 state 为 exit 则为启动失败
5 peer操作指令
5.1 创建通道
$ peer channel create [flags], 常用参数为:
-o, --orderer: orderer节点的地址
-c, --channelID: 要创建的通道的ID, 必须小写, 在250个字符以内
-f, --file: 由configtxgen 生成的通道文件, 用于提交给orderer
-t, --timeout: 创建通道的超时时长, 默认为5s
--tls: 通信时是否使用tls加密
--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# orderer节点pem格式的tls证书文件路径参考:
crypto-config/ordererOrganizations/htkj.com/orderers/orderer.htkj.com/msp/tlscacerts/tlsca.htkj.com-cert.pem
# example
$ peer channel create -o orderer节点地址:端口 -c 通道名 -f 通道文件 --tls true --cafile orderer节点pem格式的证书文件
- orderer节点地址: 可以是IP地址或者域名
- orderer节点监听的是7050端口
$ peer channel create -o orderer.htkj.com:7050 -c htkj-f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/htkj.com/msp/tlscacerts/tlsca.htkj.com-cert.pem
# 在当前工作目录下生成一个文件: 通道名.block, 本例: htkj.block
$ ls
channel-artifacts crypto `htkjchannel.block` --> 生成的文件
5.2 加入通道
$ peer channel join[flags], 常用参数为:
-b, --blockpath: 通过 peer channel create 命令生成的通道文件
# example
$ peer channel join -b 生成的通道block文件
$ peer channel join -b hktjchannel.block
5.3 更新锚节点
$ peer channel update [flags], 常用参数为:
-o, --orderer: orderer节点的地址
-c, --channelID: 要创建的通道的ID, 必须小写, 在250个字符以内
-f, --file: 由configtxgen 生成的组织锚节点文件, 用于提交给orderer
--tls: 通信时是否使用tls加密
--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# orderer节点pem格式的tls证书文件路径参考:
crypto-config/ordererOrganizations/htkj.com/orderers/orderer.htkj.com/msp/tlscacerts/tlsca.htkj.com-cert.pem
# example
$ peer channel update -o orderer节点地址:端口 -c 通道名 -f 锚节点更新文件 --tls true --cafile orderer节点pem格式的证书文件
6 链码(智能合约)
6.1安装智能合约
$ peer chaincode install [flags], 常用参数为:
-c, --ctor: JSON格式的构造参数, 默认是"{}"
-l, --lang: 编写chaincode的编程语言, 默认值是 golang
-n, --name: chaincode的名字
-p, --path: chaincode源代码的目录, 从 $GOPATH/src 路径后开始写
-v, --version: 当前操作的chaincode的版本, 适用这些命令install/instantiate/upgrade
# example
$ peer chaincode install -n 链码的名字 -v 链码的版本 -l 链码的语言 -p 链码的位置
- 链码名字自己起
- 链码的版本, 自己根据实际情况指定
$ peer chaincode install -n testcc -v 1.0 -l golang -p github.com/chaincode
6.2 链码初始化
$ peer chaincode instantiate [flags], 常用参数为:
-C,--channelID:当前命令运行的通道,默认值是“testchainid"。
-c, --ctor:JSON格式的构造参数,默认值是“{}"
-l,--lang:编写Chaincode的编程语言,默认值是golang
-n,--name:Chaincode的名字。
-P,--policy:当前Chaincode的背书策略。
-v,--version:当前操作的Chaincode的版本,适用于install/instantiate/upgrade等命令
--tls: 通信时是否使用tls加密
--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# example
# -c '{"Args":["init","a","100","b","200"]}'
# -P "AND ('OrgGoMSP.member', 'OrgCppMSP.member')"
$ peer chaincode instantiate -o orderer节点地址:端口 --tls true --cafile orderer节点pem格式的证书文件 -C 通道名称 -n 链码名称 -l 链码语言 -v 链码版本 -c 链码Init函数调用 -P 背书策略
$ peer chaincode instantiate -o orderer.htkj.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/htkj.com/msp/tlscacerts/tlsca.htkj.com-cert.pem -C htkjchannel -n testcc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('OrgGoMSP.member', 'OrgCppMSP.member')"
7 cli 基于linux客户端查询
7.1 查询
$ peer chaincode query [flags], 常用参数为:
-n,--name:Chaincode的名字。
-C,--channelID:当前命令运行的通道,默认值是“testchainid"
-c, --ctor:JSON格式的构造参数,默认值是“{}"
-x,--hex:是否对输出的内容进行编码处理
-r,--raw:是否输出二进制内容
-t, --tid: 指定当前查询的编号
# example
# '{"Args":["query","a"]}'
$ peer chaincode query -C 通道名称 -n 链码名称 -c 链码调用
** 7.2 交易**
$ peer chaincode invoke [flags], 常用参数为:
-o, --orderer: orderer节点的地址
-C,--channelID:当前命令运行的通道,默认值是“testchainid"
-c, --ctor:JSON格式的构造参数,默认值是“{}"
-n,--name:Chaincode的名字
--tls: 通信时是否使用tls加密
--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
--peerAddresses: 指定要连接的peer节点的地址
--tlsRootCertFiles: 连接的peer节点的TLS根证书
# 连接的peer节点的TLS根证书查找路径参考:
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.htkj.com/peers/peer0.orggo.htkj.com/tls/ca.crt
# example
# -c '{"Args":["invoke","a","b","10"]}'
$ peer chaincode invoke -o orderer节点地址:端口 --tls true --cafile orderer节点pem格式的证书文件 -C 通道名称 -n 链码名称 --peerAddresses 背书节点1:端口 --tlsRootCertFiles 背书节点1的TLS根证书 --peerAddresses 背书节点2:端口 --tlsRootCertFiles 背书节点2的TLS根证书 -c 交易链码调用
#关于示例链码,会在接下来的帖子附上