Ejemplo n.º 1
0
def build_channel_request(client, channel_tx, channel_name):
    """
    Args:
        client: the client instance
        channel_tx: channel config file
        channel_name: channel name
    return channel request to create a channel
    """

    signatures = []
    prop_req = TXProposalRequest()
    with open(channel_tx, 'rb') as f:
        envelope = f.read()
        config = utils.extract_channel_config(envelope)

    orderer_config = E2E_CONFIG['test-network']['orderer']
    with open(orderer_config['tls_cacerts'], 'rb') as tls_cacerts:
        pem = tls_cacerts.read()

    opts = (('grpc.ssl_target_name_override', 'orderer.example.com'),)
    orderer = Orderer(
        endpoint=orderer_config['grpc_endpoint'],
        pem=pem,
        opts=opts
    )
    orderer_admin = get_orderer_org_admin(client)
    orderer_tx_context = TXContext(orderer_admin, ecies(), prop_req, {})
    client.tx_context = orderer_tx_context
    orderer_admin_signature = client.sign_channel_config(config)
    orderer_admin_signature_bytes = orderer_admin_signature.SerializeToString()
    signatures.append(orderer_admin_signature_bytes)
    tx_id = orderer_tx_context.tx_id
    nonce = orderer_tx_context.nonce

    org1_admin = get_peer_org_user(client, 'org1.example.com')
    org1_tx_context = TXContext(org1_admin, ecies(), prop_req, {})
    client.tx_context = org1_tx_context
    org1_admin_signature = client.sign_channel_config(config)
    org1_admin_signature_bytes = org1_admin_signature.SerializeToString()

    signatures.append(org1_admin_signature_bytes)

    org2_admin = get_peer_org_user(client, 'org2.example.com')
    org2_tx_context = TXContext(org2_admin, ecies(), prop_req, {})
    client.tx_context = org2_tx_context
    org2_admin_signature = client.sign_channel_config(config)
    org2_admin_signature_bytes = org2_admin_signature.SerializeToString()
    signatures.append(org2_admin_signature_bytes)

    request = {'config': config,
               'signatures': signatures,
               'channel_name': channel_name,
               'orderer': orderer,
               'tx_id': tx_id,
               'nonce': nonce}

    return request
Ejemplo n.º 2
0
def build_channel_request(client, channel_tx, channel_name):
    """
    Args:
        client: the client instance
        channel_tx: channel config file
        channel_name: channel name
    return channel request to create a channel
    """

    signatures = []
    prop_req = TXProposalRequest()
    with open(channel_tx, 'rb') as f:
        envelope = f.read()
        config = utils.extract_channel_config(envelope)

    orderer_config = net_config['orderer']

    orderer = Orderer(
        endpoint=orderer_config['grpc_endpoint'],
        tls_ca_cert_file=orderer_config['tls_cacerts'],
        opts=(('grpc.ssl_target_name_override', 'orderer.example.com'), ),
    )
    orderer_admin = get_orderer_org_user(state_store=client.state_store)
    orderer_tx_context = TXContext(orderer_admin, ecies(), prop_req, {})
    client.tx_context = orderer_tx_context
    orderer_admin_signature = client.sign_channel_config(config)
    signatures.append(orderer_admin_signature)
    tx_id = orderer_tx_context.tx_id
    nonce = orderer_tx_context.nonce

    org1_admin = get_peer_org_user('org1.example.com', "Admin",
                                   client.state_store)
    org1_tx_context = TXContext(org1_admin, ecies(), prop_req, {})
    client.tx_context = org1_tx_context
    org1_admin_signature = client.sign_channel_config(config)
    signatures.append(org1_admin_signature)

    org2_admin = get_peer_org_user('org2.example.com', "Admin",
                                   client.state_store)
    org2_tx_context = TXContext(org2_admin, ecies(), prop_req, {})
    client.tx_context = org2_tx_context
    org2_admin_signature = client.sign_channel_config(config)
    signatures.append(org2_admin_signature)

    request = {
        'config': config,
        'signatures': signatures,
        'channel_name': channel_name,
        'orderer': orderer,
        'tx_id': tx_id,
        'nonce': nonce
    }

    return request
