Hyperledger之 Fabric0.6( 上)

阅读更多
1、安装docker
#curl -fsSL https://get.docker.com/ | sh
#service docker start
#service docker status

安装加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://88b7691f.m.daocloud.io

安装docker
curl -sSL https://get.daocloud.io/docker | sh

设置随机启动
systemctl enable docker

安装docker-compose
#curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose


2、下载镜像
docker pull hyperledger/fabric-baseimage:x86_64-0.2.1
docker pull hyperledger/fabric-membersrvc:latest
docker pull hyperledger/fabric-peer:latest


3、启动集群
git clone https://github.com/yeasy/docker-compose-files
docker-compose-files/hyperledger/0.6/pbft
docker-compose -f 4-peers-with-membersrvc.yml up|down


4、测试
docker exec -it pbft_vp0_1 bash

root@vp0:/opt/gopath/src/github.com/hyperledger/fabric# peer network login jim -p 6avZQLwcUe9b
09:41:09.300 [networkCmd] networkLogin -> INFO 001 CLI client login...
09:41:09.300 [networkCmd] networkLogin -> INFO 002 Local data store for client loginToken: /var/hyperledger/production/client/
Enter password for user 'jim': ************
09:41:17.217 [networkCmd] networkLogin -> INFO 003 Logging in user 'jim' on CLI interface...
09:41:17.326 [networkCmd] networkLogin -> INFO 004 Storing login token for user 'jim'.
09:41:17.326 [networkCmd] networkLogin -> INFO 005 Login successful for user 'jim'.
09:41:17.326 [main] main -> INFO 006 Exiting.....



5、部署链码
cd /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go

mkdir chaincode_example06

cd chaincode_example06

wget https://raw.githubusercontent.com/IBM-Blockchain/learn-chaincode/master/finished/chaincode_finished.go

mv chaincode_finished.go chaincode_example06.go

peer chaincode deploy -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example06 -c '{"Function":"init","Args":["ancun"]}' -u jim


09:46:38.363 [chaincodeCmd] getChaincodeSpecification -> INFO 001 Local user 'jim' is already logged in. Retrieving login token.
09:46:41.898 [chaincodeCmd] chaincodeDeploy -> INFO 002 Deploy result: type:GOLANG chaincodeID: ctorMsg: 
Deploy chaincode: a551e00fec9b30a1d47996cf50fe45d82e045d5f40c3ea1f3e5559bdda8a9f23b6e2d3d07c772e50e69b1f00628a883f2f658369d5619f82c01f79d1c6ce2cff
09:46:41.899 [main] main -> INFO 003 Exiting.....

Hyperledger之 Fabric0.6( 上)_第1张图片

6、执行调用
POST http://xx.xx.xx.xx:7050/chaincode
{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
    "type": 1,
    "chaincodeID": {
      "name": "上一步返回的链码ID"
    },
    "ctorMsg": {
      "function": "write",
      "args": [
        "hash",
        "xyz"
      ]
    },
    "secureContext": "jim"
  },
  "id": 2
}


这里通过调用 write 函数写入 key=hash,value=xyz。
示例响应内容:
{
    "jsonrpc": "2.0",
    "result": {
        "status": "OK",
        "message": "73304d46-5677-4ba9-9020-02f618a494d4"
    },
    "id": 2
}


7、查询
{
  "jsonrpc": "2.0",
  "method": "query",
  "params": {
    "type": 1,
    "chaincodeID": {
      "name": "上一步返回的链码ID"
    },
    "ctorMsg": {
      "function": "read",
      "args": [
        "hash"
      ]
    },
    "secureContext": "jim"
  },
  "id": 0
}


示例响应:
{
    "jsonrpc": "2.0",
    "result": {
        "status": "OK",
        "message": "xyz"
    },
    "id": 0
}


更多使用方法,可以参考 API 文档。

##############################################################
###########                  Python客户端            #########
##############################################################
yum -y install python-pip
No package python-pip available.
Error: Nothing to do
说没有python-pip软件包可以安装。
这是因为像centos这类衍生出来的发行版,他们的源有时候内容更新的比较滞后,或者说有时候一些扩展的源根本就没有。
所以在使用yum来search  python-pip的时候,会说没有找到该软件包。因此为了能够安装这些包,需要先安装扩展源EPEL。
yum -y install epel-release
安装完之后别忘了清除一下cache
yum clean all

