Skip to content

Commit

Permalink
fix: updated data retrieval from challenge_details db
Browse files Browse the repository at this point in the history
  • Loading branch information
nautilus committed Sep 26, 2024
1 parent e735414 commit f7a66b6
Showing 3 changed files with 71 additions and 71 deletions.
10 changes: 5 additions & 5 deletions neurons/Validator/calculate_pow_score.py
Original file line number Diff line number Diff line change
@@ -53,11 +53,11 @@ def calc_score(response, hotkey, allocated_hotkeys, penalized_hotkeys, validator
:return:
"""
try:
challenge_attempts = prevent_none(response["challenge_attempts"])
challenge_successes = prevent_none(response["challenge_successes"])
last_20_challenge_failed = prevent_none(response["last_20_challenge_failed"])
challenge_elapsed_time_avg = prevent_none(response["challenge_elapsed_time_avg"])
challenge_difficulty_avg = prevent_none(response["last_20_difficulty_avg"])
challenge_attempts = prevent_none(response.get("challenge_attempts",1))
challenge_successes = prevent_none(response.get("challenge_successes",0))
last_20_challenge_failed = prevent_none(response.get("last_20_challenge_failed",0))
challenge_elapsed_time_avg = prevent_none(response.get("challenge_elapsed_time_avg", compute.pow_timeout))
challenge_difficulty_avg = prevent_none(response.get("last_20_difficulty_avg", compute.pow_min_difficulty))
has_docker = response.get("has_docker", False)

# Define base weights for the PoW
100 changes: 51 additions & 49 deletions neurons/Validator/database/challenge.py
Original file line number Diff line number Diff line change
@@ -40,56 +40,58 @@ def select_challenge_stats(db: ComputeDb) -> dict:

cursor.execute(
"""
WITH RankedChallenges AS (SELECT uid,
ss58_address,
success,
created_at,
ROW_NUMBER() OVER (PARTITION BY uid, ss58_address ORDER BY created_at DESC) AS row_num
FROM challenge_details)
SELECT main_query.uid,
main_query.ss58_address,
main_query.challenge_attempts,
main_query.challenge_successes,
main_query.challenge_elapsed_time_avg,
main_query.challenge_difficulty_avg,
COALESCE(main_query.challenge_failed, 0) as challenge_failed,
COALESCE(last_20_challenge_failed.last_20_challenge_failed, 0) as last_20_challenge_failed,
last_20_query.last_20_difficulty_avg
FROM (SELECT uid,
ss58_address,
COUNT(*) AS challenge_attempts,
COUNT(CASE WHEN success = 0 THEN 1 END) AS challenge_failed,
SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) AS challenge_successes,
AVG(CASE WHEN success = 1 THEN elapsed_time END) AS challenge_elapsed_time_avg,
AVG(CASE WHEN success = 1 THEN difficulty END) AS challenge_difficulty_avg
FROM (SELECT *
WITH RankedChallenges AS (
SELECT uid,
ss58_address,
success,
elapsed_time,
difficulty,
created_at,
ROW_NUMBER() OVER (PARTITION BY uid ORDER BY created_at DESC) AS row_num
FROM challenge_details
ORDER BY created_at DESC
LIMIT 50) AS latest_entries
GROUP BY uid, ss58_address) AS main_query
LEFT JOIN (SELECT uid,
ss58_address,
COUNT(*) AS last_20_challenge_failed
FROM (SELECT uid, ss58_address, success
FROM RankedChallenges
WHERE row_num <= 20
ORDER BY created_at DESC) AS Last20Rows
WHERE success = 0
GROUP BY uid, ss58_address) AS last_20_challenge_failed
),
FilteredChallenges AS (
SELECT *
FROM RankedChallenges
WHERE row_num <= 60
)
SELECT main_query.uid,
main_query.ss58_address,
COUNT(*) AS challenge_attempts,
COUNT(CASE WHEN success = 0 THEN 1 END) AS challenge_failed,
SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) AS challenge_successes,
AVG(CASE WHEN success = 1 THEN elapsed_time END) AS challenge_elapsed_time_avg,
AVG(CASE WHEN success = 1 THEN difficulty END) AS challenge_difficulty_avg,
COALESCE(last_20_challenge_failed.last_20_challenge_failed, 0) as last_20_challenge_failed,
last_20_query.last_20_difficulty_avg
FROM (SELECT *
FROM FilteredChallenges
ORDER BY created_at DESC) AS main_query
LEFT JOIN (SELECT uid,
ss58_address,
COUNT(*) AS last_20_challenge_failed
FROM (SELECT uid,
ss58_address,
success,
ROW_NUMBER() OVER (PARTITION BY uid ORDER BY created_at DESC) AS row_num
FROM challenge_details)
WHERE row_num <= 20 AND success = 0
GROUP BY uid, ss58_address) AS last_20_challenge_failed
ON main_query.uid = last_20_challenge_failed.uid AND
main_query.ss58_address = last_20_challenge_failed.ss58_address
LEFT JOIN (SELECT uid,
ss58_address,
AVG(difficulty) AS last_20_difficulty_avg
FROM (SELECT uid,
ss58_address,
difficulty,
ROW_NUMBER() OVER (PARTITION BY uid, ss58_address ORDER BY created_at DESC) AS row_num
FROM challenge_details
WHERE success = 1) AS subquery
WHERE row_num <= 20
GROUP BY uid, ss58_address) AS last_20_query
ON main_query.uid = last_20_query.uid AND main_query.ss58_address = last_20_query.ss58_address;
LEFT JOIN (SELECT uid,
ss58_address,
AVG(difficulty) AS last_20_difficulty_avg
FROM (SELECT uid,
ss58_address,
difficulty,
ROW_NUMBER() OVER (PARTITION BY uid ORDER BY created_at DESC) AS row_num
FROM challenge_details
WHERE success = 1)
WHERE row_num <= 20
GROUP BY uid, ss58_address) AS last_20_query
ON main_query.uid = last_20_query.uid AND main_query.ss58_address = last_20_query.ss58_address
GROUP BY main_query.uid, main_query.ss58_address;
"""
)

@@ -101,18 +103,18 @@ def select_challenge_stats(db: ComputeDb) -> dict:
uid,
ss58_address,
challenge_attempts,
challenge_failed,
challenge_successes,
challenge_elapsed_time_avg,
challenge_difficulty_avg,
challenge_failed,
last_20_challenge_failed,
last_20_difficulty_avg,
) = result
stats[uid] = {
"ss58_address": ss58_address,
"challenge_attempts": challenge_attempts,
"challenge_successes": challenge_successes,
"challenge_failed": int(challenge_failed) if challenge_failed else 0,
"challenge_successes": challenge_successes,
"challenge_elapsed_time_avg": challenge_elapsed_time_avg,
"challenge_difficulty_avg": challenge_difficulty_avg,
"last_20_challenge_failed": last_20_challenge_failed,
32 changes: 15 additions & 17 deletions neurons/validator.py
Original file line number Diff line number Diff line change
@@ -292,34 +292,32 @@ def sync_scores(self):

self.pretty_print_dict_values(self.stats)

self._queryable_uids = self.get_queryable()

# Calculate score
for uid in self.uids:
try:
# Determine if the user's hotkey has Docker
hotkey = self.stats[uid].get("ss58_address")
axon = self._queryable_uids[uid]
hotkey = axon.hotkey

if uid not in self.stats:
self.stats[uid] = {} # Initialize empty dictionary for this UID

if hotkey in has_docker_hotkeys:
self.stats[uid]["has_docker"] = True
elif not self.finalized_specs_once:
self.stats[uid]["has_docker"] = True
else:
self.stats[uid]["has_docker"] = False

# Find the maximum score of all uids excluding allocated uids
# max_score_uids = max(
# self.stats[uid]["score"]
# for uid in self.stats
# if "score" in self.stats[uid] and self.stats[uid].get("ss58_address") not in self.allocated_hotkeys
# )

# score = calc_score(self.stats[uid], hotkey=hotkey, allocated_hotkeys=self.allocated_hotkeys, max_score_uid=max_score_uids)
score = calc_score(self.stats[uid],
hotkey = hotkey,
allocated_hotkeys = self.allocated_hotkeys,
penalized_hotkeys = self.penalized_hotkeys,
validator_hotkeys = valid_validator_hotkeys)

score = calc_score(self.stats[uid], hotkey, self.allocated_hotkeys, self.penalized_hotkeys, valid_validator_hotkeys)
self.stats[uid]["score"] = score
except (ValueError, KeyError):

except KeyError as e:
# bt.logging.info(f"KeyError occurred for UID {uid}: {str(e)}")
score = 0
except Exception as e:
# bt.logging.info(f"An unexpected exception occurred for UID {uid}: {str(e)}")
score = 0

self.scores[uid] = score

0 comments on commit f7a66b6

Please sign in to comment.