hfc.fabric

Package Contents

Classes

Client(net_profile=None)

Main interaction handler with end user.

NullHandler(level=NOTSET)

Handler instances dispatch logging events to specific destinations.

class hfc.fabric.Client(net_profile=None)

Bases: object

Main interaction handler with end user.

Client can maintain several channels.

Parameters

object

init_with_net_profile(self, profile_path='network.json')

Load the connection profile from external file to network_info.

Init the handlers for orgs, peers, orderers, ca nodes

Parameters

profile_path – The connection profile file path

Returns

async init_with_discovery(self, requestor, peer_target, channel_name=None)

Load the connection profile from discover.

Init the handlers for orgs, peers, orderers, ca nodes

Returns

async close_grpc_channels(self)

Close the peers/orderers gRPC channels :return:

set_tls_client_cert_and_key(self, client_key_file=None, client_cert_file=None)

Set tls client certificate and key for mutual tls for all peers and orderers

Parameters
  • client_key_file (str) – file path for Private key used for TLS when making client connections, defaults to None

  • client_cert_file (str) – file path for X.509 certificate used for TLS when making client connections, defaults to None

Returns

set success value

Return type

Boolean

get_user(self, org_name, name)

Get a user instance. :param org_name: Name of org belongs to :param name: Name of the user :return: user instance or None

get_orderer(self, name)

Get an orderer instance with the name. :param name: Name of the orderer node. :return: The orderer instance or None.

get_peer(self, name)

Get a peer instance with the name. :param name: Name of the peer node. :return: The peer instance or None.

export_net_profile(self, export_file='network_exported.json')

Export the current network profile into external file :param export_file: External file to save the result into :return:

get_net_info(self, *key_path)

Get the info from self.network_info :param key_path: path of the key, e.g., a.b.c means info[‘a’][‘b’][‘c’] :return: The value, or None

property organizations(self)

Get the organizations in the network.

Returns

organizations as dict

property orderers(self)

Get the orderers in the network.

Returns

orderers as dict

property peers(self)

Get the peers instance in the network.

Returns

peers as dict

property CAs(self)

Get the CAs in the network.

Returns

CAs as dict

new_channel(self, name)

Create a channel handler instance with given name.

Parameters

name (str) – The name of the channel.

Returns

The inited channel.

Return type

channel

get_channel(self, name)

Get a channel handler instance.

Parameters

name (str) – The name of the channel.

Returns

Get the channel instance with the name or None

Return type

str/None

async channel_create(self, orderer, channel_name, requestor, config_yaml=None, channel_profile=None, config_tx=None)

Create a channel, send request to orderer, and check the response

Parameters

orderer – Name or Orderer instance of orderer to get

genesis block from :param channel_name: Name of channel to create :param requestor: Name of creator :param config_yaml: Directory path of config yaml to be set for FABRIC_ CFG_PATH variable :param channel_profile: Name of the channel profile defined inside config yaml file :param config_tx: Path of the configtx file of createchannel generated with configtxgen :return: True (creation succeeds) or False (creation failed)

async channel_update(self, orderer, channel_name, requestor, config_yaml=None, channel_profile=None, config_tx=None, signatures=None)

Update a channel, send request to orderer, and check the response

Parameters

orderer – Name or Orderer instance of orderer to get

genesis block from :param channel_name: Name of channel to create :param requestor: Name of creator :param config_tx: Path of the configtx file of createchannel generated with configtxgen :return: True (creation succeeds) or False (creation failed)

async channel_join(self, requestor, channel_name, peers, orderer)

Join a channel. Get genesis block from orderer, then send request to peer

Parameters
  • requestor – User to send the request

  • channel_name – Name of channel to create

  • peers – List of peers to join to the channel

  • orderer – Name or Orderer instance of orderer to get

genesis block from

Returns

True (creation succeeds) or False (creation failed)

async get_channel_config(self, requestor, channel_name, peers, decode=True)

Get configuration block for the channel

Parameters
  • requestor – User role who issue the request

  • channel_name – name of channel to query

  • peers – Names or Instance of the peers to query

  • deocode – Decode the response payload

Returns

A ChaincodeQueryResponse or ProposalResponse

async get_channel_config_with_orderer(self, requestor, channel_name, orderer=None)

Get configuration block for the channel with the orderer

Parameters
  • requestor – User role who issue the request

  • channel_name – name of channel to query

  • orderer – Names or Instance of the orderer to query

Returns

A ConfigEnveloppe

extract_channel_config(self, config_envelope)
Extracts the protobuf ‘ConfigUpdate’ out of the

‘ConfigEnvelope’ that is produced by the configtxgen tool.

The returned object may then be signed using sign_channel_config() method.

Once all the signatures have been collected, the ‘ConfigUpdate’ object and the signatures may be used on create_channel() or update_channel() calls

Parameters

config_envelope (bytes) – encoded bytes of the ConfigEnvelope protobuf

Returns

encoded bytes of ConfigUpdate protobuf, ready to be signed

Return type

bytes

