forked from Ledger-Lenz/Ledgerlens-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_adversarial_detection.py
More file actions
339 lines (260 loc) · 12.9 KB
/
Copy pathtest_adversarial_detection.py
File metadata and controls
339 lines (260 loc) · 12.9 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
"""Recall and unit tests for the adversarial detection pipeline.
Asserts that:
- Each generator produces statistically correct output (unit tests).
- The default LedgerLens detection pipeline achieves ≥ minimum recall
thresholds on each evasion strategy (integration tests).
- AdversarialDataset.build() produces complete, finite feature DataFrames.
Run these alongside nightly CI to gate model robustness on adversarial data.
"""
from __future__ import annotations
import random
import string
import numpy as np
import pandas as pd
import pytest
from detection.feature_engineering import FEATURE_NAMES
from ingestion.adversarial_data import (
ASSET_PAIRS,
BENFORD_PROBS,
AdversarialDataset,
BenfordCamouflageGenerator,
CrossPairRotationGenerator,
GraphFragmentationGenerator,
TimingJitterGenerator,
)
_ALPHA = string.ascii_uppercase + "234567"
def _random_wallets(n: int, seed: int = 0) -> list[str]:
rng = random.Random(seed)
return ["G" + "".join(rng.choices(_ALPHA, k=55)) for _ in range(n)]
# ---------------------------------------------------------------------------
# Shared trained-models fixture (module-scoped to avoid re-training per test)
# ---------------------------------------------------------------------------
@pytest.fixture(scope="module")
def trained_models():
"""Train a minimal RF/XGB/LightGBM ensemble for adversarial recall tests.
Skips automatically when ``mlflow`` or other optional training deps are
absent (same pre-condition as the rest of the model-training test suite).
"""
pytest.importorskip("mlflow", reason="mlflow required for model training tests")
from detection.dataset import build_training_dataset
from detection.model_training import train_ensemble
from ingestion.synthetic_data import generate_synthetic_dataset
trades, meta, events, labels = generate_synthetic_dataset(
n_normal_accounts=60, n_wash_rings=15, ring_size=3, seed=42
)
df = build_training_dataset(trades, labels, account_metadata=meta, order_book_events=events)
results = train_ensemble(df, adversarial_augment=True, calibrate=False)
models = {
k: v["model"]
for k, v in results.items()
if not k.startswith("_") and isinstance(v, dict) and "model" in v
}
class _EnsemblePredictor:
def __init__(self, model_dict: dict) -> None:
self._models = model_dict
def predict(self, X: pd.DataFrame) -> np.ndarray:
X_arr = X.fillna(0.0).values
probas = np.stack([m.predict_proba(X_arr)[:, 1] for m in self._models.values()])
return probas.mean(axis=0)
return _EnsemblePredictor(models)
# ---------------------------------------------------------------------------
# Unit: BenfordCamouflageGenerator
# ---------------------------------------------------------------------------
def test_benford_camouflage_conformity():
"""10 000 generated amounts must not reject Benford's Law (chi-square p > 0.05)."""
from scipy.stats import chisquare
gen = BenfordCamouflageGenerator(seed=0)
amounts = [gen.sample_amount() for _ in range(10_000)]
leading = [int(str(a).lstrip("0").replace(".", "")[0]) for a in amounts]
observed = np.array([leading.count(d) for d in range(1, 10)], dtype=float)
expected = BENFORD_PROBS * len(amounts)
_, p = chisquare(observed, expected)
assert p > 0.05, f"Benford chi-square p={p:.4f} < 0.05 — amounts do not conform"
def test_benford_amounts_all_positive():
"""All generated amounts must be strictly positive."""
gen = BenfordCamouflageGenerator(seed=1)
amounts = [gen.sample_amount() for _ in range(1_000)]
assert all(a > 0 for a in amounts), "Non-positive amount generated"
def test_benford_camouflage_generates_correct_count():
"""generate() must return exactly n_trades Trade objects."""
gen = BenfordCamouflageGenerator(seed=2)
wallets = _random_wallets(5)
trades = gen.generate(wallets, n_trades=50)
assert len(trades) == 50
# ---------------------------------------------------------------------------
# Unit: TimingJitterGenerator
# ---------------------------------------------------------------------------
def test_timing_jitter_mean_interval():
"""Mean inter-arrival time must be within 20 % of λ*60 s."""
from datetime import datetime, timezone
lam = 10.0
gen = TimingJitterGenerator(mean_interval_minutes=lam, seed=0)
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
ts = gen.generate_timestamps(1_000, start)
intervals = np.array([(ts[i + 1] - ts[i]).total_seconds() for i in range(len(ts) - 1)])
mean_s = intervals.mean()
target_s = lam * 60
assert abs(mean_s - target_s) / target_s < 0.20, (
f"Mean interval {mean_s:.1f}s differs from target {target_s:.1f}s by >20 %"
)
def test_timing_jitter_coefficient_of_variation():
"""Exponential distribution has coefficient of variation ≈ 1.0 (within 0.3)."""
from datetime import datetime, timezone
gen = TimingJitterGenerator(seed=2)
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
ts = gen.generate_timestamps(1_000, start)
intervals = np.array([(ts[i + 1] - ts[i]).total_seconds() for i in range(len(ts) - 1)])
cv = intervals.std() / intervals.mean()
assert 0.70 <= cv <= 1.30, f"CoV={cv:.3f} is far from expected 1.0 (Poisson process)"
def test_timing_jitter_timestamps_monotonic():
"""Generated timestamps must be strictly increasing."""
from datetime import datetime, timezone
gen = TimingJitterGenerator(seed=3)
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
ts = gen.generate_timestamps(100, start)
assert all(ts[i] < ts[i + 1] for i in range(len(ts) - 1)), "Timestamps not monotonic"
# ---------------------------------------------------------------------------
# Unit: GraphFragmentationGenerator
# ---------------------------------------------------------------------------
def test_graph_fragmentation_scc_size():
"""All SCCs in the fragmented graph must have at most 3 nodes."""
from detection.graph_engine import build_transaction_graph, find_wash_rings
gen = GraphFragmentationGenerator()
trades = gen.generate(n_hub_wallets=12, n_trades_per_fragment=6, seed=0)
df = pd.DataFrame([t.model_dump() for t in trades])
df["ledger_close_time"] = pd.to_datetime(df["ledger_close_time"], utc=True)
graph = build_transaction_graph(df)
rings = find_wash_rings(graph, min_ring_size=2)
for ring in rings:
assert len(ring["accounts"]) <= 3, (
f"Ring has {len(ring['accounts'])} nodes; expected ≤ 3 for fragmented graph"
)
def test_graph_fragmentation_gfrag_addresses_not_stellar():
"""GFRAG addresses must not pass Stellar G-address validation."""
from ingestion.adversarial_data import _is_valid_stellar_address
gen = GraphFragmentationGenerator()
trades = gen.generate(n_hub_wallets=9, n_trades_per_fragment=3, seed=0)
addrs = {t.base_account for t in trades} | {
t.counter_account for t in trades if t.counter_account
}
for addr in addrs:
assert not _is_valid_stellar_address(addr), (
f"GFRAG address {addr!r} incorrectly passes Stellar validation"
)
def test_graph_fragmentation_generates_trades():
"""generate() with n_hub_wallets=9 must produce exactly 3 rings × n_trades trades."""
gen = GraphFragmentationGenerator()
n_per = 10
trades = gen.generate(n_hub_wallets=9, n_trades_per_fragment=n_per, seed=0)
assert len(trades) == 3 * n_per # 9 // 3 = 3 rings
# ---------------------------------------------------------------------------
# Unit: CrossPairRotationGenerator
# ---------------------------------------------------------------------------
def test_cross_pair_coverage():
"""Each asset pair must have exactly n_trades_per_pair trades."""
wallets = _random_wallets(5)
n_per_pair = 15
gen = CrossPairRotationGenerator()
trades = gen.generate(wallets, n_trades_per_pair=n_per_pair, seed=0)
counts: dict[str, int] = {}
for t in trades:
key = f"{t.base_asset.code}/{t.counter_asset.code}"
counts[key] = counts.get(key, 0) + 1
for pair in ASSET_PAIRS:
base_code, counter_code = pair.split("/")
key = f"{base_code}/{counter_code}"
assert counts.get(key, 0) >= n_per_pair, (
f"Pair {pair}: {counts.get(key, 0)} trades < {n_per_pair}"
)
def test_cross_pair_total_trade_count():
"""Total trades == len(ASSET_PAIRS) * n_trades_per_pair."""
wallets = _random_wallets(4)
n_per = 10
gen = CrossPairRotationGenerator()
trades = gen.generate(wallets, n_trades_per_pair=n_per, seed=0)
assert len(trades) == len(ASSET_PAIRS) * n_per
# ---------------------------------------------------------------------------
# Unit: zero/negative amount guard
# ---------------------------------------------------------------------------
def test_all_generators_produce_positive_amounts():
"""All generators must produce base_amount > 0."""
wallets = _random_wallets(5, seed=99)
gen_b = BenfordCamouflageGenerator(seed=99)
for t in gen_b.generate(wallets, n_trades=200):
assert t.base_amount > 0, f"BenfordCamouflage: base_amount={t.base_amount}"
gen_t = TimingJitterGenerator(seed=99)
for t in gen_t.generate(wallets, n_trades=200):
assert t.base_amount > 0, f"TimingJitter: base_amount={t.base_amount}"
gen_g = GraphFragmentationGenerator()
for t in gen_g.generate(n_hub_wallets=9, n_trades_per_fragment=10, seed=99):
assert t.base_amount > 0, f"GraphFragmentation: base_amount={t.base_amount}"
gen_c = CrossPairRotationGenerator()
for t in gen_c.generate(wallets, n_trades_per_pair=20, seed=99):
assert t.base_amount > 0, f"CrossPairRotation: base_amount={t.base_amount}"
# ---------------------------------------------------------------------------
# Integration: AdversarialDataset feature completeness
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("strategy", [
"benford_camouflage",
"timing_jitter",
"graph_fragmentation",
"cross_pair_rotation",
])
def test_adversarial_dataset_feature_completeness(strategy):
"""AdversarialDataset.build() must produce all FEATURE_NAMES columns, all finite."""
dataset = AdversarialDataset().build(
strategy=strategy, n_wallets=20, n_trades=100, seed=0
)
assert "label" in dataset.columns, "Missing 'label' column"
for col in FEATURE_NAMES:
assert col in dataset.columns, f"Missing feature column: {col}"
X = (
dataset[FEATURE_NAMES]
.fillna(0.0)
.replace([float("inf"), float("-inf")], 0.0)
.values.astype(float)
)
assert np.all(np.isfinite(X)), "Non-finite values remain in feature matrix after 0-fill"
@pytest.mark.parametrize("strategy", [
"benford_camouflage",
"timing_jitter",
"graph_fragmentation",
"cross_pair_rotation",
])
def test_adversarial_dataset_has_wash_labels(strategy):
"""AdversarialDataset.build() must include at least one wash-labelled account."""
dataset = AdversarialDataset().build(
strategy=strategy, n_wallets=10, n_trades=50, seed=0
)
assert (dataset["label"] == 1).any(), f"No wash-labelled accounts for strategy {strategy}"
def test_adversarial_dataset_unknown_strategy_raises():
"""AdversarialDataset.build() must raise ValueError for an unknown strategy."""
with pytest.raises(ValueError, match="Unknown strategy"):
AdversarialDataset().build(strategy="not_a_strategy")
def test_adversarial_dataset_reproducible():
"""AdversarialDataset.build() must be deterministic for the same seed."""
d1 = AdversarialDataset().build(strategy="timing_jitter", n_wallets=10, n_trades=40, seed=7)
d2 = AdversarialDataset().build(strategy="timing_jitter", n_wallets=10, n_trades=40, seed=7)
pd.testing.assert_frame_equal(d1.reset_index(drop=True), d2.reset_index(drop=True))
# ---------------------------------------------------------------------------
# Integration: detection recall on adversarial strategies
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("strategy,min_recall", [
("benford_camouflage", 0.60),
("timing_jitter", 0.65),
("graph_fragmentation", 0.55),
("cross_pair_rotation", 0.60),
])
def test_detection_recall_on_adversarial_strategy(strategy, min_recall, trained_models):
"""Detection recall on adversarial wash trades must meet the minimum threshold."""
dataset = AdversarialDataset().build(strategy=strategy, seed=42)
X = dataset[FEATURE_NAMES].fillna(0.0)
y = dataset["label"].values
y_pred = trained_models.predict(X)
wash_mask = y == 1
if not wash_mask.any():
pytest.skip(f"No wash accounts in adversarial dataset for strategy={strategy}")
recall = float((y_pred[wash_mask] >= 0.5).mean())
assert recall >= min_recall, (
f"Detection recall on {strategy} adversarial trades: {recall:.2%} < {min_recall:.0%}"
)