前言
FabricFabricFabricchaincode
环境搭建
FabricUbuntuWindowsFabricDockerWindowsFabricUbuntuwindowsbug
系统、软件 | 版本
:---: | :---:
VMware Pro | 16.0.0 Debian
| debian-11.2.0-amd64-DVD-1.iso git | 2.30.2
curl | 7.74.0
docker | 20.10
golang | go1.17.8
jq | jq-1.6
fabric | 2.4.0
fabric-ca | 1.5.2
fabric-samples | v2.3.0
本环境各Docker镜像版本如下:
镜像 | 版本
:---: | :---:
hyperledger/fabric-tools | 2.4
hyperledger/fabric-peer | 2.4
hyperledger/fabric-orderer | 2.4
hyperledger/fabric-ccenv | 2.4
hyperledger/fabric-baseos | 2.4
hyperledger/fabric-ca | 1.5
警告:建议Fabric所有实验过程皆在root权限下进行,否则在sudo权限切换的过程中会出现很多环境变量的问题。
杂项安装
GitShell apt install gitcURLShell apt install curlGolangShell apt install jq
安装Fabric
官方脚本安装
FabricFabricbootstrap.sh
wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
chmod +x bootstrap.sh
./bootstrap.sh
不出意外的话会看见脚本顺利的环境安装过程: 
手动安装
fabric-samplesFabricGolangJavaScripttypescriptJavaFabricfabric-samplesgit clone git@github.com:hyperledger/fabric-samples.git
wget https://github.com/hyperledger/fabric/releases/download/v2.4.0/hyperledger-fabric-linux-amd64-2.4.0.tar.gz
mkdir /usr/local/fabric
tar -xzvf hyperledger-fabric-linux-amd64-2.3.2.tar.gz -C /usr/local/fabric
- 下载fabric-ca 1.5.2并解压
wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
tar -xzvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
mv bin/* /usr/local/fabric/bin
/etc/profile
#Fabric
export FABRIC=/usr/local/fabric
export PATH=$PATH:$FABRIC/bin
source /etc/profile
安装Docker
- 如果存在则移除旧的版本
apt remove docker docker-engine docker.io containerd runc
aptHTTPS
apt update
apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
DockerGPG
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Docker
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
Docker
apt update
apt install docker-ce docker-ce-cli containerd.io
docker-compose
apt install docker-compose
安装Docker镜像依赖
Fabric
docker pull hyperledger/fabric-tools:2.4
docker pull hyperledger/fabric-peer:2.4
docker pull hyperledger/fabric-orderer:2.4
docker pull hyperledger/fabric-ccenv:2.4
docker pull hyperledger/fabric-baseos:2.4
docker pull hyperledger/fabric-ca:1.5
docker images
hyperledger/fabric-tools 2.4 625237d887db 4 weeks ago 473MB
hyperledger/fabric-peer 2.4 ee643d889779 4 weeks ago 62.3MB
hyperledger/fabric-orderer 2.4 df64446ac2df 4 weeks ago 37.3MB
hyperledger/fabric-ccenv 2.4 da4f00cb576a 4 weeks ago 517MB
hyperledger/fabric-baseos 2.4 0287ebf8aaf3 4 weeks ago 6.94MB
hyperledger/fabric-ca 1.5 4ea287b75c63 6 months ago 69.8MB
latestpulllatestlatest
# docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)
docker tag 625237d887db hyperledger/fabric-tools:latest
docker tag ee643d889779 hyperledger/fabric-peer:latest
docker tag df64446ac2df hyperledger/fabric-orderer:latest
docker tag da4f00cb576a hyperledger/fabric-ccenv:latest
docker tag 0287ebf8aaf3 hyperledger/fabric-baseos:latest
docker tag 4ea287b75c63 hyperledger/fabric-ca:latest
最终的镜像为:
运行测试
启动fabric网络
- 进入fabric-sample的test-network目录
cd fabric-samples/test-network
./network.sh up
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS
NAMES
7738c1e84751 hyperledger/fabric-tools:latest "/bin/bash" Less than a second ago Up Less than a second cli
1f24de2c6cd5 hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up Less than a second 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com
bfc48b20360c hyperledger/fabric-orderer:latest "orderer" 2 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp orderer.example.com
b9a61fdaf47a hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up Less than a second 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp peer0.org1.example.com
peer0.org1.example.compeer0.org2.example.comorderer.example.com
创建channel
上节已经在机器上运行了peer节点和orderer节点,现在可以使用network.sh为Org1和Org2之间创建channel。channel是特定网络成员之间的私有通道,只能被属于该通道的组织使用,并且对网络的其他成员是不可见的。每个channel都有一个单独的区块链账本,属于该通道的组织可以让其下peer加入该通道,以让peer能够存储channel上的帐本并验证账本上的交易。 使用以下命令创建自定义通道testchannel:
./network.sh createChannel -c testchannel

部署chaincode
rootrootroot
./network.sh deployCC
./network.sh deployCC -c testchannel -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
scripts/deployCC.sh: line 114: log.txt: Permission denied
./network.sh deployCC -c testchannel -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deployCC.sh: line 59: go: command not foundgorootgosudolinuxsudo/etc/sudoers
go: github.com/golang/protobuf@v1.3.2: Get "https://proxy.golang.org/github.com/golang/protobuf/@v/v1.3.2.mod": dial tcp 172.217.160.81:443: i/o timeout
go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,directgo get github.com/golang/protobufGOPATHgithub.com/golang/protobufgo envGOPATHsudorootgorootgo
2021-08-15 00:45:54.064 PDT [chaincodeCmd] ClientWait -> INFO 001 txid [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:7051
2021-08-15 00:45:54.144 PDT [chaincodeCmd] ClientWait -> INFO 002 txid [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:9051
Chaincode definition committed on channel 'testchannel'
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'testchannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID testchannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'testchannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'testchannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'testchannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID testchannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'testchannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'testchannel'
Chaincode initialization is not required
合约交互
fabrictest-networkpeer
export FABRIC_CFG_PATH=$PWD/../config/
# export FABRIC_CFG_PATH=/usr/local/fabric/config/
Org1
# Environment variables for Org1
# CORE_PEER_TLS_ROOTCERT_FILE和CORE_PEER_MSPCONFIGPATH环境变量指向Org1的organizations文件夹中的身份证书。
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
- 初始化chaincode
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
 4. 查询账本资产列表
peer chaincode query -C testchannel -n basic -c '{"Args":["GetAllAssets"]}'
 5. 修改账本资产
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'
./network.sh down
该命令将停止并删除节点和链码容器、组织加密材料、删除之前运行的通道项目和docker卷,并从Docker Registry移除链码镜像。
asset-transfer (basic)Org1Org2--peerAddressespeer0.org1.example.compeer0.org2.example.comTLS--tlsRootCertFilespeerTLS
参考
[^1]: Hyperledger Fabric. Hyperledger Fabric Getting Started. readthedocs.io. [发表或更新日期] [^2]: qq_JWang_03215367. 解决command not found 报错. 慕课. [2018-07-31] [^3]: 沐沐子枫. failed to normalize chaincode path: 'go list' failed with: go. 博客园. [2020-11-27]