sign_channel_config(self, config, to_string=True)
This method uses the client instance’s current signing identity to

sign over the configuration bytes passed in.

Parameters
  • config – The configuration update in bytes form.

  • tx_context – Transaction Context

  • to_string (bool) – Whether to convert the result to string, defaults to True

Returns

The signature of the current user of the config bytes.

Return type

config_signature(common_pb2.ConfigSignature)

channel_signconfigtx(self, config_tx_file, requestor)
async _create_or_update_channel(self, request)

Calls the orderer to start building the new channel.

Parameters

request (dict) – The create channel request.

Returns

OrdererResponse or an error.

Return type

Response/Error

_validate_request(self, request)

Validate a request :param request: request to validate :return:

_create_or_update_channel_request(self, request, have_envelope)

Inits the create of update channel process.

Parameters
  • request (dict) – A create_update channel request.

  • have_envelope (Boolean) – Signals if the requests contains a finished protobuf envelope.

Returns

BroadcastResponse which includes status and info

property crypto_suite(self)

Get the crypto suite.

Returns

The crypto_suite instance or None

property tx_context(self)

Get the current tx_context for the client.

Returns

The tx_context object or None

Return type

object/None

property state_store(self)

Get the KeyValue store.

Returns

Return the keyValue store instance or None

Return type

object/None

send_install_proposal(self, tx_context, peers)

Send install proposal

Parameters
  • tx_context – transaction context

  • peers – peers

Returns

A set of proposal_response

send_instantiate_proposal(self, tx_context, peers, channel_name)

Send instantiate proposal

Parameters
  • tx_context – transaction context

  • peers – peers

  • channel_name (str) – name of the channel

Returns

A set of proposal_response

send_upgrade_proposal(self, tx_context, peers, channel_name)

Send upgrade proposal

Parameters
  • tx_context – transaction context

  • peers – peers

  • channel_name (str) – the name of channel

Returns

A set of proposal_response

generate_channel_tx(self, channel_name, cfg_path, channel_profile)

Creates channel configuration transaction

Parameters
  • channel_name (str) – Name of the channel

  • cfg_path (str) – Directory path of config yaml to be set for

  • channel_profile (str) – Name of the channel profile defined inside

Returns

path to tx file if success else None

Return type

str/None

async chaincode_install(self, requestor, peers, cc_path, cc_name, cc_version, packaged_cc=None, transient_map=None)

Install chaincode to given peers by requestor role

Parameters
  • requestor – User role who issue the request

  • peers – List of peer name and/or Peer to install

  • cc_path – chaincode path

  • cc_name – chaincode name

  • cc_version – chaincode version

  • packaged_cc – packaged chaincode

  • transient_map – transient map

Returns

True or False

txEvent(self, tx_id, tx_status, block_number)
create_onCcEventArray(self, _uuid)
create_onCcEvent(self, _uuid)
async chaincode_instantiate(self, requestor, channel_name, peers, args, cc_name, cc_version, cc_endorsement_policy=None, transient_map=None, collections_config=None, wait_for_event=False, wait_for_event_timeout=30)
Instantiate installed chaincode to particular peer in

particular channel

Parameters
  • requestor – User role who issue the request

  • channel_name – the name of the channel to send tx proposal

  • peers – List of peer name and/or Peer to install

  • (list) (args) – arguments (keys and values) for initialization

  • cc_name – chaincode name

  • cc_version – chaincode version

  • cc_endorsement_policy – chaincode endorsement policy

  • transient_map – transient map

  • collection_config – collection configuration

  • wait_for_event

    Whether to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’ transaction has

    been committed successfully

  • wait_for_event_timeout

    Time to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’

    transaction has been committed successfully (default 30s)

Returns

chaincode data payload

async chaincode_upgrade(self, requestor, channel_name, peers, cc_name, cc_version, cc_endorsement_policy=None, fcn='init', args=None, transient_map=None, collections_config=None, wait_for_event=False, wait_for_event_timeout=30)
Upgrade installed chaincode to particular peer in

particular channel

Parameters
  • requestor – User role who issue the request

  • channel_name – the name of the channel to send tx proposal

  • peers – List of peer name and/or Peer to install

  • (list) (args) – arguments (keys and values) for initialization

  • cc_name – chaincode name

  • cc_version – chaincode version

  • cc_endorsement_policy – chaincode endorsement policy

  • fcn – chaincode function to send

  • args – chaincode function arguments

  • transient_map – transient map

  • collection_config – collection configuration

  • wait_for_event

    Whether to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’ transaction has

    been committed successfully

  • wait_for_event_timeout

    Time to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’

    transaction has been committed successfully (default 30s)

Returns

chaincode data payload

async chaincode_invoke(self, requestor, channel_name, peers, args, cc_name, cc_type=CC_TYPE_GOLANG, fcn='invoke', cc_pattern=None, transient_map=None, wait_for_event=False, wait_for_event_timeout=30, grpc_broker_unavailable_retry=0, grpc_broker_unavailable_retry_delay=3000, raise_broker_unavailable=True)

