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)
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)
def create_channel(self): client = Client('test/fixtures/network.json') logger.info("start to create channel") request = build_channel_request( client, self.channel_tx, self.channel_name) q = Queue(1) response = client._create_channel(request) response.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x)) status, _ = q.get(timeout=5) self.assertEqual(status.status, 200) logger.info("successfully create the channel: %s", self.channel_name) client.state_store = None
def create_channel(self): logger.info("start to create channel") request = build_channel_request(self.client, self.channel_tx, self.channel_name) q = Queue(1) response = self.client.create_channel(request) response.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x)) status, _ = q.get(timeout=5) try: self.assertEqual(status.status, 200) except AssertionError: logger.error("fail to create the channel with status code", status.status) raise Exception("fail to create channel.") logger.info("successfully create the channel")
def create_channel(self): client = Client() client.state_store = FileKeyValueStore(self.kv_store_path + 'build-channel') logger.info("start to create channel") request = build_channel_request(client, self.channel_tx, self.channel_name) q = Queue(1) response = client.create_channel(request) response.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x)) status, _ = q.get(timeout=5) self.assertEqual(status.status, 200) logger.info("successfully create the channel: %s", self.channel_name) client.state_store = None
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)
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
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]))
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