# Buggy code
def __init__(
    self,
    agent: "EthereumContractAgent",
    event_name: str,
    from_block: int,
    to_block: int = None,  # defaults to latest block
    max_blocks_per_call: int = DEFAULT_MAX_BLOCKS_PER_CALL,
    **argument_filters,
):
    if not agent:
        raise ValueError(f"Contract agent must be provided")
    if not event_name:
        raise ValueError(f"Event name must be provided")

    self.event_filter = agent.events[event_name]
    self.from_block = from_block
    self.to_block = to_block if to_block else agent.blockchain.client.block_number
    # validity check of block range
    if to_block <= from_block:
        raise ValueError(f"Invalid block range provided ({from_block} - {to_block})")

    self.max_blocks_per_call = max_blocks_per_call
    self.argument_filters = argument_filters


# 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 __init__(
    self,
    agent: "EthereumContractAgent",
    event_name: str,
    from_block: int,
    to_block: int = None,  # defaults to latest block
    max_blocks_per_call: int = DEFAULT_MAX_BLOCKS_PER_CALL,
    **argument_filters,
):
    self.event_filter = agent.events[event_name]
    self.from_block = from_block
    self.to_block = (
        to_block if to_block is not None else agent.blockchain.client.block_number
    )
    # validity check of block range
    if self.to_block < self.from_block:
        raise ValueError(
            f"Invalid events block range: to_block {self.to_block} must be greater than or equal "
            f"to from_block {self.from_block}"
        )

    self.max_blocks_per_call = max_blocks_per_call
    self.argument_filters = argument_filters