def test_block_list_paginated_by_start_id(self): """Verifies block list requests work paginated by count and start_id. Queries the default mock block store with three blocks: {header: {block_num: 2 ...}, header_signature: 'B-2' ...}, {header: {block_num: 1 ...}, header_signature: 'B-1' ...}, {header: {block_num: 0 ...}, header_signature: 'B-0' ...}, Expects to find: - a status of OK - a head_id of 'B-2', the latest - a paging response with: * a next_id of 'B-0' * a previous_id of 'B-2' * a start_index of 1 * the default total resource count of 3 - a list of blocks with 1 item - that item is an instance of Block - that item has a header_signature of 'B-1' """ response = self.make_paged_request( count=1, start_id=BlockStore.block_num_to_hex(1)) self.assertEqual(self.status.OK, response.status) self.assertEqual('B-2', response.head_id) self.assert_valid_paging(response, next_id=BlockStore.block_num_to_hex(0)) self.assertEqual(1, len(response.blocks)) self.assert_all_instances(response.blocks, Block) self.assertEqual('B-1', response.blocks[0].header_signature)
def test_block_list_paginated_by_start(self): """Verifies block list requests work paginated by limit and start. Queries the default mock block store with three blocks: {header: {block_num: 2 ...}, header_signature: 'bbb...2' ...}, {header: {block_num: 1 ...}, header_signature: 'bbb...1' ...}, {header: {block_num: 0 ...}, header_signature: 'bbb...0' ...}, Expects to find: - a status of OK - a head_id of 'bbb...2', the latest - a paging response with: * a next_id of '0x0000000000000000 * a limit of 100 * start of '0x0000000000000001' - a list of blocks with 1 item - that item is an instance of Block - that item has a header_signature of 'bbb...1' """ response = self.make_paged_request( limit=1, start=BlockStore.block_num_to_hex(1)) self.assertEqual(self.status.OK, response.status) self.assertEqual(B_2, response.head_id) self.assert_valid_paging(response, '0x0000000000000001', 1, next_id=BlockStore.block_num_to_hex(0)) self.assertEqual(1, len(response.blocks)) self.assert_all_instances(response.blocks, Block) self.assertEqual(B_1, response.blocks[0].header_signature)
def test_block_list_paginated(self): """Verifies requests for block lists work when paginated just by count. Queries the default mock block store with three blocks: {header: {block_num: 2 ...}, header_signature: 'B-2' ...}, {header: {block_num: 1 ...}, header_signature: 'B-1' ...}, {header: {block_num: 0 ...}, header_signature: 'B-0' ...}, Expects to find: - a status of OK - a head_id of 'B-2', the latest - a paging response with a next_id of 'B-0' - a list of blocks with 2 items - those items are instances of Block - the first item has a header_signature of 'B-2' """ response = self.make_paged_request(count=2) self.assertEqual(self.status.OK, response.status) self.assertEqual('B-2', response.head_id) self.assertEqual(2, len(response.blocks)) self.assert_all_instances(response.blocks, Block) self.assertEqual('B-2', response.blocks[0].header_signature) self.assert_valid_paging(response, next_id=BlockStore.block_num_to_hex(0))
def test_block_list_paginated(self): """Verifies requests for block lists work when paginated just by limit. Queries the default mock block store with three blocks: {header: {block_num: 2 ...}, header_signature: B_2 ...}, {header: {block_num: 1 ...}, header_signature: 'bbb...1' ...}, {header: {block_num: 0 ...}, header_signature: 'bbb...0' ...}, Expects to find: - a status of OK - a head_id of 'bbb...2', the latest a paging response with start of B_@, limit 100 and next of B_0 - a list of blocks with 2 items - those items are instances of Block - the first item has a header_signature of 'bbb...2' """ response = self.make_paged_request(limit=2) self.assertEqual(self.status.OK, response.status) self.assertEqual(B_2, response.head_id) self.assertEqual(2, len(response.blocks)) self.assert_all_instances(response.blocks, Block) self.assertEqual(B_2, response.blocks[0].header_signature) self.assert_valid_paging(response, '0x0000000000000002', 2, next_id=BlockStore.block_num_to_hex(0))
def _respond(self, request): head_block = self._get_head_block(request) self._validate_ids(request.block_ids) blocks = None paging_response = None if request.block_ids: blocks = self._block_store.get_blocks(request.block_ids) blocks = itertools.filterfalse( lambda block: block.block_num > head_block.block_num, blocks) # realize the iterator blocks = list(map(lambda blkw: blkw.block, blocks)) paging_response = client_list_control_pb2.ClientPagingResponse() else: paging = request.paging sort_reverse = BlockListRequest.is_reverse( request.sorting, self._status.INVALID_SORT) limit = min(paging.limit, MAX_PAGE_SIZE) or DEFAULT_PAGE_SIZE iterargs = { 'reverse': not sort_reverse } if paging.start: iterargs['start_block_num'] = paging.start elif not sort_reverse: iterargs['start_block'] = head_block block_iter = None try: block_iter = self._block_store.get_block_iter(**iterargs) blocks = block_iter if sort_reverse: blocks = itertools.takewhile( lambda block: block.block_num <= head_block.block_num, blocks) blocks = itertools.islice(blocks, limit) # realize the result list, which will evaluate the underlying # iterator blocks = list(map(lambda blkw: blkw.block, blocks)) next_block = next(block_iter, None) if next_block: next_block_num = BlockStore.block_num_to_hex( next_block.block_num) else: next_block_num = None start = self._block_store.get( blocks[0].header_signature).block_num except ValueError: if paging.start: return self._status.INVALID_PAGING return self._status.NO_ROOT paging_response = client_list_control_pb2.ClientPagingResponse( next=next_block_num, limit=limit, start=BlockStore.block_num_to_hex(start) ) if not blocks: return self._wrap_response( self._status.NO_RESOURCE, head_id=head_block.identifier, paging=paging_response) return self._wrap_response( head_id=head_block.identifier, paging=paging_response, blocks=blocks)
def _respond(self, request): head_block = self._get_head_block(request) self._validate_ids(request.block_ids) blocks = None paging_response = None if request.block_ids: blocks = self._block_store.get_blocks(request.block_ids) blocks = itertools.filterfalse( lambda block: block.block_num > head_block.block_num, blocks) # realize the iterator blocks = list(map(lambda blkw: blkw.block, blocks)) paging_response = client_list_control_pb2.ClientPagingResponse() else: paging = request.paging sort_reverse = BlockListRequest.is_reverse( request.sorting, self._status.INVALID_SORT) limit = min(paging.limit, MAX_PAGE_SIZE) or DEFAULT_PAGE_SIZE iterargs = {'reverse': not sort_reverse} if paging.start: iterargs['start_block_num'] = paging.start elif not sort_reverse: iterargs['start_block'] = head_block block_iter = None try: block_iter = self._block_store.get_block_iter(**iterargs) blocks = block_iter if sort_reverse: blocks = itertools.takewhile( lambda block: block.block_num <= head_block.block_num, blocks) blocks = itertools.islice(blocks, limit) # realize the result list, which will evaluate the underlying # iterator blocks = list(map(lambda blkw: blkw.block, blocks)) next_block = next(block_iter, None) if next_block: next_block_num = BlockStore.block_num_to_hex( next_block.block_num) else: next_block_num = None start = self._block_store.get( blocks[0].header_signature).block_num except ValueError: if paging.start: return self._status.INVALID_PAGING return self._status.NO_ROOT paging_response = client_list_control_pb2.ClientPagingResponse( next=next_block_num, limit=limit, start=BlockStore.block_num_to_hex(start)) if not blocks: return self._wrap_response(self._status.NO_RESOURCE, head_id=head_block.identifier, paging=paging_response) return self._wrap_response(head_id=head_block.identifier, paging=paging_response, blocks=blocks)