# Buggy code
def __iter__(self):
    current_from_block = self.from_block
    current_to_block = min(self.from_block + self.max_blocks_per_call, self.to_block)
    while current_from_block < current_to_block:
        for event_record in self.event_filter(
            from_block=current_from_block,
            to_block=current_to_block,
            **self.argument_filters,
        ):
            yield event_record
        current_from_block = current_to_block
        # update the 'to block' to the lesser of either the next `max_blocks_per_call` blocks,
        # or the remainder of blocks
        current_to_block = min(
            current_from_block + self.max_blocks_per_call, self.to_block
        )


# Traceback (builtins.ValueError)
2020-09-23T14:29:04+0000 [twisted.internet.defer#critical] Unhandled error in Deferred:
2020-09-23T14:29:04+0000 [twisted.internet.defer#critical]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 1283, in run
self.mainLoop()
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 1292, in mainLoop
self.runUntilCurrent()
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 913, in runUntilCurrent
call.func(*call.args, call.kw)
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/task.py", line 239, in call
d = defer.maybeDeferred(self.f, *self.a, self.kw)
--- <exception caught here> ---
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "/usr/local/lib/python3.6/dist-packages/nucypher/utilities/prometheus/metrics.py", line 147, in collect_prometheus_metrics
collector.collect()
File "/usr/local/lib/python3.6/dist-packages/nucypher/utilities/prometheus/collector.py", line 77, in collect
self._collect_internal()
File "/usr/local/lib/python3.6/dist-packages/nucypher/utilities/prometheus/collector.py", line 303, in _collect_internal
events = self.event_filter.get_new_entries()
File "/usr/local/lib/python3.6/dist-packages/web3/_utils/filters.py", line 159, in get_new_entries
log_entries = self._filter_valid_entries(self.web3.eth.getFilterChanges(self.filter_id))
File "/usr/local/lib/python3.6/dist-packages/web3/eth.py", line 493, in getFilterChanges
RPC.eth_getFilterChanges, [filter_id],
File "/usr/local/lib/python3.6/dist-packages/web3/manager.py", line 153, in request_blocking
raise ValueError(response["error"])
builtins.ValueError: {'code': -32000, 'message': 'filter not found'}

# Fixed code
def __iter__(self):
    current_from_block = self.from_block
    current_to_block = min(self.from_block + self.max_blocks_per_call, self.to_block)
    while current_from_block <= current_to_block:
        for event_record in self.event_filter(
            from_block=current_from_block,
            to_block=current_to_block,
            **self.argument_filters,
        ):
            yield event_record
        # previous block range is inclusive hence the increment
        current_from_block = current_to_block + 1
        # update the 'to block' to the lesser of either the next `max_blocks_per_call` blocks,
        # or the remainder of blocks
        current_to_block = min(
            current_from_block + self.max_blocks_per_call, self.to_block
        )