예제 #1
0
class IotexBlockRangeService(object):
    def __init__(self, iotex_rpc):
        graph = BlockTimestampGraph(iotex_rpc)
        self._graph_operations = GraphOperations(graph,
                                                 max_not_monotonic_points=5,
                                                 prefetch_size=0)

    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 = (1, 1)

        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
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
예제 #3
0
 def __init__(self, iotex_rpc):
     graph = BlockTimestampGraph(iotex_rpc)
     self._graph_operations = GraphOperations(graph,
                                              max_not_monotonic_points=5,
                                              prefetch_size=0)
예제 #4
0
 def __init__(self, block_timstamp_graph):
     self._graph_operations = GraphOperations(block_timstamp_graph,
                                              max_not_monotonic_points=5,
                                              prefetch_size=0)