Пример #1
0
class BtcBlockRangeService(object):
    def __init__(self, bitcoin_rpc):
        graph = BlockTimestampGraph(bitcoin_rpc)
        self._graph_operations = GraphOperations(graph)

    def get_block_range_for_date(self, date, start_hour=0, end_hour=23):
        max_datetime = datetime(date.year,
                                date.month,
                                date.day,
                                23,
                                59,
                                59,
                                tzinfo=timezone.utc)
        start_datetime = datetime.combine(
            date, time(hour=start_hour, minute=0,
                       second=0)).replace(tzinfo=timezone.utc)
        end_datetime = datetime.combine(
            date, time(hour=end_hour, minute=59,
                       second=59)).replace(tzinfo=timezone.utc)
        if end_datetime > max_datetime:
            end_datetime = max_datetime
        return self.get_block_range_for_timestamps(start_datetime.timestamp(),
                                                   end_datetime.timestamp())

    def get_block_range_for_timestamps(self, start_timestamp, end_timestamp):
        start_timestamp = int(start_timestamp)
        end_timestamp = int(end_timestamp)
        if start_timestamp > end_timestamp:
            raise ValueError(
                'start_timestamp must be greater or equal to end_timestamp')

        try:
            start_block_bounds = self._graph_operations.get_bounds_for_y_coordinate(
                start_timestamp)
        except OutOfBoundsError:
            start_block_bounds = (0, 0)

        try:
            end_block_bounds = self._graph_operations.get_bounds_for_y_coordinate(
                end_timestamp)
        except OutOfBoundsError as e:
            raise OutOfBoundsError(
                'The existing blocks do not completely cover the given time range'
            ) from e

        if start_block_bounds == end_block_bounds and start_block_bounds[
                0] != start_block_bounds[1]:
            raise ValueError(
                'The given timestamp range does not cover any blocks')

        start_block = start_block_bounds[1]
        end_block = end_block_bounds[0]

        return start_block, end_block
Пример #2
0
class BtcBlockRangeService(object):
    def __init__(self, eos_rpc):
        graph = BlockTimestampGraph(eos_rpc)
        self._graph_operations = GraphOperations(graph)

    def get_block_range_for_date(self, date):
        start_datetime = datetime.combine(
            date,
            datetime.min.time().replace(tzinfo=timezone.utc))
        end_datetime = datetime.combine(
            date,
            datetime.max.time().replace(tzinfo=timezone.utc))
        return self.get_block_range_for_timestamps(start_datetime.timestamp(),
                                                   end_datetime.timestamp())

    def get_block_range_for_timestamps(self, start_timestamp, end_timestamp):
        start_timestamp = start_timestamp
        end_timestamp = end_timestamp
        if start_timestamp > end_timestamp:
            raise ValueError(
                'start_timestamp must be greater or equal to end_timestamp')

        try:
            start_block_bounds = self._graph_operations.get_bounds_for_y_coordinate(
                start_timestamp)
        except OutOfBoundsError:
            start_block_bounds = (0, 0)

        try:
            end_block_bounds = self._graph_operations.get_bounds_for_y_coordinate(
                end_timestamp)
        except OutOfBoundsError as e:
            raise OutOfBoundsError(
                'The existing blocks do not completely cover the given time range'
            ) from e

        if start_block_bounds == end_block_bounds and start_block_bounds[
                0] != start_block_bounds[1]:
            raise ValueError(
                'The given timestamp range does not cover any blocks')

        start_block = start_block_bounds[1]
        end_block = end_block_bounds[0]

        return start_block, end_block
Пример #3
0
 def __init__(self, bitcoin_rpc):
     graph = BlockTimestampGraph(bitcoin_rpc)
     self._graph_operations = GraphOperations(graph)
def test_get_bounds_for_y_coordinate_simple(graph, y, expected_bounds):
    graph = MockGraph(graph)
    graph_operations = GraphOperations(graph)
    bounds = graph_operations.get_bounds_for_y_coordinate(y)
    assert bounds == expected_bounds
Пример #5
0
 def __init__(self, eos_rpc):
     graph = BlockTimestampGraph(eos_rpc)
     self._graph_operations = GraphOperations(graph)