Ejemplo n.º 3
0
    def test_install_chaincode_success(self):
        """
        Test a chaincode installation success.
        :return:
        """

        opts = (('grpc.ssl_target_name_override',
                 self.peer0_org1_tls_hostname), )
        peer0_org1 = create_peer(endpoint=self.peer0_org1_endpoint,
                                 tls_cacerts=self.peer0_org1_tls_certs,
                                 opts=opts)
        org1_admin = get_peer_org_user('org1.example.com', "Admin",
                                       self.client.state_store)

        crypto = ecies()
        tran_prop_req = create_tx_prop_req(CC_INSTALL, CC_PATH, CC_TYPE_GOLANG,
                                           CC_NAME, CC_VERSION)
        tx_context = create_tx_context(org1_admin, crypto, tran_prop_req)

        queue = Queue(1)

        sleep(5)
        response = self.client.send_install_proposal(tx_context, [peer0_org1])

        response.subscribe(on_next=lambda x: queue.put(x),
                           on_error=lambda x: queue.put(x))

        res = queue.get(timeout=5)
        proposal_response, _ = res[0][0]
        self.assertEqual(proposal_response.response.status, 200)
Ejemplo n.º 4
0
    def test_instantiate_chaincode(self):

        channel = self.client.new_channel(self.channel_name)
        org1 = 'org1.example.com'
        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )
        endpoint = peer_config['grpc_request_endpoint']

        peer = create_peer(endpoint=endpoint,
                           tls_cacerts=tls_cacerts,
                           opts=opts)

        org1_admin = get_peer_org_user(org1, 'Admin', self.client.state_store)
        crypto = ecies()

        # for chain code install
        tran_prop_req_in = create_tx_prop_req(prop_type=CC_INSTALL,
                                              cc_path=CC_PATH,
                                              cc_type=CC_TYPE_GOLANG,
                                              cc_name=CC_NAME,
                                              cc_version=CC_VERSION)

        # install chain code
        tx_context_in = create_tx_context(org1_admin, crypto, tran_prop_req_in)

        # for chain code deploy
        args = ['a', '100', 'b', '40']
        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               args=args,
                                               fcn='init')

        # deploy the chain code
        tx_context_dep = create_tx_context(org1_admin, crypto,
                                           tran_prop_req_dep)

        # create a channel
        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)

        self.client._create_channel(request)

        # join channel
        join_req = build_join_channel_req(org1, channel, self.client)
        channel.join_channel(join_req)

        self.client.send_install_proposal(tx_context_in, [peer])

        # send the transaction to the channel
        res = channel.send_instantiate_proposal(tx_context_dep, [peer])
        tran_req = build_tx_req(res)

        tx_context = create_tx_context(org1_admin, crypto, TXProposalRequest())
        response = send_transaction(channel.orderers, tran_req, tx_context)

        self.assertEqual(response[0].status, 200)
