-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathledger.py
More file actions
1395 lines (1235 loc) · 54.5 KB
/
ledger.py
File metadata and controls
1395 lines (1235 loc) · 54.5 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
"""
MIT License
Copyright (c) 2024 MANTIS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import asyncio, json, logging, os, hashlib, sqlite3, time
import requests
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Dict, List, Any
import numpy as np, aiohttp, bittensor as bt
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from timelock import Timelock
import config
from range_breakout import RangeBreakoutTracker
logger = logging.getLogger(__name__)
SAMPLE_EVERY = config.SAMPLE_EVERY
DRAND_SIGNATURE_RETRIES = 3
DRAND_SIGNATURE_RETRY_DELAY = 1.0
# Storage dim for MULTIBREAKOUT: 2 floats per asset, flattened across all BREAKOUT_ASSETS.
# config.MULTI_BREAKOUT_CHALLENGE["dim"] stays 2 (the per-asset dimension) but storage
# needs the full vector so per-asset predictions survive the pack/unpack round-trip.
_MB_STORAGE_DIM = 2 * len(config.BREAKOUT_ASSETS)
def _get_storage_dim(ticker: str) -> int:
spec = config.CHALLENGE_MAP.get(ticker)
if not spec:
return config.ASSET_EMBEDDING_DIMS.get(ticker, 0)
assets = spec.get("assets")
if assets:
return spec["dim"] * len(assets)
return spec["dim"]
_SCHEMA_SQL = """
CREATE TABLE IF NOT EXISTS blocks (
idx INTEGER PRIMARY KEY,
block INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS challenge_meta (
ticker TEXT PRIMARY KEY,
dim INTEGER NOT NULL,
blocks_ahead INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS challenge_data (
ticker TEXT NOT NULL,
sidx INTEGER NOT NULL,
price REAL,
price_data TEXT,
hotkeys TEXT,
embeddings BLOB,
PRIMARY KEY (ticker, sidx)
);
CREATE TABLE IF NOT EXISTS raw_payloads (
ts INTEGER NOT NULL,
hotkey TEXT NOT NULL,
payload BLOB,
PRIMARY KEY (ts, hotkey)
);
CREATE TABLE IF NOT EXISTS drand_cache (
round INTEGER PRIMARY KEY,
signature BLOB
);
CREATE TABLE IF NOT EXISTS breakout_state (
asset TEXT PRIMARY KEY,
state_json TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_blocks_block ON blocks(block);
"""
def _ensure_price_data_col(conn):
cols = {r[1] for r in conn.execute("PRAGMA table_info(challenge_data)")}
if "price_data" not in cols:
conn.execute("ALTER TABLE challenge_data ADD COLUMN price_data TEXT")
conn.commit()
def _pack_embeddings(emb: Dict[str, np.ndarray]) -> bytes:
if not emb:
return b""
hk_list = sorted(emb.keys())
vecs = np.array([np.asarray(emb[hk], dtype=np.float16) for hk in hk_list], dtype=np.float16)
return json.dumps(hk_list).encode() + b"\x00" + vecs.tobytes()
def _unpack_embeddings(blob: bytes, dim: int) -> Dict[str, np.ndarray]:
if not blob:
return {}
sep = blob.index(b"\x00")
hk_list = json.loads(blob[:sep].decode())
raw = blob[sep + 1:]
n = len(hk_list)
if n == 0 or len(raw) != n * dim * 2:
return {}
vecs = np.frombuffer(raw, dtype=np.float16).reshape(n, dim)
return {hk: vecs[i].copy() for i, hk in enumerate(hk_list)}
def ensure_datalog(path: str) -> str:
if os.path.exists(path):
return path
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
url = config.DATALOG_ARCHIVE_URL
r = requests.get(url, timeout=1500, stream=True)
if r.status_code == 200:
tmp = path + ".tmp"
with open(tmp, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
os.replace(tmp, path)
return path
raise SystemExit(f"Failed to download datalog from {url}")
def _sha256(*parts: bytes) -> bytes:
h = hashlib.sha256()
for part in parts:
h.update(part)
return h.digest()
def _hkdf_key_nonce(shared_secret: bytes, info: bytes = b"mantis-owner-wrap", key_len: int = 32, nonce_len: int = 12):
out = HKDF(algorithm=hashes.SHA256(), length=key_len + nonce_len, salt=None, info=info).derive(shared_secret)
return out[:key_len], out[key_len:]
def _binding(hk: str, rnd: int, owner_pk: bytes, pke: bytes) -> bytes:
return _sha256(hk.encode("utf-8"), b":", str(rnd).encode("ascii"), b":", owner_pk, b":", pke)
def _derive_pke(ske_raw: bytes) -> bytes:
return X25519PrivateKey.from_private_bytes(ske_raw).public_key().public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw,
)
def _convert_tlock_ct(ct_bytes: bytes) -> bytes:
"""Convert old ark-serialize 0.4.x TLECiphertext format (372 bytes) to 0.5.x (356 bytes).
In ark-serialize 0.4, the IBECiphertext's [u8; 32] fields (v, w) were
serialized with u64 length prefixes. In 0.5, fixed-size arrays are
written directly without length prefixes. This strips the two 8-byte
prefixes so the current timelock_wasm_wrapper can deserialize them.
"""
import struct
if len(ct_bytes) != 372:
return ct_bytes
v_len = struct.unpack_from('<Q', ct_bytes, 96)[0]
w_len = struct.unpack_from('<Q', ct_bytes, 96 + 8 + 32)[0]
if v_len != 32 or w_len != 32:
return ct_bytes
u = ct_bytes[0:96]
v = ct_bytes[104:136]
w = ct_bytes[144:176]
rest = ct_bytes[176:]
return u + v + w + rest
def _decrypt_v2_payload(payload: dict, sig: bytes | None, tlock: Timelock) -> bytes | None:
try:
if not sig:
return None
configured_owner_pk_hex = getattr(config, "OWNER_HPKE_PUBLIC_KEY_HEX", "").strip()
if not configured_owner_pk_hex:
return None
payload_owner_pk_hex = payload.get("owner_pk")
if isinstance(payload_owner_pk_hex, str) and payload_owner_pk_hex.lower() != configured_owner_pk_hex.lower():
return None
owner_pk = bytes.fromhex(configured_owner_pk_hex)
pke = bytes.fromhex(payload["W_owner"]["pke"])
binding = _binding(payload["hk"], int(payload["round"]), owner_pk, pke)
if binding != bytes.fromhex(payload["binding"]):
return None
ct_bytes = _convert_tlock_ct(bytes.fromhex(payload["W_time"]["ct"]))
skeK_raw = tlock.tld(ct_bytes, sig)
if isinstance(skeK_raw, str):
try:
skeK = bytes.fromhex(skeK_raw)
except ValueError:
skeK = skeK_raw.encode("utf-8")
else:
skeK = bytes(skeK_raw)
if len(skeK) == 128:
try:
skeK = bytes.fromhex(skeK.decode("ascii"))
except (UnicodeDecodeError, ValueError):
pass
if len(skeK) != 64:
return None
ske, key = skeK[:32], skeK[32:]
if _derive_pke(ske) != pke:
return None
shared = X25519PrivateKey.from_private_bytes(ske).exchange(X25519PublicKey.from_public_bytes(owner_pk))
k1, _ = _hkdf_key_nonce(shared, info=b"mantis-owner-wrap")
nonce = bytes.fromhex(payload["W_owner"]["nonce"])
wrapped = ChaCha20Poly1305(k1).decrypt(nonce, bytes.fromhex(payload["W_owner"]["ct"]), binding)
if wrapped != key:
return None
return ChaCha20Poly1305(key).decrypt(
bytes.fromhex(payload["C"]["nonce"]),
bytes.fromhex(payload["C"]["ct"]),
binding,
)
except Exception:
return None
@dataclass
class ChallengeData:
dim: int
blocks_ahead: int = 0
sidx: Dict[int, Dict[str, Any]] = field(default_factory=dict)
def set_price(self, sidx: int, price: float):
d = self.sidx.setdefault(sidx, {"hotkeys": [], "price": None, "emb": {}})
d["price"] = float(price)
def set_emb(self, sidx: int, hk: str, vec: List[float]):
d = self.sidx.setdefault(sidx, {"hotkeys": [], "price": None, "emb": {}})
d["emb"][hk] = np.array(vec, dtype=np.float16)
if hk not in d["hotkeys"]:
d["hotkeys"].append(hk)
class DataLog:
def __init__(self, db_path: str):
self._db_path = db_path
os.makedirs(os.path.dirname(db_path) or ".", exist_ok=True)
self._conn = sqlite3.connect(db_path, check_same_thread=False)
self._conn.execute("PRAGMA journal_mode=WAL")
self._conn.execute("PRAGMA synchronous=NORMAL")
self._conn.executescript(_SCHEMA_SQL)
_ensure_price_data_col(self._conn)
for spec in config.CHALLENGES:
self._conn.execute(
"INSERT OR REPLACE INTO challenge_meta (ticker, dim, blocks_ahead) VALUES (?, ?, ?)",
(spec["ticker"], spec["dim"], spec.get("blocks_ahead", 0)),
)
self._conn.commit()
self.tlock = Timelock(config.DRAND_PUBLIC_KEY)
self._lock = asyncio.Lock()
self._drand_cache: Dict[int, bytes] = {}
self._DRAND_MEM_CAP = 10_000
self._block_count: int = self._conn.execute("SELECT COUNT(*) FROM blocks").fetchone()[0]
self._breakout_trackers: Dict[str, RangeBreakoutTracker] = {}
self._init_breakout_trackers()
self._load_breakout_state()
try:
self._drand_db_count: int = self._conn.execute("SELECT COUNT(*) FROM drand_cache").fetchone()[0]
except sqlite3.DatabaseError as e:
logger.warning(f"drand_cache table seems corrupted: {e}")
self._drand_db_count = 0
logger.info(
"Opened live SQLite datalog: %s (%d blocks, drand_in_db=%d)",
db_path, self._block_count, self._drand_db_count,
)
def _init_breakout_trackers(self):
mb = config.CHALLENGE_MAP.get("MULTIBREAKOUT")
if not mb:
return
for asset in mb.get("assets", []):
if asset not in self._breakout_trackers:
self._breakout_trackers[asset] = RangeBreakoutTracker(
ticker=asset,
range_lookback_blocks=mb.get("range_lookback_blocks", 7200),
barrier_pct=mb.get("barrier_pct", 10.0),
min_range_pct=mb.get("min_range_pct", 1.0),
)
def _load_breakout_state(self):
tables = {r[0] for r in self._conn.execute(
"SELECT name FROM sqlite_master WHERE type='table'"
)}
if "breakout_state" not in tables:
return
mb = config.CHALLENGE_MAP.get("MULTIBREAKOUT")
if not mb:
return
for asset, state_json in self._conn.execute("SELECT asset, state_json FROM breakout_state"):
if asset in self._breakout_trackers:
state = json.loads(state_json)
restored = RangeBreakoutTracker.from_dict(state)
restored.range_lookback_blocks = mb.get("range_lookback_blocks", restored.range_lookback_blocks)
restored.barrier_pct = mb.get("barrier_pct", restored.barrier_pct)
restored.min_range_pct = mb.get("min_range_pct", restored.min_range_pct)
restored.max_pending_blocks = mb.get("max_pending_blocks", restored.max_pending_blocks)
self._breakout_trackers[asset] = restored
@property
def block_count(self) -> int:
return self._block_count
@staticmethod
def load(path: str) -> "DataLog":
if os.path.exists(path):
return DataLog(path)
db_path = path if path.endswith(".db") else os.path.splitext(path)[0] + ".db"
return DataLog(db_path)
async def append_step(self, block: int, prices: Dict[str, float], payloads: Dict[str, bytes], mg: "bt.Metagraph"):
async with self._lock:
c = self._conn.cursor()
idx = self._block_count
c.execute("INSERT INTO blocks (idx, block) VALUES (?, ?)", (idx, block))
self._block_count += 1
sidx = block // SAMPLE_EVERY
for spec in config.CHALLENGES:
ticker = spec["ticker"]
if ticker == "MULTIXSEC":
pd_map = {}
for a in config.BREAKOUT_ASSETS:
pv = prices.get(a)
if isinstance(pv, (int, float)) and pv > 0:
pd_map[a] = float(pv)
if pd_map:
c.execute(
"INSERT INTO challenge_data (ticker, sidx, price_data, hotkeys, embeddings) "
"VALUES (?, ?, ?, '[]', X'') "
"ON CONFLICT(ticker, sidx) DO UPDATE SET price_data=excluded.price_data",
(ticker, sidx, json.dumps(pd_map)),
)
continue
if ticker == "FUNDINGXSEC":
funding_rates = prices.get("_funding_rates", {})
if not isinstance(funding_rates, dict):
funding_rates = {}
fd_map = {}
for a in config.FUNDING_ASSETS:
fr = funding_rates.get(a)
if isinstance(fr, (int, float)):
fd_map[a] = float(fr)
if fd_map:
c.execute(
"INSERT INTO challenge_data (ticker, sidx, price_data, hotkeys, embeddings) "
"VALUES (?, ?, ?, '[]', X'') "
"ON CONFLICT(ticker, sidx) DO UPDATE SET price_data=excluded.price_data",
(ticker, sidx, json.dumps(fd_map)),
)
continue
p = prices.get(ticker)
if p is not None:
c.execute(
"INSERT INTO challenge_data (ticker, sidx, price, hotkeys, embeddings) "
"VALUES (?, ?, ?, '[]', X'') "
"ON CONFLICT(ticker, sidx) DO UPDATE SET price=excluded.price",
(ticker, sidx, float(p)),
)
for hk in mg.hotkeys:
ct = payloads.get(hk)
raw = json.dumps(ct).encode() if ct else b"{}"
c.execute(
"INSERT OR REPLACE INTO raw_payloads (ts, hotkey, payload) VALUES (?, ?, ?)",
(idx, hk, raw),
)
self._conn.commit()
self._update_breakout_trackers(sidx, block, prices)
def _flush_breakout_state(self):
if not self._breakout_trackers:
return
c = self._conn.cursor()
c.execute("DELETE FROM breakout_state")
for asset, tracker in self._breakout_trackers.items():
c.execute(
"INSERT INTO breakout_state (asset, state_json) VALUES (?, ?)",
(asset, json.dumps(tracker.to_dict())),
)
self._conn.commit()
def _update_breakout_trackers(self, sidx: int, block: int, prices: Dict[str, float]):
if not self._breakout_trackers:
return
for asset, tracker in self._breakout_trackers.items():
p = prices.get(asset)
if not p or p <= 0:
for spec in config.CHALLENGES:
if spec.get("price_key") == asset and spec["ticker"] in prices:
p = prices[spec["ticker"]]
break
if not p or p <= 0:
continue
tracker.update_price(sidx, p)
tracker.check_trigger(sidx, block, p, {})
tracker.check_resolutions(block, p)
self._flush_breakout_state()
def _backfill_breakout_embeddings(self):
"""Attach correct embeddings to any pending or completed breakout
that still has empty embeddings, by reading from challenge_data."""
if not self._breakout_trackers:
return
dim = _MB_STORAGE_DIM
asset_indices = {asset: i for i, asset in enumerate(config.BREAKOUT_ASSETS)}
emb_cache: Dict[int, Dict[str, np.ndarray] | None] = {}
def _get_emb(sidx: int) -> Dict[str, np.ndarray]:
if sidx in emb_cache:
return emb_cache[sidx] or {}
row = self._conn.execute(
"SELECT embeddings FROM challenge_data "
"WHERE ticker='MULTIBREAKOUT' AND sidx=?",
(sidx,),
).fetchone()
result = _unpack_embeddings(row[0], dim) if row and row[0] else {}
emb_cache[sidx] = result or None
return result
def _fill(sample, asset: str, start: int):
if sample.embeddings:
return
full_emb = _get_emb(sample.trigger_sidx)
if not full_emb:
return
for hk, full_vec in full_emb.items():
if full_vec.shape[0] >= start + 2:
sample.embeddings[hk] = full_vec[start:start + 2]
if sample.embeddings:
logger.info(
"[%s] Backfilled %d miner embeddings for breakout at sidx=%d",
asset, len(sample.embeddings), sample.trigger_sidx,
)
for asset, tracker in self._breakout_trackers.items():
aidx = asset_indices.get(asset)
if aidx is None:
continue
start = aidx * 2
for pending in (tracker.pending_high, tracker.pending_low):
if pending is not None:
_fill(pending, asset, start)
for completed in tracker.completed:
_fill(completed, asset, start)
async def _get_drand_signature(self, round_num: int, session: aiohttp.ClientSession | None = None) -> bytes | None:
cached = self._drand_cache.get(round_num)
if cached:
return cached
try:
row = self._conn.execute(
"SELECT signature FROM drand_cache WHERE round=?", (round_num,)
).fetchone()
if row and row[0]:
self._drand_cache[round_num] = row[0]
return row[0]
except sqlite3.DatabaseError as e:
logger.warning(f"Failed to read from drand_cache: {e}")
url = f"{config.DRAND_API}/beacons/{config.DRAND_BEACON_ID}/rounds/{round_num}"
sig = None
try:
if session is None:
async with aiohttp.ClientSession() as sess:
async with sess.get(url, timeout=10) as resp:
if resp.status == 200:
sig = bytes.fromhex((await resp.json())["signature"])
else:
async with session.get(url, timeout=10) as resp:
if resp.status == 200:
sig = bytes.fromhex((await resp.json())["signature"])
except Exception:
pass
if not sig:
try:
resp = requests.get(url, timeout=10)
if resp.status_code == 200:
sig_hex = resp.json().get("signature", "")
if sig_hex:
sig = bytes.fromhex(sig_hex)
except Exception:
return None
if sig:
self._drand_cache[round_num] = sig
if len(self._drand_cache) > self._DRAND_MEM_CAP:
to_drop = sorted(self._drand_cache)[:len(self._drand_cache) - self._DRAND_MEM_CAP // 2]
for k in to_drop:
del self._drand_cache[k]
try:
self._conn.execute(
"INSERT OR REPLACE INTO drand_cache (round, signature) VALUES (?, ?)",
(round_num, sig),
)
self._conn.commit()
except sqlite3.DatabaseError as e:
logger.warning(f"Failed to write to drand_cache: {e}")
return sig
def _zero_vecs(self):
return {
c["ticker"]: [0.0] * _get_storage_dim(c["ticker"])
for c in config.CHALLENGES
}
def _validate_submission(self, sub: Any) -> Dict[str, List[float]]:
def _sanitize_lbfgs_vec(vec: List[float]) -> List[float]:
if not any((float(v) != 0.0) for v in vec):
return [0.0] * 17
arr = np.asarray(vec, dtype=float).copy()
if arr.shape != (17,):
return [0.0] * 17
p = np.clip(arr[0:5], 1e-6, 1.0 - 1e-6)
s = float(np.sum(p))
if s <= 0:
p[:] = 1.0 / 5.0
else:
p = p / s
q = arr[5:17]
q = np.clip(q, 1e-6, 1.0 - 1e-6)
out = np.concatenate([p, q]).astype(float)
return out.tolist()
def _flatten_multibreakout_dict(d: dict) -> List[float]:
flat: List[float] = []
for asset in config.BREAKOUT_ASSETS:
pair = d.get(asset)
if (isinstance(pair, list) and len(pair) == 2
and all(isinstance(v, (int, float)) and 0 <= v <= 1 for v in pair)):
flat.extend([float(pair[0]), float(pair[1])])
else:
flat.extend([0.0, 0.0])
return flat
def _flatten_xsec_dict(d: dict) -> List[float]:
flat: List[float] = []
for asset in config.BREAKOUT_ASSETS:
val = d.get(asset)
if isinstance(val, (int, float)) and -1 <= val <= 1:
flat.append(float(val))
elif isinstance(val, list) and len(val) == 1 and isinstance(val[0], (int, float)):
flat.append(float(np.clip(val[0], -1, 1)))
else:
flat.append(0.0)
return flat
def _flatten_funding_xsec_dict(d: dict) -> List[float]:
flat: List[float] = []
for asset in config.FUNDING_ASSETS:
val = d.get(asset)
if isinstance(val, (int, float)) and -1 <= val <= 1:
flat.append(float(val))
elif isinstance(val, list) and len(val) == 1 and isinstance(val[0], (int, float)):
flat.append(float(np.clip(val[0], -1, 1)))
else:
flat.append(0.0)
return flat
if isinstance(sub, list) and len(sub) == len(config.CHALLENGES):
out = {}
for vec, c in zip(sub, config.CHALLENGES):
ticker = c["ticker"]
dim = _get_storage_dim(ticker)
spec = config.CHALLENGE_MAP.get(ticker)
if ticker == "MULTIBREAKOUT" and isinstance(vec, dict):
out[ticker] = _flatten_multibreakout_dict(vec)
continue
if ticker == "MULTIXSEC" and isinstance(vec, dict):
out[ticker] = _flatten_xsec_dict(vec)
continue
if ticker == "FUNDINGXSEC" and isinstance(vec, dict):
out[ticker] = _flatten_funding_xsec_dict(vec)
continue
if isinstance(vec, list) and len(vec) == dim:
if spec and spec.get("loss_func") == "lbfgs":
out[ticker] = _sanitize_lbfgs_vec(vec) if dim == 17 else [0.0] * dim
else:
ok = all(isinstance(v, (int, float)) and -1 <= v <= 1 for v in vec)
out[ticker] = [float(v) for v in vec] if ok else [0.0] * dim
else:
out[ticker] = [0.0] * dim
return out
if isinstance(sub, dict):
out = self._zero_vecs()
for key, vec in sub.items():
if key == "hotkey":
continue
ticker = key if key in config.CHALLENGE_MAP else config.CHALLENGE_NAME_TO_TICKER.get(key)
if not ticker:
continue
if ticker == "MULTIBREAKOUT" and isinstance(vec, dict):
out[ticker] = _flatten_multibreakout_dict(vec)
continue
if ticker == "MULTIXSEC" and isinstance(vec, dict):
out[ticker] = _flatten_xsec_dict(vec)
continue
if ticker == "FUNDINGXSEC" and isinstance(vec, dict):
out[ticker] = _flatten_funding_xsec_dict(vec)
continue
dim = _get_storage_dim(ticker)
if not isinstance(vec, list) or len(vec) != dim:
continue
spec = config.CHALLENGE_MAP.get(ticker)
if spec and spec.get("loss_func") == "lbfgs" and dim == 17:
out[ticker] = _sanitize_lbfgs_vec(vec)
else:
if not all(isinstance(v, (int, float)) and -1 <= v <= 1 for v in vec):
continue
out[ticker] = [float(v) for v in vec]
return out
return self._zero_vecs()
async def process_pending_payloads(self):
async with self._lock:
last_row = self._conn.execute(
"SELECT block FROM blocks ORDER BY idx DESC LIMIT 1"
).fetchone()
if not last_row:
return
current_block = last_row[0]
mature_rows = self._conn.execute(
"SELECT rp.ts, rp.hotkey, rp.payload, b.block "
"FROM raw_payloads rp "
"JOIN blocks b ON rp.ts = b.idx "
"WHERE ? - b.block >= 300",
(current_block,),
).fetchall()
if not mature_rows:
return
rounds = defaultdict(list)
mature = set()
ts_to_block: Dict[int, int] = {}
stats = {
"payloads": 0, "decrypt_failures": 0,
"signature_fetch_attempts": 0, "signature_fetch_failures": 0,
"v2": 0, "v2_fail": 0, "unsupported": 0,
}
for ts, hk, raw, block in mature_rows:
ts = int(ts)
mature.add((ts, hk))
ts_to_block[ts] = block
try:
data = json.loads(raw.decode()) if raw else {}
except Exception:
data = {}
version = 2 if isinstance(data, dict) and data.get("v") == 2 else 0
if version == 2:
stats["v2"] += 1
try:
rnd_key = int(data.get("round", 0))
except (TypeError, ValueError):
rnd_key = 0
rounds[rnd_key].append((ts, hk, data, version))
else:
stats["unsupported"] += 1
if not mature:
return
dec = {}
async def _work(rnd, items, sess: aiohttp.ClientSession):
sig = None
if rnd > 0:
stats["signature_fetch_attempts"] += 1
attempts = 0
while attempts < DRAND_SIGNATURE_RETRIES and not sig:
sig = await self._get_drand_signature(rnd, sess)
if sig:
break
attempts += 1
if attempts < DRAND_SIGNATURE_RETRIES:
await asyncio.sleep(DRAND_SIGNATURE_RETRY_DELAY)
if not sig and items:
logger.warning("Failed to fetch Drand signature for round %s after %d attempts", rnd, DRAND_SIGNATURE_RETRIES)
stats["signature_fetch_failures"] += 1
for ts, hk, data, version in items:
vecs = self._zero_vecs()
if not sig:
dec.setdefault(ts, {})[hk] = vecs
continue
if version == 2:
stats["payloads"] += 1
pt_bytes = _decrypt_v2_payload(data, sig, self.tlock)
if not pt_bytes:
stats["decrypt_failures"] += 1
stats["v2_fail"] += 1
else:
try:
obj = json.loads(pt_bytes.decode("utf-8"))
if isinstance(obj, dict) and obj.get("hotkey") == hk:
vecs = self._validate_submission(obj)
except Exception:
stats["decrypt_failures"] += 1
stats["v2_fail"] += 1
dec.setdefault(ts, {})[hk] = vecs
ROUND_BATCH = 16
round_items = list(rounds.items())
async with aiohttp.ClientSession() as sess:
for i in range(0, len(round_items), ROUND_BATCH):
batch = round_items[i:i + ROUND_BATCH]
await asyncio.gather(*(_work(r, items, sess) for r, items in batch))
await asyncio.sleep(0.1)
emb_updates: Dict[tuple, Dict[str, np.ndarray]] = defaultdict(dict)
for ts, by_hk in dec.items():
block = ts_to_block.get(ts)
if block is None or block % SAMPLE_EVERY:
continue
sidx = block // SAMPLE_EVERY
for hk, vecs in by_hk.items():
for ticker, vec in vecs.items():
if any(v != 0.0 for v in vec):
emb_updates[(ticker, sidx)][hk] = np.array(vec, dtype=np.float16)
async with self._lock:
c = self._conn.cursor()
for (ticker, sidx), new_embs in emb_updates.items():
dim = _get_storage_dim(ticker)
row = c.execute(
"SELECT hotkeys, embeddings FROM challenge_data WHERE ticker=? AND sidx=?",
(ticker, sidx),
).fetchone()
if row:
existing_hks = json.loads(row[0]) if row[0] else []
existing_emb = _unpack_embeddings(row[1], dim) if row[1] else {}
else:
existing_hks = []
existing_emb = {}
existing_emb.update(new_embs)
for hk in new_embs:
if hk not in existing_hks:
existing_hks.append(hk)
hks_json = json.dumps(existing_hks)
emb_blob = _pack_embeddings(existing_emb)
c.execute(
"INSERT INTO challenge_data (ticker, sidx, price, hotkeys, embeddings) "
"VALUES (?, ?, NULL, ?, ?) "
"ON CONFLICT(ticker, sidx) DO UPDATE SET hotkeys=excluded.hotkeys, embeddings=excluded.embeddings",
(ticker, sidx, hks_json, emb_blob),
)
self._backfill_breakout_embeddings()
self._flush_breakout_state()
c.executemany(
"DELETE FROM raw_payloads WHERE ts=? AND hotkey=?",
list(mature),
)
self._conn.commit()
total_payloads = stats["payloads"]
if total_payloads > 0:
pct = 100.0 * stats["decrypt_failures"] / total_payloads
logger.info(
"Payload decryption failures: %s/%s (%.2f%%)",
stats["decrypt_failures"], total_payloads, pct,
)
version_total = stats["v2"] + stats["unsupported"]
if version_total:
v2_pct = 100.0 * stats["v2"] / version_total
unsupported_pct = 100.0 * stats["unsupported"] / version_total
v2_fail_pct = (100.0 * stats["v2_fail"] / stats["v2"]) if stats["v2"] else 0.0
logger.info(
"Payload mix (matured): V2 %d/%d (%.1f%%), unsupported %d/%d (%.1f%%); V2 failures %d/%d (%.1f%%)",
stats["v2"], version_total, v2_pct,
stats["unsupported"], version_total, unsupported_pct,
stats["v2_fail"], stats["v2"], v2_fail_pct,
)
fetch_attempts = stats["signature_fetch_attempts"]
if fetch_attempts > 0:
pct_sig = 100.0 * stats["signature_fetch_failures"] / fetch_attempts
logger.info(
"Drand signature fetch failures: %s/%s rounds (%.2f%%)",
stats["signature_fetch_failures"], fetch_attempts, pct_sig,
)
async def save(self, path: str):
t0 = time.monotonic()
async with self._lock:
self._flush_breakout_state()
self._conn.execute("PRAGMA wal_checkpoint(PASSIVE)")
elapsed = time.monotonic() - t0
logger.info(
"Full save: breakout state (%d trackers) + WAL checkpoint [%.1fs]",
len(self._breakout_trackers), elapsed,
)
@staticmethod
def iter_challenge_training_data(
db_path: str,
max_block_number: int | None = None,
active_hotkeys: set[str] | None = None,
):
conn = sqlite3.connect(db_path, check_same_thread=False)
_ensure_price_data_col(conn)
for spec in config.CHALLENGES:
ticker = spec["ticker"]
dim = int(spec["dim"])
blocks_ahead = int(spec.get("blocks_ahead", 0))
loss_func = spec.get("loss_func")
if loss_func in ("lbfgs", "hitfirst"):
payload = DataLog._build_lbfgs_from_db(
conn, ticker, dim, blocks_ahead, max_block_number,
active_hotkeys=active_hotkeys,
)
if payload:
yield ticker, payload
continue
if loss_func == "range_breakout_multi":
completed = DataLog._load_breakout_from_db(
conn, max_block_number, active_hotkeys=active_hotkeys,
)
if completed:
yield ticker, {"completed_samples": completed}
continue
if loss_func == "xsec_rank":
payload = DataLog._build_xsec_from_db(
conn, dim, blocks_ahead, max_block_number,
active_hotkeys=active_hotkeys,
)
if payload:
yield ticker, payload
continue
if loss_func == "funding_xsec":
payload = DataLog._build_funding_xsec_from_db(
conn, dim, blocks_ahead, max_block_number,
active_hotkeys=active_hotkeys,
)
if payload:
yield ticker, payload
continue
payload = DataLog._build_binary_from_db(
conn, ticker, dim, blocks_ahead, max_block_number,
active_hotkeys=active_hotkeys,
)
if payload:
yield ticker, payload
conn.close()
@staticmethod
def _collect_hotkeys(conn, ticker, active_hotkeys=None):
hks: set[str] = set()
for (hks_json,) in conn.execute(
"SELECT hotkeys FROM challenge_data WHERE ticker = ? AND hotkeys != '[]'",
(ticker,),
):
for hk in json.loads(hks_json):
hks.add(hk)
if active_hotkeys is not None:
hks &= active_hotkeys
if not hks:
return None, None
sorted_hks = sorted(hks)
return sorted_hks, {hk: i for i, hk in enumerate(sorted_hks)}
@staticmethod
def _build_lbfgs_from_db(conn, ticker, dim, blocks_ahead, max_block_number, *, active_hotkeys=None):
c = conn.cursor()
spec = config.CHALLENGE_MAP.get(ticker)
if not spec or spec.get("loss_func") not in ("lbfgs", "hitfirst"):
return None
all_hks_sorted, hk2idx = DataLog._collect_hotkeys(c, ticker, active_hotkeys)
if all_hks_sorted is None:
return None
D = dim
rows: list[np.ndarray] = []
prices: list[float] = []
sidx_list: list[int] = []
for sidx, price, emb_blob in c.execute(
"SELECT sidx, price, embeddings FROM challenge_data "
"WHERE ticker = ? ORDER BY sidx",
(ticker,),
):
block = int(sidx) * SAMPLE_EVERY
if max_block_number and block > max_block_number:
break
if price is None:
continue
pf = float(price)
if not np.isfinite(pf) or pf <= 0:
continue
emb = _unpack_embeddings(emb_blob, D) if emb_blob else {}
row = np.zeros((len(all_hks_sorted), D), dtype=np.float32)
for hk, vec in emb.items():
idx = hk2idx.get(hk)
if idx is not None:
arr = np.asarray(vec, dtype=np.float32)
if arr.shape == (D,):
row[idx] = arr
elif arr.size == D:
row[idx] = arr.reshape(D)
rows.append(row.reshape(-1))
prices.append(pf)
sidx_list.append(int(sidx))
del emb
if not rows:
return None
return {
"hist": (np.stack(rows, axis=0), hk2idx),
"price": np.asarray(prices, dtype=np.float64),
"sidx": np.asarray(sidx_list, dtype=np.int64),
"blocks_ahead": blocks_ahead,
}
@staticmethod
def _build_binary_from_db(conn, ticker, dim, blocks_ahead, max_block_number, *, active_hotkeys=None):
c = conn.cursor()
ahead = blocks_ahead // SAMPLE_EVERY
prices_by_sidx: dict[int, float] = {}
for sidx, price in c.execute(
"SELECT sidx, price FROM challenge_data WHERE ticker = ?", (ticker,),
):
if price is not None:
prices_by_sidx[int(sidx)] = float(price)
all_hks_sorted, hk2idx = DataLog._collect_hotkeys(c, ticker, active_hotkeys)
if all_hks_sorted is None:
return None
X_list: list[np.ndarray] = []
y_list: list[float] = []
prev_price = None
unchanged_streak = 0
max_unchanged = int(getattr(config, "MAX_UNCHANGED_TIMESTEPS", 0) or 0)
for sidx, price, emb_blob in c.execute(
"SELECT sidx, price, embeddings FROM challenge_data "
"WHERE ticker = ? ORDER BY sidx",
(ticker,),
):
sidx = int(sidx)
block = sidx * SAMPLE_EVERY
if max_block_number and block > max_block_number:
break
price_now = float(price) if price is not None else None
price_fut = prices_by_sidx.get(sidx + ahead)
if price_now is not None:
if prev_price is None or price_now != prev_price:
prev_price = price_now
unchanged_streak = 0
else:
unchanged_streak += 1
if max_unchanged > 0 and unchanged_streak > max_unchanged:
continue
if price_now is None or price_fut is None:
continue
if price_now <= 0.0 or price_fut <= 0.0:
continue