-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
163 lines (139 loc) · 6.08 KB
/
monitor.py
File metadata and controls
163 lines (139 loc) · 6.08 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
import argparse
import asyncio
import json
import logging
import httpx
from alerter import Alerter
from bootstrap import bootstrap_solana, bootstrap_sui, import_scans
from config import Config
from cosmos_monitor import CosmosMonitor
from dot_monitor import DotMonitor
from enrichment import Enricher
from eth_monitor import EthMonitor
from scan_client import ScanClient
from scan_queue import ScanQueue
from sol_monitor import SolMonitor
from state import State
from sui_monitor import SuiMonitor
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
async def run_bootstrap_solana() -> None:
config = Config.from_env()
async with httpx.AsyncClient(timeout=30) as client:
await bootstrap_solana(config, client)
async def run_bootstrap_sui() -> None:
config = Config.from_env()
async with httpx.AsyncClient(timeout=30) as client:
await bootstrap_sui(config, client)
async def run_enrich(vote_account: str, identity: str) -> None:
config = Config.from_env()
async with httpx.AsyncClient(timeout=15) as client:
if not identity and config.sol_rpc_url:
payload = {"jsonrpc": "2.0", "id": 1, "method": "getVoteAccounts", "params": []}
resp = await client.post(config.sol_rpc_url, json=payload)
resp.raise_for_status()
data = resp.json().get("result", {})
for v in data.get("current", []) + data.get("delinquent", []):
if v.get("votePubkey") == vote_account:
identity = v.get("nodePubkey", "")
break
enricher = Enricher(config, client)
enricher.load_known_operators()
enricher.load_stakewiz_cache()
enricher.load_scan_index()
enricher.load_node_ip_cache()
result = await enricher.enrich_solana(vote_account, identity)
print(json.dumps(result.__dict__, default=str, indent=2))
async def run_scan(vote_account: str) -> None:
config = Config.from_env()
async with httpx.AsyncClient(timeout=30) as client:
# Resolve identity and enrich
identity = ""
if config.sol_rpc_url:
payload = {"jsonrpc": "2.0", "id": 1, "method": "getVoteAccounts", "params": []}
resp = await client.post(config.sol_rpc_url, json=payload)
resp.raise_for_status()
data = resp.json().get("result", {})
for v in data.get("current", []) + data.get("delinquent", []):
if v.get("votePubkey") == vote_account:
identity = v.get("nodePubkey", "")
break
enricher = Enricher(config, client)
enricher.load_known_operators()
enricher.load_stakewiz_cache()
enricher.load_scan_index()
enricher.load_node_ip_cache()
enriched = await enricher.enrich_solana(vote_account, identity)
scan_client = ScanClient(config, client)
scan_queue = ScanQueue(config, scan_client)
scan_queue.load()
metadata = {
"source": "ferret",
"trigger": "manual",
"validator_pubkey": vote_account,
"validator_name": enriched.name,
"network": "solana",
"incident_time": __import__("datetime").datetime.now(__import__("datetime").timezone.utc).isoformat(),
"event_type": "manual_scan",
}
result = await scan_queue.try_scan(
vote_account, "solana", enriched.ips, float("inf"), enriched.name, metadata
)
scan_queue.save()
print(json.dumps({
"vote_account": vote_account,
"name": enriched.name,
"ips": enriched.ips,
"scan_result": result.__dict__,
}, indent=2))
async def run_import_scans(path: str) -> None:
config = Config.from_env()
await import_scans(config.scanned_validators_path, path)
async def main() -> None:
config = Config.from_env()
state = State(config.state_path)
state.load()
async with httpx.AsyncClient(timeout=30) as client:
alerter = Alerter(config, state)
enricher = Enricher(config, client)
enricher.load_known_operators()
enricher.load_stakewiz_cache()
enricher.load_scan_index()
enricher.load_node_ip_cache()
scan_client = ScanClient(config, client)
scan_queue = ScanQueue(config, scan_client)
scan_queue.load()
eth = EthMonitor(config, state, alerter, client)
sol = SolMonitor(config, state, alerter, client, enricher=enricher, scan_queue=scan_queue)
sui = SuiMonitor(config, state, alerter, client)
cosmos = CosmosMonitor(config, state, alerter, client)
dot = DotMonitor(config, state, alerter, client)
logger.info("Starting validator incident monitor")
await asyncio.gather(eth.run(), sol.run(), sui.run(), cosmos.run(), dot.run())
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Validator incident monitor")
group = parser.add_mutually_exclusive_group()
group.add_argument("--bootstrap-solana", action="store_true", help="Bootstrap validators.app cache")
group.add_argument("--bootstrap-sui", action="store_true", help="Bootstrap Sui operator data")
group.add_argument("--import-scans", metavar="PATH", help="Import scan results from PATH")
group.add_argument("--enrich", metavar="VOTE_ACCOUNT", help="Print enriched data for a Solana validator")
group.add_argument("--scan", metavar="VOTE_ACCOUNT", help="Manually trigger a scan for a Solana validator")
args = parser.parse_args()
try:
if args.bootstrap_solana:
asyncio.run(run_bootstrap_solana())
elif args.bootstrap_sui:
asyncio.run(run_bootstrap_sui())
elif args.import_scans:
asyncio.run(run_import_scans(args.import_scans))
elif args.enrich:
asyncio.run(run_enrich(args.enrich, ""))
elif args.scan:
asyncio.run(run_scan(args.scan))
else:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Monitor stopped by user")