Ejemplo n.º 5
0
    def test_instantiate_chaincode(self):

        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        endpoint = peer_config['grpc_request_endpoint']

        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )

        peer = create_peer(endpoint=endpoint,
                           tls_cacerts=tls_cacerts,
                           opts=opts)

        tran_prop_req_in = create_tx_prop_req(prop_type=CC_INSTALL,
                                              cc_path=CC_PATH,
                                              cc_type=CC_TYPE_GOLANG,
                                              cc_name=CC_NAME,
                                              cc_version=CC_VERSION)
        args = ['a', '100', 'b', '40']
        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               fcn='init',
                                               args=args)

        org1 = 'org1.example.com'
        crypto = ecies()
        org1_admin = get_peer_org_user(org1, 'Admin', self.client.state_store)
        tx_context_in = create_tx_context(org1_admin, crypto, tran_prop_req_in)

        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)

        res = self.client._create_channel(request)
        q = Queue(1)
        res.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x))
        status, _ = q.get(timeout=5)
        if status.status == 200:
            logger.info("create channel successfully")

        time.sleep(5)
        channel = self.client.new_channel(self.channel_name)
        join_req = build_join_channel_req(org1, channel, self.client)
        channel.join_channel(join_req)

        res = self.client.send_install_proposal(tx_context_in, [peer])
        res.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x))
        status, _ = q.get(timeout=5)[0][0]
        if status.response.status == 200:
            logger.info("chaincode installed successfully")

        time.sleep(5)
        tx_context_dep = create_tx_context(org1_admin, crypto,
                                           tran_prop_req_dep)
        res = channel.send_instantiate_proposal(tx_context_dep, [peer])
        assert (res)
    async def upgrade_chaincode(self):
        org = 'org1.example.com'

        org_admin = get_peer_org_user(org, 'Admin', self.client.state_store)
        peers = ['peer0.' + org, 'peer1.' + org]

        # create a channel
        response = await self.client.channel_create(
            'orderer.example.com',
            self.channel_name,
            org_admin,
            config_yaml=self.config_yaml,
            channel_profile=self.channel_profile)
        self.assertTrue(response)

        # join channel
        response = await self.client.channel_join(
            requestor=org_admin,
            channel_name=self.channel_name,
            peers=['peer0.' + org, 'peer1.' + org],
            orderer='orderer.example.com')
        self.assertTrue(response)

        instantiate_args = ['a', '200', 'b', '300']
        query_args = ['b']
        policy = s2d().parse("OR('Org1MSP.member')")

        chaincode = Chaincode(self.client, CC_NAME)
        res = await chaincode.install(org_admin, peers, CC_PATH, CC_VERSION)
        self.assertTrue(res)
        res = await chaincode.instantiate(org_admin,
                                          self.channel_name,
                                          peers,
                                          CC_VERSION,
                                          policy,
                                          args=instantiate_args,
                                          wait_for_event=True)
        self.assertTrue(res)
        res = await chaincode.query(org_admin, self.channel_name, peers,
                                    query_args)
        self.assertEqual(res, '300')
        res = await chaincode.install(org_admin, peers, CC_PATH,
                                      CC_UPGRADED_VERSION)
        self.assertTrue(res)
        res = await chaincode.upgrade(org_admin,
                                      self.channel_name,
                                      peers,
                                      CC_UPGRADED_VERSION,
                                      policy,
                                      args=instantiate_args,
                                      wait_for_event=True)
        self.assertTrue(res)
        res = await chaincode.query(org_admin, self.channel_name, peers,
                                    query_args)
        self.assertEqual(res, '300')
Ejemplo n.º 7
0
    def test_invoke_chaincode_sucess(self):

        channel = self.client.new_channel(self.channel_name)
        org1 = "org1.example.com"
        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )
        endpoint = peer_config['grpc_request_endpoint']
        org1_peer = create_peer(endpoint=endpoint,
                                tls_cacerts=tls_cacerts,
                                opts=opts)
        org1_admin = get_peer_org_user(org1, "Admin", self.client.state_store)

        crypto = ecies()
        tran_prop_req_install = create_tx_prop_req(prop_type=CC_INSTALL,
                                                   cc_path=CC_PATH,
                                                   cc_type=CC_TYPE_GOLANG,
                                                   cc_name=CC_NAME,
                                                   cc_version=CC_VERSION)
        tx_context_install = create_tx_context(org1_admin, crypto,
                                               tran_prop_req_install)

        args_dep = ['a', '200', 'b', '300']
        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               args=args_dep,
                                               fcn='init')

        tx_context_dep = create_tx_context(org1_admin, crypto,
                                           tran_prop_req_dep)

        args = ['a', 'b', '100']
        tran_prop_req = create_tx_prop_req(prop_type=CC_INVOKE,
                                           cc_type=CC_TYPE_GOLANG,
                                           cc_name=CC_NAME,
                                           cc_version=CC_VERSION,
                                           fcn='invoke',
                                           args=args)
        tx_context = create_tx_context(org1_admin, crypto, tran_prop_req)

        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)
        self.client._create_channel(request)
        sleep(5)

        join_req = build_join_channel_req(org1, channel, self.client)
        channel.join_channel(join_req)
        sleep(5)

        self.client.send_install_proposal(tx_context_install, [org1_peer])
        sleep(5)

        res = channel.send_instantiate_proposal(tx_context_dep, [org1_peer])
        sleep(5)

        tran_req = build_tx_req(res)
        send_transaction(channel.orderers, tran_req, tx_context)
        sleep(5)

        tx_context_tx = create_tx_context(org1_admin, crypto,
                                          TXProposalRequest())
        res = channel.send_tx_proposal(tx_context, [org1_peer])

        tran_req = build_tx_req(res)
        response = send_transaction(channel.orderers, tran_req, tx_context_tx)
        q = Queue(1)
        response.subscribe(on_next=lambda x: q.put(x),
                           on_error=lambda x: q.put(x))
        res, _ = q.get(timeout=5)
        self.assertEqual(res.status, 200)
