Fabric0.6 of Hyperledger (Part 1)

1. Install docker
#curl -fsSL https://get.docker.com/ | sh
#service docker start
#service docker status

Install the accelerator
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://88b7691f.m.daocloud.io

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

set random startup
systemctl enable docker

Install 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. Download the mirror
docker pull hyperledger/fabric-baseimage:x86_64-0.2.1
docker pull hyperledger/fabric-membersrvc:latest
docker pull hyperledger/fabric-peer:latest


3. Start the cluster
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. Test
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. Deploy the chaincode
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:<path:"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example06" name:"a551e00fec9b30a1d47996cf50fe45d82e045d5f40c3ea1f3e5559bdda8a9f23b6e2d3d07c772e50e69b1f00628a883f2f658369d5619f82c01f79d1c6ce2cff" > ctorMsg:<args:"init" args:"ancun" >
Deploy chaincode: a551e00fec9b30a1d47996cf50fe45d82e045d5f40c3ea1f3e5559bdda8a9f23b6e2d3d07c772e50e69b1f00628a883f2f658369d5619f82c01f79d1c6ce2cff
09:46:41.899 [main] main -> INFO 003 Exiting.....



6. Execute the call to
POST http://xx.xx.xx.xx:7050/chaincode
{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
    "type": 1,
    "chaincodeID": {
      "name": "Chaincode ID returned in the previous step"
    },
    "ctorMsg": {
      "function": "write",
      "args": [
        "hash",
        "xyz"
      ]
    },
    "secureContext": "jim"
  },
  "id": 2
}


Here key=hash, value=xyz is written by calling the write function.
Sample response content:
{
    "jsonrpc": "2.0",
    "result": {
        "status": "OK",
        "message": "73304d46-5677-4ba9-9020-02f618a494d4"
    },
    "id": 2
}


7. Query
{
  "jsonrpc": "2.0",
  "method": "query",
  "params": {
    "type": 1,
    "chaincodeID": {
      "name": "Chaincode ID returned in the previous step"
    },
    "ctorMsg": {
      "function": "read",
      "args": [
        "hash"
      ]
    },
    "secureContext": "jim"
  },
  "id": 0
}


Example response:
{
    "jsonrpc": "2.0",
    "result": {
        "status": "OK",
        "message": "xyz"
    },
    "id": 0
}


For more usage methods, please refer to the API documentation .

################################################## ############
########### Python Client #########
############### ###############################################
yum -y install python-pip
No package python-pip available.
Error: Nothing to do
Says that there is no python-pip package to install.
This is because of the derived distributions such as centos, their sources sometimes lag in content updates, or sometimes some extended sources are not available at all.
So when using yum to search for python-pip, it will say that the package is not found. So in order to be able to install these packages, the extension source EPEL needs to be installed first.
yum -y install epel-release
Don't forget to clear the cache after installation
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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326883225&siteId=291194637
Recommended