Skip to content

Commit 29e71b6

Browse files
committed
address some comments
- catch more requests errors - add request information to logs for connection errors - made `get_tx_hashes_by_block` return None in case of connection error - add logger to base test class, removed logger from sub classes - add debug info for checked hashes
1 parent 2e9ccab commit 29e71b6

7 files changed

Lines changed: 31 additions & 26 deletions

src/apis/orderbookapi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def get_solver_competition_data(self, tx_hash: str) -> Optional[dict[str, Any]]:
5151
solver_competition_data = json.loads(barn_competition_data.text)
5252
else:
5353
return None
54-
except requests.exceptions.ConnectionError as err:
54+
except requests.RequestException as err:
5555
self.logger.warning(
56-
f"Connection error while fetching competition data: {err}"
56+
f"Connection error while fetching competition data. Hash: {tx_hash}, error: {err}"
5757
)
5858
return None
5959
return solver_competition_data
@@ -97,8 +97,10 @@ def get_quote(self, trade: Trade) -> Optional[Trade]:
9797
json=request_dict,
9898
timeout=REQUEST_TIMEOUT,
9999
)
100-
except ValueError as err:
101-
self.logger.warning(f"Fee quote failed with error {err}")
100+
except requests.RequestException as err:
101+
self.logger.warning(
102+
f"Fee quote failed. Request: {request_dict}, error: {err}"
103+
)
102104
return None
103105

104106
if quote_response.status_code != SUCCESS_CODE:

src/apis/web3api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def get_current_block_number(self) -> Optional[int]:
4242
self.logger.warning(f"Error while fetching block number: {err}")
4343
return None
4444

45-
def get_tx_hashes_by_block(self, start_block: int, end_block: int) -> list[str]:
45+
def get_tx_hashes_by_block(
46+
self, start_block: int, end_block: int
47+
) -> Optional[list[str]]:
4648
"""
4749
Function filters hashes by contract address, and block ranges
4850
"""
@@ -60,7 +62,7 @@ def get_tx_hashes_by_block(self, start_block: int, end_block: int) -> list[str]:
6062
log_receipts = self.web_3.eth.filter(filter_criteria).get_all_entries() # type: ignore
6163
except ValueError as err:
6264
self.logger.warning(f"ValueError while fetching hashes: {err}")
63-
log_receipts = []
65+
return None
6466

