本章节环境配置
ubuntu: 20.04 go:1.16.3 docker: 20.10.6 docker-compose: 1.27.2 fabric:2.2.0 fabric-ca: 1.4.9
一 搭建通道
新建工作目录
mkdir fabric && cd fabric
配置go代理
go env -w GO111MODULE=on #更新下载包的镜像 go env -w GOPROXY=https://goproxy.cn,direct go env -w GOPRIVATE=.gitlab.com,.gitee.com go env -w GOSUMDB="sum.golang.google.cn"
这里我提供了基本环境搭建的脚本
git clone https://gitee.com/mrwhite123/bootstrap.git #可能会有点慢,稍等即可 bash bootstrap.sh
确保docker已经开启
#开启docker服务 systemctl start docker #重启服务 systemctl restart docker
拉取docker镜像
#这里我也提供了脚本,拉取运行即可 git clone https://gitee.com/mrwhite123/shell.git #可能会有点慢,稍等即可 bash shell/shell.sh
使用如下命令测试镜像是否安装完毕:
docker images
使用测试网络
#进入到test-network目录 cd fabric-samples/test-network #运行如下命令,以从任何先前运行中删除任何容器或工件: #若是第一次运行,则同时下载缺少的依赖 ./network.sh down #通过发出以下命令来启动网络。 ./network.sh up #启动后,可以使用如下命令进行查看组成部分: docker ps -a
创建通道
./network.sh createChannel
在通道上安装链码(部署合约)
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
网络进行交互,先配置环境变量,注意,此命令要在test-network目录下使用
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":[]}'
调用链码,获取资产列表
eer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}' [{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},{"ID":"asset2","color":"red","size":5,"ow
调用链码,改变账本上的资产所有者
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"]}'
完成本章节内容后,可继续往下进行fabric合约的部署和调用