Skip to content

Commit

Permalink
Made some adjustments for api_cons_miss and fixed bugs to actually ad…
Browse files Browse the repository at this point in the history
…d the necesarry column in already existing db-s,
  • Loading branch information
Kamikaza731 committed Dec 6, 2024
1 parent c7f6cb0 commit e0f155f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ The binary will be created in the build directory. You can from there run the bi
Download the binary:
```bash
wget https://github.com/Cogwheel-Validator/tnom/releases/latest/download/tnom
wget https://github.com/Cogwheel-Validator/tnom/releases/download/v0.3.0/tnom_debian_X86_64
# fix this later when you add the binary
chmod +x tnom
./tnom --working-dir /path/to/working/dir --config-path /path/to/config.yml --alert-path /path/to/alert.yml
Expand Down
9 changes: 6 additions & 3 deletions tnom/check_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ async def check_apis(load_config: dict[str, Any]) -> list[str]:
loaded_apis, responses) if isinstance(response, Exception)]

if not online_apis_with_data:
logging.warning("No healthy APIs found")
logging.info("Unhealthy APIs: %s", unhealthy_apis)
return []

max_block_height = max(api_data[0] for _, api_data in online_apis_with_data)

healthy_apis = [api for api, (block_height, _) in online_apis_with_data
if max_block_height - block_height <= MAX_BLOCK_HEIGHT_DIFF]
logging.info("""Healthy APIs: %s\n Unhealthy APIs: %s\n""",
healthy_apis, unhealthy_apis)
if max_block_height - block_height <= MAX_BLOCK_HEIGHT_DIFF]

logging.info("Healthy APIs: %s\nUnhealthy APIs: %s",
healthy_apis, unhealthy_apis)
return healthy_apis
4 changes: 2 additions & 2 deletions tnom/database_handler/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def create_database(path: Path) -> None:
price_feed_addr_balance INTEGER,
small_balance_alert_executed INTEGER,
very_small_balance_alert_executed INTEGER,
consecutive_misses INTEGER DEFAULT 0
api_cons_miss INTEGER DEFAULT 0,
consecutive_misses INTEGER DEFAULT 0,
api_cons_miss INTEGER DEFAULT 0
)""",
)

Expand Down
8 changes: 6 additions & 2 deletions tnom/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ async def main() -> None:
# Initialize and check the database
try:
init_and_check_db(working_dir)
database_handler.check_and_update_database_schema(database_path)
except Exception as e:
logging.exception("Failed to initialize database: %s", e) # noqa: TRY401
sys.exit(1)
Expand Down Expand Up @@ -446,15 +447,15 @@ async def monitoring_loop() -> None:
try:
# Step three - check APIs
healthy_apis = await check_apis(config_yml)
while healthy_apis is None:
while not healthy_apis:
logging.error("Failed to check APIs")
latest_epoch = database_handler.read_last_recorded_epoch(database_path)
await monitoring_system.process_api_not_working(
latest_epoch, no_healthy_apis=True)
# stop the script here and start from while True again until there
# is a healthy api
await asyncio.sleep(config_yml.get("monitoring_interval", 60))
continue
healthy_apis = await check_apis(config_yml)

# Step four - Make query with random healthy API
query_results = await query_rand_api.collect_data_from_random_healthy_api( # noqa: E501
Expand Down Expand Up @@ -489,6 +490,7 @@ async def monitoring_loop() -> None:
"miss_counter_p2_executed"]
db_miss_counter_p3_executed : int = read_crw_data[
"miss_counter_p3_executed"]
db_api_cons_miss : int = read_crw_data["api_cons_miss"]
# if the check failed the return should be false adding +1 to not
# signing events
if query_data["check_for_aggregate_votes"] is False:
Expand All @@ -505,6 +507,7 @@ async def monitoring_loop() -> None:
"small_balance_alert_executed": db_small_bal_alert,
"very_small_balance_alert_executed": db_very_small_bal_alert,
"consecutive_misses": db_consecutive_misses,
"api_cons_miss": db_api_cons_miss,
}
database_handler.write_epoch_data(database_path, insert_data)
elif database_handler.check_if_epoch_is_recorded(
Expand Down Expand Up @@ -544,6 +547,7 @@ async def monitoring_loop() -> None:
"small_balance_alert_executed": prev_small_bal_alert,
"very_small_balance_alert_executed": prev_very_small_bal_alert,
"consecutive_misses": prev_consecutive_misses,
"api_cons_miss": 0,
}
database_handler.write_epoch_data(database_path, insert_data)
# Process alerts
Expand Down
6 changes: 3 additions & 3 deletions tnom/query_rand_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ async def collect_data_from_random_healthy_api(
Returns:
dict[str, Any]: A dictionary containing all the collected data. Returns
None if no healthy APIs are found.
False if no healthy APIs are found.
"""
if not healthy_apis:
logging.error("""No healthy APIs found! \n
Check your config file or is the chain halted.""")
# retrun None or an empty list
return None
# retrun False or an empty list
return False
async with aiohttp.ClientSession() as session:
# select API
random_healthy_api = (random.choice(healthy_apis)) # noqa: S311
Expand Down

0 comments on commit e0f155f

Please sign in to comment.