def _get_stream(self): """ get the events of the channel. Return: the events in success or None in fail. """ _logger.info("create peer delivery stream") if self._signed_event is not None: return self._peer.delivery(self._signed_event, filtered=self._filtered) seek_info = self._create_seek_info(self._start, self._stop) kwargs = {} if self._peer._client_cert_path: with open(self._peer._client_cert_path, 'rb') as f: b64der = pem_to_der(f.read()) kwargs['tls_cert_hash'] = sha256(b64der).digest() tx_context = TXContext(self._requestor, self._requestor.cryptoSuite, TXProposalRequest()) seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, self._channel_name, current_timestamp(), tx_context.epoch, **kwargs) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) # this is a stream response return self._peer.delivery(envelope, filtered=self._filtered)
def get_block_between(self, tx_context, orderer, start, end): """ Args: tx_context: tx_context instance orderer: orderer instance start: id of block to start query for end: id of block to end query for Returns: block(s) """ seek_info = create_seek_info(start, end) seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, self._name, current_timestamp(), tx_context.epoch) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) response = orderer.delivery(envelope) if response[0].block is None or response[0].block == '': _logger.error("fail to get block start from %s to %s" % (str(start), str(end))) return None _logger.info("get block successfully, start from %s to %s" % (str(start), str(end))) return response[0].block
def get_genesis_block(self, tx_context, channel_name): """ get the genesis block of the channel. Return: the genesis block in success or None in fail. """ _logger.info("get genesis block - start") seek_info = create_seek_info(0, 0) seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, channel_name, current_timestamp(), tx_context.epoch) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) response = self.delivery(envelope) if response[0].block is None or response[0].block == '': _logger.error("fail to get genesis block") return None _logger.info("get genesis block successfully, block=%s", response[0].block.header) return response[0].block
def get_genesis_block(self, tx_context, channel_name): """get the genesis block of the channel :return: the genesis block in success or None in fail :rtype: Block/None """ _logger.info("get genesis block - start") seek_info = create_seek_info(0, 0) kwargs = {} if self._client_cert_path: with open(self._client_cert_path, 'rb') as f: b64der = pem_to_der(f.read()) kwargs['tls_cert_hash'] = sha256(b64der).digest() seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, channel_name, current_timestamp(), tx_context.epoch, **kwargs) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) # this is a stream response return self.delivery(envelope)
def get_events(self, tx_context, channel_name, start=None, stop=None, filtered=True, behavior='BLOCK_UNTIL_READY'): """ get the events of the channel. Return: the events in success or None in fail. """ _logger.info("get events") seek_info = create_seek_info(start, stop, behavior) kwargs = {} if self._client_cert_path: with open(self._client_cert_path, 'rb') as f: b64der = pem_to_der(f.read()) kwargs['tls_cert_hash'] = sha256(b64der).digest() seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, channel_name, current_timestamp(), tx_context.epoch, **kwargs) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) # this is a stream response return self.delivery(envelope, filtered=filtered)
def get_genesis_block(self, tx_context, channel_name): """ get the genesis block of the channel. Return: the genesis block in success or None in fail. """ _logger.info("get genesis block - start") seek_info = create_seek_info(0, 0) seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, channel_name, current_timestamp(), tx_context.epoch) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) # this is a stream response return self.delivery(envelope)
def get_genesis_block(self): """ get the genesis block of the channel. Return: the genesis block in success or None in fail. """ _logger.info("get genesis block - start") orderer = self._get_random_orderer() tx_context = self._client.tx_context seek_info = create_seek_info(0, 0) seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, self.name, current_timestamp(), tx_context.epoch) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) q = Queue(1) response = orderer.delivery(envelope) response.subscribe(on_next=lambda x: q.put(x), on_error=lambda x: q.put(x)) res, _ = q.get(timeout=5) if res.block is None or res.block == '': _logger.error("fail to get genesis block") return None _logger.info("get genesis block successfully, block=%s", res.block.header) return res.block
def _discovery(self, requestor, target, local=False, config=False, interests=None): """Send a request from a target peer to discover information about the network Args: requestor (instance): a user to make the request target (instance): target peer to send discovery request local (bool): include local endpoints in the query config (bool): include channel configuration in the query interests (list): interests about an endorsement for cc Returns: Response from Discovery Service """ auth = protocol_pb2.AuthInfo() sig = utils.create_serialized_identity(requestor) auth.client_identity = sig # TODO: add tls certificate in client and there discovery_req = protocol_pb2.Request() discovery_req.authentication.CopyFrom(auth) queries = [] if local: q = protocol_pb2.Query() queries.append(q) local_peers = protocol_pb2.LocalPeerQuery() q.local_peers.CopyFrom(local_peers) _logger.info("DISCOVERY: adding local peers query") else: # It gives us state info about the channel # in addition of LocalPeerQuery information q = protocol_pb2.Query() queries.append(q) q.channel = self._name peer_query = protocol_pb2.PeerMembershipQuery() q.peer_query.CopyFrom(peer_query) _logger.info("DISCOVERY: adding channel peers query") if config: q = protocol_pb2.Query() queries.append(q) q.channel = self._name config_query = protocol_pb2.ConfigQuery() q.config_query.CopyFrom(config_query) _logger.info("DISCOVERY: adding config query") if interests and len(interests) > 0: q = protocol_pb2.Query() queries.append(q) q.channel = self._name cc_interests = [] for interest in interests: proto_interest = self._build_proto_cc_interest(interest) cc_interests.append(proto_interest) cc_query = protocol_pb2.ChaincodeQuery() cc_query.interests.extend(cc_interests) q.cc_query.CopyFrom(cc_query) _logger.info("DISCOVERY: adding chaincodes/collection query") discovery_req.queries.extend(queries) request_bytes = discovery_req.SerializeToString() sig = requestor.cryptoSuite.sign(requestor.enrollment.private_key, request_bytes) envelope = create_envelope(sig, request_bytes) return target.send_discovery(envelope)
async def get_channel_config_with_orderer(self, tx_context, orderer): """Query the current config block for this channel Args: tx_context: tx_context instance peers: peers in the channel Returns: :class:`ChaincodeQueryResponse` channelinfo with height, currently the only useful information. """ seek_info = create_seek_info() kwargs = {} if orderer._client_cert_path: with open(orderer._client_cert_path, 'rb') as f: b64der = pem_to_der(f.read()) kwargs['tls_cert_hash'] = sha256(b64der).digest() seek_info_header = build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, self.name, current_timestamp(), tx_context.epoch, **kwargs) seek_header = build_header(tx_context.identity, seek_info_header, tx_context.nonce) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) # this is a stream response block = None stream = orderer.delivery(envelope) async for v in stream: if v.block is None or v.block == '': msg = "fail to get block" _logger.error(msg) raise Exception(msg) block = v.block break block = BlockDecoder().decode(block.SerializeToString()) last_config = block['metadata']['metadata'][common_pb2.LAST_CONFIG] # if nor first block if 'index' in last_config['value']: seek_info = create_seek_info(last_config['value']['index'], last_config['value']['index']) seek_payload_bytes = create_seek_payload(seek_header, seek_info) sig = tx_context.sign(seek_payload_bytes) envelope = create_envelope(sig, seek_payload_bytes) block = None stream = orderer.delivery(envelope) async for v in stream: if v.block is None or v.block == '': msg = "fail to get block" _logger.error(msg) raise Exception(msg) block = v.block break block = BlockDecoder().decode(block.SerializeToString()) envelope = block['data']['data'][0] payload = envelope['payload'] channel_header = payload['header']['channel_header'] if channel_header['type'] != common_pb2.CONFIG: raise Exception(f'Block must be of type "CONFIG"' f' ({common_pb2.CONFIG}), but got' f' "{channel_header["type"]}" instead') config_envelope = payload['data'] return config_envelope