-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
246 lines (189 loc) · 7.22 KB
/
api.py
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
"""
This file contains the code of the API / Server.
It reads and updates the sprig instances in the data/ folder.
Environment variables:
- DATA: the path to the folder where the sprig instances are stored.
- DEV: set to true if run on localhost (allow cross-origin requests & move the API to / instead of /api).
"""
import os
from pathlib import Path
import traceback
from typing import Any, Iterator
from pprint import pprint
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
# os.environ["BANK_FILE"] = str((Path(__file__).parent / "data" / "api_bank").absolute())
import sprig
import utils
USE_BLOCKCHAIN = True
DEV = os.environ.get("DEV", "").lower() in ("true", "1", "yes", "y'")
DATA = Path(os.environ.get("DATA", str(Path(__file__).parent / "data")))
ROOT_PATH = "/api" if not DEV else ""
api = FastAPI(root_path=ROOT_PATH)
if not DATA.exists():
DATA.mkdir(parents=True)
if DEV:
# This allows cross-origin requests.
# It is needed in development because the frontend dev server is not the same as the backend.
api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
# allow_credentials=True,
allow_methods=["*"],
# allow_headers=["*"],
)
else:
api.add_middleware(
CORSMiddleware,
# Not sure if this is precisely what we need, but it seems to work.
# allow_origin_regex=r"https://(.*\.)?sprig\.therandom\.space",
allow_origins=["*"],
# allow_credentials=True,
allow_methods=["*"],
# allow_headers=["*"],
)
def all_instances_filenames() -> Iterator[Path]:
"""Yield all the filenames of sprig instances stored on disk."""
for file in DATA.glob(r"*.json"):
if file.stem != "users":
yield file
def path_from_hash(hash_: str) -> Path:
"""Return the path where the sprig instance corresponding to the hash is stored."""
assert hash_.isnumeric(), "Contract hash must be numeric."
return DATA / f"{hash_}.json"
def save(instance: sprig.Sprig) -> None:
"""Save a SPRIG instance on disk."""
path_from_hash(instance.root_hash).write_text(instance.dumps())
def load(instance_hash: str) -> sprig.Sprig:
"""Load a prig instance from disk."""
return sprig.Sprig.loads(path_from_hash(instance_hash).read_text(), blockchain=USE_BLOCKCHAIN)
@api.exception_handler(AssertionError)
async def unicorn_exception_handler(request: Request, exc: AssertionError) -> JSONResponse:
if len(exc.args) > 0:
detail = exc.args[0]
else:
detail = "Unkown error, please report this."
traceback.print_tb(exc.__traceback__)
return JSONResponse(
status_code=400,
content={"detail": detail},
)
class ParameterData(BaseModel):
root_height: int
max_length: int
time_for_questions: int
time_for_answers: int
upstakes: list[float]
downstakes: list[float]
question_bounties: list[float]
verification_cost: float
class SprigData(BaseModel):
hash: sprig.Hash
language: str
params: ParameterData
proofs: dict[sprig.Hash, sprig.ProofAttempt]
challenges: dict[sprig.Hash, sprig.Challenge]
root_question: sprig.Hash
root_hash: sprig.Hash
class Config:
arbitrary_types_allowed = True
orm_mode = True
@api.get("/everything", response_model=dict[sprig.Hash, SprigData])
def get_everything() -> Any:
"""Get all the data of all the SPRIG instances."""
everything = {}
for file in all_instances_filenames():
instance = load(file.stem)
with sprig.time_mode('real'):
instance.distribute_all_bets()
save(instance)
data = instance.dump_as_dict()
# Note: data already has a key "root_hash" of the same value, which
# was added later, but we want to keep the old key "hash" for
# compatibility.
data['hash'] = file.stem
everything[file.stem] = data
return everything
class SprigInitData(BaseModel):
language: str
params: ParameterData # I would like to use Parameters, but it doesn't work...
author: sprig.Address
root_claim: str
proof: str
contract: str
created_at: sprig.Time
@api.post("/instances", response_model=SprigData)
def add_new_instance(new_instance: SprigInitData) -> dict[str, Any]: # SprigData:
"""
Start a new instance of the sprig protocol.
The language is the name of the desired language.
"""
print("Creating new instance...", DATA)
pprint(new_instance.dict())
with sprig.time_mode('real'):
if not USE_BLOCKCHAIN:
parameters = sprig.Parameters(**new_instance.params.dict())
else: # We always use the blockchain parameters, except for testing.
parameters = sprig.ParametersBlockchain(**new_instance.params.dict())
instance = sprig.Sprig.start(
new_instance.language,
parameters,
new_instance.author,
new_instance.root_claim,
new_instance.proof,
sprig.Hash(new_instance.contract),
new_instance.created_at,
)
save(instance)
data = instance.dump_as_dict()
data['hash'] = instance.root_hash
return data
@api.get("/users", response_model=dict[sprig.Address, int])
def get_users() -> dict[sprig.Address, int]:
"""Return a mapping User -> Account Balance."""
return sprig.BANK
class ChallengeCreatedData(BaseModel):
balance: int
challenge: sprig.Challenge
parent: sprig.ProofAttempt
@api.post(
"/challenge/{instance_hash}/{claim_hash}"
) #, response_model=ChallengeCreatedData) # The response_model is not working and I don't know why.
def new_challenge(skeptic: sprig.Address, claim_hash: sprig.Hash, instance_hash: sprig.Hash,
contract: str, created_at: sprig.Time) -> ChallengeCreatedData:
"""
Challenge a claim that isn't yet challenged and still active.
created_at: The time at which the challenge was created, in milliseconds since the epoch.
"""
instance = load(instance_hash)
with sprig.time_mode('real'):
challenge = instance.challenge(skeptic, claim_hash, contract, created_at)
save(instance)
challenge = instance.challenges[claim_hash]
return ChallengeCreatedData(
balance=sprig.BANK[skeptic],
challenge=challenge,
parent=instance.proofs[challenge.parent],
)
class NewProofAttemptData(BaseModel):
contract: str
statement: str
author: sprig.Address
machine_level: bool
created_at: sprig.Time
@api.post("/proof/{instance_hash}/{challenge_hash}")
def new_proof_attempt(instance_hash: sprig.Hash, challenge_hash: sprig.Hash,
attempt_data: NewProofAttemptData) -> sprig.ProofAttempt:
"""Create a new proof attempt to answer a challenge."""
instance = load(instance_hash)
with sprig.time_mode('real'):
if attempt_data.machine_level:
method = instance.answer_low_level
else:
method = instance.answer
attempt = method(challenge_hash, attempt_data.author, attempt_data.statement,
attempt_data.contract, attempt_data.created_at)
save(instance)
return attempt