Fabric Dev开发调试模式的搭建过程

在利用Fabric开发Chaincode的时候,调试Chaincode显得尤为不方便,因为Chaincode正常应该运行在Docker容器中,每次修改Chaincode后想要使其更改生效必须得对Chaincode进行升级重新实例化,给我们的开发调试带来了很大的不便。下面给大家介绍一下如何启动并利用Dev模式来开发调试Chaincode。

1、从github上clone Hyperledger Fabric的源代码到本地,切换进入fabric目录,执行如下命令make可执行文件

make release

将$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin加入 ~/.profile 中,执行 source ~/.profile 生效

2、启动orderer

ORDERER_GENERAL_GENESISPROFILE=SampleDevModeSolo orderer
ORDERER_GENERAL_GENESISPROFILE=SampleDevModeSolo nohup orderer >/dev/null 2>&1 &  #后台启动

3、启动peer

peer node start --peer-chaincodedev=true
nohup peer node start --peer-chaincodedev=true >/dev/null 2>&1 &  #后台启动

4、用configtxgen工具生成通道交易配置文件用于创建通道

configtxgen -channelID ch1 -outputCreateChannelTx ch1.tx -profile SampleSingleMSPChannel
configtxgen -channelID ch2 -outputCreateChannelTx ch2.tx -profile SampleSingleMSPChannel

5、创建通道ch1和ch2

peer channel create -o 127.0.0.1:7050 -c ch1 -f ch1.tx
peer channel create -o 127.0.0.1:7050 -c ch2 -f ch2.tx

6、加入通道

peer channel join -b ch1.block
peer channel join -b ch2.block

7、自此,peer跟orderer就已经建立了关系,可以启动我们要调试的链码了 

cd examples/chaincode/go/chaincode_example02
go build -o example02
CORE_PEER_ADDRESS=10.0.2.15:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./example02
  
注意:在dev模式下,CORE_PEER_ADDRESS这个参数不能用127.0.0.1,而应该用启动peer时候实例化Gossip所对应的ip地址
2018-06-21 03:48:21.626 UTC [gossip/gossip] start -> INFO 01b Gossip instance 10.0.2.15:7051 started

否则会在启动链码的时候报错,具体报错信息如下:
2018-06-21 03:52:43.648 UTC [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...
2018-06-21 03:52:46.654 UTC [shim] userChaincodeStreamGetter -> ERRO 003 context deadline exceeded error trying to connect to local peer

 8、在peer上安装注册要调试的Chaincode

peer chaincode install -n mycc -v 0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

9、在peer上实例化Chaincode

peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a","100","b","200"]}' -o 127.0.0.1:7050 -C ch1
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a","100","b","200"]}' -o 127.0.0.1:7050 -C ch2

10、测试调用Chaincode

peer chaincode invoke -n mycc -c '{"Args":["invoke","a","b","10"]}' -o 127.0.0.1:7050 -C ch1
peer chaincode invoke -n mycc -c '{"Args":["invoke","a","b","10"]}' -o 127.0.0.1:7050 -C ch2

11、测试查询Chaincode

peer chaincode query -n mycc -c '{"Args":["query","a"]}' -o 127.0.0.1:7050 -C ch1
peer chaincode query -n mycc -c '{"Args":["query","a"]}' -o 127.0.0.1:7050 -C ch2

以上就是Fabric Dev模式下开发调试Chaincode的基本步骤,如果我们调试的Chaincode本地做了修改,只需要将第7步中修改过后的Chaincode重新编译,然后重新启动。直接调用即可生效,而无需再次对peer侧的Chaincode进行升级实例化等操作,会大大提高我们开发调试Chaincode的效率,希望能够对大家有所帮助。

猜你喜欢

转载自www.cnblogs.com/qiangjiyi/p/9394925.html