Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

2020年1月13日11:07:10Fabric评论2,055阅读模式
摘要

Fabric溯源场景网络部署(Native多机部署,也就是不用docker的方式),中间件开发,区块链浏览器开发,智能合约案例开发。

Fabric多机Native方式部署

本案例联盟架构中含有三个组织,生成商、代理商以及运营商。生成商及代理商向B端用户提供服务,而运营商向C端用户提供服务。生产商和代理商是溯源信息的上链者,当一件商品从生产成品到各代理处流转,每到一处责任点则将自己对应的信息上链。而当C端用户查询整个溯源信息时,向运营商组织节点发出请求,由于整个通道内账本数据一致,因此获得完整的溯源信息,与此同时,运营商处的账本对用户的查询进行存储,对可疑的查询进行预警,方便后台管理系统统计。在整个通道各组织中,厂商及代理商组织处存储产品溯源信息的账本,以明文的方式开放给通道所有成员查询;而对于运营商组织的账本,会有来自C端用户的敏感信息,必须加密存储,使其余组织不可访问。采用多机Native方式部署,节点架构如下所示。

Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

 

一. 基本环境准备

准备orderer,org1,org2,org3四台虚拟机。

安装docker

host修改

vim /etc/hosts
172.27.43.200 orderer.trace.com
172.27.43.201 org1.trace.com
172.27.43.202 org2.trace.com
172.27.43.203 org3.trace.com

下载二进制文件

https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/

将生成的二进制文件放入系统目录

