文章目录

    • 启动Fabric2.4网络
    • 调用Go编写的链码
    • 访问couchdb 查询区块数据
    • PS 同一通道可以部署多个链码

启动Fabric2.4网络

搭建部署 Fabric2.4网络的具体教程可见我写的这篇博客:

Fabric 超级账本学习【4】Ububtu环境下部署搭建 Hyperledger Fabric2.4 (2.X)区块链网络

1、前提是搭建好了Fabric 2.4(Fabric2.x)版本的区块链网络,并在以此环境下部署自己编写的链码,如下图先进入 test-network 文件夹目录下


2、假如你之前开启过网络,先关闭网络,以免节点信息冲突,部署错误

./network.sh down

3、正式启动网络,部署网络命令以及成功截图如下

./network.sh up如果想要同时启动CA服务器和couchdb,执行这条指令:./network.sh up createChannel -ca -s couchdb

!!!!!!!!!假如启动失败如下,可能之前测试网络时出现重复或者已经存在的镜像,删除即可

docker ps -qa | xargs docker stopdocker ps -qa | xargs docker rm

4、接着在测试网络加入通道:

./network.sh createChannel

5、接下来就是部署自己的链码了

前期准备:先配置Go代理

go env -w GO111MODULE=ongo env -w GOPROXY=https://goproxy.cn,direct

执行 go env 查看配置代理后的环境变量,方便后续部署链码时下载相应的go环境

然后我们再 test-network 文件夹目录下 创建单独放置自己链码的 mychaincode 文件夹

把自己编写的 链码放进去 此目录下的 asset 文件夹下

!!!!!!!!!!!依次执行如下命令

go mod initgo mod tidy go mod vendor


执行如上命令后成功截图以及生成文件如下:

此时,我们的链码环境才算完成了!接下来就是链码的安装:回到test-network目录下:

如上学习部署链码指令,我们可以执行自己的链码命令进行部署如下:

./network.sh deployCC -ccn asseth -ccp ./mychaincode/asset -ccl go

可以看得到链码成功部署!!!!

在fabric2.x版本中默认是不需要实例化链码了,所以直接调用invoke即可,首先配置一下环境变量

export PATH=${PWD}/../bin:$PATHexport FABRIC_CFG_PATH=$PWD/../config/export CORE_PEER_TLS_ENABLED=trueexport CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crtexport CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspexport CORE_PEER_ADDRESS=localhost:7051

调用Go编写的链码

1、根据链码写执行初始化链码的脚本,可见初始化可以传入三个参数

编写执行初始化链码的脚本

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 asseth --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":["asseth","hp","666"]}'

2、查询区块链中所有资产信息

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 asseth --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 '{"Args":["getAllAssets"]}'

3、查询区块链中单个资产 asset1 信息

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 asseth --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":"ReadAsset","Args":["asset1"]}'

成功查询单个资产 asset1 全部信息

4、向区块链中写入新的资产信息

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 asseth --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":"CreateAsset","Args":["asset666","金智秀","66666"]}'

成功写入新的资产!!!!

再次查询所有资产信息可以看到之前新写入的资产信息均存在

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 mychannel -n asseth --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":"AssetExists","Args":["asset888666"]}'

可以判断 资产 ID 为 asset888666 的资产存在!!!!

访问couchdb 查询区块数据

localhost:5984/_utils   


资产信息已存在 CouchDB 中

PS 同一通道可以部署多个链码

部署如下链码

./network.sh deployCC -ccn lianma -ccp ./mychaincode/lianma -ccl go

实例化链码

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 lianma --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":[]}'

可以看到之前两条链码的数据都上链了