forked from PowerLoom/node-issue-report-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporting_service_entry_point.py
424 lines (366 loc) · 13 KB
/
reporting_service_entry_point.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
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
import asyncio
import json
import time
import uuid
from functools import wraps
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
import redis
from fastapi import Depends
from fastapi import FastAPI
from fastapi import Request
from fastapi import Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from redis import asyncio as aioredis
from web3 import Web3
from auth.utils.data_models import RateLimitAuthCheck
from auth.utils.data_models import UserStatusEnum
from auth.utils.helpers import inject_rate_limit_fail_response
from auth.utils.helpers import rate_limit_auth_check
from data_models import AccountIdentifier
from data_models import GenericTxnIssue
from data_models import Message
from data_models import SnapshotterIdentifier
from data_models import SnapshotterIssue
from data_models import SnapshotterPing
from data_models import SnapshotterPingResponse
from helpers.redis_keys import get_generic_txn_issues_reported_key
from helpers.redis_keys import get_snapshotter_issues_reported_key
from helpers.redis_keys import get_snapshotters_status_zset
from settings.conf import settings
from utils.default_logger import logger
from utils.rate_limiter import load_rate_limiter_scripts
from utils.redis_conn import RedisPool
service_logger = logger.bind(
service='PowerLoom|OnChainConsensus|ServiceEntry',
)
def acquire_bounded_semaphore(fn):
@wraps(fn)
async def wrapped(*args, **kwargs):
sem: asyncio.BoundedSemaphore = kwargs['semaphore']
await sem.acquire()
result = None
try:
result = await fn(*args, **kwargs)
except Exception as e:
service_logger.opt(exception=True).error(
f'Error in {fn.__name__}: {e}',
)
pass
finally:
sem.release()
return result
return wrapped
# setup CORS origins stuff
origins = ['*']
redis_lock = redis.Redis()
app = FastAPI()
app.logger = service_logger
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.middleware('http')
async def request_middleware(request: Request, call_next: Any) -> Optional[Dict]:
request_id = str(uuid.uuid4())
request.state.request_id = request_id
with service_logger.contextualize(request_id=request_id):
service_logger.info('Request started for: {}', request.url)
try:
response = await call_next(request)
except Exception as ex:
service_logger.opt(exception=True).error(f'Request failed: {ex}')
response = JSONResponse(
content={
'info':
{
'success': False,
'response': 'Internal Server Error',
},
'request_id': request_id,
}, status_code=500,
)
finally:
response.headers['X-Request-ID'] = request_id
service_logger.info('Request ended')
return response
@app.on_event('startup')
async def startup_boilerplate():
app.state.aioredis_pool = RedisPool(writer_redis_conf=settings.redis)
await app.state.aioredis_pool.populate()
app.state.reader_redis_pool = app.state.aioredis_pool.reader_redis_pool
app.state.writer_redis_pool = app.state.aioredis_pool.writer_redis_pool
app.state.rate_limit_lua_script_shas = await load_rate_limiter_scripts(app.state.writer_redis_pool)
app.state.auth = dict()
app.state.snapshotter_aliases = dict()
@app.post('/reportIssue')
async def report_issue(
request: Request,
req_parsed: SnapshotterIssue,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Report issue from a snapshotter
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
time_of_reporting = int(time.time())
req_parsed.timeOfReporting = str(time_of_reporting)
try:
req_parsed.instanceID = Web3.to_checksum_address(req_parsed.instanceID)
except ValueError:
return JSONResponse(status_code=400, content={'message': 'Invalid instanceID.'})
await request.app.state.writer_redis_pool.zadd(
name=get_snapshotter_issues_reported_key(
snapshotter_id=req_parsed.instanceID,
),
mapping={json.dumps(req_parsed.dict()): time_of_reporting},
)
# pruning expired items
await request.app.state.writer_redis_pool.zremrangebyscore(
get_snapshotter_issues_reported_key(
snapshotter_id=req_parsed.instanceID,
), 0,
int(time.time()) - (7 * 24 * 60 * 60),
)
return JSONResponse(status_code=200, content={'message': 'Reported Issue.'})
# report issues from epoch generator or force consensus
@app.post('/reportGenericTxnIssue')
async def report_generic_txn_issue(
request: Request,
req_parsed: GenericTxnIssue,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Report issue from Epoch Generator or Force Consensus
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
reporting_address = req_parsed.accountAddress
try:
reporting_address = Web3.to_checksum_address(reporting_address)
except ValueError:
return JSONResponse(status_code=400, content={'message': 'Invalid accountAddress.'})
time_of_reporting = int(time.time())
await request.app.state.writer_redis_pool.zadd(
name=get_generic_txn_issues_reported_key(
account_address=reporting_address,
),
mapping={json.dumps(req_parsed.dict()): time_of_reporting},
)
# pruning expired items
await request.app.state.writer_redis_pool.zremrangebyscore(
get_generic_txn_issues_reported_key(
account_address=reporting_address,
), 0,
int(time.time()) - (7 * 24 * 60 * 60),
)
return JSONResponse(status_code=200, content={'message': 'Reported Issue.'})
@app.post('/ping')
async def ping(
request: Request,
req_parsed: SnapshotterPing,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Ping from a snapshotter, helps in determining active/inactive snapshotters
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
try:
req_parsed.instanceID = Web3.to_checksum_address(req_parsed.instanceID)
except ValueError:
return JSONResponse(status_code=400, content={'message': 'Invalid instanceID.'})
# add/update instanceID to zset with current time as ping time
await request.app.state.writer_redis_pool.zadd(
name=get_snapshotters_status_zset(),
mapping={req_parsed.instanceID: int(time.time())},
)
return JSONResponse(
status_code=200,
content={'message': 'Ping Successful!'},
)
@app.post(
'/metrics/activeSnapshotters/{time_window}',
response_model=List[SnapshotterPingResponse],
responses={404: {'model': Message}},
)
async def get_snapshotters_status_post(
time_window: int,
request: Request,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Get snapshotters which submitted ping in time window
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
redis_conn: aioredis.Redis = request.app.state.reader_redis_pool
# get snapshotters which submitted ping in time window
active_snapshotters = await redis_conn.zrevrangebyscore(
name=get_snapshotters_status_zset(),
max=int(time.time()),
min=int(time.time()) - time_window,
withscores=True,
)
snapshotters_status = []
for snapshotter, ping_time in active_snapshotters:
snapshotters_status.append(
SnapshotterPingResponse(
instanceID=snapshotter.decode(), timeOfReporting=int(ping_time),
),
)
return snapshotters_status
@app.post(
'/metrics/inactiveSnapshotters/{time_window}',
response_model=List[SnapshotterPingResponse],
responses={404: {'model': Message}},
)
async def get_inactive_snapshotters_status_post(
time_window: int,
request: Request,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Get snapshotters which did not submit ping in time window
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
redis_conn: aioredis.Redis = request.app.state.reader_redis_pool
# get snapshotters who did not submit ping in time window
inactive_snapshotters = await redis_conn.zrevrangebyscore(
name=get_snapshotters_status_zset(),
max=int(time.time()) - time_window,
min=0,
withscores=True,
)
snapshotters_status = []
for snapshotter, ping_time in inactive_snapshotters:
snapshotters_status.append(
SnapshotterPingResponse(
instanceID=snapshotter.decode(), timeOfReporting=int(ping_time),
),
)
return snapshotters_status
@app.post(
'/metrics/issues/{time_window}',
response_model=List[SnapshotterIssue],
responses={404: {'model': Message}},
)
async def get_snapshotter_issues_post(
time_window: int,
request: Request,
req_parsed: SnapshotterIdentifier,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Get issues reported by a snapshotter in time window
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
redis_conn: aioredis.Redis = request.app.state.reader_redis_pool
snapshotter_id = req_parsed.instanceId
# create a masked version of snapshotter_id
snapshotter_id_masked = snapshotter_id[:6] + '*********************' + snapshotter_id[-6:]
issues = await redis_conn.zrevrangebyscore(
name=get_snapshotter_issues_reported_key(
snapshotter_id=snapshotter_id,
),
max=int(time.time()),
min=int(time.time()) - time_window,
withscores=False,
)
issues_reports = []
for issue in issues:
issue_parsed = SnapshotterIssue(**json.loads(issue))
issue_parsed.instanceID = snapshotter_id_masked
issues_reports.append(issue_parsed)
return issues_reports
@app.post(
'/metrics/genericTxnIssues/{time_window}',
response_model=List[GenericTxnIssue],
responses={404: {'model': Message}},
)
async def get_generic_txn_issues_post(
time_window: int,
request: Request,
req_parsed: AccountIdentifier,
response: Response,
rate_limit_auth_dep: RateLimitAuthCheck = Depends(
rate_limit_auth_check,
),
):
"""
Get generic txn issues reported by a snapshotter in time window
"""
if not (
rate_limit_auth_dep.rate_limit_passed and
rate_limit_auth_dep.authorized and
rate_limit_auth_dep.owner.active == UserStatusEnum.active
):
return inject_rate_limit_fail_response(rate_limit_auth_dep)
redis_conn: aioredis.Redis = request.app.state.reader_redis_pool
account_address = req_parsed.accountAddress
account_address_masked = account_address[:6] + '*********************' + account_address[-6:]
issues = await redis_conn.zrevrangebyscore(
name=get_generic_txn_issues_reported_key(
account_address=account_address,
),
max=int(time.time()),
min=int(time.time()) - time_window,
withscores=False,
)
issues_reports = []
for issue in issues:
issue_parsed = GenericTxnIssue(**json.loads(issue))
issue_parsed.accountAddress = account_address_masked
issues_reports.append(issue_parsed)
return issues_reports