async def list_blocks(self, request): """Fetches list of blocks from validator, optionally filtered by id. Request: query: - head: The id of the block to use as the head of the chain - id: Comma separated list of block ids to include in results Response: data: JSON array of fully expanded Block objects head: The head used for this query (most recent if unspecified) link: The link to this exact query, including head block paging: Paging info and nav, like total resources and a next link """ paging_controls = self._get_paging_controls(request) validator_query = client_block_pb2.ClientBlockListRequest( head_id=self._get_head_id(request), block_ids=self._get_filter_ids(request), sorting=self._get_sorting_message(request, "block_num"), paging=self._make_paging_message(paging_controls)) response = await self._query_validator( Message.CLIENT_BLOCK_LIST_REQUEST, client_block_pb2.ClientBlockListResponse, validator_query) return self._wrap_paginated_response( request=request, response=response, controls=paging_controls, data=[self._expand_block(b) for b in response['blocks']])
async def _get_latest_block_id(self): resp = await self._connection.send( Message.CLIENT_BLOCK_LIST_REQUEST, client_block_pb2.ClientBlockListRequest( paging=client_list_control_pb2.ClientPagingControls( limit=1)).SerializeToString()) block_list_resp = client_block_pb2.ClientBlockListResponse() block_list_resp.ParseFromString(resp.content) if block_list_resp.status != \ client_block_pb2.ClientBlockListResponse.OK: LOGGER.error('Unable to fetch latest block id') return block_list_resp.head_id
async def _head_to_root(self, block_id): error_traps = [error_handlers.BlockNotFoundTrap] if block_id: response = await self._query_validator( Message.CLIENT_BLOCK_GET_BY_ID_REQUEST, client_block_pb2.ClientBlockGetResponse, client_block_pb2.ClientBlockGetByIdRequest(block_id=block_id), error_traps) block = self._expand_block(response['block']) else: response = await self._query_validator( Message.CLIENT_BLOCK_LIST_REQUEST, client_block_pb2.ClientBlockListResponse, client_block_pb2.ClientBlockListRequest( paging=client_list_control_pb2.ClientPagingControls( limit=1)), error_traps) block = self._expand_block(response['blocks'][0]) return ( block['header_signature'], block['header']['state_root_hash'], )