-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
37 lines (32 loc) · 1.09 KB
/
storage.py
File metadata and controls
37 lines (32 loc) · 1.09 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
# storage.py
import os, json
from pathlib import Path
from config import settings
BASE = Path(settings["relayer_storage_path"])
REDUNDANCY = int(settings.get("redundancy", 3))
# create directories relayer_0..relayer_{N-1}
for i in range(REDUNDANCY):
(BASE / f"relayer_{i}").mkdir(parents=True, exist_ok=True)
def store_local(payload: dict) -> str:
"""
Store payload to all local relayer_N directories and return CID.
"""
from crypto_utils import cid_from_payload
cid = cid_from_payload(payload)
for i in range(REDUNDANCY):
path = BASE / f"relayer_{i}" / f"{cid}.json"
with open(path, "w") as f:
json.dump(payload, f)
return cid
def fetch_local(cid: str):
for i in range(REDUNDANCY):
path = BASE / f"relayer_{i}" / f"{cid}.json"
if path.exists():
with open(path) as f:
return json.load(f)
return None
def store_to_path(cid: str, payload: dict, relayer_idx: int):
path = BASE / f"relayer_{relayer_idx}" / f"{cid}.json"
with open(path, "w") as f:
json.dump(payload, f)
return True