-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot_monitor.py
More file actions
165 lines (145 loc) · 5.68 KB
/
dot_monitor.py
File metadata and controls
165 lines (145 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import asyncio
import logging
from dataclasses import dataclass
from typing import Dict, List, Optional
import httpx
from config import Config
from state import State
logger = logging.getLogger(__name__)
@dataclass
class DotValidator:
stash: str
display: str
is_elected: bool
commission: float
bonded: int # total bonded in Planck
@dataclass
class DotSlashEvent:
stash: str
amount: int # Planck
block_num: int
event_index: str # unique ID for dedup
class DotMonitor:
def __init__(
self,
config: Config,
state: State,
alerter,
client: httpx.AsyncClient,
) -> None:
self.config = config
self.state = state
self.alerter = alerter
self.client = client
def _subscan_headers(self) -> dict:
headers = {"Content-Type": "application/json"}
if self.config.dot_subscan_api_key:
headers["X-API-Key"] = self.config.dot_subscan_api_key
return headers
def parse_validators(self, data: dict) -> List[DotValidator]:
validators = []
for item in data.get("data", {}).get("list", []) or []:
display_info = item.get("stash_account_display", {})
stash = display_info.get("address", "")
display = display_info.get("display", stash)
validators.append(
DotValidator(
stash=stash,
display=display,
is_elected=bool(item.get("is_elected", False)),
commission=float(item.get("validator_prefs_value", 0)) / 1e7,
bonded=int(item.get("bonded_total", 0)),
)
)
return validators
def parse_slash_events(self, data: dict) -> List[DotSlashEvent]:
events = []
for item in data.get("data", {}).get("events", []) or []:
params = item.get("params", [])
stash = ""
amount = 0
for p in params:
if p.get("type_name") == "T::AccountId" or p.get("name") == "stash":
stash = p.get("value", "")
elif p.get("type_name") == "BalanceOf<T>" or p.get("name") == "amount":
amount = int(p.get("value", 0))
event_index = item.get("event_index", "")
block_num = item.get("block_num", 0)
if stash:
events.append(
DotSlashEvent(
stash=stash,
amount=amount,
block_num=block_num,
event_index=event_index,
)
)
return events
async def fetch_validators(self) -> List[DotValidator]:
url = f"{self.config.dot_subscan_url}/api/scan/staking/validators"
payload = {
"row": 100,
"page": 0,
"order": "desc",
"order_field": "bonded_nominators_count",
}
resp = await self.client.post(url, json=payload, headers=self._subscan_headers())
resp.raise_for_status()
return self.parse_validators(resp.json())
async def fetch_slash_events(self) -> List[DotSlashEvent]:
url = f"{self.config.dot_subscan_url}/api/scan/events"
payload = {"module": "staking", "call": "Slash", "row": 25, "page": 0}
resp = await self.client.post(url, json=payload, headers=self._subscan_headers())
resp.raise_for_status()
return self.parse_slash_events(resp.json())
async def poll_inactive(self, validators: List[DotValidator]) -> None:
previous_active = set(self.state.get_previous_dot_active())
configured = set(self.config.dot_validators)
elected_stashes = {v.stash for v in validators if v.is_elected}
validator_map = {v.stash: v for v in validators}
for stash in configured:
was_active = stash in previous_active
is_active = stash in elected_stashes
if was_active and not is_active:
# Build a placeholder if the stash isn't in the current list
v = validator_map.get(
stash,
DotValidator(stash=stash, display=stash, is_elected=False, commission=0.0, bonded=0),
)
await self.alerter.alert_dot_inactive(v)
self.state.set_previous_dot_active(list(elected_stashes & configured))
self.state.save()
async def poll_slashing(self) -> None:
events = await self.fetch_slash_events()
configured = set(self.config.dot_validators)
for event in events:
if event.stash not in configured:
continue
event_key = f"dot_slash_{event.event_index}"
if self.state.is_seen(event_key):
continue
# Build a placeholder validator
v = DotValidator(
stash=event.stash,
display=event.stash,
is_elected=False,
commission=0.0,
bonded=0,
)
await self.alerter.alert_dot_slashed(v, event)
self.state.mark_seen(event_key)
self.state.save()
async def poll(self) -> None:
validators = await self.fetch_validators()
await self.poll_inactive(validators)
await self.poll_slashing()
async def run(self) -> None:
if not self.config.dot_validators:
logger.info("DOT_VALIDATORS not set, Polkadot monitor disabled")
return
while True:
try:
await self.poll()
except Exception as e:
logger.error("Polkadot monitor error: %s", e)
await asyncio.sleep(self.config.poll_interval_dot)