Ejemplo n.º 8
0
    async def invoke_chaincode(self):

        org1 = "org1.example.com"
        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )
        endpoint = peer_config['grpc_request_endpoint']

        self.peer = create_peer(endpoint=endpoint,
                                tls_cacerts=tls_cacerts,
                                opts=opts)

        self.org1_admin = get_peer_org_user(org1, "Admin",
                                            self.client.state_store)

        # channel create
        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)
        responses = await self.client._create_or_update_channel(request)
        self.assertTrue(all([x.status == 200 for x in responses]))

        self.channel = self.client.new_channel(self.channel_name)

        # join channel

        join_req = await build_join_channel_req(org1, self.channel,
                                                self.client)

        responses = self.channel.join_channel(join_req)
        res = await asyncio.gather(*responses)
        self.assertTrue(all([x.response.status == 200 for x in res]))

        # install

        tran_prop_req_install = create_tx_prop_req(prop_type=CC_INSTALL,
                                                   cc_path=CC_PATH,
                                                   cc_type=CC_TYPE_GOLANG,
                                                   cc_name=CC_NAME,
                                                   cc_version=CC_VERSION)

        tx_context_install = create_tx_context(self.org1_admin,
                                               self.org1_admin.cryptoSuite,
                                               tran_prop_req_install)

        responses, proposal, header = self.client.send_install_proposal(
            tx_context_install, [self.peer])
        await asyncio.gather(*responses)

        # instantiate

        args_dep = ['a', '200', 'b', '300']

        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               args=args_dep,
                                               fcn='init')
        tx_context_dep = create_tx_context(self.org1_admin,
                                           self.org1_admin.cryptoSuite,
                                           tran_prop_req_dep)
        responses, proposal, header = self.channel.send_instantiate_proposal(
            tx_context_dep, [self.peer])
        res = await asyncio.gather(*responses)

        tran_req = build_tx_req((res, proposal, header))
        tx_context = create_tx_context(self.org1_admin,
                                       self.org1_admin.cryptoSuite,
                                       TXProposalRequest())
        await get_stream_result(
            send_transaction(self.client.orderers, tran_req, tx_context))

        # wait for event instantiate
        channel_event_hub = self.channel.newChannelEventHub(
            self.peer, self.org1_admin)
        stream = channel_event_hub.connect(filtered=False)

        channel_event_hub.registerTxEvent(tx_context_dep.tx_id,
                                          disconnect=True)

        try:
            await asyncio.wait_for(stream, timeout=30)
        except asyncio.TimeoutError:
            raise TimeoutError('waitForEvent timed out.')
        except Exception as e:
            if not isinstance(e, grpc._channel._Rendezvous) \
                    or not e.cancelled():
                raise e
        finally:
            channel_event_hub.disconnect()

        # invoke part
        args = ['a', 'b', '100']
        tran_prop_req = create_tx_prop_req(prop_type=CC_INVOKE,
                                           cc_type=CC_TYPE_GOLANG,
                                           cc_name=CC_NAME,
                                           fcn='invoke',
                                           args=args)

        tx_context = create_tx_context(self.org1_admin,
                                       self.org1_admin.cryptoSuite,
                                       tran_prop_req)

        responses, p, h = self.channel.send_tx_proposal(
            tx_context, [self.peer])
        res = await asyncio.gather(*responses)
        tran_req = build_tx_req((res, p, h))

        tx_context_tx = create_tx_context(self.org1_admin,
                                          self.org1_admin.cryptoSuite,
                                          tran_req)
        await get_stream_result(
            send_transaction(self.channel.orderers, tran_req, tx_context_tx))

        # wait for chaincode events
        channel_event_hub = self.channel.newChannelEventHub(
            self.peer, self.org1_admin)

        stream = channel_event_hub.connect(filtered=False)

        self.evts = {}
        # with tx event
        # channel_event_hub.registerTxEvent(tx_context.tx_id,
        #                                  unregister=True, disconnect=True,
        #                                  onEvent=self.onTxEvent)

        # with chaincode event
        self.registerChaincodeEvent(tx_context.tx_id, CC_NAME, '^invoked*',
                                    channel_event_hub)

        try:
            await asyncio.wait_for(stream, timeout=30)
        except asyncio.TimeoutError:
            raise TimeoutError('waitForEvent timed out.')
        except Exception as e:
            if not isinstance(e, grpc._channel._Rendezvous) \
                    or not e.cancelled():
                raise e
        finally:
            channel_event_hub.disconnect()

        return tx_context.tx_id