(rm -rf configtx configtxlator configtxlator cryptogen discover get-docker-images.sh idemixgen orderer peer configtxgen)
cp /home/wang/sourceOrSoftware/bin/* /usr/local/bin

二. 在Orderer节点操作

以下.yaml文件要严格遵照格式,否则会失败报错。

1. 生成证书材料

在/home/wang/sourceOrSoftware/traceabilityProjectConfig文件夹下创建crypto-config.yaml

并在该目录下运行:

export FABRIC_CFG_PATH=$PWD
cryptogen generate --config=crypto-config.yaml --output ./crypto-config
2. 交易配置文件

在traceabilityProjectConfig文件夹下创建configtx.yaml文件

并在该目录下运行:

configtxgen -profile TestOrgsOrdererGenesis -outputBlock ./orderer.genesis.block

Orderer 模块启动之前需要生成一个创始块文件和Orderer 模块所需的配置文件。创始

块是Fabric 的第一个区块,主要存储相关的配置信息。

将整个文件夹赋值给每个Org节点

scp -r traceabilityProjectConfig/ root@172.27.43.201:/home/wang/sourceOrSoftware/
scp -r traceabilityProjectConfig/ root@172.27.43.202:/home/wang/sourceOrSoftware/
scp -r traceabilityProjectConfig/ root@172.27.43.203:/home/wang/sourceOrSoftware/

三. 在各peer节点上操作

1. 在org1节点

在traceabilityProjectConfig目录下执行下面命令,生成通道提案文件(traceChannel.tx):

export set FABRIC_CFG_PATH=$pwd
configtxgen -profile TestOrgsChannel -outputCreateChannelTx ./tracechannel.tx -channelID tracechannel

(注意:channelID不能含有数字,也不能有大写字母!“Channel names can only contain lowercase alphanumeric characters,dots, and dashes.”)

生成锚通知提案文件

configtxgen -profile TestOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID tracechannel -asOrg Org1MSP

锚节点通知提案负责将Channel 创建的消息通知告诉锚节点,锚节点负责通知其他组织

Channel 创建的消息。

2.在org2节点
cd /home/wang/sourceOrSoftware/traceabilityProjectConfig
export set FABRIC_CFG_PATH=$pwd

生成锚节点文件

configtxgen -profile TestOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID tracechannel -asOrg Org2MSP
3.在org3节点
cd /home/wang/sourceOrSoftware/traceabilityProjectConfig
export set FABRIC_CFG_PATH=$pwd

生成锚节点文件

configtxgen -profile TestOrgsChannel -outputAnchorPeersUpdate ./Org3MSPanchors.tx -channelID tracechannel -asOrg Org3MSP

四. Orderer节点启动

在orderer节点操作

traceabilityProjectConfig文件夹下创建orderer文件夹

再在traceabilityProjectConfig/orderer下创建orderer.yaml文件:

cd /home/wang/sourceOrSoftware/traceabilityProjectConfig/orderer/
export set FABRIC_CFG_PATH=$pwd
orderer start

五. peer节点启动

1. org1节点

配置文件创建

在traceabilityProjectConfig创建文件夹peer,用于存放所有该peer节点的数据。

在traceabilityProjectConfig/peer文件夹中创建:

启动peer

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
peer node start >> log_peer.log 2>&1 &

(若退出:则 lsof -i:7051杀掉进程重进,如下才是正确的)

Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

创建通道创始块(org1)

export set CORE_PEER_LOCALMSPID=Org1MSP
(创建channel的组织的编号)
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org1.trace.com/users/<a>Admin@org1.trace.com</a>/msp
(执行创建Channel的用户账号)
peer channel create -t 50 -o orderer.trace.com:7050 -c tracechannel -f /home/wang/sourceOrSoftware/traceabilityProjectConfig/tracechannel.tx

加入通道

export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org1.trace.com/users/<a>Admin@org1.trace.com</a>/msp/
peer channel join -b /home/wang/sourceOrSoftware/traceabilityProjectConfig/tracechannel.block

通知锚节点

export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org1.trace.com/users/<a>Admin@org1.trace.com</a>/msp/
peer channel update -o orderer.trace.com:7050 -c tracechannel -f /home/wang/sourceOrSoftware/traceabilityProjectConfig/Org1MSPanchors.tx

此时,org1已加入到tracechannel通道当中了。此时,org1_producer节点,文件夹下内容:

Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

其余节点:

Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

2. 其余peer节点

其余peer节点想要加入名为tracechannel的channel,必须首先获取tracechannel的block文件。

cd peer/

(1)发送peer文件

scp core.yaml root@172.27.43.202:/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer
scp core.yaml root@172.27.43.203:/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer

并进行修改(将org1改为orgX):

org1:

org2:

org3:

(将上面三个文件下载后都改名为core.yaml)

(2) 启动peer节点:

org2:

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/

peer node start

(此处执行错误时:如fabric Error creating GRPC server,是环境变量未export,按前面重新弄一次或换个窗口。)

org3:

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
peer node start
3. 发送tracechannel.block给其余节点
cd /home/wang/sourceOrSoftware/traceabilityProjectConfig

org1:

scp tracechannel.block root@172.27.43.202:/home/wang/sourceOrSoftware/traceabilityProjectConfig/

scp tracechannel.block root@172.27.43.203:/home/wang/sourceOrSoftware/traceabilityProjectConfig/

org2:

export set CORE_PEER_LOCALMSPID=Org2MSP

export set CORE_PEER_ADDRESS=peer0.org2.trace.com:7051

export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org2.trace.com/users/Admin@org2.trace.com/msp/

(必须先启动peer)

peer channel join -b /home/wang/sourceOrSoftware/traceabilityProjectConfig/tracechannel.block

peer channel update -o orderer.trace.com:7050 -c tracechannel -f /home/wang/sourceOrSoftware/traceabilityProjectConfig/Org2MSPanchors.tx

org3:

export set CORE_PEER_LOCALMSPID=Org3MSP
export set CORE_PEER_ADDRESS=peer0.org3.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org3.trace.com/users/<a>Admin@org3.trace.com</a>/msp/
(必须先启动peer)
peer channel join -b /home/wang/sourceOrSoftware/traceabilityProjectConfig/tracechannel.block
peer channel update -o orderer.trace.com:7050 -c tracechannel -f /home/wang/sourceOrSoftware/traceabilityProjectConfig/Org3MSPanchors.tx
4. 启动当前组织的Fabric-ca

在项目初始化时,采用了cryptogen模块生成了相关的账号,但这种方式无法满足动态添加用户账户信息的场景,因此需要将Fabric-ca-server的配置文件fabric-ca-server-config.yaml中设置以下信息完成绑定

Fabric溯源场景Native部署+中间件+区块链浏览器+智能合约案例开发

六. chaincode的开发部署

1.安装链码

org1:

安装链码

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
export set ORDERER_GENERAL_LOGLEVEL=debug
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org1.trace.com/users/<a>Admin@org1.trace.com</a>/msp/
peer chaincode install -n cc_producer -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_producer/main
2.实例化Chaincode

实例化之前需先保持docker启动:

rm -rf /var/lib/containerd/io.containerd.runtime.v2.task/docker/dockerd rm -rf /run/containerd/io.containerd.runtime.v2.task/docker/dockerd
service docker restart

 

peer chaincode instantiate -o orderer.trace.com:7050 -C tracechannel -ncc_producer -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org1MSP.member','Org2MSP.member','Org3MSP.member')"

启动node程序进行url调用方法。

org2:

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
export set ORDERER_GENERAL_LOGLEVEL=debug
export set CORE_PEER_LOCALMSPID=Org2MSP
export set CORE_PEER_ADDRESS=peer0.org2.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org2.trace.com/users/<a>Admin@org2.trace.com</a>/msp/

安装链码

peer chaincode install -n cc_agency -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_agency/main

实例化链码

peer chaincode instantiate -o orderer.trace.com:7050 -C tracechannel -n cc_agency -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org1MSP.member','Org2MSP.member','Org3MSP.member')"

org3:

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
export set ORDERER_GENERAL_LO
GLEVEL=debug
export set CORE_PEER_LOCALMSPID=Org3MSP
export set CORE_PEER_ADDRESS=peer0.org3.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org3.trace.com/users/<a>Admin@org3.trace.com</a>/msp/

安装链码

peer chaincode install -n cc_retailer -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_retailer/main

实例化链码

peer chaincode instantiate -o orderer.trace.com:7050 -C tracechannel -n cc_retailer -v 1.0 -c '{"Args":["init","A","B","C"]}' -P "OR ('Org1MSP.member','Org2MSP.member','Org3MSP.member')"

由于org3的cc_salesterminal链码调用了cc_producer和cc_agency链码,所以(无需实例化)

peer chaincode install -n cc_producer -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_producer/main
peer chaincode install -n cc_agency -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_agency/main

nodejs后台开发

源码地址:https://github.com/wanghaoyi1/traceability-fabric-Demo-node

后台服务代码

fabricapiSever.js

运行入口

区块链浏览器

 

备注

一. docker删除

docker ps -a|awk '{print $1}'|xargs docker rm

然后

docker rmi <ID>

二. 链码更新:

先安装下一个版本

export set FABRIC_CFG_PATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/peer/
export set ORDERER_GENERAL_LOGLEVEL=debug
export set CORE_PEER_LOCALMSPID=Org3MSP
export set CORE_PEER_ADDRESS=peer0.org3.trace.com:7051
export set CORE_PEER_MSPCONFIGPATH=/home/wang/sourceOrSoftware/traceabilityProjectConfig/crypto-config/peerOrganizations/org3.trace.com/users/<a>Admin@org3.trace.com</a>/msp/
peer chaincode install -n cc_retailer -v 2.0 -p github.com/hyperledger/fabric/examples/chaincode/go/traceability/src/origin_retailer/main

然后再upgrade

peer chaincode upgrade -o orderer.trace.com:7050 -C tracechannel -n cc_retailer -v 2.0 -c '{"Args":["init","A","B","C"]}' -P "OR ('Org1MSP.member','Org2MSP.member','Org3MSP.member')"

三. docker run'之后docker ps还是没有东西

docker ps -a 找到挂掉的容器

docker logs e45b51188d0b 看日志

(重启:docker rm b73d5af1cb0f)

网络中 tracechannel通道的instantiated的链码列表:

peer chaincode list --instantiated -C tracechannel

四. 直接调用invoke

cc_retailer:

peer chaincode invoke -o orderer.trace.com:7050 -C tracechannel -n cc_retailer -c '{"Args":["init","AAA2","BBB2","CCC2"]}'

peer chaincode invoke -o orderer.trace.com:7050 -C tracechannel -n cc_retailer -c '{"Args":["invoke","getmilkhistory","NWRuxXJ75fBWUjBZiDIyR16PdFLIOG","a"]}'

peer chaincode invoke -o orderer.trace.com:7050 -C tracechannel -n cc_retailer -c '{"Args":["invoke","putvalue","NWRuxXJ75fBWUjBZiDIyR16PdFLIOG","777777777777"]}'

peer chaincode invoke -o orderer.trace.com:7050 -C tracechannel -n cc_producer -c '{"Args":["invoke","gethistory","A00010011","a"]}'

五. 删除所有通道与账本

orderer:(只删orderer文件夹下数据文件夹)

cd /home/wang/sourceOrSoftware/traceabilityProjectConfig/orderer

rm -rf chains/ index/

peer1:(只删peer文件夹下数据文件夹)

cd /home/wang/sourceOrSoftware/traceabilityProjectConfig/peer

rm -rf production/

docker ps -a(若有已退出的容器)

docker rm a5583071056e

docker ps

docker rmi 4a2a324ab8d7

peer2、peer3同上

Fabric最后更新:2022-1-2