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:<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.....

[img]http://dl2.iteye.com/upload/attachment/0122/3608/4513028e-62f1-39a4-a054-14165ae2168b.png[/img]

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
}


更多使用方法,可以参考 [url=https://github.com/yeasy/hyperledger-py/blob/master/docs/api.md]API 文档[/url]。

##############################################################
########### 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


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

[b]Params:[/b]

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.

[b]2、chaincode_deploy[/b]

>>> 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}

[b]Params[/b]

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.

[img]http://dl2.iteye.com/upload/attachment/0122/3889/198c0be0-1d02-3ca7-8e78-41a00850d088.jpg[/img]

[b]3、chaincode_query[/b]

>>> 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}

[b]Params[/b]

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.

[b]4、chaincode_invoke[/b]

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}

[b]Params[/b]

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值