Ejemplo n.º 9
0
def build_join_channel_req(org, channel, client):
    """
    For test, there is only one peer.

    Args:
        org: org
        channel: the channel to join
        client: client instance
    Return:
        return request for joining channel
        """

    def block_event_callback(block):

        pass

    client._crypto_suite = ecies()
    request = {}
    tx_prop_req = TXProposalRequest()

    # add the orderer
    orderer_config = test_network['orderer']
    endpoint = orderer_config['grpc_endpoint']
    ca_root_path = orderer_config['tls_cacerts']
    orderer = Orderer(endpoint=endpoint, tls_ca_cert_file=ca_root_path,
                      opts=(('grpc.ssl_target_name_override',
                             'orderer.example.com'),))
    channel.add_orderer(orderer)

    # get the genesis block
    orderer_admin = get_orderer_org_user(state_store=client.state_store)
    tx_context = TXContext(orderer_admin, ecies(), tx_prop_req)
    client.tx_context = tx_context
    genesis_block = channel.get_genesis_block().SerializeToString()

    # create the peer
    org_admin = get_peer_org_user(org, "Admin", client.state_store)
    client.tx_context = TXContext(org_admin, ecies(), tx_prop_req)
    tx_id = client.tx_context.tx_id

    peer_config = test_network[org]["peers"]['peer0']
    ca_root = peer_config["tls_cacerts"]

    endpoint = peer_config["grpc_request_endpoint"]
    opts = (('grpc.ssl_target_name_override',
             peer_config['server_hostname']),)
    peer = Peer(endpoint=endpoint, tls_cacerts=ca_root, opts=opts)

    """
    # connect the peer
    eh = EventHub()
    event = peer_config['grpc_event_endpoint']

    tx_id = client.tx_context.tx_id
    eh.set_peer_addr(event)
    eh.connect()
    eh.register_block_event(block_event_callback)
    all_ehs.append(eh)
    """

    request["targets"] = [peer]
    request["block"] = genesis_block
    request["tx_id"] = tx_id
    request["transient_map"] = {}

    return request
