-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_queue.py
More file actions
167 lines (139 loc) · 5.67 KB
/
scan_queue.py
File metadata and controls
167 lines (139 loc) · 5.67 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
166
167
import json
import logging
from dataclasses import dataclass, field
from datetime import datetime, timezone, timedelta
from typing import List
from config import Config
from scan_client import ScanClient, ScanClientError
logger = logging.getLogger(__name__)
@dataclass
class ScanRequest:
pubkey: str
network: str
ips: List[str]
metadata: dict
queued_at: str
reason: str
@dataclass
class ScanResult:
status: str # triggered | queued | skipped_cooldown | skipped_stake | skipped_no_ips | skipped_disabled
ips: List[str] = field(default_factory=list)
scan_ids: List[str] = field(default_factory=list)
cdn_blocked_ips: List[str] = field(default_factory=list)
queue_position: int = 0
last_scan_at: str = ""
class ScanQueue:
def __init__(self, config: Config, scan_client: ScanClient) -> None:
self.config = config
self.scan_client = scan_client
self._state: dict = {"queued": [], "last_ferret_scan": {}}
self._call_timestamps: List[datetime] = []
def load(self) -> None:
try:
with open(self.config.scan_queue_path) as f:
data = json.load(f)
self._state = {
"queued": data.get("queued", []),
"last_ferret_scan": data.get("last_ferret_scan", {}),
}
except (FileNotFoundError, json.JSONDecodeError):
self._state = {"queued": [], "last_ferret_scan": {}}
def save(self) -> None:
with open(self.config.scan_queue_path, "w") as f:
json.dump(self._state, f, indent=2)
def _within_rate_limit(self) -> bool:
cutoff = datetime.now(timezone.utc) - timedelta(hours=1)
self._call_timestamps = [t for t in self._call_timestamps if t > cutoff]
return len(self._call_timestamps) < self.config.scan_rate_limit
def _record_call(self) -> None:
self._call_timestamps.append(datetime.now(timezone.utc))
def _available_slots(self) -> int:
cutoff = datetime.now(timezone.utc) - timedelta(hours=1)
self._call_timestamps = [t for t in self._call_timestamps if t > cutoff]
return max(0, self.config.scan_rate_limit - len(self._call_timestamps))
async def try_scan(
self,
pubkey: str,
network: str,
ips: List[str],
stake: float,
operator_name: str,
metadata: dict,
) -> ScanResult:
if not self.config.enable_auto_scan:
return ScanResult(status="skipped_disabled")
if not ips:
return ScanResult(status="skipped_no_ips")
if network == "solana" and stake < self.config.scan_min_stake_sol:
return ScanResult(status="skipped_stake")
if network == "sui" and stake < self.config.scan_min_stake_sui:
return ScanResult(status="skipped_stake")
last_scan = self._state["last_ferret_scan"].get(pubkey)
if last_scan:
last_dt = datetime.fromisoformat(last_scan)
if last_dt.tzinfo is None:
last_dt = last_dt.replace(tzinfo=timezone.utc)
age = (datetime.now(timezone.utc) - last_dt).total_seconds()
if age < self.config.scan_cooldown:
return ScanResult(status="skipped_cooldown", last_scan_at=last_scan)
if not self._within_rate_limit():
position = len(self._state["queued"]) + 1
self._state["queued"].append({
"pubkey": pubkey,
"network": network,
"ips": ips,
"metadata": metadata,
"queued_at": datetime.now(timezone.utc).isoformat(),
"reason": "rate_limited",
})
return ScanResult(status="queued", ips=ips, queue_position=position)
return await self._do_submit(pubkey, network, ips, metadata)
async def _do_submit(
self, pubkey: str, network: str, ips: List[str], metadata: dict
) -> ScanResult:
scan_ids = []
cdn_blocked_ips = []
submitted_ips = []
for ip in ips:
try:
sub = await self.scan_client.submit(ip, metadata, protocol=network)
self._record_call()
submitted_ips.append(ip)
if sub.cdn_blocked:
cdn_blocked_ips.append(ip)
else:
scan_ids.append(sub.scan_id)
except ScanClientError as e:
logger.warning("Scan submission failed for %s/%s: %s", pubkey, ip, e)
self._state["last_ferret_scan"][pubkey] = datetime.now(timezone.utc).isoformat()
return ScanResult(
status="triggered",
ips=submitted_ips,
scan_ids=scan_ids,
cdn_blocked_ips=cdn_blocked_ips,
)
async def process_queue(self) -> None:
if not self._state["queued"]:
return
available = self._available_slots()
if available <= 0:
return
remaining = list(self._state["queued"])
self._state["queued"] = []
processed = 0
for item in remaining:
if processed >= available:
self._state["queued"].append(item)
continue
pubkey = item["pubkey"]
network = item["network"]
ips = item["ips"]
metadata = item.get("metadata", {})
# How many IPs can we still submit?
slots_left = available - processed
batch_ips = ips[:slots_left]
remainder_ips = ips[slots_left:]
await self._do_submit(pubkey, network, batch_ips, metadata)
processed += len(batch_ips)
if remainder_ips:
self._state["queued"].append({**item, "ips": remainder_ips})