-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_longrun_policy_study.py
More file actions
395 lines (351 loc) · 14.8 KB
/
Copy pathrun_longrun_policy_study.py
File metadata and controls
395 lines (351 loc) · 14.8 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
"""Long-run policy study for probing attacks and recovery behavior."""
from __future__ import annotations
import json
from dataclasses import asdict, replace
import numpy as np
from coinjoin_simulator.network import (
DEFAULT_ORDERBOOK_URL,
NetworkSimulationConfig,
RealisticNetworkSimulator,
extract_bonded_maker_profiles,
fetch_orderbook_snapshot,
load_orderbook_snapshot,
)
ORDERBOOK_CACHE_PATH = "data/orderbook_live_snapshot.json"
def _load_snapshot() -> dict[str, object]:
import os
if os.path.exists(ORDERBOOK_CACHE_PATH):
print(f"Loading cached orderbook from {ORDERBOOK_CACHE_PATH}...")
return load_orderbook_snapshot(ORDERBOOK_CACHE_PATH)
print("Fetching live orderbook...")
return fetch_orderbook_snapshot(DEFAULT_ORDERBOOK_URL)
def _initial_network_stats(sim: RealisticNetworkSimulator) -> dict[str, float | int]:
total_live_utxos = 0
total_funds_sats = 0
for maker in sim.makers:
for depth in maker.mixdepths:
total_live_utxos += len(depth)
total_funds_sats += sum(u.value_sats for u in depth)
return {
"initial_total_live_utxos": total_live_utxos,
"initial_total_funds_sats": total_funds_sats,
"initial_avg_utxos_per_maker": total_live_utxos / len(sim.makers),
"initial_avg_funds_per_maker_sats": total_funds_sats / len(sim.makers),
}
def _known_live_utxo_fraction(sim: RealisticNetworkSimulator) -> float:
total_live = 0
known_live = 0
for maker in sim.makers:
live_ids: set[str] = set()
for depth in maker.mixdepths:
for utxo in depth:
live_ids.add(utxo.utxo_id)
total_live += len(live_ids)
known_live += len(sim.known_utxos_by_maker[maker.maker_id] & live_ids)
return (known_live / total_live) if total_live > 0 else 0.0
def _run_single(
config: NetworkSimulationConfig,
profiles: list,
seed_offset: int,
) -> dict[str, object]:
cfg = replace(config, random_seed=(config.random_seed or 0) + seed_offset)
sim = RealisticNetworkSimulator(cfg, profiles)
init_stats = _initial_network_stats(sim)
result = sim.run().to_dict()
result.update(init_stats)
result["honest_volume_btc"] = float(result["total_honest_volume_sats"]) / 100_000_000
return result
def _run_recovery_timeline(
config: NetworkSimulationConfig,
profiles: list,
attack_rounds: int,
recovery_rounds: int,
attack_evil_fraction: float,
sample_every: int = 50,
recent_window: int = 200,
) -> dict[str, object]:
sim = RealisticNetworkSimulator(config, profiles)
taker_anon_history: list[int] = []
attack_anon_history: list[int] = []
recovery_anon_history: list[int] = []
timeline: list[dict[str, float | int | str]] = []
total_rounds = attack_rounds + recovery_rounds
attack_probe_actions = 0
attack_probed_utxos = 0
attack_end_known_live: float | None = None
for round_idx in range(total_rounds):
phase = "attack" if round_idx < attack_rounds else "recovery"
evil_fraction = attack_evil_fraction if phase == "attack" else 0.0
if sim.rng.random() < evil_fraction:
targets, probed = sim._run_evil_round()
if phase == "attack":
attack_probe_actions += targets
attack_probed_utxos += probed
else:
rec = sim.simulate_single_honest_coinjoin(round_index=round_idx)
if rec is not None:
taker_anon_history.append(rec.taker_anon_set)
if phase == "attack":
attack_anon_history.append(rec.taker_anon_set)
else:
recovery_anon_history.append(rec.taker_anon_set)
if round_idx == attack_rounds - 1:
attack_end_known_live = _known_live_utxo_fraction(sim)
if (round_idx + 1) % sample_every == 0:
recent = taker_anon_history[-recent_window:]
if recent:
recent_deanon = float(np.mean(np.asarray(recent) <= 1))
recent_mean_anon = float(np.mean(recent))
else:
recent_deanon = 0.0
recent_mean_anon = 0.0
timeline.append(
{
"round": round_idx + 1,
"phase": phase,
"known_live_utxo_fraction": _known_live_utxo_fraction(sim),
"recent_taker_deanon_fraction": recent_deanon,
"recent_mean_taker_anon": recent_mean_anon,
"cumulative_probing_cost_sats": sim._total_probing_cost_sats,
}
)
# Recovery milestones from attack->recovery boundary onward
known_live_recovery_round: int | None = None
deanon_recovery_round: int | None = None
for item in timeline:
if int(item["round"]) <= attack_rounds:
continue
if known_live_recovery_round is None and float(item["known_live_utxo_fraction"]) <= 0.10:
known_live_recovery_round = int(item["round"])
if deanon_recovery_round is None and float(item["recent_taker_deanon_fraction"]) <= 0.05:
deanon_recovery_round = int(item["round"])
attack_deanon_fraction = (
float(np.mean(np.asarray(attack_anon_history) <= 1)) if attack_anon_history else None
)
attack_mean_anon = float(np.mean(attack_anon_history)) if attack_anon_history else None
first_120 = recovery_anon_history[:120]
post120_deanon = float(np.mean(np.asarray(first_120) <= 1)) if first_120 else None
post120_mean_anon = float(np.mean(first_120)) if first_120 else None
return {
"attack_rounds": attack_rounds,
"recovery_rounds": recovery_rounds,
"attack_evil_fraction": attack_evil_fraction,
"sample_every": sample_every,
"timeline": timeline,
"recovery_round_known_live_le_10pct": known_live_recovery_round,
"recovery_round_recent_deanon_le_5pct": deanon_recovery_round,
"attack_probe_actions": attack_probe_actions,
"attack_probed_utxos": attack_probed_utxos,
"upfront_probe_actions": sim._preprobe_actions,
"upfront_probed_utxos": sim._preprobe_utxos,
"attack_end_known_live_utxo_fraction": attack_end_known_live,
"attack_honest_cj_count": len(attack_anon_history),
"attack_deanon_fraction": attack_deanon_fraction,
"attack_mean_anon": attack_mean_anon,
"post_attack_first120_honest_deanon": post120_deanon,
"post_attack_first120_honest_mean_anon": post120_mean_anon,
"upfront_fee_cost_sats": sim._preprobe_actions * config.initiation_fee_sats,
"attack_fee_cost_sats": attack_probe_actions * config.initiation_fee_sats,
"total_fee_cost_sats": sim._total_probing_cost_sats,
}
def _baseline_policy(rounds: int, fee_sats: int, seed: int) -> NetworkSimulationConfig:
return NetworkSimulationConfig(
n_makers=100,
n_rounds=rounds,
n_makers_per_coinjoin=8,
n_mixdepths=5,
pre_probe_all_makers=True,
wallet_init_mode="seeded_depth0",
initiation_fee_sats=fee_sats,
random_seed=seed,
)
def _recommended_policy(rounds: int, fee_sats: int, seed: int) -> NetworkSimulationConfig:
return NetworkSimulationConfig.recommended_policy_defaults(
n_makers=100,
n_rounds=rounds,
pre_probe_all_makers=True,
initiation_fee_sats=fee_sats,
random_seed=seed,
)
def main() -> None:
snapshot = _load_snapshot()
profiles = extract_bonded_maker_profiles(snapshot)
sustained_rounds = 5000
sustained_evil = [0.1, 0.2, 0.4, 0.6]
fee_levels = [0, 500]
n_seeds_sustained = 5
sustained_rows: list[dict[str, object]] = []
seed_offset = 0
for policy_name, policy_builder in (
("baseline", _baseline_policy),
("recommended", _recommended_policy),
):
for fee in fee_levels:
for evil in sustained_evil:
for seed_idx in range(n_seeds_sustained):
cfg = policy_builder(sustained_rounds, fee, 100 + seed_idx * 17)
cfg = replace(cfg, evil_taker_fraction=evil)
row = _run_single(cfg, profiles, seed_offset)
row["policy_name"] = policy_name
row["scenario"] = "sustained_attack"
row["seed_index"] = seed_idx
sustained_rows.append(row)
seed_offset += 1
# Impact threshold: first evil fraction where mean sustained deanon >= 30%
threshold_rows: list[dict[str, object]] = []
for policy_name in ("baseline", "recommended"):
for fee in fee_levels:
subset = [
r
for r in sustained_rows
if r["policy_name"] == policy_name and int(r["initiation_fee_sats"]) == fee
]
# Aggregate seeds per evil fraction (mean deanon)
by_evil: dict[float, list[dict[str, object]]] = {}
for r in subset:
evil = float(r["evil_taker_fraction"])
by_evil.setdefault(evil, []).append(r)
evil_summaries: list[dict[str, object]] = []
for evil in sorted(by_evil):
rs = by_evil[evil]
mean_deanon = sum(float(r["taker_deanonymized_fraction"]) for r in rs) / len(rs)
evil_summaries.append(
{
"evil_taker_fraction": evil,
"mean_deanon": mean_deanon,
"rep_row": rs[0],
}
)
crossing = next(
(s for s in evil_summaries if float(s["mean_deanon"]) >= 0.30),
None,
)
crossing_row = crossing["rep_row"] if crossing is not None else None
threshold_rows.append(
{
"policy_name": policy_name,
"initiation_fee_sats": fee,
"deanon_threshold": 0.30,
"crossing_evil_fraction": (
None if crossing is None else float(crossing["evil_taker_fraction"])
),
"crossing_total_probing_cost_sats": (
None
if crossing_row is None
else int(crossing_row["total_probing_cost_sats"])
),
"crossing_cost_to_volume_ratio": (
None
if crossing_row is None
else float(crossing_row["probing_cost_to_volume_ratio"])
),
}
)
# Sensitivity to initial UTXO count and total funds
sensitivity_rows: list[dict[str, object]] = []
sensitivity_specs = [
{
"name": "few_utxos_low_funds",
"wallet_init_mode": "seeded_depth0",
"seed_depth0_min_initial_utxos": 1,
"seed_depth0_max_initial_utxos": 2,
"total_balance_ratio_mean": 3.6,
"total_balance_ratio_cap": 3.9,
},
{
"name": "few_utxos_high_funds",
"wallet_init_mode": "seeded_depth0",
"seed_depth0_min_initial_utxos": 1,
"seed_depth0_max_initial_utxos": 2,
"total_balance_ratio_mean": 4.8,
"total_balance_ratio_cap": 5.2,
},
{
"name": "many_utxos_low_funds",
"wallet_init_mode": "distributed",
"total_balance_ratio_mean": 3.6,
"total_balance_ratio_cap": 3.9,
},
{
"name": "many_utxos_high_funds",
"wallet_init_mode": "distributed",
"total_balance_ratio_mean": 4.8,
"total_balance_ratio_cap": 5.2,
},
]
for spec in sensitivity_specs:
for fee in fee_levels:
cfg_kwargs = {k: v for k, v in spec.items() if k != "name"}
cfg = NetworkSimulationConfig.recommended_policy_defaults(
n_makers=100,
n_rounds=3000,
pre_probe_all_makers=True,
initiation_fee_sats=fee,
evil_taker_fraction=0.4,
probes_per_evil_taker=5,
random_seed=600,
**cfg_kwargs,
)
row = _run_single(cfg, profiles, seed_offset)
row["scenario"] = "utxo_funds_sensitivity"
row["sensitivity_name"] = spec["name"]
sensitivity_rows.append(row)
seed_offset += 1
# Recovery study after attack pulse
recovery_runs: list[dict[str, object]] = []
for policy_name, policy_builder in (
("baseline", _baseline_policy),
("recommended", _recommended_policy),
):
for fee in fee_levels:
for attack_evil in (0.4, 0.6):
cfg = policy_builder(rounds=1, fee_sats=fee, seed=700)
cfg = replace(cfg, evil_taker_fraction=attack_evil)
timeline = _run_recovery_timeline(
config=cfg,
profiles=profiles,
attack_rounds=1200,
recovery_rounds=3200,
attack_evil_fraction=attack_evil,
sample_every=50,
)
timeline["policy_name"] = policy_name
timeline["initiation_fee_sats"] = fee
recovery_runs.append(timeline)
# Extreme sustained attack pressure (evil close to 1.0)
extreme_rows: list[dict[str, object]] = []
for policy_name, policy_builder in (
("baseline", _baseline_policy),
("recommended", _recommended_policy),
):
for fee in fee_levels:
for attack_evil in (0.9, 1.0):
cfg = policy_builder(rounds=1, fee_sats=fee, seed=800)
cfg = replace(cfg, evil_taker_fraction=attack_evil)
item = _run_recovery_timeline(
config=cfg,
profiles=profiles,
attack_rounds=1800,
recovery_rounds=4200,
attack_evil_fraction=attack_evil,
sample_every=25,
)
item["policy_name"] = policy_name
item["initiation_fee_sats"] = fee
extreme_rows.append(item)
payload = {
"orderbook_url": DEFAULT_ORDERBOOK_URL,
"n_bonded_profiles": len(profiles),
"recommended_defaults": asdict(NetworkSimulationConfig.recommended_policy_defaults()),
"sustained_attack_results": sustained_rows,
"substantial_impact_thresholds": threshold_rows,
"utxo_funds_sensitivity_results": sensitivity_rows,
"recovery_timelines": recovery_runs,
"extreme_attack_results": extreme_rows,
}
with open("longrun_policy_results.json", "w") as f:
json.dump(payload, f, indent=2, default=str)
print("Wrote longrun_policy_results.json")
if __name__ == "__main__":
main()