如何轻松搞定超级账本chaincode编写与升级,通过invoke方法


    编写chaincode需要实现hyperledger/fabric/core/chaincode/shim/interfaces_stable.go文件中的init和Invoke方法(接口Chaincode中),在init和invoke方法中,传递的参数为ChaincodeStubInterface接口,该接口也定义在interfaces_stable.go文件中。通过invoke方法为入口,实现调用自定义chaincode中的其他业务方法。

    下面的gbivcc.go的chaincode,分别实现了方法:Init,Invoke,verify,storeResult,getResult方法。通过invoke方法,可以分别调用verify,storeResult,getResult方法。Gbivcc的代码见文章最后,下面按照启动fabric-samples的终端个数来说明如何安装chaincode。

1号终端

    启动fabric-samples示例

docker-compose -f docker-compose-simple.yaml up

2号终端

    docker exec –it chaincode bash

root@d2629980e76b:/opt/gopath/src/chaincode#
mkdir gbivcc && cd gbivcc
touch gbivcc.go (拷贝文件末尾的chaincode到该文件中)
go build gbivcc.go
运行chaincode
CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=gbivcc:0 ./gbivcc

3号终端
    进入cli,安装chaincode

    docker exec –it cli bash

安装chaincode
peer chaincode install -p chaincodedev/chaincode/gb_chaincode/ -n gbivcc -v 0 
peer chaincode instantiate -n gbivcc -v 0 -c '{"Args":[]}' -C myc 
调用getResult方法
peer chaincode invoke -n gbivcc -c '{"Args":["getResult", "request_id"]}' -C myc

相关内容