下载地址:https://github.com/hyperledger/fabric-sdk-py

参考地址:https://fabric-sdk-py.readthedocs.io/en/latest/tutorial.html

 

首先需要准备好fabric服务器环境和network.json文件,network.json文件示例存放在test/fixtures/路径下。

1.1 查询组织、peers、orderers、cas信息

from hfc.fabric import Client

cli = Client(net_profile="test/fixtures/network.json")

print(cli.organizations)    # orgs in the network
print(cli.peers)            # peers in the network
print(cli.orderers)         # orderers in the network
print(cli.CAs)              # ca nodes in the network

1.2 加载有效凭证

from hfc.fabric import Client

cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user(org_name='org1.example.com', name='Admin')        # get the admin user from local path

1.3 创建通道、将peers节点加入通道

import asyncio
from hfc.fabric import Client

loop = asyncio.get_event_loop()

cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user(org_name='org1.example.com', name='Admin')

# Create a New Channel, the response should be true if succeed
response = loop.run_until_complete(cli.channel_create(
            orderer='orderer.example.com',
            channel_name='businesschannel',
            requestor=org1_admin,
            config_yaml='test/fixtures/e2e_cli/',
            channel_profile='TwoOrgsChannel'
            ))
print(response == True)

# Join Peers into Channel, the response should be true if succeed
orderer_admin = cli.get_user(org_name='orderer.example.com', name='Admin')
responses = loop.run_until_complete(cli.channel_join(
               requestor=org1_admin,
               channel_name='businesschannel',
               peers=['peer0.org1.example.com',
                      'peer1.org1.example.com'],
               orderer='orderer.example.com'
               ))
print(len(responses))     # len(responses)是成功加入通道的peers节点的个数


# Join Peers from a different MSP into Channel
org2_admin = cli.get_user(org_name='org2.example.com', name='Admin')
responses = loop.run_until_complete(cli.channel_join(
               requestor=org2_admin,
               channel_name='businesschannel',
               peers=['peer0.org2.example.com',
                      'peer1.org2.example.com'],
               orderer='orderer.example.com'
               ))
print(len(responses) == 2)

1.4 安装链码、实例化链码

import asyncio
from hfc.fabric import Client

loop = asyncio.get_event_loop()

cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user('org1.example.com', 'Admin')

# Make the client know there is a channel in the network
cli.new_channel('businesschannel')

# Install Example Chaincode to Peers
# GOPATH setting is only needed to use the example chaincode inside sdk
import os
gopath_bak = os.environ.get('GOPATH', '')
gopath = os.path.normpath(os.path.join(
                      os.path.dirname(os.path.realpath('__file__')),
                      'test/fixtures/chaincode'
                     ))
os.environ['GOPATH'] = os.path.abspath(gopath)

# 安装链码
responses = loop.run_until_complete(cli.chaincode_install(
               requestor=org1_admin,
               peers=['peer0.org1.example.com',
                      'peer1.org1.example.com'],
               cc_path='github.com/example_cc',
               cc_name='example_cc',
               cc_version='v1.0'
               ))

# 实例化链码
response = loop.run_until_complete(cli.chaincode_instantiate(
               requestor=org1_admin,
               channel_name='businesschannel',
               peers=['peer0.org1.example.com'],
               args=args,
               cc_name='example_cc',
               cc_version='v1.0',
               cc_endorsement_policy=policy, # optional, but recommended
               collections_config=None, # optional, for private data policy
               transient_map=None, # optional, for private data
               wait_for_event=True # optional, for being sure chaincode is instantiated
               ))

1.5 查询区块链节点信息

import asyncio
from hfc.fabric import Client

loop = asyncio.get_event_loop()
cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user('org1.example.com', 'Admin')

# 查看peers节点加入的通道
response = loop.run_until_complete(cli.query_channels(
               requestor=org1_admin,
               peers=['peer0.org1.example.com'],
               decode=True
               ))

# 查看peers节点安装的链码
response = loop.run_until_complete(cli.query_installed_chaincodes(
               requestor=org1_admin,
               peers=['peer0.org1.example.com'],
               decode=True
               ))

# 查看peers节点实例化的链码
response = loop.run_until_complete(cli.query_instantiated_chaincodes(
               requestor=org1_admin,
               channel_name='businesschannel',
               peers=['peer0.org1.example.com'],
               decode=True
               ))

1.6 查询区块信息

import asyncio
from hfc.fabric import Client

loop = asyncio.get_event_loop()

cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user('org1.example.com', 'Admin')

# Make the client know there is a channel in the network
cli.new_channel('businesschannel')

# 查询最新区块信息,返回区块高度、哈希值、上一区块哈希值
response = loop.run_until_complete(cli.query_info(
               requestor=org1_admin,
               channel_name=channel_name,
               peers=['peer0.org1.example.com'],
               decode=True
               ))

# 根据区块哈希值查询
response = loop.run_until_complete(cli.query_block_by_hash(
    requestor=org1_admin,
    channel_name=channel_name,
    peers=['peer0.org1.example.com'],
    block_hash=hash,
    decode=True
))

# 根据区块高度查询
response = loop.run_until_complete(cli.query_block(
    requestor=org1_admin,
    channel_name=channel_name,
    peers=['peer0.org1.example.com'],
    block_number=height,
    decode=True
))

# 根据区块链key查找
args = [key]
response = loop.run_until_complete(cli.chaincode_query(
    requestor=org1_admin,
    channel_name=channel_name,
    peers=['peer0.org1.example.com'],
    args=args,
    cc_name=chaincode_name))

1.7 调用链码

import asyncio
from hfc.fabric import Client

loop = asyncio.get_event_loop()

cli = Client(net_profile="test/fixtures/network.json")
org1_admin = cli.get_user('org1.example.com', 'Admin')

# Make the client know there is a channel in the network
cli.new_channel('businesschannel')


args = ['a', 'b', '100']  # 这里改成你需要传进去的参数
response = loop.run_until_complete(cli.chaincode_invoke(
    requestor=org1_admin,
    channel_name=channel_name,
    peers=['peer0.org1.example.com'],
    args=list,
    cc_name=chaincode_name,
    fcn='query'     # 这里改为你链码中的函数名称
))

 

Logo

鸿蒙生态一站式服务平台。

更多推荐