Ejemplo n.º 10
0
    def test_invoke_chaincode_sucess(self):
        loop = asyncio.get_event_loop()

        channel = self.client.new_channel(self.channel_name)
        org1 = "org1.example.com"
        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )
        endpoint = peer_config['grpc_request_endpoint']
        org1_peer = create_peer(endpoint=endpoint,
                                tls_cacerts=tls_cacerts,
                                opts=opts)
        org1_admin = get_peer_org_user(org1, "Admin", self.client.state_store)

        crypto = ecies()
        tran_prop_req_install = create_tx_prop_req(prop_type=CC_INSTALL,
                                                   cc_path=CC_PATH,
                                                   cc_type=CC_TYPE_GOLANG,
                                                   cc_name=CC_NAME,
                                                   cc_version=CC_VERSION)
        tx_context_install = create_tx_context(org1_admin, crypto,
                                               tran_prop_req_install)

        args_dep = ['a', '200', 'b', '300']
        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               args=args_dep,
                                               fcn='init')

        tx_context_dep = create_tx_context(org1_admin, crypto,
                                           tran_prop_req_dep)

        args = ['a', 'b', '100']
        tran_prop_req = create_tx_prop_req(prop_type=CC_INVOKE,
                                           cc_type=CC_TYPE_GOLANG,
                                           cc_name=CC_NAME,
                                           fcn='invoke',
                                           args=args)
        tx_context = create_tx_context(org1_admin, crypto, tran_prop_req)

        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)
        loop.run_until_complete(self.client._create_or_update_channel(request))

        join_req = loop.run_until_complete(
            build_join_channel_req(org1, channel, self.client))
        responses = channel.join_channel(join_req)
        res = loop.run_until_complete(asyncio.gather(*responses))
        self.assertTrue(all([x.response.status == 200 for x in res]))

        responses, proposal, header = self.client.send_install_proposal(
            tx_context_install, [org1_peer])
        loop.run_until_complete(asyncio.gather(*responses))

        responses, proposal, header = channel.send_instantiate_proposal(
            tx_context_dep, [org1_peer])
        res = loop.run_until_complete(asyncio.gather(*responses))

        tran_req = build_tx_req((res, proposal, header))
        send_transaction(channel.orderers, tran_req, tx_context)
        loop.run_until_complete(
            get_stream_result(
                send_transaction(channel.orderers, tran_req, tx_context)))

        tx_context_tx = create_tx_context(org1_admin, crypto,
                                          TXProposalRequest())
        responses, proposal, header = channel.send_tx_proposal(
            tx_context, [org1_peer])
        res = loop.run_until_complete(asyncio.gather(*responses))

        tran_req = build_tx_req((res, proposal, header))

        responses = loop.run_until_complete(
            get_stream_result(
                send_transaction(channel.orderers, tran_req, tx_context_tx)))

        self.assertTrue(all([x.status == 200 for x in responses]))
Ejemplo n.º 11
0
    def invoke_chaincode(self):

        self.channel = self.client.new_channel(self.channel_name)
        org1 = "org1.example.com"
        peer_config = test_network['org1.example.com']['peers']['peer0']
        tls_cacerts = peer_config['tls_cacerts']
        opts = (('grpc.ssl_target_name_override',
                 peer_config['server_hostname']), )
        endpoint = peer_config['grpc_request_endpoint']
        self.org1_peer = create_peer(endpoint=endpoint,
                                     tls_cacerts=tls_cacerts,
                                     opts=opts)
        self.org1_admin = get_peer_org_user(org1, "Admin",
                                            self.client.state_store)

        crypto = ecies()
        tran_prop_req_install = create_tx_prop_req(prop_type=CC_INSTALL,
                                                   cc_path=CC_PATH,
                                                   cc_type=CC_TYPE_GOLANG,
                                                   cc_name=CC_NAME,
                                                   cc_version=CC_VERSION)
        tx_context_install = create_tx_context(self.org1_admin, crypto,
                                               tran_prop_req_install)

        args_dep = ['a', '200', 'b', '300']
        tran_prop_req_dep = create_tx_prop_req(prop_type=CC_INSTANTIATE,
                                               cc_type=CC_TYPE_GOLANG,
                                               cc_name=CC_NAME,
                                               cc_version=CC_VERSION,
                                               args=args_dep,
                                               fcn='init')

        tx_context_dep = create_tx_context(self.org1_admin, crypto,
                                           tran_prop_req_dep)

        args = ['a', 'b', '100']
        tran_prop_req = create_tx_prop_req(prop_type=CC_INVOKE,
                                           cc_type=CC_TYPE_GOLANG,
                                           cc_name=CC_NAME,
                                           cc_version=CC_VERSION,
                                           fcn='invoke',
                                           args=args)
        tx_context = create_tx_context(self.org1_admin, crypto, tran_prop_req)

        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)
        self.client._create_channel(request)

        join_req = build_join_channel_req(org1, self.channel, self.client)
        self.channel.join_channel(join_req)

        self.client.send_install_proposal(tx_context_install, [self.org1_peer])

        res = self.channel.send_instantiate_proposal(tx_context_dep,
                                                     [self.org1_peer])
        tran_req = build_tx_req(res)
        send_transaction(self.channel.orderers, tran_req, tx_context)

        tx_context_tx = create_tx_context(self.org1_admin, crypto,
                                          TXProposalRequest())

        res = self.channel.send_tx_proposal(tx_context, [self.org1_peer])

        tran_req = build_tx_req(res)

        res = send_transaction(self.channel.orderers, tran_req, tx_context_tx)

        tx_id = tx_context_dep.tx_id

        return tx_id