6567
settlement_hashes_list = list(
6668
{log_receipt["transactionHash"].hex() for log_receipt in log_receipts}

src/daemon.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# pylint: disable=logging-fstring-interpolation
88

99
import time
10+
from typing import Optional
1011
from src.apis.web3api import Web3API
1112
from src.monitoring_tests.solver_competition_surplus_test import (
1213
SolverCompetitionSurplusTest,
@@ -33,24 +34,28 @@ def main() -> None:
3334
PartialFillCostCoverageTest(),
3435
]
3536

36-
start_block = web3_api.get_current_block_number()
37-
if start_block is None:
38-
return
37+
start_block: Optional[int] = None
3938

4039
web3_api.logger.debug("Start infinite loop")
4140
while True:
4241
time.sleep(SLEEP_TIME_IN_SEC)
42+
if start_block is None:
43+
start_block = web3_api.get_current_block_number()
44+
continue
4345
end_block = web3_api.get_current_block_number()
4446
if end_block is None:
4547
continue
48+
4649
tx_hashes = web3_api.get_tx_hashes_by_block(start_block, end_block)
50+
if tx_hashes is None:
51+
continue
4752

4853
web3_api.logger.debug(f"{len(tx_hashes)} hashes found: {tx_hashes}")
4954
for test in tests:
5055
test.add_hashes_to_queue(tx_hashes)
5156
web3_api.logger.debug(f"Running test ({test}) for hashes {test.tx_hashes}.")
5257
test.run_queue()
53-
web3_api.logger.debug("Test completed.")
58+
web3_api.logger.debug(f"Test ({test}) completed.")
5459

5560
start_block = end_block + 1
5661

src/monitoring_tests/base_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
In this file, we introduce the BaseTest class, whose purpose is to be used as the basis
33
for all tests developed.
44
"""
5+
# pylint: disable=logging-fstring-interpolation
6+
57
from abc import ABC, abstractmethod
8+
from src.helper_functions import get_logger
69

710

811
class BaseTest(ABC):
@@ -14,6 +17,7 @@ class BaseTest(ABC):
1417

1518
def __init__(self) -> None:
1619
self.tx_hashes: list[str] = []
20+
self.logger = get_logger()
1721

1822
@abstractmethod
1923
def run(self, tx_hash: str) -> bool:
@@ -32,6 +36,13 @@ def run_queue(self) -> None:
3236
success = self.run(tx_hash)
3337
if not success:
3438
tx_hashes_fails.append(tx_hash)
39+
tx_hashes_success = [
40+
tx_hash for tx_hash in self.tx_hashes if tx_hash not in tx_hashes_fails
41+
]
42+
self.logger.debug(
43+
f"Test ran successefully for hashes {tx_hashes_success} and"
44+
f"needs to be rerun for hashes {tx_hashes_fails}."
45+
)
3546
self.tx_hashes = tx_hashes_fails
3647

3748
def add_hashes_to_queue(self, tx_hashes: list[str]) -> None:
@@ -40,9 +51,9 @@ def add_hashes_to_queue(self, tx_hashes: list[str]) -> None:
4051
"""
4152
self.tx_hashes += tx_hashes
4253

43-
@abstractmethod
4454
def alert(self, msg: str) -> None:
4555
"""
4656
This function is called to create an alert for a failed test.
4757
It must be implemented by all subclasses.
4858
"""
59+
self.logger.error(msg)

src/monitoring_tests/partially_fillable_cost_coverage_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from src.monitoring_tests.base_test import BaseTest
88
from src.apis.web3api import Web3API
99
from src.apis.orderbookapi import OrderbookAPI
10-
from src.helper_functions import get_logger
1110
from src.models import find_partially_fillable
1211
from src.constants import (
1312
COST_COVERAGE_ABSOLUTE_DEVIATION_ETH,
@@ -24,7 +23,6 @@ def __init__(self):
2423
super().__init__()
2524
self.web3_api = Web3API()
2625
self.orderbook_api = OrderbookAPI()
27-
self.logger = get_logger()
2826

2927
def run(self, tx_hash) -> bool:
3028
"""
@@ -56,9 +54,6 @@ def run(self, tx_hash) -> bool:
5654

5755
return True
5856

59-
def alert(self, msg: str):
60-
self.logger.error(msg)
61-
6257
def run_cost_coverage_test(self, transaction: TxData, receipt: TxReceipt) -> bool:
6358
"""
6459
Test if the cost of a batch are close to the fees collected in that batch.

src/monitoring_tests/partially_fillable_fee_quote_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from src.monitoring_tests.base_test import BaseTest
99
from src.apis.web3api import Web3API
1010
from src.apis.orderbookapi import OrderbookAPI
11-
from src.helper_functions import get_logger
1211
from src.models import Trade, find_partially_fillable
1312
from src.constants import (
1413
FEE_ABSOLUTE_DEVIATION_ETH_FLAG,
@@ -25,7 +24,6 @@ def __init__(self):
2524
super().__init__()
2625
self.web3_api = Web3API()
2726
self.orderbook_api = OrderbookAPI()
28-
self.logger = get_logger()
2927

3028
def run(self, tx_hash) -> bool:
3129
"""
@@ -55,9 +53,6 @@ def run(self, tx_hash) -> bool:
5553

5654
return True
5755

58-
def alert(self, msg: str):
59-
self.logger.error(msg)
60-
6156
def run_quote_test(self, trade: Trade, transaction: TxData) -> bool:
6257
"""
6358
Check if the fee proposed by a solver is close to the fee proposed by our quoting system.

src/monitoring_tests/solver_competition_surplus_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from src.monitoring_tests.base_test import BaseTest
99
from src.apis.web3api import Web3API
1010
from src.apis.orderbookapi import OrderbookAPI
11-
from src.helper_functions import get_logger
1211
from src.models import Trade
1312
from src.constants import ABSOLUTE_ETH_FLAG_AMOUNT, REL_DEVIATION_FLAG_PERCENT
1413

@@ -23,7 +22,6 @@ def __init__(self):
2322
super().__init__()
2423
self.web3_api = Web3API()
2524
self.orderbook_api = OrderbookAPI()
26-
self.logger = get_logger()
2725

2826
def compare_orders_surplus(self, competition_data: dict[str, Any]) -> bool:
2927
"""
@@ -129,6 +127,3 @@ def run(self, tx_hash) -> bool:
129127
success = self.compare_orders_surplus(solver_competition_data)
130128

131129
return success
132-
133-
def alert(self, msg: str) -> None:
134-
self.logger.error(msg)

0 commit comments

Comments
 (0)