def test_string_to_signature(self): with open(self.channel_tx, 'rb') as f: channel_tx = f.read() channel_config = utils.extract_channel_config(channel_tx) client = Client() client.state_store = FileKeyValueStore(self.kv_store_path) orderer_org_admin = get_orderer_org_admin(client) orderer_org_admin_tx_context = \ TXContext(orderer_org_admin, Ecies(), {}) client.tx_context = orderer_org_admin_tx_context orderer_org_admin_signature = client.sign_channel_config( channel_config) orderer_org_admin_signature_bytes = \ orderer_org_admin_signature.SerializeToString() proto_signature = utils.string_to_signature( [orderer_org_admin_signature_bytes]) self.assertIsInstance(proto_signature, list) self.assertTrue( 'OrdererMSP' in proto_signature[0].signature_header.__str__())
def test_build_header(self): timestamp = utils.current_timestamp() client = Client() client.state_store = FileKeyValueStore(self.kv_store_path) orderer_org_admin = get_orderer_org_admin(client) orderer_org_admin_tx_context = \ TXContext(orderer_org_admin, Ecies(), {}) client.tx_context = orderer_org_admin_tx_context orderer_org_admin_serialized = utils.create_serialized_identity( orderer_org_admin) serialized_identity = identities_pb2.SerializedIdentity() serialized_identity.ParseFromString(orderer_org_admin_serialized) proto_channel_header = utils.build_channel_header( common_pb2.HeaderType.Value('CONFIG_UPDATE'), orderer_org_admin_tx_context.tx_id, self.channel_id, timestamp ) channel_header = utils.build_header( orderer_org_admin_tx_context.identity, proto_channel_header, orderer_org_admin_tx_context.nonce ) self.assertIsInstance(channel_header, common_pb2.Header)
def wait(): with get_event_loop() as loop: channel_name = LEDGER['channel_name'] chaincode_name = LEDGER['chaincode_name'] peer = LEDGER['peer'] peer_port = peer["port"][os.environ.get('BACKEND_PEER_PORT', 'external')] client = Client() channel = client.new_channel(channel_name) target_peer = Peer(name=peer['name']) requestor_config = LEDGER['client'] target_peer.init_with_bundle({ 'url': f'{peer["host"]}:{peer_port}', 'grpcOptions': peer['grpcOptions'], 'tlsCACerts': { 'path': peer['tlsCACerts'] }, 'clientKey': { 'path': peer['clientKey'] }, 'clientCert': { 'path': peer['clientCert'] }, }) try: # can fail requestor = create_user( name=requestor_config['name'] + '_events', org=requestor_config['org'], state_store=FileKeyValueStore(requestor_config['state_store']), msp_id=requestor_config['msp_id'], key_path=glob.glob(requestor_config['key_path'])[0], cert_path=requestor_config['cert_path']) except BaseException: pass else: channel_event_hub = channel.newChannelEventHub( target_peer, requestor) # use chaincode event # uncomment this line if you want to replay blocks from the beginning for debugging purposes # stream = channel_event_hub.connect(start=0, filtered=False) stream = channel_event_hub.connect(filtered=False) channel_event_hub.registerChaincodeEvent(chaincode_name, 'tuples-updated', onEvent=on_tuples) loop.run_until_complete(stream)
def create_user(self, enrollment_id, org, msp_id, state_store=None): """ Returns an instance of a user whose identity is stored in the FileSystemWallet """ if not self.exists(enrollment_id): raise AttributeError('"user" does not exist') state_store = FileKeyValueStore(self._path) cert_path = self._path + '/' + enrollment_id + '/' + 'private_sk' key_path = self._path + '/' + enrollment_id + '/' + 'enrollmentCert.pem' user = create_user(enrollment_id, org, state_store, msp_id, key_path, cert_path) return user
def test_create_serialized_identity(self): client = Client() client.state_store = FileKeyValueStore(self.kv_store_path) orderer_org_admin = get_orderer_org_admin(client) orderer_org_admin_serialized = utils.create_serialized_identity( orderer_org_admin) serialized_identity = identities_pb2.SerializedIdentity() serialized_identity.ParseFromString(orderer_org_admin_serialized) self.assertEqual(serialized_identity.mspid, self.orderer_org_mspid)
def setUp(self): self.base_path = "/tmp/fabric-sdk-py" self.kv_store_path = os.path.join(self.base_path, "key-value-store") self.client = Client() self.client.state_store = FileKeyValueStore(self.kv_store_path) self.channel_tx = \ E2E_CONFIG['test-network']['channel-artifacts']['channel.tx'] self.channel_name = \ E2E_CONFIG['test-network']['channel-artifacts']['channel_id'] self.compose_file_path = \ E2E_CONFIG['test-network']['docker']['compose_file_tls'] self.start_test_env()
def setUp(self): self.gopath_bak = os.environ.get('GOPATH', '') gopath = os.path.normpath( os.path.join(os.path.dirname(__file__), "../../fixtures/chaincode")) os.environ['GOPATH'] = os.path.abspath(gopath) self.configtx_path = \ E2E_CONFIG['test-network']['channel-artifacts']['channel.tx'] self.compose_file_path = \ E2E_CONFIG['test-network']['docker']['compose_file_tls'] self.base_path = "/tmp/fabric-sdk-py" self.kv_store_path = os.path.join(self.base_path, "key-value-store") self.client = Client() self.client.state_store = FileKeyValueStore(self.kv_store_path) self.start_test_env()
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
def create_user(self, enrollment_id, org, msp_id, state_store=None): """Returns an instance of a user whose identity is stored in the FileSystemWallet :param enrollment_id: enrollment id :param org: organization :param msp_id: MSP id :param state_store: state store (Default value = None) :return: a user instance """ if not self.exists(enrollment_id): raise AttributeError('"user" does not exist') state_store = FileKeyValueStore(self._path) key_path = self._path + '/' + enrollment_id + '/' + 'private_sk' cert_path = self._path + '/' + enrollment_id + '/' + 'enrollmentCert.pem' user = create_user(enrollment_id, org, state_store, msp_id, key_path, cert_path) return user
def join_channel(self): # sleep 5 seconds for channel created time.sleep(5) client = Client() client.state_store = FileKeyValueStore(self.kv_store_path + 'join-channel') channel = client.new_channel(self.channel_name) logger.info("start to join channel") orgs = ["org1.example.com", "org2.example.com"] for org in orgs: request = build_join_channel_req(org, channel, client) assert (request) # result = True and channel.join_channel(request) logger.info("peers in org: %s join channel: %", org, self.channel_name) logger.info("joining channel tested succefully") client.state_store = None
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 join_channel(self): # wait for channel created time.sleep(5) client = Client('test/fixtures/network.json') channel = client.new_channel(self.channel_name) logger.info("start to join channel") orgs = ["org1.example.com", "org2.example.com"] done = True for org in orgs: client.state_store = FileKeyValueStore( self.client.kv_store_path + org) request = build_join_channel_req(org, channel, client) done = done and channel.join_channel(request) if done: logger.info("peers in org: %s join channel: %s.", org, self.channel_name) if done: logger.info("joining channel tested successfully.") client.state_store = None assert(done)
'LEDGER_CONFIG_FILE', f'{SUBSTRA_FOLDER}/conf/{ORG}/substra-backend/conf.json') LEDGER = json.load(open(LEDGER_CONFIG_FILE, 'r')) LEDGER_SYNC_ENABLED = True LEDGER_CALL_RETRY = True LEDGER_MAX_RETRY_TIMEOUT = 5 PEER_PORT = LEDGER['peer']['port'][os.environ.get('BACKEND_PEER_PORT', 'external')] LEDGER['requestor'] = create_user( name=LEDGER['client']['name'], org=LEDGER['client']['org'], state_store=FileKeyValueStore(LEDGER['client']['state_store']), msp_id=LEDGER['client']['msp_id'], key_path=glob.glob(LEDGER['client']['key_path'])[0], cert_path=LEDGER['client']['cert_path']) 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({
def init_cli(orgs): cli = Client() cli._state_store = FileKeyValueStore('/tmp/kvs/') return update_cli(cli, orgs)
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
} peer1_owkin = create_peer(endpoint=peer_config['url'], tls_cacerts=peer_config['tlsCACerts']['path'], client_key=peer_config['clientKey']['path'], client_cert=peer_config['clientServer']['path'], opts=[(k, v) for k, v in peer_config['grpcOptions'].items() ]) key_path = glob.glob('/substra/data/orgs/owkin/admin/msp/keystore/*')[0] cert_path = '/substra/data/orgs/owkin/admin/msp/signcerts/cert.pem' admin_owkin = create_user(name='admin', org='owkin', state_store=FileKeyValueStore('/tmp/kvs/'), msp_id='owkinMSP', key_path=key_path, cert_path=cert_path) client = Client() print(client.query_peers(admin_owkin, peer1_owkin)) print( client.query_peers(admin_owkin, peer1_owkin, channel='mychannel', local=False)) client.init_with_discovery(admin_owkin, peer1_owkin, 'mychannel')
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)
def wait(channel_name): def on_channel_event(cc_event, block_number, tx_id, tx_status): on_event(channel_name, cc_event, block_number, tx_id, tx_status) with get_event_loop() as loop: client = Client() channel = client.new_channel(channel_name) target_peer = Peer(name=settings.LEDGER_PEER_NAME) target_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 }, }) try: # can fail requestor = create_user(name=f'{settings.LEDGER_USER_NAME}_events', 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) except BaseException: pass else: # Note: # We do a loop to connect to the channel event hub because grpc may disconnect and create an exception # Since we're in a django app of backend, an exception here will not crash the server (if the "ready" # method has already returned "true"). # It makes it difficult to reconnect automatically because we need to kill the server # to trigger the connexion. # So we catch this exception (RPC error) and retry to connect to the event loop. while True: # use chaincode event channel_event_hub = channel.newChannelEventHub( target_peer, requestor) try: # We want to replay blocks from the beginning (start=0) if channel event hub was disconnected during # events emission stream = channel_event_hub.connect(start=0, filtered=False) channel_event_hub.registerChaincodeEvent( settings.LEDGER_CHANNELS[channel_name]['chaincode'] ['name'], 'chaincode-updates', onEvent=on_channel_event) logger.info( f'Connect to Channel Event Hub ({channel_name})') loop.run_until_complete(stream) except Exception as e: logger.error( f'Channel Event Hub failed for {channel_name} ({type(e)}): {e} re-connecting in 5s' ) time.sleep(5)