Invoke chaincode for ledger update

Parameters
  • requestor – User role who issue the request

  • channel_name – the name of the channel to send tx proposal

  • peers – List of peer name and/or Peer to install

  • (list) (args) – arguments (keys and values) for initialization

  • cc_name – chaincode name

  • cc_type – chaincode type language

  • fcn – chaincode function

  • cc_pattern – chaincode event name regex

  • transient_map – transient map

  • wait_for_event

    Whether to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’ transaction has

    been committed successfully

  • wait_for_event_timeout

    Time to wait for the event from each peer’s deliver filtered service signifying that the ‘invoke’

    transaction has been committed successfully (default 30s)

  • grpc_broker_unavailable_retry – Number of retry if a broker is unavailable (default 0)

:param grpc_broker_unavailable_retry_delayDelay in ms to retry

(default 3000 ms)

Parameters

raise_broker_unavailable – Raise if any broker is unavailable, else always send the proposal regardless of unavailable brokers.

Returns

invoke result

async chaincode_query(self, requestor, channel_name, peers, args, cc_name, cc_type=CC_TYPE_GOLANG, fcn='query', transient_map=None)

Query chaincode

Parameters
  • requestor – User role who issue the request

  • channel_name – the name of the channel to send tx proposal

  • peers – List of peer name and/or Peer to install

  • (list) (args) – arguments (keys and values) for initialization

  • cc_name – chaincode name

  • cc_type – chaincode type language

  • fcn – chaincode function

  • transient_map – transient map

Returns

True or False

async query_channels(self, requestor, peers, transient_map=None, decode=True)

Queries channel name joined by a peer

Parameters
  • requestor – User role who issue the request

  • peers – List of peer name and/or Peer to install

  • transient_map – transient map

  • decode – Decode the response payload

Returns

A ChannelQueryResponse or ProposalResponse

async query_info(self, requestor, channel_name, peers, decode=True)

Queries information of a channel

Parameters
  • requestor – User role who issue the request

  • channel_name – Name of channel to query

  • peers – List of peer name and/or Peer to install

  • deocode – Decode the response payload

Returns

A BlockchainInfo or ProposalResponse

async query_block_by_txid(self, requestor, channel_name, peers, tx_id, decode=True)

Queries block by tx id

Parameters
  • requestor – User role who issue the request

  • channel_name – Name of channel to query

  • peers – List of peer name and/or Peer to install

  • tx_id – Transaction ID

  • deocode – Decode the response payload

Returns

A BlockDecoder or ProposalResponse

async query_block_by_hash(self, requestor, channel_name, peers, block_hash, decode=True)

Queries block by hash

Parameters
  • requestor – User role who issue the request

  • channel_name – Name of channel to query

  • peers – List of peer name and/or Peer to install

  • block_hash – Hash of a block

  • deocode – Decode the response payload

Returns

A BlockDecoder or ProposalResponse

async query_block(self, requestor, channel_name, peers, block_number, decode=True)

Queries block by number

Parameters
  • requestor – User role who issue the request

  • channel_name – name of channel to query

  • peers – List of peer name and/or Peer to install

  • block_number – Number of a block

  • deocode – Decode the response payload

Returns

A BlockDecoder or ProposalResponse

async query_transaction(self, requestor, channel_name, peers, tx_id, decode=True)

Queries block by number

Parameters
  • requestor – User role who issue the request

  • channel_name – name of channel to query

  • peers – List of peer name and/or Peer to install

  • tx_id – The id of the transaction

  • decode – Decode the response payload

Returns

A BlockDecoder or ProposalResponse

async query_instantiated_chaincodes(self, requestor, channel_name, peers, transient_map=None, decode=True)

Queries instantiated chaincode

Parameters
  • requestor – User role who issue the request

  • channel_name – name of channel to query

  • peers – Names or Instance of the peers to query

  • transient_map – transient map

  • decode – Decode the response payload

Returns

A ChaincodeQueryResponse or ProposalResponse

async query_installed_chaincodes(self, requestor, peers, transient_map=None, decode=True)

Queries installed chaincode, returns all chaincodes installed on a peer

Parameters
  • requestor – User role who issue the request

  • peers – Names or Instance of the peers to query

  • transient_map – transient map

  • decode – Decode the response payload

Returns

A ChaincodeQueryResponse or ProposalResponse

async query_peers(self, requestor, peer, channel=None, local=True, decode=True)

Queries peers with discovery api

Parameters
  • requestor – User role who issue the request

  • peer – Name or Instance of the peer to send request

  • crypto – crypto method to sign the request

  • deocode – Decode the response payload

Return result

a nested dict of query result

_process_discovery_membership_result(self, q_members)
class hfc.fabric.NullHandler(level=NOTSET)

Bases: logging.Handler

Handler instances dispatch logging events to specific destinations.

The base handler class. Acts as a placeholder which defines the Handler interface. Handlers can optionally use Formatter instances to format records as desired. By default, no formatter is specified; in this case, the ‘raw’ message as determined by record.message is logged.

emit(self, record)

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.