def generate_block_numbers(self, block_number: BlockNumber=None) -> Tuple[BlockNumber, ...]: if block_number is None and not self.is_numbered: raise TypeError( "A `block_number` must be supplied to generate block numbers " "for hash based header requests" ) elif block_number is not None and self.is_numbered: raise TypeError( "The `block_number` parameter may not be used for number based " "header requests" ) elif block_number is None: block_number = cast(BlockNumber, self.block_number_or_hash) max_headers = min(self.max_size, self.max_headers) # inline import until this module is moved to `trinity` from trinity.utils.headers import sequence_builder return sequence_builder( block_number, max_headers, self.skip, self.reverse, )
def _get_request_size(self, request: GetBlockHeadersRequest) -> Optional[int]: payload = request.command_payload['query'] if isinstance(payload['block_number_or_hash'], int): return len(sequence_builder( start_number=payload['block_number_or_hash'], max_length=payload['max_headers'], skip=payload['skip'], reverse=payload['reverse'], )) else: return None
def _generate_block_numbers(self, block_number: BlockNumber=None) -> Tuple[BlockNumber, ...]: if block_number is None and not self._is_numbered: raise TypeError( "A `block_number` must be supplied to generate block numbers " "for hash based header requests" ) elif block_number is not None and self._is_numbered: raise TypeError( "The `block_number` parameter may not be used for number based " "header requests" ) elif block_number is None: block_number = cast(BlockNumber, self.block_number_or_hash) max_headers = min(self.protocol_max_request_size, self.max_headers) return sequence_builder( block_number, max_headers, self.skip, self.reverse, )
def test_sequence(start_num, max_length, skip, reverse, expected): assert sequence_builder(start_num, max_length, skip, reverse) == expected
def test_oversize_sequence(start_num, skip, reverse): # Instead of using the specific constant, just use a rough TOO_LONG number # We don't need to worry about edge cases for this gut check with pytest.raises(OversizeObject): sequence_builder(start_num, TOO_LONG, skip, reverse)