Ejemplo n.º 12
0
    def test_create_channel_missing_signatures(self):
        signatures = []

        with open(self.orderer_tls_certs) as f:
            pem = f.read()

        opts = (('grpc.ssl_target_name_override', 'orderer.example.com'), )
        orderer = Orderer(pem=pem, opts=opts)

        with open(self.configtx_path, 'rb') as f:
            envelope = f.read()

        # convert envelope to config
        config = extract_channel_config(envelope)

        channel_name = 'businesschannel'

        # signatures orderer admin
        orderer_admin = get_orderer_org_admin(self.client)
        orderer_admin_tx_context = TXContext(orderer_admin, Ecies(), {})
        self.client.tx_context = orderer_admin_tx_context

        orderer_admin_signature = self.client.sign_channel_config(config)
        orderer_admin_signature.SerializeToString()

        # take the tx_id and nonce from the oderer user context
        tx_id = orderer_admin_tx_context.tx_id
        nonce = orderer_admin_tx_context.nonce

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org1 admin
        org1_admin = get_peer_org_user(self.client, 'org1.example.com')
        org1_admin_tx_context = TXContext(org1_admin, Ecies(), {})
        self.client.tx_context = org1_admin_tx_context

        org1_admin_signature = self.client.sign_channel_config(config)
        org1_admin_signature.SerializeToString()

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org2 admin
        org2_admin = get_peer_org_user(self.client, 'org2.example.com')
        org2_admin_tx_context = TXContext(org2_admin, Ecies(), {})
        self.client.tx_context = org2_admin_tx_context

        org2_admin_signature = self.client.sign_channel_config(config)
        org2_admin_signature.SerializeToString()

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }

        queue = Queue(1)

        response = self.client.create_channel(request)

        response.subscribe(on_next=lambda x: queue.put(x),
                           on_error=lambda x: queue.put(x))

        status, _ = queue.get(timeout=5)
        self.assertEqual(status.status, 400)
Ejemplo n.º 13
0
def build_join_channel_req(org, channel, client):
    """
    For test, there is only one peer.

    Args:
        org: org
        channel: the channel to join
        client: client instance
    Return:
        return request for joining channel
        """

    def block_event_callback(block):

        pass

    client._crypto_suite = ecies()
    all_ehs = []
    request = {}
    tx_prop_req = TXProposalRequest()

    # add the orderer
    orderer_config = test_network['orderer']
    endpoint = orderer_config['grpc_endpoint']
    opts = (('grpc.ssl_target_name_override',
             orderer_config['server_hostname']),)

    ca_root_path = orderer_config['tls_cacerts']
    with open(ca_root_path, 'rb') as f:
        pem = f.read()
    orderer = Orderer(endpoint=endpoint, pem=pem, opts=opts)
    channel.add_orderer(orderer)

    # get the genesis block
    orderer_admin = get_orderer_org_admin(client)
    tx_context = TXContext(orderer_admin, ecies(), tx_prop_req)
    client.tx_context = tx_context
    genesis_block = channel.get_genesis_block().SerializeToString()
    if not genesis_block:
        return None

    # create the peer
    org_admin = get_peer_org_user(client, org)
    client.tx_context = TXContext(org_admin, ecies(), tx_prop_req)

    peer_config = test_network[org]["peers"]['peer0']
    ca_root = peer_config["tls_cacerts"]
    with open(ca_root, 'rb') as f:
        pem = f.read()
    peer = Peer(pem=pem, opts=None)

    # connect the peer
    eh = EventHub()
    event = peer_config['grpc_event_endpoint']
    opts = {'pem': pem, 'hostname': peer_config['server_hostname']}

    tx_id = client.tx_context.tx_id
    eh.set_peer_addr(event)
    eh.connect()
    eh.register_block_event(block_event_callback)
    all_ehs.append(eh)

    request["targets"] = [peer]
    request["block"] = genesis_block
    request["tx_id"] = tx_id

    return request