Skip to content

Commit 7677080

Browse files
authored
Add high score alert (#122)
This PR adds a very simple alerts that checks if the winning score for auctions that made it onchain is larger than some threshold. Very large scores might be an indication of bogus native prices so this is one way to detect those cases. I realized that currently the tests, as they are set up, will pass if the wrapper function returns True. But this doesn't really check the logic the test implements, so in order to properly see logs and behavior, one needs to run the tests as follows. E.g: `python -m pytest --log-cli-level=DEBUG tests/e2e/high_score_test.py`
1 parent ee97d79 commit 7677080

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
# threshold to generate an alert for violating UDP
3838
UDP_SENSITIVITY_THRESHOLD = 0.005
3939

40+
# threshold to generate an alert for high score
41+
HIGH_SCORE_THRESHOLD_ETH = 10
42+
4043
# relevant addresses
4144
SETTLEMENT_CONTRACT_ADDRESS = "0x9008D19f58AAbD9eD0D60971565AA8510560ab41"
4245
MEV_BLOCKER_KICKBACKS_ADDRESSES = [

src/daemon.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from src.monitoring_tests.mev_blocker_kickbacks_test import (
1717
MEVBlockerRefundsMonitoringTest,
1818
)
19+
from src.monitoring_tests.high_score_test import (
20+
HighScoreTest,
21+
)
1922
from src.constants import SLEEP_TIME_IN_SEC
2023

2124

@@ -29,6 +32,7 @@ def main() -> None:
2932
tests = [
3033
SolverCompetitionSurplusTest(),
3134
MEVBlockerRefundsMonitoringTest(),
35+
HighScoreTest(),
3236
]
3337

3438
start_block: Optional[int] = None
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Checking winner's score and generating an alert if score is very large
3+
"""
4+
5+
# pylint: disable=logging-fstring-interpolation
6+
7+
from typing import Any
8+
from src.monitoring_tests.base_test import BaseTest
9+
from src.apis.orderbookapi import OrderbookAPI
10+
from src.constants import HIGH_SCORE_THRESHOLD_ETH
11+
12+
13+
class HighScoreTest(BaseTest):
14+
"""
15+
This test checks how large is the winning score and raises an alert if score
16+
is above certain threshold
17+
"""
18+
19+
def __init__(self) -> None:
20+
super().__init__()
21+
self.orderbook_api = OrderbookAPI()
22+
23+
def compute_winning_score(self, competition_data: dict[str, Any]) -> bool:
24+
"""
25+
This function simply returns the winning score.
26+
"""
27+
solution = competition_data["solutions"][-1]
28+
score = int(solution["score"]) / 10**18
29+
log_output = "\t".join(
30+
[
31+
"Large score test:",
32+
f"Tx Hash: {competition_data['transactionHash']}",
33+
f"Winning Solver: {solution['solver']}",
34+
f"Score in ETH: {score}",
35+
]
36+
)
37+
if score > HIGH_SCORE_THRESHOLD_ETH:
38+
self.alert(log_output)
39+
return True
40+
41+
def run(self, tx_hash: str) -> bool:
42+
"""
43+
Wrapper function for the whole test. Checks if solver competition data is retrievable
44+
and then checks how large the winning score is.
45+
"""
46+
47+
solver_competition_data = self.orderbook_api.get_solver_competition_data(
48+
tx_hash
49+
)
50+
if solver_competition_data is None:
51+
return False
52+
53+
success = self.compute_winning_score(solver_competition_data)
54+
55+
return success

tests/e2e/high_score_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Tests for large score test.
3+
"""
4+
5+
import unittest
6+
from src.monitoring_tests.high_score_test import (
7+
HighScoreTest,
8+
)
9+
10+
11+
class TestHighScore(unittest.TestCase):
12+
def test_high_score(self) -> None:
13+
high_score_test = HighScoreTest()
14+
# large score tx
15+
tx_hash = "0x5eef22d04a2f30e62df76614decf43e1cc92ab957285a687f182a0191d85d15a"
16+
self.assertTrue(high_score_test.run(tx_hash))
17+
18+
# small score tx
19+
tx_hash = "0x37e7dbc2f3df5f31f017634d07855933c6d82f4fda033daebd4377987d2b5ae9"
20+
self.assertTrue(high_score_test.run(tx_hash))
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)