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_stream(self, 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._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=filtered)
def get_events(self, tx_context, channel_name, start=None, stop=None, filtered=False, 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) response = self.delivery(envelope, filtered=filtered) if (len(response) > 0 and (response[0].block is None or response[0].block == '')): _logger.error("fail to get event blocks") return None _logger.info("get %s event blocks successfully" % len(response)) return response
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
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