前面我们已经说到,容器的7050端口会映射成Ubuntu的7050端口,我们在Ubuntu下,运行ifconfig,可以看到Ubuntu的IP,然后我们回到Windows,就可以通过REST的Client来测试,这里我喜欢用Chrome的插件DHC,很好用,强烈推荐!不过要FQ才能装。
这里我Ubuntu的IP是192.168.100.129,下面就用DHC进行REST API的Example02部署。
3.2.1通过REST API部署GO语言的ChainCodePOST 192.168.100.129:7050/chaincode
Body是:
{ "jsonrpc": "2.0", "method": "deploy", "params": { "type": 1, "chaincodeID":{ "path":"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" }, "ctorMsg": { "function":"init", "args":["a", "1000", "b", "2000"] } }, "id": 1 }
这里为了区别,我们把a账户初始化1000元,b账户初始化2000元。返回的结果是:
{ "jsonrpc": "2.0", "result":{ "status": "OK", "message": "04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050" }, "id": 1 }
这里04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050就是部署后的ChainCodeID。
3.2.2通过REST API查询ChainCodePOST 192.168.100.129:7050/chaincode
Body内容是:
{ "jsonrpc": "2.0", "method": "query", "params": { "type": 1, "chaincodeID":{ "name":"04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050" }, "ctorMsg": { "function":"query", "args":["a"] } }, "id": 2 }
系统返回的结果是:
{ "jsonrpc": "2.0", "result":{ "status": "OK", "message": "1000" }, "id": 2 }
一切正常,返回a账户的1000元。
3.2.3通过REST API调用ChainCode我们试着从a向b转账100元:
POST 192.168.100.129:7050/chaincode
Body内容是:
{ "jsonrpc": "2.0", "method": "invoke", "params": { "type": 1, "chaincodeID":{ "name":"04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050" }, "ctorMsg": { "function":"invoke", "args":["a", "b", "100"] } }, "id": 3 }
返回的结果:
{ "jsonrpc": "2.0", "result":{ "status": "OK", "message": "2ac78b5f-6d35-400d-b7c4-75ef81e14d3e" }, "id": 3 }
3.2.4通过REST API检查调用ChainCode后的结果这里我们来查询一下b账户。
POST 192.168.100.129:7050/chaincode
Body内容改为:
{ "jsonrpc": "2.0", "method": "query", "params": { "type": 1, "chaincodeID":{ "name":"04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050" }, "ctorMsg": { "function":"query", "args":["b"] } }, "id": 4 }
返回结果:
{ "jsonrpc": "2.0", "result":{ "status": "OK", "message": "2100" }, "id": 4 }
一切正常,b账户果然真假了100元。
关于更多的REST API,我们可以参考这里:https://github.com/hyperledger-archives/fabric/blob/master/docs/API/CoreAPI.md#rest-api
3.3测试Java版Chain CodeFabric除了支持本身的Go语言的ChainCode,也可以支持其他语言,比如最常用的Java语言。Fabric的源代码中也提供了Java示例,这里我们就用SimpleSample这个示例:
https://github.com/hyperledger/fabric/tree/master/examples/chaincode/java/SimpleSample
3.3.1在CLI中部署该Java代码的ChainCode到Fabric命令是:
peer chaincode deploy -l java -p /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
运行结果为:
root@vp0:/opt/gopath/src/github.com/hyperledger/fabric# peer chaincode deploy -l java -p /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample -c '{"Function":"init", "Args": ["a","100", "b", "200"]}' 09:20:16.857 [chaincodeCmd] chaincodeDeploy -> INFO 001 Deploy result: type:JAVA chaincodeID:<path:"/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample" name:"0f5b1d65041bc6d500bd0f1cab50eb6154c291ef0f4596d64b6797e8ef8f7c34a179b5a2cea82253ff3d74e768512fe0481503eadcf13d18f9761bbb8133efd0" > ctorMsg:<args:"init" args:"a" args:"100" args:"b" args:"200" > Deploy chaincode: 0f5b1d65041bc6d500bd0f1cab50eb6154c291ef0f4596d64b6797e8ef8f7c34a179b5a2cea82253ff3d74e768512fe0481503eadcf13d18f9761bbb8133efd0 09:20:16.857 [main] main -> INFO 002 Exiting.....
接下来的各种查询,调用都是差不多的,我就不再累述了。
3.3.2通过REST API部署Java ChainCode到FabricPOST 192.168.100.129:7050/chaincode
Body为