pip --version
pip install --upgrade pip


1、First you need to import the hyperledger client and create one instance.
>>> from hyperledger.client import Client
>>> c = Client(base_url="http://127.0.0.1:7050")

Params:

  base_url (str): Refers to the protocol+hostname+port where the Hyperledger service listening on.
  version (str): The version of the API the client will use.
  timeout (int): The HTTP request timeout, in seconds. Default to DEFAULT_TIMEOUT_SECONDS.
  tls (bool): Whether to use tls. Default to False.

2、chaincode_deploy
>>> from hyperledger.client import Client
>>> c = Client(base_url="http://127.0.0.1:7050")
>>> c.chaincode_deploy(chaincode_path="github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", function="init", args=["a","1000","b","2000"], secure_context="jim")

{u'jsonrpc': u'2.0', u'result': {u'status': u'OK', u'message': u'04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050'}, u'id': 1}

Params

  chaincode_path (str): path to the chaincode. Default to DEFAULT_CHAINCODE_PATH.
  type (int): chaincode language type: 1 for golang, 2 for node. Default to CHAINCODE_LANG_GO.
  function (str): chaincode function name. Default to DEFAULT_CHAINCODE_INIT_FUNC
  args (str): chaincode function args. Default to DEFAULT_CHAINCODE_INIT_ARGS.
  id (int): JSON-RPC requires this value for a response. Default to 1.
  secure_context (str): secure context if enable authentication. Default to None.
  confidentiality_level (int): level of confidentiality. Default to CHAINCODE_CONFIDENTIAL_PUB.
  metadata (str): Metadata by client.



3、chaincode_query
>>> c.chaincode_query(chaincode_name="04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050", function="query", args=["a"], secure_context="jim")

{u'jsonrpc': u'2.0', u'result': {u'status': u'OK', u'message': u'1000'}, u'id': 1}

Params

  chaincode_name (str): Name of the chaincode. Usually returned by the deploy API.
  type (int): chaincode language type: 1 for golang, 2 for node. Default to CHAINCODE_LANG_GO.
  function (str): chaincode function name. Default to DEFAULT_CHAINCODE_INIT_FUNC
  args (str): chaincode function args. Default to DEFAULT_CHAINCODE_INIT_ARGS.
  id (int): JSON-RPC requires this value for a response. Default to 1.
  secure_context (str): secure context if enable authentication. Default to None.
  confidentiality_level (int): level of confidentiality. Default to CHAINCODE_CONFIDENTIAL_PUB.
  metadata (str): Metadata by client.

4、chaincode_invoke
c.chaincode_invoke(chaincode_name="04233c6dd8364b9f0749882eb6d1b50992b942aa0a664182946f411ab46802a88574932ccd75f8c75e780036e363d52dd56ccadc2bfde95709fc39148d76f050", function="invoke", args=["a","b","10"], secure_context="jim")

{u'jsonrpc': u'2.0', u'result': {u'status': u'OK', u'message': u'df4a7dfd-67c0-43c0-90bd-be383d354faf'}, u'id': 1}

Params

  chaincode_name (str): Name of the chaincode. Usually returned by the deploy API.
  type (int): chaincode language type: 1 for golang, 2 for node. Default to CHAINCODE_LANG_GO.
  function (str): chaincode function name. Default to DEFAULT_CHAINCODE_INIT_FUNC
  args (str): chaincode function args. Default to DEFAULT_CHAINCODE_INIT_ARGS.
  id (int): JSON-RPC requires this value for a response. Default to 1.
  secure_context (str): secure context if enable authentication. Default to None.
  confidentiality_level (int): level of confidentiality. Default to CHAINCODE_CONFIDENTIAL_PUB.
  metadata (str): Metadata by client.
  • Hyperledger之 Fabric0.6( 上)_第2张图片
  • 大小: 70.5 KB
  • Hyperledger之 Fabric0.6( 上)_第3张图片
  • 大小: 11 KB
  • 查看图片附件

你可能感兴趣的:(Hyperledger之 Fabric0.6( 上))