Пример #1
0
    def 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

        :param profile_path: The connection profile file path
        :return:
        """
        with open(profile_path, 'r') as profile:
            d = json.load(profile)
            self.network_info = d

        # read kv store path
        self.kv_store_path = self.get_net_info('client', 'credentialStore',
                                               'path')
        if self.kv_store_path:
            self._state_store = FileKeyValueStore(self.kv_store_path)
        else:
            _logger.warning(
                'No kv store path exists in profile {}'.format(profile_path))

        # Init organizations
        orgs = self.get_net_info('organizations')
        for name in orgs:
            _logger.debug("create org with name={}".format(name))
            org = create_org(name, orgs[name], self.state_store)
            self._organizations[name] = org

        # Init CAs
        # TODO

        # Init orderer nodes
        orderers = self.get_net_info('orderers')
        _logger.debug("Import orderers = {}".format(orderers.keys()))
        for name in orderers:
            orderer = Orderer(name=name, endpoint=orderers[name]['url'])
            orderer.init_with_bundle(orderers[name])
            self.orderers[name] = orderer

        # Init peer nodes
        peers = self.get_net_info('peers')
        _logger.debug("Import peers = {}".format(peers.keys()))
        for name in peers:
            peer = Peer(name=name)
            peer.init_with_bundle(peers[name])
            self._peers[name] = peer
Пример #2
0
def update_client_with_discovery(client, discovery_results):

    # Get all msp tls root cert files
    tls_root_certs = {}

    for mspid, msp_info in discovery_results['config']['msps'].items():
        tls_root_certs[mspid] = base64.decodebytes(
            msp_info['tls_root_certs'].pop().encode())

    # Load one peer per msp for endorsing transaction
    for msp in discovery_results['members']:
        if not len(msp):
            continue

        peer_info = msp[0]

        if peer_info['mspid'] != LEDGER['client']['msp_id']:
            peer = Peer(name=peer_info['mspid'])

            with tempfile.NamedTemporaryFile() as tls_root_cert:
                tls_root_cert.write(tls_root_certs[peer_info['mspid']])
                tls_root_cert.flush()

                url = peer_info['endpoint']
                external_port = os.environ.get('BACKEND_PEER_PORT_EXTERNAL',
                                               None)
                # use case for external development
                if external_port:
                    url = f"{peer_info['endpoint'].split(':')[0]}:{external_port}"
                peer.init_with_bundle({
                    'url': url,
                    'grpcOptions': {
                        'grpc-max-send-message-length':
                        15,
                        'grpc.ssl_target_name_override':
                        peer_info['endpoint'].split(':')[0]
                    },
                    'tlsCACerts': {
                        'path': tls_root_cert.name
                    },
                    'clientKey': {
                        'path': LEDGER['peer']['clientKey']
                    },  # use peer creds (mutual tls)
                    'clientCert': {
                        'path': LEDGER['peer']['clientCert']
                    },  # use peer creds (mutual tls)
                })

            client._peers[peer_info['mspid']] = peer

    # Load one orderer for broadcasting transaction
    orderer_mspid, orderer_info = list(
        discovery_results['config']['orderers'].items())[0]

    orderer = Orderer(name=orderer_mspid)

    with tempfile.NamedTemporaryFile() as tls_root_cert:
        tls_root_cert.write(tls_root_certs[orderer_mspid])
        tls_root_cert.flush()

        # Need loop
        orderer.init_with_bundle({
            'url': f"{orderer_info[0]['host']}:{orderer_info[0]['port']}",
            'grpcOptions': {
                'grpc-max-send-message-length': 15,
                'grpc.ssl_target_name_override': orderer_info[0]['host']
            },
            'tlsCACerts': {
                'path': tls_root_cert.name
            },
            'clientKey': {
                'path': LEDGER['peer']['clientKey']
            },  # use peer creds (mutual tls)
            'clientCert': {
                'path': LEDGER['peer']['clientCert']
            },  # use peer creds (mutual tls)
        })

    client._orderers[orderer_mspid] = orderer
def _update_client_with_discovery(client, discovery_results):

    # Get all msp tls root cert files
    tls_root_certs = {}

    for mspid, msp_info in discovery_results['config']['msps'].items():
        tls_root_certs[mspid] = base64.decodebytes(
            msp_info['tls_root_certs'].pop().encode())

    # Load one peer per msp for endorsing transaction
    for msp in discovery_results['members']:
        if not len(msp):
            continue

        peer_info = msp[0]

        if peer_info['mspid'] != settings.LEDGER_MSP_ID:
            peer = Peer(name=peer_info['mspid'])

            with tempfile.NamedTemporaryFile() as tls_root_cert:
                tls_root_cert.write(tls_root_certs[peer_info['mspid']])
                tls_root_cert.flush()

                url = peer_info['endpoint']
                peer.init_with_bundle({
                    'url':
                    url,
                    'grpcOptions':
                    ledger_grpc_options(peer_info['endpoint'].split(':')[0]),
                    'tlsCACerts': {
                        'path': tls_root_cert.name
                    },
                    'clientKey': {
                        'path': settings.LEDGER_PEER_TLS_CLIENT_KEY
                    },
                    'clientCert': {
                        'path': settings.LEDGER_PEER_TLS_CLIENT_CERT
                    }
                })

            client._peers[peer_info['mspid']] = peer

    # Load one orderer for broadcasting transaction
    orderer_mspid, orderer_info = list(
        discovery_results['config']['orderers'].items())[0]

    orderer = Orderer(name=orderer_mspid)

    with tempfile.NamedTemporaryFile() as tls_root_cert:
        tls_root_cert.write(tls_root_certs[orderer_mspid])
        tls_root_cert.flush()

        # Need loop
        orderer.init_with_bundle({
            'url':
            f"{orderer_info[0]['host']}:{orderer_info[0]['port']}",
            'grpcOptions':
            ledger_grpc_options(orderer_info[0]['host']),
            'tlsCACerts': {
                'path': tls_root_cert.name
            },
            'clientKey': {
                'path': settings.LEDGER_PEER_TLS_CLIENT_KEY
            },
            'clientCert': {
                'path': settings.LEDGER_PEER_TLS_CLIENT_CERT
            }
        })

    client._orderers[orderer_mspid] = orderer