# Buggy code
def initialize(self, metrics_prefix: str, registry: CollectorRegistry) -> None:
    super().initialize(metrics_prefix=metrics_prefix, registry=registry)

    missing_commitments = self.contract_agent.get_missing_commitments(
        checksum_address=self.staker_address
    )
    if missing_commitments == 0:
        # has either already committed to this period or the next period

        # use local event filter for initial data
        last_committed_period = self.contract_agent.get_last_committed_period(
            staker_address=self.staker_address
        )
        arg_filters = {"staker": self.staker_address, "period": last_committed_period}
        latest_block = self.contract_agent.blockchain.client.block_number
        previous_period = self.contract_agent.get_current_period() - 1  # just in case
        # we estimate the block number for the previous period to start search from since either
        # - commitment made during previous period for current period, OR
        # - commitment made during current period for next period
        block_number_for_previous_period = estimate_block_number_for_period(
            period=previous_period,
            seconds_per_period=self.contract_agent.staking_parameters()[0],
            latest_block=latest_block,
        )

        events_throttler = ContractEventsThrottler(
            agent=self.contract_agent,
            event_name=self.event_name,
            from_block=block_number_for_previous_period,
            to_block=latest_block,
            **arg_filters,
        )
        for event_record in events_throttler:
            self._event_occurred(event_record.raw_event)


# 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 initialize(self, metrics_prefix: str, registry: CollectorRegistry) -> None:
    super().initialize(metrics_prefix=metrics_prefix, registry=registry)

    missing_commitments = self.contract_agent.get_missing_commitments(
        checksum_address=self.staker_address
    )
    if missing_commitments == 0:
        # has either already committed to this period or the next period

        # use local event filter for initial data
        last_committed_period = self.contract_agent.get_last_committed_period(
            staker_address=self.staker_address
        )
        arg_filters = {"staker": self.staker_address, "period": last_committed_period}
        latest_block = self.contract_agent.blockchain.client.block_number
        previous_period = self.contract_agent.get_current_period() - 1  # just in case
        # we estimate the block number for the previous period to start search from since either
        # - commitment made during previous period for current period, OR
        # - commitment made during current period for next period
        block_number_for_previous_period = estimate_block_number_for_period(
            period=previous_period,
            seconds_per_period=self.contract_agent.staking_parameters()[0],
            latest_block=latest_block,
        )

        events_throttler = ContractEventsThrottler(
            agent=self.contract_agent,
            event_name=self.event_name,
            from_block=block_number_for_previous_period,
            to_block=latest_block,
            **arg_filters,
        )
        for event_record in events_throttler:
            self._event_occurred(event_record.raw_event)

        # update last block checked since we just looked for this event up to and including latest block
        # block range is inclusive, hence the increment
        self.filter_current_from_block = latest_block + 1