fabric 手动构建网络

fabric源码基本结构

首先假设你已经获取到fabric的源码(基本架构由fabric1.0说明)

那么得到的fabric 1.0的源码目录如下:

image-20200502162949010

fabric 核心代码包

包名或文件名 相关功能描述
bccsp 包 实现对加解密算法和机制的支持
common 包 一些通用的模块
core 包 大部分核心实现代码都在本包下。其它包的代码封装上层接口,最终调用本包内代码
examples 包括一些示例的 chaincode 代码
gossip 包 实现 gossip 协议
msp 包 Member Service Provider 包
order 包 order 服务相关的入口和框架代码
peer 包 peer 的入口和框架代码
protos 包 包括各种协议和消息的 protobuf 定义文件和生成的 go 文件

相关辅助代码包

包名或文件名 相关功能描述
gotools golang 开发相关工具安装
vendor 包 管理依赖

安装部署包

名或文件名 相关功能描述
devenv包 配置开发环境
images 镜像生成模板等
scripts 各种安装配置脚本

除以上外其他说明

名或文件名 相关功能描述
.dockerignore 生成 Docker 镜像时忽略一些目录,包括 .git 目录
.gitattributes git 代码管理时候的属性文件,带有不同类型文件中换行符的规则
.gitignore git 代码管理时候忽略的文件和目录,包括 build 和 bin 等中间生成路径
LICENSE Apache 2 许可文件
docker-env.mk 被 Makefile 引用,生成 Docker 镜像时的环节变量
Makefile 执行测试、格式检查、安装依赖、生成镜像等操作。

手动构建网络

1. 配置go环境和docker环境

2. 编译cryptogen和configtxgen工具

​ 这两个工具可以生成fabric网络需要的证书和公私钥等信息

1
2
3
4
# 首先进入fabric的根目录
cd {fabric根目录}
# 编译cryptogen工具
make release

make时发生了错误:

  1. ltdl.h: No such file or directory

    解决方案:

    1
    2
    3
    >    # 安装依赖环境
    > apt install libltdl3-dev
    >

3 . 生成证书

1
2
3
4
# 进入到e2e_cli例子目录(只有fabric1.0.0有)
cd examples/e2e_cli
# 用cryptogen工具生成证书,
../../release/linux-amd64/bin/cryptogen generate --config=./crypto-config.yaml

生成的证书在文件夹 crypto-config 内,可用tree命令查看

4. 生成创世区块

1
2
SYS_CHANNEL="sys-channel"
../../release/linux-amd64/bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID $SYS_CHANNEL -outputBlock ./channel-artifacts/genesis.block

ps: 这里的profile指定的名称定义在文件 configtx.yaml里

生成创世区块报错

1
2
3
2020-05-02 17:51:32.611 +08 [common/configtx/tool] main -> INFO 001 Loading configuration
2020-05-02 17:51:32.611 +08 [common/configtx/tool/localconfig] Load -> CRIT 002 Error reading configuration: Unsupported Config Type ""
2020-05-02 17:51:32.611 +08 [common/configtx/tool] func1 -> ERRO 003 Could not find configtx.yaml. Please make sure that FABRIC_CFG_PATH is set to a path which contains configtx.yaml

解决办法:

​ 根据脚本所报的提示,现在缺少环境变量 FABRIC_CFG_PATH,我们加上

1
export FABRIC_CFG_PATH=$(pwd)

5. 生成channel 配置块

1
2
export CHANNEL_NAME="mychannel"
../../release/linux-amd64/bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

6. 生成锚节点更新配置块

1
2
../../release/linux-amd64/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../../release/linux-amd64/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

7. 利用docker-compose文件启动docekr容器网络

1
2
# 注意,没有的容器这里会进行拉取,注意流量
docker-compose -f ./docker-compose-cli.yaml up -d

这里出现版本问题了。。。。我决定换到1.4,重新手动部署

同样重复操作步骤 1-6。

需要注意的是,需要加入全局变量

1
2
3
CONSENSUS_TYPE="solo" # 共识方式,单order
SYS_CHANNEL="sys-channel" # 系统配置的channel name
CHANNEL_NAME="mychannel"

8. cli启动后,创建通道

1
2
3
4
5
6
7
8
9
10
CHANNEL_NAME="mychannel"
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

# setglobal
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=PEER0_ORG1_CA=/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/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
#创建通道
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA

9. 将各个结点加入到通道里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# peer0.org1
CORE_PEER_LOCALMSPID="Org1MSP"
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/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer channel join -b $CHANNEL_NAME.block
# peer1.org1
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
peer channel join -b $CHANNEL_NAME.block
# peer0.org2
CORE_PEER_LOCALMSPID="Org2MSP"
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer channel join -b $CHANNEL_NAME.block
# peer1.org2
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
peer channel join -b $CHANNEL_NAME.block

10. 更新锚节点

1
2
3
4
5
6
7
8
9
10
11
12
# peer0.org1
CORE_PEER_LOCALMSPID="Org1MSP"
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/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA
#peer0.org2
CORE_PEER_LOCALMSPID="Org2MSP"
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA

11. 安装链码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# peer0.org1
CORE_PEER_LOCALMSPID="Org1MSP"
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/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051

peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

# peer0.org2
CORE_PEER_LOCALMSPID="Org2MSP"
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

12. 实例化链码

1
2
3
4
5
CORE_PEER_LOCALMSPID="Org2MSP"
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"

13. 后续操作

  1. 查询

    1
    2
    3
    4
    5
    6
    # peer0.org1
    CORE_PEER_LOCALMSPID="Org1MSP"
    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/Admin@org1.example.com/msp
    CORE_PEER_ADDRESS=peer0.org1.example.com:7051
    peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

此时报错: failed to create new connection context deadline exceeded

经查,是第四步的生成创世区块命令有误,可能是版本差异造成的,1.0和1.4的创世区块生成命令不同,导致这里的错误

最后更新: 2020年05月18日 13:12

原始链接: /2020/05/18/2020-05-18-fabric根据构建脚本手动启动first-network/

× 请我吃糖~
打赏二维码