-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipeline.py
More file actions
368 lines (314 loc) · 14.6 KB
/
Copy pathpipeline.py
File metadata and controls
368 lines (314 loc) · 14.6 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
VaultWatch — Main Orchestration Pipeline
Wires all 6 agents + sidecar SSE into concurrent asyncio queues.
Run with: python pipeline.py
"""
from __future__ import annotations
import os
import asyncio
import logging
import signal
from typing import Any, Dict
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
# Agents
from agents.scanner_agent import ScannerAgent
from agents.anomaly_agent import AnomalyAgent, AnomalyResult
from agents.self_correction_agent import SelfCorrectionAgent
from agents.rwa_agent import RWAAgent
from agents.safety_guard import SafetyGuard
from agents.audit_agent import AuditAgent
from agents.intel_agent import IntelAgent
# Sidecar
from streaming.sidecar_client import SidecarClient
# Casper
from casper_client import CasperContractClient
# ---------------------------------------------------------------------------
# Logging & tracing
# ---------------------------------------------------------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
)
logger = logging.getLogger("vaultwatch.pipeline")
_provider = TracerProvider()
_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(_provider)
tracer = trace.get_tracer("vaultwatch.pipeline")
# ---------------------------------------------------------------------------
# Queue capacity
# ---------------------------------------------------------------------------
QUEUE_SIZE = 256
# ---------------------------------------------------------------------------
# Pipeline
# ---------------------------------------------------------------------------
class VaultWatchPipeline:
"""
Orchestrates VaultWatch agents over async queues.
Data flow:
sidecar_events -> scanner_queue -> scanner_agent
-> anomaly_queue -> anomaly_agent -> self_correction
-> rwa_queue -> rwa_agent
-> intel_queue -> intel_agent
-> audit_queue -> audit_agent
"""
def __init__(self) -> None:
groq_key = os.getenv("GROQ_API_KEY", "")
mock_casper = os.getenv("CASPER_MOCK", "true").lower() == "true"
self.casper = CasperContractClient(mock=mock_casper)
self.sidecar = SidecarClient(
url=os.getenv("CASPER_SIDECAR_URL", "http://localhost:9999/events/main"),
)
self.scanner = ScannerAgent(groq_api_key=groq_key)
self.anomaly = AnomalyAgent(groq_api_key=groq_key)
self.correction = SelfCorrectionAgent(groq_api_key=groq_key)
self.rwa = RWAAgent(groq_api_key=groq_key)
self.safety = SafetyGuard(groq_api_key=groq_key)
self.audit = AuditAgent(casper_client=self.casper)
self.intel = IntelAgent(groq_api_key=groq_key)
# Inter-agent queues
self.scanner_q: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(QUEUE_SIZE)
self.anomaly_q: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(QUEUE_SIZE)
self.rwa_q: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(QUEUE_SIZE)
self.intel_q: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(QUEUE_SIZE)
self.audit_q: asyncio.Queue[Dict[str, Any]] = asyncio.Queue(QUEUE_SIZE)
self.correction_q: asyncio.Queue[AnomalyResult] = asyncio.Queue(QUEUE_SIZE)
self._running = False
# ------------------------------------------------------------------
# Sidecar event fan-out
# ------------------------------------------------------------------
async def _sidecar_fan_out(self) -> None:
"""
Consume Casper Sidecar SSE events and distribute to relevant queues.
Reconnects on error.
"""
logger.info("Sidecar fan-out started")
while self._running:
try:
async for event in self.sidecar.stream():
if not self._running:
break
with tracer.start_as_current_span("pipeline.fan_out") as span:
span.set_attribute("event_type", event.get("type", "unknown"))
event_type = event.get("type", "")
# Route Deploy events to scanner + anomaly
if event_type in ("Deploy", "DeployProcessed"):
await _put_nowait(self.scanner_q, event, "scanner_q")
await _put_nowait(self.anomaly_q, event, "anomaly_q")
await _put_nowait(
self.audit_q,
{"action": "deploy_event", "data": event},
"audit_q",
)
# Block events go to intel
elif event_type in ("Block", "BlockAdded"):
await _put_nowait(self.intel_q, event, "intel_q")
# Transfer/step events -> rwa
elif event_type in ("Step", "Transfer"):
await _put_nowait(self.rwa_q, event, "rwa_q")
# Unclassified -> intel as catch-all
else:
await _put_nowait(self.intel_q, event, "intel_q")
except asyncio.CancelledError:
break
except Exception as exc:
logger.warning("Sidecar stream error (%s) — reconnecting in 5s", exc)
await asyncio.sleep(5)
# ------------------------------------------------------------------
# Agent workers
# ------------------------------------------------------------------
async def _scanner_worker(self) -> None:
logger.info("Scanner worker started")
while self._running:
try:
event = await asyncio.wait_for(self.scanner_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.scanner"):
protocol = _extract_protocol(event)
result = await self.scanner.scan(protocol=protocol, chain="casper")
logger.debug("Scanner result for %s: %s", protocol, result.get("risk_level"))
# High-risk scans get forwarded to intel
if result.get("risk_level") in ("HIGH", "CRITICAL"):
await _put_nowait(
self.intel_q,
{"type": "scanner_alert", "scan": result},
"intel_q",
)
except Exception as exc:
logger.error("Scanner worker error: %s", exc)
finally:
self.scanner_q.task_done()
async def _anomaly_worker(self) -> None:
logger.info("Anomaly worker started")
while self._running:
try:
event = await asyncio.wait_for(self.anomaly_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.anomaly") as span:
metrics = _event_to_metrics(event)
result: AnomalyResult = await self.anomaly.detect(metrics)
span.set_attribute("risk_score", result.risk_score)
if result.risk_score >= 70:
# High risk — trigger self-correction
await _put_nowait(self.correction_q, result, "correction_q")
await _put_nowait(
self.audit_q,
{"action": "high_risk_anomaly", "data": result.__dict__},
"audit_q",
)
except Exception as exc:
logger.error("Anomaly worker error: %s", exc)
finally:
self.anomaly_q.task_done()
async def _correction_worker(self) -> None:
logger.info("Self-correction worker started")
while self._running:
try:
anomaly_result = await asyncio.wait_for(self.correction_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.self_correction"):
corrected = await self.correction.correct(anomaly_result)
logger.info(
"Self-correction applied for %s — confidence: %.2f",
anomaly_result.protocol,
corrected.get("confidence", 0),
)
except Exception as exc:
logger.error("Correction worker error: %s", exc)
finally:
self.correction_q.task_done()
async def _rwa_worker(self) -> None:
logger.info("RWA worker started")
while self._running:
try:
event = await asyncio.wait_for(self.rwa_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.rwa"):
asset_data = _event_to_asset(event)
result = await self.rwa.assess(asset_data)
logger.debug("RWA assessment: %s", result.get("verdict"))
except Exception as exc:
logger.error("RWA worker error: %s", exc)
finally:
self.rwa_q.task_done()
async def _intel_worker(self) -> None:
logger.info("Intel worker started")
while self._running:
try:
event = await asyncio.wait_for(self.intel_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.intel"):
query = _event_to_query(event)
result = await self.intel.analyze(query)
logger.debug(
"Intel analysis complete — findings: %d",
result.get("findings_count", 0),
)
except Exception as exc:
logger.error("Intel worker error: %s", exc)
finally:
self.intel_q.task_done()
async def _audit_worker(self) -> None:
logger.info("Audit worker started")
while self._running:
try:
item = await asyncio.wait_for(self.audit_q.get(), timeout=1.0)
except asyncio.TimeoutError:
continue
except asyncio.CancelledError:
break
try:
with tracer.start_as_current_span("pipeline.audit"):
await self.audit.record(
action=item.get("action", "unknown"),
actor="pipeline",
details=str(item.get("data", "")),
)
except Exception as exc:
logger.error("Audit worker error: %s", exc)
finally:
self.audit_q.task_done()
# ------------------------------------------------------------------
# Lifecycle
# ------------------------------------------------------------------
async def run(self) -> None:
"""Start all workers and the sidecar stream concurrently."""
self._running = True
logger.info("VaultWatch pipeline starting...")
tasks = [
asyncio.create_task(self._sidecar_fan_out(), name="sidecar_fan_out"),
asyncio.create_task(self._scanner_worker(), name="scanner_worker"),
asyncio.create_task(self._anomaly_worker(), name="anomaly_worker"),
asyncio.create_task(self._correction_worker(), name="correction_worker"),
asyncio.create_task(self._rwa_worker(), name="rwa_worker"),
asyncio.create_task(self._intel_worker(), name="intel_worker"),
asyncio.create_task(self._audit_worker(), name="audit_worker"),
]
def _shutdown(sig: Any, frame: Any) -> None:
logger.info("Shutdown signal received")
self._running = False
for t in tasks:
t.cancel()
signal.signal(signal.SIGINT, _shutdown)
signal.signal(signal.SIGTERM, _shutdown)
logger.info("VaultWatch pipeline running — %d workers active", len(tasks))
await asyncio.gather(*tasks, return_exceptions=True)
logger.info("VaultWatch pipeline shut down cleanly")
# ---------------------------------------------------------------------------
# Event extraction helpers
# ---------------------------------------------------------------------------
def _extract_protocol(event: Dict[str, Any]) -> str:
return event.get("contract_package_hash") or event.get("deploy_hash", "")[:16] or "unknown"
def _event_to_metrics(event: Dict[str, Any]) -> Dict[str, Any]:
return {
"protocol": _extract_protocol(event),
"tvl": float(event.get("amount", 0)),
"volume_24h": float(event.get("volume", 0)),
"price_change_1h": float(event.get("price_delta", 0)),
"num_transactions": int(event.get("tx_count", 1)),
"liquidity_ratio": float(event.get("liquidity_ratio", 1.0)),
}
def _event_to_asset(event: Dict[str, Any]) -> Dict[str, Any]:
return {
"asset_id": event.get("deploy_hash", "unknown"),
"asset_type": event.get("asset_type", "token"),
"issuer": event.get("from", "unknown"),
"collateral_ratio": float(event.get("collateral_ratio", 1.5)),
"maturity_days": int(event.get("maturity_days", 365)),
"credit_rating": event.get("credit_rating", "BBB"),
}
def _event_to_query(event: Dict[str, Any]) -> str:
event_type = event.get("type", "unknown")
protocol = _extract_protocol(event)
return f"Analyze {event_type} event for protocol {protocol}: {str(event)[:200]}"
async def _put_nowait(q: asyncio.Queue, item: Any, name: str) -> None:
try:
q.put_nowait(item)
except asyncio.QueueFull:
logger.warning("Queue %s is full — dropping item", name)
# ---------------------------------------------------------------------------
# Entrypoint
# ---------------------------------------------------------------------------
if __name__ == "__main__": # pragma: no cover
asyncio.run(VaultWatchPipeline().run())