docker安装
使用阿里云镜像通过官方脚本自动安装,可以先去修改软件源,用国内的源速度更快
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
检验docker的安装
docker -v
显示当前安装的版本,安装成功
docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改为可执行模式
sudo chmod +x /usr/local/bin/docker-compose
检验方法类似
docker-compose -v
go语言安装
详情见我的另一篇文章
ubuntu18.04下搭建golang环境
安装fabric
这里条件允许的话建议挂vpn,用官方提供的脚本安装,不容易出问题
curl -sSL https://bit.ly/2ysbOFE | bash -s
脚本会自动下载fabric的二进制文件和docker镜像,完成后出现fabric-samples文件夹
安装测试
测试前的准备
在测试前需要安装一个go语言的依赖包
先把golang的代理地址切换成国内地址
go env -w GOPROXY="https://goproxy.cn,direct"
在fabric-samples下进入go语言链码目录
cd asset-transfer-basic/chaincode-go
安装vendor
go mod vendor
启动测试网络
进入到fabric测试用例
cd ../test-network
运行测试网络脚本
./network.sh up
日志显示各节点创建成功,docker镜像也成功启动
创建通道
./network.sh createChannel
通道创建成功后,会显示Org1和Org2的锚节点anchor peer都加入了创建的通道“mychannel”
安装链码
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
与联盟链进行交互
先引入相关的环境变量
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
引入Org1的环境变量
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
初始化账本资产
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 mychannel -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":[]}'
显示设置成功
查询
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
返回查询结果
调用资产转移链码来转移链上的资产
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 mychannel -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"]}'
同样,后续可以通过引入Org2的环境变量,从Org2的节点进行查询和链码调用操作
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
关闭网络
操作结束后切记及时关闭网络,否则后续再次操作时,会因docker镜像没有关闭报错
./network.sh down