1、环境安装
HyperLedger Fabric 1.2.0环境的安装推荐一篇博客:https://www.jianshu.com/p/4f58e0fcb9f9
此博客写的非常详细,本人完全按照这个上面安装,但第九步出现了错误,这个错误是因为权限问题,解决方法参考这里。其余地方均未发现问题。
2、链码安装、部署和测试
1、执行./byfn.sh down关闭前面的网络。
2、进入/fabric-samples/chaincode-docker-devmode
hisee@ubuntu:~/hyperledger-fabric/fabric-samples$ cd chaincode-docker-devmode/
3、在终端1启动网络
$ docker-compose -f docker-compose-simple.yaml up
执行成功后如下:
Recreating orderer ... done
Recreating peer ... done
Recreating cli ... done
Recreating chaincode ... done
Attaching to orderer, peer, cli, chaincode
orderer | 2018-11-24 09:05:10.277 UTC [localconfig] completeInitialization -> INFO 001 Kafka.Version unset, setting to 0.10.2.0
orderer | 2018-11-24 09:05:10.277 UTC [bccsp_sw] openKeyStore -> DEBU 002 KeyStore opened at [/etc/hyperledger/msp/keystore]...done
orderer | 2018-11-24 09:05:10.277 UTC [bccsp] initBCCSP -> DEBU 003 Initialize BCCSP [SW]
4、在终端2编译和启动链码:
docker exec -it chaincode bash
sacc
go build
启动链码
CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc
root@ubuntu:~/hyperledger-fabric/fabric-samples/chaincode-docker-devmode# docker exec -it chaincode bash
root@962857b26396:/opt/gopath/src/chaincode# cd sacc
root@962857b26396:/opt/gopath/src/chaincode/sacc# go build
root@962857b26396:/opt/gopath/src/chaincode/sacc# CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc
2018-11-24 09:06:43.778 UTC [shim] SetupChaincodeLogging -> INFO 001 Chaincode log level not provided; defaulting to: INFO
2018-11-24 09:06:43.778 UTC [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...
5、终端3中使用链码
docker exec -it cli bash
安装和初始化链码在devmode:peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0
设置a的初始值:peer chaincode instantiate -n mycc -v 0 -c '{"Args":["a","10"]}' -C myc
修改a的值:peer chaincode invoke -n mycc -c '{"Args":["set", "a", "20"]}' -C myc
结果如下:
2018-11-24 09:52:22.044 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 0a8 Chaincode invoke successful. result: status:200 payload:"20"
验证查询a的值:peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C myc
结果如下:
2018-11-24 09:53:49.796 UTC [msp/identity] Sign -> DEBU 045 Sign: digest: A13DADC70175E3E7AAB2294C6D60B28A7A372645EF79B642452CB57549A876FC
20