Пример #1
0
    ##############################################################################
    # Query Peer installed chaincodes
    response = loop.run_until_complete(
        cli.query_installed_chaincodes(requestor=org1_admin,
                                       peers=['peer0.org1.example.com']))
    print("Query installed chaincode.")
    print(response)

    # check if channel named "businesschannel" is already exists
    response = cli.get_channel(name="businesschannel")
    print("Channel named `businesschannel`: ", response)

    # query channels joined in by given peers
    response = loop.run_until_complete(
        cli.query_channels(requestor=org1_admin,
                           peers=['peer0.org1.example.com']))
    print("Channels: ", response)

    # query channel info of a given channel named "businesschannel"
    response = loop.run_until_complete(
        cli.query_info(requestor=org1_admin,
                       channel_name="businesschannel",
                       peers=['peer0.org1.example.com']))
    print("Channels: ", response)

    # Get channel config
    response = loop.run_until_complete(
        cli.get_channel_config(requestor=org1_admin,
                               channel_name='businesschannel',
                               peers=['peer0.org1.example.com']))
    print("Get channel config done.")
Пример #2
0
def get_hfc_client():

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    client = Client()

    # Add peer from backend ledger config file
    peer = Peer(name=LEDGER['peer']['name'])
    peer.init_with_bundle({
        'url': f'{LEDGER["peer"]["host"]}:{PEER_PORT}',
        'grpcOptions': LEDGER['peer']['grpcOptions'],
        'tlsCACerts': {
            'path': LEDGER['peer']['tlsCACerts']
        },
        'clientKey': {
            'path': LEDGER['peer']['clientKey']
        },
        'clientCert': {
            'path': LEDGER['peer']['clientCert']
        },
    })
    client._peers[LEDGER['peer']['name']] = peer

    # Check peer has joined channel

    response = loop.run_until_complete(
        client.query_channels(requestor=LEDGER['requestor'],
                              peers=[peer],
                              decode=True))

    channels = [ch.channel_id for ch in response.channels]

    if not LEDGER['channel_name'] in channels:
        raise Exception(
            f'Peer has not joined channel: {LEDGER["channel_name"]}')

    channel = client.new_channel(LEDGER['channel_name'])

    # Check chaincode is instantiated in the channel

    responses = loop.run_until_complete(
        client.query_instantiated_chaincodes(
            requestor=LEDGER['requestor'],
            channel_name=LEDGER['channel_name'],
            peers=[peer],
            decode=True))

    chaincodes = [cc.name for resp in responses for cc in resp.chaincodes]

    if not LEDGER['chaincode_name'] in chaincodes:
        raise Exception(
            f'Chaincode : {LEDGER["chaincode_name"]}'
            f' is not instantiated in the channel :  {LEDGER["channel_name"]}')

    # Discover orderers and peers from channel discovery
    results = loop.run_until_complete(
        channel._discovery(LEDGER['requestor'],
                           peer,
                           config=True,
                           local=False,
                           interests=[{
                               'chaincodes': [{
                                   'name': LEDGER['chaincode_name']
                               }]
                           }]))

    results = deserialize_discovery(results)

    update_client_with_discovery(client, results)

    return loop, client
        cli.chaincode_query(requestor=admin_owkin,
                            channel_name='mychannel',
                            peers=['peer1-owkin'],
                            args=[],
                            cc_name='mycc',
                            cc_version='1.0',
                            fcn='queryDataManagers'))
    print(response)

    response = loop.run_until_complete(
        cli.query_installed_chaincodes(requestor=admin_owkin,
                                       peers=['peer1-owkin']))
    print(response)

    response = loop.run_until_complete(
        cli.query_channels(requestor=admin_owkin, peers=['peer1-owkin']))
    print(response)

    response = loop.run_until_complete(
        cli.query_info(requestor=admin_owkin,
                       channel_name='mychannel',
                       peers=['peer1-owkin']))
    print(response)

    dir_path = os.path.dirname(os.path.realpath(__file__))

    response = loop.run_until_complete(
        cli.chaincode_invoke(
            requestor=admin_owkin,
            channel_name='mychannel',
            peers=['peer1-owkin'],
def _get_hfc(channel_name):
    global user

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    if not user:
        with user_lock:
            # Only call `create_user` once in the lifetime of the application.
            # Calling `create_user` twice breaks thread-safety (bug in fabric-sdk-py)
            user = create_user(name=settings.LEDGER_USER_NAME,
                               org=settings.ORG_NAME,
                               state_store=FileKeyValueStore(
                                   settings.LEDGER_CLIENT_STATE_STORE),
                               msp_id=settings.LEDGER_MSP_ID,
                               key_path=glob.glob(
                                   settings.LEDGER_CLIENT_KEY_PATH)[0],
                               cert_path=settings.LEDGER_CLIENT_CERT_PATH)

    client = Client()

    # Add peer from backend ledger config file
    peer = Peer(name=settings.LEDGER_PEER_NAME)
    peer.init_with_bundle({
        'url':
        f'{settings.LEDGER_PEER_HOST}:{settings.LEDGER_PEER_PORT}',
        'grpcOptions':
        ledger_grpc_options(settings.LEDGER_PEER_HOST),
        'tlsCACerts': {
            'path': settings.LEDGER_PEER_TLS_CA_CERTS
        },
        'clientKey': {
            'path': settings.LEDGER_PEER_TLS_CLIENT_KEY
        },
        'clientCert': {
            'path': settings.LEDGER_PEER_TLS_CLIENT_CERT
        },
    })
    client._peers[settings.LEDGER_PEER_NAME] = peer

    # Check peer has joined channel

    response = loop.run_until_complete(
        client.query_channels(requestor=user, peers=[peer], decode=True))

    channels = [ch.channel_id for ch in response.channels]

    if channel_name not in channels:
        raise Exception(f'Peer has not joined channel: {channel_name}')

    channel = client.new_channel(channel_name)

    # This part is commented because `query_committed_chaincodes` is not implemented in
    # the last version of fabric-sdk-py

    # chaincode_name = settings.LEDGER_CHANNELS[channel_name]['chaincode']['name']

    # /!\ New chaincode lifecycle.

    # Check chaincode is committed in the channel
    # responses = loop.run_until_complete(
    #     client.query_committed_chaincodes(
    #         requestor=user,
    #         channel_name=channel_name,
    #         peers=[peer],
    #         decode=True
    #     )
    # )
    # chaincodes = [cc.name
    #               for resp in responses
    #               for cc in resp.chaincode_definitions]
    # if chaincode_name not in chaincodes:
    #     raise Exception(f'Chaincode : {chaincode_name}'
    #                     f' is not committed in the channel :  {channel_name}')

    # Discover orderers and peers from channel discovery
    results = loop.run_until_complete(
        channel._discovery(user,
                           peer,
                           config=True,
                           local=False,
                           interests=[{
                               'chaincodes': [{
                                   'name': "_lifecycle"
                               }]
                           }]))

    results = _deserialize_discovery(results)

    _validate_channels(channel_name, results)
    _update_client_with_discovery(client, results)

    return loop, client, user