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_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_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)
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