Repository: tellor-io/telliot-feeds (language: python)

Issue:
Error Out with Flashbots "Internal Server Error" Message
This error was encountered reporting on mainnet through the Flashbots relay. 

Start Command: `telliot report -a mainnet -sa flashbots -qt eth-usd-spot -mnb 0.1 -gps average`

Versions:
`telliot-core.git@cc6f165b0724bc70c6f5d96948bfb706e2046d1a`
`telliot-feeds.git@91513444fbbcde6729d59b7e3c8ed1f0eeb38ce8`

log:
```
ERROR   | telliot_core | Exception occurred in telliot-core app
ERROR   | telliot_core | <class 'requests.exceptions.HTTPError'>
ERROR   | telliot_core | 500 Server Error: Internal Server Error for url: https://relay.flashbots.net/
ERROR   | telliot_core | ['  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/cli/commands/report.py", line 496, in report\n    await reporter.report()\n', '  File "/home/ubuntu/telliot-feeds/
src/telliot_feeds/reporters/interval.py", line 464, in report\n    _, _ = await self.report_once()\n', '  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/reporters/flashbot.py", line 156, in 
report_once\n    result = self.endpoint._web3.flashbots.send_bundle(bundle, target_block_number=block + 1)\n', '  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/module.py", line 57, in
 caller\n    result = w3.manager.request_blocking(method_str,\n', '  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/manager.py", line 197, in request_blocking\n    response = self._mak
e_request(method, params)\n', '  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/manager.py", line 150, in _make_request\n    return request_func(method, params)\n', '  File "/home/ubun
tu/telliot-feeds/src/telliot_feeds/flashbots/middleware.py", line 41, in middleware\n    return flashbots_provider.make_request(method, params)\n', '  File "/home/ubuntu/telliot-feeds/src/tellio
t_feeds/flashbots/provider.py", line 57, in make_request\n    raw_response = make_post_request(self.endpoint_uri, request_data, headers=headers)\n', '  File "/home/ubuntu/tenv/lib/python3.9/site
-packages/web3/_utils/request.py", line 49, in make_post_request\n    response.raise_for_status()\n', '  File "/home/ubuntu/tenv/lib/python3.9/site-packages/requests/models.py", line 1021, in ra
ise_for_status\n    raise HTTPError(http_error_msg, response=self)\n']
Traceback (most recent call last):
  File "/home/ubuntu/tenv/bin/telliot", line 8, in <module>
    sys.exit(main())
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/ubuntu/telliot-core/src/telliot_core/cli/utils.py", line 18, in wrapper
    return asyncio.run(f(*args, **kwargs))
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/cli/commands/report.py", line 496, in report
    await reporter.report()
  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/reporters/interval.py", line 464, in report
    _, _ = await self.report_once()
  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/reporters/flashbot.py", line 156, in report_once
    result = self.endpoint._web3.flashbots.send_bundle(bundle, target_block_number=block + 1)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/manager.py", line 197, in request_blocking
    response = self._make_request(method, params)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/manager.py", line 150, in _make_request
    return request_func(method, params)
  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/flashbots/middleware.py", line 41, in middleware
    return flashbots_provider.make_request(method, params)
  File "/home/ubuntu/telliot-feeds/src/telliot_feeds/flashbots/provider.py", line 57, in make_request
    raw_response = make_post_request(self.endpoint_uri, request_data, headers=headers)
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/web3/_utils/request.py", line 49, in make_post_request
    response.raise_for_status()   
  File "/home/ubuntu/tenv/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://relay.flashbots.net/
```




Discussion / hints:

Fix patch:
diff --git a/src/telliot_feeds/reporters/flashbot.py b/src/telliot_feeds/reporters/flashbot.py
index 36e466bb..3858ddb3 100644
--- a/src/telliot_feeds/reporters/flashbot.py
+++ b/src/telliot_feeds/reporters/flashbot.py
@@ -10,6 +10,7 @@
 from eth_account.account import Account
 from eth_account.signers.local import LocalAccount
 from eth_utils import to_checksum_address
+from requests.exceptions import HTTPError
 from telliot_core.utils.response import error_status
 from telliot_core.utils.response import ResponseStatus
 from web3 import Web3
@@ -179,7 +180,11 @@ async def report_once(
         #         )
         #     )
         # result = results[-1]
-        result = self.endpoint._web3.flashbots.send_bundle(bundle, target_block_number=block + 1)
+        try:
+            result = self.endpoint._web3.flashbots.send_bundle(bundle, target_block_number=block + 1)
+        except HTTPError as e:
+            msg = "Unable to send bundle to miners due to HTTP error"
+            return None, error_status(note=msg, e=e, log=logger.error)
         logger.info(f"Bundle sent to miners in block {block}")
 
         # Wait for transaction confirmation


Test patch:
diff --git a/tests/reporters/test_flashbot_reporter.py b/tests/reporters/test_flashbot_reporter.py
new file mode 100644
index 00000000..696cdeac
--- /dev/null
+++ b/tests/reporters/test_flashbot_reporter.py
@@ -0,0 +1,33 @@
+from unittest import mock
+
+import pytest
+import requests
+
+from telliot_feeds.feeds.eth_usd_feed import eth_usd_median_feed
+from telliot_feeds.reporters.flashbot import FlashbotsReporter
+
+
+@pytest.mark.asyncio
+async def test_http_error(tellor_360):
+    """Test FlashbotsReporter HTTPError"""
+    contracts, account = tellor_360
+    account.unlock("")
+
+    r = FlashbotsReporter(
+        oracle=contracts.oracle,
+        token=contracts.token,
+        autopay=contracts.autopay,
+        endpoint=contracts.oracle.node,
+        account=account,
+        signature_account=account,
+        chain_id=5,
+        transaction_type=0,
+        min_native_token_balance=0,
+        datafeed=eth_usd_median_feed,
+        check_rewards=False,
+    )
+
+    with mock.patch("telliot_feeds.flashbots.provider.make_post_request", side_effect=requests.exceptions.HTTPError):
+        res, status = await r.report_once()
+        assert res is None
+        assert not status.ok